mirror of
https://github.com/RandomityGuy/MBHaxe.git
synced 2026-04-26 04:31:40 +00:00
fix > 65k verts
This commit is contained in:
parent
ef4d9b4861
commit
c033bb17e7
2 changed files with 98 additions and 89 deletions
|
|
@ -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) {
|
for (index => value in triangles) {
|
||||||
if (mats.exists(value.texture)) {
|
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 {
|
} else {
|
||||||
mats.set(value.texture, [value]);
|
mats.set(value.texture, [[value]]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
collider.finalize();
|
collider.finalize();
|
||||||
|
|
@ -674,81 +680,89 @@ class DifBuilder {
|
||||||
});
|
});
|
||||||
|
|
||||||
var prim = new Polygon();
|
var prim = new Polygon();
|
||||||
|
|
||||||
var materials = [];
|
var materials = [];
|
||||||
|
|
||||||
for (grp => tris in mats) {
|
for (grp => trigroup in mats) {
|
||||||
var points = [];
|
for (tris in trigroup) {
|
||||||
var normals = [];
|
var points = [];
|
||||||
var uvs = [];
|
var normals = [];
|
||||||
for (tri in tris) {
|
var uvs = [];
|
||||||
var p1 = new Point(-tri.p1.x, tri.p1.y, tri.p1.z);
|
for (tri in tris) {
|
||||||
var p2 = new Point(-tri.p2.x, tri.p2.y, tri.p2.z);
|
var p1 = new Point(-tri.p1.x, tri.p1.y, tri.p1.z);
|
||||||
var p3 = new Point(-tri.p3.x, tri.p3.y, tri.p3.z);
|
var p2 = new Point(-tri.p2.x, tri.p2.y, tri.p2.z);
|
||||||
var n1 = new Point(-tri.normal1.x, tri.normal1.y, tri.normal1.z);
|
var p3 = new Point(-tri.p3.x, tri.p3.y, tri.p3.z);
|
||||||
var n2 = new Point(-tri.normal2.x, tri.normal2.y, tri.normal2.z);
|
var n1 = new Point(-tri.normal1.x, tri.normal1.y, tri.normal1.z);
|
||||||
var n3 = new Point(-tri.normal3.x, tri.normal3.y, tri.normal3.z);
|
var n2 = new Point(-tri.normal2.x, tri.normal2.y, tri.normal2.z);
|
||||||
var uv1 = new UV(tri.uv1.x, tri.uv1.y);
|
var n3 = new Point(-tri.normal3.x, tri.normal3.y, tri.normal3.z);
|
||||||
var uv2 = new UV(tri.uv2.x, tri.uv2.y);
|
var uv1 = new UV(tri.uv1.x, tri.uv1.y);
|
||||||
var uv3 = new UV(tri.uv3.x, tri.uv3.y);
|
var uv2 = new UV(tri.uv2.x, tri.uv2.y);
|
||||||
points.push(p3);
|
var uv3 = new UV(tri.uv3.x, tri.uv3.y);
|
||||||
points.push(p2);
|
points.push(p3);
|
||||||
points.push(p1);
|
points.push(p2);
|
||||||
normals.push(n3);
|
points.push(p1);
|
||||||
normals.push(n2);
|
normals.push(n3);
|
||||||
normals.push(n1);
|
normals.push(n2);
|
||||||
uvs.push(uv3);
|
normals.push(n1);
|
||||||
uvs.push(uv2);
|
uvs.push(uv3);
|
||||||
uvs.push(uv1);
|
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");
|
|
||||||
}
|
}
|
||||||
if (shaderMaterialDict.exists(matDictName)) {
|
|
||||||
var retrievefunc = shaderMaterialDict[matDictName];
|
if (prim.vertexCount() + points.length > 65535) {
|
||||||
shaderWorker.addTask(fwd -> {
|
prim.endPrimitive();
|
||||||
retrievefunc(shad -> {
|
var mesh = new MultiMaterial(prim, materials, itr);
|
||||||
material.mainPass.removeShader(material.textureShader);
|
materials = [];
|
||||||
material.mainPass.addShader(shad);
|
prim = new Polygon();
|
||||||
var thisprops:Dynamic = material.getDefaultProps();
|
}
|
||||||
thisprops.light = false; // We will calculate our own lighting
|
|
||||||
material.props = thisprops;
|
prim.addPoints(points);
|
||||||
material.shadows = false;
|
prim.addUVs(uvs);
|
||||||
material.receiveShadows = true;
|
prim.addNormals(normals);
|
||||||
fwd();
|
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 {
|
} else {
|
||||||
|
Console.warn('Unable to load ${grp} texture for dif ${path}');
|
||||||
|
material = Material.create();
|
||||||
material.shadows = false;
|
material.shadows = false;
|
||||||
material.receiveShadows = true;
|
material.receiveShadows = true;
|
||||||
}
|
}
|
||||||
} else {
|
// material.mainPass.addShader(new h3d.shader.pbr.PropsValues(1, 0, 0, 1));
|
||||||
Console.warn('Unable to load ${grp} texture for dif ${path}');
|
if (Debug.wireFrame)
|
||||||
material = Material.create();
|
material.mainPass.wireframe = true;
|
||||||
material.shadows = false;
|
materials.push(material);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
prim.endPrimitive();
|
prim.endPrimitive();
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ class Polygon extends MeshPrimitive {
|
||||||
public var normals:Array<Float>;
|
public var normals:Array<Float>;
|
||||||
public var tangents:Array<Float>;
|
public var tangents:Array<Float>;
|
||||||
public var uvs:Array<Float>;
|
public var uvs:Array<Float>;
|
||||||
public var idx:hxd.IndexBuffer;
|
|
||||||
public var indexStarts:Array<Int>;
|
public var indexStarts:Array<Int>;
|
||||||
public var indexCounts:Array<Int>;
|
public var indexCounts:Array<Int>;
|
||||||
|
|
||||||
|
|
@ -18,10 +18,9 @@ class Polygon extends MeshPrimitive {
|
||||||
|
|
||||||
var bounds:h3d.col.Bounds;
|
var bounds:h3d.col.Bounds;
|
||||||
|
|
||||||
public function new(?idx) {
|
public function new() {
|
||||||
this.indexStarts = [0];
|
this.indexStarts = [0];
|
||||||
this.indexCounts = [];
|
this.indexCounts = [];
|
||||||
this.idx = idx;
|
|
||||||
this.points = [];
|
this.points = [];
|
||||||
this.uvs = [];
|
this.uvs = [];
|
||||||
this.normals = [];
|
this.normals = [];
|
||||||
|
|
@ -121,8 +120,7 @@ class Polygon extends MeshPrimitive {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
var flags:Array<h3d.Buffer.BufferFlag> = [];
|
var flags:Array<h3d.Buffer.BufferFlag> = [];
|
||||||
if (idx == null)
|
flags.push(Triangles);
|
||||||
flags.push(Triangles);
|
|
||||||
if (normals == null || tangents != null)
|
if (normals == null || tangents != null)
|
||||||
flags.push(RawFormat);
|
flags.push(RawFormat);
|
||||||
buffer = h3d.Buffer.ofFloats(buf, size, flags);
|
buffer = h3d.Buffer.ofFloats(buf, size, flags);
|
||||||
|
|
@ -130,8 +128,13 @@ class Polygon extends MeshPrimitive {
|
||||||
for (i in 0...names.length)
|
for (i in 0...names.length)
|
||||||
addBuffer(names[i], buffer, positions[i]);
|
addBuffer(names[i], buffer, positions[i]);
|
||||||
|
|
||||||
if (idx != null)
|
if (indexes == null && Std.int(points.length / 3) > 65535) {
|
||||||
indexes = h3d.Indexes.alloc(idx);
|
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() {
|
public function addTangents() {
|
||||||
|
|
@ -141,15 +144,9 @@ class Polygon extends MeshPrimitive {
|
||||||
var pos = 0;
|
var pos = 0;
|
||||||
for (i in 0...triCount()) {
|
for (i in 0...triCount()) {
|
||||||
var i0, i1, i2;
|
var i0, i1, i2;
|
||||||
if (idx == null) {
|
i0 = pos++;
|
||||||
i0 = pos++;
|
i1 = pos++;
|
||||||
i1 = pos++;
|
i2 = pos++;
|
||||||
i2 = pos++;
|
|
||||||
} else {
|
|
||||||
i0 = idx[pos++];
|
|
||||||
i1 = idx[pos++];
|
|
||||||
i2 = idx[pos++];
|
|
||||||
}
|
|
||||||
var p0 = new Vector(points[i0 * 3], points[i0 * 3 + 1], points[i0 * 3 + 2]);
|
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 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]);
|
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();
|
var n = super.triCount();
|
||||||
if (n != 0)
|
if (n != 0)
|
||||||
return n;
|
return n;
|
||||||
return Std.int((idx == null ? points.length / 3 : idx.length) / 3);
|
return Std.int(points.length / 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
override function vertexCount() {
|
override function vertexCount() {
|
||||||
|
|
@ -212,9 +209,7 @@ class Polygon extends MeshPrimitive {
|
||||||
alloc(engine);
|
alloc(engine);
|
||||||
var bufs = getBuffers(engine);
|
var bufs = getBuffers(engine);
|
||||||
if (indexes != null)
|
if (indexes != null)
|
||||||
engine.renderMultiBuffers(bufs, indexes);
|
engine.renderMultiBuffers(bufs, indexes, indexStarts[currentMaterial], indexCounts[currentMaterial]);
|
||||||
else if (buffer.flags.has(Quads))
|
|
||||||
engine.renderMultiBuffers(bufs, engine.mem.quadIndexes, 0, triCount());
|
|
||||||
else
|
else
|
||||||
engine.renderMultiBuffers(bufs, engine.mem.triIndexes, indexStarts[currentMaterial], indexCounts[currentMaterial]);
|
engine.renderMultiBuffers(bufs, engine.mem.triIndexes, indexStarts[currentMaterial], indexCounts[currentMaterial]);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue