@polygonjs/polygonjs
Version:
node-based WebGL 3D engine https://polygonjs.com
118 lines (117 loc) • 5.28 kB
JavaScript
;
import { BaseSopOperation } from "./_Base";
import { InputCloneMode } from "../../../engine/poly/InputCloneMode";
import {
Float32BufferAttribute,
Float16BufferAttribute,
Uint32BufferAttribute,
Int32BufferAttribute,
Uint16BufferAttribute,
Int16BufferAttribute,
Uint8ClampedBufferAttribute,
Uint8BufferAttribute,
Int8BufferAttribute
} from "three";
import { isBooleanTrue } from "../../../core/BooleanValue";
import { corePointClassFactory, coreVertexClassFactory } from "../../../core/geometry/CoreObjectFactory";
export var AttribType = /* @__PURE__ */ ((AttribType2) => {
AttribType2["Float32BufferAttribute"] = "Float32BufferAttribute";
AttribType2["Float16BufferAttribute"] = "Float16BufferAttribute";
AttribType2["Uint32BufferAttribute"] = "Uint32BufferAttribute";
AttribType2["Int32BufferAttribute"] = "Int32BufferAttribute";
AttribType2["Uint16BufferAttribute"] = "Uint16BufferAttribute";
AttribType2["Int16BufferAttribute"] = "Int16BufferAttribute";
AttribType2["Uint8ClampedBufferAttribute"] = "Uint8ClampedBufferAttribute";
AttribType2["Uint8BufferAttribute"] = "Uint8BufferAttribute";
AttribType2["Int8BufferAttribute"] = "Int8BufferAttribute";
return AttribType2;
})(AttribType || {});
export const ATTRIB_TYPES = [
"Float32BufferAttribute" /* Float32BufferAttribute */,
"Float16BufferAttribute" /* Float16BufferAttribute */,
"Uint32BufferAttribute" /* Uint32BufferAttribute */,
"Int32BufferAttribute" /* Int32BufferAttribute */,
"Uint16BufferAttribute" /* Uint16BufferAttribute */,
"Int16BufferAttribute" /* Int16BufferAttribute */,
"Uint8ClampedBufferAttribute" /* Uint8ClampedBufferAttribute */,
"Uint8BufferAttribute" /* Uint8BufferAttribute */,
"Int8BufferAttribute" /* Int8BufferAttribute */
];
const ATTRIB_CLASS_BY_TYPE = {
["Float32BufferAttribute" /* Float32BufferAttribute */]: Float32BufferAttribute,
["Float16BufferAttribute" /* Float16BufferAttribute */]: Float16BufferAttribute,
["Uint32BufferAttribute" /* Uint32BufferAttribute */]: Uint32BufferAttribute,
["Int32BufferAttribute" /* Int32BufferAttribute */]: Int32BufferAttribute,
["Uint16BufferAttribute" /* Uint16BufferAttribute */]: Uint16BufferAttribute,
["Int16BufferAttribute" /* Int16BufferAttribute */]: Int16BufferAttribute,
["Uint8ClampedBufferAttribute" /* Uint8ClampedBufferAttribute */]: Uint8ClampedBufferAttribute,
["Uint8BufferAttribute" /* Uint8BufferAttribute */]: Uint8BufferAttribute,
["Int8BufferAttribute" /* Int8BufferAttribute */]: Int8BufferAttribute
};
const ARRAY_CLASS_BY_TYPE = {
["Float32BufferAttribute" /* Float32BufferAttribute */]: Float32Array,
["Float16BufferAttribute" /* Float16BufferAttribute */]: Uint16Array,
["Uint32BufferAttribute" /* Uint32BufferAttribute */]: Uint32Array,
["Int32BufferAttribute" /* Int32BufferAttribute */]: Int32Array,
["Uint16BufferAttribute" /* Uint16BufferAttribute */]: Uint16Array,
["Int16BufferAttribute" /* Int16BufferAttribute */]: Int16Array,
["Uint8ClampedBufferAttribute" /* Uint8ClampedBufferAttribute */]: Uint8Array,
["Uint8BufferAttribute" /* Uint8BufferAttribute */]: Uint8Array,
["Int8BufferAttribute" /* Int8BufferAttribute */]: Int8Array
};
export class AttribCastSopOperation extends BaseSopOperation {
static type() {
return "attribCast";
}
cook(inputCoreGroups, params) {
const coreGroup = inputCoreGroups[0];
const objects = coreGroup.allObjects();
for (let object of objects) {
this._castPointAttributes(object, params);
}
return coreGroup;
}
_castPointAttributes(object, params) {
const type = ATTRIB_TYPES[params.type];
const attribClass = ATTRIB_CLASS_BY_TYPE[type];
const arrayClass = ARRAY_CLASS_BY_TYPE[type];
const corePointClass = corePointClassFactory(object);
if (isBooleanTrue(params.castAttributes)) {
const attribNames = corePointClass.attributeNamesMatchingMask(object, params.mask);
for (let attribName of attribNames) {
const attrib = corePointClass.attribute(object, attribName);
const array = attrib.array;
const count = attrib.count;
const itemSize = attrib.itemSize;
const newArray = new arrayClass(count * itemSize);
for (let i = 0; i < array.length; i++) {
newArray[i] = array[i];
}
const newAttribute = new attribClass(newArray, itemSize);
corePointClass.addAttribute(object, attribName, newAttribute);
}
}
if (params.castIndex) {
const coreVertexClass = coreVertexClassFactory(object);
const index = coreVertexClass.indexAttribute(object);
if (index) {
const array = index.array;
const count = index.count;
const itemSize = 1;
const newArray = new arrayClass(count * itemSize);
for (let i = 0; i < array.length; i++) {
newArray[i] = array[i];
}
const newAttr = new attribClass(newArray, itemSize);
coreVertexClass.setIndexAttribute(object, newAttr);
}
}
}
}
AttribCastSopOperation.DEFAULT_PARAMS = {
castAttributes: true,
mask: "*",
castIndex: false,
type: ATTRIB_TYPES.indexOf("Float32BufferAttribute" /* Float32BufferAttribute */)
};
AttribCastSopOperation.INPUT_CLONED_STATE = InputCloneMode.FROM_NODE;