webgl_modifier_curve.html 4.8 KB

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