webgpu_volume_cloud.html 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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="main.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgpu - volumetric cloud
  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 { 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 { ImprovedNoise } from 'three/addons/math/ImprovedNoise.js';
  29. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  30. let renderer, scene, camera;
  31. let mesh;
  32. init();
  33. 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, 0, 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 = 128;
  63. const data = new Uint8Array( size * size * size );
  64. let i = 0;
  65. const scale = 0.05;
  66. const perlin = new ImprovedNoise();
  67. const vector = new THREE.Vector3();
  68. for ( let z = 0; z < size; z ++ ) {
  69. for ( let y = 0; y < size; y ++ ) {
  70. for ( let x = 0; x < size; x ++ ) {
  71. const d = 1.0 - vector.set( x, y, z ).subScalar( size / 2 ).divideScalar( size ).length();
  72. data[ i ] = ( 128 + 128 * perlin.noise( x * scale / 1.5, y * scale, z * scale / 1.5 ) ) * d * d;
  73. i ++;
  74. }
  75. }
  76. }
  77. const texture = new THREE.Data3DTexture( data, size, size, size );
  78. texture.format = THREE.RedFormat;
  79. texture.minFilter = THREE.LinearFilter;
  80. texture.magFilter = THREE.LinearFilter;
  81. texture.unpackAlignment = 1;
  82. texture.needsUpdate = true;
  83. // Shader
  84. const transparentRaymarchingTexture = Fn( ( {
  85. texture,
  86. range = float( 0.1 ),
  87. threshold = float( 0.25 ),
  88. opacity = float( 0.25 ),
  89. steps = float( 100 )
  90. } ) => {
  91. const finalColor = vec4( 0 ).toVar();
  92. RaymarchingBox( steps, ( { positionRay } ) => {
  93. const mapValue = float( texture.sample( positionRay.add( 0.5 ) ).r ).toVar();
  94. mapValue.assign( smoothstep( threshold.sub( range ), threshold.add( range ), mapValue ).mul( opacity ) );
  95. const shading = texture.sample( positionRay.add( vec3( - 0.01 ) ) ).r.sub( texture.sample( positionRay.add( vec3( 0.01 ) ) ).r );
  96. const col = shading.mul( 3.0 ).add( positionRay.x.add( positionRay.y ).mul( 0.25 ) ).add( 0.2 );
  97. finalColor.rgb.addAssign( finalColor.a.oneMinus().mul( mapValue ).mul( col ) );
  98. finalColor.a.addAssign( finalColor.a.oneMinus().mul( mapValue ) );
  99. If( finalColor.a.greaterThanEqual( 0.95 ), () => {
  100. Break();
  101. } );
  102. } );
  103. return finalColor;
  104. } );
  105. // Material
  106. const baseColor = uniform( new THREE.Color( 0x798aa0 ) );
  107. const range = uniform( 0.1 );
  108. const threshold = uniform( 0.25 );
  109. const opacity = uniform( 0.25 );
  110. const steps = uniform( 100 );
  111. const cloud3d = transparentRaymarchingTexture( {
  112. texture: texture3D( texture, null, 0 ),
  113. range,
  114. threshold,
  115. opacity,
  116. steps
  117. } );
  118. const finalCloud = cloud3d.setRGB( cloud3d.rgb.add( baseColor ) );
  119. const material = new THREE.NodeMaterial();
  120. material.colorNode = finalCloud;
  121. material.side = THREE.BackSide;
  122. material.transparent = true;
  123. mesh = new THREE.Mesh( new THREE.BoxGeometry( 1, 1, 1 ), material );
  124. scene.add( mesh );
  125. //
  126. const gui = new GUI();
  127. gui.add( threshold, 'value', 0, 1, 0.01 ).name( 'threshold' );
  128. gui.add( opacity, 'value', 0, 1, 0.01 ).name( 'opacity' );
  129. gui.add( range, 'value', 0, 1, 0.01 ).name( 'range' );
  130. gui.add( steps, 'value', 0, 200, 1 ).name( 'steps' );
  131. window.addEventListener( 'resize', onWindowResize );
  132. }
  133. function onWindowResize() {
  134. camera.aspect = window.innerWidth / window.innerHeight;
  135. camera.updateProjectionMatrix();
  136. renderer.setSize( window.innerWidth, window.innerHeight );
  137. }
  138. function animate() {
  139. mesh.rotation.y = - performance.now() / 7500;
  140. renderer.render( scene, camera );
  141. }
  142. </script>
  143. </body>
  144. </html>
粤ICP备19079148号