public byte[] GetBytes(ChunkType type) { List <byte> result = new List <byte>(); int ind = Indexes.Length; if (Reversed) { ind = -ind; } result.AddRange(ByteConverter.GetBytes((short)(ind))); for (int i = 0; i < Indexes.Length; i++) { result.AddRange(ByteConverter.GetBytes(Indexes[i])); switch (type) { case ChunkType.Strip_StripUVN: case ChunkType.Strip_StripUVNColor: case ChunkType.Strip_StripUVN2: result.AddRange(UVs[i].GetBytes(false)); break; case ChunkType.Strip_StripUVH: case ChunkType.Strip_StripUVHColor: case ChunkType.Strip_StripUVH2: result.AddRange(UVs[i].GetBytes(true)); break; } switch (type) { case ChunkType.Strip_StripColor: case ChunkType.Strip_StripUVNColor: case ChunkType.Strip_StripUVHColor: result.AddRange(VColor.GetBytes(VColors[i], ColorType.ARGB8888_16)); break; } if (i > 1) { if (UserFlags1 != null) { result.AddRange(ByteConverter.GetBytes(UserFlags1[i - 2])); if (UserFlags2 != null) { result.AddRange(ByteConverter.GetBytes(UserFlags2[i - 2])); if (UserFlags3 != null) { result.AddRange(ByteConverter.GetBytes(UserFlags3[i - 2])); } } } } } return(result.ToArray()); }
public override byte[] GetBytes() { byte t = (byte)ChunkType.Material; Size = 0; if (Diffuse.HasValue) { t |= 1; Size += 2; } if (Ambient.HasValue) { t |= 2; Size += 2; } if (Specular.HasValue) { t |= 4; Size += 2; } if (Second) { t |= 8; } Type = (ChunkType)t; List <byte> result = new List <byte>(base.GetBytes()); if (Diffuse.HasValue) { result.AddRange(VColor.GetBytes(Diffuse.Value, ColorType.ARGB8888_16)); } if (Ambient.HasValue) { result.AddRange(VColor.GetBytes(Ambient.Value, ColorType.XRGB8888_16)); } if (Specular.HasValue) { int i = Specular.Value.ToArgb(); result.AddRange(ByteConverter.GetBytes((ushort)(i & 0xFFFF))); result.AddRange(ByteConverter.GetBytes((ushort)(((i >> 16) & 0xFF) | SpecularExponent << 8))); } return(result.ToArray()); }
public override byte[] GetBytes(uint imageBase, bool DX, Dictionary <string, uint> labels, List <uint> njOffsets, out uint address) { List <byte> result = new List <byte>(); uint materialAddress = 0; if (Material != null && Material.Count > 0) { if (labels.ContainsKey(MaterialName)) { materialAddress = labels[MaterialName]; } else { materialAddress = imageBase; labels.Add(MaterialName, materialAddress); foreach (NJS_MATERIAL item in Material) { result.AddRange(item.GetBytes()); } } } uint meshAddress; if (labels.ContainsKey(MeshName)) { meshAddress = labels[MeshName]; } else { uint[] polyAddrs = new uint[Mesh.Count]; uint[] polyNormalAddrs = new uint[Mesh.Count]; uint[] vColorAddrs = new uint[Mesh.Count]; uint[] uVAddrs = new uint[Mesh.Count]; for (int i = 0; i < Mesh.Count; i++) { if (labels.ContainsKey(Mesh[i].PolyName)) { polyAddrs[i] = labels[Mesh[i].PolyName]; } else { result.Align(4); polyAddrs[i] = (uint)result.Count + imageBase; labels.Add(Mesh[i].PolyName, polyAddrs[i]); for (int j = 0; j < Mesh[i].Poly.Count; j++) { result.AddRange(Mesh[i].Poly[j].GetBytes()); } } } for (int i = 0; i < Mesh.Count; i++) { if (Mesh[i].PolyNormal != null && Mesh[i].PolyNormal.Length > 0) { if (labels.ContainsKey(Mesh[i].PolyNormalName)) { polyNormalAddrs[i] = labels[Mesh[i].PolyNormalName]; } else { result.Align(4); polyNormalAddrs[i] = (uint)result.Count + imageBase; labels.Add(Mesh[i].PolyNormalName, polyNormalAddrs[i]); for (int j = 0; j < Mesh[i].PolyNormal.Length; j++) { result.AddRange(Mesh[i].PolyNormal[j].GetBytes()); } } } } for (int i = 0; i < Mesh.Count; i++) { if (Mesh[i].VColor != null && Mesh[i].VColor.Length > 0) { if (labels.ContainsKey(Mesh[i].VColorName)) { vColorAddrs[i] = labels[Mesh[i].VColorName]; } else { result.Align(4); vColorAddrs[i] = (uint)result.Count + imageBase; labels.Add(Mesh[i].VColorName, vColorAddrs[i]); for (int j = 0; j < Mesh[i].VColor.Length; j++) { result.AddRange(VColor.GetBytes(Mesh[i].VColor[j])); } } } } for (int i = 0; i < Mesh.Count; i++) { if (Mesh[i].UV != null && Mesh[i].UV.Length > 0) { if (labels.ContainsKey(Mesh[i].UVName)) { uVAddrs[i] = labels[Mesh[i].UVName]; } else { result.Align(4); uVAddrs[i] = (uint)result.Count + imageBase; labels.Add(Mesh[i].UVName, uVAddrs[i]); for (int j = 0; j < Mesh[i].UV.Length; j++) { result.AddRange(Mesh[i].UV[j].GetBytes()); } } } } result.Align(4); meshAddress = (uint)result.Count + imageBase; labels.Add(MeshName, meshAddress); for (int i = 0; i < Mesh.Count; i++) { //POF0 if (polyAddrs[i] != 0) { njOffsets.Add((uint)(result.Count + imageBase + 0x4)); } if (polyNormalAddrs[i] != 0) { njOffsets.Add((uint)(result.Count + imageBase + 0xC)); } if (vColorAddrs[i] != 0) { njOffsets.Add((uint)(result.Count + imageBase + 0x10)); } if (uVAddrs[i] != 0) { njOffsets.Add((uint)(result.Count + imageBase + 0x14)); } result.AddRange(Mesh[i].GetBytes(polyAddrs[i], polyNormalAddrs[i], vColorAddrs[i], uVAddrs[i], DX)); } } result.Align(4); uint vertexAddress; if (labels.ContainsKey(VertexName)) { vertexAddress = labels[VertexName]; } else { vertexAddress = (uint)result.Count + imageBase; labels.Add(VertexName, vertexAddress); foreach (Vertex item in Vertex) { if (item == null) { result.AddRange(new byte[SAModel.Vertex.Size]); } else { result.AddRange(item.GetBytes()); } } } result.Align(4); uint normalAddress; if (labels.ContainsKey(NormalName)) { normalAddress = labels[NormalName]; } else { normalAddress = (uint)result.Count + imageBase; labels.Add(NormalName, normalAddress); foreach (Vertex item in Normal) { if (item == null) { result.AddRange(new byte[SAModel.Vertex.Size]); } else { result.AddRange(item.GetBytes()); } } } result.Align(4); address = (uint)result.Count; //POF0 if (vertexAddress != 0) { njOffsets.Add((uint)(result.Count + imageBase)); } if (normalAddress != 0) { njOffsets.Add((uint)(result.Count + imageBase + 0x4)); } if (meshAddress != 0) { njOffsets.Add((uint)(result.Count + imageBase + 0xC)); } if (materialAddress != 0) { njOffsets.Add((uint)(result.Count + imageBase + 0x10)); } result.AddRange(ByteConverter.GetBytes(vertexAddress)); result.AddRange(ByteConverter.GetBytes(normalAddress)); result.AddRange(ByteConverter.GetBytes(Vertex.Length)); result.AddRange(ByteConverter.GetBytes(meshAddress)); result.AddRange(ByteConverter.GetBytes(materialAddress)); result.AddRange(ByteConverter.GetBytes((short)Mesh.Count)); result.AddRange(ByteConverter.GetBytes((short)Material.Count)); result.AddRange(Bounds.GetBytes()); if (DX) { result.AddRange(new byte[4]); } labels.Add(Name, address + imageBase); return(result.ToArray()); }
public byte[] GetBytes() { VertexChunk next = null; int vertlimit; int vertcount = Vertices.Count; switch (Type) { case ChunkType.Vertex_VertexSH: vertlimit = 65535 / 4; if (Vertices.Count > vertlimit) { next = new VertexChunk(Type) { Vertices = Vertices.Skip(vertlimit).ToList() }; vertcount = vertlimit; } break; case ChunkType.Vertex_VertexNormalSH: vertlimit = 65535 / 8; if (Vertices.Count > vertlimit) { next = new VertexChunk(Type) { Vertices = Vertices.Skip(vertlimit).ToList(), Normals = Normals.Skip(vertlimit).ToList() }; vertcount = vertlimit; } break; case ChunkType.Vertex_Vertex: vertlimit = 65535 / 3; if (Vertices.Count > vertlimit) { next = new VertexChunk(Type) { Vertices = Vertices.Skip(vertlimit).ToList() }; vertcount = vertlimit; } break; case ChunkType.Vertex_VertexDiffuse8: vertlimit = 65535 / 4; if (Vertices.Count > vertlimit) { next = new VertexChunk(Type) { Vertices = Vertices.Skip(vertlimit).ToList(), Diffuse = Diffuse.Skip(vertlimit).ToList() }; vertcount = vertlimit; } break; case ChunkType.Vertex_VertexUserFlags: vertlimit = 65535 / 4; if (Vertices.Count > vertlimit) { next = new VertexChunk(Type) { Vertices = Vertices.Skip(vertlimit).ToList(), UserFlags = UserFlags.Skip(vertlimit).ToList() }; vertcount = vertlimit; } break; case ChunkType.Vertex_VertexNinjaFlags: vertlimit = 65535 / 4; if (Vertices.Count > vertlimit) { next = new VertexChunk(Type) { Vertices = Vertices.Skip(vertlimit).ToList(), NinjaFlags = NinjaFlags.Skip(vertlimit).ToList() }; vertcount = vertlimit; } break; case ChunkType.Vertex_VertexDiffuseSpecular5: case ChunkType.Vertex_VertexDiffuseSpecular4: vertlimit = 65535 / 4; if (Vertices.Count > vertlimit) { next = new VertexChunk(Type) { Vertices = Vertices.Skip(vertlimit).ToList(), Diffuse = Diffuse.Skip(vertlimit).ToList(), Specular = Specular.Skip(vertlimit).ToList() }; vertcount = vertlimit; } break; case ChunkType.Vertex_VertexNormal: vertlimit = 65535 / 6; if (Vertices.Count > vertlimit) { next = new VertexChunk(Type) { Vertices = Vertices.Skip(vertlimit).ToList(), Normals = Normals.Skip(vertlimit).ToList() }; vertcount = vertlimit; } break; case ChunkType.Vertex_VertexNormalDiffuse8: vertlimit = 65535 / 7; if (Vertices.Count > vertlimit) { next = new VertexChunk(Type) { Vertices = Vertices.Skip(vertlimit).ToList(), Normals = Normals.Skip(vertlimit).ToList(), Diffuse = Diffuse.Skip(vertlimit).ToList() }; vertcount = vertlimit; } break; case ChunkType.Vertex_VertexNormalUserFlags: vertlimit = 65535 / 7; if (Vertices.Count > vertlimit) { next = new VertexChunk(Type) { Vertices = Vertices.Skip(vertlimit).ToList(), Normals = Normals.Skip(vertlimit).ToList(), UserFlags = UserFlags.Skip(vertlimit).ToList() }; vertcount = vertlimit; } break; case ChunkType.Vertex_VertexNormalNinjaFlags: vertlimit = 65535 / 7; if (Vertices.Count > vertlimit) { next = new VertexChunk(Type) { Vertices = Vertices.Skip(vertlimit).ToList(), Normals = Normals.Skip(vertlimit).ToList(), NinjaFlags = NinjaFlags.Skip(vertlimit).ToList() }; vertcount = vertlimit; } break; case ChunkType.Vertex_VertexNormalDiffuseSpecular5: case ChunkType.Vertex_VertexNormalDiffuseSpecular4: vertlimit = 65535 / 7; if (Vertices.Count > vertlimit) { next = new VertexChunk(Type) { Vertices = Vertices.Skip(vertlimit).ToList(), Normals = Normals.Skip(vertlimit).ToList(), Diffuse = Diffuse.Skip(vertlimit).ToList(), Specular = Specular.Skip(vertlimit).ToList() }; vertcount = vertlimit; } break; case ChunkType.End: break; default: throw new NotSupportedException("Unsupported chunk type " + Type + "."); } SetVertCount(vertcount); switch (Type) { case ChunkType.Vertex_Vertex: Size = (ushort)(vertcount * 3 + 1); break; case ChunkType.Vertex_VertexSH: case ChunkType.Vertex_VertexDiffuse8: case ChunkType.Vertex_VertexUserFlags: case ChunkType.Vertex_VertexNinjaFlags: case ChunkType.Vertex_VertexDiffuseSpecular5: case ChunkType.Vertex_VertexDiffuseSpecular4: Size = (ushort)(vertcount * 4 + 1); break; case ChunkType.Vertex_VertexNormal: Size = (ushort)(vertcount * 6 + 1); break; case ChunkType.Vertex_VertexNormalDiffuse8: case ChunkType.Vertex_VertexNormalUserFlags: case ChunkType.Vertex_VertexNormalNinjaFlags: case ChunkType.Vertex_VertexNormalDiffuseSpecular5: case ChunkType.Vertex_VertexNormalDiffuseSpecular4: Size = (ushort)(vertcount * 7 + 1); break; case ChunkType.Vertex_VertexNormalSH: Size = (ushort)(vertcount * 8 + 1); break; } List <byte> result = new List <byte>((Size * 4) + 4); result.AddRange(ByteConverter.GetBytes(Header1)); result.AddRange(ByteConverter.GetBytes(Header2)); for (int i = 0; i < vertcount; i++) { switch (Type) { case ChunkType.Vertex_VertexSH: result.AddRange(Vertices[i].GetBytes()); result.AddRange(ByteConverter.GetBytes(1.0f)); break; case ChunkType.Vertex_VertexNormalSH: result.AddRange(Vertices[i].GetBytes()); result.AddRange(ByteConverter.GetBytes(1.0f)); result.AddRange(Normals[i].GetBytes()); result.AddRange(ByteConverter.GetBytes(1.0f)); break; case ChunkType.Vertex_Vertex: result.AddRange(Vertices[i].GetBytes()); break; case ChunkType.Vertex_VertexDiffuse8: result.AddRange(Vertices[i].GetBytes()); result.AddRange(VColor.GetBytes(Diffuse[i], ColorType.ARGB8888_32)); break; case ChunkType.Vertex_VertexUserFlags: result.AddRange(Vertices[i].GetBytes()); result.AddRange(ByteConverter.GetBytes(UserFlags[i])); break; case ChunkType.Vertex_VertexNinjaFlags: result.AddRange(Vertices[i].GetBytes()); result.AddRange(ByteConverter.GetBytes(NinjaFlags[i])); break; case ChunkType.Vertex_VertexDiffuseSpecular5: result.AddRange(Vertices[i].GetBytes()); result.AddRange(ByteConverter.GetBytes( ByteConverter.ToUInt16(VColor.GetBytes(Diffuse[i], ColorType.RGB565), 0) | (ByteConverter.ToUInt16(VColor.GetBytes(Specular[i], ColorType.RGB565), 0) << 16))); break; case ChunkType.Vertex_VertexDiffuseSpecular4: result.AddRange(Vertices[i].GetBytes()); result.AddRange(ByteConverter.GetBytes( ByteConverter.ToUInt16(VColor.GetBytes(Diffuse[i], ColorType.ARGB4444), 0) | (ByteConverter.ToUInt16(VColor.GetBytes(Specular[i], ColorType.RGB565), 0) << 16))); break; case ChunkType.Vertex_VertexNormal: result.AddRange(Vertices[i].GetBytes()); result.AddRange(Normals[i].GetBytes()); break; case ChunkType.Vertex_VertexNormalDiffuse8: result.AddRange(Vertices[i].GetBytes()); result.AddRange(Normals[i].GetBytes()); result.AddRange(VColor.GetBytes(Diffuse[i], ColorType.ARGB8888_32)); break; case ChunkType.Vertex_VertexNormalUserFlags: result.AddRange(Vertices[i].GetBytes()); result.AddRange(Normals[i].GetBytes()); result.AddRange(ByteConverter.GetBytes(UserFlags[i])); break; case ChunkType.Vertex_VertexNormalNinjaFlags: result.AddRange(Vertices[i].GetBytes()); result.AddRange(Normals[i].GetBytes()); result.AddRange(ByteConverter.GetBytes(NinjaFlags[i])); break; case ChunkType.Vertex_VertexNormalDiffuseSpecular5: result.AddRange(Vertices[i].GetBytes()); result.AddRange(Normals[i].GetBytes()); result.AddRange(ByteConverter.GetBytes( ByteConverter.ToUInt16(VColor.GetBytes(Diffuse[i], ColorType.RGB565), 0) | (ByteConverter.ToUInt16(VColor.GetBytes(Specular[i], ColorType.RGB565), 0) << 16))); break; case ChunkType.Vertex_VertexNormalDiffuseSpecular4: result.AddRange(Vertices[i].GetBytes()); result.AddRange(Normals[i].GetBytes()); result.AddRange(ByteConverter.GetBytes( ByteConverter.ToUInt16(VColor.GetBytes(Diffuse[i], ColorType.ARGB4444), 0) | (ByteConverter.ToUInt16(VColor.GetBytes(Specular[i], ColorType.RGB565), 0) << 16))); break; } } if (next != null) { result.AddRange(next.GetBytes()); } return(result.ToArray()); }