webgl_buffergeometry_instancing2.html 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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="js/libs/dat.gui.min.js"></script>
  46. <script src="../build/three.js"></script>
  47. <script src="js/Detector.js"></script>
  48. <script src="js/libs/stats.min.js"></script>
  49. <script src="js/controls/TrackballControls.js"></script>
  50. <script id="vertexShader" type="x-shader/x-vertex">
  51. precision highp float;
  52. attribute vec3 instancePosition;
  53. attribute vec4 instanceQuaternion;
  54. attribute vec3 instanceScale;
  55. vec3 applyTRS( vec3 position, vec3 translation, vec4 quaternion, vec3 scale ) {
  56. position *= scale;
  57. position += 2.0 * cross( quaternion.xyz, cross( quaternion.xyz, position ) + quaternion.w * position );
  58. return position + translation;
  59. }
  60. attribute vec3 color;
  61. varying vec3 vColor;
  62. void main(){
  63. vColor = color;
  64. vec3 transformed = applyTRS( position.xyz, instancePosition, instanceQuaternion, instanceScale );
  65. gl_Position = projectionMatrix * modelViewMatrix * vec4( transformed, 1.0 );
  66. }
  67. </script>
  68. <script id="fragmentShader" type="x-shader/x-fragment">
  69. precision highp float;
  70. varying vec3 vColor;
  71. void main() {
  72. gl_FragColor = vec4( vColor, 1.0 );
  73. }
  74. </script>
  75. <script>
  76. if ( !Detector.webgl ) Detector.addGetWebGLMessage();
  77. var container, stats;
  78. var camera, scene, renderer;
  79. var controls;
  80. init();
  81. animate();
  82. function init() {
  83. container = document.getElementById( 'container' );
  84. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.01, 100 );
  85. camera.position.z = 4;
  86. controls = new THREE.TrackballControls( camera );
  87. scene = new THREE.Scene();
  88. //
  89. // var geometry = new THREE.BoxBufferGeometry( 0.01, 0.01, 0.01 );
  90. var geometry = new THREE.IcosahedronBufferGeometry( 0.1, 1 );
  91. var colors = [];
  92. for ( var i = 0, l = geometry.attributes.position.count; i < l; i ++ ) {
  93. colors.push( Math.random(), Math.random(), Math.random() );
  94. }
  95. geometry.addAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );
  96. var material = new THREE.MeshBasicMaterial( { color: 0xff0000, vertexColors: THREE.VertexColors } );
  97. var mesh = new THREE.Mesh( geometry, material );
  98. // scene.add( mesh );
  99. //
  100. var INSTANCE_COUNT = 100;
  101. var geometry2 = new THREE.InstancedBufferGeometry().copy( geometry );
  102. var instancePositions = [];
  103. var instanceQuaternions = [];
  104. var instanceScales = [];
  105. // var position = new THREE.Vector3();
  106. // var quaternion = new THREE.Quaternion();
  107. for ( var i = 0; i < INSTANCE_COUNT; i ++ ) {
  108. var mesh = new THREE.Mesh( geometry, material );
  109. scene.add( mesh );
  110. var position = mesh.position;
  111. var quaternion = mesh.quaternion;
  112. var scale = mesh.scale;
  113. position.set( Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1 );
  114. quaternion.set( Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1 );
  115. quaternion.normalize();
  116. scale.set( Math.random() * 2, Math.random() * 2, Math.random() * 2 );
  117. instancePositions.push( position.x, position.y, position.z );
  118. instanceQuaternions.push( quaternion.x, quaternion.y, quaternion.z, quaternion.w );
  119. instanceScales.push( scale.x, scale.y, scale.z );
  120. }
  121. var attribute = new THREE.InstancedBufferAttribute( new Float32Array( instancePositions ), 3 );
  122. geometry2.addAttribute( 'instancePosition', attribute );
  123. var attribute = new THREE.InstancedBufferAttribute( new Float32Array( instanceQuaternions ), 4 );
  124. geometry2.addAttribute( 'instanceQuaternion', attribute );
  125. var attribute = new THREE.InstancedBufferAttribute( new Float32Array( instanceScales ), 3 );
  126. geometry2.addAttribute( 'instanceScale', attribute );
  127. var material = new THREE.ShaderMaterial( {
  128. uniforms: {},
  129. vertexShader: document.getElementById( 'vertexShader' ).textContent,
  130. fragmentShader: document.getElementById( 'fragmentShader' ).textContent
  131. } );
  132. var mesh2 = new THREE.Mesh( geometry2, material );
  133. mesh2.position.x = 0.1;
  134. scene.add( mesh2 );
  135. /*
  136. // geometry
  137. var triangles = 1;
  138. var instances = 65000;
  139. var geometry = new THREE.InstancedBufferGeometry();
  140. geometry.maxInstancedCount = instances; // set so its initalized for dat.GUI, will be set in first draw otherwise
  141. var gui = new dat.GUI();
  142. gui.add( geometry, "maxInstancedCount", 0, instances );
  143. var vertices = new THREE.BufferAttribute( new Float32Array( triangles * 3 * 3 ), 3 );
  144. vertices.setXYZ( 0, 0.025, -0.025, 0 );
  145. vertices.setXYZ( 1, -0.025, 0.025, 0 );
  146. vertices.setXYZ( 2, 0, 0, 0.025 );
  147. geometry.addAttribute( 'position', vertices );
  148. var offsets = new THREE.InstancedBufferAttribute( new Float32Array( instances * 3 ), 3, 1 );
  149. for ( var i = 0, ul = offsets.count; i < ul; i++ ) {
  150. offsets.setXYZ( i, Math.random() - 0.5, Math.random() - 0.5, Math.random() - 0.5 );
  151. }
  152. geometry.addAttribute( 'offset', offsets );
  153. var colors = new THREE.InstancedBufferAttribute( new Float32Array( instances * 4 ), 4, 1 );
  154. for ( var i = 0, ul = colors.count; i < ul; i++ ) {
  155. colors.setXYZW( i, Math.random(), Math.random(), Math.random(), Math.random() );
  156. }
  157. geometry.addAttribute( 'color', colors );
  158. var vector = new THREE.Vector4();
  159. var orientationsStart = new THREE.InstancedBufferAttribute( new Float32Array( instances * 4 ), 4, 1 );
  160. for ( var i = 0, ul = orientationsStart.count; i < ul; i++ ) {
  161. vector.set( Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1 );
  162. vector.normalize();
  163. orientationsStart.setXYZW( i, vector.x, vector.y, vector.z, vector.w );
  164. }
  165. geometry.addAttribute( 'orientationStart', orientationsStart );
  166. var orientationsEnd = new THREE.InstancedBufferAttribute( new Float32Array( instances * 4 ), 4, 1 );
  167. for ( var i = 0, ul = orientationsEnd.count; i < ul; i++ ) {
  168. vector.set( Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1 );
  169. vector.normalize();
  170. orientationsEnd.setXYZW( i, vector.x, vector.y, vector.z, vector.w );
  171. }
  172. geometry.addAttribute( 'orientationEnd', orientationsEnd );
  173. // material
  174. var material = new THREE.RawShaderMaterial( {
  175. uniforms: {
  176. time: { value: 1.0 },
  177. sineTime: { value: 1.0 }
  178. },
  179. vertexShader: document.getElementById( 'vertexShader' ).textContent,
  180. fragmentShader: document.getElementById( 'fragmentShader' ).textContent,
  181. side: THREE.DoubleSide,
  182. transparent: true
  183. } );
  184. var mesh = new THREE.Mesh( geometry, material );
  185. scene.add( mesh );
  186. */
  187. renderer = new THREE.WebGLRenderer();
  188. if ( renderer.extensions.get( 'ANGLE_instanced_arrays' ) === false ) {
  189. document.getElementById( "notSupported" ).style.display = "";
  190. return;
  191. }
  192. renderer.setClearColor( 0x101010 );
  193. renderer.setPixelRatio( window.devicePixelRatio );
  194. renderer.setSize( window.innerWidth, window.innerHeight );
  195. container.appendChild( renderer.domElement );
  196. stats = new Stats();
  197. container.appendChild( stats.dom );
  198. window.addEventListener( 'resize', onWindowResize, false );
  199. }
  200. function onWindowResize( event ) {
  201. camera.aspect = window.innerWidth / window.innerHeight;
  202. camera.updateProjectionMatrix();
  203. renderer.setSize( window.innerWidth, window.innerHeight );
  204. }
  205. //
  206. function animate() {
  207. requestAnimationFrame( animate );
  208. render();
  209. stats.update();
  210. }
  211. function render() {
  212. controls.update();
  213. renderer.render( scene, camera );
  214. }
  215. </script>
  216. </body>
  217. </html>