private void ReadMaterial(BinaryReader reader, int msize)
{
int total = 6;
string name = null;
var luminance = Colors.Transparent;
var diffuse = Colors.Transparent;
var specular = Colors.Transparent;
var shininess = Colors.Transparent;
string texture = null;
while (total < msize)
{
ChunkID id = this.ReadChunkId(reader);
int size = this.ReadChunkSize(reader);
// Debug.WriteLine(id);
total += size;
switch (id)
{
case ChunkID.MAT_NAME01:
name = this.ReadString(reader);
// name = ReadString(size - 6);
break;
case ChunkID.MAT_LUMINANCE:
luminance = this.ReadColor(reader);
break;
case ChunkID.MAT_DIFFUSE:
diffuse = this.ReadColor(reader);
break;
case ChunkID.MAT_SPECULAR:
specular = this.ReadColor(reader);
break;
case ChunkID.MAT_SHININESS:
byte[] bytes = this.ReadData(reader, size - 6);
// shininess = ReadColor(r, size);
break;
case ChunkID.MAT_MAP:
texture = this.ReadMatMap(reader, size - 6);
break;
case ChunkID.MAT_MAPFILE:
this.ReadData(reader, size - 6);
break;
default:
this.ReadData(reader, size - 6);
break;
}
}
int specularPower = 100;
var mg = new MaterialGroup();
// mg.Children.Add(new DiffuseMaterial(new SolidColorBrush(luminance)));
if (texture != null)
{
string ext = Path.GetExtension(texture);
if (ext != null)
{
ext = ext.ToLower();
}
// TGA not supported - convert textures to .png!
if (ext == ".tga")
{
texture = Path.ChangeExtension(texture, ".png");
}
var actualTexturePath = this.TexturePath ?? string.Empty;
string path = Path.Combine(actualTexturePath, texture);
if (File.Exists(path))
{
var img = new BitmapImage(new Uri(path, UriKind.Relative));
var textureBrush = new ImageBrush(img) { ViewportUnits = BrushMappingMode.Absolute, TileMode = TileMode.Tile };
mg.Children.Add(new DiffuseMaterial(textureBrush));
}
else
{
Debug.WriteLine(string.Format("Texture not found: {0}", Path.GetFullPath(path)));
mg.Children.Add(new DiffuseMaterial(new SolidColorBrush(diffuse)));
}
}
else
{
mg.Children.Add(new DiffuseMaterial(new SolidColorBrush(diffuse)));
}
mg.Children.Add(new SpecularMaterial(new SolidColorBrush(specular), specularPower));
if (name != null)
{
this.materials[name] = mg;
}
}