@polygonjs/polygonjs
Version:
node-based WebGL 3D engine https://polygonjs.com
55 lines (54 loc) • 1.66 kB
JavaScript
;
import { Vector3 } from "three";
import { Attribute } from "../../Attribute";
import { objectCloneDeep } from "../../../ObjectUtils";
const _v3 = new Vector3();
export class QuadGeometry {
constructor() {
this.attributes = {};
this.index = [];
this.userData = {};
}
setAttribute(attribName, attribute) {
this.attributes[attribName] = attribute;
}
setIndex(indices) {
this.index = indices;
}
quadsCount() {
return this.index.length / 4;
}
applyMatrix(matrix) {
const positionAttribute = this.attributes[Attribute.POSITION];
const positionArray = positionAttribute.array;
const pointsCount = positionArray.length / 3;
for (let i = 0; i < pointsCount; i++) {
_v3.fromArray(positionArray, i * 3);
_v3.applyMatrix4(matrix);
_v3.toArray(positionArray, i * 3);
}
return this;
}
clone() {
const clonedGeometry = new this.constructor();
const pointAttributeNames = Object.keys(this.attributes);
for (const attributeName of pointAttributeNames) {
clonedGeometry.setAttribute(attributeName, this.attributes[attributeName].clone());
}
clonedGeometry.setIndex([...this.index]);
clonedGeometry.userData = objectCloneDeep(this.userData);
return clonedGeometry;
}
boundingBox(target) {
target.makeEmpty();
const positionAttribute = this.attributes[Attribute.POSITION];
if (!positionAttribute) {
return;
}
const positions = positionAttribute.array;
const arrayLength = positions.length;
for (let i = 0; i < arrayLength; i += 3) {
target.expandByPoint(_v3.fromArray(positions, i));
}
}
}