xtorcga
Version:
Xtor Compute Geometry Algorithm Libary 计算几何算法库
81 lines (80 loc) • 3.01 kB
JavaScript
;
var __spreadArrays = (this && this.__spreadArrays) || function () {
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
for (var r = Array(s), k = 0, i = 0; i < il; i++)
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
r[k] = a[j];
return r;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.trianglutionToGeometryBuffer = exports.toGeoBuffer = exports.triangListToBuffer = exports.indexable = void 0;
/*
* @Description :
* @Author : 赵耀圣
* @Q群 : 632839661
* @Date : 2020-12-10 15:01:42
* @LastEditTime : 2021-03-11 09:09:57
* @FilePath : \cga.js\src\render\mesh.ts
*/
var Vec3_1 = require("../math/Vec3");
var array_1 = require("../utils/array");
var geometry_1 = require("./geometry");
var trianglution_1 = require("../alg/trianglution");
function indexable(obj, refIndexInfo, force) {
if (refIndexInfo === void 0) { refIndexInfo = { index: 0 }; }
if (force === void 0) { force = false; }
if (obj instanceof Array) {
for (var i = 0; i < obj.length; i++)
indexable(obj[i], refIndexInfo);
}
else if (obj instanceof Object) {
if (obj.index === undefined)
obj.index = refIndexInfo.index++;
else if (force)
obj.index = refIndexInfo.index++;
}
}
exports.indexable = indexable;
function triangListToBuffer(vertices, triangleList) {
indexable(triangleList);
var indices = [];
array_1.forall(triangleList, function (v) {
indices.push(v.index);
});
return toGeoBuffer(vertices, indices);
}
exports.triangListToBuffer = triangListToBuffer;
/**
* 顶点纹理坐标所以转化为buffer数据
* @param {Array<Verctor3|Number>} vertices
* @param {Array<Number>} indices
* @param {Array<Verctor2|Number>} uvs
*/
function toGeoBuffer(vertices, indices, uvs) {
var geometry = new geometry_1.BufferGeometry();
geometry.addAttribute('position', vertices, 3);
geometry.addAttribute('uv', uvs || new Float32Array(geometry.getAttribute('position').array.length / 3 * 2), 2);
geometry.setIndex(indices);
return geometry;
}
exports.toGeoBuffer = toGeoBuffer;
/**
* 三角剖分后转成几何体
* 只考虑XY平面
* @param {*} boundary
* @param {*} hole
* @param {*} options
*/
function trianglutionToGeometryBuffer(boundary, holes, options) {
if (holes === void 0) { holes = []; }
if (options === void 0) { options = { normal: Vec3_1.Vec3.UnitZ }; }
var triangles = trianglution_1.triangulation(boundary, holes, options);
var vertices = __spreadArrays(boundary, array_1.flat(holes));
var uvs = [];
vertices.forEach(function (v) {
uvs.push(v.x, v.z);
});
var geometry = toGeoBuffer(vertices, triangles, uvs);
return geometry;
}
exports.trianglutionToGeometryBuffer = trianglutionToGeometryBuffer;