toGeometry static method
BufferGeometry
toGeometry(
- CSG csg,
- Matrix4 toMatrix
)
Implementation
static BufferGeometry toGeometry(CSG csg, Matrix4 toMatrix){
int triCount = 0;
final ps = csg.polygons;
for (final p in ps) {
triCount += p.vertices.length - 2;
}
final geom = BufferGeometry();
final vertices = NBuf3(triCount * 3 * 3);
final normals = NBuf3(triCount * 3 * 3);
final uvs = NBuf2(triCount * 2 * 3);
NBuf3? colors;
final Map<int,List<int>> grps = {};
final List<int> dgrp = [];
for (final p in ps) {
final pvs = p.vertices;
final pvlen = pvs.length;
if (p.shared != null) {
if (grps[p.shared!] == null) grps[p.shared!] = [];
}
if (pvlen > 0 && pvs[0].color != null) {
colors ??= NBuf3(triCount * 3 * 3);
}
for (int j = 3; j <= pvlen; j++) {
final grp = p.shared == null ? dgrp : grps[p.shared!];
grp!.addAll([vertices.top ~/ 3, vertices.top ~/ 3 + 1, vertices.top ~/ 3 + 2]);
vertices.write(pvs[0].position);
vertices.write(pvs[j - 2].position);
vertices.write(pvs[j - 1].position);
normals.write(pvs[0].normal);
normals.write(pvs[j - 2].normal);
normals.write(pvs[j - 1].normal);
//if (uvs != null) {
uvs.write(pvs[0].uv);
uvs.write(pvs[j - 2].uv);
uvs.write(pvs[j - 1].uv);
//}
if (colors != null) {
colors.write(pvs[0].color!);
colors.write(pvs[j - 2].color!);
colors.write(pvs[j - 1].color!);
}
}
}
geom.setAttributeFromString('position', Float32BufferAttribute.fromList(vertices.array, 3));
geom.setAttributeFromString('normal', Float32BufferAttribute.fromList(normals.array, 3));
//if(uvs != null)
geom.setAttributeFromString('uv', Float32BufferAttribute.fromList(uvs.array, 2));
if(colors != null) geom.setAttributeFromString('color', Float32BufferAttribute.fromList(colors.array, 3));
for (int gi = 0; gi < grps.length; gi++) {
if (grps[gi] == null) {
grps[gi] = [];
}
}
if (grps.isNotEmpty) {
List<int> index = [];
int gbase = 0;
for (int gi = 0; gi < grps.length; gi++) {
geom.addGroup(gbase, grps[gi]!.length, gi);
gbase += grps[gi]!.length;
index = List.from(index)..addAll(grps[gi]!);
}
geom.addGroup(gbase, dgrp.length, grps.length);
index = List.from(index)..addAll(dgrp);
geom.setIndex(index);
}
final inv = Matrix4().setFrom(toMatrix).invert();
geom.applyMatrix4(inv);
geom.computeBoundingSphere();
geom.computeBoundingBox();
return geom;
}