public static PMXColorRGB LoadFromStreamStatic(BinaryReader br) { PMXColorRGB res = new PMXColorRGB(); res.LoadFromStream(br); return(res); }
public override void LoadFromStream(BinaryReader br, MMDImportSettings importSettings) { int mtIndex = PMXParser.ReadIndex(br, importSettings.BitSettings.MaterialIndexLength); if (mtIndex < 0) { this.Material = null; } else { this.Material = this.Model.Materials[mtIndex]; } this.Type = (MaterialMorphOffsetType)(int)br.ReadByte(); this.Diffuse = PMXColorRGBA.LoadFromStreamStatic(br); this.Specular = PMXColorRGBA.LoadFromStreamStatic(br); this.Ambient = PMXColorRGB.LoadFromStreamStatic(br); this.EdgeColor = PMXColorRGBA.LoadFromStreamStatic(br); this.EdgeSize = br.ReadSingle(); this.TextureFactor = PMXColorRGBA.LoadFromStreamStatic(br); this.SphereTextureFactor = PMXColorRGBA.LoadFromStreamStatic(br); this.ToonTextureFactor = PMXColorRGBA.LoadFromStreamStatic(br); }
public void LoadFromStream(BinaryReader br, MMDImportSettings importSettings, string[] textures, List <PMXTriangle> triangles) { if (textures == null) { textures = new string[0]; } int triangleVerticesCount; if (importSettings.Format == MMDImportSettings.ModelFormat.PMX) { //PMX format this.NameJP = PMXParser.ReadString(br, importSettings.TextEncoding); this.NameEN = PMXParser.ReadString(br, importSettings.TextEncoding); this.Diffuse = PMXColorRGB.LoadFromStreamStatic(br); this.Alpha = br.ReadSingle(); this.Specular = PMXColorRGB.LoadFromStreamStatic(br); this.SpecularFactor = br.ReadSingle(); this.Ambient = PMXColorRGB.LoadFromStreamStatic(br); byte flags = br.ReadByte(); //Flag parsing //1st bit = double sided this.DoubleSided = ((flags & PMXMaterial.MATERIAL_DOUBLE_SIDED) != 0); //2nd bit = ground shadow this.GroundShadow = ((flags & PMXMaterial.MATERIAL_GROUND_SHADOW) != 0); //3rd bit - self shadow this.SelfShadow = ((flags & PMXMaterial.MATERIAL_SELF_SHADOW) != 0); //4th bit - self shadow+ this.SelfShadowPlus = ((flags & PMXMaterial.MATERIAL_SELF_SHADOW_PLUS) != 0); //5th bit - has edge line this.EdgeEnabled = ((flags & PMXMaterial.MATERIAL_EDGE_ENABLED) != 0); //6th bit - shine with vertex colour this.VertexColor = ((flags & PMXMaterial.MATERIAL_VERTEX_COLOR) != 0); //7th and 8bit - Tri, Point or Line shadow int shadowType = ((flags & 0xC0) >> 6); this.GroundShadowType = (PMXGroundShadowType)shadowType; this.EdgeColor = PMXColorRGBA.LoadFromStreamStatic(br); this.EdgeSize = br.ReadSingle(); int diffIndex = PMXParser.ReadIndex(br, importSettings.BitSettings.TextureIndexLength); this.DiffuseTexture = this.GetTextureFromIndex(diffIndex, textures); int sphereIndex = PMXParser.ReadIndex(br, importSettings.BitSettings.TextureIndexLength); this.SphereTexture = this.GetTextureFromIndex(sphereIndex, textures); this.SphereMode = (PMXSphereMode)(int)(br.ReadByte()); this.StandardToon = (br.ReadByte() == 1); if (this.StandardToon) { this.StandardToonIndex = br.ReadSByte(); } else { int toonTexIndex = PMXParser.ReadIndex(br, importSettings.BitSettings.TextureIndexLength); this.NonStandardToonTexture = this.GetTextureFromIndex(toonTexIndex, textures); } this.Comment = PMXParser.ReadString(br, importSettings.TextEncoding); triangleVerticesCount = br.ReadInt32(); } else { //PMD format this.Diffuse = PMXColorRGB.LoadFromStreamStatic(br); this.Alpha = br.ReadSingle(); this.SpecularFactor = br.ReadSingle(); this.Specular = PMXColorRGB.LoadFromStreamStatic(br); this.Ambient = PMXColorRGB.LoadFromStreamStatic(br); this._pmdToonIndex = br.ReadSByte(); byte flags = br.ReadByte(); this.EdgeEnabled = (flags == 1); this.GroundShadow = this.EdgeEnabled; triangleVerticesCount = br.ReadInt32(); string textureFile = PMDParser.ReadString(br, 20, importSettings.TextEncoding); if (textureFile != null) { string[] textureRefs = textureFile.Split(new char[] { '*' }, 2, StringSplitOptions.RemoveEmptyEntries); this.DiffuseTexture = textureRefs[0]; if (textureRefs.Length > 1) { this.SphereTexture = textureRefs[1]; } } } if (triangleVerticesCount % 3 != 0) { throw new InvalidDataException("Invalid triangle format!"); } if (triangles != null) { int triangleCount = triangleVerticesCount / 3; if (triangleCount > triangles.Count) { throw new InvalidDataException("Model doesn't have enough triangles!"); } this.Triangles = triangles.GetRange(0, triangleCount); triangles.RemoveRange(0, triangleCount); } }