@polygonjs/polygonjs
Version:
node-based WebGL 3D engine https://polygonjs.com
106 lines (105 loc) • 4.39 kB
JavaScript
"use strict";
import { TypedMatNode } from "../_Base";
import { BaseTextureMapController, BooleanParamOptions, NodePathOptions } from "./_BaseTextureController";
import { NodeParamsConfig, ParamConfig } from "../../utils/params/ParamsConfig";
import { TypedNodePathParamValue } from "../../../../core/Walker";
export const METALNESS_ROUGHNESS_OPERATION_DEFAULT_PARAMS = {
useMetalnessMap: false,
metalnessMap: new TypedNodePathParamValue(""),
metalness: 0,
useRoughnessMap: false,
roughnessMap: new TypedNodePathParamValue(""),
roughness: 1
};
const DEFAULT_PARAMS = METALNESS_ROUGHNESS_OPERATION_DEFAULT_PARAMS;
export function MetalnessRoughnessMapParamConfig(Base) {
return class Mixin extends Base {
constructor() {
super(...arguments);
/** @param toggle if you want to use a metalness map */
this.useMetalnessMap = ParamConfig.BOOLEAN(DEFAULT_PARAMS.useMetalnessMap, {
separatorBefore: true,
...BooleanParamOptions(TextureMetalnessRoughnessMapController)
});
/** @param specify the metalness map COP node */
this.metalnessMap = ParamConfig.NODE_PATH(
"",
NodePathOptions(TextureMetalnessRoughnessMapController, "useMetalnessMap")
);
/** @param metalness. It's recommended to either set this value to 0 or to 1, as objects are either metallic or not. Any value in between tends to look like an alien plastic */
this.metalness = ParamConfig.FLOAT(DEFAULT_PARAMS.metalness);
// a default of 0 is good to non builder materials, but 1 should be better for builders, in case metalness is multiplied with this param from its child output node
/** @param toggle if you want to use a roughness map */
this.useRoughnessMap = ParamConfig.BOOLEAN(DEFAULT_PARAMS.useRoughnessMap, {
separatorBefore: true,
...BooleanParamOptions(TextureMetalnessRoughnessMapController)
});
/** @param specify the roughness map COP node */
this.roughnessMap = ParamConfig.NODE_PATH(
"",
NodePathOptions(TextureMetalnessRoughnessMapController, "useRoughnessMap")
);
/** @param roughness. 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.roughness = ParamConfig.FLOAT(DEFAULT_PARAMS.roughness);
}
};
}
function _isValidMaterial(material) {
if (!material) {
return false;
}
return material.metalness != null;
}
class TextureMetalnessMapParamsConfig extends MetalnessRoughnessMapParamConfig(NodeParamsConfig) {
}
class TextureMetalnessMapMatNode extends TypedMatNode {
async material() {
const container = await this.compute();
return container.material();
}
}
export class TextureMetalnessRoughnessMapController extends BaseTextureMapController {
constructor(node) {
super(node);
this.node = node;
}
initializeNode() {
this.add_hooks(this.node.p.useMetalnessMap, this.node.p.metalnessMap);
}
static async update(node) {
node.controllers.metalnessRoughnessMap.update();
}
async update() {
const material = await this.node.material();
if (!_isValidMaterial(material)) {
return;
}
await this.updateMaterial(material);
}
async updateMaterial(material) {
material.metalness = this.node.pv.metalness;
material.roughness = this.node.pv.roughness;
await Promise.all([
this._update(material, "metalnessMap", this.node.p.useMetalnessMap, this.node.p.metalnessMap),
this._update(material, "roughnessMap", this.node.p.useRoughnessMap, this.node.p.roughnessMap)
]);
}
getTextures(material, record) {
record.set("metalnessMap", material.metalnessMap);
record.set("roughnessMap", material.roughnessMap);
}
setParamsFromMaterial(material, record) {
const metalnessMapNode = record.get("metalnessMap");
const roughnessMapNode = record.get("roughnessMap");
this.node.p.useMetalnessMap.set(metalnessMapNode != null);
this.node.p.useRoughnessMap.set(roughnessMapNode != null);
if (metalnessMapNode) {
this.node.p.metalnessMap.setNode(metalnessMapNode, { relative: true });
}
if (roughnessMapNode) {
this.node.p.roughnessMap.setNode(roughnessMapNode, { relative: true });
}
this.node.p.metalness.set(material.metalness);
this.node.p.roughness.set(material.roughness);
}
}