@polygonjs/polygonjs
Version:
node-based WebGL 3D engine https://polygonjs.com
71 lines (70 loc) • 2.44 kB
JavaScript
;
import { TypedMatNode } from "../_Base";
import { BaseTextureMapController, BooleanParamOptions, NodePathOptions } from "./_BaseTextureController";
import { NodeParamsConfig, ParamConfig } from "../../utils/params/ParamsConfig";
export function LightMapParamConfig(Base) {
return class Mixin extends Base {
constructor() {
super(...arguments);
/** @param toggle if you want to use a light map */
this.useLightMap = ParamConfig.BOOLEAN(0, {
separatorBefore: true,
...BooleanParamOptions(TextureLightMapController)
});
/** @param specify the light map COP node */
this.lightMap = ParamConfig.NODE_PATH("", NodePathOptions(TextureLightMapController, "useLightMap"));
/** @param light. When set to 0, reflections from environment maps will be very sharp, or blurred when 1. Any value between 0 and 1 can help modulate this. */
this.lightMapIntensity = ParamConfig.FLOAT(1, {
visibleIf: { useLightMap: 1 }
});
}
};
}
function _isValidMaterial(material) {
if (!material) {
return false;
}
return material.lightMapIntensity != null;
}
class TextureLightMapParamsConfig extends LightMapParamConfig(NodeParamsConfig) {
}
class TextureLightMapMatNode extends TypedMatNode {
async material() {
const container = await this.compute();
return container.material();
}
}
export class TextureLightMapController extends BaseTextureMapController {
constructor(node) {
super(node);
this.node = node;
}
initializeNode() {
this.add_hooks(this.node.p.useLightMap, this.node.p.lightMap);
}
static async update(node) {
node.controllers.lightMap.update();
}
async update() {
const material = await this.node.material();
if (!_isValidMaterial(material)) {
return;
}
await this.updateMaterial(material);
}
async updateMaterial(material) {
await this._update(material, "lightMap", this.node.p.useLightMap, this.node.p.lightMap);
material.lightMapIntensity = this.node.pv.lightMapIntensity;
}
getTextures(material, record) {
record.set("lightMap", material.lightMap);
}
setParamsFromMaterial(material, record) {
const mapNode = record.get("lightMap");
this.node.p.useLightMap.set(mapNode != null);
if (mapNode) {
this.node.p.lightMap.setNode(mapNode, { relative: true });
}
this.node.p.lightMapIntensity.set(material.lightMapIntensity);
}
}