webgpu_particles.html 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <html lang="en">
  2. <head>
  3. <title>three.js webgpu - Particles</title>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  6. <link type="text/css" rel="stylesheet" href="main.css">
  7. </head>
  8. <body>
  9. <div id="info">
  10. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgpu - particles
  11. </div>
  12. <script type="importmap">
  13. {
  14. "imports": {
  15. "three": "../build/three.webgpu.js",
  16. "three/webgpu": "../build/three.webgpu.js",
  17. "three/tsl": "../build/three.tsl.js",
  18. "three/addons/": "./jsm/"
  19. }
  20. }
  21. </script>
  22. <script type="module">
  23. import * as THREE from 'three';
  24. import { range, texture, mix, uv, color, rotateUV, positionLocal, time, uniform } from 'three/tsl';
  25. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  26. import WebGPU from 'three/addons/capabilities/WebGPU.js';
  27. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  28. let camera, scene, renderer;
  29. let controls;
  30. init();
  31. function init() {
  32. if ( WebGPU.isAvailable() === false ) {
  33. document.body.appendChild( WebGPU.getErrorMessage() );
  34. throw new Error( 'No WebGPU support' );
  35. }
  36. const { innerWidth, innerHeight } = window;
  37. camera = new THREE.PerspectiveCamera( 60, innerWidth / innerHeight, 1, 5000 );
  38. camera.position.set( 1300, 500, 0 );
  39. scene = new THREE.Scene();
  40. // textures
  41. const textureLoader = new THREE.TextureLoader();
  42. const map = textureLoader.load( 'textures/opengameart/smoke1.png' );
  43. // create nodes
  44. const lifeRange = range( .1, 1 );
  45. const offsetRange = range( new THREE.Vector3( - 2, 3, - 2 ), new THREE.Vector3( 2, 5, 2 ) );
  46. const speed = uniform( .2 );
  47. const scaledTime = time.add( 5 ).mul( speed );
  48. const lifeTime = scaledTime.mul( lifeRange ).mod( 1 );
  49. const scaleRange = range( .3, 2 );
  50. const rotateRange = range( .1, 4 );
  51. const life = lifeTime.div( lifeRange );
  52. const fakeLightEffect = positionLocal.y.oneMinus().max( 0.2 );
  53. const textureNode = texture( map, rotateUV( uv(), scaledTime.mul( rotateRange ) ) );
  54. const opacityNode = textureNode.a.mul( life.oneMinus() );
  55. const smokeColor = mix( color( 0x2c1501 ), color( 0x222222 ), positionLocal.y.mul( 3 ).clamp() );
  56. // create particles
  57. const smokeNodeMaterial = new THREE.SpriteNodeMaterial();
  58. smokeNodeMaterial.colorNode = mix( color( 0xf27d0c ), smokeColor, life.mul( 2.5 ).min( 1 ) ).mul( fakeLightEffect );
  59. smokeNodeMaterial.opacityNode = opacityNode;
  60. smokeNodeMaterial.positionNode = offsetRange.mul( lifeTime );
  61. smokeNodeMaterial.scaleNode = scaleRange.mul( lifeTime.max( 0.3 ) );
  62. smokeNodeMaterial.depthWrite = false;
  63. const smokeInstancedSprite = new THREE.Mesh( new THREE.PlaneGeometry( 1, 1 ), smokeNodeMaterial );
  64. smokeInstancedSprite.scale.setScalar( 400 );
  65. smokeInstancedSprite.count = 2000;
  66. scene.add( smokeInstancedSprite );
  67. //
  68. const fireGeometry = new THREE.PlaneGeometry( 1, 1 );
  69. const fireCount = 1000;
  70. const fireNodeMaterial = new THREE.SpriteNodeMaterial();
  71. fireNodeMaterial.colorNode = mix( color( 0xb72f17 ), color( 0xb72f17 ), life );
  72. fireNodeMaterial.positionNode = range( new THREE.Vector3( - 1, 1, - 1 ), new THREE.Vector3( 1, 2, 1 ) ).mul( lifeTime );
  73. fireNodeMaterial.scaleNode = smokeNodeMaterial.scaleNode;
  74. fireNodeMaterial.opacityNode = opacityNode.mul( .5 );
  75. fireNodeMaterial.blending = THREE.AdditiveBlending;
  76. fireNodeMaterial.transparent = true;
  77. fireNodeMaterial.depthWrite = false;
  78. const fireInstancedSprite = new THREE.Mesh( fireGeometry, fireNodeMaterial );
  79. fireInstancedSprite.scale.setScalar( 400 );
  80. fireInstancedSprite.count = fireCount;
  81. fireInstancedSprite.position.y = - 100;
  82. fireInstancedSprite.renderOrder = 1;
  83. scene.add( fireInstancedSprite );
  84. // indirect draw ( optional )
  85. // each indirect draw call is 5 uint32 values for indexes ( different structure for non-indexed draw calls using 4 uint32 values )
  86. const indexCount = fireGeometry.index.array.length;
  87. const uint32 = new Uint32Array( 5 );
  88. uint32[ 0 ] = indexCount; // indexCount
  89. uint32[ 1 ] = fireCount; // instanceCount
  90. uint32[ 2 ] = 0; // firstIndex
  91. uint32[ 3 ] = 0; // baseVertex
  92. uint32[ 4 ] = 0; // firstInstance
  93. const indirectAttribute = new THREE.IndirectStorageBufferAttribute( uint32, 5 );
  94. fireGeometry.setIndirect( indirectAttribute );
  95. //
  96. const helper = new THREE.GridHelper( 3000, 40, 0x303030, 0x303030 );
  97. helper.position.y = - 75;
  98. scene.add( helper );
  99. //
  100. renderer = new THREE.WebGPURenderer( { antialias: true } );
  101. renderer.setPixelRatio( window.devicePixelRatio );
  102. renderer.setSize( window.innerWidth, window.innerHeight );
  103. renderer.setAnimationLoop( render );
  104. document.body.appendChild( renderer.domElement );
  105. //
  106. controls = new OrbitControls( camera, renderer.domElement );
  107. controls.maxDistance = 2700;
  108. controls.target.set( 0, 500, 0 );
  109. controls.update();
  110. //
  111. window.addEventListener( 'resize', onWindowResize );
  112. // gui
  113. const gui = new GUI();
  114. gui.add( speed, 'value', 0, 1, 0.01 ).name( 'speed' );
  115. }
  116. function onWindowResize() {
  117. const { innerWidth, innerHeight } = window;
  118. camera.aspect = innerWidth / innerHeight;
  119. camera.updateProjectionMatrix();
  120. renderer.setSize( innerWidth, innerHeight );
  121. }
  122. function render() {
  123. renderer.render( scene, camera );
  124. }
  125. </script>
  126. </body>
  127. </html>
粤ICP备19079148号