PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/examples/webgl_modifier_curve_instanced.html

https://github.com/nosy-b/three.js
HTML | 256 lines | 182 code | 72 blank | 2 comment | 0 complexity | 2b1aeea15f39e6143a8270484c1bfaf8 MD5 | raw file
  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/es-module-shims@1.3.6/dist/es-module-shims.js"></script>
  19. <script type="importmap">
  20. {
  21. "imports": {
  22. "three": "../build/three.module.js"
  23. }
  24. }
  25. </script>
  26. <script type="module">
  27. import * as THREE from 'three';
  28. import { TransformControls } from './jsm/controls/TransformControls.js';
  29. import Stats from './jsm/libs/stats.module.js';
  30. import { InstancedFlow } from './jsm/modifiers/CurveModifier.js';
  31. import { FontLoader } from './jsm/loaders/FontLoader.js';
  32. import { TextGeometry } from './jsm/geometries/TextGeometry.js';
  33. const ACTION_SELECT = 1, ACTION_NONE = 0;
  34. const curveHandles = [];
  35. const mouse = new THREE.Vector2();
  36. let stats;
  37. let scene,
  38. camera,
  39. renderer,
  40. rayCaster,
  41. control,
  42. flow,
  43. action = ACTION_NONE;
  44. init();
  45. animate();
  46. function init() {
  47. scene = new THREE.Scene();
  48. camera = new THREE.PerspectiveCamera(
  49. 40,
  50. window.innerWidth / window.innerHeight,
  51. 1,
  52. 1000
  53. );
  54. camera.position.set( 2, 2, 4 );
  55. camera.lookAt( scene.position );
  56. const boxGeometry = new THREE.BoxGeometry( 0.1, 0.1, 0.1 );
  57. const boxMaterial = new THREE.MeshBasicMaterial();
  58. const curves = [[
  59. { x: 1, y: - 0.5, z: - 1 },
  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. ],
  64. [
  65. { x: - 1, y: 0.5, z: - 1 },
  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. ]].map( function ( curvePoints ) {
  70. const curveVertices = curvePoints.map( function ( handlePos ) {
  71. const handle = new THREE.Mesh( boxGeometry, boxMaterial );
  72. handle.position.copy( handlePos );
  73. curveHandles.push( handle );
  74. scene.add( handle );
  75. return handle.position;
  76. } );
  77. const curve = new THREE.CatmullRomCurve3( curveVertices );
  78. curve.curveType = 'centripetal';
  79. curve.closed = true;
  80. const points = curve.getPoints( 50 );
  81. const line = new THREE.LineLoop(
  82. new THREE.BufferGeometry().setFromPoints( points ),
  83. new THREE.LineBasicMaterial( { color: 0x00ff00 } )
  84. );
  85. scene.add( line );
  86. return {
  87. curve,
  88. line
  89. };
  90. } );
  91. //
  92. const light = new THREE.DirectionalLight( 0xffaa33 );
  93. light.position.set( - 10, 10, 10 );
  94. light.intensity = 1.0;
  95. scene.add( light );
  96. const light2 = new THREE.AmbientLight( 0x003973 );
  97. light2.intensity = 1.0;
  98. scene.add( light2 );
  99. //
  100. const loader = new FontLoader();
  101. loader.load( 'fonts/helvetiker_regular.typeface.json', function ( font ) {
  102. const geometry = new TextGeometry( 'Hello three.js!', {
  103. font: font,
  104. size: 0.2,
  105. height: 0.05,
  106. curveSegments: 12,
  107. bevelEnabled: true,
  108. bevelThickness: 0.02,
  109. bevelSize: 0.01,
  110. bevelOffset: 0,
  111. bevelSegments: 5,
  112. } );
  113. geometry.rotateX( Math.PI );
  114. const material = new THREE.MeshStandardMaterial( {
  115. color: 0x99ffff
  116. } );
  117. const numberOfInstances = 8;
  118. flow = new InstancedFlow( numberOfInstances, curves.length, geometry, material );
  119. curves.forEach( function ( { curve }, i ) {
  120. flow.updateCurve( i, curve );
  121. scene.add( flow.object3D );
  122. } );
  123. for ( let i = 0; i < numberOfInstances; i ++ ) {
  124. const curveIndex = i % curves.length;
  125. flow.setCurve( i, curveIndex );
  126. flow.moveIndividualAlongCurve( i, i * 1 / numberOfInstances );
  127. flow.object3D.setColorAt( i, new THREE.Color( 0xffffff * Math.random() ) );
  128. }
  129. } );
  130. //
  131. renderer = new THREE.WebGLRenderer( { antialias: true } );
  132. renderer.setPixelRatio( window.devicePixelRatio );
  133. renderer.setSize( window.innerWidth, window.innerHeight );
  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>