webgpu_postprocessing_anamorphic.html 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. Anamorphic bloom filter using custom high pass filter.
  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, screenUV, uniform, uv, vec2, vec3, color, Fn, vec4, Loop, float, mix, luminance, smoothstep, rtt, time, viewportSize, instanceIndex, positionLocal } from 'three/tsl';
  36. import { bloom } from 'three/addons/tsl/display/BloomNode.js';
  37. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  38. import { Inspector } from 'three/addons/inspector/Inspector.js';
  39. let camera, scene, renderer, instancedMesh, controls;
  40. let renderPipeline;
  41. const _dummy = new THREE.Object3D();
  42. init();
  43. async function init() {
  44. const container = document.createElement( 'div' );
  45. document.body.appendChild( container );
  46. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.25, 250 );
  47. camera.position.set( 0, 0, 20 );
  48. scene = new THREE.Scene();
  49. scene.backgroundNode = Fn( () => {
  50. const dist = screenUV.distance( 0.5 ).mul( 2.0 );
  51. return mix( color( 0x111111 ), color( 0x000000 ), dist );
  52. } )();
  53. const timeScale = uniform( 0.5 );
  54. const material = new THREE.MeshBasicNodeMaterial( {
  55. color: 0xffffff,
  56. // Animate the spheres up and down continuously using TSL and their instanceIndex as a phase offset
  57. positionNode: positionLocal.add( vec3( 0, time.add( float( instanceIndex ).mul( 0.5 ) ).mul( timeScale ).sin().mul( 5.0 ), 0 ) )
  58. } );
  59. const maxCount = 200;
  60. instancedMesh = new THREE.InstancedMesh( new THREE.SphereGeometry( 0.1, 32, 32 ), material, maxCount );
  61. const _color = new THREE.Color();
  62. for ( let i = 0; i < maxCount; i ++ ) {
  63. _dummy.position.x = ( Math.random() - 0.5 ) * 20;
  64. _dummy.position.y = ( Math.random() - 0.5 ) * 20;
  65. _dummy.position.z = ( Math.random() - 0.5 ) * 20;
  66. _dummy.updateMatrix();
  67. instancedMesh.setMatrixAt( i, _dummy.matrix );
  68. _color.setHex( Math.random() * 0xffffff );
  69. instancedMesh.setColorAt( i, _color );
  70. }
  71. scene.add( instancedMesh );
  72. renderer = new THREE.WebGPURenderer( { antialias: true } );
  73. renderer.setPixelRatio( window.devicePixelRatio );
  74. renderer.setSize( window.innerWidth, window.innerHeight );
  75. renderer.toneMapping = THREE.NeutralToneMapping;
  76. renderer.toneMappingExposure = 1;
  77. renderer.inspector = new Inspector();
  78. renderer.setAnimationLoop( render );
  79. container.appendChild( renderer.domElement );
  80. controls = new OrbitControls( camera, renderer.domElement );
  81. controls.minDistance = 2;
  82. controls.maxDistance = 25;
  83. // post-processing
  84. const scenePass = pass( scene, camera );
  85. const intensity = uniform( 5.0 );
  86. const tintColor = uniform( new THREE.Color( 0x7a8aff ) );
  87. const threshold = uniform( 0.3 );
  88. const radius = uniform( 0.0 );
  89. const samples = uniform( 80 );
  90. const bloomPass = bloom( scenePass.getTextureNode().toInspector( 'Color' ), intensity, radius, threshold );
  91. bloomPass.setResolutionScale( 0.25 );
  92. // Custom high pass for anamorphic bloom
  93. bloomPass.highPassFn = Fn( ( { input, threshold, smoothWidth } ) => {
  94. // Extract bright areas to a texture (computed once, sampled many times)
  95. const v = luminance( input.rgb );
  96. const alpha = smoothstep( threshold, threshold.add( smoothWidth ), v );
  97. const brightPass = rtt( mix( vec4( 0 ), input, alpha ), null, null, {
  98. wrapS: THREE.MirroredRepeatWrapping,
  99. wrapT: THREE.MirroredRepeatWrapping
  100. } );
  101. // Horizontal blur
  102. const total = vec4( 0 );
  103. const halfSamples = samples.div( 2 );
  104. const invSize = vec2( 1.0 ).div( viewportSize );
  105. Loop( { start: halfSamples.negate(), end: halfSamples }, ( { i } ) => {
  106. let softness = float( i ).abs().div( halfSamples ).oneMinus();
  107. softness = softness.pow( 2.0 );
  108. const shiftedUV = vec2( uv().x.add( invSize.x.mul( i ).mul( 4.0 ) ), uv().y );
  109. total.addAssign( brightPass.sample( shiftedUV ).mul( softness ) );
  110. } );
  111. return total.div( samples.div( 3.0 ) );
  112. } );
  113. const anamorphicPass = bloomPass.mul( tintColor ).toInspector( 'Anamorphic' );
  114. renderPipeline = new THREE.RenderPipeline( renderer );
  115. renderPipeline.outputNode = scenePass.add( anamorphicPass );
  116. // gui
  117. const gui = renderer.inspector.createParameters( 'Settings' );
  118. gui.add( intensity, 'value', 0, 10 ).name( 'intensity' );
  119. gui.add( threshold, 'value', 0, .9 ).name( 'threshold' );
  120. gui.add( samples, 'value', 2, 128, 1 ).name( 'samples' );
  121. gui.addColor( tintColor, 'value' ).name( 'tint color' );
  122. gui.add( radius, 'value', 0, 1, 0.01 ).name( 'bloom radius' );
  123. gui.add( timeScale, 'value', 0, 1 ).name( 'time scale' );
  124. //
  125. window.addEventListener( 'resize', onWindowResize );
  126. }
  127. function onWindowResize() {
  128. camera.aspect = window.innerWidth / window.innerHeight;
  129. camera.updateProjectionMatrix();
  130. renderer.setSize( window.innerWidth, window.innerHeight );
  131. }
  132. //
  133. function render() {
  134. controls.update();
  135. renderPipeline.render();
  136. }
  137. </script>
  138. </body>
  139. </html>
粤ICP备19079148号