webgl_buffergeometry_instancing2.html 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - instancing test</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. #oldie a {
  28. color: #da0;
  29. }
  30. #notSupported {
  31. width: 50%;
  32. margin: auto;
  33. border: 2px red solid;
  34. margin-top: 20px;
  35. padding: 10px;
  36. }
  37. </style>
  38. </head>
  39. <body>
  40. <div id="container"></div>
  41. <div id="info">
  42. <a href="http://threejs.org" target="_blank">three.js</a> - instancing demo
  43. <div id="notSupported" style="display:none">Sorry your graphics card + browser does not support hardware instancing</div>
  44. </div>
  45. <script src="../build/three.js"></script>
  46. <script src="js/Detector.js"></script>
  47. <script src="js/libs/stats.min.js"></script>
  48. <script src="js/controls/TrackballControls.js"></script>
  49. <script id="vertexShader" type="x-shader/x-vertex">
  50. precision highp float;
  51. attribute vec3 instancePosition;
  52. attribute vec4 instanceQuaternion;
  53. attribute vec3 instanceScale;
  54. vec3 applyTRS( vec3 position, vec3 translation, vec4 quaternion, vec3 scale ) {
  55. position *= scale;
  56. position += 2.0 * cross( quaternion.xyz, cross( quaternion.xyz, position ) + quaternion.w * position );
  57. return position + translation;
  58. }
  59. attribute vec3 color;
  60. varying vec3 vColor;
  61. void main(){
  62. vColor = color;
  63. vec3 transformed = applyTRS( position.xyz, instancePosition, instanceQuaternion, instanceScale );
  64. gl_Position = projectionMatrix * modelViewMatrix * vec4( transformed, 1.0 );
  65. }
  66. </script>
  67. <script id="fragmentShader" type="x-shader/x-fragment">
  68. precision highp float;
  69. varying vec3 vColor;
  70. void main() {
  71. gl_FragColor = vec4( vColor, 1.0 );
  72. }
  73. </script>
  74. <script>
  75. if ( !Detector.webgl ) Detector.addGetWebGLMessage();
  76. var container, stats;
  77. var camera, scene, renderer;
  78. var controls;
  79. init();
  80. animate();
  81. function init() {
  82. container = document.getElementById( 'container' );
  83. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.01, 100 );
  84. camera.position.z = 4;
  85. controls = new THREE.TrackballControls( camera );
  86. scene = new THREE.Scene();
  87. //
  88. // var geometry = new THREE.BoxBufferGeometry( 0.01, 0.01, 0.01 );
  89. var geometry = new THREE.IcosahedronBufferGeometry( 0.1, 1 );
  90. var colors = [];
  91. for ( var i = 0, l = geometry.attributes.position.count; i < l; i ++ ) {
  92. colors.push( Math.random(), Math.random(), Math.random() );
  93. }
  94. geometry.addAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );
  95. var material = new THREE.MeshBasicMaterial( { color: 0xff0000, vertexColors: THREE.VertexColors } );
  96. var mesh = new THREE.Mesh( geometry, material );
  97. // scene.add( mesh );
  98. //
  99. var INSTANCE_COUNT = 100;
  100. var geometry2 = new THREE.InstancedBufferGeometry().copy( geometry );
  101. var instancePositions = [];
  102. var instanceQuaternions = [];
  103. var instanceScales = [];
  104. for ( var i = 0; i < INSTANCE_COUNT; i ++ ) {
  105. var mesh = new THREE.Mesh( geometry, material );
  106. scene.add( mesh );
  107. var position = mesh.position;
  108. var quaternion = mesh.quaternion;
  109. var scale = mesh.scale;
  110. position.set( Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1 );
  111. quaternion.set( Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1 );
  112. quaternion.normalize();
  113. scale.set( Math.random() * 2, Math.random() * 2, Math.random() * 2 );
  114. instancePositions.push( position.x, position.y, position.z );
  115. instanceQuaternions.push( quaternion.x, quaternion.y, quaternion.z, quaternion.w );
  116. instanceScales.push( scale.x, scale.y, scale.z );
  117. }
  118. var attribute = new THREE.InstancedBufferAttribute( new Float32Array( instancePositions ), 3 );
  119. geometry2.addAttribute( 'instancePosition', attribute );
  120. var attribute = new THREE.InstancedBufferAttribute( new Float32Array( instanceQuaternions ), 4 );
  121. geometry2.addAttribute( 'instanceQuaternion', attribute );
  122. var attribute = new THREE.InstancedBufferAttribute( new Float32Array( instanceScales ), 3 );
  123. geometry2.addAttribute( 'instanceScale', attribute );
  124. var material = new THREE.ShaderMaterial( {
  125. uniforms: {},
  126. vertexShader: document.getElementById( 'vertexShader' ).textContent,
  127. fragmentShader: document.getElementById( 'fragmentShader' ).textContent
  128. } );
  129. var mesh2 = new THREE.Mesh( geometry2, material );
  130. mesh2.position.x = 0.1;
  131. scene.add( mesh2 );
  132. renderer = new THREE.WebGLRenderer();
  133. if ( renderer.extensions.get( 'ANGLE_instanced_arrays' ) === false ) {
  134. document.getElementById( "notSupported" ).style.display = "";
  135. return;
  136. }
  137. renderer.setClearColor( 0x101010 );
  138. renderer.setPixelRatio( window.devicePixelRatio );
  139. renderer.setSize( window.innerWidth, window.innerHeight );
  140. container.appendChild( renderer.domElement );
  141. stats = new Stats();
  142. container.appendChild( stats.dom );
  143. window.addEventListener( 'resize', onWindowResize, false );
  144. }
  145. function onWindowResize( event ) {
  146. camera.aspect = window.innerWidth / window.innerHeight;
  147. camera.updateProjectionMatrix();
  148. renderer.setSize( window.innerWidth, window.innerHeight );
  149. }
  150. //
  151. function animate() {
  152. requestAnimationFrame( animate );
  153. render();
  154. stats.update();
  155. }
  156. function render() {
  157. controls.update();
  158. renderer.render( scene, camera );
  159. }
  160. </script>
  161. </body>
  162. </html>