Skip to content

Commit

Permalink
Examples: Improved webgl_cubemap_dynamic.
Browse files Browse the repository at this point in the history
  • Loading branch information
mrdoob committed Feb 23, 2022
1 parent c495edd commit 944aeb1
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 90 deletions.
Binary file modified examples/screenshots/webgl_materials_cubemap_dynamic.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
130 changes: 40 additions & 90 deletions examples/webgl_materials_cubemap_dynamic.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
</head>
<body>

<div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js webgl</a> - materials - dynamic cube reflection<br/>Photo by <a href="http://www.flickr.com/photos/jonragnarsson/2294472375/" target="_blank" rel="noopener">J&oacute;n Ragnarsson</a>.</div>
<div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js webgl</a> - materials - dynamic cube reflection</div>

<!-- Import maps polyfill -->
<!-- Remove this when import maps will be widely supported -->
Expand All @@ -31,81 +31,86 @@

import * as THREE from 'three';

import { OrbitControls } from './jsm/controls/OrbitControls.js';
import { RGBELoader } from './jsm/loaders/RGBELoader.js';

import { GUI } from './jsm/libs/lil-gui.module.min.js';
import Stats from './jsm/libs/stats.module.js';

let camera, scene, renderer, stats;
let cube, sphere, torus, material;

let count = 0, cubeCamera1, cubeCamera2, cubeRenderTarget1, cubeRenderTarget2;

let onPointerDownPointerX, onPointerDownPointerY, onPointerDownLon, onPointerDownLat;

let lon = 0, lat = 0;
let phi = 0, theta = 0;

const textureLoader = new THREE.TextureLoader();

textureLoader.load( 'textures/2294472375_24a3b8ef46_o.jpg', function ( texture ) {
let controls;

texture.encoding = THREE.sRGBEncoding;
texture.mapping = THREE.EquirectangularReflectionMapping;
init();
animate();

init( texture );
animate();

} );

function init( texture ) {
function init() {

renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.outputEncoding = THREE.sRGBEncoding;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
document.body.appendChild( renderer.domElement );

window.addEventListener( 'resize', onWindowResized );

stats = new Stats();
document.body.appendChild( stats.dom );

camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 1000 );
camera.position.z = 75;

scene = new THREE.Scene();
scene.background = texture;

camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 1000 );
new RGBELoader()
.setPath( 'textures/equirectangular/' )
.load( 'quarry_01_1k.hdr', function ( texture ) {

//
texture.mapping = THREE.EquirectangularReflectionMapping;
scene.background = texture;

const envSize = 64; // minimum size for roughness >= 0.1
} );

cubeRenderTarget1 = new THREE.WebGLCubeRenderTarget( envSize );
//

cubeCamera1 = new THREE.CubeCamera( 1, 1000, cubeRenderTarget1 );
const envSize = 256;

cubeRenderTarget1 = new THREE.WebGLCubeRenderTarget( envSize );
cubeRenderTarget2 = new THREE.WebGLCubeRenderTarget( envSize );

cubeCamera1 = new THREE.CubeCamera( 1, 1000, cubeRenderTarget1 );
cubeCamera2 = new THREE.CubeCamera( 1, 1000, cubeRenderTarget2 );

//

material = new THREE.MeshStandardMaterial( {
envMap: cubeRenderTarget2.texture,
roughness: 0.1,
metalness: 1
roughness: 0.05,
metalness: 0
} );

sphere = new THREE.Mesh( new THREE.IcosahedronGeometry( 20, 8 ), material );
const gui = new GUI();
gui.add( material, 'roughness', 0, 1 );
gui.add( material, 'metalness', 0, 1 );
gui.add( renderer, 'toneMappingExposure', 0, 2 ).name( 'exposure' );

sphere = new THREE.Mesh( new THREE.IcosahedronGeometry( 15, 8 ), material );
scene.add( sphere );

cube = new THREE.Mesh( new THREE.BoxGeometry( 20, 20, 20 ), material );
cube = new THREE.Mesh( new THREE.BoxGeometry( 15, 15, 15 ), material );
scene.add( cube );

torus = new THREE.Mesh( new THREE.TorusKnotGeometry( 10, 5, 128, 16 ), material );
torus = new THREE.Mesh( new THREE.TorusKnotGeometry( 10, 3, 128, 16 ), material );
scene.add( torus );

//

document.addEventListener( 'pointerdown', onPointerDown );
document.addEventListener( 'wheel', onDocumentMouseWheel );

window.addEventListener( 'resize', onWindowResized );
controls = new OrbitControls( camera, renderer.domElement );
controls.autoRotate = true;

}

Expand All @@ -118,57 +123,12 @@

}

function onPointerDown( event ) {

event.preventDefault();

onPointerDownPointerX = event.clientX;
onPointerDownPointerY = event.clientY;

onPointerDownLon = lon;
onPointerDownLat = lat;

document.addEventListener( 'pointermove', onPointerMove );
document.addEventListener( 'pointerup', onPointerUp );

}

function onPointerMove( event ) {

lon = ( event.clientX - onPointerDownPointerX ) * 0.1 + onPointerDownLon;
lat = ( event.clientY - onPointerDownPointerY ) * 0.1 + onPointerDownLat;

}

function onPointerUp() {

document.removeEventListener( 'pointermove', onPointerMove );
document.removeEventListener( 'pointerup', onPointerUp );

}

function onDocumentMouseWheel( event ) {

const fov = camera.fov + event.deltaY * 0.05;

camera.fov = THREE.MathUtils.clamp( fov, 10, 75 );

camera.updateProjectionMatrix();

}

function animate() {

requestAnimationFrame( animate );

const time = Date.now();

lon += .15;

lat = Math.max( - 85, Math.min( 85, lat ) );
phi = THREE.MathUtils.degToRad( 90 - lat );
theta = THREE.MathUtils.degToRad( lon );

cube.position.x = Math.cos( time * 0.001 ) * 30;
cube.position.y = Math.sin( time * 0.001 ) * 30;
cube.position.z = Math.sin( time * 0.001 ) * 30;
Expand All @@ -183,12 +143,6 @@
torus.rotation.x += 0.02;
torus.rotation.y += 0.03;

camera.position.x = 100 * Math.sin( phi ) * Math.cos( theta );
camera.position.y = 100 * Math.cos( phi );
camera.position.z = 100 * Math.sin( phi ) * Math.sin( theta );

camera.lookAt( scene.position );

// pingpong

if ( count % 2 === 0 ) {
Expand All @@ -205,16 +159,12 @@

count ++;

render();

stats.update();

}

function render() {
controls.update();

renderer.render( scene, camera );

stats.update();

}

</script>
Expand Down

0 comments on commit 944aeb1

Please sign in to comment.