webgpu_volume_cloud.html 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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/tsl": "../build/three.webgpu.js",
  18. "three/addons/": "./jsm/"
  19. }
  20. }
  21. </script>
  22. <script type="module">
  23. import * as THREE from 'three';
  24. import { vec3, materialReference, smoothstep, If, Break, Fn } from 'three/tsl';
  25. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  26. import { ImprovedNoise } from 'three/addons/math/ImprovedNoise.js';
  27. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  28. let renderer, scene, camera;
  29. let mesh;
  30. init();
  31. function init() {
  32. renderer = new THREE.WebGPURenderer( { antialias: true } );
  33. renderer.setPixelRatio( window.devicePixelRatio );
  34. renderer.setSize( window.innerWidth, window.innerHeight );
  35. renderer.setAnimationLoop( animate );
  36. document.body.appendChild( renderer.domElement );
  37. scene = new THREE.Scene();
  38. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 100 );
  39. camera.position.set( 0, 0, 1.5 );
  40. new OrbitControls( camera, renderer.domElement );
  41. // Sky
  42. const canvas = document.createElement( 'canvas' );
  43. canvas.width = 1;
  44. canvas.height = 32;
  45. const context = canvas.getContext( '2d' );
  46. const gradient = context.createLinearGradient( 0, 0, 0, 32 );
  47. gradient.addColorStop( 0.0, '#014a84' );
  48. gradient.addColorStop( 0.5, '#0561a0' );
  49. gradient.addColorStop( 1.0, '#437ab6' );
  50. context.fillStyle = gradient;
  51. context.fillRect( 0, 0, 1, 32 );
  52. const skyMap = new THREE.CanvasTexture( canvas );
  53. skyMap.colorSpace = THREE.SRGBColorSpace;
  54. const sky = new THREE.Mesh(
  55. new THREE.SphereGeometry( 10 ),
  56. new THREE.MeshBasicNodeMaterial( { map: skyMap, side: THREE.BackSide } )
  57. );
  58. scene.add( sky );
  59. // Texture
  60. const size = 128;
  61. const data = new Uint8Array( size * size * size );
  62. let i = 0;
  63. const scale = 0.05;
  64. const perlin = new ImprovedNoise();
  65. const vector = new THREE.Vector3();
  66. for ( let z = 0; z < size; z ++ ) {
  67. for ( let y = 0; y < size; y ++ ) {
  68. for ( let x = 0; x < size; x ++ ) {
  69. const d = 1.0 - vector.set( x, y, z ).subScalar( size / 2 ).divideScalar( size ).length();
  70. data[ i ] = ( 128 + 128 * perlin.noise( x * scale / 1.5, y * scale, z * scale / 1.5 ) ) * d * d;
  71. i ++;
  72. }
  73. }
  74. }
  75. const texture = new THREE.Data3DTexture( data, size, size, size );
  76. texture.format = THREE.RedFormat;
  77. texture.minFilter = THREE.LinearFilter;
  78. texture.magFilter = THREE.LinearFilter;
  79. texture.unpackAlignment = 1;
  80. texture.needsUpdate = true;
  81. const geometry = new THREE.BoxGeometry( 1, 1, 1 );
  82. const material = new THREE.VolumeNodeMaterial( {
  83. side: THREE.BackSide,
  84. transparent: true
  85. } );
  86. material.map = texture;
  87. material.base = new THREE.Color( 0x798aa0 );
  88. material.steps = 100;
  89. material.range = 0.1;
  90. material.threshold = 0.25;
  91. material.opacity = 0.25;
  92. const range = materialReference( 'range', 'float' );
  93. const threshold = materialReference( 'threshold', 'float' );
  94. const opacity = materialReference( 'opacity', 'float' );
  95. material.testNode = Fn( ( { map, mapValue, probe, finalColor } ) => {
  96. mapValue.assign( smoothstep( threshold.sub( range ), threshold.add( range ), mapValue ).mul( opacity ) );
  97. const shading = map.uv( probe.add( vec3( - 0.01 ) ) ).r.sub( map.uv( probe.add( vec3( 0.01 ) ) ).r );
  98. const col = shading.mul( 3.0 ).add( probe.x.add( probe.y ).mul( 0.25 ) ).add( 0.2 );
  99. finalColor.rgb.addAssign( finalColor.a.oneMinus().mul( mapValue ).mul( col ) );
  100. finalColor.a.addAssign( finalColor.a.oneMinus().mul( mapValue ) );
  101. If( finalColor.a.greaterThanEqual( 0.95 ), () => {
  102. Break();
  103. } );
  104. } );
  105. mesh = new THREE.Mesh( geometry, material );
  106. scene.add( mesh );
  107. //
  108. const parameters = {
  109. threshold: 0.25,
  110. opacity: 0.25,
  111. range: 0.1,
  112. steps: 100
  113. };
  114. function update() {
  115. material.threshold = parameters.threshold;
  116. material.opacity = parameters.opacity;
  117. material.range = parameters.range;
  118. material.steps = parameters.steps;
  119. }
  120. const gui = new GUI();
  121. gui.add( parameters, 'threshold', 0, 1, 0.01 ).onChange( update );
  122. gui.add( parameters, 'opacity', 0, 1, 0.01 ).onChange( update );
  123. gui.add( parameters, 'range', 0, 1, 0.01 ).onChange( update );
  124. gui.add( parameters, 'steps', 0, 200, 1 ).onChange( update );
  125. window.addEventListener( 'resize', onWindowResize );
  126. }
  127. function onWindowResize() {
  128. camera.aspect = window.innerWidth / window.innerHeight;
  129. camera.updateProjectionMatrix();
  130. renderer.setSize( window.innerWidth, window.innerHeight );
  131. }
  132. function animate() {
  133. mesh.rotation.y = - performance.now() / 7500;
  134. renderer.render( scene, camera );
  135. }
  136. </script>
  137. </body>
  138. </html>