1
0

webgpu_pmrem_test.html 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - PMREM directional light test</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 - PMREM directional light test">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgpu_pmrem_test.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgpu_pmrem_test.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>PMREM Directional Light Test</span>
  18. </div>
  19. <small>
  20. 1: white metal. 2: white dielectric. 3: black dielectric.
  21. <br>PMREM on: HDR with a single bright pixel. PMREM off: DirectionalLight.
  22. <br>The difference between these renders indicates the error in the PMREM approximations.
  23. <br>PMREM test by <a href="https://github.com/elalish" target="_blank" rel="noopener">Emmett Lalish</a>
  24. </small>
  25. </div>
  26. <script type="importmap">
  27. {
  28. "imports": {
  29. "three": "../build/three.webgpu.js",
  30. "three/webgpu": "../build/three.webgpu.js",
  31. "three/tsl": "../build/three.tsl.js",
  32. "three/addons/": "./jsm/"
  33. }
  34. }
  35. </script>
  36. <script type="module">
  37. import * as THREE from 'three';
  38. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  39. import { HDRLoader } from 'three/addons/loaders/HDRLoader.js';
  40. import { Inspector } from 'three/addons/inspector/Inspector.js';
  41. let scene, camera, controls, renderer;
  42. async function init() {
  43. const width = window.innerWidth;
  44. const height = window.innerHeight;
  45. const aspect = width / height;
  46. // renderer
  47. renderer = new THREE.WebGPURenderer( { antialias: true } );
  48. renderer.setPixelRatio( window.devicePixelRatio );
  49. renderer.setSize( width, height );
  50. renderer.setAnimationLoop( render );
  51. renderer.inspector = new Inspector();
  52. // tonemapping
  53. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  54. renderer.toneMappingExposure = 1;
  55. document.body.appendChild( renderer.domElement );
  56. window.addEventListener( 'resize', onWindowResize );
  57. await renderer.init();
  58. // scene
  59. scene = new THREE.Scene();
  60. // camera
  61. camera = new THREE.PerspectiveCamera( 40, aspect, 1, 30 );
  62. updateCamera();
  63. camera.position.set( 0, 0, 16 );
  64. // controls
  65. controls = new OrbitControls( camera, renderer.domElement );
  66. controls.minDistance = 4;
  67. controls.maxDistance = 20;
  68. // light
  69. const directionalLight = new THREE.DirectionalLight( 0xffffff, 0 ); // set intensity to 0 to start
  70. const x = 597;
  71. const y = 213;
  72. const theta = ( x + 0.5 ) * Math.PI / 512;
  73. const phi = ( y + 0.5 ) * Math.PI / 512;
  74. directionalLight.position.setFromSphericalCoords( 100, - phi, Math.PI / 2 - theta );
  75. scene.add( directionalLight );
  76. // scene.add( new THREE.DirectionalLightHelper( directionalLight ) );
  77. // The spot1Lux HDR environment map is expressed in nits (lux / sr). The directional light has units of lux,
  78. // so to match a 1 lux light, we set a single pixel with a value equal to 1 divided by the solid
  79. // angle of the pixel in steradians. This image is 1024 x 512,
  80. // so the value is 1 / ( sin( phi ) * ( pi / 512 ) ^ 2 ) = 27,490 nits.
  81. const gui = renderer.inspector.createParameters( 'Settings' );
  82. gui.add( { enabled: true }, 'enabled' )
  83. .name( 'PMREM' )
  84. .onChange( value => {
  85. directionalLight.intensity = value ? 0 : 1;
  86. scene.traverse( function ( child ) {
  87. if ( child.isMesh ) {
  88. child.material.envMapIntensity = 1 - directionalLight.intensity;
  89. }
  90. } );
  91. } );
  92. }
  93. function createObjects() {
  94. let radianceMap = null;
  95. new HDRLoader()
  96. // .setDataType( THREE.FloatType )
  97. .setPath( 'textures/equirectangular/' )
  98. .load( 'spot1Lux.hdr', function ( texture ) {
  99. radianceMap = pmremGenerator.fromEquirectangular( texture ).texture;
  100. pmremGenerator.dispose();
  101. scene.background = radianceMap;
  102. const geometry = new THREE.SphereGeometry( 0.4, 32, 32 );
  103. for ( let x = 0; x <= 10; x ++ ) {
  104. for ( let y = 0; y <= 2; y ++ ) {
  105. const material = new THREE.MeshPhysicalMaterial( {
  106. roughness: x / 10,
  107. metalness: y < 1 ? 1 : 0,
  108. color: y < 2 ? 0xffffff : 0x000000,
  109. envMap: radianceMap,
  110. envMapIntensity: 1
  111. } );
  112. const mesh = new THREE.Mesh( geometry, material );
  113. mesh.position.x = x - 5;
  114. mesh.position.y = 1 - y;
  115. scene.add( mesh );
  116. }
  117. }
  118. } );
  119. const pmremGenerator = new THREE.PMREMGenerator( renderer );
  120. pmremGenerator.compileEquirectangularShader();
  121. }
  122. function onWindowResize() {
  123. const width = window.innerWidth;
  124. const height = window.innerHeight;
  125. camera.aspect = width / height;
  126. updateCamera();
  127. renderer.setSize( width, height );
  128. }
  129. function updateCamera() {
  130. const horizontalFoV = 40;
  131. const verticalFoV = 2 * Math.atan( Math.tan( horizontalFoV / 2 * Math.PI / 180 ) / camera.aspect ) * 180 / Math.PI;
  132. camera.fov = verticalFoV;
  133. camera.updateProjectionMatrix();
  134. }
  135. function render() {
  136. renderer.render( scene, camera );
  137. }
  138. Promise.resolve()
  139. .then( init )
  140. .then( createObjects );
  141. </script>
  142. </body>
  143. </html>
粤ICP备19079148号