webgpu_compute_water.html 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - compute water</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. <link type="text/css" rel="stylesheet" href="example.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 Water</span>
  15. </div>
  16. <small>
  17. Click and move mouse to disturb water.
  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 { instanceIndex, struct, If, uint, int, floor, float, length, clamp, vec2, cos, vec3, vertexIndex, Fn, uniform, instancedArray, min, max, positionLocal, transformNormalToView, globalId } from 'three/tsl';
  33. import { Inspector } from 'three/addons/inspector/Inspector.js';
  34. import { SimplexNoise } from 'three/addons/math/SimplexNoise.js';
  35. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  36. import { HDRLoader } from 'three/addons/loaders/HDRLoader.js';
  37. import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js';
  38. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  39. import WebGPU from 'three/addons/capabilities/WebGPU.js';
  40. // Dimensions of simulation grid.
  41. const WIDTH = 128;
  42. // Water size in system units.
  43. const BOUNDS = 6;
  44. const BOUNDS_HALF = BOUNDS * 0.5;
  45. const limit = BOUNDS_HALF - 0.2;
  46. const waterMaxHeight = 0.1;
  47. let container;
  48. let camera, scene, renderer, controls;
  49. let mouseDown = false;
  50. let firstClick = true;
  51. let updateOriginMouseDown = false;
  52. const mouseCoords = new THREE.Vector2();
  53. const raycaster = new THREE.Raycaster();
  54. let frame = 0;
  55. const effectController = {
  56. mousePos: uniform( new THREE.Vector2() ).setName( 'mousePos' ),
  57. mouseSpeed: uniform( new THREE.Vector2() ).setName( 'mouseSpeed' ),
  58. mouseDeep: uniform( .5 ).setName( 'mouseDeep' ),
  59. mouseSize: uniform( 0.12 ).setName( 'mouseSize' ),
  60. viscosity: uniform( 0.96 ).setName( 'viscosity' ),
  61. ducksEnabled: true,
  62. wireframe: false,
  63. speed: 5,
  64. };
  65. let sun;
  66. let waterMesh;
  67. let poolBorder;
  68. let meshRay;
  69. let computeHeight, computeDucks;
  70. let duckModel = null;
  71. const NUM_DUCKS = 100;
  72. const simplex = new SimplexNoise();
  73. // TODO: Fix example with WebGL backend
  74. if ( WebGPU.isAvailable() === false ) {
  75. document.body.appendChild( WebGPU.getErrorMessage() );
  76. throw new Error( 'No WebGPU support' );
  77. }
  78. init();
  79. function noise( x, y ) {
  80. let multR = waterMaxHeight;
  81. let mult = 0.025;
  82. let r = 0;
  83. for ( let i = 0; i < 15; i ++ ) {
  84. r += multR * simplex.noise( x * mult, y * mult );
  85. multR *= 0.53 + 0.025 * i;
  86. mult *= 1.25;
  87. }
  88. return r;
  89. }
  90. async function init() {
  91. container = document.createElement( 'div' );
  92. document.body.appendChild( container );
  93. camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 3000 );
  94. camera.position.set( 0, 2.00, 4 );
  95. camera.lookAt( 0, 0, 0 );
  96. scene = new THREE.Scene();
  97. sun = new THREE.DirectionalLight( 0xFFFFFF, 4.0 );
  98. sun.position.set( - 1, 2.6, 1.4 );
  99. scene.add( sun );
  100. //
  101. // Initialize height storage buffers
  102. const heightArray = new Float32Array( WIDTH * WIDTH );
  103. const prevHeightArray = new Float32Array( WIDTH * WIDTH );
  104. let p = 0;
  105. for ( let j = 0; j < WIDTH; j ++ ) {
  106. for ( let i = 0; i < WIDTH; i ++ ) {
  107. const x = i * 128 / WIDTH;
  108. const y = j * 128 / WIDTH;
  109. const height = noise( x, y );
  110. heightArray[ p ] = height;
  111. prevHeightArray[ p ] = height;
  112. p ++;
  113. }
  114. }
  115. const heightStorage = instancedArray( heightArray ).setName( 'Height' );
  116. const prevHeightStorage = instancedArray( prevHeightArray ).setName( 'PrevHeight' );
  117. // Get Indices of Neighbor Values of an Index in the Simulation Grid
  118. const getNeighborIndicesTSL = ( index ) => {
  119. const width = uint( WIDTH );
  120. // Get 2-D compute coordinate from one-dimensional instanceIndex. The calculation will
  121. // still work even if you dispatch your compute shader 2-dimensionally, since within a compute
  122. // context, instanceIndex is a 1-dimensional value derived from the workgroup dimensions.
  123. // Cast to int to prevent unintended index overflow upon subtraction.
  124. const x = int( index.mod( WIDTH ) );
  125. const y = int( index.div( WIDTH ) );
  126. // The original shader accesses height via texture uvs. However, unlike with textures, we can't
  127. // access areas that are out of bounds. Accordingly, we emulate the Clamp to Edge Wrapping
  128. // behavior of accessing a DataTexture with out of bounds uvs.
  129. const leftX = max( 0, x.sub( 1 ) );
  130. const rightX = min( x.add( 1 ), width.sub( 1 ) );
  131. const bottomY = max( 0, y.sub( 1 ) );
  132. const topY = min( y.add( 1 ), width.sub( 1 ) );
  133. const westIndex = y.mul( width ).add( leftX );
  134. const eastIndex = y.mul( width ).add( rightX );
  135. const southIndex = bottomY.mul( width ).add( x );
  136. const northIndex = topY.mul( width ).add( x );
  137. return { northIndex, southIndex, eastIndex, westIndex };
  138. };
  139. // Get simulation index neighbor values
  140. const getNeighborValuesTSL = ( index, store ) => {
  141. const { northIndex, southIndex, eastIndex, westIndex } = getNeighborIndicesTSL( index );
  142. const north = store.element( northIndex );
  143. const south = store.element( southIndex );
  144. const east = store.element( eastIndex );
  145. const west = store.element( westIndex );
  146. return { north, south, east, west };
  147. };
  148. // Get new normals of simulation area.
  149. const getNormalsFromHeightTSL = ( index, store ) => {
  150. const { north, south, east, west } = getNeighborValuesTSL( index, store );
  151. const normalX = ( west.sub( east ) ).mul( WIDTH / BOUNDS );
  152. const normalY = ( south.sub( north ) ).mul( WIDTH / BOUNDS );
  153. return { normalX, normalY };
  154. };
  155. computeHeight = Fn( () => {
  156. const { viscosity, mousePos, mouseSize, mouseDeep, mouseSpeed } = effectController;
  157. const height = heightStorage.element( instanceIndex ).toVar();
  158. const prevHeight = prevHeightStorage.element( instanceIndex ).toVar();
  159. const { north, south, east, west } = getNeighborValuesTSL( instanceIndex, heightStorage );
  160. const neighborHeight = north.add( south ).add( east ).add( west );
  161. neighborHeight.mulAssign( 0.5 );
  162. neighborHeight.subAssign( prevHeight );
  163. const newHeight = neighborHeight.mul( viscosity );
  164. // Get x and y position of the coordinate in the water plane
  165. const x = float( globalId.x ).mul( 1 / WIDTH );
  166. const y = float( globalId.y ).mul( 1 / WIDTH );
  167. // Mouse influence
  168. const centerVec = vec2( 0.5 );
  169. // Get length of position in range [ -BOUNDS / 2, BOUNDS / 2 ], offset by mousePos, then scale.
  170. const mousePhase = clamp( length( ( vec2( x, y ).sub( centerVec ) ).mul( BOUNDS ).sub( mousePos ) ).mul( Math.PI ).div( mouseSize ), 0.0, Math.PI );
  171. // "Indent" water down by scaled distance from center of mouse impact
  172. newHeight.addAssign( cos( mousePhase ).add( 1.0 ).mul( mouseDeep ).mul( mouseSpeed.length() ) );
  173. prevHeightStorage.element( instanceIndex ).assign( height );
  174. heightStorage.element( instanceIndex ).assign( newHeight );
  175. } )().compute( WIDTH * WIDTH, [ 16, 16 ] ).setName( 'Update Height' );
  176. // Water Geometry corresponds with buffered compute grid.
  177. const waterGeometry = new THREE.PlaneGeometry( BOUNDS, BOUNDS, WIDTH - 1, WIDTH - 1 );
  178. const waterMaterial = new THREE.MeshStandardNodeMaterial( {
  179. color: 0x9bd2ec,
  180. metalness: 0.9,
  181. roughness: 0,
  182. transparent: true,
  183. opacity: 0.8,
  184. side: THREE.DoubleSide
  185. } );
  186. waterMaterial.normalNode = Fn( () => {
  187. // To correct the lighting as our mesh undulates, we have to reassign the normals in the normal shader.
  188. const { normalX, normalY } = getNormalsFromHeightTSL( vertexIndex, heightStorage );
  189. return transformNormalToView( vec3( normalX, normalY.negate(), 1.0 ) ).toVertexStage();
  190. } )();
  191. waterMaterial.positionNode = Fn( () => {
  192. return vec3( positionLocal.x, positionLocal.y, heightStorage.element( vertexIndex ) );
  193. } )();
  194. waterMesh = new THREE.Mesh( waterGeometry, waterMaterial );
  195. waterMesh.rotation.x = - Math.PI * 0.5;
  196. waterMesh.matrixAutoUpdate = false;
  197. waterMesh.updateMatrix();
  198. scene.add( waterMesh );
  199. // Pool border
  200. const borderGeom = new THREE.TorusGeometry( 4.2, 0.1, 12, 4 );
  201. borderGeom.rotateX( Math.PI * 0.5 );
  202. borderGeom.rotateY( Math.PI * 0.25 );
  203. poolBorder = new THREE.Mesh( borderGeom, new THREE.MeshStandardMaterial( { color: 0x908877, roughness: 0.2 } ) );
  204. scene.add( poolBorder );
  205. // THREE.Mesh just for mouse raycasting
  206. const geometryRay = new THREE.PlaneGeometry( BOUNDS, BOUNDS, 1, 1 );
  207. meshRay = new THREE.Mesh( geometryRay, new THREE.MeshBasicMaterial( { color: 0xFFFFFF, visible: false } ) );
  208. meshRay.rotation.x = - Math.PI / 2;
  209. meshRay.matrixAutoUpdate = false;
  210. meshRay.updateMatrix();
  211. scene.add( meshRay );
  212. // Initialize sphere mesh instance position and velocity.
  213. // position<vec3> + velocity<vec2> + unused<vec3> = 8 floats per sphere.
  214. // for structs arrays must be enclosed in multiple of 4
  215. const duckStride = 8;
  216. const duckInstanceDataArray = new Float32Array( NUM_DUCKS * duckStride );
  217. // Only hold velocity in x and z directions.
  218. // The sphere is wedded to the surface of the water, and will only move vertically with the water.
  219. for ( let i = 0; i < NUM_DUCKS; i ++ ) {
  220. duckInstanceDataArray[ i * duckStride + 0 ] = ( Math.random() - 0.5 ) * BOUNDS * 0.7;
  221. duckInstanceDataArray[ i * duckStride + 1 ] = 0;
  222. duckInstanceDataArray[ i * duckStride + 2 ] = ( Math.random() - 0.5 ) * BOUNDS * 0.7;
  223. }
  224. const DuckStruct = struct( {
  225. position: 'vec3',
  226. velocity: 'vec2'
  227. } );
  228. // Duck instance data storage
  229. const duckInstanceDataStorage = instancedArray( duckInstanceDataArray, DuckStruct ).setName( 'DuckInstanceData' );
  230. computeDucks = Fn( () => {
  231. const yOffset = float( - 0.04 );
  232. const verticalResponseFactor = float( 0.98 );
  233. const waterPushFactor = float( 0.015 );
  234. const linearDamping = float( 0.92 );
  235. const bounceDamping = float( - 0.4 );
  236. // Get 2-D compute coordinate from one-dimensional instanceIndex.
  237. const instancePosition = duckInstanceDataStorage.element( instanceIndex ).get( 'position' ).toVar();
  238. const velocity = duckInstanceDataStorage.element( instanceIndex ).get( 'velocity' ).toVar();
  239. const gridCoordX = instancePosition.x.div( BOUNDS ).add( 0.5 ).mul( WIDTH );
  240. const gridCoordZ = instancePosition.z.div( BOUNDS ).add( 0.5 ).mul( WIDTH );
  241. // Cast to int to prevent unintended index overflow upon subtraction.
  242. const xCoord = uint( clamp( floor( gridCoordX ), 0, WIDTH - 1 ) );
  243. const zCoord = uint( clamp( floor( gridCoordZ ), 0, WIDTH - 1 ) );
  244. const heightInstanceIndex = zCoord.mul( WIDTH ).add( xCoord );
  245. // Get height of water at the duck's position
  246. const waterHeight = heightStorage.element( heightInstanceIndex );
  247. const { normalX, normalY } = getNormalsFromHeightTSL( heightInstanceIndex, heightStorage );
  248. // Calculate the target Y position based on the water height and the duck's vertical offset
  249. const targetY = waterHeight.add( yOffset );
  250. const deltaY = targetY.sub( instancePosition.y );
  251. instancePosition.y.addAssign( deltaY.mul( verticalResponseFactor ) ); // Gradually update position
  252. // Get the normal of the water surface at the duck's position
  253. const pushX = normalX.mul( waterPushFactor );
  254. const pushZ = normalY.mul( waterPushFactor );
  255. // Apply the water push to the duck's velocity
  256. velocity.x.mulAssign( linearDamping );
  257. velocity.y.mulAssign( linearDamping );
  258. velocity.x.addAssign( pushX );
  259. velocity.y.addAssign( pushZ );
  260. // update position based on velocity
  261. instancePosition.x.addAssign( velocity.x );
  262. instancePosition.z.addAssign( velocity.y );
  263. // Clamp position to the pool bounds
  264. If( instancePosition.x.lessThan( - limit ), () => {
  265. instancePosition.x = - limit;
  266. velocity.x.mulAssign( bounceDamping );
  267. } ).ElseIf( instancePosition.x.greaterThan( limit ), () => {
  268. instancePosition.x = limit;
  269. velocity.x.mulAssign( bounceDamping );
  270. } );
  271. If( instancePosition.z.lessThan( - limit ), () => {
  272. instancePosition.z = - limit;
  273. velocity.y.mulAssign( bounceDamping ); // Invert and damp vz (velocity.y)
  274. } ).ElseIf( instancePosition.z.greaterThan( limit ), () => {
  275. instancePosition.z = limit;
  276. velocity.y.mulAssign( bounceDamping );
  277. } );
  278. // assignment of new values to the instance data storage
  279. duckInstanceDataStorage.element( instanceIndex ).get( 'position' ).assign( instancePosition );
  280. duckInstanceDataStorage.element( instanceIndex ).get( 'velocity' ).assign( velocity );
  281. } )().compute( NUM_DUCKS ).setName( 'Update Ducks' );
  282. // Models / Textures
  283. const hdrLoader = new HDRLoader().setPath( './textures/equirectangular/' );
  284. const glbloader = new GLTFLoader().setPath( 'models/gltf/' );
  285. glbloader.setDRACOLoader( new DRACOLoader().setDecoderPath( 'jsm/libs/draco/gltf/' ) );
  286. const [ env, model ] = await Promise.all( [ hdrLoader.loadAsync( 'blouberg_sunrise_2_1k.hdr' ), glbloader.loadAsync( 'duck.glb' ) ] );
  287. env.mapping = THREE.EquirectangularReflectionMapping;
  288. scene.environment = env;
  289. scene.background = env;
  290. scene.backgroundBlurriness = 0.3;
  291. scene.environmentIntensity = 1.25;
  292. duckModel = model.scene.children[ 0 ];
  293. duckModel.material.positionNode = Fn( () => {
  294. const instancePosition = duckInstanceDataStorage.element( instanceIndex ).get( 'position' );
  295. const newPosition = positionLocal.add( instancePosition );
  296. return newPosition;
  297. } )();
  298. const duckMesh = new THREE.InstancedMesh( duckModel.geometry, duckModel.material, NUM_DUCKS );
  299. scene.add( duckMesh );
  300. renderer = new THREE.WebGPURenderer( { antialias: true } );
  301. renderer.setPixelRatio( window.devicePixelRatio );
  302. renderer.setSize( window.innerWidth, window.innerHeight );
  303. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  304. renderer.toneMappingExposure = 0.5;
  305. renderer.setAnimationLoop( render );
  306. container.appendChild( renderer.domElement );
  307. renderer.inspector = new Inspector();
  308. document.body.appendChild( renderer.inspector.domElement );
  309. controls = new OrbitControls( camera, container );
  310. container.style.touchAction = 'none';
  311. //
  312. container.style.touchAction = 'none';
  313. container.addEventListener( 'pointermove', onPointerMove );
  314. container.addEventListener( 'pointerdown', onPointerDown );
  315. container.addEventListener( 'pointerup', onPointerUp );
  316. window.addEventListener( 'resize', onWindowResize );
  317. // GUI
  318. const gui = renderer.inspector.createParameters( 'Settings' );
  319. gui.add( effectController.mouseSize, 'value', 0.1, .3 ).name( 'Mouse Size' );
  320. gui.add( effectController.mouseDeep, 'value', 0.1, 1 ).name( 'Mouse Deep' );
  321. gui.add( effectController.viscosity, 'value', 0.9, 0.96, 0.001 ).name( 'viscosity' );
  322. gui.add( effectController, 'speed', 1, 6, 1 );
  323. gui.add( effectController, 'ducksEnabled' ).onChange( () => {
  324. duckMesh.visible = effectController.ducksEnabled;
  325. } );
  326. gui.add( effectController, 'wireframe' ).onChange( () => {
  327. waterMesh.material.wireframe = ! waterMesh.material.wireframe;
  328. poolBorder.material.wireframe = ! poolBorder.material.wireframe;
  329. duckModel.material.wireframe = ! duckModel.material.wireframe;
  330. waterMesh.material.needsUpdate = true;
  331. poolBorder.material.needsUpdate = true;
  332. } );
  333. }
  334. function onWindowResize() {
  335. camera.aspect = window.innerWidth / window.innerHeight;
  336. camera.updateProjectionMatrix();
  337. renderer.setSize( window.innerWidth, window.innerHeight );
  338. }
  339. function setMouseCoords( x, y ) {
  340. mouseCoords.set( ( x / renderer.domElement.clientWidth ) * 2 - 1, - ( y / renderer.domElement.clientHeight ) * 2 + 1 );
  341. }
  342. function onPointerDown() {
  343. mouseDown = true;
  344. firstClick = true;
  345. updateOriginMouseDown = true;
  346. }
  347. function onPointerUp() {
  348. mouseDown = false;
  349. firstClick = false;
  350. updateOriginMouseDown = false;
  351. controls.enabled = true;
  352. }
  353. function onPointerMove( event ) {
  354. if ( event.isPrimary === false ) return;
  355. setMouseCoords( event.clientX, event.clientY );
  356. }
  357. function raycast() {
  358. if ( mouseDown && ( firstClick || ! controls.enabled ) ) {
  359. raycaster.setFromCamera( mouseCoords, camera );
  360. const intersects = raycaster.intersectObject( meshRay );
  361. if ( intersects.length > 0 ) {
  362. const point = intersects[ 0 ].point;
  363. if ( updateOriginMouseDown ) {
  364. effectController.mousePos.value.set( point.x, point.z );
  365. updateOriginMouseDown = false;
  366. }
  367. effectController.mouseSpeed.value.set(
  368. ( point.x - effectController.mousePos.value.x ),
  369. ( point.z - effectController.mousePos.value.y )
  370. );
  371. effectController.mousePos.value.set( point.x, point.z );
  372. if ( firstClick ) {
  373. controls.enabled = false;
  374. }
  375. } else {
  376. updateOriginMouseDown = true;
  377. effectController.mouseSpeed.value.set( 0, 0 );
  378. }
  379. firstClick = false;
  380. } else {
  381. updateOriginMouseDown = true;
  382. effectController.mouseSpeed.value.set( 0, 0 );
  383. }
  384. }
  385. function render() {
  386. raycast();
  387. frame ++;
  388. if ( frame >= 7 - effectController.speed ) {
  389. renderer.compute( computeHeight, [ 8, 8, 1 ] );
  390. if ( effectController.ducksEnabled ) {
  391. renderer.compute( computeDucks );
  392. }
  393. frame = 0;
  394. }
  395. renderer.render( scene, camera );
  396. }
  397. </script>
  398. </body>
  399. </html>
粤ICP备19079148号