misc_curve_modifier.html 5.0 KB

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