Skip to content

Instantly share code, notes, and snippets.

@hYdos
Created August 9, 2024 15:57
Show Gist options
  • Save hYdos/96d0e56229cbe9f6dc54d7a83e3312bf to your computer and use it in GitHub Desktop.
Save hYdos/96d0e56229cbe9f6dc54d7a83e3312bf to your computer and use it in GitHub Desktop.
e
// TODO: for model replacement just delete the old elements and make a new one
var element0 = drawCalls[0];
var vertexBuffer = element0._graphicsVertexBuffer._vertexBuffer;
var indexBuffer = element0._graphicsIndexBuffer._indexBuffer;
var replacementModel = new Model("C:/Users/hydos/Desktop/skibidiToilet/tarclop.gltf");
var mesh = replacementModel.Meshes[0]; // TODO: in future we will support > 1 mesh. for now just do 1 mesh
using var newVertexBytes = new MemoryStream();
using (var writer = new EndianAwareBinaryWriter(newVertexBytes, Endianness.BigEndian)) {
foreach (var vert in mesh.vertices) {
writer.Write(vert.pos.X);
writer.Write(vert.pos.Y);
writer.Write(vert.pos.Z);
writer.Write((Half) vert.uv.X);
writer.Write((Half) vert.uv.Y);
}
}
using var newIndexBytes = new MemoryStream();
using (var writer = new EndianAwareBinaryWriter(newIndexBytes, Endianness.BigEndian)) {
foreach (var idx in mesh.indices) {
writer.Write((ushort) idx);
}
}
vertexBuffer._format._vertexSize = 4 * 3 + 4 * 2 ; // float3(pos) + float2(uv)
vertexBuffer._format._enableSoftwareBlending = false; // this requires an extra vertex format. nuh-uh
vertexBuffer._format._elements.Realloc(2);
vertexBuffer._format._elements[0] = new igVertexElementMetaField {
_count = 0,
_freq = 0,
_offset = 0,
_stream = 0,
_type = 3,
_usage = 0,
_usageIndex = 0,
_mapToElement = 0,
_packDataOffset = 0,
_packTypeAndFracHint = 0
};
vertexBuffer._format._elements[1] = new igVertexElementMetaField {
_count = 0,
_freq = 0,
_offset = 12,
_stream = 0,
_type = 42,
_usage = 5,
_usageIndex = 0,
_mapToElement = 0,
_packDataOffset = 0,
_packTypeAndFracHint = 0
};
indexBuffer._vertexFormat._vertexSize = 4 * 3 + 4 * 2 ; // float3(pos) + float2(uv)
indexBuffer._vertexFormat._enableSoftwareBlending = false; // this requires an extra vertex format. nuh-uh
indexBuffer._vertexFormat._elements.Realloc(2);
indexBuffer._vertexFormat._elements[0] = new igVertexElementMetaField {
_count = 0,
_freq = 0,
_offset = 0,
_stream = 0,
_type = 3,
_usage = 0,
_usageIndex = 0,
_mapToElement = 0,
_packDataOffset = 0,
_packTypeAndFracHint = 0
};
indexBuffer._vertexFormat._elements[1] = new igVertexElementMetaField {
_count = 0,
_freq = 0,
_offset = 12,
_stream = 0,
_type = 42,
_usage = 5,
_usageIndex = 0,
_mapToElement = 0,
_packDataOffset = 0,
_packTypeAndFracHint = 0
};
var newData = newVertexBytes.ToArray();
vertexBuffer._data.Realloc(newData.Length);
for (var i = 0; i < newData.Length; i++) vertexBuffer._data[i] = newData[i];
vertexBuffer._vertexCount = (uint)mesh.vertices.Count;
vertexBuffer._vertexCountArray[0] = (uint)mesh.vertices.Count;
newData = newIndexBytes.ToArray();
indexBuffer._data.Realloc(newData.Length);
for (var i = 0; i < newData.Length; i++) indexBuffer._data[i] = newData[i];
indexBuffer._indexCount = (uint)mesh.indices.Count;
indexBuffer._indexCountArray[0] = (uint)mesh.indices.Count;
Save(directory);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment