UNPKG

@polygonjs/polygonjs

Version:

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

64 lines (63 loc) 2.18 kB
"use strict"; import { Color, Vector2, Vector3, Vector4 } from "three"; import { ThreejsCoreObject } from "../../core/geometry/modules/three/ThreejsCoreObject"; import { ObjectNamedFunction4 } from "./_Base"; import { JsConnectionPointType } from "../nodes/utils/io/connections/Js"; import { TypeAssert } from "../poly/Assert"; import { mix } from "../../core/math/_Module"; const tmpColor = new Color(); const tmpV2 = new Vector2(); const tmpV3 = new Vector3(); const tmpV4 = new Vector4(); function _lerpAndGetValue(object3D, attribName, lerp, newValue, type) { switch (type) { case JsConnectionPointType.BOOLEAN: { return newValue; } case JsConnectionPointType.COLOR: { ThreejsCoreObject.attribValue(object3D, attribName, 0, tmpColor); tmpColor.lerp(newValue, lerp); return tmpColor; } case JsConnectionPointType.FLOAT: case JsConnectionPointType.INT: { let currentValue = ThreejsCoreObject.attribValue(object3D, attribName, 0); if (currentValue == null) { currentValue = 0; } return mix(currentValue, newValue, lerp); } case JsConnectionPointType.STRING: { return newValue; } case JsConnectionPointType.VECTOR2: { ThreejsCoreObject.attribValue(object3D, attribName, 0, tmpV2); tmpV2.lerp(newValue, lerp); return tmpV2; } case JsConnectionPointType.VECTOR3: { ThreejsCoreObject.attribValue(object3D, attribName, 0, tmpV3); tmpV3.lerp(newValue, lerp); return tmpV3; } case JsConnectionPointType.VECTOR4: { ThreejsCoreObject.attribValue(object3D, attribName, 0, tmpV4); tmpV4.lerp(newValue, lerp); return tmpV4; } } TypeAssert.unreachable(type); } export class setObjectAttribute extends ObjectNamedFunction4 { static type() { return "setObjectAttribute"; } func(object3D, attribName, lerp, newValue, type) { if (lerp >= 1) { ThreejsCoreObject.setAttribute(object3D, attribName, newValue); } else { const lerpedValue = _lerpAndGetValue(object3D, attribName, lerp, newValue, type); ThreejsCoreObject.setAttribute(object3D, attribName, lerpedValue); } } }