webgl_modifier_curve_instanced.html 5.7 KB

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