webgpu_custom_fog_scattering.html 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - fog - scattering</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. <meta property="og:title" content="three.js webgpu - fog - scattering">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgpu_custom_fog_scattering.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgpu_custom_fog_scattering.jpg">
  11. <link type="text/css" rel="stylesheet" href="example.css">
  12. </head>
  13. <body>
  14. <div id="info" class="invert">
  15. <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
  16. <div class="title-wrapper">
  17. <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>Fog - Scattering</span>
  18. </div>
  19. <small>
  20. The demo simulates light scattering due to fog.<br/>
  21. </small>
  22. </div>
  23. <script type="importmap">
  24. {
  25. "imports": {
  26. "three": "../build/three.webgpu.js",
  27. "three/webgpu": "../build/three.webgpu.js",
  28. "three/tsl": "../build/three.tsl.js",
  29. "three/addons/": "./jsm/"
  30. }
  31. }
  32. </script>
  33. <script type="module">
  34. import * as THREE from 'three/webgpu';
  35. import { densityFogFactor, mix, pass, reference, uniform, vec2 } from 'three/tsl';
  36. import { gaussianBlur } from 'three/addons/tsl/display/GaussianBlurNode.js';
  37. import { FirstPersonControls } from 'three/addons/controls/FirstPersonControls.js';
  38. import { Inspector } from 'three/addons/inspector/Inspector.js';
  39. import { TreeGenerator } from 'three/addons/generators/TreeGenerator.js';
  40. let camera, scene, renderer, renderPipeline, controls;
  41. const timer = new THREE.Timer();
  42. const params = {
  43. scatteringEnabled: true
  44. };
  45. init();
  46. async function init() {
  47. camera = new THREE.PerspectiveCamera( 55, window.innerWidth / window.innerHeight, 0.1, 120 );
  48. camera.position.set( 0.4, 1.7, 9 ); // ~human eye height
  49. scene = new THREE.Scene();
  50. // fog: a bright, cool haze. the background matches it so the trunks read
  51. // as dark silhouettes that dissolve into depth — the scattering subject
  52. scene.fog = new THREE.FogExp2( 0xc6cace, 0.11 );
  53. scene.background = new THREE.Color( 0xc6cace );
  54. // everything is a flat black silhouette — the fog and the scattering blur
  55. // do all the shaping, so no lights are needed ( MeshBasicMaterial is unlit )
  56. const material = new THREE.MeshBasicMaterial( { color: 0x000000 } );
  57. // a few seeded variants of a tall scots pine: a clean bole rising into a
  58. // high, open crown of fine bare branches
  59. const variants = [];
  60. const generator = new TreeGenerator( material );
  61. for ( let v = 0; v < 6; v ++ ) {
  62. const mesh = generator
  63. .setSeed( v + 1 )
  64. .setTrunkLength( 2.6 + v * 0.3 ) // the crowns ride high in the fog
  65. .setTrunkRadius( 0.06 )
  66. .setTaper( 0.28 ) // slender, tapers slowly
  67. .setLevels( 4 )
  68. .setChildren( [ 7, 5, 4 ] ) // a wide whorl of limbs ramifying into many fine twigs
  69. .setBranchAngle( [ 55, 50, 46 ] ) // a rounded crown that turns up
  70. .setAngleVariance( 22 )
  71. .setLengthRatio( 0.46 ) // short crown limbs ( a modest pine crown )
  72. .setMinLength( 0.04 )
  73. .setDroop( 0.05 )
  74. .setUpPull( 0.42 ) // crown branches reach up toward the light
  75. .setGnarl( [ 0, 0.14, 0.24, 0.34 ] ) // dead-straight trunk, wispier twigs
  76. .setSectionLength( 0.34 )
  77. .setRadialSegments( 7 )
  78. .setRadiusExponent( 2.5 ) // slender bole, whippy thin twigs
  79. .setMinRadius( 0.0023 ) // very fine twigs
  80. .setTrunkClear( 0.72 ) // tall clean bole; the crown sits only at the top
  81. .build();
  82. variants.push( mesh.geometry );
  83. }
  84. const placements = variants.map( () => [] );
  85. const dummy = new THREE.Object3D();
  86. const cols = 13, rows = 12, spacing = 1.9;
  87. for ( let i = 0; i < cols; i ++ ) {
  88. for ( let j = 0; j < rows; j ++ ) {
  89. const v = Math.floor( Math.random() * variants.length );
  90. const x = ( i - cols / 2 ) * spacing + ( Math.random() - 0.5 ) * spacing * 0.8;
  91. const z = j * spacing - rows * spacing + 4.2 + ( Math.random() - 0.5 ) * spacing * 0.8;
  92. dummy.position.set( x, 0, z );
  93. const scale = 0.85 + Math.random() * 0.4;
  94. dummy.rotation.set( ( Math.random() - 0.5 ) * 0.05, Math.random() * Math.PI * 2, ( Math.random() - 0.5 ) * 0.05 ); // slight lean
  95. dummy.scale.set( scale, scale * ( 0.9 + Math.random() * 0.3 ), scale );
  96. dummy.updateMatrix();
  97. placements[ v ].push( dummy.matrix.clone() );
  98. }
  99. }
  100. variants.forEach( ( geometry, v ) => {
  101. const list = placements[ v ];
  102. const mesh = new THREE.InstancedMesh( geometry, material, list.length );
  103. for ( let k = 0; k < list.length; k ++ ) mesh.setMatrixAt( k, list[ k ] );
  104. mesh.instanceMatrix.needsUpdate = true;
  105. scene.add( mesh );
  106. } );
  107. // a couple of dominant trunks close to the camera to anchor the depth
  108. [ [ - 1.1, 4.9, 1.5, 1.1 ], [ 1.5, 4, 1.2, 0.3 ] ].forEach( ( [ x, z, s, ry ] ) => {
  109. const hero = new THREE.Mesh( variants[ variants.length - 1 ], material );
  110. hero.position.set( x, 0, z );
  111. hero.rotation.y = ry;
  112. hero.scale.setScalar( s );
  113. scene.add( hero );
  114. } );
  115. // ground
  116. const ground = new THREE.Mesh( new THREE.PlaneGeometry( 600, 600 ).rotateX( - Math.PI / 2 ), material );
  117. scene.add( ground );
  118. // renderer
  119. renderer = new THREE.WebGPURenderer( { antialias: true } );
  120. renderer.setPixelRatio( window.devicePixelRatio );
  121. renderer.setSize( window.innerWidth, window.innerHeight );
  122. renderer.setAnimationLoop( animate );
  123. renderer.inspector = new Inspector();
  124. document.body.appendChild( renderer.domElement );
  125. // controls
  126. controls = new FirstPersonControls( camera, renderer.domElement );
  127. controls.movementSpeed = 2;
  128. controls.lookSpeed = 0.1;
  129. controls.lookAt( - 0.2, 1.7, - 8 );
  130. renderPipeline = new THREE.RenderPipeline( renderer );
  131. // uniforms
  132. const density = reference( 'density', 'float', scene.fog );
  133. const scattering = uniform( 2 );
  134. // scene pass
  135. const scenePass = pass( scene, camera );
  136. const scenePassColor = scenePass.getTextureNode( 'output' );
  137. const scenePassViewZ = scenePass.getViewZNode();
  138. // blur pass (always downsampled to improve performance)
  139. const sceneColorBlurred = gaussianBlur( scenePassColor, vec2( scattering ), 4, { resolutionScale: 0.5 } );
  140. // composite
  141. const fogFactor = densityFogFactor( density ).context( { getViewZ: () => scenePassViewZ } );
  142. const compositeNode = mix( scenePassColor, sceneColorBlurred, fogFactor );
  143. renderPipeline.outputNode = compositeNode;
  144. // gui
  145. const gui = renderer.inspector.createParameters( 'Settings' );
  146. gui.add( scene.fog, 'density', 0.025, 0.16 ).step( 0.0005 ).name( 'fog density' );
  147. gui.add( scattering, 'value', 0, 5 ).name( 'scattering factor' );
  148. gui.add( params, 'scatteringEnabled' ).name( 'enable scattering' ).onChange( ( value ) => {
  149. if ( value === true ) {
  150. renderPipeline.outputNode = compositeNode;
  151. } else {
  152. renderPipeline.outputNode = scenePassColor;
  153. }
  154. renderPipeline.needsUpdate = true;
  155. } );
  156. window.addEventListener( 'resize', resize );
  157. }
  158. function resize() {
  159. camera.aspect = window.innerWidth / window.innerHeight;
  160. camera.updateProjectionMatrix();
  161. renderer.setSize( window.innerWidth, window.innerHeight );
  162. }
  163. function animate() {
  164. timer.update();
  165. controls.update( timer.getDelta() );
  166. renderPipeline.render();
  167. }
  168. </script>
  169. </body>
  170. </html>
粤ICP备19079148号