2
0

webgl_buffergeometry_instancing.html 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - instancing test (single triangle)</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. #webglmessage 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" rel="noopener">three.js</a> - instancing demo (single triangle)
  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/libs/stats.min.js"></script>
  48. <script src="js/WebGL.js"></script>
  49. <script id="vertexShader" type="x-shader/x-vertex">
  50. precision highp float;
  51. uniform float sineTime;
  52. uniform mat4 modelViewMatrix;
  53. uniform mat4 projectionMatrix;
  54. attribute vec3 position;
  55. attribute vec3 offset;
  56. attribute vec4 color;
  57. attribute vec4 orientationStart;
  58. attribute vec4 orientationEnd;
  59. varying vec3 vPosition;
  60. varying vec4 vColor;
  61. void main(){
  62. vPosition = offset * max( abs( sineTime * 2.0 + 1.0 ), 0.5 ) + position;
  63. vec4 orientation = normalize( mix( orientationStart, orientationEnd, sineTime ) );
  64. vec3 vcV = cross( orientation.xyz, vPosition );
  65. vPosition = vcV * ( 2.0 * orientation.w ) + ( cross( orientation.xyz, vcV ) * 2.0 + vPosition );
  66. vColor = color;
  67. gl_Position = projectionMatrix * modelViewMatrix * vec4( vPosition, 1.0 );
  68. }
  69. </script>
  70. <script id="fragmentShader" type="x-shader/x-fragment">
  71. precision highp float;
  72. uniform float time;
  73. varying vec3 vPosition;
  74. varying vec4 vColor;
  75. void main() {
  76. vec4 color = vec4( vColor );
  77. color.r += sin( vPosition.x * 10.0 + time ) * 0.5;
  78. gl_FragColor = color;
  79. }
  80. </script>
  81. <script>
  82. if ( WEBGL.isWebGLAvailable() === false ) {
  83. document.body.appendChild( WEBGL.getWebGLErrorMessage() );
  84. }
  85. var container, stats;
  86. var camera, scene, renderer;
  87. init();
  88. animate();
  89. function init() {
  90. container = document.getElementById( 'container' );
  91. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 10 );
  92. camera.position.z = 2;
  93. scene = new THREE.Scene();
  94. // geometry
  95. var vector = new THREE.Vector4();
  96. var instances = 50000;
  97. var positions = [];
  98. var offsets = [];
  99. var colors = [];
  100. var orientationsStart = [];
  101. var orientationsEnd = [];
  102. positions.push( 0.025, -0.025, 0 );
  103. positions.push( -0.025, 0.025, 0 );
  104. positions.push( 0, 0, 0.025 );
  105. // instanced attributes
  106. for ( var i = 0; i < instances; i ++ ) {
  107. // offsets
  108. offsets.push( Math.random() - 0.5, Math.random() - 0.5, Math.random() - 0.5 );
  109. // colors
  110. colors.push( Math.random(), Math.random(), Math.random(), Math.random() );
  111. // orientation start
  112. vector.set( Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1 );
  113. vector.normalize();
  114. orientationsStart.push( vector.x, vector.y, vector.z, vector.w );
  115. // orientation end
  116. vector.set( Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1 );
  117. vector.normalize();
  118. orientationsEnd.push( vector.x, vector.y, vector.z, vector.w );
  119. }
  120. var geometry = new THREE.InstancedBufferGeometry();
  121. geometry.maxInstancedCount = instances; // set so its initalized for dat.GUI, will be set in first draw otherwise
  122. geometry.addAttribute( 'position', new THREE.Float32BufferAttribute( positions, 3 ) );
  123. geometry.addAttribute( 'offset', new THREE.InstancedBufferAttribute( new Float32Array( offsets ), 3 ) );
  124. geometry.addAttribute( 'color', new THREE.InstancedBufferAttribute( new Float32Array( colors ), 4 ) );
  125. geometry.addAttribute( 'orientationStart', new THREE.InstancedBufferAttribute( new Float32Array( orientationsStart ), 4 ) );
  126. geometry.addAttribute( 'orientationEnd', new THREE.InstancedBufferAttribute( new Float32Array( orientationsEnd ), 4 ) );
  127. // material
  128. var material = new THREE.RawShaderMaterial( {
  129. uniforms: {
  130. time: { value: 1.0 },
  131. sineTime: { value: 1.0 }
  132. },
  133. vertexShader: document.getElementById( 'vertexShader' ).textContent,
  134. fragmentShader: document.getElementById( 'fragmentShader' ).textContent,
  135. side: THREE.DoubleSide,
  136. transparent: true
  137. } );
  138. //
  139. var mesh = new THREE.Mesh( geometry, material );
  140. scene.add( mesh );
  141. //
  142. renderer = new THREE.WebGLRenderer();
  143. renderer.setPixelRatio( window.devicePixelRatio );
  144. renderer.setSize( window.innerWidth, window.innerHeight );
  145. container.appendChild( renderer.domElement );
  146. if ( renderer.extensions.get( 'ANGLE_instanced_arrays' ) === null ) {
  147. document.getElementById( 'notSupported' ).style.display = '';
  148. return;
  149. }
  150. //
  151. var gui = new dat.GUI( { width: 350 } );
  152. gui.add( geometry, 'maxInstancedCount', 0, instances );
  153. //
  154. stats = new Stats();
  155. container.appendChild( stats.dom );
  156. //
  157. window.addEventListener( 'resize', onWindowResize, false );
  158. }
  159. function onWindowResize( event ) {
  160. camera.aspect = window.innerWidth / window.innerHeight;
  161. camera.updateProjectionMatrix();
  162. renderer.setSize( window.innerWidth, window.innerHeight );
  163. }
  164. //
  165. function animate() {
  166. requestAnimationFrame( animate );
  167. render();
  168. stats.update();
  169. }
  170. function render() {
  171. var time = performance.now();
  172. var object = scene.children[ 0 ];
  173. object.rotation.y = time * 0.0005;
  174. object.material.uniforms.time.value = time * 0.005;
  175. object.material.uniforms.sineTime.value = Math.sin( object.material.uniforms.time.value * 0.05 );
  176. renderer.render( scene, camera );
  177. }
  178. </script>
  179. </body>
  180. </html>