webgpu_custom_fog_background.html 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - custom fog background</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>Custom Fog Background</span>
  14. </div>
  15. <small>
  16. Custom Fog Background via Post-Processing.<br/>
  17. Battle Damaged Sci-fi Helmet by <a href="https://sketchfab.com/theblueturtle_" target="_blank" rel="noopener">theblueturtle_</a>.
  18. <a href="https://hdrihaven.com/hdri/?h=royal_esplanade" target="_blank" rel="noopener">Royal Esplanade</a> by <a href="https://hdrihaven.com/" target="_blank" rel="noopener">HDRI Haven</a>.
  19. </small>
  20. </div>
  21. <script type="importmap">
  22. {
  23. "imports": {
  24. "three": "../build/three.webgpu.js",
  25. "three/webgpu": "../build/three.webgpu.js",
  26. "three/tsl": "../build/three.tsl.js",
  27. "three/addons/": "./jsm/"
  28. }
  29. }
  30. </script>
  31. <script type="module">
  32. import * as THREE from 'three/webgpu';
  33. import { pass, color, rangeFogFactor } from 'three/tsl';
  34. import { HDRLoader } from 'three/addons/loaders/HDRLoader.js';
  35. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  36. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  37. let camera, scene, renderer;
  38. let postProcessing;
  39. init();
  40. function init() {
  41. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.25, 20 );
  42. camera.position.set( - 1.8, 0.6, 2.7 );
  43. scene = new THREE.Scene();
  44. renderer = new THREE.WebGPURenderer( { antialias: true } );
  45. renderer.setPixelRatio( window.devicePixelRatio );
  46. renderer.setSize( window.innerWidth, window.innerHeight );
  47. renderer.toneMapping = THREE.NoToneMapping; // apply tone mapping in post processing, instead
  48. document.body.appendChild( renderer.domElement );
  49. // post processing
  50. // render scene pass
  51. const scenePass = pass( scene, camera );
  52. const scenePassViewZ = scenePass.getViewZNode();
  53. // fog color
  54. const fogColor = color( 0x4080cc ); // in sRGB color space
  55. // get fog factor from the scene pass context
  56. // equivalent to: scene.fog = new THREE.Fog( 0x4080cc, 2.7, 4 );
  57. const fogFactor = rangeFogFactor( 2.7, 4 ).context( { getViewZ: () => scenePassViewZ } );
  58. // tone-mapped scene pass
  59. const scenePassTM = scenePass.toneMapping( THREE.ACESFilmicToneMapping, 1 );
  60. // mix fog using fog factor and fog color
  61. const compose = fogFactor.mix( scenePassTM, fogColor );
  62. postProcessing = new THREE.PostProcessing( renderer );
  63. postProcessing.outputColorTransform = true; // no tone mapping will be applied, only the default color space transform
  64. postProcessing.outputNode = compose;
  65. //
  66. new HDRLoader()
  67. .setPath( 'textures/equirectangular/' )
  68. .load( 'royal_esplanade_1k.hdr', function ( texture ) {
  69. texture.mapping = THREE.EquirectangularReflectionMapping;
  70. scene.environment = texture;
  71. // model
  72. const loader = new GLTFLoader().setPath( 'models/gltf/DamagedHelmet/glTF/' );
  73. loader.load( 'DamagedHelmet.gltf', function ( gltf ) {
  74. scene.add( gltf.scene );
  75. render();
  76. } );
  77. } );
  78. //
  79. const controls = new OrbitControls( camera, renderer.domElement );
  80. controls.minDistance = 2;
  81. controls.maxDistance = 5;
  82. controls.target.set( 0, - 0.1, - 0.2 );
  83. controls.update();
  84. controls.addEventListener( 'change', render ); // use if there is no animation loop
  85. window.addEventListener( 'resize', onWindowResize );
  86. }
  87. function onWindowResize() {
  88. camera.aspect = window.innerWidth / window.innerHeight;
  89. camera.updateProjectionMatrix();
  90. renderer.setSize( window.innerWidth, window.innerHeight );
  91. render();
  92. }
  93. //
  94. function render() {
  95. postProcessing.renderAsync();
  96. }
  97. </script>
  98. </body>
  99. </html>
粤ICP备19079148号