@polygonjs/polygonjs
Version:
node-based WebGL 3D engine https://polygonjs.com
65 lines (64 loc) • 2.19 kB
JavaScript
;
import { TypedMatNode } from "../_Base";
import { BaseTextureMapController, BooleanParamOptions, NodePathOptions } from "./_BaseTextureController";
import { NodeParamsConfig, ParamConfig } from "../../utils/params/ParamsConfig";
export function GradientMapParamConfig(Base) {
return class Mixin extends Base {
constructor() {
super(...arguments);
/** @param toggle if you want to use a gradient map */
this.useGradientMap = ParamConfig.BOOLEAN(0, {
...BooleanParamOptions(TextureGradientMapController),
separatorBefore: true
});
/** Gradient map for toon shading. It's required to set Texture.minFilter and Texture.magFilter to THREE.NearestFilter when using this type of texture */
this.gradientMap = ParamConfig.NODE_PATH("", NodePathOptions(TextureGradientMapController, "useGradientMap"));
}
};
}
function _isValidMaterial(material) {
if (!material) {
return false;
}
return true;
}
class TextureGradientMapParamsConfig extends GradientMapParamConfig(NodeParamsConfig) {
}
class TextureGradientMapMatNode extends TypedMatNode {
async material() {
const container = await this.compute();
return container.material();
}
}
export class TextureGradientMapController extends BaseTextureMapController {
constructor(node) {
super(node);
this.node = node;
}
initializeNode() {
this.add_hooks(this.node.p.useGradientMap, this.node.p.gradientMap);
}
static async update(node) {
node.controllers.gradientMap.update();
}
async update() {
const material = await this.node.material();
if (!_isValidMaterial(material)) {
return;
}
await this.updateMaterial(material);
}
async updateMaterial(material) {
await this._update(material, "gradientMap", this.node.p.useGradientMap, this.node.p.gradientMap);
}
getTextures(material, record) {
record.set("gradientMap", material.gradientMap);
}
setParamsFromMaterial(material, record) {
const mapNode = record.get("gradientMap");
this.node.p.useGradientMap.set(mapNode != null);
if (mapNode) {
this.node.p.gradientMap.setNode(mapNode, { relative: true });
}
}
}