webgpu_custom_fog_background.html 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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.module.js",
  25. "three/addons/": "./jsm/",
  26. "three/nodes": "./jsm/nodes/Nodes.js"
  27. }
  28. }
  29. </script>
  30. <script type="module">
  31. import * as THREE from 'three';
  32. import WebGPURenderer from 'three/addons/renderers/webgpu/WebGPURenderer.js';
  33. import PostProcessing from 'three/addons/renderers/common/PostProcessing.js';
  34. import { pass, color, rangeFog } from 'three/nodes';
  35. import { RGBELoader } from 'three/addons/loaders/RGBELoader.js';
  36. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  37. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  38. let camera, scene, renderer;
  39. let postProcessing;
  40. init();
  41. function init() {
  42. const container = document.createElement( 'div' );
  43. document.body.appendChild( container );
  44. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.25, 20 );
  45. camera.position.set( - 1.8, 0.6, 2.7 );
  46. scene = new THREE.Scene();
  47. renderer = new WebGPURenderer( { antialias: true } );
  48. renderer.setPixelRatio( window.devicePixelRatio );
  49. renderer.setSize( window.innerWidth, window.innerHeight );
  50. //renderer.toneMapping = THREE.ACESFilmicToneMapping; // apply tone mapping in post processing
  51. container.appendChild( renderer.domElement );
  52. // post processing
  53. // render scene pass ( the same of css )
  54. const scenePass = pass( scene, camera );
  55. const scenePassViewZ = scenePass.getViewZNode();
  56. // background color
  57. const backgroundColor = color( 0x0066ff );
  58. // get fog factor from scene pass context
  59. // equivalent to: scene.fog = new THREE.Fog( 0x0066ff, 2.7, 4 );
  60. const fogFactor = rangeFog( null, 2.7, 4 ).context( { getViewZ: () => scenePassViewZ } );
  61. // tone mapping scene pass
  62. const scenePassTM = scenePass.toneMapping( THREE.ACESFilmicToneMapping );
  63. // mix fog from fog factor and background color
  64. const compose = fogFactor.mix( scenePassTM, backgroundColor );
  65. postProcessing = new PostProcessing( renderer );
  66. postProcessing.outputNode = compose;
  67. //
  68. new RGBELoader()
  69. .setPath( 'textures/equirectangular/' )
  70. .load( 'royal_esplanade_1k.hdr', function ( texture ) {
  71. texture.mapping = THREE.EquirectangularReflectionMapping;
  72. scene.environment = texture;
  73. // model
  74. const loader = new GLTFLoader().setPath( 'models/gltf/DamagedHelmet/glTF/' );
  75. loader.load( 'DamagedHelmet.gltf', function ( gltf ) {
  76. scene.add( gltf.scene );
  77. render();
  78. } );
  79. } );
  80. //
  81. const controls = new OrbitControls( camera, renderer.domElement );
  82. controls.minDistance = 2;
  83. controls.maxDistance = 5;
  84. controls.target.set( 0, - 0.1, - 0.2 );
  85. controls.update();
  86. controls.addEventListener( 'change', render ); // use if there is no animation loop
  87. window.addEventListener( 'resize', onWindowResize );
  88. }
  89. function onWindowResize() {
  90. camera.aspect = window.innerWidth / window.innerHeight;
  91. camera.updateProjectionMatrix();
  92. renderer.setSize( window.innerWidth, window.innerHeight );
  93. render();
  94. }
  95. //
  96. function render() {
  97. postProcessing.renderAsync();
  98. }
  99. </script>
  100. </body>
  101. </html>