webgpu_compute_particles_snow.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. <html lang="en">
  2. <head>
  3. <title>three.js webgpu - compute snow</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="example.css">
  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" class="logo-link"></a>
  12. <div class="title-wrapper">
  13. <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a>
  14. <span>Compute Snow</span>
  15. </div>
  16. <small>
  17. 100k snow particles simulation.
  18. </small>
  19. </div>
  20. <script type="importmap">
  21. {
  22. "imports": {
  23. "three": "../build/three.webgpu.js",
  24. "three/webgpu": "../build/three.webgpu.js",
  25. "three/tsl": "../build/three.tsl.js",
  26. "three/addons/": "./jsm/"
  27. }
  28. }
  29. </script>
  30. <script type="module">
  31. import * as THREE from 'three/webgpu';
  32. import { Fn, texture, vec3, pass, color, uint, screenUV, instancedArray, positionWorld, positionLocal, time, vec2, hash, instanceIndex, If } from 'three/tsl';
  33. import { gaussianBlur } from 'three/addons/tsl/display/GaussianBlurNode.js';
  34. import { Inspector } from 'three/addons/inspector/Inspector.js';
  35. import { TeapotGeometry } from 'three/addons/geometries/TeapotGeometry.js';
  36. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  37. const maxParticleCount = 100000;
  38. let camera, scene, renderer;
  39. let controls;
  40. let computeParticles;
  41. let postProcessing;
  42. let collisionCamera, collisionPosRT, collisionPosMaterial;
  43. init();
  44. async function init() {
  45. const { innerWidth, innerHeight } = window;
  46. camera = new THREE.PerspectiveCamera( 60, innerWidth / innerHeight, .1, 100 );
  47. camera.position.set( 20, 2, 20 );
  48. camera.layers.enable( 2 );
  49. camera.lookAt( 0, 40, 0 );
  50. scene = new THREE.Scene();
  51. scene.fog = new THREE.Fog( 0x0f3c37, 5, 40 );
  52. const dirLight = new THREE.DirectionalLight( 0xf9ff9b, 9 );
  53. dirLight.castShadow = true;
  54. dirLight.position.set( 10, 10, 0 );
  55. dirLight.castShadow = true;
  56. dirLight.shadow.camera.near = 1;
  57. dirLight.shadow.camera.far = 30;
  58. dirLight.shadow.camera.right = 30;
  59. dirLight.shadow.camera.left = - 30;
  60. dirLight.shadow.camera.top = 30;
  61. dirLight.shadow.camera.bottom = - 30;
  62. dirLight.shadow.mapSize.width = 2048;
  63. dirLight.shadow.mapSize.height = 2048;
  64. dirLight.shadow.bias = - 0.009;
  65. scene.add( dirLight );
  66. scene.add( new THREE.HemisphereLight( 0x0f3c37, 0x080d10, 100 ) );
  67. //
  68. collisionCamera = new THREE.OrthographicCamera( - 50, 50, 50, - 50, .1, 50 );
  69. collisionCamera.position.y = 50;
  70. collisionCamera.lookAt( 0, 0, 0 );
  71. collisionCamera.layers.enable( 1 );
  72. collisionPosRT = new THREE.RenderTarget( 1024, 1024 );
  73. collisionPosRT.texture.type = THREE.HalfFloatType;
  74. collisionPosRT.texture.magFilter = THREE.NearestFilter;
  75. collisionPosRT.texture.minFilter = THREE.NearestFilter;
  76. collisionPosRT.texture.generateMipmaps = false;
  77. collisionPosMaterial = new THREE.MeshBasicNodeMaterial();
  78. collisionPosMaterial.fog = false;
  79. collisionPosMaterial.toneMapped = false;
  80. collisionPosMaterial.colorNode = positionWorld.y;
  81. //
  82. const positionBuffer = instancedArray( maxParticleCount, 'vec3' );
  83. const scaleBuffer = instancedArray( maxParticleCount, 'vec3' );
  84. const staticPositionBuffer = instancedArray( maxParticleCount, 'vec3' );
  85. const dataBuffer = instancedArray( maxParticleCount, 'vec4' );
  86. // compute
  87. const randUint = () => uint( Math.random() * 0xFFFFFF );
  88. const computeInit = Fn( () => {
  89. const position = positionBuffer.element( instanceIndex );
  90. const scale = scaleBuffer.element( instanceIndex );
  91. const particleData = dataBuffer.element( instanceIndex );
  92. const randX = hash( instanceIndex );
  93. const randY = hash( instanceIndex.add( randUint() ) );
  94. const randZ = hash( instanceIndex.add( randUint() ) );
  95. position.x = randX.mul( 100 ).add( - 50 );
  96. position.y = randY.mul( 500 ).add( 3 );
  97. position.z = randZ.mul( 100 ).add( - 50 );
  98. scale.xyz = hash( instanceIndex.add( Math.random() ) ).mul( .8 ).add( .2 );
  99. staticPositionBuffer.element( instanceIndex ).assign( vec3( 1000, 10000, 1000 ) );
  100. particleData.y = randY.mul( - .1 ).add( - .02 );
  101. particleData.x = position.x;
  102. particleData.z = position.z;
  103. particleData.w = randX;
  104. } )().compute( maxParticleCount ).setName( 'Init Particles' );
  105. //
  106. const surfaceOffset = .2;
  107. const speed = .4;
  108. const computeUpdate = Fn( () => {
  109. const getCoord = ( pos ) => pos.add( 50 ).div( 100 );
  110. const position = positionBuffer.element( instanceIndex );
  111. const scale = scaleBuffer.element( instanceIndex );
  112. const particleData = dataBuffer.element( instanceIndex );
  113. const velocity = particleData.y;
  114. const random = particleData.w;
  115. const rippleOnSurface = texture( collisionPosRT.texture, getCoord( position.xz ) ).toInspector( 'Collision Test', () => {
  116. return texture( collisionPosRT.texture ).y; // .div( collisionCamera.position.y );
  117. } );
  118. const rippleFloorArea = rippleOnSurface.y.add( scale.x.mul( surfaceOffset ) );
  119. If( position.y.greaterThan( rippleFloorArea ), () => {
  120. position.x = particleData.x.add( time.mul( random.mul( random ) ).mul( speed ).sin().mul( 3 ) );
  121. position.z = particleData.z.add( time.mul( random ).mul( speed ).cos().mul( random.mul( 10 ) ) );
  122. position.y = position.y.add( velocity );
  123. } ).Else( () => {
  124. staticPositionBuffer.element( instanceIndex ).assign( position );
  125. } );
  126. } );
  127. computeParticles = computeUpdate().compute( maxParticleCount );
  128. computeParticles.name = 'Update Particles';
  129. // rain
  130. const geometry = new THREE.SphereGeometry( surfaceOffset, 5, 5 );
  131. function particle( staticParticles ) {
  132. const posBuffer = staticParticles ? staticPositionBuffer : positionBuffer;
  133. const layer = staticParticles ? 1 : 2;
  134. const staticMaterial = new THREE.MeshStandardNodeMaterial( {
  135. color: 0xeeeeee,
  136. roughness: .9,
  137. metalness: 0
  138. } );
  139. staticMaterial.positionNode = positionLocal.mul( scaleBuffer.toAttribute() ).add( posBuffer.toAttribute() );
  140. const rainParticles = new THREE.Mesh( geometry, staticMaterial );
  141. rainParticles.count = maxParticleCount;
  142. rainParticles.castShadow = true;
  143. rainParticles.layers.disableAll();
  144. rainParticles.layers.enable( layer );
  145. return rainParticles;
  146. }
  147. const dynamicParticles = particle();
  148. const staticParticles = particle( true );
  149. scene.add( dynamicParticles );
  150. scene.add( staticParticles );
  151. // floor geometry
  152. const floorGeometry = new THREE.PlaneGeometry( 100, 100 );
  153. floorGeometry.rotateX( - Math.PI / 2 );
  154. const plane = new THREE.Mesh( floorGeometry, new THREE.MeshStandardMaterial( {
  155. color: 0x0c1e1e,
  156. roughness: .5,
  157. metalness: 0,
  158. transparent: true
  159. } ) );
  160. plane.material.opacityNode = positionLocal.xz.mul( .05 ).distance( 0 ).saturate().oneMinus();
  161. scene.add( plane );
  162. // tree
  163. function tree( count = 8 ) {
  164. const coneMaterial = new THREE.MeshStandardNodeMaterial( {
  165. color: 0x0d492c,
  166. roughness: .6,
  167. metalness: 0
  168. } );
  169. const object = new THREE.Group();
  170. for ( let i = 0; i < count; i ++ ) {
  171. const radius = 1 + i;
  172. const coneGeometry = new THREE.ConeGeometry( radius * 0.95, radius * 1.25, 32 );
  173. const cone = new THREE.Mesh( coneGeometry, coneMaterial );
  174. cone.castShadow = true;
  175. cone.position.y = ( ( count - i ) * 1.5 ) + ( count * .6 );
  176. object.add( cone );
  177. }
  178. const geometry = new THREE.CylinderGeometry( 1, 1, count, 32 );
  179. const cone = new THREE.Mesh( geometry, coneMaterial );
  180. cone.position.y = count / 2;
  181. object.add( cone );
  182. return object;
  183. }
  184. const teapotTree = new THREE.Mesh( new TeapotGeometry( .5, 18 ), new THREE.MeshBasicNodeMaterial( {
  185. color: 0xfcfb9e
  186. } ) );
  187. teapotTree.name = 'Teapot Pass';
  188. teapotTree.position.y = 18;
  189. scene.add( tree() );
  190. scene.add( teapotTree );
  191. //
  192. scene.backgroundNode = screenUV.distance( .5 ).mul( 2 ).mix( color( 0x0f4140 ), color( 0x060a0d ) );
  193. //
  194. renderer = new THREE.WebGPURenderer( { antialias: true } );
  195. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  196. renderer.setPixelRatio( window.devicePixelRatio );
  197. renderer.setSize( window.innerWidth, window.innerHeight );
  198. renderer.setAnimationLoop( animate );
  199. renderer.inspector = new Inspector();
  200. document.body.appendChild( renderer.domElement );
  201. await renderer.init();
  202. //
  203. controls = new OrbitControls( camera, renderer.domElement );
  204. controls.target.set( 0, 10, 0 );
  205. controls.minDistance = 25;
  206. controls.maxDistance = 35;
  207. controls.maxPolarAngle = Math.PI / 1.7;
  208. controls.autoRotate = true;
  209. controls.autoRotateSpeed = - 0.7;
  210. controls.update();
  211. // post processing
  212. const scenePass = pass( scene, camera );
  213. const scenePassColor = scenePass.getTextureNode();
  214. const vignette = screenUV.distance( .5 ).mul( 1.35 ).clamp().oneMinus();
  215. const teapotTreePass = pass( teapotTree, camera ).getTextureNode();
  216. const teapotTreePassBlurred = gaussianBlur( teapotTreePass, vec2( 1 ), 6 );
  217. teapotTreePassBlurred.resolutionScale = 0.2;
  218. const scenePassColorBlurred = gaussianBlur( scenePassColor );
  219. scenePassColorBlurred.resolutionScale = 0.5;
  220. scenePassColorBlurred.directionNode = vec2( 1 );
  221. // compose
  222. let totalPass = scenePass.toInspector( 'Scene' );
  223. totalPass = totalPass.add( scenePassColorBlurred.mul( .1 ) );
  224. totalPass = totalPass.mul( vignette );
  225. totalPass = totalPass.add( teapotTreePass.mul( 10 ).add( teapotTreePassBlurred ).toInspector( 'Teapot Blur' ) );
  226. postProcessing = new THREE.PostProcessing( renderer );
  227. postProcessing.outputNode = totalPass;
  228. //
  229. renderer.compute( computeInit );
  230. //
  231. window.addEventListener( 'resize', onWindowResize );
  232. }
  233. function onWindowResize() {
  234. const { innerWidth, innerHeight } = window;
  235. camera.aspect = innerWidth / innerHeight;
  236. camera.updateProjectionMatrix();
  237. renderer.setSize( innerWidth, innerHeight );
  238. }
  239. function animate() {
  240. controls.update();
  241. // position
  242. scene.name = 'Collider Position';
  243. scene.overrideMaterial = collisionPosMaterial;
  244. renderer.setRenderTarget( collisionPosRT );
  245. renderer.render( scene, collisionCamera );
  246. // compute
  247. renderer.compute( computeParticles );
  248. // result
  249. scene.name = 'Scene';
  250. scene.overrideMaterial = null;
  251. renderer.setRenderTarget( null );
  252. postProcessing.render();
  253. }
  254. </script>
  255. </body>
  256. </html>
粤ICP备19079148号