UNPKG

@polygonjs/polygonjs

Version:

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

225 lines (224 loc) 7.06 kB
"use strict"; import { Color, Quaternion, Vector2, Vector3, Vector4 } from "three"; import { ObjectNamedFunction3, ObjectNamedFunction4 } from "./_Base"; import { Attribute, markAttributeAsNeedsUpdateForFrame } from "../../core/geometry/Attribute"; const _c = new Color(); const _q = new Quaternion(); const _v2 = new Vector2(); const _v3 = new Vector3(); const _v3m = new Vector3(); const _v4 = new Vector4(); export function _setPointAttributeVector3Name(namedFunction, attribName) { return function(object3D, ptnum, newValue, lerp) { const geometry = object3D.geometry; if (!geometry) { return; } const attribute = geometry.getAttribute(attribName); if (!attribute) { return; } if (lerp >= 1) { newValue.toArray(attribute.array, ptnum * 3); } else { _v3.fromBufferAttribute(attribute, ptnum); _v3.lerp(newValue, lerp); _v3.toArray(attribute.array, ptnum * 3); } markAttributeAsNeedsUpdateForFrame(attribute, namedFunction.timeController.frame()); }; } export function _setPointAttributeVector3MultName(namedFunction, attribName) { return function(object3D, ptnum, newValue, mult, lerp) { const geometry = object3D.geometry; if (!geometry) { return; } const attribute = geometry.getAttribute(attribName); if (!attribute) { return; } _v3m.copy(newValue).multiplyScalar(mult); if (lerp >= 1) { _v3m.toArray(attribute.array, ptnum * 3); } else { _v3.fromBufferAttribute(attribute, ptnum); _v3.lerp(_v3m, lerp); _v3.toArray(attribute.array, ptnum * 3); } markAttributeAsNeedsUpdateForFrame(attribute, namedFunction.timeController.frame()); }; } export function _setPointAttributeQuaternionName(namedFunction, attribName) { return function(object3D, ptnum, newValue, lerp) { const geometry = object3D.geometry; if (!geometry) { return; } const attribute = geometry.getAttribute(attribName); if (!attribute) { return; } if (lerp >= 1) { newValue.toArray(attribute.array, ptnum * 4); } else { _q.fromBufferAttribute(attribute, ptnum); _q.slerp(newValue, lerp); _q.toArray(attribute.array, ptnum * 4); } markAttributeAsNeedsUpdateForFrame(attribute, namedFunction.timeController.frame()); }; } export function _setPointAttributeNumberForNamedFunction(namedFunction) { return function _setPointAttributeNumber(object3D, attribName, ptnum, newValue, lerp) { const geometry = object3D.geometry; if (!geometry) { return; } const attribute = geometry.getAttribute(attribName); if (!attribute) { return; } if (lerp >= 1) { attribute.array[ptnum] = newValue; } else { const oldValue = attribute.array[ptnum]; attribute.array[ptnum] = oldValue + (newValue - oldValue) * lerp; } markAttributeAsNeedsUpdateForFrame(attribute, namedFunction.timeController.frame()); }; } export function _setPointAttributeColorForNamedFunction(namedFunction) { return function _setPointAttributeColor(object3D, attribName, ptnum, newValue, lerp) { const geometry = object3D.geometry; if (!geometry) { return; } const attribute = geometry.getAttribute(attribName); if (!attribute) { return; } if (lerp >= 1) { newValue.toArray(attribute.array, ptnum * 3); } else { _c.fromBufferAttribute(attribute, ptnum); _c.lerp(newValue, lerp); _c.toArray(attribute.array, ptnum * 3); } markAttributeAsNeedsUpdateForFrame(attribute, namedFunction.timeController.frame()); }; } export function _setPointAttributeVector2ForNamedFunction(namedFunction) { return function _setPointAttributeVector2(object3D, attribName, ptnum, newValue, lerp) { const geometry = object3D.geometry; if (!geometry) { return; } const attribute = geometry.getAttribute(attribName); if (!attribute) { return; } if (lerp >= 1) { newValue.toArray(attribute.array, ptnum * 2); } else { _v2.fromBufferAttribute(attribute, ptnum); _v2.lerp(newValue, lerp); _v2.toArray(attribute.array, ptnum * 2); } markAttributeAsNeedsUpdateForFrame(attribute, namedFunction.timeController.frame()); }; } export function _setPointAttributeVector3ForNamedFunction(namedFunction) { return function _setPointAttributeVector3(object3D, attribName, ptnum, newValue, lerp) { const geometry = object3D.geometry; if (!geometry) { return; } const attribute = geometry.getAttribute(attribName); if (!attribute) { return; } if (lerp >= 1) { newValue.toArray(attribute.array, ptnum * 3); } else { _v3.fromBufferAttribute(attribute, ptnum); _v3.lerp(newValue, lerp); _v3.toArray(attribute.array, ptnum * 3); } markAttributeAsNeedsUpdateForFrame(attribute, namedFunction.timeController.frame()); }; } export function _setPointAttributeVector4ForNamedFunction(namedFunction) { return function _setPointAttributeVector4(object3D, attribName, ptnum, newValue, lerp) { const geometry = object3D.geometry; if (!geometry) { return; } const attribute = geometry.getAttribute(attribName); if (!attribute) { return; } if (lerp >= 1) { newValue.toArray(attribute.array, ptnum * 4); } else { _v4.fromBufferAttribute(attribute, ptnum); _v4.lerp(newValue, lerp); _v4.toArray(attribute.array, ptnum * 4); } markAttributeAsNeedsUpdateForFrame(attribute, namedFunction.timeController.frame()); }; } export class setPointPosition extends ObjectNamedFunction3 { constructor() { super(...arguments); this.func = _setPointAttributeVector3Name(this, Attribute.POSITION); } static type() { return "setPointPosition"; } } export class setPointAttributeNumber extends ObjectNamedFunction4 { constructor() { super(...arguments); this.func = _setPointAttributeNumberForNamedFunction(this); } static type() { return "setPointAttributeNumber"; } } export class setPointAttributeColor extends ObjectNamedFunction4 { constructor() { super(...arguments); this.func = _setPointAttributeColorForNamedFunction(this); } static type() { return "setPointAttributeColor"; } } export class setPointAttributeVector2 extends ObjectNamedFunction4 { constructor() { super(...arguments); this.func = _setPointAttributeVector2ForNamedFunction(this); } static type() { return "setPointAttributeVector2"; } } export class setPointAttributeVector3 extends ObjectNamedFunction4 { constructor() { super(...arguments); this.func = _setPointAttributeVector3ForNamedFunction(this); } static type() { return "setPointAttributeVector3"; } } export class setPointAttributeVector4 extends ObjectNamedFunction4 { constructor() { super(...arguments); this.func = _setPointAttributeVector4ForNamedFunction(this); } static type() { return "setPointAttributeVector4"; } }