webgl_instancing_dynamic.html 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - instancing - dynamic</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <meta property="og:title" content="three.js webgl - instancing - dynamic">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgl_instancing_dynamic.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgl_instancing_dynamic.jpg">
  11. <link type="text/css" rel="stylesheet" href="main.css">
  12. </head>
  13. <body>
  14. <div id="info">
  15. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - instancing - dynamic<br/>
  16. Based on <a href="https://oosmoxiecode.com/archive/js_webgl/cubescape/" target="_blank" rel="noopener">Cubescape</a>
  17. by <a href="https://github.com/oosmoxiecode" target="_blank" rel="noopener">oosmoxiecode</a>
  18. </div>
  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 { RoomEnvironment } from 'three/addons/environments/RoomEnvironment.js';
  30. import TWEEN from 'three/addons/libs/tween.module.js';
  31. let camera, scene, renderer, timer, mesh;
  32. const amount = 100;
  33. const count = Math.pow( amount, 2 );
  34. const dummy = new THREE.Object3D();
  35. const seeds = [];
  36. const baseColors = [];
  37. const color = new THREE.Color();
  38. const colors = [ new THREE.Color( 0x00ffff ), new THREE.Color( 0xffff00 ), new THREE.Color( 0xff00ff ) ];
  39. const animation = { t: 0 };
  40. let currentColorIndex = 0;
  41. let nextColorIndex = 1;
  42. const maxDistance = 75;
  43. const cameraTarget = new THREE.Vector3();
  44. init();
  45. function init() {
  46. renderer = new THREE.WebGLRenderer( { antialias: true } );
  47. renderer.setPixelRatio( window.devicePixelRatio );
  48. renderer.setSize( window.innerWidth, window.innerHeight );
  49. renderer.toneMapping = THREE.NeutralToneMapping;
  50. renderer.setAnimationLoop( animate );
  51. document.body.appendChild( renderer.domElement );
  52. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 100 );
  53. camera.position.set( 10, 10, 10 );
  54. camera.lookAt( 0, 0, 0 );
  55. const pmremGenerator = new THREE.PMREMGenerator( renderer );
  56. scene = new THREE.Scene();
  57. scene.background = new THREE.Color( 0xadd8e6 );
  58. scene.environment = pmremGenerator.fromScene( new RoomEnvironment(), 0.04 ).texture;
  59. timer = new THREE.Timer();
  60. timer.connect( document );
  61. const loader = new THREE.TextureLoader();
  62. const texture = loader.load( 'textures/edge3.jpg' );
  63. texture.colorSpace = THREE.SRGBColorSpace;
  64. const geometry = new THREE.BoxGeometry();
  65. const material = new THREE.MeshStandardMaterial( { map: texture } );
  66. mesh = new THREE.InstancedMesh( geometry, material, count );
  67. mesh.instanceMatrix.setUsage( THREE.DynamicDrawUsage ); // will be updated every frame
  68. scene.add( mesh );
  69. let i = 0;
  70. const offset = ( amount - 1 ) / 2;
  71. for ( let x = 0; x < amount; x ++ ) {
  72. for ( let z = 0; z < amount; z ++ ) {
  73. dummy.position.set( offset - x, 0, offset - z );
  74. dummy.scale.set( 1, 2, 1 );
  75. dummy.updateMatrix();
  76. color.setHSL( 1, 0.5 + ( Math.random() * 0.5 ), 0.5 + ( Math.random() * 0.5 ) );
  77. baseColors.push( color.getHex() );
  78. mesh.setMatrixAt( i, dummy.matrix );
  79. mesh.setColorAt( i, color.multiply( colors[ 0 ] ) );
  80. i ++;
  81. seeds.push( Math.random() );
  82. }
  83. }
  84. //
  85. window.addEventListener( 'resize', onWindowResize );
  86. setInterval( startTween, 3000 );
  87. }
  88. function startTween() {
  89. // tween for animating color transition
  90. new TWEEN.Tween( animation )
  91. .to( {
  92. t: 1
  93. }, 2000 )
  94. .easing( TWEEN.Easing.Sinusoidal.In )
  95. .onComplete( () => {
  96. animation.t = 0;
  97. currentColorIndex = nextColorIndex;
  98. nextColorIndex ++;
  99. if ( nextColorIndex >= colors.length ) nextColorIndex = 0;
  100. } )
  101. .start();
  102. }
  103. function onWindowResize() {
  104. camera.aspect = window.innerWidth / window.innerHeight;
  105. camera.updateProjectionMatrix();
  106. renderer.setSize( window.innerWidth, window.innerHeight );
  107. }
  108. //
  109. function animate() {
  110. timer.update();
  111. const time = timer.getElapsed();
  112. TWEEN.update();
  113. // animate camera
  114. camera.position.x = Math.sin( time / 4 ) * 10;
  115. camera.position.z = Math.cos( time / 4 ) * 10;
  116. camera.position.y = 8 + Math.cos( time / 2 ) * 2;
  117. cameraTarget.x = Math.sin( time / 4 ) * - 8;
  118. cameraTarget.z = Math.cos( time / 2 ) * - 8;
  119. camera.lookAt( cameraTarget );
  120. camera.up.x = Math.sin( time / 400 );
  121. // animate instance positions and colors
  122. for ( let i = 0; i < mesh.count; i ++ ) {
  123. mesh.getMatrixAt( i, dummy.matrix );
  124. dummy.matrix.decompose( dummy.position, dummy.quaternion, dummy.scale );
  125. dummy.position.y = Math.abs( Math.sin( ( time + seeds[ i ] ) * 2 + seeds[ i ] ) );
  126. dummy.updateMatrix();
  127. mesh.setMatrixAt( i, dummy.matrix );
  128. // colors
  129. if ( animation.t > 0 ) {
  130. const currentColor = colors[ currentColorIndex ];
  131. const nextColor = colors[ nextColorIndex ];
  132. const f = dummy.position.length() / maxDistance;
  133. if ( f <= animation.t ) {
  134. color.set( baseColors[ i ] ).multiply( nextColor );
  135. } else {
  136. color.set( baseColors[ i ] ).multiply( currentColor );
  137. }
  138. mesh.setColorAt( i, color );
  139. }
  140. }
  141. mesh.instanceMatrix.needsUpdate = true;
  142. if ( animation.t > 0 ) mesh.instanceColor.needsUpdate = true;
  143. mesh.computeBoundingSphere();
  144. renderer.render( scene, camera );
  145. }
  146. </script>
  147. </body>
  148. </html>
粤ICP备19079148号