(function (global, factory) { if (typeof define === "function" && define.amd) { define(["exports", "three"], factory); } else if (typeof exports !== "undefined") { factory(exports, require("three")); } else { var mod = { exports: {} }; factory(mod.exports, global.three); global.XREstimatedLight = mod.exports; } })(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_exports, _three) { "use strict"; Object.defineProperty(_exports, "__esModule", { value: true }); _exports.XREstimatedLight = void 0; function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } Object.defineProperty(subClass, "prototype", { value: Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }), writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _possibleConstructorReturn(self, call) { if (call && (typeof call === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } var SessionLightProbe = /*#__PURE__*/function () { function SessionLightProbe(xrLight, renderer, lightProbe, environmentEstimation, estimationStartCallback) { var _this = this; _classCallCheck(this, SessionLightProbe); this.xrLight = xrLight; this.renderer = renderer; this.lightProbe = lightProbe; this.xrWebGLBinding = null; this.estimationStartCallback = estimationStartCallback; this.frameCallback = this.onXRFrame.bind(this); var session = renderer.xr.getSession(); // If the XRWebGLBinding class is available then we can also query an // estimated reflection cube map. if (environmentEstimation && 'XRWebGLBinding' in window) { // This is the simplest way I know of to initialize a WebGL cubemap in Three. var cubeRenderTarget = new _three.WebGLCubeRenderTarget(16); xrLight.environment = cubeRenderTarget.texture; var gl = renderer.getContext(); // Ensure that we have any extensions needed to use the preferred cube map format. switch (session.preferredReflectionFormat) { case 'srgba8': gl.getExtension('EXT_sRGB'); break; case 'rgba16f': gl.getExtension('OES_texture_half_float'); break; } this.xrWebGLBinding = new XRWebGLBinding(session, gl); this.lightProbe.addEventListener('reflectionchange', function () { _this.updateReflection(); }); } // Start monitoring the XR animation frame loop to look for lighting // estimation changes. session.requestAnimationFrame(this.frameCallback); } _createClass(SessionLightProbe, [{ key: "updateReflection", value: function updateReflection() { var textureProperties = this.renderer.properties.get(this.xrLight.environment); if (textureProperties) { var cubeMap = this.xrWebGLBinding.getReflectionCubeMap(this.lightProbe); if (cubeMap) { textureProperties.__webglTexture = cubeMap; } } } }, { key: "onXRFrame", value: function onXRFrame(time, xrFrame) { // If either this obejct or the XREstimatedLight has been destroyed, stop // running the frame loop. if (!this.xrLight) { return; } var session = xrFrame.session; session.requestAnimationFrame(this.frameCallback); var lightEstimate = xrFrame.getLightEstimate(this.lightProbe); if (lightEstimate) { // We can copy the estimate's spherical harmonics array directly into the light probe. this.xrLight.lightProbe.sh.fromArray(lightEstimate.sphericalHarmonicsCoefficients); this.xrLight.lightProbe.intensity = 1.0; // For the directional light we have to normalize the color and set the scalar as the // intensity, since WebXR can return color values that exceed 1.0. var intensityScalar = Math.max(1.0, Math.max(lightEstimate.primaryLightIntensity.x, Math.max(lightEstimate.primaryLightIntensity.y, lightEstimate.primaryLightIntensity.z))); this.xrLight.directionalLight.color.setRGB(lightEstimate.primaryLightIntensity.x / intensityScalar, lightEstimate.primaryLightIntensity.y / intensityScalar, lightEstimate.primaryLightIntensity.z / intensityScalar); this.xrLight.directionalLight.intensity = intensityScalar; this.xrLight.directionalLight.position.copy(lightEstimate.primaryLightDirection); if (this.estimationStartCallback) { this.estimationStartCallback(); this.estimationStartCallback = null; } } } }, { key: "dispose", value: function dispose() { this.xrLight = null; this.renderer = null; this.lightProbe = null; this.xrWebGLBinding = null; } }]); return SessionLightProbe; }(); var XREstimatedLight = /*#__PURE__*/function (_Group) { _inherits(XREstimatedLight, _Group); var _super = _createSuper(XREstimatedLight); function XREstimatedLight(renderer) { var _this2; var environmentEstimation = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true; _classCallCheck(this, XREstimatedLight); _this2 = _super.call(this); _this2.lightProbe = new _three.LightProbe(); _this2.lightProbe.intensity = 0; _this2.add(_this2.lightProbe); _this2.directionalLight = new _three.DirectionalLight(); _this2.directionalLight.intensity = 0; _this2.add(_this2.directionalLight); // Will be set to a cube map in the SessionLightProbe is environment estimation is // available and requested. _this2.environment = null; var sessionLightProbe = null; var estimationStarted = false; renderer.xr.addEventListener('sessionstart', function () { var session = renderer.xr.getSession(); if ('requestLightProbe' in session) { session.requestLightProbe({ reflectionFormat: session.preferredReflectionFormat }).then(function (probe) { sessionLightProbe = new SessionLightProbe(_assertThisInitialized(_this2), renderer, probe, environmentEstimation, function () { estimationStarted = true; // Fired to indicate that the estimated lighting values are now being updated. _this2.dispatchEvent({ type: 'estimationstart' }); }); }); } }); renderer.xr.addEventListener('sessionend', function () { if (sessionLightProbe) { sessionLightProbe.dispose(); sessionLightProbe = null; } if (estimationStarted) { // Fired to indicate that the estimated lighting values are no longer being updated. _this2.dispatchEvent({ type: 'estimationend' }); } }); // Done inline to provide access to sessionLightProbe. _this2.dispose = function () { if (sessionLightProbe) { sessionLightProbe.dispose(); sessionLightProbe = null; } _this2.remove(_this2.lightProbe); _this2.lightProbe = null; _this2.remove(_this2.directionalLight); _this2.directionalLight = null; _this2.environment = null; }; return _this2; } return _createClass(XREstimatedLight); }(_three.Group); _exports.XREstimatedLight = XREstimatedLight; });