webgl_buffergeometry_instancing.html 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. </head>
  9. <body>
  10. <div id="container"></div>
  11. <div id="info">
  12. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - instancing demo (single triangle)
  13. <div id="notSupported" style="display:none">Sorry your graphics card + browser does not support hardware instancing</div>
  14. </div>
  15. <script id="vertexShader" type="x-shader/x-vertex">
  16. precision highp float;
  17. uniform float sineTime;
  18. uniform mat4 modelViewMatrix;
  19. uniform mat4 projectionMatrix;
  20. attribute vec3 position;
  21. attribute vec3 offset;
  22. attribute vec4 color;
  23. attribute vec4 orientationStart;
  24. attribute vec4 orientationEnd;
  25. varying vec3 vPosition;
  26. varying vec4 vColor;
  27. void main(){
  28. vPosition = offset * max( abs( sineTime * 2.0 + 1.0 ), 0.5 ) + position;
  29. vec4 orientation = normalize( mix( orientationStart, orientationEnd, sineTime ) );
  30. vec3 vcV = cross( orientation.xyz, vPosition );
  31. vPosition = vcV * ( 2.0 * orientation.w ) + ( cross( orientation.xyz, vcV ) * 2.0 + vPosition );
  32. vColor = color;
  33. gl_Position = projectionMatrix * modelViewMatrix * vec4( vPosition, 1.0 );
  34. }
  35. </script>
  36. <script id="fragmentShader" type="x-shader/x-fragment">
  37. precision highp float;
  38. uniform float time;
  39. varying vec3 vPosition;
  40. varying vec4 vColor;
  41. void main() {
  42. vec4 color = vec4( vColor );
  43. color.r += sin( vPosition.x * 10.0 + time ) * 0.5;
  44. gl_FragColor = color;
  45. }
  46. </script>
  47. <script type="module">
  48. import * as THREE from '../build/three.module.js';
  49. import Stats from './jsm/libs/stats.module.js';
  50. import { GUI } from './jsm/libs/dat.gui.module.js';
  51. let container, stats;
  52. let camera, scene, renderer;
  53. init();
  54. animate();
  55. function init() {
  56. container = document.getElementById( 'container' );
  57. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 10 );
  58. camera.position.z = 2;
  59. scene = new THREE.Scene();
  60. // geometry
  61. const vector = new THREE.Vector4();
  62. const instances = 50000;
  63. const positions = [];
  64. const offsets = [];
  65. const colors = [];
  66. const orientationsStart = [];
  67. const orientationsEnd = [];
  68. positions.push( 0.025, - 0.025, 0 );
  69. positions.push( - 0.025, 0.025, 0 );
  70. positions.push( 0, 0, 0.025 );
  71. // instanced attributes
  72. for ( let i = 0; i < instances; i ++ ) {
  73. // offsets
  74. offsets.push( Math.random() - 0.5, Math.random() - 0.5, Math.random() - 0.5 );
  75. // colors
  76. colors.push( Math.random(), Math.random(), Math.random(), Math.random() );
  77. // orientation start
  78. vector.set( Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1 );
  79. vector.normalize();
  80. orientationsStart.push( vector.x, vector.y, vector.z, vector.w );
  81. // orientation end
  82. vector.set( Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1 );
  83. vector.normalize();
  84. orientationsEnd.push( vector.x, vector.y, vector.z, vector.w );
  85. }
  86. const geometry = new THREE.InstancedBufferGeometry();
  87. geometry.instanceCount = instances; // set so its initalized for dat.GUI, will be set in first draw otherwise
  88. geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( positions, 3 ) );
  89. geometry.setAttribute( 'offset', new THREE.InstancedBufferAttribute( new Float32Array( offsets ), 3 ) );
  90. geometry.setAttribute( 'color', new THREE.InstancedBufferAttribute( new Float32Array( colors ), 4 ) );
  91. geometry.setAttribute( 'orientationStart', new THREE.InstancedBufferAttribute( new Float32Array( orientationsStart ), 4 ) );
  92. geometry.setAttribute( 'orientationEnd', new THREE.InstancedBufferAttribute( new Float32Array( orientationsEnd ), 4 ) );
  93. // material
  94. const material = new THREE.RawShaderMaterial( {
  95. uniforms: {
  96. "time": { value: 1.0 },
  97. "sineTime": { value: 1.0 }
  98. },
  99. vertexShader: document.getElementById( 'vertexShader' ).textContent,
  100. fragmentShader: document.getElementById( 'fragmentShader' ).textContent,
  101. side: THREE.DoubleSide,
  102. transparent: true
  103. } );
  104. //
  105. const mesh = new THREE.Mesh( geometry, material );
  106. scene.add( mesh );
  107. //
  108. renderer = new THREE.WebGLRenderer();
  109. renderer.setPixelRatio( window.devicePixelRatio );
  110. renderer.setSize( window.innerWidth, window.innerHeight );
  111. container.appendChild( renderer.domElement );
  112. if ( renderer.capabilities.isWebGL2 === false && renderer.extensions.has( 'ANGLE_instanced_arrays' ) === false ) {
  113. document.getElementById( 'notSupported' ).style.display = '';
  114. return;
  115. }
  116. //
  117. const gui = new GUI( { width: 350 } );
  118. gui.add( geometry, 'instanceCount', 0, instances );
  119. //
  120. stats = new Stats();
  121. container.appendChild( stats.dom );
  122. //
  123. window.addEventListener( 'resize', onWindowResize );
  124. }
  125. function onWindowResize() {
  126. camera.aspect = window.innerWidth / window.innerHeight;
  127. camera.updateProjectionMatrix();
  128. renderer.setSize( window.innerWidth, window.innerHeight );
  129. }
  130. //
  131. function animate() {
  132. requestAnimationFrame( animate );
  133. render();
  134. stats.update();
  135. }
  136. function render() {
  137. const time = performance.now();
  138. const object = scene.children[ 0 ];
  139. object.rotation.y = time * 0.0005;
  140. object.material.uniforms[ "time" ].value = time * 0.005;
  141. object.material.uniforms[ "sineTime" ].value = Math.sin( object.material.uniforms[ "time" ].value * 0.05 );
  142. renderer.render( scene, camera );
  143. }
  144. </script>
  145. </body>
  146. </html>