webgl_instancing_dynamic.html 5.5 KB

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