123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>threejs webgl - materials - hdr environment mapping</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">
- </head>
- <body>
- <div id="container"></div>
- <div id="info">
- <a href="https://threejs.org" target="_blank" rel="noopener">threejs</a> - High dynamic range (RGBE) Image-based Lighting (IBL)<br />using run-time generated pre-filtered roughness mipmaps (PMREM)<br/>
- Created by Prashant Sharma and <a href="http://clara.io/" target="_blank" rel="noopener">Ben Houston</a>.
- </div>
- <script type="module">
- import * as THREE from '../build/three.module.js';
- import Stats from './jsm/libs/stats.module.js';
- import { GUI } from './jsm/libs/dat.gui.module.js';
- import { OrbitControls } from './jsm/controls/OrbitControls.js';
- import { HDRCubeTextureLoader } from './jsm/loaders/HDRCubeTextureLoader.js';
- var params = {
- envMap: 'HDR',
- roughness: 0.0,
- metalness: 0.0,
- exposure: 1.0,
- debug: false
- };
- var container, stats;
- var camera, scene, renderer, controls;
- var torusMesh, planeMesh;
- var generatedCubeRenderTarget, ldrCubeRenderTarget, hdrCubeRenderTarget, rgbmCubeRenderTarget;
- var ldrCubeMap, hdrCubeMap, rgbmCubeMap;
- init();
- animate();
- function getEnvScene() {
- var envScene = new THREE.Scene();
- var geometry = new THREE.BoxBufferGeometry();
- geometry.deleteAttribute( 'uv' );
- var roomMaterial = new THREE.MeshStandardMaterial( { metalness: 0, side: THREE.BackSide } );
- var room = new THREE.Mesh( geometry, roomMaterial );
- room.scale.setScalar( 10 );
- envScene.add( room );
- var mainLight = new THREE.PointLight( 0xffffff, 50, 0, 2 );
- envScene.add( mainLight );
- var lightMaterial = new THREE.MeshLambertMaterial( { color: 0x000000, emissive: 0xffffff, emissiveIntensity: 10 } );
- var light1 = new THREE.Mesh( geometry, lightMaterial );
- light1.position.set( - 5, 2, 0 );
- light1.scale.set( 0.1, 1, 1 );
- envScene.add( light1 );
- var light2 = new THREE.Mesh( geometry, lightMaterial );
- light2.position.set( 0, 5, 0 );
- light2.scale.set( 1, 0.1, 1 );
- envScene.add( light2 );
- var light2 = new THREE.Mesh( geometry, lightMaterial );
- light2.position.set( 2, 1, 5 );
- light2.scale.set( 1.5, 2, 0.1 );
- envScene.add( light2 );
- return envScene;
- }
- function init() {
- container = document.createElement( 'div' );
- document.body.appendChild( container );
- camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 1000 );
- camera.position.set( 0, 0, 120 );
- scene = new THREE.Scene();
- scene.background = new THREE.Color( 0x000000 );
- renderer = new THREE.WebGLRenderer();
- renderer.physicallyCorrectLights = true;
- renderer.toneMapping = THREE.LinearToneMapping;
- //
- var geometry = new THREE.TorusKnotBufferGeometry( 18, 8, 150, 20 );
- var material = new THREE.MeshStandardMaterial( {
- color: 0xffffff,
- metalness: params.metalness,
- roughness: params.roughness
- } );
- torusMesh = new THREE.Mesh( geometry, material );
- scene.add( torusMesh );
- var geometry = new THREE.PlaneBufferGeometry( 200, 200 );
- var material = new THREE.MeshBasicMaterial();
- planeMesh = new THREE.Mesh( geometry, material );
- planeMesh.position.y = - 50;
- planeMesh.rotation.x = - Math.PI * 0.5;
- scene.add( planeMesh );
- THREE.DefaultLoadingManager.onLoad = function ( ) {
- pmremGenerator.dispose();
- };
- var hdrUrls = [ 'px.hdr', 'nx.hdr', 'py.hdr', 'ny.hdr', 'pz.hdr', 'nz.hdr' ];
- hdrCubeMap = new HDRCubeTextureLoader()
- .setPath( './textures/cube/pisaHDR/' )
- .setDataType( THREE.UnsignedByteType )
- .load( hdrUrls, function () {
- hdrCubeRenderTarget = pmremGenerator.fromCubemap( hdrCubeMap );
- hdrCubeMap.magFilter = THREE.LinearFilter;
- hdrCubeMap.needsUpdate = true;
- } );
- var ldrUrls = [ 'px.png', 'nx.png', 'py.png', 'ny.png', 'pz.png', 'nz.png' ];
- ldrCubeMap = new THREE.CubeTextureLoader()
- .setPath( './textures/cube/pisa/' )
- .load( ldrUrls, function () {
- ldrCubeMap.encoding = THREE.sRGBEncoding;
- ldrCubeRenderTarget = pmremGenerator.fromCubemap( ldrCubeMap );
- } );
- var rgbmUrls = [ 'px.png', 'nx.png', 'py.png', 'ny.png', 'pz.png', 'nz.png' ];
- rgbmCubeMap = new THREE.CubeTextureLoader()
- .setPath( './textures/cube/pisaRGBM16/' )
- .load( rgbmUrls, function () {
- rgbmCubeMap.encoding = THREE.RGBM16Encoding;
- rgbmCubeMap.format = THREE.RGBAFormat;
- rgbmCubeRenderTarget = pmremGenerator.fromCubemap( rgbmCubeMap );
- rgbmCubeMap.magFilter = THREE.LinearFilter;
- rgbmCubeMap.needsUpdate = true;
- } );
- var pmremGenerator = new THREE.PMREMGenerator( renderer );
- pmremGenerator.compileCubemapShader();
- var envScene = getEnvScene();
- generatedCubeRenderTarget = pmremGenerator.fromScene( envScene, 0.04 );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- container.appendChild( renderer.domElement );
- //renderer.toneMapping = ReinhardToneMapping;
- renderer.outputEncoding = THREE.sRGBEncoding;
- stats = new Stats();
- container.appendChild( stats.dom );
- controls = new OrbitControls( camera, renderer.domElement );
- controls.minDistance = 50;
- controls.maxDistance = 300;
- window.addEventListener( 'resize', onWindowResize, false );
- var gui = new GUI();
- gui.add( params, 'envMap', [ 'Generated', 'LDR', 'HDR', 'RGBM16' ] );
- 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', false );
- gui.open();
- }
- function onWindowResize() {
- var width = window.innerWidth;
- var height = window.innerHeight;
- camera.aspect = width / height;
- camera.updateProjectionMatrix();
- renderer.setSize( width, height );
- }
- function animate() {
- requestAnimationFrame( animate );
- stats.begin();
- render();
- stats.end();
- }
- function render() {
- torusMesh.material.roughness = params.roughness;
- torusMesh.material.metalness = params.metalness;
- var renderTarget, cubeMap;
- switch ( params.envMap ) {
- case 'Generated':
- renderTarget = generatedCubeRenderTarget;
- cubeMap = generatedCubeRenderTarget.texture;
- break;
- case 'LDR':
- renderTarget = ldrCubeRenderTarget;
- cubeMap = ldrCubeMap;
- break;
- case 'HDR':
- renderTarget = hdrCubeRenderTarget;
- cubeMap = hdrCubeMap;
- break;
- case 'RGBM16':
- renderTarget = rgbmCubeRenderTarget;
- cubeMap = rgbmCubeMap;
- break;
- }
- var newEnvMap = renderTarget ? renderTarget.texture : null;
- if ( newEnvMap && newEnvMap !== torusMesh.material.envMap ) {
- torusMesh.material.envMap = newEnvMap;
- torusMesh.material.needsUpdate = true;
- planeMesh.material.map = newEnvMap;
- planeMesh.material.needsUpdate = true;
- }
- torusMesh.rotation.y += 0.005;
- planeMesh.visible = params.debug;
- scene.background = cubeMap;
- renderer.toneMappingExposure = params.exposure;
- renderer.render( scene, camera );
- }
- </script>
- </body>
- </html>
|