webgpu_postprocessing_anamorphic.html 4.0 KB

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