webgl_loader_gainmap.html 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - gainmap hdr</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. <style>
  9. .lbl {
  10. color: #fff;
  11. font-size: 16px;
  12. font-weight: bold;
  13. position: absolute;
  14. bottom: 0px;
  15. z-index: 100;
  16. text-shadow: #000 1px 1px 1px;
  17. background-color: rgba(0,0,0,0.85);
  18. padding: 1em;
  19. }
  20. #lbl_left {
  21. text-align:left;
  22. left:0px;
  23. }
  24. </style>
  25. </head>
  26. <body>
  27. <div id="info">
  28. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - gain map (ultra hdr) loader <br/>
  29. Gain map images converted from hdr with <a href="https://gainmap-creator.mono-grid.com/" target="_blank" rel="noopener">Gain map converter</a>. <br />
  30. See external <a href="https://github.com/MONOGRID/gainmap-js" target="_blank" rel="noopener">gainmap-js</a> for more information on how to use and create gain map images.
  31. </div>
  32. <div id="lbl_left" class="lbl"></div>
  33. <script type="importmap">
  34. {
  35. "imports": {
  36. "three": "../build/three.module.js",
  37. "three/addons/": "./jsm/",
  38. "@monogrid/gainmap-js": "https://unpkg.com/@monogrid/[email protected]/dist/decode.js"
  39. }
  40. }
  41. </script>
  42. <script type="module">
  43. import * as THREE from 'three';
  44. import Stats from 'three/addons/libs/stats.module.js';
  45. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  46. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  47. import { RGBELoader } from 'three/addons/loaders/RGBELoader.js';
  48. import { GainMapLoader, HDRJPGLoader } from '@monogrid/gainmap-js';
  49. const params = {
  50. envMap: 'HDR JPG',
  51. roughness: 0.0,
  52. metalness: 0.0,
  53. exposure: 1.0,
  54. debug: false
  55. };
  56. let container, stats;
  57. let camera, scene, renderer, controls;
  58. let torusMesh, planeMesh;
  59. let hdrJpg, hdrJpgPMREMRenderTarget, hdrJpgEquirectangularMap;
  60. let gainMap, gainMapPMREMRenderTarget, gainMapBackground;
  61. let hdrPMREMRenderTarget, hdrEquirectangularMap;
  62. const fileSizes = {};
  63. const resolutions = {};
  64. init();
  65. animate();
  66. function init() {
  67. const lbl = document.getElementById( 'lbl_left' );
  68. container = document.createElement( 'div' );
  69. document.body.appendChild( container );
  70. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 1000 );
  71. camera.position.set( 0, 0, 120 );
  72. scene = new THREE.Scene();
  73. scene.background = new THREE.Color( 0x000000 );
  74. renderer = new THREE.WebGLRenderer();
  75. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  76. //
  77. let geometry = new THREE.TorusKnotGeometry( 18, 8, 150, 20 );
  78. // let geometry = new THREE.SphereGeometry( 26, 64, 32 );
  79. let material = new THREE.MeshStandardMaterial( {
  80. color: 0xffffff,
  81. metalness: params.metalness,
  82. roughness: params.roughness
  83. } );
  84. torusMesh = new THREE.Mesh( geometry, material );
  85. scene.add( torusMesh );
  86. geometry = new THREE.PlaneGeometry( 200, 200 );
  87. material = new THREE.MeshBasicMaterial();
  88. planeMesh = new THREE.Mesh( geometry, material );
  89. planeMesh.position.y = - 50;
  90. planeMesh.rotation.x = - Math.PI * 0.5;
  91. scene.add( planeMesh );
  92. const pmremGenerator = new THREE.PMREMGenerator( renderer );
  93. pmremGenerator.compileEquirectangularShader();
  94. THREE.DefaultLoadingManager.onLoad = function ( ) {
  95. pmremGenerator.dispose();
  96. };
  97. hdrJpg = new HDRJPGLoader( renderer )
  98. .load( 'textures/gainmap/spruit_sunrise_4k.jpg', function ( ) {
  99. resolutions[ 'HDR JPG' ] = hdrJpg.width + 'x' + hdrJpg.height;
  100. displayStats( 'HDR JPG' );
  101. hdrJpgPMREMRenderTarget = pmremGenerator.fromEquirectangular( hdrJpg.renderTarget.texture );
  102. hdrJpgEquirectangularMap = hdrJpg.toDataTexture();
  103. hdrJpgEquirectangularMap.mapping = THREE.EquirectangularReflectionMapping;
  104. hdrJpgEquirectangularMap.minFilter = THREE.LinearFilter;
  105. hdrJpgEquirectangularMap.magFilter = THREE.LinearFilter;
  106. hdrJpgEquirectangularMap.generateMipmaps = false;
  107. hdrJpgEquirectangularMap.needsUpdate = true;
  108. }, function ( progress ) {
  109. fileSizes[ 'HDR JPG' ] = humanFileSize( progress.total );
  110. } );
  111. gainMap = new GainMapLoader( renderer )
  112. .load( [
  113. 'textures/gainmap/spruit_sunrise_4k.webp',
  114. 'textures/gainmap/spruit_sunrise_4k-gainmap.webp',
  115. 'textures/gainmap/spruit_sunrise_4k.json'
  116. ], function ( ) {
  117. resolutions[ 'Webp Gain map (separate)' ] = gainMap.width + 'x' + gainMap.height;
  118. gainMapPMREMRenderTarget = pmremGenerator.fromEquirectangular( gainMap.renderTarget.texture );
  119. gainMapBackground = gainMap.toDataTexture();
  120. gainMapBackground.mapping = THREE.EquirectangularReflectionMapping;
  121. gainMapBackground.minFilter = THREE.LinearFilter;
  122. gainMapBackground.magFilter = THREE.LinearFilter;
  123. gainMapBackground.generateMipmaps = false;
  124. gainMapBackground.needsUpdate = true;
  125. }, function ( progress ) {
  126. fileSizes[ 'Webp Gain map (separate)' ] = humanFileSize( progress.total );
  127. } );
  128. hdrEquirectangularMap = new RGBELoader()
  129. .load( 'textures/gainmap/spruit_sunrise_1k.hdr', function ( ) {
  130. resolutions[ 'HDR' ] = hdrEquirectangularMap.image.width + 'x' + hdrEquirectangularMap.image.height;
  131. hdrPMREMRenderTarget = pmremGenerator.fromEquirectangular( hdrEquirectangularMap );
  132. hdrEquirectangularMap.mapping = THREE.EquirectangularReflectionMapping;
  133. hdrEquirectangularMap.minFilter = THREE.LinearFilter;
  134. hdrEquirectangularMap.magFilter = THREE.LinearFilter;
  135. hdrEquirectangularMap.needsUpdate = true;
  136. }, function ( progress ) {
  137. fileSizes[ 'HDR' ] = humanFileSize( progress.total );
  138. } );
  139. renderer.setPixelRatio( window.devicePixelRatio );
  140. renderer.setSize( window.innerWidth, window.innerHeight );
  141. container.appendChild( renderer.domElement );
  142. stats = new Stats();
  143. container.appendChild( stats.dom );
  144. controls = new OrbitControls( camera, renderer.domElement );
  145. controls.minDistance = 50;
  146. controls.maxDistance = 300;
  147. window.addEventListener( 'resize', onWindowResize );
  148. const gui = new GUI();
  149. gui.add( params, 'envMap', [ 'HDR JPG', 'Webp Gain map (separate)', 'HDR' ] ).onChange( displayStats );
  150. gui.add( params, 'roughness', 0, 1, 0.01 );
  151. gui.add( params, 'metalness', 0, 1, 0.01 );
  152. gui.add( params, 'exposure', 0, 2, 0.01 );
  153. gui.add( params, 'debug' );
  154. gui.open();
  155. function displayStats( value ) {
  156. lbl.innerHTML = value + ' size : ' + fileSizes[ value ] + ', Resolution: ' + resolutions[ value ];
  157. }
  158. }
  159. function humanFileSize( bytes, si = true, dp = 1 ) {
  160. const thresh = si ? 1000 : 1024;
  161. if ( Math.abs( bytes ) < thresh ) {
  162. return bytes + ' B';
  163. }
  164. const units = si
  165. ? [ 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB' ]
  166. : [ 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB' ];
  167. let u = - 1;
  168. const r = 10 ** dp;
  169. do {
  170. bytes /= thresh;
  171. ++ u;
  172. } while ( Math.round( Math.abs( bytes ) * r ) / r >= thresh && u < units.length - 1 );
  173. return bytes.toFixed( dp ) + ' ' + units[ u ];
  174. }
  175. function onWindowResize() {
  176. const width = window.innerWidth;
  177. const height = window.innerHeight;
  178. camera.aspect = width / height;
  179. camera.updateProjectionMatrix();
  180. renderer.setSize( width, height );
  181. }
  182. function animate() {
  183. requestAnimationFrame( animate );
  184. stats.begin();
  185. render();
  186. stats.end();
  187. }
  188. function render() {
  189. torusMesh.material.roughness = params.roughness;
  190. torusMesh.material.metalness = params.metalness;
  191. let pmremRenderTarget, equirectangularMap;
  192. switch ( params.envMap ) {
  193. case 'HDR JPG':
  194. pmremRenderTarget = hdrJpgPMREMRenderTarget;
  195. equirectangularMap = hdrJpgEquirectangularMap || hdrJpg.renderTarget.texture;
  196. break;
  197. case 'Webp Gain map (separate)':
  198. pmremRenderTarget = gainMapPMREMRenderTarget;
  199. equirectangularMap = gainMapBackground || gainMap.renderTarget.texture;
  200. break;
  201. case 'HDR':
  202. pmremRenderTarget = hdrPMREMRenderTarget;
  203. equirectangularMap = hdrEquirectangularMap;
  204. break;
  205. }
  206. const newEnvMap = pmremRenderTarget ? pmremRenderTarget.texture : null;
  207. if ( newEnvMap && newEnvMap !== torusMesh.material.envMap ) {
  208. torusMesh.material.envMap = newEnvMap;
  209. torusMesh.material.needsUpdate = true;
  210. planeMesh.material.map = newEnvMap;
  211. planeMesh.material.needsUpdate = true;
  212. }
  213. torusMesh.rotation.y += 0.005;
  214. planeMesh.visible = params.debug;
  215. scene.background = equirectangularMap;
  216. renderer.toneMappingExposure = params.exposure;
  217. renderer.render( scene, camera );
  218. }
  219. </script>
  220. </body>
  221. </html>