UNPKG

@polygonjs/polygonjs

Version:

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

131 lines (130 loc) 4.62 kB
"use strict"; import { Vector2 } from "three"; import { Vector3 } from "three"; import { Vector4 } from "three"; import { TypedSopNode } from "./_Base"; import { AttribSize } from "../../../core/geometry/Constant"; import { TypeAssert } from "../../poly/Assert"; import { NodeParamsConfig, ParamConfig } from "../utils/params/ParamsConfig"; import { isBooleanTrue } from "../../../core/BooleanValue"; import { pointsFromObject } from "../../../core/geometry/entities/point/CorePointUtils"; import { corePointClassFactory } from "../../../core/geometry/CoreObjectFactory"; const _points = []; class AttribRemapSopParamsConfig extends NodeParamsConfig { constructor() { super(...arguments); /** @param name of the attribute to remap */ this.name = ParamConfig.STRING(); /** @param ramp used to remap */ this.ramp = ParamConfig.RAMP(); /** @param toggle if you want to create a new attribute */ this.changeName = ParamConfig.BOOLEAN(0); /** @param new attribute name */ this.newName = ParamConfig.STRING("", { visibleIf: { changeName: 1 } }); } } const ParamsConfig = new AttribRemapSopParamsConfig(); export class AttribRemapSopNode extends TypedSopNode { constructor() { super(...arguments); this.paramsConfig = ParamsConfig; } static type() { return "attribRemap"; } initializeNode() { this.io.inputs.setCount(1); } cook(inputCoreGroups) { const coreGroup = inputCoreGroups[0]; const objects = coreGroup.allObjects(); for (const object of objects) { this._remapAttribute(object); } this.setCoreGroup(coreGroup); } _remapAttribute(object) { pointsFromObject(object, _points); if (_points.length === 0) { return; } if (this.pv.name === "") { return; } const corePointClass = corePointClassFactory(object); const attribSize = corePointClass.attribSize(object, this.pv.name); const values = _points.map((point) => point.attribValue(this.pv.name)); const remapedValues = new Array(_points.length); this._get_remaped_values(attribSize, values, remapedValues); let targetName = this.pv.name; if (isBooleanTrue(this.pv.changeName)) { targetName = this.pv.newName; if (!corePointClass.hasAttribute(object, targetName)) { corePointClass.addNumericAttribute(object, targetName, attribSize, 0); } } let i = 0; for (const normalized_value of remapedValues) { _points[i].setAttribValue(targetName, normalized_value); i++; } } _get_remaped_values(attrib_size, values, remaped_values) { switch (attrib_size) { case AttribSize.FLOAT: return this._getNormalizedFloat(values, remaped_values); case AttribSize.VECTOR2: return this._getNormalizedVector2(values, remaped_values); case AttribSize.VECTOR3: return this._getNormalizedVector3(values, remaped_values); case AttribSize.VECTOR4: return this._getNormalizedVector4(values, remaped_values); } TypeAssert.unreachable(attrib_size); } _getNormalizedFloat(values, remaped_values) { const valuesf = values; const ramp_param = this.p.ramp; for (let i = 0; i < valuesf.length; i++) { const value = valuesf[i]; const remaped_value = ramp_param.valueAtPosition(value); remaped_values[i] = remaped_value; } } _getNormalizedVector2(values, remaped_values) { const valuesv = values; const ramp_param = this.p.ramp; for (let i = 0; i < valuesv.length; i++) { const value = valuesv[i]; const remaped_value = new Vector2(ramp_param.valueAtPosition(value.x), ramp_param.valueAtPosition(value.y)); remaped_values[i] = remaped_value; } } _getNormalizedVector3(values, remaped_values) { const valuesv = values; const ramp_param = this.p.ramp; for (let i = 0; i < valuesv.length; i++) { const value = valuesv[i]; const remaped_value = new Vector3( ramp_param.valueAtPosition(value.x), ramp_param.valueAtPosition(value.y), ramp_param.valueAtPosition(value.z) ); remaped_values[i] = remaped_value; } } _getNormalizedVector4(values, remaped_values) { const valuesv = values; const ramp_param = this.p.ramp; for (let i = 0; i < valuesv.length; i++) { const value = valuesv[i]; const remaped_value = new Vector4( ramp_param.valueAtPosition(value.x), ramp_param.valueAtPosition(value.y), ramp_param.valueAtPosition(value.z), ramp_param.valueAtPosition(value.w) ); remaped_values[i] = remaped_value; } } }