|
| 1 | +import { RectAreaLight } from 'three'; |
| 2 | +import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js'; |
| 3 | + |
| 4 | +const ORB_SCENE_URL = 'https://raw.githubusercontent.com/gkjohnson/3d-demo-data/main/models/usd-shader-ball/usd-shaderball-scene.glb'; |
| 5 | + |
| 6 | +function assignWatts( light, watts ) { |
| 7 | + |
| 8 | + // https://github.com/will-ca/glTF-Blender-IO/blob/af9e7f06508a95425b05e485fa83681b268bbdfc/addons/io_scene_gltf2/blender/exp/gltf2_blender_gather_lights.py#L92-L97 |
| 9 | + const PBR_WATTS_TO_LUMENS = 683; |
| 10 | + const area = light.width * light.height; |
| 11 | + const lumens = PBR_WATTS_TO_LUMENS * watts; |
| 12 | + light.intensity = lumens / ( area * 4 * Math.PI ); |
| 13 | + |
| 14 | +} |
| 15 | + |
| 16 | +export class MaterialOrbSceneLoader { |
| 17 | + |
| 18 | + constructor( manager ) { |
| 19 | + |
| 20 | + this.manager = manager; |
| 21 | + |
| 22 | + } |
| 23 | + |
| 24 | + loadAsync( url = ORB_SCENE_URL, ...rest ) { |
| 25 | + |
| 26 | + return new GLTFLoader( this.manager ) |
| 27 | + .loadAsync( url, ...rest ) |
| 28 | + .then( gltf => { |
| 29 | + |
| 30 | + const { |
| 31 | + scene, |
| 32 | + lights, |
| 33 | + cameras, |
| 34 | + } = gltf; |
| 35 | + |
| 36 | + const leftLight = new RectAreaLight( 0xffffff, 1, 15, 15 ); |
| 37 | + assignWatts( leftLight, 6327.84 ); |
| 38 | + scene.getObjectByName( 'light' ).add( leftLight ); |
| 39 | + |
| 40 | + for ( let i = 0; i < 4; i ++ ) { |
| 41 | + |
| 42 | + const light = new RectAreaLight( 0xffffff, 1, 24.36, 24.36 ); |
| 43 | + assignWatts( leftLight, 11185.5 ); |
| 44 | + scene.getObjectByName( 'light' + i ).add( light ); |
| 45 | + |
| 46 | + } |
| 47 | + |
| 48 | + return { |
| 49 | + |
| 50 | + material: scene.getObjectByName( 'material_surface' ).material, |
| 51 | + cameras, |
| 52 | + lights, |
| 53 | + scene, |
| 54 | + |
| 55 | + }; |
| 56 | + |
| 57 | + } ); |
| 58 | + |
| 59 | + } |
| 60 | + |
| 61 | +} |
0 commit comments