webgpu_custom_fog_background.html 4.0 KB

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