webgl_modifier_curve_instanced.html 5.5 KB

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