c# - Cutting a XNA model in half by removing vertices and indices -
i attempting cut loaded 3d model in half using monogame (which extremely similar xna). not need in real time performance not huge issue.
i vertices , indices using modelmeshpart such.
vector3[] vertices = new vector3[part.numvertices]; part.vertexbuffer.getdata<vector3>(vertices); short[] indices = new short[part.primitivecount * 3]; part.indexbuffer.getdata<short>(indices); and set them using
part.indexbuffer.setdata<vector3>(vertices); part.vertexbuffer.setdata<short>(indices); prior though take arrays , try empty out vertices (and indices refer them) positioned behind center z location of model such.
float centerz = modelmesh.boundingsphere.center.z; (int = 0; < indices.length; += 3) { short index0 = indices[i]; short index1 = indices[i + 1]; short index2 = indices[i + 2]; vector3 vert0 = vertices[index0]; vector3 vert1 = vertices[index1]; vector3 vert2 = vertices[index2]; if (vert0.z > centerz && vert1.z > centerz && vert2.z > centerz) { vert0 = vector3.zero; vert1 = vector3.zero; vert2 = vector3.zero; indices[i] = short.minvalue; indices[i + 1] = short.minvalue; indices[i + 2] = short.minvalue; } } but in end looks rather model cut in half. new games programming , comprehension of vertices , indices still extremely poor. missing fundamental, sincerely appreciated.

short.minvalue -32767 since short signed, shouldn't using that, since indice value vertice can't negative. should use = 0 or ushort.minvalue. keep in mind method not cut model in half perfectly, triangles perpendicular center still remain.
you can try this:
if (vert0.z <= centerz || vert1.z <= centerz || vert2.z <= centerz) { if (vert0.z > centerz) vert0.z = centerz; if (vert1.z > centerz) vert1.z = centerz; if (vert2.z > centerz) vert2.z = centerz; } else { indices[i] = 0; indices[i + 1] = 0; indices[i + 2] = 0; } edit: part looks wrong:
part.indexbuffer.setdata<short>(vertices); part.vertexbuffer.setdata<vector3>(indices); you should pass vertices vertexbuffer , indices indexbuffer. , should use ushort on indices, since theres no reason indice negative.
Comments
Post a Comment