webgl_modifier_curve_instanced.html 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - animation - groups</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"
  15. >three.js</a
  16. >
  17. webgl - curve modifier - instanced
  18. </div>
  19. <script type="module">
  20. import * as THREE from "../build/three.module.js";
  21. import { TransformControls } from "./jsm/controls/TransformControls.js";
  22. import Stats from "./jsm/libs/stats.module.js";
  23. import { InstancedFlow } from "./jsm/modifiers/CurveModifier.js";
  24. const ACTION_SELECT = 1,
  25. ACTION_NONE = 0;
  26. const curveHandles = [];
  27. let stats, clock;
  28. let scene,
  29. camera,
  30. renderer,
  31. mixer,
  32. rayCaster,
  33. control,
  34. mouse,
  35. flow,
  36. action = ACTION_NONE;
  37. init();
  38. animate();
  39. function init() {
  40. scene = new THREE.Scene();
  41. camera = new THREE.PerspectiveCamera(
  42. 40,
  43. window.innerWidth / window.innerHeight,
  44. 1,
  45. 1000
  46. );
  47. camera.position.set( 2, 2, 4 );
  48. camera.lookAt( scene.position );
  49. const boxGeometry = new THREE.BoxGeometry( 0.1, 0.1, 0.1 );
  50. const boxMaterial = new THREE.MeshBasicMaterial( 0x99ff99 );
  51. const curves = [[
  52. { x: 1, y: - 0.5, z: - 1 },
  53. { x: 1, y: - 0.5, z: 1 },
  54. { x: - 1, y: - 0.5, z: 1 },
  55. { x: - 1, y: - 0.5, z: - 1 },
  56. ],
  57. [
  58. { x: - 1, y: 0.5, z: - 1 },
  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. ]].map( function ( curvePoints ) {
  63. const curveVertices = curvePoints.map( function ( handlePos ) {
  64. const handle = new THREE.Mesh( boxGeometry, boxMaterial );
  65. handle.position.copy( handlePos );
  66. curveHandles.push( handle );
  67. scene.add( handle );
  68. return handle.position;
  69. } );
  70. const curve = new THREE.CatmullRomCurve3( curveVertices );
  71. curve.curveType = "centripetal";
  72. curve.closed = true;
  73. const points = curve.getPoints( 50 );
  74. const line = new THREE.LineLoop(
  75. new THREE.BufferGeometry().setFromPoints( points ),
  76. new THREE.LineBasicMaterial( { color: 0x00ff00 } )
  77. );
  78. scene.add( line );
  79. return {
  80. curve,
  81. line
  82. };
  83. } );
  84. //
  85. const light = new THREE.DirectionalLight( 0xffaa33 );
  86. light.position.set( - 10, 10, 10 );
  87. light.intensity = 1.0;
  88. scene.add( light );
  89. const light2 = new THREE.AmbientLight( 0x003973 );
  90. light2.intensity = 1.0;
  91. scene.add( light2 );
  92. //
  93. const loader = new THREE.FontLoader();
  94. loader.load( "fonts/helvetiker_regular.typeface.json", function (
  95. font
  96. ) {
  97. const geometry = new THREE.TextBufferGeometry( "Hello three.js!", {
  98. font: font,
  99. size: 0.2,
  100. height: 0.05,
  101. curveSegments: 12,
  102. bevelEnabled: true,
  103. bevelThickness: 0.02,
  104. bevelSize: 0.01,
  105. bevelOffset: 0,
  106. bevelSegments: 5,
  107. } );
  108. geometry.rotateX( Math.PI );
  109. const material = new THREE.MeshStandardMaterial( {
  110. color: 0x99ffff
  111. } );
  112. const numberOfInstances = 8;
  113. flow = new InstancedFlow( numberOfInstances, curves.length, geometry, material );
  114. curves.forEach( function ( { curve }, i ) {
  115. flow.updateCurve( i, curve );
  116. scene.add( flow.object3D );
  117. } );
  118. for ( let i = 0; i < numberOfInstances; i ++ ) {
  119. const curveIndex = i % curves.length;
  120. flow.setCurve( i, curveIndex );
  121. flow.moveIndividualAlongCurve( i, i * 1 / numberOfInstances );
  122. flow.object3D.setColorAt( i, new THREE.Color( 0xffffff * Math.random() ) );
  123. }
  124. } );
  125. //
  126. renderer = new THREE.WebGLRenderer( { antialias: true } );
  127. renderer.setPixelRatio( window.devicePixelRatio );
  128. renderer.setSize( window.innerWidth, window.innerHeight );
  129. document.body.appendChild( renderer.domElement );
  130. mouse = new THREE.Vector2();
  131. renderer.domElement.addEventListener(
  132. "click",
  133. function ( event ) {
  134. action = ACTION_SELECT;
  135. mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
  136. mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
  137. },
  138. false
  139. );
  140. rayCaster = new THREE.Raycaster();
  141. control = new TransformControls( camera, renderer.domElement );
  142. control.addEventListener( "dragging-changed", function ( event ) {
  143. if ( ! event.value ) {
  144. curves.forEach( function ( { curve, line }, i ) {
  145. const points = curve.getPoints( 50 );
  146. line.geometry.setFromPoints( points );
  147. flow.updateCurve( i, curve );
  148. } );
  149. }
  150. } );
  151. stats = new Stats();
  152. document.body.appendChild( stats.dom );
  153. clock = new THREE.Clock();
  154. window.addEventListener( "resize", onWindowResize, false );
  155. }
  156. function onWindowResize() {
  157. camera.aspect = window.innerWidth / window.innerHeight;
  158. camera.updateProjectionMatrix();
  159. renderer.setSize( window.innerWidth, window.innerHeight );
  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 );
  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. const delta = clock.getDelta();
  180. if ( mixer ) {
  181. mixer.update( delta );
  182. }
  183. renderer.render( scene, camera );
  184. stats.update();
  185. }
  186. </script>
  187. </body>
  188. </html>
粤ICP备19079148号