2
0

webgl_instancing_modified.html 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - instancing - modified</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 - modified
  12. </div>
  13. <!-- Import maps polyfill -->
  14. <!-- Remove this when import maps will be widely supported -->
  15. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  16. <script type="importmap">
  17. {
  18. "imports": {
  19. "three": "../build/three.module.js"
  20. }
  21. }
  22. </script>
  23. <script type="module">
  24. import * as THREE from 'three';
  25. import Stats from './jsm/libs/stats.module.js';
  26. let camera, scene, renderer, stats;
  27. let mesh;
  28. const dummy = new THREE.Object3D();
  29. const amount = 8;
  30. const count = Math.pow( amount, 3 );
  31. init();
  32. animate();
  33. function init() {
  34. renderer = new THREE.WebGLRenderer( { antialias: false } ); // false improves the frame rate
  35. renderer.setPixelRatio( window.devicePixelRatio );
  36. renderer.setSize( window.innerWidth, window.innerHeight );
  37. document.body.appendChild( renderer.domElement );
  38. renderer.outputEncoding = THREE.sRGBEncoding;
  39. scene = new THREE.Scene();
  40. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 100 );
  41. camera.position.set( 0, 0, 20 );
  42. new THREE.BufferGeometryLoader().load( 'models/json/suzanne_buffergeometry.json', function ( geometry ) {
  43. const instanceColors = [];
  44. for ( let i = 0; i < count; i ++ ) {
  45. instanceColors.push( Math.random() );
  46. instanceColors.push( Math.random() );
  47. instanceColors.push( Math.random() );
  48. }
  49. geometry.setAttribute( 'instanceColor', new THREE.InstancedBufferAttribute( new Float32Array( instanceColors ), 3 ) );
  50. geometry.computeVertexNormals();
  51. geometry.scale( 0.5, 0.5, 0.5 );
  52. //console.log( geometry );
  53. //
  54. new THREE.TextureLoader().load( 'textures/matcaps/matcap-porcelain-white.jpg', function ( texture ) {
  55. texture.encoding = THREE.sRGBEncoding;
  56. const material = new THREE.MeshMatcapMaterial( { color: 0xaaaaff, matcap: texture } );
  57. const colorParsChunk = [
  58. 'attribute vec3 instanceColor;',
  59. 'varying vec3 vInstanceColor;',
  60. '#include <common>'
  61. ].join( '\n' );
  62. const instanceColorChunk = [
  63. '#include <begin_vertex>',
  64. '\tvInstanceColor = instanceColor;'
  65. ].join( '\n' );
  66. const fragmentParsChunk = [
  67. 'varying vec3 vInstanceColor;',
  68. '#include <common>'
  69. ].join( '\n' );
  70. const colorChunk = [
  71. 'vec4 diffuseColor = vec4( diffuse * vInstanceColor, opacity );'
  72. ].join( '\n' );
  73. material.onBeforeCompile = function ( shader ) {
  74. shader.vertexShader = shader.vertexShader
  75. .replace( '#include <common>', colorParsChunk )
  76. .replace( '#include <begin_vertex>', instanceColorChunk );
  77. shader.fragmentShader = shader.fragmentShader
  78. .replace( '#include <common>', fragmentParsChunk )
  79. .replace( 'vec4 diffuseColor = vec4( diffuse, opacity );', colorChunk );
  80. //console.log( shader.uniforms );
  81. //console.log( shader.vertexShader );
  82. //console.log( shader.fragmentShader );
  83. };
  84. mesh = new THREE.InstancedMesh( geometry, material, count );
  85. mesh.instanceMatrix.setUsage( THREE.DynamicDrawUsage ); // will be updated every frame
  86. scene.add( mesh );
  87. } );
  88. } );
  89. //
  90. stats = new Stats();
  91. document.body.appendChild( stats.dom );
  92. //
  93. window.addEventListener( 'resize', onWindowResize );
  94. }
  95. function onWindowResize() {
  96. camera.aspect = window.innerWidth / window.innerHeight;
  97. camera.updateProjectionMatrix();
  98. renderer.setSize( window.innerWidth, window.innerHeight );
  99. }
  100. //
  101. function animate() {
  102. requestAnimationFrame( animate );
  103. render();
  104. stats.update();
  105. }
  106. function render() {
  107. if ( mesh ) {
  108. const time = Date.now() * 0.001;
  109. mesh.rotation.x = Math.sin( time / 4 );
  110. mesh.rotation.y = Math.sin( time / 2 );
  111. let i = 0;
  112. const offset = ( amount - 1 ) / 2;
  113. for ( let x = 0; x < amount; x ++ ) {
  114. for ( let y = 0; y < amount; y ++ ) {
  115. for ( let z = 0; z < amount; z ++ ) {
  116. dummy.position.set( offset - x, offset - y, offset - z );
  117. dummy.rotation.y = ( Math.sin( x / 4 + time ) + Math.sin( y / 4 + time ) + Math.sin( z / 4 + time ) );
  118. dummy.rotation.z = dummy.rotation.y * 2;
  119. dummy.updateMatrix();
  120. mesh.setMatrixAt( i ++, dummy.matrix );
  121. }
  122. }
  123. }
  124. mesh.instanceMatrix.needsUpdate = true;
  125. }
  126. renderer.render( scene, camera );
  127. }
  128. </script>
  129. </body>
  130. </html>