webgpu_compute_texture_3d.html 5.9 KB

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