webgpu_postprocessing_anamorphic.html 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. <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 anamorphic<br />
  12. Battle Damaged Sci-fi Helmet by
  13. <a href="https://sketchfab.com/theblueturtle_" target="_blank" rel="noopener">theblueturtle_</a><br />
  14. </div>
  15. <script type="importmap">
  16. {
  17. "imports": {
  18. "three": "../build/three.webgpu.js",
  19. "three/tsl": "../build/three.webgpu.js",
  20. "three/addons/": "./jsm/"
  21. }
  22. }
  23. </script>
  24. <script type="module">
  25. import * as THREE from 'three';
  26. import { pass, cubeTexture, screenUV, grayscale, uniform } from 'three/tsl';
  27. import { anamorphic } from 'three/addons/tsl/display/AnamorphicNode.js';
  28. import { RGBMLoader } from 'three/addons/loaders/RGBMLoader.js';
  29. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  30. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  31. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  32. let camera, scene, renderer;
  33. let postProcessing;
  34. init();
  35. async function init() {
  36. const container = document.createElement( 'div' );
  37. document.body.appendChild( container );
  38. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.25, 20 );
  39. camera.position.set( - 1.8, - 0.6, 2.7 );
  40. scene = new THREE.Scene();
  41. const rgbmUrls = [ 'px.png', 'nx.png', 'py.png', 'ny.png', 'pz.png', 'nz.png' ];
  42. const cube1Texture = await new RGBMLoader()
  43. .setMaxRange( 16 )
  44. .setPath( './textures/cube/pisaRGBM16/' )
  45. .loadCubemapAsync( rgbmUrls );
  46. scene.environment = cube1Texture;
  47. scene.backgroundNode = grayscale( cubeTexture( cube1Texture ).mul( screenUV.distance( .5 ).oneMinus().remapClamp( .1, 4 ) ) );
  48. const loader = new GLTFLoader().setPath( 'models/gltf/DamagedHelmet/glTF/' );
  49. loader.load( 'DamagedHelmet.gltf', function ( gltf ) {
  50. scene.add( gltf.scene );
  51. } );
  52. renderer = new THREE.WebGPURenderer( { antialias: true } );
  53. renderer.setPixelRatio( window.devicePixelRatio );
  54. renderer.setSize( window.innerWidth, window.innerHeight );
  55. renderer.toneMapping = THREE.LinearToneMapping;
  56. renderer.toneMappingExposure = 1;
  57. renderer.setAnimationLoop( render );
  58. container.appendChild( renderer.domElement );
  59. const controls = new OrbitControls( camera, renderer.domElement );
  60. controls.minDistance = 2;
  61. controls.maxDistance = 10;
  62. // post-processing
  63. const scenePass = pass( scene, camera );
  64. const threshold = uniform( 1.4 );
  65. const scaleNode = uniform( 5 );
  66. const intensity = uniform( 1 );
  67. const samples = 64;
  68. const anamorphicPass = anamorphic( scenePass.getTextureNode(), threshold, scaleNode, samples );
  69. anamorphicPass.resolution = new THREE.Vector2( .2, .2 ); // 1 = full resolution
  70. postProcessing = new THREE.PostProcessing( renderer );
  71. postProcessing.outputNode = scenePass.add( anamorphicPass.mul( intensity ) );
  72. //postProcessing.outputNode = scenePass.add( anamorphicPass.getTextureNode().gaussianBlur() );
  73. // gui
  74. const gui = new GUI();
  75. gui.add( intensity, 'value', 0, 4, 0.1 ).name( 'intensity' );
  76. gui.add( threshold, 'value', .8, 3, .001 ).name( 'threshold' );
  77. gui.add( scaleNode, 'value', 1, 10, 0.1 ).name( 'scale' );
  78. gui.add( anamorphicPass.resolution, 'x', .1, 1, 0.1 ).name( 'resolution' ).onChange( ( v ) => anamorphicPass.resolution.y = v );
  79. //
  80. window.addEventListener( 'resize', onWindowResize );
  81. }
  82. function onWindowResize() {
  83. camera.aspect = window.innerWidth / window.innerHeight;
  84. camera.updateProjectionMatrix();
  85. renderer.setSize( window.innerWidth, window.innerHeight );
  86. }
  87. //
  88. function render() {
  89. postProcessing.render();
  90. }
  91. </script>
  92. </body>
  93. </html>