@polygonjs/polygonjs
Version:
node-based WebGL 3D engine https://polygonjs.com
81 lines (80 loc) • 2.69 kB
JavaScript
;
import { ParamConfig } from "../../../utils/params/ParamsConfig";
import { isBooleanTrue } from "../../../../../core/BooleanValue";
import { NodeContext } from "../../../../poly/NodeContext";
import { Euler, Vector3 } from "three";
import { degToRad } from "three/src/math/MathUtils";
const _rotationInDegrees = new Vector3();
const _euler = new Euler();
const CallbackOptions = {
cook: false,
callback: (node) => {
SceneEnvController.update(node);
}
};
export function SceneEnvParamConfig(Base) {
return class Mixin extends Base {
constructor() {
super(...arguments);
/** @param toggle on to use an environment map */
this.useEnvironment = ParamConfig.BOOLEAN(0, {
...CallbackOptions,
separatorBefore: true
});
/** @param environment map */
this.environment = ParamConfig.NODE_PATH("", {
visibleIf: { useEnvironment: 1 },
nodeSelection: {
context: NodeContext.COP
},
// dependentOnFoundNode: false,
...CallbackOptions
});
/** @param environment map intensity */
this.environmentIntensity = ParamConfig.FLOAT(1, { visibleIf: { useEnvironment: 1 }, ...CallbackOptions });
/** @param environment map rotation */
this.environmentRotation = ParamConfig.VECTOR3([0, 0, 0], { visibleIf: { useEnvironment: 1 }, ...CallbackOptions });
}
};
}
const CALLBACK_NAME = "SceneEnvController";
export class SceneEnvController {
constructor(node) {
this.node = node;
this._updateBound = this.update.bind(this);
}
addHooks() {
const p = this.node.p;
const params = [p.useEnvironment, p.environment];
for (const param of params) {
param.addPostDirtyHook(CALLBACK_NAME, this._updateBound);
}
}
async update() {
const scene = this.node.object;
const pv = this.node.pv;
if (isBooleanTrue(pv.useEnvironment)) {
const node = pv.environment.nodeWithContext(NodeContext.COP);
if (node) {
node.compute().then((container) => {
scene.environment = container.texture();
});
} else {
scene.environment = null;
this.node.states.error.set("environment node not found");
}
scene.environmentIntensity = pv.environmentIntensity;
_rotationInDegrees.copy(pv.environmentRotation);
const x = degToRad(_rotationInDegrees.x);
const y = degToRad(_rotationInDegrees.y);
const z = degToRad(_rotationInDegrees.z);
_euler.set(x, y, z);
scene.environmentRotation.copy(_euler);
} else {
scene.environment = null;
}
}
static async update(node) {
node.sceneEnvController.update();
}
}