@itwin/core-frontend
Version:
iTwin.js frontend components
87 lines • 3.64 kB
JavaScript
"use strict";
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module WebGL
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.ShadowUniforms = void 0;
const core_bentley_1 = require("@itwin/core-bentley");
const core_geometry_1 = require("@itwin/core-geometry");
const core_common_1 = require("@itwin/core-common");
const FloatRGBA_1 = require("./FloatRGBA");
const Matrix_1 = require("./Matrix");
const Sync_1 = require("./Sync");
/** Maintains state of uniforms used for applying shadows.
* @internal
*/
class ShadowUniforms {
// The projection matrix depends on the model matrix.
syncToken;
syncKey = 0;
// CPU state
_target;
_enabled = false;
_projectionMatrix = core_geometry_1.Matrix4d.createIdentity();
_color = core_common_1.RgbColor.fromJSON(undefined);
_bias = 0;
// GPU state
_projection32 = new Matrix_1.Matrix4();
_colorAndBias = new FloatRGBA_1.FloatRgba();
// Working variables
_scratchModel = core_geometry_1.Matrix4d.createIdentity();
_scratchModelProjection = core_geometry_1.Matrix4d.createIdentity();
constructor(target) {
this._target = target;
}
update() {
const map = this._target.solarShadowMap;
if (this._enabled !== map.isEnabled) {
(0, Sync_1.desync)(this);
this._enabled = map.isEnabled;
}
if (!map.isEnabled)
return;
const settings = (0, core_bentley_1.expectDefined)(map.settings);
if (this._bias !== settings.bias) {
(0, Sync_1.desync)(this);
this._bias = this._colorAndBias.alpha = settings.bias;
}
if (!this._color.equals(settings.color)) {
(0, Sync_1.desync)(this);
this._color = settings.color;
this._colorAndBias.setTbgr(core_common_1.ColorDef.computeTbgrFromComponents(settings.color.r, settings.color.g, settings.color.b));
this._colorAndBias.alpha = this._bias;
}
// NB: The projection matrix must be computed later when it is bound because it uses the model matrix.
const proj = map.projectionMatrix;
if (!proj.isExactEqual(this._projectionMatrix)) {
(0, Sync_1.desync)(this);
proj.clone(this._projectionMatrix);
}
}
computeProjection() {
const branch = this._target.uniforms.branch;
if ((0, Sync_1.sync)(branch, this))
return;
// NB: We could decouple from the other uniforms so they don't get invalidated when frustum changes but meh.
(0, Sync_1.desync)(this);
const proj = this._target.solarShadowMap.projectionMatrix;
const model = core_geometry_1.Matrix4d.createTransform(this._target.currentTransform, this._scratchModel);
const modelProj = proj.multiplyMatrixMatrix(model, this._scratchModelProjection);
this._projection32.initFromMatrix4d(modelProj);
}
bindColorAndBias(uniform) {
if (!(0, Sync_1.sync)(this, uniform))
this._colorAndBias.bind(uniform);
}
bindProjectionMatrix(uniform) {
this.computeProjection();
if (!(0, Sync_1.sync)(this, uniform))
uniform.setMatrix4(this._projection32);
}
}
exports.ShadowUniforms = ShadowUniforms;
//# sourceMappingURL=ShadowUniforms.js.map