webgl_instancing_modified.html 4.6 KB

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