webgpu_compute_texture_3d.html 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - compute texture 3D</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="main.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> WebGPU - Compute Texture 3D
  12. </div>
  13. <script type="importmap">
  14. {
  15. "imports": {
  16. "three": "../build/three.webgpu.js",
  17. "three/webgpu": "../build/three.webgpu.js",
  18. "three/tsl": "../build/three.tsl.js",
  19. "three/addons/": "./jsm/"
  20. }
  21. }
  22. </script>
  23. <script type="module">
  24. import * as THREE from 'three/webgpu';
  25. import { time, mx_noise_vec3, instanceIndex, textureStore, float, vec3, vec4, If, Break, Fn, smoothstep, texture3D, uniform } from 'three/tsl';
  26. import { RaymarchingBox } from 'three/addons/tsl/utils/Raymarching.js';
  27. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  28. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  29. import WebGPU from 'three/addons/capabilities/WebGPU.js';
  30. let renderer, scene, camera;
  31. let mesh;
  32. let computeNode;
  33. if ( WebGPU.isAvailable() === false ) {
  34. document.body.appendChild( WebGPU.getErrorMessage() );
  35. throw new Error( 'No WebGPU support' );
  36. }
  37. init();
  38. async function init() {
  39. renderer = new THREE.WebGPURenderer( { antialias: true } );
  40. renderer.setPixelRatio( window.devicePixelRatio );
  41. renderer.setSize( window.innerWidth, window.innerHeight );
  42. renderer.setAnimationLoop( animate );
  43. document.body.appendChild( renderer.domElement );
  44. scene = new THREE.Scene();
  45. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 100 );
  46. camera.position.set( 0, 1, 1.5 );
  47. new OrbitControls( camera, renderer.domElement );
  48. // Sky
  49. const canvas = document.createElement( 'canvas' );
  50. canvas.width = 1;
  51. canvas.height = 32;
  52. const context = canvas.getContext( '2d' );
  53. const gradient = context.createLinearGradient( 0, 0, 0, 32 );
  54. gradient.addColorStop( 0.0, '#014a84' );
  55. gradient.addColorStop( 0.5, '#0561a0' );
  56. gradient.addColorStop( 1.0, '#437ab6' );
  57. context.fillStyle = gradient;
  58. context.fillRect( 0, 0, 1, 32 );
  59. const skyMap = new THREE.CanvasTexture( canvas );
  60. skyMap.colorSpace = THREE.SRGBColorSpace;
  61. const sky = new THREE.Mesh(
  62. new THREE.SphereGeometry( 10 ),
  63. new THREE.MeshBasicNodeMaterial( { map: skyMap, side: THREE.BackSide } )
  64. );
  65. scene.add( sky );
  66. // Texture
  67. const size = 200;
  68. const computeCloud = Fn( ( { storageTexture } ) => {
  69. const scale = float( 0.05 );
  70. const id = instanceIndex;
  71. const x = id.mod( size );
  72. const y = id.div( size ).mod( size );
  73. const z = id.div( size * size );
  74. const coord3d = vec3( x, y, z );
  75. const centered = coord3d.sub( size / 2 ).div( size );
  76. const d = float( 1.0 ).sub( centered.length() );
  77. const noiseCoord = coord3d.mul( scale.div( 1.5 ) ).add( time );
  78. const noise = mx_noise_vec3( noiseCoord ).toConst( 'noise' );
  79. const data = noise.mul( d ).mul( d ).toConst( 'data' );
  80. textureStore( storageTexture, vec3( x, y, z ), vec4( vec3( data.x ), 1.0 ) );
  81. } );
  82. const storageTexture = new THREE.Storage3DTexture( size, size, size );
  83. storageTexture.generateMipmaps = false;
  84. storageTexture.name = 'cloud';
  85. computeNode = computeCloud( { storageTexture } ).compute( size * size * size ).setName( 'computeCloud' );
  86. // Shader
  87. const transparentRaymarchingTexture = Fn( ( {
  88. texture,
  89. range = float( 0.14 ),
  90. threshold = float( 0.08 ),
  91. opacity = float( 0.18 ),
  92. steps = float( 100 )
  93. } ) => {
  94. const finalColor = vec4( 0 ).toVar();
  95. RaymarchingBox( steps, ( { positionRay } ) => {
  96. const mapValue = float( texture.sample( positionRay.add( 0.5 ) ).r ).toVar();
  97. mapValue.assign( smoothstep( threshold.sub( range ), threshold.add( range ), mapValue ).mul( opacity ) );
  98. const shading = texture.sample( positionRay.add( vec3( - 0.01 ) ) ).r.sub( texture.sample( positionRay.add( vec3( 0.01 ) ) ).r );
  99. const col = shading.mul( 4.0 ).add( positionRay.x.add( positionRay.y ).mul( 0.5 ) ).add( 0.3 );
  100. finalColor.rgb.addAssign( finalColor.a.oneMinus().mul( mapValue ).mul( col ) );
  101. finalColor.a.addAssign( finalColor.a.oneMinus().mul( mapValue ) );
  102. If( finalColor.a.greaterThanEqual( 0.95 ), () => {
  103. Break();
  104. } );
  105. } );
  106. return finalColor;
  107. } );
  108. // Material
  109. const baseColor = uniform( new THREE.Color( 0x798aa0 ) );
  110. const range = uniform( 0.1 );
  111. const threshold = uniform( 0.08 );
  112. const opacity = uniform( 0.08 );
  113. const steps = uniform( 100 );
  114. const cloud3d = transparentRaymarchingTexture( {
  115. texture: texture3D( storageTexture, null, 0 ),
  116. range,
  117. threshold,
  118. opacity,
  119. steps
  120. } );
  121. const finalCloud = cloud3d.setRGB( cloud3d.rgb.add( baseColor ) );
  122. const material = new THREE.NodeMaterial();
  123. material.colorNode = finalCloud;
  124. material.side = THREE.BackSide;
  125. material.transparent = true;
  126. material.name = 'transparentRaymarchingMaterial';
  127. mesh = new THREE.Mesh( new THREE.BoxGeometry( 10, 10, 10 ), material );
  128. scene.add( mesh );
  129. mesh.rotation.y = Math.PI / 2;
  130. //
  131. await renderer.init();
  132. await renderer.computeAsync( computeNode );
  133. const gui = new GUI();
  134. gui.add( threshold, 'value', 0, 1, 0.01 ).name( 'threshold' );
  135. gui.add( opacity, 'value', 0, 1, 0.01 ).name( 'opacity' );
  136. gui.add( range, 'value', 0, 1, 0.01 ).name( 'range' );
  137. gui.add( steps, 'value', 0, 200, 1 ).name( 'steps' );
  138. window.addEventListener( 'resize', onWindowResize );
  139. }
  140. function onWindowResize() {
  141. camera.aspect = window.innerWidth / window.innerHeight;
  142. camera.updateProjectionMatrix();
  143. renderer.setSize( window.innerWidth, window.innerHeight );
  144. }
  145. function animate() {
  146. renderer.computeAsync( computeNode );
  147. renderer.render( scene, camera );
  148. }
  149. </script>
  150. </body>
  151. </html>
粤ICP备19079148号