webgl_buffergeometry_instancing_billboards.html 4.5 KB

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