webgpu_compute_particles_snow.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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.type = THREE.HalfFloatType;
  67. collisionPosRT.texture.magFilter = THREE.NearestFilter;
  68. collisionPosRT.texture.minFilter = THREE.NearestFilter;
  69. collisionPosRT.texture.generateMipmaps = false;
  70. collisionPosMaterial = new THREE.MeshBasicNodeMaterial();
  71. collisionPosMaterial.fog = false;
  72. collisionPosMaterial.toneMapped = false;
  73. collisionPosMaterial.colorNode = positionWorld.y;
  74. //
  75. const positionBuffer = instancedArray( maxParticleCount, 'vec3' );
  76. const scaleBuffer = instancedArray( maxParticleCount, 'vec3' );
  77. const staticPositionBuffer = instancedArray( maxParticleCount, 'vec3' );
  78. const dataBuffer = instancedArray( maxParticleCount, 'vec4' );
  79. // compute
  80. const randUint = () => uint( Math.random() * 0xFFFFFF );
  81. const computeInit = Fn( () => {
  82. const position = positionBuffer.element( instanceIndex );
  83. const scale = scaleBuffer.element( instanceIndex );
  84. const particleData = dataBuffer.element( instanceIndex );
  85. const randX = hash( instanceIndex );
  86. const randY = hash( instanceIndex.add( randUint() ) );
  87. const randZ = hash( instanceIndex.add( randUint() ) );
  88. position.x = randX.mul( 100 ).add( - 50 );
  89. position.y = randY.mul( 500 ).add( 3 );
  90. position.z = randZ.mul( 100 ).add( - 50 );
  91. scale.xyz = hash( instanceIndex.add( Math.random() ) ).mul( .8 ).add( .2 );
  92. staticPositionBuffer.element( instanceIndex ).assign( vec3( 1000, 10000, 1000 ) );
  93. particleData.y = randY.mul( - .1 ).add( - .02 );
  94. particleData.x = position.x;
  95. particleData.z = position.z;
  96. particleData.w = randX;
  97. } )().compute( maxParticleCount ).setName( 'Init Particles' );
  98. //
  99. const surfaceOffset = .2;
  100. const speed = .4;
  101. const computeUpdate = Fn( () => {
  102. const getCoord = ( pos ) => pos.add( 50 ).div( 100 );
  103. const position = positionBuffer.element( instanceIndex );
  104. const scale = scaleBuffer.element( instanceIndex );
  105. const particleData = dataBuffer.element( instanceIndex );
  106. const velocity = particleData.y;
  107. const random = particleData.w;
  108. const rippleOnSurface = texture( collisionPosRT.texture, getCoord( position.xz ) ).toInspector( 'Collision Test', () => {
  109. return texture( collisionPosRT.texture ).y; // .div( collisionCamera.position.y );
  110. } );
  111. const rippleFloorArea = rippleOnSurface.y.add( scale.x.mul( surfaceOffset ) );
  112. If( position.y.greaterThan( rippleFloorArea ), () => {
  113. position.x = particleData.x.add( time.mul( random.mul( random ) ).mul( speed ).sin().mul( 3 ) );
  114. position.z = particleData.z.add( time.mul( random ).mul( speed ).cos().mul( random.mul( 10 ) ) );
  115. position.y = position.y.add( velocity );
  116. } ).Else( () => {
  117. staticPositionBuffer.element( instanceIndex ).assign( position );
  118. } );
  119. } );
  120. computeParticles = computeUpdate().compute( maxParticleCount );
  121. computeParticles.name = 'Update Particles';
  122. // rain
  123. const geometry = new THREE.SphereGeometry( surfaceOffset, 5, 5 );
  124. function particle( staticParticles ) {
  125. const posBuffer = staticParticles ? staticPositionBuffer : positionBuffer;
  126. const layer = staticParticles ? 1 : 2;
  127. const staticMaterial = new THREE.MeshStandardNodeMaterial( {
  128. color: 0xeeeeee,
  129. roughness: .9,
  130. metalness: 0
  131. } );
  132. staticMaterial.positionNode = positionLocal.mul( scaleBuffer.toAttribute() ).add( posBuffer.toAttribute() );
  133. const rainParticles = new THREE.Mesh( geometry, staticMaterial );
  134. rainParticles.count = maxParticleCount;
  135. rainParticles.layers.disableAll();
  136. rainParticles.layers.enable( layer );
  137. return rainParticles;
  138. }
  139. const dynamicParticles = particle();
  140. const staticParticles = particle( true );
  141. scene.add( dynamicParticles );
  142. scene.add( staticParticles );
  143. // floor geometry
  144. const floorGeometry = new THREE.PlaneGeometry( 100, 100 );
  145. floorGeometry.rotateX( - Math.PI / 2 );
  146. const plane = new THREE.Mesh( floorGeometry, new THREE.MeshStandardMaterial( {
  147. color: 0x0c1e1e,
  148. roughness: .5,
  149. metalness: 0,
  150. transparent: true
  151. } ) );
  152. plane.material.opacityNode = positionLocal.xz.mul( .05 ).distance( 0 ).saturate().oneMinus();
  153. scene.add( plane );
  154. // tree
  155. function tree( count = 8 ) {
  156. const coneMaterial = new THREE.MeshStandardNodeMaterial( {
  157. color: 0x0d492c,
  158. roughness: .6,
  159. metalness: 0
  160. } );
  161. const object = new THREE.Group();
  162. for ( let i = 0; i < count; i ++ ) {
  163. const radius = 1 + i;
  164. const coneGeometry = new THREE.ConeGeometry( radius * 0.95, radius * 1.25, 32 );
  165. const cone = new THREE.Mesh( coneGeometry, coneMaterial );
  166. cone.position.y = ( ( count - i ) * 1.5 ) + ( count * .6 );
  167. object.add( cone );
  168. }
  169. const geometry = new THREE.CylinderGeometry( 1, 1, count, 32 );
  170. const cone = new THREE.Mesh( geometry, coneMaterial );
  171. cone.position.y = count / 2;
  172. object.add( cone );
  173. return object;
  174. }
  175. const teapotTree = new THREE.Mesh( new TeapotGeometry( .5, 18 ), new THREE.MeshBasicNodeMaterial( {
  176. color: 0xfcfb9e
  177. } ) );
  178. teapotTree.name = 'Teapot Pass';
  179. teapotTree.position.y = 18;
  180. scene.add( tree() );
  181. scene.add( teapotTree );
  182. //
  183. scene.backgroundNode = screenUV.distance( .5 ).mul( 2 ).mix( color( 0x0f4140 ), color( 0x060a0d ) );
  184. //
  185. renderer = new THREE.WebGPURenderer( { antialias: true } );
  186. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  187. renderer.setPixelRatio( window.devicePixelRatio );
  188. renderer.setSize( window.innerWidth, window.innerHeight );
  189. renderer.setClearColor( 0x000000 );
  190. renderer.setAnimationLoop( animate );
  191. renderer.inspector = new Inspector();
  192. document.body.appendChild( renderer.domElement );
  193. await renderer.init();
  194. //
  195. controls = new OrbitControls( camera, renderer.domElement );
  196. controls.target.set( 0, 10, 0 );
  197. controls.minDistance = 25;
  198. controls.maxDistance = 35;
  199. controls.maxPolarAngle = Math.PI / 1.7;
  200. controls.autoRotate = true;
  201. controls.autoRotateSpeed = - 0.7;
  202. controls.update();
  203. // post processing
  204. const scenePass = pass( scene, camera );
  205. const scenePassColor = scenePass.getTextureNode();
  206. const vignette = screenUV.distance( .5 ).mul( 1.35 ).clamp().oneMinus();
  207. const teapotTreePass = pass( teapotTree, camera ).getTextureNode();
  208. const teapotTreePassBlurred = gaussianBlur( teapotTreePass, vec2( 1 ), 6 );
  209. teapotTreePassBlurred.resolutionScale = 0.2;
  210. const scenePassColorBlurred = gaussianBlur( scenePassColor );
  211. scenePassColorBlurred.resolutionScale = 0.5;
  212. scenePassColorBlurred.directionNode = vec2( 1 );
  213. // compose
  214. let totalPass = scenePass.toInspector( 'Scene' );
  215. totalPass = totalPass.add( scenePassColorBlurred.mul( .1 ) );
  216. totalPass = totalPass.mul( vignette );
  217. totalPass = totalPass.add( teapotTreePass.mul( 10 ).add( teapotTreePassBlurred ).toInspector( 'Teapot Blur' ) );
  218. renderPipeline = new THREE.RenderPipeline( renderer );
  219. renderPipeline.outputNode = totalPass;
  220. //
  221. renderer.compute( computeInit );
  222. //
  223. window.addEventListener( 'resize', onWindowResize );
  224. }
  225. function onWindowResize() {
  226. const { innerWidth, innerHeight } = window;
  227. camera.aspect = innerWidth / innerHeight;
  228. camera.updateProjectionMatrix();
  229. renderer.setSize( innerWidth, innerHeight );
  230. }
  231. function animate() {
  232. controls.update();
  233. // position
  234. scene.name = 'Collider Position';
  235. scene.overrideMaterial = collisionPosMaterial;
  236. renderer.setRenderTarget( collisionPosRT );
  237. renderer.render( scene, collisionCamera );
  238. // compute
  239. renderer.compute( computeParticles );
  240. // result
  241. scene.name = 'Scene';
  242. scene.overrideMaterial = null;
  243. renderer.setRenderTarget( null );
  244. renderPipeline.render();
  245. }
  246. </script>
  247. </body>
  248. </html>
粤ICP备19079148号