webgl_modifier_curve_instanced.html 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. animate();
  44. function init() {
  45. scene = new THREE.Scene();
  46. camera = new THREE.PerspectiveCamera(
  47. 40,
  48. window.innerWidth / window.innerHeight,
  49. 1,
  50. 1000
  51. );
  52. camera.position.set( 2, 2, 4 );
  53. camera.lookAt( scene.position );
  54. const boxGeometry = new THREE.BoxGeometry( 0.1, 0.1, 0.1 );
  55. const boxMaterial = new THREE.MeshBasicMaterial();
  56. const curves = [[
  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. ],
  62. [
  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. { x: 1, y: 0.5, z: - 1 },
  67. ]].map( function ( curvePoints ) {
  68. const curveVertices = curvePoints.map( function ( handlePos ) {
  69. const handle = new THREE.Mesh( boxGeometry, boxMaterial );
  70. handle.position.copy( handlePos );
  71. curveHandles.push( handle );
  72. scene.add( handle );
  73. return handle.position;
  74. } );
  75. const curve = new THREE.CatmullRomCurve3( curveVertices );
  76. curve.curveType = 'centripetal';
  77. curve.closed = true;
  78. const points = curve.getPoints( 50 );
  79. const line = new THREE.LineLoop(
  80. new THREE.BufferGeometry().setFromPoints( points ),
  81. new THREE.LineBasicMaterial( { color: 0x00ff00 } )
  82. );
  83. scene.add( line );
  84. return {
  85. curve,
  86. line
  87. };
  88. } );
  89. //
  90. const light = new THREE.DirectionalLight( 0xffaa33, 3 );
  91. light.position.set( - 10, 10, 10 );
  92. scene.add( light );
  93. const light2 = new THREE.AmbientLight( 0x003973, 3 );
  94. scene.add( light2 );
  95. //
  96. const loader = new FontLoader();
  97. loader.load( 'fonts/helvetiker_regular.typeface.json', function ( font ) {
  98. const geometry = new TextGeometry( 'Hello three.js!', {
  99. font: font,
  100. size: 0.2,
  101. height: 0.05,
  102. curveSegments: 12,
  103. bevelEnabled: true,
  104. bevelThickness: 0.02,
  105. bevelSize: 0.01,
  106. bevelOffset: 0,
  107. bevelSegments: 5,
  108. } );
  109. geometry.rotateX( Math.PI );
  110. const material = new THREE.MeshStandardMaterial( {
  111. color: 0x99ffff
  112. } );
  113. const numberOfInstances = 8;
  114. flow = new InstancedFlow( numberOfInstances, curves.length, geometry, material );
  115. curves.forEach( function ( { curve }, i ) {
  116. flow.updateCurve( i, curve );
  117. scene.add( flow.object3D );
  118. } );
  119. for ( let i = 0; i < numberOfInstances; i ++ ) {
  120. const curveIndex = i % curves.length;
  121. flow.setCurve( i, curveIndex );
  122. flow.moveIndividualAlongCurve( i, i * 1 / numberOfInstances );
  123. flow.object3D.setColorAt( i, new THREE.Color( 0xffffff * Math.random() ) );
  124. }
  125. } );
  126. //
  127. renderer = new THREE.WebGLRenderer( { antialias: true } );
  128. renderer.setPixelRatio( window.devicePixelRatio );
  129. renderer.setSize( window.innerWidth, window.innerHeight );
  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. requestAnimationFrame( animate );
  159. if ( action === ACTION_SELECT ) {
  160. rayCaster.setFromCamera( mouse, camera );
  161. action = ACTION_NONE;
  162. const intersects = rayCaster.intersectObjects( curveHandles, false );
  163. if ( intersects.length ) {
  164. const target = intersects[ 0 ].object;
  165. control.attach( target );
  166. scene.add( control );
  167. }
  168. }
  169. if ( flow ) {
  170. flow.moveAlongCurve( 0.001 );
  171. }
  172. render();
  173. }
  174. function render() {
  175. renderer.render( scene, camera );
  176. stats.update();
  177. }
  178. </script>
  179. </body>
  180. </html>