webgl_buffergeometry_instancing.html 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. <!-- Import maps polyfill -->
  48. <!-- Remove this when import maps will be widely supported -->
  49. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  50. <script type="importmap">
  51. {
  52. "imports": {
  53. "three": "../build/three.module.js"
  54. }
  55. }
  56. </script>
  57. <script type="module">
  58. import * as THREE from 'three';
  59. import Stats from './jsm/libs/stats.module.js';
  60. import { GUI } from './jsm/libs/lil-gui.module.min.js';
  61. let container, stats;
  62. let camera, scene, renderer;
  63. init();
  64. animate();
  65. function init() {
  66. container = document.getElementById( 'container' );
  67. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 10 );
  68. camera.position.z = 2;
  69. scene = new THREE.Scene();
  70. // geometry
  71. const vector = new THREE.Vector4();
  72. const instances = 50000;
  73. const positions = [];
  74. const offsets = [];
  75. const colors = [];
  76. const orientationsStart = [];
  77. const orientationsEnd = [];
  78. positions.push( 0.025, - 0.025, 0 );
  79. positions.push( - 0.025, 0.025, 0 );
  80. positions.push( 0, 0, 0.025 );
  81. // instanced attributes
  82. for ( let i = 0; i < instances; i ++ ) {
  83. // offsets
  84. offsets.push( Math.random() - 0.5, Math.random() - 0.5, Math.random() - 0.5 );
  85. // colors
  86. colors.push( Math.random(), Math.random(), Math.random(), Math.random() );
  87. // orientation start
  88. vector.set( Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1 );
  89. vector.normalize();
  90. orientationsStart.push( vector.x, vector.y, vector.z, vector.w );
  91. // orientation end
  92. vector.set( Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1 );
  93. vector.normalize();
  94. orientationsEnd.push( vector.x, vector.y, vector.z, vector.w );
  95. }
  96. const geometry = new THREE.InstancedBufferGeometry();
  97. geometry.instanceCount = instances; // set so its initalized for dat.GUI, will be set in first draw otherwise
  98. geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( positions, 3 ) );
  99. geometry.setAttribute( 'offset', new THREE.InstancedBufferAttribute( new Float32Array( offsets ), 3 ) );
  100. geometry.setAttribute( 'color', new THREE.InstancedBufferAttribute( new Float32Array( colors ), 4 ) );
  101. geometry.setAttribute( 'orientationStart', new THREE.InstancedBufferAttribute( new Float32Array( orientationsStart ), 4 ) );
  102. geometry.setAttribute( 'orientationEnd', new THREE.InstancedBufferAttribute( new Float32Array( orientationsEnd ), 4 ) );
  103. // material
  104. const material = new THREE.RawShaderMaterial( {
  105. uniforms: {
  106. 'time': { value: 1.0 },
  107. 'sineTime': { value: 1.0 }
  108. },
  109. vertexShader: document.getElementById( 'vertexShader' ).textContent,
  110. fragmentShader: document.getElementById( 'fragmentShader' ).textContent,
  111. side: THREE.DoubleSide,
  112. transparent: true
  113. } );
  114. //
  115. const mesh = new THREE.Mesh( geometry, material );
  116. scene.add( mesh );
  117. //
  118. renderer = new THREE.WebGLRenderer();
  119. renderer.setPixelRatio( window.devicePixelRatio );
  120. renderer.setSize( window.innerWidth, window.innerHeight );
  121. container.appendChild( renderer.domElement );
  122. if ( renderer.capabilities.isWebGL2 === false && renderer.extensions.has( 'ANGLE_instanced_arrays' ) === false ) {
  123. document.getElementById( 'notSupported' ).style.display = '';
  124. return;
  125. }
  126. //
  127. const gui = new GUI( { width: 350 } );
  128. gui.add( geometry, 'instanceCount', 0, instances );
  129. //
  130. stats = new Stats();
  131. container.appendChild( stats.dom );
  132. //
  133. window.addEventListener( 'resize', onWindowResize );
  134. }
  135. function onWindowResize() {
  136. camera.aspect = window.innerWidth / window.innerHeight;
  137. camera.updateProjectionMatrix();
  138. renderer.setSize( window.innerWidth, window.innerHeight );
  139. }
  140. //
  141. function animate() {
  142. requestAnimationFrame( animate );
  143. render();
  144. stats.update();
  145. }
  146. function render() {
  147. const time = performance.now();
  148. const object = scene.children[ 0 ];
  149. object.rotation.y = time * 0.0005;
  150. object.material.uniforms[ 'time' ].value = time * 0.005;
  151. object.material.uniforms[ 'sineTime' ].value = Math.sin( object.material.uniforms[ 'time' ].value * 0.05 );
  152. renderer.render( scene, camera );
  153. }
  154. </script>
  155. </body>
  156. </html>