webgpu_compute_particles_snow.html 11 KB

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