webgpu_postprocessing_ssr.html 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <head>
  5. <title>three.js webgpu - postprocessing - Screen Space Reflection</title>
  6. <meta charset="utf-8">
  7. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  8. <link type="text/css" rel="stylesheet" href="main.css">
  9. </head>
  10. <body>
  11. <div id="container"></div>
  12. <div id="info">
  13. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> -
  14. SSRPass demo by <a href="https://github.com/gonnavis" target="_blank">Vis</a>.<br />
  15. </div>
  16. <script type="importmap">
  17. {
  18. "imports": {
  19. "three": "../build/three.webgpu.js",
  20. "three/tsl": "../build/three.webgpu.js",
  21. "three/addons/": "./jsm/"
  22. }
  23. }
  24. </script>
  25. <script type="module">
  26. import * as THREE from 'three';
  27. import { pass, mrt, output, transformedNormalView, metalness, vec4 } from 'three/tsl';
  28. import { ssr } from 'three/addons/tsl/display/SSRNode.js';
  29. import Stats from 'three/addons/libs/stats.module.js';
  30. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  31. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  32. import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js';
  33. const params = {
  34. maxDistance: 0.1,
  35. opacity: 1,
  36. thickness: 0.018,
  37. enabled: true,
  38. autoRotate: false
  39. };
  40. let camera, scene, renderer, postProcessing, ssrPass;
  41. let gui, stats, controls;
  42. // Configure and create Draco decoder.
  43. const dracoLoader = new DRACOLoader();
  44. dracoLoader.setDecoderPath( 'jsm/libs/draco/' );
  45. dracoLoader.setDecoderConfig( { type: 'js' } );
  46. init();
  47. function init() {
  48. camera = new THREE.PerspectiveCamera( 35, window.innerWidth / window.innerHeight, 0.1, 15 );
  49. camera.position.set( 0.1, 0.3, 0.5 );
  50. scene = new THREE.Scene();
  51. scene.background = new THREE.Color( 0x443333 );
  52. scene.fog = new THREE.Fog( 0x443333, 1, 4 );
  53. // Ground
  54. const plane = new THREE.Mesh(
  55. new THREE.PlaneGeometry( 8, 8 ),
  56. new THREE.MeshPhongMaterial( { color: 0xcbcbcb } )
  57. );
  58. plane.rotation.x = - Math.PI / 2;
  59. plane.position.y = - 0.0001;
  60. scene.add( plane );
  61. // Lights
  62. const hemiLight = new THREE.HemisphereLight( 0x8d7c7c, 0x494966, 3 );
  63. scene.add( hemiLight );
  64. const spotLight = new THREE.SpotLight();
  65. spotLight.intensity = 8;
  66. spotLight.angle = Math.PI / 16;
  67. spotLight.penumbra = 0.5;
  68. spotLight.position.set( - 1, 1, 1 );
  69. scene.add( spotLight );
  70. dracoLoader.load( 'models/draco/bunny.drc', function ( geometry ) {
  71. geometry.computeVertexNormals();
  72. const material = new THREE.MeshStandardMaterial( { color: 0xa5a5a5, metalness: 0.01 } );
  73. const mesh = new THREE.Mesh( geometry, material );
  74. mesh.position.y = - 0.0365;
  75. scene.add( mesh );
  76. dracoLoader.dispose();
  77. } );
  78. let geometry, material, mesh;
  79. geometry = new THREE.BoxGeometry( .05, .05, .05 );
  80. material = new THREE.MeshStandardMaterial( { color: 'green', metalness: 0.01 } );
  81. mesh = new THREE.Mesh( geometry, material );
  82. mesh.position.set( - .12, .025, .015 );
  83. scene.add( mesh );
  84. geometry = new THREE.IcosahedronGeometry( .025, 4 );
  85. material = new THREE.MeshStandardMaterial( { color: 'cyan', metalness: 0.01 } );
  86. mesh = new THREE.Mesh( geometry, material );
  87. mesh.position.set( - .05, .025, .08 );
  88. scene.add( mesh );
  89. geometry = new THREE.ConeGeometry( .025, .05, 64 );
  90. material = new THREE.MeshStandardMaterial( { color: 'yellow', metalness: 0.01 } );
  91. mesh = new THREE.Mesh( geometry, material );
  92. mesh.position.set( - .05, .025, - .055 );
  93. scene.add( mesh );
  94. //
  95. renderer = new THREE.WebGPURenderer();
  96. renderer.setPixelRatio( window.devicePixelRatio );
  97. renderer.setSize( window.innerWidth, window.innerHeight );
  98. renderer.setAnimationLoop( animate );
  99. document.body.appendChild( renderer.domElement );
  100. //
  101. postProcessing = new THREE.PostProcessing( renderer );
  102. const scenePass = pass( scene, camera, { minFilter: THREE.NearestFilter, magFilter: THREE.NearestFilter } );
  103. scenePass.setMRT( mrt( {
  104. output: output,
  105. normal: transformedNormalView,
  106. metalness: metalness
  107. } ) );
  108. const scenePassColor = scenePass.getTextureNode( 'output' );
  109. const scenePassNormal = scenePass.getTextureNode( 'normal' );
  110. const scenePassDepth = scenePass.getTextureNode( 'depth' );
  111. const scenePassMetalness = scenePass.getTextureNode( 'metalness' );
  112. ssrPass = ssr( scenePassColor, scenePassDepth, scenePassNormal, scenePassMetalness, camera );
  113. // blend SSR over beauty
  114. const outputNode = vec4( scenePass.rgb.mul( ssrPass.a.oneMinus() ).add( ssrPass.rgb.mul( ssrPass.a ) ), scenePass.a );
  115. postProcessing.outputNode = outputNode;
  116. //
  117. controls = new OrbitControls( camera, renderer.domElement );
  118. controls.enableDamping = true;
  119. controls.target.set( 0, 0.05, 0 );
  120. controls.update();
  121. // stats
  122. stats = new Stats();
  123. document.body.appendChild( stats.dom );
  124. window.addEventListener( 'resize', onWindowResize );
  125. // GUI
  126. gui = new GUI();
  127. gui.add( params, 'maxDistance' ).min( 0 ).max( 1 ).onChange( updateParameters );
  128. gui.add( params, 'opacity' ).min( 0 ).max( 1 ).onChange( updateParameters );
  129. gui.add( params, 'thickness' ).min( 0 ).max( 0.1 ).onChange( updateParameters );
  130. gui.add( params, 'enabled' ).onChange( () => {
  131. if ( params.enabled === true ) {
  132. postProcessing.outputNode = outputNode;
  133. } else {
  134. postProcessing.outputNode = scenePass;
  135. }
  136. postProcessing.needsUpdate = true;
  137. } );
  138. gui.add( params, 'autoRotate' );
  139. updateParameters();
  140. }
  141. function updateParameters() {
  142. ssrPass.maxDistance.value = params.maxDistance;
  143. ssrPass.opacity.value = params.opacity;
  144. ssrPass.thickness.value = params.thickness;
  145. }
  146. function onWindowResize() {
  147. camera.aspect = window.innerWidth / window.innerHeight;
  148. camera.updateProjectionMatrix();
  149. renderer.setSize( window.innerWidth, window.innerHeight );
  150. }
  151. function animate() {
  152. stats.begin();
  153. controls.autoRotate = params.autoRotate;
  154. controls.update();
  155. postProcessing.render();
  156. stats.end();
  157. }
  158. </script>
  159. </body>
  160. </html>
粤ICP备19079148号