2
0

webgl_modifier_curve.html 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - curve modifier</title>
  5. <meta charset="utf-8" />
  6. <meta
  7. name="viewport"
  8. content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0"
  9. />
  10. <link type="text/css" rel="stylesheet" href="main.css" />
  11. </head>
  12. <body>
  13. <div id="info">
  14. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - curve modifier
  15. </div>
  16. <script type="module">
  17. import * as THREE from "../build/three.module.js";
  18. import { TransformControls } from "./jsm/controls/TransformControls.js";
  19. import Stats from "./jsm/libs/stats.module.js";
  20. import { Flow } from "./jsm/modifiers/CurveModifier.js";
  21. const ACTION_SELECT = 1,
  22. ACTION_NONE = 0;
  23. const curveHandles = [];
  24. const mouse = new THREE.Vector2();
  25. let stats;
  26. let scene,
  27. camera,
  28. renderer,
  29. rayCaster,
  30. control,
  31. flow,
  32. action = ACTION_NONE;
  33. init();
  34. animate();
  35. function init() {
  36. scene = new THREE.Scene();
  37. camera = new THREE.PerspectiveCamera(
  38. 40,
  39. window.innerWidth / window.innerHeight,
  40. 1,
  41. 1000
  42. );
  43. camera.position.set( 2, 2, 4 );
  44. camera.lookAt( scene.position );
  45. const initialPoints = [
  46. { x: 1, y: 0, z: - 1 },
  47. { x: 1, y: 0, z: 1 },
  48. { x: - 1, y: 0, z: 1 },
  49. { x: - 1, y: 0, z: - 1 },
  50. ];
  51. const boxGeometry = new THREE.BoxGeometry( 0.1, 0.1, 0.1 );
  52. const boxMaterial = new THREE.MeshBasicMaterial();
  53. for ( const handlePos of initialPoints ) {
  54. const handle = new THREE.Mesh( boxGeometry, boxMaterial );
  55. handle.position.copy( handlePos );
  56. curveHandles.push( handle );
  57. scene.add( handle );
  58. }
  59. const curve = new THREE.CatmullRomCurve3(
  60. curveHandles.map( ( handle ) => handle.position )
  61. );
  62. curve.curveType = "centripetal";
  63. curve.closed = true;
  64. const points = curve.getPoints( 50 );
  65. const line = new THREE.LineLoop(
  66. new THREE.BufferGeometry().setFromPoints( points ),
  67. new THREE.LineBasicMaterial( { color: 0x00ff00 } )
  68. );
  69. scene.add( line );
  70. //
  71. const light = new THREE.DirectionalLight( 0xffaa33 );
  72. light.position.set( - 10, 10, 10 );
  73. light.intensity = 1.0;
  74. scene.add( light );
  75. const light2 = new THREE.AmbientLight( 0x003973 );
  76. light2.intensity = 1.0;
  77. scene.add( light2 );
  78. //
  79. const loader = new THREE.FontLoader();
  80. loader.load( "fonts/helvetiker_regular.typeface.json", function (
  81. font
  82. ) {
  83. const geometry = new THREE.TextGeometry( "Hello three.js!", {
  84. font: font,
  85. size: 0.2,
  86. height: 0.05,
  87. curveSegments: 12,
  88. bevelEnabled: true,
  89. bevelThickness: 0.02,
  90. bevelSize: 0.01,
  91. bevelOffset: 0,
  92. bevelSegments: 5,
  93. } );
  94. geometry.rotateX( Math.PI );
  95. const material = new THREE.MeshStandardMaterial( {
  96. color: 0x99ffff
  97. } );
  98. const objectToCurve = new THREE.Mesh( geometry, material );
  99. flow = new Flow( objectToCurve );
  100. flow.updateCurve( 0, curve );
  101. scene.add( flow.object3D );
  102. } );
  103. //
  104. renderer = new THREE.WebGLRenderer( { antialias: true } );
  105. renderer.setPixelRatio( window.devicePixelRatio );
  106. renderer.setSize( window.innerWidth, window.innerHeight );
  107. document.body.appendChild( renderer.domElement );
  108. renderer.domElement.addEventListener( 'pointerdown', onPointerDown );
  109. rayCaster = new THREE.Raycaster();
  110. control = new TransformControls( camera, renderer.domElement );
  111. control.addEventListener( "dragging-changed", function ( event ) {
  112. if ( ! event.value ) {
  113. const points = curve.getPoints( 50 );
  114. line.geometry.setFromPoints( points );
  115. flow.updateCurve( 0, curve );
  116. }
  117. } );
  118. stats = new Stats();
  119. document.body.appendChild( stats.dom );
  120. window.addEventListener( "resize", onWindowResize, false );
  121. }
  122. function onWindowResize() {
  123. camera.aspect = window.innerWidth / window.innerHeight;
  124. camera.updateProjectionMatrix();
  125. renderer.setSize( window.innerWidth, window.innerHeight );
  126. }
  127. function onPointerDown( event ) {
  128. action = ACTION_SELECT;
  129. mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
  130. mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
  131. }
  132. function animate() {
  133. requestAnimationFrame( animate );
  134. if ( action === ACTION_SELECT ) {
  135. rayCaster.setFromCamera( mouse, camera );
  136. action = ACTION_NONE;
  137. const intersects = rayCaster.intersectObjects( curveHandles );
  138. if ( intersects.length ) {
  139. const target = intersects[ 0 ].object;
  140. control.attach( target );
  141. scene.add( control );
  142. }
  143. }
  144. if ( flow ) {
  145. flow.moveAlongCurve( 0.001 );
  146. }
  147. render();
  148. }
  149. function render() {
  150. renderer.render( scene, camera );
  151. stats.update();
  152. }
  153. </script>
  154. </body>
  155. </html>