webgl_buffergeometry_instancing2.html 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - instancing test (meshes)</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. <style>
  9. a {
  10. color: #08f;
  11. }
  12. #notSupported {
  13. width: 50%;
  14. margin: auto;
  15. background-color: #f00;
  16. margin-top: 20px;
  17. padding: 10px;
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. <div id="container"></div>
  23. <div id="info">
  24. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - instancing test (meshes)
  25. <div id="notSupported" style="display:none">Sorry your graphics card + browser does not support hardware instancing</div>
  26. </div>
  27. <script id="vertexShader" type="x-shader/x-vertex">
  28. precision highp float;
  29. attribute vec3 instancePosition;
  30. attribute vec4 instanceQuaternion;
  31. attribute vec3 instanceScale;
  32. varying vec3 vColor;
  33. vec3 applyTRS( vec3 position, vec3 translation, vec4 quaternion, vec3 scale ) {
  34. position *= scale;
  35. position += 2.0 * cross( quaternion.xyz, cross( quaternion.xyz, position ) + quaternion.w * position );
  36. return position + translation;
  37. }
  38. void main(){
  39. vColor = color;
  40. vec3 transformed = applyTRS( position.xyz, instancePosition, instanceQuaternion, instanceScale );
  41. gl_Position = projectionMatrix * modelViewMatrix * vec4( transformed, 1.0 );
  42. }
  43. </script>
  44. <script id="fragmentShader" type="x-shader/x-fragment">
  45. precision highp float;
  46. varying vec3 vColor;
  47. void main() {
  48. gl_FragColor = vec4( vColor, 1.0 );
  49. }
  50. </script>
  51. <script type="module">
  52. import {
  53. Float32BufferAttribute,
  54. IcosahedronBufferGeometry,
  55. InstancedBufferAttribute,
  56. InstancedBufferGeometry,
  57. Mesh,
  58. MeshBasicMaterial,
  59. PerspectiveCamera,
  60. Scene,
  61. ShaderMaterial,
  62. VertexColors,
  63. WebGLRenderer
  64. } from "../build/three.module.js";
  65. import Stats from './jsm/libs/stats.module.js';
  66. import { TrackballControls } from './jsm/controls/TrackballControls.js';
  67. var container, stats;
  68. var camera, scene, renderer;
  69. var controls;
  70. init();
  71. animate();
  72. function init() {
  73. container = document.getElementById( 'container' );
  74. camera = new PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.01, 100 );
  75. camera.position.z = 4;
  76. scene = new Scene();
  77. //
  78. var geometry = new IcosahedronBufferGeometry( 0.1, 1 );
  79. var colors = [];
  80. for ( var i = 0, l = geometry.attributes.position.count; i < l; i ++ ) {
  81. colors.push( Math.random(), Math.random(), Math.random() );
  82. }
  83. geometry.addAttribute( 'color', new Float32BufferAttribute( colors, 3 ) );
  84. var material = new MeshBasicMaterial( { color: 0xff0000, vertexColors: VertexColors } );
  85. //
  86. var instances = 100;
  87. var instancePositions = [];
  88. var instanceQuaternions = [];
  89. var instanceScales = [];
  90. // we create for each mesh a counterpart in our instanced geometry data
  91. for ( var i = 0; i < instances; i ++ ) {
  92. // the red meshes are drawn with separate draw calls
  93. var mesh = new Mesh( geometry, material );
  94. scene.add( mesh );
  95. var position = mesh.position;
  96. var quaternion = mesh.quaternion;
  97. var scale = mesh.scale;
  98. position.set( Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1 );
  99. quaternion.set( Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1 );
  100. quaternion.normalize();
  101. scale.set( Math.random() * 2, Math.random() * 2, Math.random() * 2 );
  102. // instanced attribute data
  103. instancePositions.push( position.x, position.y, position.z );
  104. instanceQuaternions.push( quaternion.x, quaternion.y, quaternion.z, quaternion.w );
  105. instanceScales.push( scale.x, scale.y, scale.z );
  106. }
  107. var instancedGeometry = new InstancedBufferGeometry();
  108. instancedGeometry.attributes.position = geometry.attributes.position;
  109. instancedGeometry.attributes.color = geometry.attributes.color;
  110. instancedGeometry.addAttribute( 'instancePosition', new InstancedBufferAttribute( new Float32Array( instancePositions ), 3 ) );
  111. instancedGeometry.addAttribute( 'instanceQuaternion', new InstancedBufferAttribute( new Float32Array( instanceQuaternions ), 4 ) );
  112. instancedGeometry.addAttribute( 'instanceScale', new InstancedBufferAttribute( new Float32Array( instanceScales ), 3 ) );
  113. //
  114. var shaderMaterial = new ShaderMaterial( {
  115. uniforms: {},
  116. vertexShader: document.getElementById( 'vertexShader' ).textContent,
  117. fragmentShader: document.getElementById( 'fragmentShader' ).textContent,
  118. vertexColors: true
  119. } );
  120. // counterparts are drawn all at once with a single draw call (via instanced rendering)
  121. var instancedMesh = new Mesh( instancedGeometry, shaderMaterial );
  122. instancedMesh.position.x = 0.1;
  123. scene.add( instancedMesh );
  124. //
  125. renderer = new WebGLRenderer();
  126. renderer.setPixelRatio( window.devicePixelRatio );
  127. renderer.setSize( window.innerWidth, window.innerHeight );
  128. container.appendChild( renderer.domElement );
  129. controls = new TrackballControls( camera, renderer.domElement );
  130. if ( renderer.extensions.get( 'ANGLE_instanced_arrays' ) === null ) {
  131. document.getElementById( 'notSupported' ).style.display = '';
  132. return;
  133. }
  134. //
  135. stats = new Stats();
  136. container.appendChild( stats.dom );
  137. //
  138. window.addEventListener( 'resize', onWindowResize, false );
  139. }
  140. function onWindowResize() {
  141. camera.aspect = window.innerWidth / window.innerHeight;
  142. camera.updateProjectionMatrix();
  143. renderer.setSize( window.innerWidth, window.innerHeight );
  144. }
  145. //
  146. function animate() {
  147. requestAnimationFrame( animate );
  148. render();
  149. stats.update();
  150. }
  151. function render() {
  152. controls.update();
  153. renderer.render( scene, camera );
  154. }
  155. </script>
  156. </body>
  157. </html>