webgl_modifier_curve_instanced.html 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. THREE.ColorManagement.enabled = false; // TODO: Confirm correct color management.
  35. const ACTION_SELECT = 1, ACTION_NONE = 0;
  36. const curveHandles = [];
  37. const mouse = new THREE.Vector2();
  38. let stats;
  39. let scene,
  40. camera,
  41. renderer,
  42. rayCaster,
  43. control,
  44. flow,
  45. action = ACTION_NONE;
  46. init();
  47. animate();
  48. function init() {
  49. scene = new THREE.Scene();
  50. camera = new THREE.PerspectiveCamera(
  51. 40,
  52. window.innerWidth / window.innerHeight,
  53. 1,
  54. 1000
  55. );
  56. camera.position.set( 2, 2, 4 );
  57. camera.lookAt( scene.position );
  58. const boxGeometry = new THREE.BoxGeometry( 0.1, 0.1, 0.1 );
  59. const boxMaterial = new THREE.MeshBasicMaterial();
  60. const curves = [[
  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. { x: - 1, y: - 0.5, z: - 1 },
  65. ],
  66. [
  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. { x: 1, y: 0.5, z: - 1 },
  71. ]].map( function ( curvePoints ) {
  72. const curveVertices = curvePoints.map( function ( handlePos ) {
  73. const handle = new THREE.Mesh( boxGeometry, boxMaterial );
  74. handle.position.copy( handlePos );
  75. curveHandles.push( handle );
  76. scene.add( handle );
  77. return handle.position;
  78. } );
  79. const curve = new THREE.CatmullRomCurve3( curveVertices );
  80. curve.curveType = 'centripetal';
  81. curve.closed = true;
  82. const points = curve.getPoints( 50 );
  83. const line = new THREE.LineLoop(
  84. new THREE.BufferGeometry().setFromPoints( points ),
  85. new THREE.LineBasicMaterial( { color: 0x00ff00 } )
  86. );
  87. scene.add( line );
  88. return {
  89. curve,
  90. line
  91. };
  92. } );
  93. //
  94. const light = new THREE.DirectionalLight( 0xffaa33 );
  95. light.position.set( - 10, 10, 10 );
  96. light.intensity = 1.0;
  97. scene.add( light );
  98. const light2 = new THREE.AmbientLight( 0x003973 );
  99. light2.intensity = 1.0;
  100. scene.add( light2 );
  101. //
  102. const loader = new FontLoader();
  103. loader.load( 'fonts/helvetiker_regular.typeface.json', function ( font ) {
  104. const geometry = new TextGeometry( 'Hello three.js!', {
  105. font: font,
  106. size: 0.2,
  107. height: 0.05,
  108. curveSegments: 12,
  109. bevelEnabled: true,
  110. bevelThickness: 0.02,
  111. bevelSize: 0.01,
  112. bevelOffset: 0,
  113. bevelSegments: 5,
  114. } );
  115. geometry.rotateX( Math.PI );
  116. const material = new THREE.MeshStandardMaterial( {
  117. color: 0x99ffff
  118. } );
  119. const numberOfInstances = 8;
  120. flow = new InstancedFlow( numberOfInstances, curves.length, geometry, material );
  121. curves.forEach( function ( { curve }, i ) {
  122. flow.updateCurve( i, curve );
  123. scene.add( flow.object3D );
  124. } );
  125. for ( let i = 0; i < numberOfInstances; i ++ ) {
  126. const curveIndex = i % curves.length;
  127. flow.setCurve( i, curveIndex );
  128. flow.moveIndividualAlongCurve( i, i * 1 / numberOfInstances );
  129. flow.object3D.setColorAt( i, new THREE.Color( 0xffffff * Math.random() ) );
  130. }
  131. } );
  132. //
  133. renderer = new THREE.WebGLRenderer( { antialias: true } );
  134. renderer.outputColorSpace = THREE.LinearSRGBColorSpace;
  135. renderer.setPixelRatio( window.devicePixelRatio );
  136. renderer.setSize( window.innerWidth, window.innerHeight );
  137. document.body.appendChild( renderer.domElement );
  138. renderer.domElement.addEventListener( 'pointerdown', onPointerDown );
  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 );
  153. }
  154. function onWindowResize() {
  155. camera.aspect = window.innerWidth / window.innerHeight;
  156. camera.updateProjectionMatrix();
  157. renderer.setSize( window.innerWidth, window.innerHeight );
  158. }
  159. function onPointerDown( event ) {
  160. action = ACTION_SELECT;
  161. mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
  162. mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
  163. }
  164. function animate() {
  165. requestAnimationFrame( animate );
  166. if ( action === ACTION_SELECT ) {
  167. rayCaster.setFromCamera( mouse, camera );
  168. action = ACTION_NONE;
  169. const intersects = rayCaster.intersectObjects( curveHandles, false );
  170. if ( intersects.length ) {
  171. const target = intersects[ 0 ].object;
  172. control.attach( target );
  173. scene.add( control );
  174. }
  175. }
  176. if ( flow ) {
  177. flow.moveAlongCurve( 0.001 );
  178. }
  179. render();
  180. }
  181. function render() {
  182. renderer.render( scene, camera );
  183. stats.update();
  184. }
  185. </script>
  186. </body>
  187. </html>