Created
April 23, 2019 02:59
-
-
Save kohlmannj/d64df72eb8d7309ff7120bc9743e95dd to your computer and use it in GitHub Desktop.
A Three.js helper function which determines if the given scene contains lights
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as THREE from 'three'; | |
/** | |
* Determine if the given scene contains any lights, i.e. objects | |
* that are subclasses of the `THREE.Light` abstract base class. | |
* | |
* @param scene the scene to check for lights | |
* | |
* @returns whether or not the scene contains lights | |
*/ | |
export function hasLights(scene: THREE.Scene): boolean { | |
// Add lights to camera so that the camera can "see" them and render them | |
let sceneHasLights = false; | |
// The original `traverse()` callback has the signature `(object: THREE.Object3D) => any`. | |
// We're modifying it so that we can check to see if `object` is a `THREE.Light` subclass. | |
scene.traverse((object: THREE.Object3D | THREE.Light) => { | |
if ('isLight' in object) { | |
console.log('Found light in scene', object); | |
// This line has a TypeScript error in Three.js r103: | |
object.isLight; // ERROR: Property 'isLight' does not exist on type 'never'.ts(2339) | |
sceneHasLights = true; | |
} | |
}); | |
return sceneHasLights; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment