webgpu_postprocessing_lensflare.html 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - postprocessing lensflares</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. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgpu - postprocessing lensflares<br />
  12. <a href="https://skfb.ly/6SqUF" target="_blank" rel="noopener">Space Ship Hallway</a> by
  13. <a href="https://sketchfab.com/yeeyeeman" target="_blank" rel="noopener">yeeyeeman</a> is licensed under <a href="https://creativecommons.org/licenses/by/4.0/" target="_blank" rel="noopener">Creative Commons Attribution</a>.<br />
  14. <a href="https://www.spacespheremaps.com/planetary-spheremaps/" target="_blank" rel="noopener">Ice Planet Close</a> from <a href="https://www.spacespheremaps.com/" target="_blank" rel="noopener">Space Spheremaps</a>.
  15. </div>
  16. <script type="importmap">
  17. {
  18. "imports": {
  19. "three": "../build/three.webgpu.js",
  20. "three/webgpu": "../build/three.webgpu.js",
  21. "three/tsl": "../build/three.tsl.js",
  22. "three/addons/": "./jsm/"
  23. }
  24. }
  25. </script>
  26. <script type="module">
  27. import * as THREE from 'three';
  28. import { pass, mrt, output, emissive, uniform } from 'three/tsl';
  29. import { bloom } from 'three/addons/tsl/display/BloomNode.js';
  30. import { lensflare } from 'three/addons/tsl/display/LensflareNode.js';
  31. import { gaussianBlur } from 'three/addons/tsl/display/GaussianBlurNode.js';
  32. import { UltraHDRLoader } from 'three/addons/loaders/UltraHDRLoader.js';
  33. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  34. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  35. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  36. import Stats from 'three/addons/libs/stats.module.js';
  37. let camera, scene, renderer, controls, stats;
  38. let postProcessing;
  39. init();
  40. async function init() {
  41. const container = document.createElement( 'div' );
  42. document.body.appendChild( container );
  43. //
  44. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 100 );
  45. camera.position.set( 0, 0.5, - 0.5 );
  46. scene = new THREE.Scene();
  47. const texture = await new UltraHDRLoader().loadAsync( 'textures/equirectangular/ice_planet_close.jpg' );
  48. texture.mapping = THREE.EquirectangularReflectionMapping;
  49. scene.background = texture;
  50. scene.environment = texture;
  51. scene.backgroundIntensity = 2;
  52. scene.environmentIntensity = 15;
  53. // model
  54. const loader = new GLTFLoader();
  55. const gltf = await loader.loadAsync( 'models/gltf/space_ship_hallway.glb' );
  56. const object = gltf.scene;
  57. const aabb = new THREE.Box3().setFromObject( object );
  58. const center = aabb.getCenter( new THREE.Vector3() );
  59. object.position.x += ( object.position.x - center.x );
  60. object.position.y += ( object.position.y - center.y );
  61. object.position.z += ( object.position.z - center.z );
  62. scene.add( object );
  63. //
  64. renderer = new THREE.WebGPURenderer();
  65. renderer.setPixelRatio( window.devicePixelRatio );
  66. renderer.setSize( window.innerWidth, window.innerHeight );
  67. renderer.setAnimationLoop( render );
  68. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  69. container.appendChild( renderer.domElement );
  70. //
  71. const scenePass = pass( scene, camera );
  72. scenePass.setMRT( mrt( {
  73. output,
  74. emissive
  75. } ) );
  76. const outputPass = scenePass.getTextureNode();
  77. const emissivePass = scenePass.getTextureNode( 'emissive' );
  78. const bloomPass = bloom( emissivePass, 1, 1 );
  79. const threshold = uniform( 0.5 );
  80. const ghostAttenuationFactor = uniform( 25 );
  81. const ghostSpacing = uniform( 0.25 );
  82. const flarePass = lensflare( bloomPass, {
  83. threshold,
  84. ghostAttenuationFactor,
  85. ghostSpacing
  86. } );
  87. const blurPass = gaussianBlur( flarePass, 8 ); // optional (blurring produces better flare quality but also adds some overhead)
  88. postProcessing = new THREE.PostProcessing( renderer );
  89. postProcessing.outputNode = outputPass.add( bloomPass ).add( blurPass );
  90. //
  91. controls = new OrbitControls( camera, renderer.domElement );
  92. controls.enableDamping = true;
  93. controls.enablePan = false;
  94. controls.enableZoom = false;
  95. controls.target.copy( camera.position );
  96. controls.target.z -= 0.01;
  97. controls.update();
  98. window.addEventListener( 'resize', onWindowResize );
  99. //
  100. stats = new Stats();
  101. document.body.appendChild( stats.dom );
  102. //
  103. const gui = new GUI();
  104. const bloomFolder = gui.addFolder( 'bloom' );
  105. bloomFolder.add( bloomPass.strength, 'value', 0.0, 2.0 ).name( 'strength' );
  106. bloomFolder.add( bloomPass.radius, 'value', 0.0, 1.0 ).name( 'radius' );
  107. const lensflareFolder = gui.addFolder( 'lensflare' );
  108. lensflareFolder.add( threshold, 'value', 0.0, 1.0 ).name( 'threshold' );
  109. lensflareFolder.add( ghostAttenuationFactor, 'value', 10.0, 50.0 ).name( 'attenuation' );
  110. lensflareFolder.add( ghostSpacing, 'value', 0.0, 0.3 ).name( 'spacing' );
  111. const toneMappingFolder = gui.addFolder( 'tone mapping' );
  112. toneMappingFolder.add( renderer, 'toneMappingExposure', 0.1, 2 ).name( 'exposure' );
  113. }
  114. function onWindowResize() {
  115. camera.aspect = window.innerWidth / window.innerHeight;
  116. camera.updateProjectionMatrix();
  117. renderer.setSize( window.innerWidth, window.innerHeight );
  118. }
  119. //
  120. function render() {
  121. stats.update();
  122. controls.update();
  123. postProcessing.render();
  124. }
  125. </script>
  126. </body>
  127. </html>
粤ICP备19079148号