webgl_modifier_curve_instanced.html 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 - instanced
  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 { InstancedFlow } 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 boxGeometry = new THREE.BoxGeometry( 0.1, 0.1, 0.1 );
  49. const boxMaterial = new THREE.MeshBasicMaterial( 0x99ff99 );
  50. const curves = [[
  51. { x: 1, y: - 0.5, z: - 1 },
  52. { x: 1, y: - 0.5, z: 1 },
  53. { x: - 1, y: - 0.5, z: 1 },
  54. { x: - 1, y: - 0.5, z: - 1 },
  55. ],
  56. [
  57. { x: - 1, y: 0.5, z: - 1 },
  58. { x: - 1, y: 0.5, z: 1 },
  59. { x: 1, y: 0.5, z: 1 },
  60. { x: 1, y: 0.5, z: - 1 },
  61. ]].map( function ( curvePoints ) {
  62. const curveVertices = curvePoints.map( function ( handlePos ) {
  63. const handle = new THREE.Mesh( boxGeometry, boxMaterial );
  64. handle.position.copy( handlePos );
  65. curveHandles.push( handle );
  66. scene.add( handle );
  67. return handle.position;
  68. } );
  69. const curve = new THREE.CatmullRomCurve3( curveVertices );
  70. curve.curveType = "centripetal";
  71. curve.closed = true;
  72. const points = curve.getPoints( 50 );
  73. const line = new THREE.LineLoop(
  74. new THREE.BufferGeometry().setFromPoints( points ),
  75. new THREE.LineBasicMaterial( { color: 0x00ff00 } )
  76. );
  77. scene.add( line );
  78. return {
  79. curve,
  80. line
  81. };
  82. } );
  83. //
  84. const light = new THREE.DirectionalLight( 0xffaa33 );
  85. light.position.set( - 10, 10, 10 );
  86. light.intensity = 1.0;
  87. scene.add( light );
  88. const light2 = new THREE.AmbientLight( 0x003973 );
  89. light2.intensity = 1.0;
  90. scene.add( light2 );
  91. //
  92. const loader = new THREE.FontLoader();
  93. loader.load( "fonts/helvetiker_regular.typeface.json", function (
  94. font
  95. ) {
  96. const geometry = new THREE.TextBufferGeometry( "Hello three.js!", {
  97. font: font,
  98. size: 0.2,
  99. height: 0.05,
  100. curveSegments: 12,
  101. bevelEnabled: true,
  102. bevelThickness: 0.02,
  103. bevelSize: 0.01,
  104. bevelOffset: 0,
  105. bevelSegments: 5,
  106. } );
  107. geometry.rotateX( Math.PI );
  108. const material = new THREE.MeshStandardMaterial( {
  109. color: 0x99ffff
  110. } );
  111. const numberOfInstances = 8;
  112. flow = new InstancedFlow( numberOfInstances, curves.length, geometry, material );
  113. curves.forEach( function ( { curve }, i ) {
  114. flow.updateCurve( i, curve );
  115. scene.add( flow.object3D );
  116. } );
  117. for ( let i = 0; i < numberOfInstances; i ++ ) {
  118. const curveIndex = i % curves.length;
  119. flow.setCurve( i, curveIndex );
  120. flow.moveIndividualAlongCurve( i, i * 1 / numberOfInstances );
  121. flow.object3D.setColorAt( i, new THREE.Color( 0xffffff * Math.random() ) );
  122. }
  123. } );
  124. //
  125. renderer = new THREE.WebGLRenderer( { antialias: true } );
  126. renderer.setPixelRatio( window.devicePixelRatio );
  127. renderer.setSize( window.innerWidth, window.innerHeight );
  128. document.body.appendChild( renderer.domElement );
  129. mouse = new THREE.Vector2();
  130. renderer.domElement.addEventListener(
  131. "click",
  132. function ( event ) {
  133. action = ACTION_SELECT;
  134. mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
  135. mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
  136. },
  137. false
  138. );
  139. rayCaster = new THREE.Raycaster();
  140. control = new TransformControls( camera, renderer.domElement );
  141. control.addEventListener( "dragging-changed", function ( event ) {
  142. if ( ! event.value ) {
  143. curves.forEach( function ( { curve, line }, i ) {
  144. const points = curve.getPoints( 50 );
  145. line.geometry.setFromPoints( points );
  146. flow.updateCurve( i, curve );
  147. } );
  148. }
  149. } );
  150. stats = new Stats();
  151. document.body.appendChild( stats.dom );
  152. window.addEventListener( "resize", onWindowResize, false );
  153. }
  154. function onWindowResize() {
  155. camera.aspect = window.innerWidth / window.innerHeight;
  156. camera.updateProjectionMatrix();
  157. renderer.setSize( window.innerWidth, window.innerHeight );
  158. }
  159. function animate() {
  160. requestAnimationFrame( animate );
  161. if ( action === ACTION_SELECT ) {
  162. rayCaster.setFromCamera( mouse, camera );
  163. action = ACTION_NONE;
  164. const intersects = rayCaster.intersectObjects( curveHandles );
  165. if ( intersects.length ) {
  166. const target = intersects[ 0 ].object;
  167. control.attach( target );
  168. scene.add( control );
  169. }
  170. }
  171. if ( flow ) {
  172. flow.moveAlongCurve( 0.001 );
  173. }
  174. render();
  175. }
  176. function render() {
  177. renderer.render( scene, camera );
  178. stats.update();
  179. }
  180. </script>
  181. </body>
  182. </html>