webgpu_compute_texture_3d.html 6.4 KB

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