webgl_buffergeometry_instancing_billboards.html 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. }
  67. }
  68. </script>
  69. <script type="module">
  70. import * as THREE from 'three';
  71. import Stats from './jsm/libs/stats.module.js';
  72. let container, stats;
  73. let camera, scene, renderer;
  74. let geometry, material, mesh;
  75. function init() {
  76. renderer = new THREE.WebGLRenderer();
  77. if ( renderer.capabilities.isWebGL2 === false && renderer.extensions.has( 'ANGLE_instanced_arrays' ) === false ) {
  78. document.getElementById( 'notSupported' ).style.display = '';
  79. return false;
  80. }
  81. container = document.createElement( 'div' );
  82. document.body.appendChild( container );
  83. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 5000 );
  84. camera.position.z = 1400;
  85. scene = new THREE.Scene();
  86. const circleGeometry = new THREE.CircleGeometry( 1, 6 );
  87. geometry = new THREE.InstancedBufferGeometry();
  88. geometry.index = circleGeometry.index;
  89. geometry.attributes = circleGeometry.attributes;
  90. const particleCount = 75000;
  91. const translateArray = new Float32Array( particleCount * 3 );
  92. for ( let i = 0, i3 = 0, l = particleCount; i < l; i ++, i3 += 3 ) {
  93. translateArray[ i3 + 0 ] = Math.random() * 2 - 1;
  94. translateArray[ i3 + 1 ] = Math.random() * 2 - 1;
  95. translateArray[ i3 + 2 ] = Math.random() * 2 - 1;
  96. }
  97. geometry.setAttribute( 'translate', new THREE.InstancedBufferAttribute( translateArray, 3 ) );
  98. material = new THREE.RawShaderMaterial( {
  99. uniforms: {
  100. 'map': { value: new THREE.TextureLoader().load( 'textures/sprites/circle.png' ) },
  101. 'time': { value: 0.0 }
  102. },
  103. vertexShader: document.getElementById( 'vshader' ).textContent,
  104. fragmentShader: document.getElementById( 'fshader' ).textContent,
  105. depthTest: true,
  106. depthWrite: true
  107. } );
  108. mesh = new THREE.Mesh( geometry, material );
  109. mesh.scale.set( 500, 500, 500 );
  110. scene.add( mesh );
  111. renderer.setPixelRatio( window.devicePixelRatio );
  112. renderer.setSize( window.innerWidth, window.innerHeight );
  113. container.appendChild( renderer.domElement );
  114. stats = new Stats();
  115. container.appendChild( stats.dom );
  116. window.addEventListener( 'resize', onWindowResize );
  117. return true;
  118. }
  119. function onWindowResize() {
  120. camera.aspect = window.innerWidth / window.innerHeight;
  121. camera.updateProjectionMatrix();
  122. renderer.setSize( window.innerWidth, window.innerHeight );
  123. }
  124. function animate() {
  125. requestAnimationFrame( animate );
  126. render();
  127. stats.update();
  128. }
  129. function render() {
  130. const time = performance.now() * 0.0005;
  131. material.uniforms[ 'time' ].value = time;
  132. mesh.rotation.x = time * 0.2;
  133. mesh.rotation.y = time * 0.4;
  134. renderer.render( scene, camera );
  135. }
  136. if ( init() ) {
  137. animate();
  138. }
  139. </script>
  140. </body>
  141. </html>