123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- <!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> - Standard Material Reflectivity (reflectance at F0)<br/>
- example by <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 { OBJLoader } from './jsm/loaders/OBJLoader.js';
- import { RGBELoader } from './jsm/loaders/RGBELoader.js';
- var container, stats;
- var params = {
- projection: 'normal',
- autoRotate: true,
- reflectivity: 1.0,
- background: false,
- exposure: 1.0,
- gemColor: 'Green'
- };
- var camera, scene, renderer, objects = [];
- var gemBackMaterial, gemFrontMaterial;
- var hdrCubeRenderTarget;
- init();
- animate();
- function init() {
- container = document.createElement( 'div' );
- document.body.appendChild( container );
- camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 2000 );
- camera.position.set( 0.0, - 10, 20 * 3.5 );
- scene = new THREE.Scene();
- scene.background = new THREE.Color( 0x000000 );
- renderer = new THREE.WebGLRenderer( { antialias: true } );
- gemBackMaterial = new THREE.MeshPhysicalMaterial( {
- map: null,
- color: 0x0000ff,
- metalness: 1,
- roughness: 0,
- opacity: 0.5,
- side: THREE.BackSide,
- transparent: true,
- envMapIntensity: 5,
- premultipliedAlpha: true
- // TODO: Add custom blend mode that modulates background color by this materials color.
- } );
- gemFrontMaterial = new THREE.MeshPhysicalMaterial( {
- map: null,
- color: 0x0000ff,
- metalness: 0,
- roughness: 0,
- opacity: 0.25,
- side: THREE.FrontSide,
- transparent: true,
- envMapIntensity: 10,
- premultipliedAlpha: true
- } );
- var manager = new THREE.LoadingManager();
- manager.onProgress = function ( item, loaded, total ) {
- console.log( item, loaded, total );
- };
- var loader = new OBJLoader( manager );
- loader.load( 'models/obj/emerald.obj', function ( object ) {
- object.traverse( function ( child ) {
- if ( child instanceof THREE.Mesh ) {
- child.material = gemBackMaterial;
- var second = child.clone();
- second.material = gemFrontMaterial;
- var parent = new THREE.Group();
- parent.add( second );
- parent.add( child );
- scene.add( parent );
- objects.push( parent );
- }
- } );
- } );
- new RGBELoader()
- .setDataType( THREE.UnsignedByteType )
- .setPath( 'textures/equirectangular/' )
- .load( 'royal_esplanade_1k.hdr', function ( hdrEquirect ) {
- hdrCubeRenderTarget = pmremGenerator.fromEquirectangular( hdrEquirect );
- pmremGenerator.dispose();
- gemFrontMaterial.envMap = gemBackMaterial.envMap = hdrCubeRenderTarget.texture;
- gemFrontMaterial.needsUpdate = gemBackMaterial.needsUpdate = true;
- hdrEquirect.dispose();
- } );
- var pmremGenerator = new THREE.PMREMGenerator( renderer );
- pmremGenerator.compileEquirectangularShader();
- // Lights
- scene.add( new THREE.AmbientLight( 0x222222 ) );
- var pointLight1 = new THREE.PointLight( 0xffffff );
- pointLight1.position.set( 150, 10, 0 );
- pointLight1.castShadow = false;
- scene.add( pointLight1 );
- var pointLight2 = new THREE.PointLight( 0xffffff );
- pointLight2.position.set( - 150, 0, 0 );
- scene.add( pointLight2 );
- var pointLight3 = new THREE.PointLight( 0xffffff );
- pointLight3.position.set( 0, - 10, - 150 );
- scene.add( pointLight3 );
- var pointLight4 = new THREE.PointLight( 0xffffff );
- pointLight4.position.set( 0, 0, 150 );
- scene.add( pointLight4 );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- renderer.shadowMap.enabled = true;
- container.appendChild( renderer.domElement );
- renderer.outputEncoding = THREE.sRGBEncoding;
- stats = new Stats();
- container.appendChild( stats.dom );
- var controls = new OrbitControls( camera, renderer.domElement );
- window.addEventListener( 'resize', onWindowResize, false );
- var gui = new GUI();
- gui.add( params, 'reflectivity', 0, 1 );
- gui.add( params, 'exposure', 0.1, 2 );
- gui.add( params, 'autoRotate' );
- gui.add( params, 'gemColor', [ 'Blue', 'Green', 'Red', 'White', 'Black' ] );
- 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() {
- if ( gemBackMaterial !== undefined && gemFrontMaterial !== undefined ) {
- gemFrontMaterial.reflectivity = gemBackMaterial.reflectivity = params.reflectivity;
- var newColor = gemBackMaterial.color;
- switch ( params.gemColor ) {
- case 'Blue': newColor = new THREE.Color( 0x000088 ); break;
- case 'Red': newColor = new THREE.Color( 0x880000 ); break;
- case 'Green': newColor = new THREE.Color( 0x008800 ); break;
- case 'White': newColor = new THREE.Color( 0x888888 ); break;
- case 'Black': newColor = new THREE.Color( 0x0f0f0f ); break;
- }
- gemBackMaterial.color = gemFrontMaterial.color = newColor;
- }
- renderer.toneMappingExposure = params.exposure;
- camera.lookAt( scene.position );
- if ( params.autoRotate ) {
- for ( var i = 0, l = objects.length; i < l; i ++ ) {
- var object = objects[ i ];
- object.rotation.y += 0.005;
- }
- }
- renderer.render( scene, camera );
- }
- </script>
- </body>
- </html>
|