polygonjs-engine
Version:
node-based webgl 3D engine https://polygonjs.com
73 lines (72 loc) • 2.43 kB
JavaScript
import {BaseSopOperation} from "./_Base";
import {Vector3 as Vector32} from "three/src/math/Vector3";
import {ObjectType} from "../../../core/geometry/Constant";
import {BufferGeometry as BufferGeometry2} from "three/src/core/BufferGeometry";
import {BufferAttribute as BufferAttribute2, Float32BufferAttribute} from "three/src/core/BufferAttribute";
export class AddSopOperation extends BaseSopOperation {
static type() {
return "add";
}
cook(input_contents, params) {
const objects = [];
this._create_point(objects, params);
this._create_polygon(input_contents[0], objects, params);
return this.create_core_group_from_objects(objects);
}
_create_point(objects, params) {
if (!params.createPoint) {
return;
}
const geometry = new BufferGeometry2();
const positions = [];
for (let i = 0; i < params.pointsCount; i++) {
params.position.toArray(positions, i * 3);
}
geometry.setAttribute("position", new BufferAttribute2(new Float32Array(positions), 3));
const object = this.create_object(geometry, ObjectType.POINTS);
if (objects) {
objects.push(object);
}
}
_create_polygon(core_group, objects, params) {
if (!params.connectInputPoints) {
return;
}
const points = core_group.points();
if (points.length > 0) {
this._create_polygon_open(core_group, objects, params);
}
}
_create_polygon_open(core_group, objects, params) {
const points = core_group.points();
let positions = [];
const indices = [];
let point;
for (let i = 0; i < points.length; i++) {
point = points[i];
point.position().toArray(positions, i * 3);
if (i > 0) {
indices.push(i - 1);
indices.push(i);
}
}
if (points.length > 2 && params.connectToLastPoint) {
points[0].position().toArray(positions, positions.length);
const last_index = indices[indices.length - 1];
indices.push(last_index);
indices.push(0);
}
const geometry = new BufferGeometry2();
geometry.setAttribute("position", new Float32BufferAttribute(positions, 3));
geometry.setIndex(indices);
const object = this.create_object(geometry, ObjectType.LINE_SEGMENTS);
objects.push(object);
}
}
AddSopOperation.DEFAULT_PARAMS = {
createPoint: true,
pointsCount: 1,
position: new Vector32(0, 0, 0),
connectInputPoints: false,
connectToLastPoint: false
};