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