public static PmxMaterial Parse(BinaryReader br, PmxDocument doc) { var rt = new PmxMaterial { Name = doc.Header.Encoding.GetString(br.ReadSizedBuffer()), EnglishName = doc.Header.Encoding.GetString(br.ReadSizedBuffer()), Diffuse = new[] { br.ReadSingle(), br.ReadSingle(), br.ReadSingle(), br.ReadSingle() }, Specular = new[] { br.ReadSingle(), br.ReadSingle(), br.ReadSingle() }, Power = br.ReadSingle(), Ambient = new[] { br.ReadSingle(), br.ReadSingle(), br.ReadSingle() }, Options = (PmxMaterialOptions)br.ReadByte(), EdgeColor = new[] { br.ReadSingle(), br.ReadSingle(), br.ReadSingle(), br.ReadSingle() }, EdgeSize = br.ReadSingle(), MainTexture = doc.ReadIndex(br, PmxIndexKind.Texture), SubTexture = doc.ReadIndex(br, PmxIndexKind.Texture), SubTextureMode = (PmxTextureMode)br.ReadByte(), UseSharedToonTexture = br.ReadBoolean(), }; rt.ToonTexture = rt.UseSharedToonTexture ? br.ReadByte() : doc.ReadIndex(br, PmxIndexKind.Texture); rt.Comment = doc.Header.Encoding.GetString(br.ReadSizedBuffer()); rt.IndexCount = br.ReadInt32(); return(rt); }
public static PmxDocument Parse(Stream stream) { // leave open var br = new BinaryReader(stream); var header = Encoding.ASCII.GetString(br.ReadBytes(4)); var rt = new PmxDocument(); if (header != "PMX ") { throw new InvalidOperationException("invalid format"); } rt.Version = br.ReadSingle(); if (rt.Version != 2 && rt.Version != 2.1f) { throw new NotSupportedException("specified format version not supported"); } rt.Header = PmxHeader.Parse(br); rt.ModelInformation = PmxModelInformation.Parse(br, rt); rt.Vertices = Enumerable.Range(0, br.ReadInt32()).Select(_ => PmxVertex.Parse(br, rt)).ToList(); rt.Indices = Enumerable.Range(0, br.ReadInt32()).Select(_ => rt.ReadIndex(br, PmxIndexKind.Vertex)).ToList(); rt.Textures = Enumerable.Range(0, br.ReadInt32()).Select(_ => rt.ReadString(br)).ToList(); rt.Materials = Enumerable.Range(0, br.ReadInt32()).Select(_ => PmxMaterial.Parse(br, rt)).ToList(); rt.Bones = Enumerable.Range(0, br.ReadInt32()).Select(_ => PmxBone.Parse(br, rt)).ToList(); rt.Morphs = Enumerable.Range(0, br.ReadInt32()).Select(_ => PmxMorph.Parse(br, rt)).ToList(); rt.DisplayList = Enumerable.Range(0, br.ReadInt32()).Select(_ => PmxDisplayList.Parse(br, rt)).ToList(); rt.Rigids = Enumerable.Range(0, br.ReadInt32()).Select(_ => PmxRigidBody.Parse(br, rt)).ToList(); rt.Constraints = Enumerable.Range(0, br.ReadInt32()).Select(_ => PmxConstraint.Parse(br, rt)).ToList(); if (rt.Version > 2) { rt.SoftBodies = Enumerable.Range(0, br.ReadInt32()).Select(_ => PmxSoftBody.Parse(br, rt)).ToList(); } return(rt); }
public static PmxMaterial Parse(BinaryReader br, PmxDocument doc) { var rt = new PmxMaterial { Name = doc.Header.Encoding.GetString(br.ReadSizedBuffer()), EnglishName = doc.Header.Encoding.GetString(br.ReadSizedBuffer()), Diffuse = new[] { br.ReadSingle(), br.ReadSingle(), br.ReadSingle(), br.ReadSingle() }, Specular = new[] { br.ReadSingle(), br.ReadSingle(), br.ReadSingle() }, Power = br.ReadSingle(), Ambient = new[] { br.ReadSingle(), br.ReadSingle(), br.ReadSingle() }, Options = (PmxMaterialOptions)br.ReadByte(), EdgeColor = new[] { br.ReadSingle(), br.ReadSingle(), br.ReadSingle(), br.ReadSingle() }, EdgeSize = br.ReadSingle(), MainTexture = doc.ReadIndex(br, PmxIndexKind.Texture), SubTexture = doc.ReadIndex(br, PmxIndexKind.Texture), SubTextureMode = (PmxTextureMode)br.ReadByte(), UseSharedToonTexture = br.ReadBoolean(), }; rt.ToonTexture = rt.UseSharedToonTexture ? br.ReadByte() : doc.ReadIndex(br, PmxIndexKind.Texture); rt.Comment = doc.Header.Encoding.GetString(br.ReadSizedBuffer()); rt.IndexCount = br.ReadInt32(); return rt; }
public void Write(PmxMaterial material) { doc.WriteIndex(bw, PmxIndexKind.Material, material == null ? -1 : this.Materials[material]); }