@polygonjs/polygonjs
Version:
node-based WebGL 3D engine https://polygonjs.com
77 lines (76 loc) • 2.28 kB
JavaScript
"use strict";
import { TypedCopNode } from "./_Base";
import { NodeParamsConfig, ParamConfig } from "../utils/params/ParamsConfig";
import { NodeContext } from "../../poly/NodeContext";
import { CoreMask } from "../../../core/geometry/Mask";
import { isArray } from "../../../core/Type";
class FetchCopParamsConfig extends NodeParamsConfig {
constructor() {
super(...arguments);
/** @param sop node */
this.node = ParamConfig.NODE_PATH("", {
nodeSelection: {
context: NodeContext.SOP
}
});
/** @param group to read the material from */
this.group = ParamConfig.STRING("", {
objectMask: true
});
/** @param texture name */
this.name = ParamConfig.STRING("");
}
}
const ParamsConfig = new FetchCopParamsConfig();
export class FetchCopNode extends TypedCopNode {
constructor() {
super(...arguments);
this.paramsConfig = ParamsConfig;
}
static type() {
return "fetch";
}
async cook() {
const geometryNode = this.pv.node.nodeWithContext(NodeContext.SOP, this.states.error);
if (!geometryNode) {
this.states.error.set(`node not found at path '${this.pv.node.path()}'`);
return;
}
const container = await geometryNode.compute();
const coreGroup = container.coreContent();
if (!coreGroup) {
this.states.error.set(`geometry invalid`);
return;
}
const selectedObjects = CoreMask.filterThreejsObjects(coreGroup, this.pv);
if (selectedObjects.length == 0) {
this.states.error.set(`no object matching group`);
return;
}
let texture;
for (const selectedObject of selectedObjects) {
const material = selectedObject.material;
if (material) {
if (isArray(material)) {
for (const mat of material) {
texture = texture || this._textureFromMaterial(mat);
}
} else {
texture = texture || this._textureFromMaterial(material);
}
}
}
if (!texture) {
this.states.error.set(`no texture found`);
return;
}
this.setTexture(texture);
}
_textureFromMaterial(material) {
const textureName = this.pv.name;
const texture = material[textureName];
if (texture && texture.isTexture) {
return texture;
}
}
}