UNPKG

@polygonjs/polygonjs

Version:

node-based WebGL 3D engine https://polygonjs.com

66 lines (65 loc) 1.98 kB
"use strict"; import { TypedMatNode } from "../_Base"; import { BaseTextureMapController, BooleanParamOptions, NodePathOptions } from "./_BaseTextureController"; import { NodeParamsConfig, ParamConfig } from "../../utils/params/ParamsConfig"; export function MapParamConfig(Base) { return class Mixin extends Base { constructor() { super(...arguments); /** @param toggle on to use a map affecting color */ this.useMap = ParamConfig.BOOLEAN(0, { ...BooleanParamOptions(TextureMapController), separatorBefore: true }); /** @param texture map affecting color */ this.map = ParamConfig.NODE_PATH("", NodePathOptions(TextureMapController, "useMap")); } }; } function _isValidMaterial(material) { if (!material) { return false; } return true; } class TextureMapParamsConfig extends MapParamConfig(NodeParamsConfig) { } class TextureMapMatNode extends TypedMatNode { async material() { const container = await this.compute(); return container.material(); } } export class TextureMapController extends BaseTextureMapController { constructor(node) { super(node); this.node = node; } initializeNode() { this.add_hooks(this.node.p.useMap, this.node.p.map); } static async update(node) { node.controllers.map.update(); } async update() { const material = await this.node.material(); if (!_isValidMaterial(material)) { console.warn("invalid mat for TextureMapController", material); return; } await this.updateMaterial(material); } async updateMaterial(material) { await this._update(material, "map", this.node.p.useMap, this.node.p.map); } getTextures(material, record) { record.set("map", material.map); } setParamsFromMaterial(material, record) { const mapNode = record.get("map"); this.node.p.useMap.set(mapNode != null); if (mapNode) { this.node.p.map.setNode(mapNode, { relative: true }); } } }