Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Examples: Add webgl_loader_gltf_lights. #25506

Merged
merged 6 commits into from
Feb 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/files.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
"webgl_loader_gltf_compressed",
"webgl_loader_gltf_instancing",
"webgl_loader_gltf_iridescence",
"webgl_loader_gltf_lights",
"webgl_loader_gltf_sheen",
"webgl_loader_gltf_transmission",
"webgl_loader_gltf_variants",
Expand Down
Binary file added examples/models/gltf/LightsPunctualLamp.glb
Binary file not shown.
Binary file added examples/screenshots/webgl_loader_gltf_lights.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
133 changes: 133 additions & 0 deletions examples/webgl_loader_gltf_lights.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - GLTFloader + Punctual Lights</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="info">
<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - GLTFLoader + <a href="https://github.com/KhronosGroup/glTF/tree/main/extensions/2.0/Khronos/KHR_lights_punctual" target="_blank" rel="noopener">KHR_lights_punctual</a><br />
Lamp by
<a href="https://www.artstation.com/teresagviegas" target="_blank" rel="noopener">Teresa González Viegas</a>
</div>

<!-- Import maps polyfill -->
<!-- Remove this when import maps will be widely supported -->
<script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>

<script type="importmap">
{
"imports": {
"three": "../build/three.module.js",
"three/addons/": "./jsm/"
}
}
</script>

<script type="module">

import * as THREE from 'three';

import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
import { RGBELoader } from 'three/addons/loaders/RGBELoader.js';

import { GUI } from 'three/addons/libs/lil-gui.module.min.js';

let camera, scene, renderer;

const params = {
punctualLightsEnabled: true
};

init().then( render );

async function init() {

const container = document.createElement( 'div' );
document.body.appendChild( container );

camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.25, 20 );
camera.position.set( - 2, 1.5, 3 );

scene = new THREE.Scene();

const rgbeLoader = new RGBELoader();
const envMap = await rgbeLoader.loadAsync( 'textures/equirectangular/moonless_golf_1k.hdr ' );
envMap.mapping = THREE.EquirectangularReflectionMapping;

scene.background = envMap;
scene.environment = envMap;

const loader = new GLTFLoader();
const gltf = await loader.loadAsync( 'models/gltf/LightsPunctualLamp.glb' );

scene.add( gltf.scene );

const gui = new GUI();

gui.add( params, 'punctualLightsEnabled' ).onChange( toggleLights );
gui.open();

renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1;
renderer.outputEncoding = THREE.sRGBEncoding;
renderer.useLegacyLights = false;
container.appendChild( renderer.domElement );

const controls = new OrbitControls( camera, renderer.domElement );
controls.addEventListener( 'change', render ); // use if there is no animation loop
controls.minDistance = 2;
controls.maxDistance = 10;
controls.target.set( 0, 1, 0 );
controls.update();

window.addEventListener( 'resize', onWindowResize );

}

function onWindowResize() {

camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();

renderer.setSize( window.innerWidth, window.innerHeight );

render();

}

function toggleLights( visible ) {

scene.traverse( function ( object ) {

if ( object.isLight ) {

object.visible = visible;

}

} );

render();

}

//

function render() {

renderer.render( scene, camera );

}

</script>

</body>
</html>