webgpu_compute_texture_3d.html 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. scene = new THREE.Scene();
  53. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 100 );
  54. camera.position.set( 0, 1, 1.5 );
  55. new OrbitControls( camera, renderer.domElement );
  56. // Sky
  57. const canvas = document.createElement( 'canvas' );
  58. canvas.width = 1;
  59. canvas.height = 32;
  60. const context = canvas.getContext( '2d' );
  61. const gradient = context.createLinearGradient( 0, 0, 0, 32 );
  62. gradient.addColorStop( 0.0, '#014a84' );
  63. gradient.addColorStop( 0.5, '#0561a0' );
  64. gradient.addColorStop( 1.0, '#437ab6' );
  65. context.fillStyle = gradient;
  66. context.fillRect( 0, 0, 1, 32 );
  67. const skyMap = new THREE.CanvasTexture( canvas );
  68. skyMap.colorSpace = THREE.SRGBColorSpace;
  69. const sky = new THREE.Mesh(
  70. new THREE.SphereGeometry( 10 ),
  71. new THREE.MeshBasicNodeMaterial( { map: skyMap, side: THREE.BackSide } )
  72. );
  73. scene.add( sky );
  74. // Texture
  75. const size = 200;
  76. const computeCloud = Fn( ( { storageTexture } ) => {
  77. const scale = float( 0.05 );
  78. const id = instanceIndex;
  79. const x = id.mod( size );
  80. const y = id.div( size ).mod( size );
  81. const z = id.div( size * size );
  82. const coord3d = vec3( x, y, z );
  83. const centered = coord3d.sub( size / 2 ).div( size );
  84. const d = float( 1.0 ).sub( centered.length() );
  85. const noiseCoord = coord3d.mul( scale.div( 1.5 ) ).add( time );
  86. const noise = mx_noise_vec3( noiseCoord ).toConst( 'noise' );
  87. const data = noise.mul( d ).mul( d ).toConst( 'data' );
  88. textureStore( storageTexture, vec3( x, y, z ), vec4( vec3( data.x ), 1.0 ) );
  89. } );
  90. const storageTexture = new THREE.Storage3DTexture( size, size, size );
  91. storageTexture.generateMipmaps = false;
  92. storageTexture.name = 'cloud';
  93. computeNode = computeCloud( { storageTexture } ).compute( size * size * size ).setName( 'computeCloud' );
  94. // Shader
  95. const transparentRaymarchingTexture = Fn( ( {
  96. texture,
  97. range = float( 0.14 ),
  98. threshold = float( 0.08 ),
  99. opacity = float( 0.18 ),
  100. steps = float( 100 )
  101. } ) => {
  102. const finalColor = vec4( 0 ).toVar();
  103. RaymarchingBox( steps, ( { positionRay } ) => {
  104. const mapValue = float( texture.sample( positionRay.add( 0.5 ) ).r ).toVar();
  105. mapValue.assign( smoothstep( threshold.sub( range ), threshold.add( range ), mapValue ).mul( opacity ) );
  106. const shading = texture.sample( positionRay.add( vec3( - 0.01 ) ) ).r.sub( texture.sample( positionRay.add( vec3( 0.01 ) ) ).r );
  107. const col = shading.mul( 4.0 ).add( positionRay.x.add( positionRay.y ).mul( 0.5 ) ).add( 0.3 );
  108. finalColor.rgb.addAssign( finalColor.a.oneMinus().mul( mapValue ).mul( col ) );
  109. finalColor.a.addAssign( finalColor.a.oneMinus().mul( mapValue ) );
  110. If( finalColor.a.greaterThanEqual( 0.95 ), () => {
  111. Break();
  112. } );
  113. } );
  114. return finalColor;
  115. } );
  116. // Material
  117. const baseColor = uniform( new THREE.Color( 0x798aa0 ) );
  118. const range = uniform( 0.1 );
  119. const threshold = uniform( 0.08 );
  120. const opacity = uniform( 0.08 );
  121. const steps = uniform( 100 );
  122. const cloud3d = transparentRaymarchingTexture( {
  123. texture: texture3D( storageTexture, null, 0 ),
  124. range,
  125. threshold,
  126. opacity,
  127. steps
  128. } );
  129. const finalCloud = cloud3d.setRGB( cloud3d.rgb.add( baseColor ) );
  130. const material = new THREE.NodeMaterial();
  131. material.colorNode = finalCloud;
  132. material.side = THREE.BackSide;
  133. material.transparent = true;
  134. material.name = 'transparentRaymarchingMaterial';
  135. mesh = new THREE.Mesh( new THREE.BoxGeometry( 10, 10, 10 ), material );
  136. scene.add( mesh );
  137. mesh.rotation.y = Math.PI / 2;
  138. //
  139. await renderer.init();
  140. await renderer.computeAsync( 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.computeAsync( computeNode );
  155. renderer.render( scene, camera );
  156. }
  157. </script>
  158. </body>
  159. </html>
粤ICP备19079148号