webgpu_custom_fog_background.html 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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="main.css">
  8. <style>
  9. #info {
  10. background-color: #0066ff;
  11. }
  12. </style>
  13. </head>
  14. <body>
  15. <div id="info">
  16. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgpu - custom fog background<br />
  17. Battle Damaged Sci-fi Helmet by
  18. <a href="https://sketchfab.com/theblueturtle_" target="_blank" rel="noopener">theblueturtle_</a><br />
  19. <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>
  20. </div>
  21. <script type="importmap">
  22. {
  23. "imports": {
  24. "three": "../build/three.webgpu.js",
  25. "three/tsl": "../build/three.webgpu.js",
  26. "three/addons/": "./jsm/"
  27. }
  28. }
  29. </script>
  30. <script type="module">
  31. import * as THREE from 'three';
  32. import { pass, color, rangeFog } from 'three/tsl';
  33. import { RGBELoader } from 'three/addons/loaders/RGBELoader.js';
  34. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  35. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  36. let camera, scene, renderer;
  37. let postProcessing;
  38. init();
  39. function init() {
  40. const container = document.createElement( 'div' );
  41. document.body.appendChild( container );
  42. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.25, 20 );
  43. camera.position.set( - 1.8, 0.6, 2.7 );
  44. scene = new THREE.Scene();
  45. renderer = new THREE.WebGPURenderer( { antialias: true } );
  46. renderer.setPixelRatio( window.devicePixelRatio );
  47. renderer.setSize( window.innerWidth, window.innerHeight );
  48. //renderer.toneMapping = THREE.ACESFilmicToneMapping; // apply tone mapping in post processing
  49. container.appendChild( renderer.domElement );
  50. // post processing
  51. // render scene pass ( the same of css )
  52. const scenePass = pass( scene, camera );
  53. const scenePassViewZ = scenePass.getViewZNode();
  54. // background color
  55. const backgroundColor = color( 0x0066ff );
  56. // get fog factor from scene pass context
  57. // equivalent to: scene.fog = new THREE.Fog( 0x0066ff, 2.7, 4 );
  58. const fogFactor = rangeFog( null, 2.7, 4 ).context( { getViewZ: () => scenePassViewZ } );
  59. // tone mapping scene pass
  60. const scenePassTM = scenePass.toneMapping( THREE.ACESFilmicToneMapping );
  61. // mix fog from fog factor and background color
  62. const compose = fogFactor.mix( scenePassTM, backgroundColor );
  63. postProcessing = new THREE.PostProcessing( renderer );
  64. postProcessing.outputNode = compose;
  65. //
  66. new RGBELoader()
  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>