123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - hdr jpg</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
- <link type="text/css" rel="stylesheet" href="main.css">
- <style>
- .lbl {
- color: #fff;
- font-size: 16px;
- font-weight: bold;
- position: absolute;
- bottom: 0px;
- z-index: 100;
- text-shadow: #000 1px 1px 1px;
- background-color: rgba(0,0,0,0.85);
- padding: 1em;
- }
- #lbl_left {
- text-align:left;
- left:0px;
- }
- </style>
- </head>
- <body>
- <div id="info">
- <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - hdr jpg loader <br/>
- Converted from hdr with <a href="https://gainmap-creator.mono-grid.com/" target="_blank" rel="noopener">converter</a>. <br />
- See external <a href="https://github.com/MONOGRID/gainmap-js" target="_blank" rel="noopener">gainmap-js</a> for more information.
- </div>
- <div id="lbl_left" class="lbl"></div>
- <script type="importmap">
- {
- "imports": {
- "three": "../build/three.module.js",
- "three/addons/": "./jsm/",
- "@monogrid/gainmap-js": "https://cdn.jsdelivr.net/npm/@monogrid/[email protected]/dist/decode.js"
- }
- }
- </script>
- <script type="module">
- import * as THREE from 'three';
- import Stats from 'three/addons/libs/stats.module.js';
- import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
- import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
- import { RGBELoader } from 'three/addons/loaders/RGBELoader.js';
- import { HDRJPGLoader } from '@monogrid/gainmap-js';
- const params = {
- envMap: 'HDR JPG',
- roughness: 0.0,
- metalness: 1.0,
- exposure: 1.0,
- debug: false
- };
- let container, stats;
- let camera, scene, renderer, controls;
- let torusMesh, planeMesh;
- let hdrJpg, hdrJpgPMREMRenderTarget, hdrJpgEquirectangularMap;
- let hdrPMREMRenderTarget, hdrEquirectangularMap;
- const fileSizes = {};
- const resolutions = {};
- init();
- function init() {
- const lbl = document.getElementById( 'lbl_left' );
- container = document.createElement( 'div' );
- document.body.appendChild( container );
- camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 500 );
- camera.position.set( 0, 0, - 120 );
- scene = new THREE.Scene();
- renderer = new THREE.WebGLRenderer();
- renderer.toneMapping = THREE.ACESFilmicToneMapping;
- //
- let geometry = new THREE.TorusKnotGeometry( 18, 8, 200, 40, 1, 3 );
- let material = new THREE.MeshStandardMaterial( {
- color: 0xffffff,
- metalness: params.metalness,
- roughness: params.roughness
- } );
- torusMesh = new THREE.Mesh( geometry, material );
- scene.add( torusMesh );
- geometry = new THREE.PlaneGeometry( 200, 200 );
- material = new THREE.MeshBasicMaterial();
- planeMesh = new THREE.Mesh( geometry, material );
- planeMesh.position.y = - 50;
- planeMesh.rotation.x = - Math.PI * 0.5;
- scene.add( planeMesh );
- const pmremGenerator = new THREE.PMREMGenerator( renderer );
- pmremGenerator.compileEquirectangularShader();
- THREE.DefaultLoadingManager.onLoad = function ( ) {
- pmremGenerator.dispose();
- };
- hdrJpg = new HDRJPGLoader( renderer )
- .load( 'textures/equirectangular/spruit_sunrise_4k.hdr.jpg', function ( ) {
- resolutions[ 'HDR JPG' ] = hdrJpg.width + 'x' + hdrJpg.height;
- displayStats( 'HDR JPG' );
- hdrJpgEquirectangularMap = hdrJpg.renderTarget.texture;
- hdrJpgPMREMRenderTarget = pmremGenerator.fromEquirectangular( hdrJpgEquirectangularMap );
- hdrJpgEquirectangularMap.mapping = THREE.EquirectangularReflectionMapping;
- hdrJpgEquirectangularMap.needsUpdate = true;
- hdrJpg.dispose();
- }, function ( progress ) {
- fileSizes[ 'HDR JPG' ] = humanFileSize( progress.total );
- } );
- hdrEquirectangularMap = new RGBELoader()
- .load( 'textures/equirectangular/spruit_sunrise_1k.hdr', function ( ) {
- resolutions[ 'HDR' ] = hdrEquirectangularMap.image.width + 'x' + hdrEquirectangularMap.image.height;
- hdrPMREMRenderTarget = pmremGenerator.fromEquirectangular( hdrEquirectangularMap );
- hdrEquirectangularMap.mapping = THREE.EquirectangularReflectionMapping;
- hdrEquirectangularMap.minFilter = THREE.LinearFilter;
- hdrEquirectangularMap.magFilter = THREE.LinearFilter;
- hdrEquirectangularMap.needsUpdate = true;
- }, function ( progress ) {
- fileSizes[ 'HDR' ] = humanFileSize( progress.total );
- } );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- renderer.setAnimationLoop( animate );
- container.appendChild( renderer.domElement );
- stats = new Stats();
- container.appendChild( stats.dom );
- controls = new OrbitControls( camera, renderer.domElement );
- controls.minDistance = 50;
- controls.maxDistance = 300;
- window.addEventListener( 'resize', onWindowResize );
- const gui = new GUI();
- gui.add( params, 'envMap', [ 'HDR JPG', 'HDR' ] ).onChange( displayStats );
- gui.add( params, 'roughness', 0, 1, 0.01 );
- gui.add( params, 'metalness', 0, 1, 0.01 );
- gui.add( params, 'exposure', 0, 2, 0.01 );
- gui.add( params, 'debug' );
- gui.open();
- function displayStats( value ) {
- lbl.innerHTML = value + ' size : ' + fileSizes[ value ] + ', Resolution: ' + resolutions[ value ];
- }
- }
- function humanFileSize( bytes, si = true, dp = 1 ) {
- const thresh = si ? 1000 : 1024;
- if ( Math.abs( bytes ) < thresh ) {
- return bytes + ' B';
- }
- const units = si
- ? [ 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB' ]
- : [ 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB' ];
- let u = - 1;
- const r = 10 ** dp;
- do {
- bytes /= thresh;
- ++ u;
- } while ( Math.round( Math.abs( bytes ) * r ) / r >= thresh && u < units.length - 1 );
- return bytes.toFixed( dp ) + ' ' + units[ u ];
- }
- function onWindowResize() {
- const width = window.innerWidth;
- const height = window.innerHeight;
- camera.aspect = width / height;
- camera.updateProjectionMatrix();
- renderer.setSize( width, height );
- }
- function animate() {
-
- stats.begin();
- render();
- stats.end();
- }
- function render() {
- torusMesh.material.roughness = params.roughness;
- torusMesh.material.metalness = params.metalness;
- let pmremRenderTarget, equirectangularMap;
- switch ( params.envMap ) {
- case 'HDR JPG':
- pmremRenderTarget = hdrJpgPMREMRenderTarget;
- equirectangularMap = hdrJpgEquirectangularMap;
- break;
- case 'HDR':
- pmremRenderTarget = hdrPMREMRenderTarget;
- equirectangularMap = hdrEquirectangularMap;
- break;
- }
- const newEnvMap = pmremRenderTarget ? pmremRenderTarget.texture : null;
- if ( newEnvMap && newEnvMap !== torusMesh.material.envMap ) {
- planeMesh.material.map = newEnvMap;
- planeMesh.material.needsUpdate = true;
- }
- torusMesh.rotation.y += 0.005;
- planeMesh.visible = params.debug;
- scene.environment = equirectangularMap;
- scene.background = equirectangularMap;
- renderer.toneMappingExposure = params.exposure;
- renderer.render( scene, camera );
- }
- </script>
- </body>
- </html>
|