webgpu_mrt.html 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - mrt</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 - mrt<br />
  12. Final / Beauty / Normal / Emissive / Diffuse
  13. </div>
  14. <script type="importmap">
  15. {
  16. "imports": {
  17. "three": "../build/three.webgpu.js",
  18. "three/webgpu": "../build/three.webgpu.js",
  19. "three/tsl": "../build/three.tsl.js",
  20. "three/addons/": "./jsm/"
  21. }
  22. }
  23. </script>
  24. <script type="module">
  25. import * as THREE from 'three';
  26. import { output, normalView, pass, step, diffuseColor, emissive, directionToColor, screenUV, mix, mrt, Fn } from 'three/tsl';
  27. import { RGBELoader } from 'three/addons/loaders/RGBELoader.js';
  28. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  29. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  30. let camera, scene, renderer;
  31. let postProcessing;
  32. init();
  33. function init() {
  34. const container = document.createElement( 'div' );
  35. document.body.appendChild( container );
  36. // scene
  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. new RGBELoader()
  41. .setPath( 'textures/equirectangular/' )
  42. .load( 'royal_esplanade_1k.hdr', function ( texture ) {
  43. texture.mapping = THREE.EquirectangularReflectionMapping;
  44. scene.background = texture;
  45. scene.environment = texture;
  46. // model
  47. const loader = new GLTFLoader().setPath( 'models/gltf/DamagedHelmet/glTF/' );
  48. loader.load( 'DamagedHelmet.gltf', function ( gltf ) {
  49. scene.add( gltf.scene );
  50. } );
  51. } );
  52. // renderer
  53. renderer = new THREE.WebGPURenderer( { antialias: true } );
  54. renderer.setPixelRatio( window.devicePixelRatio );
  55. renderer.setSize( window.innerWidth, window.innerHeight );
  56. renderer.setAnimationLoop( render );
  57. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  58. container.appendChild( renderer.domElement );
  59. // post processing
  60. const scenePass = pass( scene, camera, { minFilter: THREE.NearestFilter, magFilter: THREE.NearestFilter } );
  61. scenePass.setMRT( mrt( {
  62. output: output,
  63. normal: directionToColor( normalView ),
  64. diffuse: diffuseColor,
  65. emissive: emissive
  66. } ) );
  67. // optimize textures
  68. const normalTexture = scenePass.getTexture( 'normal' );
  69. const diffuseTexture = scenePass.getTexture( 'diffuse' );
  70. const emissiveTexture = scenePass.getTexture( 'emissive' );
  71. normalTexture.type = diffuseTexture.type = emissiveTexture.type = THREE.UnsignedByteType;
  72. // post processing - mrt
  73. postProcessing = new THREE.PostProcessing( renderer );
  74. postProcessing.outputColorTransform = false;
  75. postProcessing.outputNode = Fn( () => {
  76. const output = scenePass.getTextureNode( 'output' ); // output name is optional here
  77. const normal = scenePass.getTextureNode( 'normal' );
  78. const diffuse = scenePass.getTextureNode( 'diffuse' );
  79. const emissive = scenePass.getTextureNode( 'emissive' );
  80. const out = mix( output.renderOutput(), output, step( 0.2, screenUV.x ) );
  81. const nor = mix( out, normal, step( 0.4, screenUV.x ) );
  82. const emi = mix( nor, emissive, step( 0.6, screenUV.x ) );
  83. const dif = mix( emi, diffuse, step( 0.8, screenUV.x ) );
  84. return dif;
  85. } )();
  86. // controls
  87. const controls = new OrbitControls( camera, renderer.domElement );
  88. controls.minDistance = 2;
  89. controls.maxDistance = 10;
  90. controls.target.set( 0, 0, - 0.2 );
  91. controls.update();
  92. window.addEventListener( 'resize', onWindowResize );
  93. }
  94. function onWindowResize() {
  95. camera.aspect = window.innerWidth / window.innerHeight;
  96. camera.updateProjectionMatrix();
  97. renderer.setSize( window.innerWidth, window.innerHeight );
  98. }
  99. //
  100. function render() {
  101. postProcessing.render();
  102. }
  103. </script>
  104. </body>
  105. </html>
粤ICP备19079148号