UNPKG

@polygonjs/polygonjs

Version:

node-based WebGL 3D engine https://polygonjs.com

90 lines (89 loc) 3.88 kB
"use strict"; import { DirectionalLight, LightProbe } from "three"; import { XREstimatedLight } from "three/examples/jsm/webxr/XREstimatedLight"; import { coreObjectClassFactory } from "../../geometry/CoreObjectFactory"; const ATTRIB_NAME = { IS_ESTIMATED_LIGHT: "CoreWebXRAREstimatedLight_isEstimatedLight", IS_DEFAULT_LIGHTS_PARENT: "CoreWebXRAREstimatedLight_defaultLightsParent", // DEFAULT_ENVIRONMENT_COP_NODE_ID: 'CoreWebXRAREstimatedLight_defaultEnvCopNodeId', APPLY_ENV: "CoreWebXRAREstimatedLight_applyEnv", APPLY_LIGHT_PROBE: "CoreWebXRAREstimatedLight_applyLightProbe", APPLY_DIR_LIGHT: "CoreWebXRAREstimatedLight_applyDirLight" // DIR_LIGHT_INTENSITY: 'CoreWebXRAREstimatedLight_dirLightIntensity', }; export class CoreWebXRAREstimatedLightController { initialize(scene, renderer) { this._estimatedLightSourceObject = void 0; scene.threejsScene().traverse((object) => { if (this._estimatedLightSourceObject) { return; } const coreObjectClass = coreObjectClassFactory(object); const isEstimatedLight = coreObjectClass.attribValue(object, ATTRIB_NAME.IS_ESTIMATED_LIGHT); if (isEstimatedLight == true) { this._estimatedLightSourceObject = object; this._initEstimatedLight(scene, renderer); } }); } // const estimatedLight = new XREstimatedLight(renderer); async _initEstimatedLight(scene, renderer) { if (!this._estimatedLightSourceObject) { return; } const estimatedLightSourceObject = this._estimatedLightSourceObject; const defaultLightsParent = this._estimatedLightSourceObject.children.find((child) => { const coreObjectClass2 = coreObjectClassFactory(child); coreObjectClass2.attribValue(child, ATTRIB_NAME.IS_DEFAULT_LIGHTS_PARENT); }); const coreObjectClass = coreObjectClassFactory(estimatedLightSourceObject); const applyEnv = coreObjectClass.attribValue(estimatedLightSourceObject, ATTRIB_NAME.APPLY_ENV) || false; const applyLightProbe = coreObjectClass.attribValue(estimatedLightSourceObject, ATTRIB_NAME.APPLY_LIGHT_PROBE) || false; const applyDirLight = coreObjectClass.attribValue(estimatedLightSourceObject, ATTRIB_NAME.APPLY_DIR_LIGHT) || false; if (this._estimatedLight) { this._estimatedLightSourceObject.remove(this._estimatedLight); } this._estimatedLight = new XREstimatedLight(renderer); this._estimatedLightSourceObject.add(this._estimatedLight); const threejsScene = scene.threejsScene(); let previousEnv = null; const estimatedLight = this._estimatedLight; this._estimatedLight.addEventListener("estimationstart", () => { estimatedLightSourceObject.add(estimatedLight); if (defaultLightsParent) { estimatedLightSourceObject.remove(defaultLightsParent); } estimatedLight.traverse((child) => { if (child instanceof LightProbe) { if (applyLightProbe == false) { child.removeFromParent(); } } if (child instanceof DirectionalLight) { if (applyDirLight == false) { child.removeFromParent(); } } }); if (applyEnv && estimatedLight.environment) { previousEnv = threejsScene.environment; threejsScene.environment = estimatedLight.environment; } }); estimatedLight.addEventListener("estimationend", () => { if (defaultLightsParent) { estimatedLightSourceObject.add(defaultLightsParent); } estimatedLightSourceObject.remove(estimatedLight); threejsScene.environment = previousEnv; }); } dispose() { var _a; if (this._estimatedLight) { (_a = this._estimatedLightSourceObject) == null ? void 0 : _a.remove(this._estimatedLight); this._estimatedLight = void 0; } } } CoreWebXRAREstimatedLightController.ATTRIB_NAME = ATTRIB_NAME;