webgpu_volume_cloud.html 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - volumetric cloud</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><span>Volumetric Cloud</span>
  14. </div>
  15. <small>
  16. Volumetric cloud using 3D texture raymarching.
  17. </small>
  18. </div>
  19. <script type="importmap">
  20. {
  21. "imports": {
  22. "three": "../build/three.webgpu.js",
  23. "three/webgpu": "../build/three.webgpu.js",
  24. "three/tsl": "../build/three.tsl.js",
  25. "three/addons/": "./jsm/"
  26. }
  27. }
  28. </script>
  29. <script type="module">
  30. import * as THREE from 'three/webgpu';
  31. import { float, vec3, vec4, If, Break, Fn, smoothstep, texture3D, uniform } from 'three/tsl';
  32. import { RaymarchingBox } from 'three/addons/tsl/utils/Raymarching.js';
  33. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  34. import { ImprovedNoise } from 'three/addons/math/ImprovedNoise.js';
  35. import { Inspector } from 'three/addons/inspector/Inspector.js';
  36. let renderer, scene, camera;
  37. let mesh;
  38. init();
  39. function init() {
  40. renderer = new THREE.WebGPURenderer( { antialias: true } );
  41. renderer.setPixelRatio( window.devicePixelRatio );
  42. renderer.setSize( window.innerWidth, window.innerHeight );
  43. renderer.setAnimationLoop( animate );
  44. renderer.inspector = new Inspector();
  45. document.body.appendChild( renderer.domElement );
  46. scene = new THREE.Scene();
  47. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 100 );
  48. camera.position.set( 0, 0, 1.5 );
  49. new OrbitControls( camera, renderer.domElement );
  50. // Sky
  51. const canvas = document.createElement( 'canvas' );
  52. canvas.width = 1;
  53. canvas.height = 32;
  54. const context = canvas.getContext( '2d' );
  55. const gradient = context.createLinearGradient( 0, 0, 0, 32 );
  56. gradient.addColorStop( 0.0, '#014a84' );
  57. gradient.addColorStop( 0.5, '#0561a0' );
  58. gradient.addColorStop( 1.0, '#437ab6' );
  59. context.fillStyle = gradient;
  60. context.fillRect( 0, 0, 1, 32 );
  61. const skyMap = new THREE.CanvasTexture( canvas );
  62. skyMap.colorSpace = THREE.SRGBColorSpace;
  63. const sky = new THREE.Mesh(
  64. new THREE.SphereGeometry( 10 ),
  65. new THREE.MeshBasicNodeMaterial( { map: skyMap, side: THREE.BackSide } )
  66. );
  67. scene.add( sky );
  68. // Texture
  69. const size = 128;
  70. const data = new Uint8Array( size * size * size );
  71. let i = 0;
  72. const scale = 0.05;
  73. const perlin = new ImprovedNoise();
  74. const vector = new THREE.Vector3();
  75. for ( let z = 0; z < size; z ++ ) {
  76. for ( let y = 0; y < size; y ++ ) {
  77. for ( let x = 0; x < size; x ++ ) {
  78. const d = 1.0 - vector.set( x, y, z ).subScalar( size / 2 ).divideScalar( size ).length();
  79. data[ i ] = ( 128 + 128 * perlin.noise( x * scale / 1.5, y * scale, z * scale / 1.5 ) ) * d * d;
  80. i ++;
  81. }
  82. }
  83. }
  84. const texture = new THREE.Data3DTexture( data, size, size, size );
  85. texture.format = THREE.RedFormat;
  86. texture.minFilter = THREE.LinearFilter;
  87. texture.magFilter = THREE.LinearFilter;
  88. texture.unpackAlignment = 1;
  89. texture.needsUpdate = true;
  90. // Shader
  91. const transparentRaymarchingTexture = Fn( ( {
  92. texture,
  93. range = float( 0.1 ),
  94. threshold = float( 0.25 ),
  95. opacity = float( 0.25 ),
  96. steps = float( 100 )
  97. } ) => {
  98. const finalColor = vec4( 0 ).toVar();
  99. RaymarchingBox( steps, ( { positionRay } ) => {
  100. const mapValue = float( texture.sample( positionRay.add( 0.5 ) ).r ).toVar();
  101. mapValue.assign( smoothstep( threshold.sub( range ), threshold.add( range ), mapValue ).mul( opacity ) );
  102. const shading = texture.sample( positionRay.add( vec3( - 0.01 ) ) ).r.sub( texture.sample( positionRay.add( vec3( 0.01 ) ) ).r );
  103. const col = shading.mul( 3.0 ).add( positionRay.x.add( positionRay.y ).mul( 0.25 ) ).add( 0.2 );
  104. finalColor.rgb.addAssign( finalColor.a.oneMinus().mul( mapValue ).mul( col ) );
  105. finalColor.a.addAssign( finalColor.a.oneMinus().mul( mapValue ) );
  106. If( finalColor.a.greaterThanEqual( 0.95 ), () => {
  107. Break();
  108. } );
  109. } );
  110. return finalColor;
  111. } );
  112. // Material
  113. const baseColor = uniform( new THREE.Color( 0x798aa0 ) );
  114. const range = uniform( 0.1 );
  115. const threshold = uniform( 0.25 );
  116. const opacity = uniform( 0.25 );
  117. const steps = uniform( 100 );
  118. const cloud3d = transparentRaymarchingTexture( {
  119. texture: texture3D( texture, null, 0 ),
  120. range,
  121. threshold,
  122. opacity,
  123. steps
  124. } );
  125. const finalCloud = cloud3d.setRGB( cloud3d.rgb.add( baseColor ) );
  126. const material = new THREE.NodeMaterial();
  127. material.colorNode = finalCloud;
  128. material.side = THREE.BackSide;
  129. material.transparent = true;
  130. mesh = new THREE.Mesh( new THREE.BoxGeometry( 1, 1, 1 ), material );
  131. scene.add( mesh );
  132. //
  133. const gui = renderer.inspector.createParameters( 'Parameters' );
  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. mesh.rotation.y = - performance.now() / 7500;
  147. renderer.render( scene, camera );
  148. }
  149. </script>
  150. </body>
  151. </html>
粤ICP备19079148号