webgl_modifier_curve_instanced.html 6.0 KB

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