@polygonjs/polygonjs
Version:
node-based WebGL 3D engine https://polygonjs.com
79 lines (78 loc) • 2.54 kB
JavaScript
;
import { TypedMatNode } from "../_Base";
import { BaseTextureMapController, BooleanParamOptions, NodePathOptions } from "./_BaseTextureController";
import { NodeParamsConfig, ParamConfig } from "../../utils/params/ParamsConfig";
export function BumpMapParamConfig(Base) {
return class Mixin extends Base {
constructor() {
super(...arguments);
/** @param toggle if you want to use a bump map */
this.useBumpMap = ParamConfig.BOOLEAN(0, {
separatorBefore: true,
...BooleanParamOptions(TextureBumpMapController)
});
/** @param specify the bump map COP node */
this.bumpMap = ParamConfig.NODE_PATH("", NodePathOptions(TextureBumpMapController, "useBumpMap"));
/** @param bump scale */
this.bumpScale = ParamConfig.FLOAT(1, {
range: [0, 1],
rangeLocked: [false, false],
...NodePathOptions(TextureBumpMapController, "useBumpMap")
});
/** @param bump bias */
this.bumpBias = ParamConfig.FLOAT(0, {
range: [0, 1],
rangeLocked: [false, false],
...NodePathOptions(TextureBumpMapController, "useBumpMap")
});
}
};
}
function _isValidMaterial(material) {
if (!material) {
return false;
}
return material.bumpScale != null;
}
class TextureBumpMapParamsConfig extends BumpMapParamConfig(NodeParamsConfig) {
}
class TextureBumpMapMatNode extends TypedMatNode {
async material() {
const container = await this.compute();
return container.material();
}
}
export class TextureBumpMapController extends BaseTextureMapController {
constructor(node) {
super(node);
this.node = node;
}
initializeNode() {
this.add_hooks(this.node.p.useBumpMap, this.node.p.bumpMap);
}
static async update(node) {
node.controllers.bumpMap.update();
}
async update() {
const material = await this.node.material();
if (!_isValidMaterial(material)) {
return;
}
await this.updateMaterial(material);
}
async updateMaterial(material) {
await this._update(material, "bumpMap", this.node.p.useBumpMap, this.node.p.bumpMap);
material.bumpScale = this.node.pv.bumpScale;
}
getTextures(material, record) {
record.set("bumpMap", material.bumpMap);
}
setParamsFromMaterial(material, record) {
const mapNode = record.get("emissiveMap");
this.node.p.useBumpMap.set(mapNode != null);
if (mapNode) {
this.node.p.bumpMap.setNode(mapNode, { relative: true });
}
this.node.p.bumpScale.set(material.bumpScale);
}
}