webgl_buffergeometry_instancing.html 5.9 KB

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