webgpu_postprocessing_retro.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. <meta property="og:title" content="three.js webgpu - retro">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgpu_postprocessing_retro.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgpu_postprocessing_retro.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>Retro</span>
  18. </div>
  19. <small>
  20. 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 />
  21. Perlin noise texture from <a href="http://kitfox.com/projects/perlinNoiseMaker/" target="_blank" rel="noopener">Perlin Noise Maker</a>.
  22. </small>
  23. </div>
  24. <script type="importmap">
  25. {
  26. "imports": {
  27. "three": "../build/three.webgpu.js",
  28. "three/webgpu": "../build/three.webgpu.js",
  29. "three/tsl": "../build/three.tsl.js",
  30. "three/addons/": "./jsm/"
  31. }
  32. }
  33. </script>
  34. <script type="module">
  35. import * as THREE from 'three/webgpu';
  36. 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, screenSize } from 'three/tsl';
  37. import { retroPass } from 'three/addons/tsl/display/RetroPassNode.js';
  38. import { bayerDither } from 'three/addons/tsl/math/Bayer.js';
  39. import { scanlines, vignette, colorBleeding, barrelUV } from 'three/addons/tsl/display/CRT.js';
  40. import { circle } from 'three/addons/tsl/display/Shape.js';
  41. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  42. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  43. import { HDRLoader } from 'three/addons/loaders/HDRLoader.js';
  44. import { Inspector } from 'three/addons/inspector/Inspector.js';
  45. let camera, scene, renderer, renderPipeline, controls;
  46. let environment;
  47. let currentModel;
  48. init();
  49. async function init() {
  50. camera = new THREE.PerspectiveCamera( 25, window.innerWidth / window.innerHeight, 0.1, 100 );
  51. camera.position.set( 8, 5, 20 );
  52. scene = new THREE.Scene();
  53. // PS1-style background: gradient sky with simple stars
  54. const ps1Background = Fn( () => {
  55. // Flip Y coordinate for correct orientation
  56. const flippedY = normalWorld.y.negate();
  57. const skyUV = flippedY.mul( 0.5 ).add( 0.5 );
  58. // Simple gradient sky (dark blue at top to purple/orange at horizon)
  59. const topColor = color( 0x000033 ); // dark blue night sky
  60. const midColor = color( 0x330066 ); // purple
  61. const horizonColor = color( 0x663322 ); // warm orange/brown horizon
  62. // Two-step gradient (inverted - top is dark, horizon is warm)
  63. const skyGradient = mix(
  64. horizonColor,
  65. mix( midColor, topColor, skyUV.smoothstep( 0.4, 0.9 ) ),
  66. skyUV.smoothstep( 0.0, 0.4 )
  67. );
  68. // PS1-style "stars" using spherical coordinates
  69. const longitude = atan( normalWorld.x, normalWorld.z );
  70. const latitude = flippedY.asin(); // Use flipped Y for latitude too
  71. // More stars with smaller scale
  72. const starScale = float( 50.0 );
  73. const starUV = vec2( longitude.mul( starScale ), latitude.mul( starScale ) );
  74. const starCell = floor( starUV );
  75. // Hash for randomness
  76. const cellHash = fract( sin( dot( starCell, vec2( 12.9898, 78.233 ) ) ).mul( 43758.5453 ) );
  77. // Position within cell (0-1)
  78. const cellUV = fract( starUV );
  79. const toCenter = cellUV.sub( 0.5 );
  80. // Gemini-style star: bright center with soft glow + cross flare
  81. const distToCenter = length( toCenter );
  82. // Core (small bright center)
  83. const core = smoothstep( float( 0.08 ), float( 0.0 ), distToCenter );
  84. // Soft glow around
  85. const glow = smoothstep( float( 0.25 ), float( 0.0 ), distToCenter ).mul( 0.4 );
  86. // Cross/diamond flare effect
  87. const crossX = smoothstep( float( 0.15 ), float( 0.0 ), toCenter.x.abs() ).mul( smoothstep( float( 0.4 ), float( 0.0 ), toCenter.y.abs() ) );
  88. const crossY = smoothstep( float( 0.15 ), float( 0.0 ), toCenter.y.abs() ).mul( smoothstep( float( 0.4 ), float( 0.0 ), toCenter.x.abs() ) );
  89. const cross = crossX.add( crossY ).mul( 0.3 );
  90. // Combine star shape
  91. const starShape = core.add( glow ).add( cross );
  92. // More stars (lower threshold = more stars)
  93. const isStar = step( 0.85, cellHash );
  94. // Show stars from horizon up
  95. const aboveHorizon = smoothstep( float( - 0.2 ), float( 0.1 ), flippedY );
  96. // Star brightness varies + twinkle color
  97. const starIntensity = isStar.mul( aboveHorizon ).mul( starShape ).mul( cellHash.mul( 0.6 ).add( 0.4 ) );
  98. // Slight color variation (white to light blue)
  99. const starColor = mix( vec3( 1.0, 1.0, 0.95 ), vec3( 0.8, 0.9, 1.0 ), cellHash );
  100. // Combine sky and stars
  101. const finalColor = mix( skyGradient, starColor, starIntensity.clamp( 0.0, 1.0 ) );
  102. return finalColor;
  103. } )();
  104. scene.backgroundNode = ps1Background;
  105. // Loaders
  106. const gltfLoader = new GLTFLoader();
  107. const textureLoader = new THREE.TextureLoader();
  108. // Model
  109. const models = {
  110. 'Coffee Mug': 'models/gltf/coffeeMug.glb',
  111. 'Damaged Helmet': 'models/gltf/DamagedHelmet/glTF/DamagedHelmet.gltf',
  112. };
  113. function loadModel( name ) {
  114. function loadEnvironment() {
  115. if ( environment ) return;
  116. environment = new HDRLoader()
  117. .setPath( 'textures/equirectangular/' )
  118. .load( 'venice_sunset_1k.hdr', ( texture ) => {
  119. texture.mapping = THREE.EquirectangularReflectionMapping;
  120. scene.environment = texture;
  121. // re-invalidate retro pass textures
  122. retro.dispose();
  123. } );
  124. }
  125. if ( currentModel ) {
  126. scene.remove( currentModel );
  127. currentModel.traverse( ( child ) => {
  128. if ( child.isMesh ) {
  129. child.geometry.dispose();
  130. child.material.dispose();
  131. }
  132. } );
  133. }
  134. gltfLoader.load( models[ name ], ( gltf ) => {
  135. currentModel = gltf.scene;
  136. currentModel.position.set( 0, 0, 0 );
  137. smoke.visible = false;
  138. if ( name === 'Damaged Helmet' ) {
  139. loadEnvironment();
  140. currentModel.scale.setScalar( 3 );
  141. currentModel.position.y = 1;
  142. } else if ( name === 'Coffee Mug' ) {
  143. smoke.visible = true;
  144. }
  145. scene.add( currentModel );
  146. } );
  147. }
  148. loadModel( 'Coffee Mug' );
  149. // lighting
  150. const ambientLight = new THREE.AmbientLight( 0x404040, 2 );
  151. scene.add( ambientLight );
  152. const directionalLight = new THREE.DirectionalLight( 0xffffff, 3 );
  153. directionalLight.position.set( 5, 10, 5 );
  154. scene.add( directionalLight );
  155. const pointLight = new THREE.PointLight( 0xff6600, 5, 20 );
  156. pointLight.position.set( - 3, 3, 2 );
  157. scene.add( pointLight );
  158. // geometry
  159. const smokeGeometry = new THREE.PlaneGeometry( 1, 1, 16, 64 );
  160. smokeGeometry.translate( 0, 0.5, 0 );
  161. smokeGeometry.scale( 1.5, 6, 1.5 );
  162. // texture
  163. const noiseTexture = textureLoader.load( './textures/noises/perlin/128x128.png' );
  164. noiseTexture.wrapS = THREE.RepeatWrapping;
  165. noiseTexture.wrapT = THREE.RepeatWrapping;
  166. // material
  167. const smokeMaterial = new THREE.MeshBasicNodeMaterial( { transparent: true, side: THREE.DoubleSide, depthWrite: false } );
  168. // position
  169. smokeMaterial.positionNode = Fn( () => {
  170. // twist
  171. const twistNoiseUv = vec2( 0.5, uv().y.mul( 0.2 ).sub( time.mul( 0.005 ) ).mod( 1 ) );
  172. const twist = texture( noiseTexture, twistNoiseUv ).r.mul( 10 );
  173. positionLocal.xz.assign( rotateUV( positionLocal.xz, twist, vec2( 0 ) ) );
  174. // wind
  175. const windOffset = vec2(
  176. texture( noiseTexture, vec2( 0.25, time.mul( 0.01 ) ).mod( 1 ) ).r.sub( 0.5 ),
  177. texture( noiseTexture, vec2( 0.75, time.mul( 0.01 ) ).mod( 1 ) ).r.sub( 0.5 ),
  178. ).mul( uv().y.pow( 2 ).mul( 10 ) );
  179. positionLocal.addAssign( windOffset );
  180. return positionLocal;
  181. } )();
  182. // color
  183. smokeMaterial.colorNode = Fn( () => {
  184. // alpha
  185. const alphaNoiseUv = uv().mul( vec2( 0.5, 0.3 ) ).add( vec2( 0, time.mul( 0.03 ).negate() ) );
  186. const alpha = mul(
  187. // pattern
  188. texture( noiseTexture, alphaNoiseUv ).r.smoothstep( 0.4, 1 ),
  189. // edges fade
  190. smoothstep( 0, 0.1, uv().x ),
  191. smoothstep( 0, 0.1, oneMinus( uv().x ) ),
  192. smoothstep( 0, 0.1, uv().y ),
  193. smoothstep( 0, 0.1, oneMinus( uv().y ) )
  194. );
  195. // color
  196. const finalColor = mix( vec3( 0.6, 0.3, 0.2 ), vec3( 1, 1, 1 ), alpha.pow( 3 ) );
  197. return vec4( finalColor, alpha );
  198. } )();
  199. // mesh
  200. const smoke = new THREE.Mesh( smokeGeometry, smokeMaterial );
  201. smoke.position.y = 1.83;
  202. scene.add( smoke );
  203. // renderer
  204. renderer = new THREE.WebGPURenderer( { antialias: true } );
  205. renderer.setPixelRatio( window.devicePixelRatio );
  206. renderer.setSize( window.innerWidth, window.innerHeight );
  207. renderer.setAnimationLoop( animate );
  208. renderer.inspector = new Inspector();
  209. document.body.appendChild( renderer.domElement );
  210. // uniforms
  211. // PS1-style: 15-bit color (32 levels per channel)
  212. const colorDepthSteps = uniform( 32 );
  213. // CRT effect parameters (subtle for PS1 look)
  214. const scanlineIntensity = uniform( 0.3 ); // subtle scanlines
  215. const scanlineDensity = uniform( 1 ); // 0.1-1: normalized scanline density (1 = full screen resolution)
  216. const scanlineSpeed = uniform( 0.0 ); // no scanline movement
  217. const vignetteIntensity = uniform( 0.3 ); // subtle vignette
  218. const bleeding = uniform( 0.001 ); // minimal bleeding
  219. const curvature = uniform( 0.02 ); // subtle curve
  220. const affineDistortion = uniform( 0 ); // no affine distortion
  221. // render pipeline
  222. renderPipeline = new THREE.RenderPipeline( renderer );
  223. // retro pipeline
  224. const distortedUV = barrelUV( curvature );
  225. const distortedDelta = circle( curvature.add( .1 ).mul( 10 ), 1 ).mul( curvature ).mul( .05 );
  226. const retro = retroPass( scene, camera, { affineDistortion } );
  227. let retroPipeline = retro;
  228. retroPipeline = replaceDefaultUV( distortedUV, retroPipeline );
  229. retroPipeline = colorBleeding( retroPipeline, bleeding.add( distortedDelta ) );
  230. retroPipeline = bayerDither( retroPipeline, colorDepthSteps );
  231. retroPipeline = posterize( retroPipeline, colorDepthSteps );
  232. retroPipeline = vignette( retroPipeline, vignetteIntensity, 0.6 );
  233. retroPipeline = scanlines( retroPipeline, scanlineIntensity, screenSize.y.mul( scanlineDensity ), scanlineSpeed );
  234. renderPipeline.outputNode = retroPipeline;
  235. // default pass (no post-processing)
  236. const defaultPass = pass( scene, camera );
  237. // controls
  238. controls = new OrbitControls( camera, renderer.domElement );
  239. controls.enableDamping = true;
  240. controls.minDistance = 0.1;
  241. controls.maxDistance = 50;
  242. controls.target.y = 1;
  243. // gui
  244. const gui = renderer.inspector.createParameters( 'Settings' );
  245. gui.add( { model: 'Coffee Mug' }, 'model', Object.keys( models ) ).name( 'Model' ).onChange( loadModel );
  246. gui.add( { enabled: true }, 'enabled' ).onChange( v => {
  247. renderPipeline.outputNode = v ? retroPipeline : defaultPass;
  248. renderPipeline.needsUpdate = true;
  249. } ).name( 'Retro Pipeline' );
  250. gui.add( curvature, 'value', 0, 0.2, 0.01 ).name( 'Curvature' );
  251. gui.add( colorDepthSteps, 'value', 4, 32, 1 ).name( 'Color Depth' );
  252. gui.add( scanlineIntensity, 'value', 0, 1, 0.01 ).name( 'Scanlines' );
  253. gui.add( scanlineDensity, 'value', 0.02, 1, 0.01 ).name( 'Scanline Density' );
  254. gui.add( scanlineSpeed, 'value', 0, .1, 0.01 ).name( 'Scanline Speed' );
  255. gui.add( vignetteIntensity, 'value', 0, 1, 0.01 ).name( 'Vignette' );
  256. gui.add( bleeding, 'value', 0, 0.005, 0.001 ).name( 'Color Bleeding' );
  257. gui.add( affineDistortion, 'value', 0, 1 ).name( 'Affine Distortion' );
  258. gui.add( retro, 'filterTextures' ).name( 'Filter Textures' ).onChange( () => {
  259. retro.dispose();
  260. } );
  261. window.addEventListener( 'resize', onWindowResize );
  262. }
  263. function onWindowResize() {
  264. camera.aspect = window.innerWidth / window.innerHeight;
  265. camera.updateProjectionMatrix();
  266. renderer.setSize( window.innerWidth, window.innerHeight );
  267. }
  268. async function animate() {
  269. controls.update();
  270. renderPipeline.render();
  271. }
  272. </script>
  273. </body>
  274. </html>
粤ICP备19079148号