webgl_modifier_curve_instanced.html 5.5 KB

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