webgl_modifier_curve_instanced.html 5.6 KB

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