@polygonjs/polygonjs
Version:
node-based WebGL 3D engine https://polygonjs.com
61 lines (60 loc) • 2.27 kB
JavaScript
;
import { BaseSopOperation } from "./_Base";
import { InputCloneMode } from "../../../engine/poly/InputCloneMode";
import { CubeTexture, LightProbe } from "three";
import { DEFAULT_LIGHT_PROBE_PARAMS } from "../../../core/lights/LightProbe";
import { NodeContext } from "../../poly/NodeContext";
import { LightProbeGenerator } from "three/examples/jsm/lights/LightProbeGenerator";
import { ObjectType, registerObjectType } from "../../../core/geometry/Constant";
export class LightProbeSopOperation extends BaseSopOperation {
static type() {
return "lightProbe";
}
async cook(inputCoreGroups, params) {
const light = this.createLight();
light.name = params.name;
await this.updateLightParams(light, params);
return this.createCoreGroupFromObjects([light]);
}
createLight() {
var _a;
registerObjectType({
type: ObjectType.LIGHT_PROBE,
checkFunc: (o) => {
if (o.isLightProbe) {
return ObjectType.LIGHT_PROBE;
}
},
ctor: LightProbe,
humanName: "LightProbe"
});
const light = new LightProbe();
light.name = `LightProbe_${((_a = this._node) == null ? void 0 : _a.name()) || ""}`;
light.matrixAutoUpdate = false;
light.updateMatrix();
return light;
}
async updateLightParams(light, params) {
var _a, _b, _c, _d;
const copNode = params.cubeMap.nodeWithContext(NodeContext.COP, (_a = this.states) == null ? void 0 : _a.error);
if (copNode) {
const container = await copNode.compute();
if (container) {
const texture = container.texture();
if (texture instanceof CubeTexture) {
const lightProbe = LightProbeGenerator.fromCubeTexture(texture);
light.copy(lightProbe);
light.sh.scale(params.intensity);
} else {
(_b = this.states) == null ? void 0 : _b.error.set(`texture node is not a cubeMap`);
}
} else {
(_c = this.states) == null ? void 0 : _c.error.set(`texture node invalid`);
}
} else {
(_d = this.states) == null ? void 0 : _d.error.set(`no texture node found`);
}
}
}
LightProbeSopOperation.DEFAULT_PARAMS = DEFAULT_LIGHT_PROBE_PARAMS;
LightProbeSopOperation.INPUT_CLONED_STATE = InputCloneMode.NEVER;