webgl_buffergeometry_instancing.html 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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="importmap">
  48. {
  49. "imports": {
  50. "three": "../build/three.module.js",
  51. "three/addons/": "./jsm/"
  52. }
  53. }
  54. </script>
  55. <script type="module">
  56. import * as THREE from 'three';
  57. import Stats from 'three/addons/libs/stats.module.js';
  58. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  59. let container, stats;
  60. let camera, scene, renderer;
  61. init();
  62. function init() {
  63. container = document.getElementById( 'container' );
  64. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 10 );
  65. camera.position.z = 2;
  66. scene = new THREE.Scene();
  67. // geometry
  68. const vector = new THREE.Vector4();
  69. const instances = 50000;
  70. const positions = [];
  71. const offsets = [];
  72. const colors = [];
  73. const orientationsStart = [];
  74. const orientationsEnd = [];
  75. positions.push( 0.025, - 0.025, 0 );
  76. positions.push( - 0.025, 0.025, 0 );
  77. positions.push( 0, 0, 0.025 );
  78. // instanced attributes
  79. for ( let i = 0; i < instances; i ++ ) {
  80. // offsets
  81. offsets.push( Math.random() - 0.5, Math.random() - 0.5, Math.random() - 0.5 );
  82. // colors
  83. colors.push( Math.random(), Math.random(), Math.random(), Math.random() );
  84. // orientation start
  85. vector.set( Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1 );
  86. vector.normalize();
  87. orientationsStart.push( vector.x, vector.y, vector.z, vector.w );
  88. // orientation end
  89. vector.set( Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1 );
  90. vector.normalize();
  91. orientationsEnd.push( vector.x, vector.y, vector.z, vector.w );
  92. }
  93. const geometry = new THREE.InstancedBufferGeometry();
  94. geometry.instanceCount = instances; // set so its initalized for dat.GUI, will be set in first draw otherwise
  95. geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( positions, 3 ) );
  96. geometry.setAttribute( 'offset', new THREE.InstancedBufferAttribute( new Float32Array( offsets ), 3 ) );
  97. geometry.setAttribute( 'color', new THREE.InstancedBufferAttribute( new Float32Array( colors ), 4 ) );
  98. geometry.setAttribute( 'orientationStart', new THREE.InstancedBufferAttribute( new Float32Array( orientationsStart ), 4 ) );
  99. geometry.setAttribute( 'orientationEnd', new THREE.InstancedBufferAttribute( new Float32Array( orientationsEnd ), 4 ) );
  100. // material
  101. const material = new THREE.RawShaderMaterial( {
  102. uniforms: {
  103. 'time': { value: 1.0 },
  104. 'sineTime': { value: 1.0 }
  105. },
  106. vertexShader: document.getElementById( 'vertexShader' ).textContent,
  107. fragmentShader: document.getElementById( 'fragmentShader' ).textContent,
  108. side: THREE.DoubleSide,
  109. forceSinglePass: true,
  110. transparent: true
  111. } );
  112. //
  113. const mesh = new THREE.Mesh( geometry, material );
  114. scene.add( mesh );
  115. //
  116. renderer = new THREE.WebGLRenderer();
  117. renderer.setPixelRatio( window.devicePixelRatio );
  118. renderer.setSize( window.innerWidth, window.innerHeight );
  119. renderer.setAnimationLoop( animate );
  120. container.appendChild( renderer.domElement );
  121. //
  122. const gui = new GUI( { width: 350 } );
  123. gui.add( geometry, 'instanceCount', 0, instances );
  124. //
  125. stats = new Stats();
  126. container.appendChild( stats.dom );
  127. //
  128. window.addEventListener( 'resize', onWindowResize );
  129. }
  130. function onWindowResize() {
  131. camera.aspect = window.innerWidth / window.innerHeight;
  132. camera.updateProjectionMatrix();
  133. renderer.setSize( window.innerWidth, window.innerHeight );
  134. }
  135. //
  136. function animate() {
  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. stats.update();
  144. }
  145. </script>
  146. </body>
  147. </html>