fix > 65k verts

This commit is contained in:
RandomityGuy 2024-07-01 19:39:32 +05:30
parent 1706c16540
commit c07083f400
2 changed files with 98 additions and 89 deletions

View file

@ -581,12 +581,18 @@ class DifBuilder {
}
}
}
var mats = new Map<String, Array<DifBuilderTriangle>>();
var mats = new Map<String, Array<Array<DifBuilderTriangle>>>();
for (index => value in triangles) {
if (mats.exists(value.texture)) {
mats[value.texture].push(value);
var arr = mats[value.texture];
if (arr[arr.length - 1].length >= Math.floor(65535 / 3)) {
var newArr = [value];
arr.push(newArr);
} else {
arr[arr.length - 1].push(value);
}
} else {
mats.set(value.texture, [value]);
mats.set(value.texture, [[value]]);
}
}
collider.finalize();
@ -674,81 +680,89 @@ class DifBuilder {
});
var prim = new Polygon();
var materials = [];
for (grp => tris in mats) {
var points = [];
var normals = [];
var uvs = [];
for (tri in tris) {
var p1 = new Point(-tri.p1.x, tri.p1.y, tri.p1.z);
var p2 = new Point(-tri.p2.x, tri.p2.y, tri.p2.z);
var p3 = new Point(-tri.p3.x, tri.p3.y, tri.p3.z);
var n1 = new Point(-tri.normal1.x, tri.normal1.y, tri.normal1.z);
var n2 = new Point(-tri.normal2.x, tri.normal2.y, tri.normal2.z);
var n3 = new Point(-tri.normal3.x, tri.normal3.y, tri.normal3.z);
var uv1 = new UV(tri.uv1.x, tri.uv1.y);
var uv2 = new UV(tri.uv2.x, tri.uv2.y);
var uv3 = new UV(tri.uv3.x, tri.uv3.y);
points.push(p3);
points.push(p2);
points.push(p1);
normals.push(n3);
normals.push(n2);
normals.push(n1);
uvs.push(uv3);
uvs.push(uv2);
uvs.push(uv1);
}
prim.addPoints(points);
prim.addUVs(uvs);
prim.addNormals(normals);
prim.nextMaterial();
var material:Material;
var texture:Texture;
if (canFindTex(grp)) {
texture = ResourceLoader.getTextureRealpath(tex(grp)).resource; // ResourceLoader.getTexture(tex(grp), false).resource;
texture.wrap = Wrap.Repeat;
texture.mipMap = Nearest;
var exactName = StringTools.replace(texture.name, "data/", "").toLowerCase();
exactName = exactName.substring(0, exactName.lastIndexOf('.'));
material = h3d.mat.Material.create(texture);
var matDictName = exactName;
if (!shaderMaterialDict.exists(matDictName)) {
matDictName = StringTools.replace(exactName, "multiplayer/interiors/mbu", "interiors_mbu");
for (grp => trigroup in mats) {
for (tris in trigroup) {
var points = [];
var normals = [];
var uvs = [];
for (tri in tris) {
var p1 = new Point(-tri.p1.x, tri.p1.y, tri.p1.z);
var p2 = new Point(-tri.p2.x, tri.p2.y, tri.p2.z);
var p3 = new Point(-tri.p3.x, tri.p3.y, tri.p3.z);
var n1 = new Point(-tri.normal1.x, tri.normal1.y, tri.normal1.z);
var n2 = new Point(-tri.normal2.x, tri.normal2.y, tri.normal2.z);
var n3 = new Point(-tri.normal3.x, tri.normal3.y, tri.normal3.z);
var uv1 = new UV(tri.uv1.x, tri.uv1.y);
var uv2 = new UV(tri.uv2.x, tri.uv2.y);
var uv3 = new UV(tri.uv3.x, tri.uv3.y);
points.push(p3);
points.push(p2);
points.push(p1);
normals.push(n3);
normals.push(n2);
normals.push(n1);
uvs.push(uv3);
uvs.push(uv2);
uvs.push(uv1);
}
if (shaderMaterialDict.exists(matDictName)) {
var retrievefunc = shaderMaterialDict[matDictName];
shaderWorker.addTask(fwd -> {
retrievefunc(shad -> {
material.mainPass.removeShader(material.textureShader);
material.mainPass.addShader(shad);
var thisprops:Dynamic = material.getDefaultProps();
thisprops.light = false; // We will calculate our own lighting
material.props = thisprops;
material.shadows = false;
material.receiveShadows = true;
fwd();
if (prim.vertexCount() + points.length > 65535) {
prim.endPrimitive();
var mesh = new MultiMaterial(prim, materials, itr);
materials = [];
prim = new Polygon();
}
prim.addPoints(points);
prim.addUVs(uvs);
prim.addNormals(normals);
prim.nextMaterial();
var material:Material;
var texture:Texture;
if (canFindTex(grp)) {
texture = ResourceLoader.getTextureRealpath(tex(grp)).resource; // ResourceLoader.getTexture(tex(grp), false).resource;
texture.wrap = Wrap.Repeat;
texture.mipMap = Nearest;
var exactName = StringTools.replace(texture.name, "data/", "").toLowerCase();
exactName = exactName.substring(0, exactName.lastIndexOf('.'));
material = h3d.mat.Material.create(texture);
var matDictName = exactName;
if (!shaderMaterialDict.exists(matDictName)) {
matDictName = StringTools.replace(exactName, "multiplayer/interiors/mbu", "interiors_mbu");
}
if (shaderMaterialDict.exists(matDictName)) {
var retrievefunc = shaderMaterialDict[matDictName];
shaderWorker.addTask(fwd -> {
retrievefunc(shad -> {
material.mainPass.removeShader(material.textureShader);
material.mainPass.addShader(shad);
var thisprops:Dynamic = material.getDefaultProps();
thisprops.light = false; // We will calculate our own lighting
material.props = thisprops;
material.shadows = false;
material.receiveShadows = true;
fwd();
});
});
});
prim.addTangents();
prim.addTangents();
} else {
material.shadows = false;
material.receiveShadows = true;
}
} else {
Console.warn('Unable to load ${grp} texture for dif ${path}');
material = Material.create();
material.shadows = false;
material.receiveShadows = true;
}
} else {
Console.warn('Unable to load ${grp} texture for dif ${path}');
material = Material.create();
material.shadows = false;
material.receiveShadows = true;
// material.mainPass.addShader(new h3d.shader.pbr.PropsValues(1, 0, 0, 1));
if (Debug.wireFrame)
material.mainPass.wireframe = true;
materials.push(material);
}
// material.mainPass.addShader(new h3d.shader.pbr.PropsValues(1, 0, 0, 1));
if (Debug.wireFrame)
material.mainPass.wireframe = true;
materials.push(material);
}
prim.endPrimitive();

View file

@ -9,7 +9,7 @@ class Polygon extends MeshPrimitive {
public var normals:Array<Float>;
public var tangents:Array<Float>;
public var uvs:Array<Float>;
public var idx:hxd.IndexBuffer;
public var indexStarts:Array<Int>;
public var indexCounts:Array<Int>;
@ -18,10 +18,9 @@ class Polygon extends MeshPrimitive {
var bounds:h3d.col.Bounds;
public function new(?idx) {
public function new() {
this.indexStarts = [0];
this.indexCounts = [];
this.idx = idx;
this.points = [];
this.uvs = [];
this.normals = [];
@ -121,8 +120,7 @@ class Polygon extends MeshPrimitive {
}
}
var flags:Array<h3d.Buffer.BufferFlag> = [];
if (idx == null)
flags.push(Triangles);
flags.push(Triangles);
if (normals == null || tangents != null)
flags.push(RawFormat);
buffer = h3d.Buffer.ofFloats(buf, size, flags);
@ -130,8 +128,13 @@ class Polygon extends MeshPrimitive {
for (i in 0...names.length)
addBuffer(names[i], buffer, positions[i]);
if (idx != null)
indexes = h3d.Indexes.alloc(idx);
if (indexes == null && Std.int(points.length / 3) > 65535) {
var indices = new haxe.io.BytesOutput();
for (i in 0...Std.int(points.length / 3))
indices.writeInt32(i);
indexes = new h3d.Indexes(indices.length >> 2, true);
indexes.uploadBytes(indices.getBytes(), 0, indices.length >> 2);
}
}
public function addTangents() {
@ -141,15 +144,9 @@ class Polygon extends MeshPrimitive {
var pos = 0;
for (i in 0...triCount()) {
var i0, i1, i2;
if (idx == null) {
i0 = pos++;
i1 = pos++;
i2 = pos++;
} else {
i0 = idx[pos++];
i1 = idx[pos++];
i2 = idx[pos++];
}
i0 = pos++;
i1 = pos++;
i2 = pos++;
var p0 = new Vector(points[i0 * 3], points[i0 * 3 + 1], points[i0 * 3 + 2]);
var p1 = new Vector(points[i1 * 3], points[i1 * 3 + 1], points[i1 * 3 + 2]);
var p2 = new Vector(points[i2 * 3], points[i2 * 3 + 1], points[i2 * 3 + 2]);
@ -192,7 +189,7 @@ class Polygon extends MeshPrimitive {
var n = super.triCount();
if (n != 0)
return n;
return Std.int((idx == null ? points.length / 3 : idx.length) / 3);
return Std.int(points.length / 3);
}
override function vertexCount() {
@ -212,9 +209,7 @@ class Polygon extends MeshPrimitive {
alloc(engine);
var bufs = getBuffers(engine);
if (indexes != null)
engine.renderMultiBuffers(bufs, indexes);
else if (buffer.flags.has(Quads))
engine.renderMultiBuffers(bufs, engine.mem.quadIndexes, 0, triCount());
engine.renderMultiBuffers(bufs, indexes, indexStarts[currentMaterial], indexCounts[currentMaterial]);
else
engine.renderMultiBuffers(bufs, engine.mem.triIndexes, indexStarts[currentMaterial], indexCounts[currentMaterial]);
}