2
0

webgl_buffergeometry_instancing_billboards.html 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - instanced particles - billboards - colors</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="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - instanced circle billboards - colors
  12. <div id="notSupported" style="display:none">Sorry, your graphics card + browser does not support hardware instancing</div>
  13. </div>
  14. <script id="vshader" type="x-shader/x-vertex">
  15. precision highp float;
  16. uniform mat4 modelViewMatrix;
  17. uniform mat4 projectionMatrix;
  18. uniform float time;
  19. attribute vec3 position;
  20. attribute vec2 uv;
  21. attribute vec3 translate;
  22. varying vec2 vUv;
  23. varying float vScale;
  24. void main() {
  25. vec4 mvPosition = modelViewMatrix * vec4( translate, 1.0 );
  26. vec3 trTime = vec3(translate.x + time,translate.y + time,translate.z + time);
  27. float scale = sin( trTime.x * 2.1 ) + sin( trTime.y * 3.2 ) + sin( trTime.z * 4.3 );
  28. vScale = scale;
  29. scale = scale * 10.0 + 10.0;
  30. mvPosition.xyz += position * scale;
  31. vUv = uv;
  32. gl_Position = projectionMatrix * mvPosition;
  33. }
  34. </script>
  35. <script id="fshader" type="x-shader/x-fragment">
  36. precision highp float;
  37. uniform sampler2D map;
  38. varying vec2 vUv;
  39. varying float vScale;
  40. // HSL to RGB Convertion helpers
  41. vec3 HUEtoRGB(float H){
  42. H = mod(H,1.0);
  43. float R = abs(H * 6.0 - 3.0) - 1.0;
  44. float G = 2.0 - abs(H * 6.0 - 2.0);
  45. float B = 2.0 - abs(H * 6.0 - 4.0);
  46. return clamp(vec3(R,G,B),0.0,1.0);
  47. }
  48. vec3 HSLtoRGB(vec3 HSL){
  49. vec3 RGB = HUEtoRGB(HSL.x);
  50. float C = (1.0 - abs(2.0 * HSL.z - 1.0)) * HSL.y;
  51. return (RGB - 0.5) * C + HSL.z;
  52. }
  53. void main() {
  54. vec4 diffuseColor = texture2D( map, vUv );
  55. gl_FragColor = vec4( diffuseColor.xyz * HSLtoRGB(vec3(vScale/5.0, 1.0, 0.5)), diffuseColor.w );
  56. if ( diffuseColor.w < 0.5 ) discard;
  57. }
  58. </script>
  59. <!-- Import maps polyfill -->
  60. <!-- Remove this when import maps will be widely supported -->
  61. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  62. <script type="importmap">
  63. {
  64. "imports": {
  65. "three": "../build/three.module.js",
  66. "three/addons/": "./jsm/"
  67. }
  68. }
  69. </script>
  70. <script type="module">
  71. import * as THREE from 'three';
  72. import Stats from 'three/addons/libs/stats.module.js';
  73. let container, stats;
  74. let camera, scene, renderer;
  75. let geometry, material, mesh;
  76. function init() {
  77. renderer = new THREE.WebGLRenderer();
  78. if ( renderer.capabilities.isWebGL2 === false && renderer.extensions.has( 'ANGLE_instanced_arrays' ) === false ) {
  79. document.getElementById( 'notSupported' ).style.display = '';
  80. return false;
  81. }
  82. container = document.createElement( 'div' );
  83. document.body.appendChild( container );
  84. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 5000 );
  85. camera.position.z = 1400;
  86. scene = new THREE.Scene();
  87. const circleGeometry = new THREE.CircleGeometry( 1, 6 );
  88. geometry = new THREE.InstancedBufferGeometry();
  89. geometry.index = circleGeometry.index;
  90. geometry.attributes = circleGeometry.attributes;
  91. const particleCount = 75000;
  92. const translateArray = new Float32Array( particleCount * 3 );
  93. for ( let i = 0, i3 = 0, l = particleCount; i < l; i ++, i3 += 3 ) {
  94. translateArray[ i3 + 0 ] = Math.random() * 2 - 1;
  95. translateArray[ i3 + 1 ] = Math.random() * 2 - 1;
  96. translateArray[ i3 + 2 ] = Math.random() * 2 - 1;
  97. }
  98. geometry.setAttribute( 'translate', new THREE.InstancedBufferAttribute( translateArray, 3 ) );
  99. material = new THREE.RawShaderMaterial( {
  100. uniforms: {
  101. 'map': { value: new THREE.TextureLoader().load( 'textures/sprites/circle.png' ) },
  102. 'time': { value: 0.0 }
  103. },
  104. vertexShader: document.getElementById( 'vshader' ).textContent,
  105. fragmentShader: document.getElementById( 'fshader' ).textContent,
  106. depthTest: true,
  107. depthWrite: true
  108. } );
  109. mesh = new THREE.Mesh( geometry, material );
  110. mesh.scale.set( 500, 500, 500 );
  111. scene.add( mesh );
  112. renderer.setPixelRatio( window.devicePixelRatio );
  113. renderer.setSize( window.innerWidth, window.innerHeight );
  114. container.appendChild( renderer.domElement );
  115. stats = new Stats();
  116. container.appendChild( stats.dom );
  117. window.addEventListener( 'resize', onWindowResize );
  118. return true;
  119. }
  120. function onWindowResize() {
  121. camera.aspect = window.innerWidth / window.innerHeight;
  122. camera.updateProjectionMatrix();
  123. renderer.setSize( window.innerWidth, window.innerHeight );
  124. }
  125. function animate() {
  126. requestAnimationFrame( animate );
  127. render();
  128. stats.update();
  129. }
  130. function render() {
  131. const time = performance.now() * 0.0005;
  132. material.uniforms[ 'time' ].value = time;
  133. mesh.rotation.x = time * 0.2;
  134. mesh.rotation.y = time * 0.4;
  135. renderer.render( scene, camera );
  136. }
  137. if ( init() ) {
  138. animate();
  139. }
  140. </script>
  141. </body>
  142. </html>