playcanvas
Version:
PlayCanvas WebGL game engine
60 lines (57 loc) • 1.93 kB
JavaScript
import { BoundingBox } from '../../core/shape/bounding-box.js';
import { BoundingSphere } from '../../core/shape/bounding-sphere.js';
import { LIGHTTYPE_DIRECTIONAL } from '../../scene/constants.js';
const tempSphere = new BoundingSphere();
class BakeLight {
constructor(scene, light, lightingParams){
this.scene = scene;
this.light = light;
this.store();
light.numCascades = 1;
if (this.scene.clusteredLightingEnabled && !lightingParams.shadowsEnabled) {
light.castShadows = false;
}
if (light.type !== LIGHTTYPE_DIRECTIONAL) {
light._node.getWorldTransform();
light.getBoundingSphere(tempSphere);
this.lightBounds = new BoundingBox();
this.lightBounds.center.copy(tempSphere.center);
this.lightBounds.halfExtents.set(tempSphere.radius, tempSphere.radius, tempSphere.radius);
}
}
store() {
this.mask = this.light.mask;
this.shadowUpdateMode = this.light.shadowUpdateMode;
this.enabled = this.light.enabled;
this.intensity = this.light.intensity;
this.rotation = this.light._node.getLocalRotation().clone();
this.numCascades = this.light.numCascades;
this.castShadows = this.light._castShadows;
}
restore() {
const light = this.light;
light.mask = this.mask;
light.shadowUpdateMode = this.shadowUpdateMode;
light.enabled = this.enabled;
light.intensity = this.intensity;
light._node.setLocalRotation(this.rotation);
light.numCascades = this.numCascades;
light._castShadows = this.castShadows;
}
startBake() {
this.light.enabled = true;
this.light._destroyShadowMap();
this.light.beginFrame();
}
endBake(shadowMapCache) {
const light = this.light;
light.enabled = false;
if (light.shadowMap) {
if (light.shadowMap.cached) {
shadowMapCache.add(light, light.shadowMap);
}
light.shadowMap = null;
}
}
}
export { BakeLight };