webgl_buffergeometry_instancing_billboards.html 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. <meta property="og:title" content="three.js webgl - instanced particles - billboards - colors">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgl_buffergeometry_instancing_billboards.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgl_buffergeometry_instancing_billboards.jpg">
  11. <link type="text/css" rel="stylesheet" href="main.css">
  12. </head>
  13. <body>
  14. <div id="info">
  15. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - instanced circle billboards - colors
  16. <div id="notSupported" style="display:none">Sorry, your graphics card + browser does not support hardware instancing</div>
  17. </div>
  18. <script id="vshader" type="x-shader/x-vertex">
  19. precision highp float;
  20. uniform mat4 modelViewMatrix;
  21. uniform mat4 projectionMatrix;
  22. uniform float time;
  23. attribute vec3 position;
  24. attribute vec2 uv;
  25. attribute vec3 translate;
  26. varying vec2 vUv;
  27. varying float vScale;
  28. void main() {
  29. vec4 mvPosition = modelViewMatrix * vec4( translate, 1.0 );
  30. vec3 trTime = vec3(translate.x + time,translate.y + time,translate.z + time);
  31. float scale = sin( trTime.x * 2.1 ) + sin( trTime.y * 3.2 ) + sin( trTime.z * 4.3 );
  32. vScale = scale;
  33. scale = scale * 10.0 + 10.0;
  34. mvPosition.xyz += position * scale;
  35. vUv = uv;
  36. gl_Position = projectionMatrix * mvPosition;
  37. }
  38. </script>
  39. <script id="fshader" type="x-shader/x-fragment">
  40. precision highp float;
  41. uniform sampler2D map;
  42. varying vec2 vUv;
  43. varying float vScale;
  44. // HSL to RGB Conversion helpers
  45. vec3 HUEtoRGB(float H){
  46. H = mod(H,1.0);
  47. float R = abs(H * 6.0 - 3.0) - 1.0;
  48. float G = 2.0 - abs(H * 6.0 - 2.0);
  49. float B = 2.0 - abs(H * 6.0 - 4.0);
  50. return clamp(vec3(R,G,B),0.0,1.0);
  51. }
  52. vec3 HSLtoRGB(vec3 HSL){
  53. vec3 RGB = HUEtoRGB(HSL.x);
  54. float C = (1.0 - abs(2.0 * HSL.z - 1.0)) * HSL.y;
  55. return (RGB - 0.5) * C + HSL.z;
  56. }
  57. void main() {
  58. vec4 diffuseColor = texture2D( map, vUv );
  59. gl_FragColor = vec4( diffuseColor.xyz * HSLtoRGB(vec3(vScale/5.0, 1.0, 0.5)), diffuseColor.w );
  60. if ( diffuseColor.w < 0.5 ) discard;
  61. }
  62. </script>
  63. <script type="importmap">
  64. {
  65. "imports": {
  66. "three": "../build/three.module.js",
  67. "three/addons/": "./jsm/"
  68. }
  69. }
  70. </script>
  71. <script type="module">
  72. import * as THREE from 'three';
  73. import Stats from 'three/addons/libs/stats.module.js';
  74. let container, stats;
  75. let camera, scene, renderer;
  76. let geometry, material, mesh;
  77. init();
  78. function init() {
  79. container = document.createElement( 'div' );
  80. document.body.appendChild( container );
  81. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 5000 );
  82. camera.position.z = 1400;
  83. scene = new THREE.Scene();
  84. const circleGeometry = new THREE.CircleGeometry( 1, 6 );
  85. geometry = new THREE.InstancedBufferGeometry();
  86. geometry.index = circleGeometry.index;
  87. geometry.attributes = circleGeometry.attributes;
  88. const particleCount = 75000;
  89. const translateArray = new Float32Array( particleCount * 3 );
  90. for ( let i = 0, i3 = 0, l = particleCount; i < l; i ++, i3 += 3 ) {
  91. translateArray[ i3 + 0 ] = Math.random() * 2 - 1;
  92. translateArray[ i3 + 1 ] = Math.random() * 2 - 1;
  93. translateArray[ i3 + 2 ] = Math.random() * 2 - 1;
  94. }
  95. geometry.setAttribute( 'translate', new THREE.InstancedBufferAttribute( translateArray, 3 ) );
  96. material = new THREE.RawShaderMaterial( {
  97. uniforms: {
  98. 'map': { value: new THREE.TextureLoader().load( 'textures/sprites/circle.png' ) },
  99. 'time': { value: 0.0 }
  100. },
  101. vertexShader: document.getElementById( 'vshader' ).textContent,
  102. fragmentShader: document.getElementById( 'fshader' ).textContent,
  103. depthTest: true,
  104. depthWrite: true
  105. } );
  106. mesh = new THREE.Mesh( geometry, material );
  107. mesh.scale.set( 500, 500, 500 );
  108. scene.add( mesh );
  109. renderer = new THREE.WebGLRenderer();
  110. renderer.setPixelRatio( window.devicePixelRatio );
  111. renderer.setSize( window.innerWidth, window.innerHeight );
  112. renderer.setAnimationLoop( animate );
  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. const time = performance.now() * 0.0005;
  126. material.uniforms[ 'time' ].value = time;
  127. mesh.rotation.x = time * 0.2;
  128. mesh.rotation.y = time * 0.4;
  129. renderer.render( scene, camera );
  130. stats.update();
  131. }
  132. </script>
  133. </body>
  134. </html>
粤ICP备19079148号