webgl_buffergeometry_instancing2.html 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 * as THREE from '../build/three.module.js';
  53. import Stats from './jsm/libs/stats.module.js';
  54. import { TrackballControls } from './jsm/controls/TrackballControls.js';
  55. var container, stats;
  56. var camera, scene, renderer;
  57. var controls;
  58. init();
  59. animate();
  60. function init() {
  61. container = document.getElementById( 'container' );
  62. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.01, 100 );
  63. camera.position.z = 4;
  64. scene = new THREE.Scene();
  65. //
  66. var geometry = new THREE.IcosahedronBufferGeometry( 0.1, 1 );
  67. var colors = [];
  68. for ( var i = 0, l = geometry.attributes.position.count; i < l; i ++ ) {
  69. colors.push( Math.random(), Math.random(), Math.random() );
  70. }
  71. geometry.addAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );
  72. var material = new THREE.MeshBasicMaterial( { color: 0xff0000, vertexColors: THREE.VertexColors } );
  73. //
  74. var instances = 100;
  75. var instancePositions = [];
  76. var instanceQuaternions = [];
  77. var instanceScales = [];
  78. // we create for each mesh a counterpart in our instanced geometry data
  79. for ( var i = 0; i < instances; i ++ ) {
  80. // the red meshes are drawn with separate draw calls
  81. var mesh = new THREE.Mesh( geometry, material );
  82. scene.add( mesh );
  83. var position = mesh.position;
  84. var quaternion = mesh.quaternion;
  85. var scale = mesh.scale;
  86. position.set( Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1 );
  87. quaternion.set( Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1 );
  88. quaternion.normalize();
  89. scale.set( Math.random() * 2, Math.random() * 2, Math.random() * 2 );
  90. // instanced attribute data
  91. instancePositions.push( position.x, position.y, position.z );
  92. instanceQuaternions.push( quaternion.x, quaternion.y, quaternion.z, quaternion.w );
  93. instanceScales.push( scale.x, scale.y, scale.z );
  94. }
  95. var instancedGeometry = new THREE.InstancedBufferGeometry();
  96. instancedGeometry.attributes.position = geometry.attributes.position;
  97. instancedGeometry.attributes.color = geometry.attributes.color;
  98. instancedGeometry.addAttribute( 'instancePosition', new THREE.InstancedBufferAttribute( new Float32Array( instancePositions ), 3 ) );
  99. instancedGeometry.addAttribute( 'instanceQuaternion', new THREE.InstancedBufferAttribute( new Float32Array( instanceQuaternions ), 4 ) );
  100. instancedGeometry.addAttribute( 'instanceScale', new THREE.InstancedBufferAttribute( new Float32Array( instanceScales ), 3 ) );
  101. //
  102. var shaderMaterial = new THREE.ShaderMaterial( {
  103. uniforms: {},
  104. vertexShader: document.getElementById( 'vertexShader' ).textContent,
  105. fragmentShader: document.getElementById( 'fragmentShader' ).textContent,
  106. vertexColors: true
  107. } );
  108. // counterparts are drawn all at once with a single draw call (via instanced rendering)
  109. var instancedMesh = new THREE.Mesh( instancedGeometry, shaderMaterial );
  110. instancedMesh.position.x = 0.1;
  111. scene.add( instancedMesh );
  112. //
  113. renderer = new THREE.WebGLRenderer();
  114. renderer.setPixelRatio( window.devicePixelRatio );
  115. renderer.setSize( window.innerWidth, window.innerHeight );
  116. container.appendChild( renderer.domElement );
  117. controls = new TrackballControls( camera, renderer.domElement );
  118. if ( renderer.extensions.get( 'ANGLE_instanced_arrays' ) === null ) {
  119. document.getElementById( 'notSupported' ).style.display = '';
  120. return;
  121. }
  122. //
  123. stats = new Stats();
  124. container.appendChild( stats.dom );
  125. //
  126. window.addEventListener( 'resize', onWindowResize, false );
  127. }
  128. function onWindowResize() {
  129. camera.aspect = window.innerWidth / window.innerHeight;
  130. camera.updateProjectionMatrix();
  131. renderer.setSize( window.innerWidth, window.innerHeight );
  132. }
  133. //
  134. function animate() {
  135. requestAnimationFrame( animate );
  136. render();
  137. stats.update();
  138. }
  139. function render() {
  140. controls.update();
  141. renderer.render( scene, camera );
  142. }
  143. </script>
  144. </body>
  145. </html>