webgpu_postprocessing_retro.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - retro</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="example.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
  12. <div class="title-wrapper">
  13. <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>Retro</span>
  14. </div>
  15. <small>
  16. Retro post-processing effects, with scene based on <a href="https://threejs-journey.com/lessons/coffee-smoke-shader" target="_blank" rel="noopener">Three.js Journey</a> lesson.<br />
  17. Perlin noise texture from <a href="http://kitfox.com/projects/perlinNoiseMaker/" target="_blank" rel="noopener">Perlin Noise Maker</a>.
  18. </small>
  19. </div>
  20. <script type="importmap">
  21. {
  22. "imports": {
  23. "three": "../build/three.webgpu.js",
  24. "three/webgpu": "../build/three.webgpu.js",
  25. "three/tsl": "../build/three.tsl.js",
  26. "three/addons/": "./jsm/"
  27. }
  28. }
  29. </script>
  30. <script type="module">
  31. import * as THREE from 'three/webgpu';
  32. import { pass, mix, mul, oneMinus, positionLocal, smoothstep, texture, time, rotateUV, Fn, uv, vec2, vec3, vec4, uniform, posterize, floor, float, sin, fract, dot, step, color, normalWorld, length, atan, replaceDefaultUV } from 'three/tsl';
  33. import { retroPass } from 'three/addons/tsl/display/RetroPassNode.js';
  34. import { bayerDither } from 'three/addons/tsl/math/Bayer.js';
  35. import { scanlines, vignette, colorBleeding, barrelUV } from 'three/addons/tsl/display/CRT.js';
  36. import { circle } from 'three/addons/tsl/display/Shape.js';
  37. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  38. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  39. import { Inspector } from 'three/addons/inspector/Inspector.js';
  40. let camera, scene, renderer, renderPipeline, controls;
  41. init();
  42. async function init() {
  43. camera = new THREE.PerspectiveCamera( 25, window.innerWidth / window.innerHeight, 0.1, 100 );
  44. camera.position.set( 8, 5, 20 );
  45. scene = new THREE.Scene();
  46. // PS1-style background: gradient sky with simple stars
  47. const ps1Background = Fn( () => {
  48. // Flip Y coordinate for correct orientation
  49. const flippedY = normalWorld.y.negate();
  50. const skyUV = flippedY.mul( 0.5 ).add( 0.5 );
  51. // Simple gradient sky (dark blue at top to purple/orange at horizon)
  52. const topColor = color( 0x000033 ); // dark blue night sky
  53. const midColor = color( 0x330066 ); // purple
  54. const horizonColor = color( 0x663322 ); // warm orange/brown horizon
  55. // Two-step gradient (inverted - top is dark, horizon is warm)
  56. const skyGradient = mix(
  57. horizonColor,
  58. mix( midColor, topColor, skyUV.smoothstep( 0.4, 0.9 ) ),
  59. skyUV.smoothstep( 0.0, 0.4 )
  60. );
  61. // PS1-style "stars" using spherical coordinates
  62. const longitude = atan( normalWorld.x, normalWorld.z );
  63. const latitude = flippedY.asin(); // Use flipped Y for latitude too
  64. // More stars with smaller scale
  65. const starScale = float( 50.0 );
  66. const starUV = vec2( longitude.mul( starScale ), latitude.mul( starScale ) );
  67. const starCell = floor( starUV );
  68. // Hash for randomness
  69. const cellHash = fract( sin( dot( starCell, vec2( 12.9898, 78.233 ) ) ).mul( 43758.5453 ) );
  70. // Position within cell (0-1)
  71. const cellUV = fract( starUV );
  72. const toCenter = cellUV.sub( 0.5 );
  73. // Gemini-style star: bright center with soft glow + cross flare
  74. const distToCenter = length( toCenter );
  75. // Core (small bright center)
  76. const core = smoothstep( float( 0.08 ), float( 0.0 ), distToCenter );
  77. // Soft glow around
  78. const glow = smoothstep( float( 0.25 ), float( 0.0 ), distToCenter ).mul( 0.4 );
  79. // Cross/diamond flare effect
  80. const crossX = smoothstep( float( 0.15 ), float( 0.0 ), toCenter.x.abs() ).mul( smoothstep( float( 0.4 ), float( 0.0 ), toCenter.y.abs() ) );
  81. const crossY = smoothstep( float( 0.15 ), float( 0.0 ), toCenter.y.abs() ).mul( smoothstep( float( 0.4 ), float( 0.0 ), toCenter.x.abs() ) );
  82. const cross = crossX.add( crossY ).mul( 0.3 );
  83. // Combine star shape
  84. const starShape = core.add( glow ).add( cross );
  85. // More stars (lower threshold = more stars)
  86. const isStar = step( 0.85, cellHash );
  87. // Show stars from horizon up
  88. const aboveHorizon = smoothstep( float( - 0.2 ), float( 0.1 ), flippedY );
  89. // Star brightness varies + twinkle color
  90. const starIntensity = isStar.mul( aboveHorizon ).mul( starShape ).mul( cellHash.mul( 0.6 ).add( 0.4 ) );
  91. // Slight color variation (white to light blue)
  92. const starColor = mix( vec3( 1.0, 1.0, 0.95 ), vec3( 0.8, 0.9, 1.0 ), cellHash );
  93. // Combine sky and stars
  94. const finalColor = mix( skyGradient, starColor, starIntensity.clamp( 0.0, 1.0 ) );
  95. return finalColor;
  96. } )();
  97. scene.backgroundNode = ps1Background;
  98. // Loaders
  99. const gltfLoader = new GLTFLoader();
  100. const textureLoader = new THREE.TextureLoader();
  101. // baked model
  102. gltfLoader.load(
  103. './models/gltf/coffeeMug.glb',
  104. ( gltf ) => {
  105. gltf.scene.getObjectByName( 'baked' ).material.map.anisotropy = 8;
  106. scene.add( gltf.scene );
  107. }
  108. );
  109. // geometry
  110. const smokeGeometry = new THREE.PlaneGeometry( 1, 1, 16, 64 );
  111. smokeGeometry.translate( 0, 0.5, 0 );
  112. smokeGeometry.scale( 1.5, 6, 1.5 );
  113. // texture
  114. const noiseTexture = textureLoader.load( './textures/noises/perlin/128x128.png' );
  115. noiseTexture.wrapS = THREE.RepeatWrapping;
  116. noiseTexture.wrapT = THREE.RepeatWrapping;
  117. // material
  118. const smokeMaterial = new THREE.MeshBasicNodeMaterial( { transparent: true, side: THREE.DoubleSide, depthWrite: false } );
  119. // position
  120. smokeMaterial.positionNode = Fn( () => {
  121. // twist
  122. const twistNoiseUv = vec2( 0.5, uv().y.mul( 0.2 ).sub( time.mul( 0.005 ) ).mod( 1 ) );
  123. const twist = texture( noiseTexture, twistNoiseUv ).r.mul( 10 );
  124. positionLocal.xz.assign( rotateUV( positionLocal.xz, twist, vec2( 0 ) ) );
  125. // wind
  126. const windOffset = vec2(
  127. texture( noiseTexture, vec2( 0.25, time.mul( 0.01 ) ).mod( 1 ) ).r.sub( 0.5 ),
  128. texture( noiseTexture, vec2( 0.75, time.mul( 0.01 ) ).mod( 1 ) ).r.sub( 0.5 ),
  129. ).mul( uv().y.pow( 2 ).mul( 10 ) );
  130. positionLocal.addAssign( windOffset );
  131. return positionLocal;
  132. } )();
  133. // color
  134. smokeMaterial.colorNode = Fn( () => {
  135. // alpha
  136. const alphaNoiseUv = uv().mul( vec2( 0.5, 0.3 ) ).add( vec2( 0, time.mul( 0.03 ).negate() ) );
  137. const alpha = mul(
  138. // pattern
  139. texture( noiseTexture, alphaNoiseUv ).r.smoothstep( 0.4, 1 ),
  140. // edges fade
  141. smoothstep( 0, 0.1, uv().x ),
  142. smoothstep( 0, 0.1, oneMinus( uv().x ) ),
  143. smoothstep( 0, 0.1, uv().y ),
  144. smoothstep( 0, 0.1, oneMinus( uv().y ) )
  145. );
  146. // color
  147. const finalColor = mix( vec3( 0.6, 0.3, 0.2 ), vec3( 1, 1, 1 ), alpha.pow( 3 ) );
  148. return vec4( finalColor, alpha );
  149. } )();
  150. // mesh
  151. const smoke = new THREE.Mesh( smokeGeometry, smokeMaterial );
  152. smoke.position.y = 1.83;
  153. scene.add( smoke );
  154. // renderer
  155. renderer = new THREE.WebGPURenderer( { antialias: true } );
  156. renderer.setPixelRatio( window.devicePixelRatio );
  157. renderer.setSize( window.innerWidth, window.innerHeight );
  158. renderer.setAnimationLoop( animate );
  159. renderer.inspector = new Inspector();
  160. document.body.appendChild( renderer.domElement );
  161. // uniforms
  162. // PS1-style: 15-bit color (32 levels per channel)
  163. const colorDepthSteps = uniform( 32 );
  164. // CRT effect parameters (subtle for PS1 look)
  165. const scanlineIntensity = uniform( 0.08 ); // subtle scanlines
  166. const scanlineCount = uniform( 60 ); // match vertical resolution
  167. const scanlineSpeed = uniform( 0.05 ); // slow scroll
  168. const vignetteIntensity = uniform( 0.3 ); // subtle vignette
  169. const bleeding = uniform( 0.001 ); // minimal bleeding
  170. const curvature = uniform( 0.02 ); // subtle curve
  171. const affineDistortion = uniform( 0 ); // no affine distortion
  172. // render pipeline
  173. renderPipeline = new THREE.RenderPipeline( renderer );
  174. // retro pipeline
  175. const distortedUV = barrelUV( curvature );
  176. const distortedDelta = circle( curvature.add( .1 ).mul( 10 ), 1 ).mul( curvature ).mul( .05 );
  177. let retroPipeline = retroPass( scene, camera, { affineDistortion } );
  178. retroPipeline = replaceDefaultUV( distortedUV, retroPipeline );
  179. retroPipeline = colorBleeding( retroPipeline, bleeding.add( distortedDelta ) );
  180. retroPipeline = bayerDither( retroPipeline, colorDepthSteps );
  181. retroPipeline = posterize( retroPipeline, colorDepthSteps );
  182. retroPipeline = vignette( retroPipeline, vignetteIntensity, 0.6 );
  183. retroPipeline = scanlines( retroPipeline, scanlineIntensity, scanlineCount, scanlineSpeed );
  184. renderPipeline.outputNode = retroPipeline;
  185. // default pass (no post-processing)
  186. const defaultPass = pass( scene, camera );
  187. // controls
  188. controls = new OrbitControls( camera, renderer.domElement );
  189. controls.enableDamping = true;
  190. controls.minDistance = 0.1;
  191. controls.maxDistance = 50;
  192. controls.target.y = 1;
  193. // gui
  194. const gui = renderer.inspector.createParameters( 'Settings' );
  195. gui.add( { enabled: true }, 'enabled' ).onChange( v => {
  196. renderPipeline.outputNode = v ? retroPipeline : defaultPass;
  197. renderPipeline.needsUpdate = true;
  198. } ).name( 'Retro Pipeline' );
  199. gui.add( curvature, 'value', 0, 0.2, 0.01 ).name( 'Curvature' );
  200. gui.add( colorDepthSteps, 'value', 4, 32, 1 ).name( 'Color Depth' );
  201. gui.add( scanlineIntensity, 'value', 0, 1, 0.01 ).name( 'Scanlines' );
  202. gui.add( scanlineCount, 'value', 20, 480, 1 ).name( 'Scanline Count' );
  203. gui.add( scanlineSpeed, 'value', 0, .1, 0.01 ).name( 'Scanline Speed' );
  204. gui.add( vignetteIntensity, 'value', 0, 1, 0.01 ).name( 'Vignette' );
  205. gui.add( bleeding, 'value', 0, 0.005, 0.001 ).name( 'Color Bleeding' );
  206. gui.add( affineDistortion, 'value', 0, 1 ).name( 'Affine Distortion' );
  207. window.addEventListener( 'resize', onWindowResize );
  208. }
  209. function onWindowResize() {
  210. camera.aspect = window.innerWidth / window.innerHeight;
  211. camera.updateProjectionMatrix();
  212. renderer.setSize( window.innerWidth, window.innerHeight );
  213. }
  214. async function animate() {
  215. controls.update();
  216. renderPipeline.render();
  217. }
  218. </script>
  219. </body>
  220. </html>
粤ICP备19079148号