Mesh _AssembleStandardMesh(ShapeStream shapeStream, Mesh mesh) { try { if (mesh == null) mesh = new Mesh(); shapeStream.CheckGuard(); mesh._numFrames = shapeStream.ReadS32(); mesh._numMatFrames = shapeStream.ReadS32(); mesh._parentMesh = shapeStream.ReadS32(); shapeStream.ReadPoint3F(ref mesh._bounds.Min); shapeStream.ReadPoint3F(ref mesh._bounds.Max); shapeStream.ReadPoint3F(ref mesh._center); mesh._radius = shapeStream.ReadF32(); int numVerts = shapeStream.ReadS32(); mesh._verts = mesh._parentMesh < 0 ? shapeStream.ReadPoint3Fs(numVerts) : _shape.Meshes[mesh._parentMesh]._verts; int numTVerts = shapeStream.ReadS32(); mesh._tverts = mesh._parentMesh < 0 ? shapeStream.ReadPoint2Fs(numTVerts) : _shape.Meshes[mesh._parentMesh]._tverts; // Version 26 added vertex color and texcoord2. if (Shape.ReadVersion > 25) { int numTVerts2 = shapeStream.ReadS32(); if (numTVerts2 > 0) { if (mesh._parentMesh < 0) mesh._tverts2 = shapeStream.ReadPoint2Fs(numTVerts2); else mesh._tverts2 = _shape.Meshes[mesh._parentMesh]._tverts2; } int numVColors = shapeStream.ReadS32(); if (numVColors > 0) { if (mesh._parentMesh < 0) mesh._colors = shapeStream.ReadColors(numVColors); else mesh._colors = _shape.Meshes[mesh._parentMesh]._colors; } } mesh._norms = mesh._parentMesh < 0 ? shapeStream.ReadPoint3Fs(numVerts) : _shape.Meshes[mesh._parentMesh]._norms; if (mesh._parentMesh < 0) shapeStream.ReadU8s(numVerts); // Read past encoded normals int szPrim; ushort[] prim16; int[] prim32; int szInd; short[] ind16; // Version 26 has 32-bit indices. // We don't, so read them into our 16-bit buffers. if (Shape.ReadVersion > 25) { szPrim = shapeStream.ReadS32(); int[] primIn = shapeStream.ReadS32s(szPrim * 3); szInd = shapeStream.ReadS32(); int[] indIn = shapeStream.ReadS32s(szInd); // prim16 = new ushort[szPrim * 2]; prim32 = new int[szPrim]; int j = 0; for (int i = 0; i < szPrim; i += 3) { prim16[j] = (ushort)primIn[i]; prim16[j + 1] = (ushort)primIn[i + 1]; prim32[j] = primIn[i + 2]; j++; } // Copy down the array of indices from 32-bit to 16. ind16 = new short[szInd]; for (int i = 0; i < szInd; i++) ind16[i] = (short)indIn[i]; } else { // Copy the _primitives and _indices...how we do this depends on what // form we want them in when copied...first just get the data szPrim = shapeStream.ReadS32(); prim16 = shapeStream.ReadU16s(szPrim * 2); // start, numElements prim32 = shapeStream.ReadS32s(szPrim); // matIndex szInd = shapeStream.ReadS32(); ind16 = shapeStream.ReadS16s(szInd); } // count then Copy... int cpyPrim, cpyInd; if (_useTriangles) _ConvertToTris(prim16, prim32, ind16, szPrim, out cpyPrim, out cpyInd, null, null); else if (_useOneStrip) _ConvertToSingleStrip(prim16, prim32, ind16, szPrim, out cpyPrim, out cpyInd, null, null); else _LeaveAsMultipleStrips(prim16, prim32, ind16, szPrim, out cpyPrim, out cpyInd, null, null); mesh._primitives = new DrawPrimitive[cpyPrim]; mesh._indices = new short[cpyInd]; // _primitives and _indices counted and allocated above, now Copy them in int chkPrim; int chkInd; if (_useTriangles) _ConvertToTris(prim16, prim32, ind16, szPrim, out chkPrim, out chkInd, mesh._primitives, mesh._indices); else if (_useOneStrip) _ConvertToSingleStrip(prim16, prim32, ind16, szPrim, out chkPrim, out chkInd, mesh._primitives, mesh._indices); else _LeaveAsMultipleStrips(prim16, prim32, ind16, szPrim, out chkPrim, out chkInd, mesh._primitives, mesh._indices); Assert.Fatal(chkPrim == cpyPrim && chkInd == cpyInd, "TSShapeReader._AssembleStandardMesh - DrawPrimitive conversion error."); // Read in merge _indices...deprecated int numMerge = shapeStream.ReadS32(); shapeStream.ReadS16s(numMerge); mesh._vertsPerFrame = shapeStream.ReadS32(); mesh.SetFlags((Mesh.MeshEnum)shapeStream.ReadU32()); shapeStream.CheckGuard(); } catch { } return mesh; }
Mesh _AssembleSkinMesh(ShapeStream shapeStream) { SkinMesh mesh = new SkinMesh(); _AssembleStandardMesh(shapeStream, mesh); int numVerts = shapeStream.ReadS32(); if (mesh._parentMesh < 0) { mesh._initialVerts = shapeStream.ReadPoint3Fs(numVerts); mesh._initialNormals = shapeStream.ReadPoint3Fs(numVerts); // eat up the encoded normals if (mesh._parentMesh < 0) shapeStream.ReadU8s(numVerts); int sz = shapeStream.ReadS32(); mesh._initialTransforms = new Matrix[sz]; for (int i = 0; i < sz; i++) { float[] mat = shapeStream.ReadF32s(16); mesh._initialTransforms[i].M11 = mat[0]; mesh._initialTransforms[i].M21 = mat[1]; mesh._initialTransforms[i].M31 = mat[2]; mesh._initialTransforms[i].M41 = mat[3]; mesh._initialTransforms[i].M12 = mat[4]; mesh._initialTransforms[i].M22 = mat[5]; mesh._initialTransforms[i].M32 = mat[6]; mesh._initialTransforms[i].M42 = mat[7]; mesh._initialTransforms[i].M13 = mat[8]; mesh._initialTransforms[i].M23 = mat[9]; mesh._initialTransforms[i].M33 = mat[10]; mesh._initialTransforms[i].M43 = mat[11]; mesh._initialTransforms[i].M14 = mat[12]; mesh._initialTransforms[i].M24 = mat[13]; mesh._initialTransforms[i].M34 = mat[14]; mesh._initialTransforms[i].M44 = mat[15]; } sz = shapeStream.ReadS32(); mesh._vertexIndex = shapeStream.ReadS32s(sz); mesh._boneIndex = shapeStream.ReadU32s(sz); mesh._weight = shapeStream.ReadF32s(sz); sz = shapeStream.ReadS32(); mesh._nodeIndex = shapeStream.ReadS32s(sz); } else { SkinMesh otherSkin = _shape.Meshes[mesh._parentMesh] as SkinMesh; if (otherSkin == null) { Assert.Fatal(false, "TSShapeReader._AssembleSkinMesh - Parent of skin mesh not a skin mesh."); return null; } // consult our parent for our vectors mesh._initialVerts = otherSkin._initialVerts; mesh._initialNormals = otherSkin._initialNormals; // eat up the encoded normals if (mesh._parentMesh < 0) shapeStream.ReadU8s(numVerts); int sz = shapeStream.ReadS32(); mesh._initialTransforms = otherSkin._initialTransforms; sz = shapeStream.ReadS32(); mesh._vertexIndex = otherSkin._vertexIndex; mesh._boneIndex = otherSkin._boneIndex; mesh._weight = otherSkin._weight; sz = shapeStream.ReadS32(); mesh._nodeIndex = otherSkin._nodeIndex; } shapeStream.CheckGuard(); return mesh; }