webgl_buffergeometry_instancing2.html 5.9 KB

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