webgpu_postprocessing_anamorphic.html 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - postprocessing anamorphic</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. <meta property="og:title" content="three.js webgpu - postprocessing anamorphic">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgpu_postprocessing_anamorphic.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgpu_postprocessing_anamorphic.jpg">
  11. <link type="text/css" rel="stylesheet" href="example.css">
  12. </head>
  13. <body>
  14. <div id="info">
  15. <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
  16. <div class="title-wrapper">
  17. <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>Anamorphic Lensflares</span>
  18. </div>
  19. <small>
  20. Battle Damaged Sci-fi Helmet by <a href="https://sketchfab.com/theblueturtle_" target="_blank" rel="noopener">theblueturtle_</a>.
  21. </small>
  22. </div>
  23. <script type="importmap">
  24. {
  25. "imports": {
  26. "three": "../build/three.webgpu.js",
  27. "three/webgpu": "../build/three.webgpu.js",
  28. "three/tsl": "../build/three.tsl.js",
  29. "three/addons/": "./jsm/"
  30. }
  31. }
  32. </script>
  33. <script type="module">
  34. import * as THREE from 'three/webgpu';
  35. import { pass, cubeTexture, screenUV, grayscale, uniform } from 'three/tsl';
  36. import { anamorphic } from 'three/addons/tsl/display/AnamorphicNode.js';
  37. import { HDRCubeTextureLoader } from 'three/addons/loaders/HDRCubeTextureLoader.js';
  38. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  39. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  40. import { Inspector } from 'three/addons/inspector/Inspector.js';
  41. let camera, scene, renderer;
  42. let renderPipeline;
  43. const params = {
  44. resolutionScale: 0.2
  45. };
  46. init();
  47. async function init() {
  48. const container = document.createElement( 'div' );
  49. document.body.appendChild( container );
  50. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.25, 20 );
  51. camera.position.set( - 1.8, - 0.6, 2.7 );
  52. scene = new THREE.Scene();
  53. const hdrUrls = [ 'px.hdr', 'nx.hdr', 'py.hdr', 'ny.hdr', 'pz.hdr', 'nz.hdr' ];
  54. const cube1Texture = await new HDRCubeTextureLoader()
  55. .setPath( './textures/cube/pisaHDR/' )
  56. .loadAsync( hdrUrls );
  57. scene.environment = cube1Texture;
  58. scene.backgroundNode = grayscale( cubeTexture( cube1Texture ).mul( screenUV.distance( .5 ).oneMinus().remapClamp( .1, 4 ) ) );
  59. const loader = new GLTFLoader().setPath( 'models/gltf/DamagedHelmet/glTF/' );
  60. loader.load( 'DamagedHelmet.gltf', function ( gltf ) {
  61. scene.add( gltf.scene );
  62. } );
  63. renderer = new THREE.WebGPURenderer( { antialias: true } );
  64. renderer.setPixelRatio( window.devicePixelRatio );
  65. renderer.setSize( window.innerWidth, window.innerHeight );
  66. renderer.toneMapping = THREE.LinearToneMapping;
  67. renderer.toneMappingExposure = 1;
  68. renderer.inspector = new Inspector();
  69. renderer.setAnimationLoop( render );
  70. container.appendChild( renderer.domElement );
  71. const controls = new OrbitControls( camera, renderer.domElement );
  72. controls.minDistance = 2;
  73. controls.maxDistance = 10;
  74. // post-processing
  75. const scenePass = pass( scene, camera );
  76. const threshold = uniform( 1.4 );
  77. const scaleNode = uniform( 5 );
  78. const intensity = uniform( 1 );
  79. const samples = 64;
  80. const anamorphicPass = anamorphic( scenePass.getTextureNode().toInspector( 'Color' ), threshold, scaleNode, samples ).toInspector( 'Anamorphic' );
  81. anamorphicPass.resolutionScale = params.resolutionScale; // 1 = full resolution
  82. renderPipeline = new THREE.RenderPipeline( renderer );
  83. renderPipeline.outputNode = scenePass.add( anamorphicPass.mul( intensity ) );
  84. //renderPipeline.outputNode = scenePass.add( anamorphicPass.getTextureNode().gaussianBlur() );
  85. // gui
  86. const gui = renderer.inspector.createParameters( 'Settings' );
  87. gui.add( intensity, 'value', 0, 4, 0.1 ).name( 'intensity' );
  88. gui.add( threshold, 'value', .8, 3, .001 ).name( 'threshold' );
  89. gui.add( scaleNode, 'value', 1, 10, 0.1 ).name( 'scale' );
  90. gui.add( params, 'resolutionScale', .1, 1, 0.1 ).name( 'resolution scale' ).onChange( value => anamorphicPass.resolutionScale = value );
  91. //
  92. window.addEventListener( 'resize', onWindowResize );
  93. }
  94. function onWindowResize() {
  95. camera.aspect = window.innerWidth / window.innerHeight;
  96. camera.updateProjectionMatrix();
  97. renderer.setSize( window.innerWidth, window.innerHeight );
  98. }
  99. //
  100. function render() {
  101. renderPipeline.render();
  102. }
  103. </script>
  104. </body>
  105. </html>
粤ICP备19079148号