Skip to content

Instantly share code, notes, and snippets.

/-

Created April 28, 2017 12:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/f15f99e8dac5de8ca37413b5969e522b to your computer and use it in GitHub Desktop.
Save anonymous/f15f99e8dac5de8ca37413b5969e522b to your computer and use it in GitHub Desktop.
Array<uint8> ModelGetWeights(Geometry@ geo, int index)
{
Array<uint8> ret;
for (int i = 0; i < geo.numVertexBuffers; i++) {
VertexBuffer@ vb = geo.vertexBuffers[i];
VectorBuffer vvb = vb.GetData();
int pos = index * vb.vertexSize;
for (int j = 0; j < vb.elements.length; j++) {
if (vb.elements[j].semantic == SEM_BLENDINDICES) {
vvb.Seek(pos + vb.elements[j].offset);
ret.Push(vvb.ReadUByte());
ret.Push(vvb.ReadUByte());
ret.Push(vvb.ReadUByte());
ret.Push(vvb.ReadUByte());
break;
}
}
}
return ret;
}
uint GetIndex(IndexBuffer@ ib, int index)
{
VectorBuffer ivb = ib.GetData();
ivb.Seek(index * ib.indexSize);
uint ret;
switch (ib.indexSize) {
case 1:
ret = ivb.ReadUByte();
break;
case 2:
ret = ivb.ReadUShort();
break;
case 4:
ret = ivb.ReadUInt();
break;
default: /* Any other sizes? */
ret = 0;
}
return ret;
}
Array<uint8> ModelGetWeightsIndex(Model@ model, int geo, int lod, int index)
{
Array<uint8> ret;
Geometry@ geom = model.GetGeometry(geo, lod);
IndexBuffer@ ib = geom.indexBuffer;
uint point = GetIndex(ib, index);
return ModelGetWeights(geom, point);
}
void ModelRebuildIB(Model@ model, int minidx, int maxidx)
{
for (int i = 0; i < model.numGeometries; i++) {
for (int j = 0; j < model.numGeometryLodLevels[i]; j++) {
Geometry@ geo = model.GetGeometry(i, j);
IndexBuffer@ ib = geo.indexBuffer;
IndexBuffer@ ibnew = IndexBuffer();
ibnew.shadowed = true;
VectorBuffer ivb = ib.GetData();
VectorBuffer ivbnew;
int count = 0;
if (geo.primitiveType != TRIANGLE_LIST)
continue;
Print("fixing index count: " + ib.indexCount);
Print("fixing index size: " + ib.indexSize);
for (int l = 0; l < ib.indexCount; l++) {
Array<uint8> indices = ModelGetWeightsIndex(model, i, j, l);
bool skip = false;
for (int k = 0; k < indices.length; k++)
if (indices[k] < minidx || indices[k] > maxidx)
skip = true;
uint index = GetIndex(ib, l);
if (!skip) {
Print("index: " + index);
ivbnew.WriteUInt(index);
count++;
}
}
Print("IB count:" + count);
ibnew.SetSize(count, true);
ibnew.SetData(ivbnew);
geo.SetIndexBuffer(ibnew);
model.SetGeometry(i, j, geo);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment