private void bindTypes(MaterialI material, IDictionary values, List <MaterialFieldBinder> binders, string contentDirectory, Dictionary <string, string> fileExtOverrides, FoundBinderMethod handleFoundBinder) { if (binders == null) { return; } var materialType = material.GetType(); foreach (var binder in binders) { if (binder.MaterialName == material.Name && values.Contains(binder.InputID)) { #if WINRT var materialField = materialType.GetTypeInfo().GetDeclaredField(binder.ShaderMaterialFieldName); #else var materialField = materialType.GetField(binder.ShaderMaterialFieldName); #endif if (materialField == null) { Debug.ThrowError("Model", "Shader material field name does not exist: " + binder.ShaderMaterialFieldName); } if (handleFoundBinder != null) { handleFoundBinder(material, materialField, values, binder, contentDirectory, fileExtOverrides); } } } }
private void handleFoundValueBinder(MaterialI material, FieldInfo materialField, IDictionary values, MaterialFieldBinder binder, string contentDirectory, Dictionary <string, string> fileExtOverrides) { var value = values[binder.InputID]; var valueType = value.GetType(); var materialType = materialField.FieldType; if (materialType == valueType) { materialField.SetValue(material, values[binder.InputID]); } else if (materialType == typeof(Vector2)) { if (valueType == typeof(Vector3)) { var vector = (Vector3)value; materialField.SetValue(material, new Vector2(vector.X, vector.Y)); } else if (valueType == typeof(Vector4)) { var vector = (Vector4)value; materialField.SetValue(material, new Vector2(vector.X, vector.Y)); } } else if (materialType == typeof(Vector3)) { if (valueType == typeof(Vector2)) { var vector = (Vector2)value; materialField.SetValue(material, new Vector3(vector.X, vector.Y, 0)); } else if (valueType == typeof(Vector4)) { var vector = (Vector4)value; materialField.SetValue(material, new Vector3(vector.X, vector.Y, vector.Z)); } } else if (materialType == typeof(Vector4)) { if (valueType == typeof(Vector2)) { var vector = (Vector2)value; materialField.SetValue(material, new Vector4(vector.X, vector.Y, 0, 0)); } else if (valueType == typeof(Vector3)) { var vector = (Vector3)value; materialField.SetValue(material, new Vector4(vector.X, vector.Y, vector.Z, 0)); } } }
private void handleFoundTextureBinder(MaterialI material, FieldInfo materialField, IDictionary values, MaterialFieldBinder binder, string contentDirectory, Dictionary <string, string> fileExtOverrides) { var textureFileName = ((Dictionary <string, string>)values)[binder.InputID]; if (fileExtOverrides != null) { string ext = Streams.GetFileExt(textureFileName); if (fileExtOverrides.ContainsKey(ext)) { textureFileName = Streams.GetFileNameWithoutExt(textureFileName) + fileExtOverrides[ext]; } else { textureFileName = Streams.GetFileNameWithExt(textureFileName); } } else { textureFileName = Streams.GetFileNameWithExt(textureFileName); } // TODO load texture unless already loaded... //texture = parent.FindChild<Texture2D> //( // "NewReference", // new ConstructorParam(typeof(IDisposableResource), parent), // new ConstructorParam(typeof(string), filename), // new ConstructorParam(typeof(Loader.LoadedCallbackMethod), null) //); //if (texture != null) //{ // ++texture.referenceCount; // return texture; //} //return new Texture2D(parent, filename, loadedCallback); //var texture = Texture2DAPI.New(Parent, contentDirectory + textureFileName, null); //texture.AddReference(); ITexture2D texture = null; if (!Textures.Contains(texture)) { Textures.Add(texture); } materialField.SetValue(material, texture); }
private void init(string filename, Stream stream, string contentDirectory, Dictionary<string,Type> materialTypes, List<MaterialFieldBinder> value1BinderTypes, List<MaterialFieldBinder> value2BinderTypes, List<MaterialFieldBinder> value3BinderTypes, List<MaterialFieldBinder> value4BinderTypes, List<MaterialFieldBinder> textureBinderTypes, Dictionary<string,string> fileExtOverrides, int classicInstanceCount, Loader.LoadedCallbackMethod loadedCallback) { try { var reader = new BinaryReader(stream); // meta data if (reader.ReadInt32() != Streams.MakeFourCC('R', 'M', 'F', 'T')) Debug.ThrowError("Error", "Not a ReignModel file: " + filename); float version = reader.ReadSingle(); if (version != 1.0f) Debug.ThrowError("Error", "Unsuported model version: " + version.ToString()); bool compressed = reader.ReadBoolean(); // frames FrameStart = reader.ReadSingle(); FrameEnd = reader.ReadSingle(); FrameCount = FrameEnd - FrameStart; FPS = reader.ReadSingle(); // materials int materialCount = reader.ReadInt32(); Materials = new MaterialI[materialCount]; Textures = new List<ITexture2D>(); for (int i = 0; i != materialCount; ++i) { string name = reader.ReadString(); // create material bool pass = false; foreach (var materialType in (Dictionary<string,Type>)materialTypes) { if (materialType.Key == name) { Materials[i] = (MaterialI)Activator.CreateInstance(materialType.Value); Materials[i].Name = name; pass = true; break; } } if (!pass) Debug.ThrowError("Model", "Failed to find a valid material type for: " + name); var material = Materials[i]; // values1 var values1 = new Dictionary<string,float>(); int valueCount = reader.ReadInt32(); for (int i2 = 0; i2 != valueCount; ++i2) values1.Add(reader.ReadString(), reader.ReadSingle()); bindTypes(material, values1, value1BinderTypes, contentDirectory, fileExtOverrides, handleFoundValueBinder); // values2 var values2 = new Dictionary<string,Vector2>(); valueCount = reader.ReadInt32(); for (int i2 = 0; i2 != valueCount; ++i2) values2.Add(reader.ReadString(), reader.ReadVector2()); bindTypes(material, values2, value2BinderTypes, contentDirectory, fileExtOverrides, handleFoundValueBinder); // values3 var values3 = new Dictionary<string,Vector3>(); valueCount = reader.ReadInt32(); for (int i2 = 0; i2 != valueCount; ++i2) values3.Add(reader.ReadString(), reader.ReadVector3()); bindTypes(material, values3, value3BinderTypes, contentDirectory, fileExtOverrides, handleFoundValueBinder); // values4 var values4 = new Dictionary<string,Vector4>(); valueCount = reader.ReadInt32(); for (int i2 = 0; i2 != valueCount; ++i2) values4.Add(reader.ReadString(), reader.ReadVector4()); bindTypes(material, values4, value4BinderTypes, contentDirectory, fileExtOverrides, handleFoundValueBinder); // textures var textures = new Dictionary<string,string>(); int textureCount = reader.ReadInt32(); for (int i2 = 0; i2 != textureCount; ++i2) textures.Add(reader.ReadString(), reader.ReadString()); bindTypes(material, textures, textureBinderTypes, contentDirectory, fileExtOverrides, handleFoundTextureBinder); } // meshes int meshCount = reader.ReadInt32(); Meshes = new Mesh[meshCount]; for (int i = 0; i != meshCount; ++i) { Meshes[i] = new Mesh(reader, this, classicInstanceCount); } // actions int actionCount = reader.ReadInt32(); Actions = new Action[actionCount]; for (int i = 0; i != actionCount; ++i) { Actions[i] = new Action(reader); } // armatures int armatureCount = reader.ReadInt32(); Armatures = new Armature[armatureCount]; for (int i = 0; i != armatureCount; ++i) { Armatures[i] = new Armature(reader); } // objects int objectCount = reader.ReadInt32(); Objects = new Object[objectCount]; for (int i = 0; i != objectCount; ++i) { string type = reader.ReadString(); if (type == "MESH") Objects[i] = new ObjectMesh(reader, this); else if (type == "ARMATURE") Objects[i] = new ObjectArmature(reader, this); else Debug.ThrowError("Mesh", "Unsuported Object type: " + type); } // link objects foreach (var o in Objects) { o.linkObjects(Objects); } } catch (Exception e) { FailedToLoad = true; Loader.AddLoadableException(e); Dispose(); if (loadedCallback != null) loadedCallback(this, false); return; } if (Textures.Count == 0) { Loaded = true; if (loadedCallback != null) loadedCallback(this, true); } else { new LoadWaiter(Textures.ToArray(), delegate(object sender, bool succeeded) { if (succeeded) { Loaded = true; if (loadedCallback != null) loadedCallback(this, true); } else { FailedToLoad = true; Dispose(); if (loadedCallback != null) loadedCallback(this, false); } }); } }
private void handleFoundTextureBinder(MaterialI material, FieldInfo materialField, IDictionary values, MaterialFieldBinder binder, string contentDirectory, Dictionary<string,string> fileExtOverrides) { var textureFileName = ((Dictionary<string,string>)values)[binder.InputID]; if (fileExtOverrides != null) { string ext = Streams.GetFileExt(textureFileName); if (fileExtOverrides.ContainsKey(ext)) textureFileName = Streams.GetFileNameWithoutExt(textureFileName) + fileExtOverrides[ext]; else textureFileName = Streams.GetFileNameWithExt(textureFileName); } else { textureFileName = Streams.GetFileNameWithExt(textureFileName); } // TODO load texture unless already loaded... //texture = parent.FindChild<Texture2D> //( // "NewReference", // new ConstructorParam(typeof(IDisposableResource), parent), // new ConstructorParam(typeof(string), filename), // new ConstructorParam(typeof(Loader.LoadedCallbackMethod), null) //); //if (texture != null) //{ // ++texture.referenceCount; // return texture; //} //return new Texture2D(parent, filename, loadedCallback); //var texture = Texture2DAPI.New(Parent, contentDirectory + textureFileName, null); //texture.AddReference(); ITexture2D texture = null; if (!Textures.Contains(texture)) Textures.Add(texture); materialField.SetValue(material, texture); }
private void handleFoundValueBinder(MaterialI material, FieldInfo materialField, IDictionary values, MaterialFieldBinder binder, string contentDirectory, Dictionary<string,string> fileExtOverrides) { var value = values[binder.InputID]; var valueType = value.GetType(); var materialType = materialField.FieldType; if (materialType == valueType) { materialField.SetValue(material, values[binder.InputID]); } else if (materialType == typeof(Vector2)) { if (valueType == typeof(Vector3)) { var vector = (Vector3)value; materialField.SetValue(material, new Vector2(vector.X, vector.Y)); } else if (valueType == typeof(Vector4)) { var vector = (Vector4)value; materialField.SetValue(material, new Vector2(vector.X, vector.Y)); } } else if (materialType == typeof(Vector3)) { if (valueType == typeof(Vector2)) { var vector = (Vector2)value; materialField.SetValue(material, new Vector3(vector.X, vector.Y, 0)); } else if (valueType == typeof(Vector4)) { var vector = (Vector4)value; materialField.SetValue(material, new Vector3(vector.X, vector.Y, vector.Z)); } } else if (materialType == typeof(Vector4)) { if (valueType == typeof(Vector2)) { var vector = (Vector2)value; materialField.SetValue(material, new Vector4(vector.X, vector.Y, 0, 0)); } else if (valueType == typeof(Vector3)) { var vector = (Vector3)value; materialField.SetValue(material, new Vector4(vector.X, vector.Y, vector.Z, 0)); } } }
private void bindTypes(MaterialI material, IDictionary values, List<MaterialFieldBinder> binders, string contentDirectory, Dictionary<string,string> fileExtOverrides, FoundBinderMethod handleFoundBinder) { if (binders == null) return; var materialType = material.GetType(); foreach (var binder in binders) { if (binder.MaterialName == material.Name && values.Contains(binder.InputID)) { #if WINRT var materialField = materialType.GetTypeInfo().GetDeclaredField(binder.ShaderMaterialFieldName); #else var materialField = materialType.GetField(binder.ShaderMaterialFieldName); #endif if (materialField == null) Debug.ThrowError("Model", "Shader material field name does not exist: " + binder.ShaderMaterialFieldName); if (handleFoundBinder != null) handleFoundBinder(material, materialField, values, binder, contentDirectory, fileExtOverrides); } } }
public Mesh(BinaryReader reader, Model model, int classicInstanceCount) : base(model) { try { Name = reader.ReadString(); // material int materialIndex = reader.ReadInt32(); if (materialIndex != -1) Material = model.Materials[materialIndex]; // elements int elementCount = reader.ReadInt32(); var elements = new List<BufferLayoutElement>(); for (int i = 0; i != elementCount; ++i) { elements.Add(new BufferLayoutElement((BufferLayoutElementTypes)reader.ReadInt32(), (BufferLayoutElementUsages)reader.ReadInt32(), reader.ReadInt32(), reader.ReadInt32(), reader.ReadInt32())); } LayoutDesc = BufferLayoutDescAPI.New(elements); // vertices int vertexFloatCount = reader.ReadInt32(); var vertices = new float[vertexFloatCount]; for (int i = 0; i != vertexFloatCount; ++i) { vertices[i] = reader.ReadSingle(); } VertexBuffer = VertexBufferAPI.New(this, LayoutDesc, BufferUsages.Default, VertexBufferTopologys.Triangle, vertices); // indices int indexCount = reader.ReadInt32(); var indices = new int[indexCount]; for (int i = 0; i != indexCount; ++i) { indices[i] = reader.ReadInt32(); } IndexBuffer = IndexBufferAPI.New(this, BufferUsages.Default, indices); // create instancing buffers ClassicInstanceCount = classicInstanceCount; if (classicInstanceCount > 0) { var intancingElements = new List<BufferLayoutElement>(); foreach (var element in elements) intancingElements.Add(element); intancingElements.Add(new BufferLayoutElement(BufferLayoutElementTypes.Float, BufferLayoutElementUsages.IndexClassic, 0, 0, LayoutDesc.FloatCount)); InstancingLayoutDesc = BufferLayoutDescAPI.New(intancingElements); int instanceVertexFloatCount = (vertexFloatCount * classicInstanceCount) + (VertexBuffer.VertexCount * classicInstanceCount); var instancingVertices = new float[instanceVertexFloatCount]; int vi = 0; for (int i = 0; i != classicInstanceCount; ++i) { int vOffset = 0; for (int i2 = 0; i2 != VertexBuffer.VertexCount; ++i2) { for (int i3 = 0; i3 != VertexBuffer.VertexFloatArraySize; ++i3) { instancingVertices[vi] = vertices[vOffset]; ++vi; ++vOffset; } instancingVertices[vi] = i; ++vi; } } InstancingVertexBuffer = VertexBufferAPI.New(this, InstancingLayoutDesc, BufferUsages.Default, VertexBuffer.Topology, instancingVertices); int instanceIndexCount = (indexCount * classicInstanceCount); var instancingIndices = new int[instanceIndexCount]; int ii = 0, iOffset = 0; for (int i = 0; i != classicInstanceCount; ++i) { for (int i2 = 0; i2 != indexCount; ++i2) { instancingIndices[ii] = indices[i2] + iOffset; ++ii; } iOffset += VertexBuffer.VertexCount; } InstancingIndexBuffer = IndexBufferAPI.New(this, BufferUsages.Default, instancingIndices); } } catch (Exception e) { Dispose(); throw e; } }
private void init(string filename, Stream stream, string contentDirectory, Dictionary <string, Type> materialTypes, List <MaterialFieldBinder> value1BinderTypes, List <MaterialFieldBinder> value2BinderTypes, List <MaterialFieldBinder> value3BinderTypes, List <MaterialFieldBinder> value4BinderTypes, List <MaterialFieldBinder> textureBinderTypes, Dictionary <string, string> fileExtOverrides, int classicInstanceCount, Loader.LoadedCallbackMethod loadedCallback) { try { var reader = new BinaryReader(stream); // meta data if (reader.ReadInt32() != Streams.MakeFourCC('R', 'M', 'F', 'T')) { Debug.ThrowError("Error", "Not a ReignModel file: " + filename); } float version = reader.ReadSingle(); if (version != 1.0f) { Debug.ThrowError("Error", "Unsuported model version: " + version.ToString()); } bool compressed = reader.ReadBoolean(); // frames FrameStart = reader.ReadSingle(); FrameEnd = reader.ReadSingle(); FrameCount = FrameEnd - FrameStart; FPS = reader.ReadSingle(); // materials int materialCount = reader.ReadInt32(); Materials = new MaterialI[materialCount]; Textures = new List <ITexture2D>(); for (int i = 0; i != materialCount; ++i) { string name = reader.ReadString(); // create material bool pass = false; foreach (var materialType in (Dictionary <string, Type>)materialTypes) { if (materialType.Key == name) { Materials[i] = (MaterialI)Activator.CreateInstance(materialType.Value); Materials[i].Name = name; pass = true; break; } } if (!pass) { Debug.ThrowError("Model", "Failed to find a valid material type for: " + name); } var material = Materials[i]; // values1 var values1 = new Dictionary <string, float>(); int valueCount = reader.ReadInt32(); for (int i2 = 0; i2 != valueCount; ++i2) { values1.Add(reader.ReadString(), reader.ReadSingle()); } bindTypes(material, values1, value1BinderTypes, contentDirectory, fileExtOverrides, handleFoundValueBinder); // values2 var values2 = new Dictionary <string, Vector2>(); valueCount = reader.ReadInt32(); for (int i2 = 0; i2 != valueCount; ++i2) { values2.Add(reader.ReadString(), reader.ReadVector2()); } bindTypes(material, values2, value2BinderTypes, contentDirectory, fileExtOverrides, handleFoundValueBinder); // values3 var values3 = new Dictionary <string, Vector3>(); valueCount = reader.ReadInt32(); for (int i2 = 0; i2 != valueCount; ++i2) { values3.Add(reader.ReadString(), reader.ReadVector3()); } bindTypes(material, values3, value3BinderTypes, contentDirectory, fileExtOverrides, handleFoundValueBinder); // values4 var values4 = new Dictionary <string, Vector4>(); valueCount = reader.ReadInt32(); for (int i2 = 0; i2 != valueCount; ++i2) { values4.Add(reader.ReadString(), reader.ReadVector4()); } bindTypes(material, values4, value4BinderTypes, contentDirectory, fileExtOverrides, handleFoundValueBinder); // textures var textures = new Dictionary <string, string>(); int textureCount = reader.ReadInt32(); for (int i2 = 0; i2 != textureCount; ++i2) { textures.Add(reader.ReadString(), reader.ReadString()); } bindTypes(material, textures, textureBinderTypes, contentDirectory, fileExtOverrides, handleFoundTextureBinder); } // meshes int meshCount = reader.ReadInt32(); Meshes = new Mesh[meshCount]; for (int i = 0; i != meshCount; ++i) { Meshes[i] = new Mesh(reader, this, classicInstanceCount); } // actions int actionCount = reader.ReadInt32(); Actions = new Action[actionCount]; for (int i = 0; i != actionCount; ++i) { Actions[i] = new Action(reader); } // armatures int armatureCount = reader.ReadInt32(); Armatures = new Armature[armatureCount]; for (int i = 0; i != armatureCount; ++i) { Armatures[i] = new Armature(reader); } // objects int objectCount = reader.ReadInt32(); Objects = new Object[objectCount]; for (int i = 0; i != objectCount; ++i) { string type = reader.ReadString(); if (type == "MESH") { Objects[i] = new ObjectMesh(reader, this); } else if (type == "ARMATURE") { Objects[i] = new ObjectArmature(reader, this); } else { Debug.ThrowError("Mesh", "Unsuported Object type: " + type); } } // link objects foreach (var o in Objects) { o.linkObjects(Objects); } } catch (Exception e) { FailedToLoad = true; Loader.AddLoadableException(e); Dispose(); if (loadedCallback != null) { loadedCallback(this, false); } return; } if (Textures.Count == 0) { Loaded = true; if (loadedCallback != null) { loadedCallback(this, true); } } else { new LoadWaiter(Textures.ToArray(), delegate(object sender, bool succeeded) { if (succeeded) { Loaded = true; if (loadedCallback != null) { loadedCallback(this, true); } } else { FailedToLoad = true; Dispose(); if (loadedCallback != null) { loadedCallback(this, false); } } }); } }
public Mesh(BinaryReader reader, Model model, int classicInstanceCount) : base(model) { try { Name = reader.ReadString(); // material int materialIndex = reader.ReadInt32(); if (materialIndex != -1) { Material = model.Materials[materialIndex]; } // elements int elementCount = reader.ReadInt32(); var elements = new List <BufferLayoutElement>(); for (int i = 0; i != elementCount; ++i) { elements.Add(new BufferLayoutElement((BufferLayoutElementTypes)reader.ReadInt32(), (BufferLayoutElementUsages)reader.ReadInt32(), reader.ReadInt32(), reader.ReadInt32(), reader.ReadInt32())); } LayoutDesc = BufferLayoutDescAPI.New(elements); // vertices int vertexFloatCount = reader.ReadInt32(); var vertices = new float[vertexFloatCount]; for (int i = 0; i != vertexFloatCount; ++i) { vertices[i] = reader.ReadSingle(); } VertexBuffer = VertexBufferAPI.New(this, LayoutDesc, BufferUsages.Default, VertexBufferTopologys.Triangle, vertices); // indices int indexCount = reader.ReadInt32(); var indices = new int[indexCount]; for (int i = 0; i != indexCount; ++i) { indices[i] = reader.ReadInt32(); } IndexBuffer = IndexBufferAPI.New(this, BufferUsages.Default, indices); // create instancing buffers ClassicInstanceCount = classicInstanceCount; if (classicInstanceCount > 0) { var intancingElements = new List <BufferLayoutElement>(); foreach (var element in elements) { intancingElements.Add(element); } intancingElements.Add(new BufferLayoutElement(BufferLayoutElementTypes.Float, BufferLayoutElementUsages.IndexClassic, 0, 0, LayoutDesc.FloatCount)); InstancingLayoutDesc = BufferLayoutDescAPI.New(intancingElements); int instanceVertexFloatCount = (vertexFloatCount * classicInstanceCount) + (VertexBuffer.VertexCount * classicInstanceCount); var instancingVertices = new float[instanceVertexFloatCount]; int vi = 0; for (int i = 0; i != classicInstanceCount; ++i) { int vOffset = 0; for (int i2 = 0; i2 != VertexBuffer.VertexCount; ++i2) { for (int i3 = 0; i3 != VertexBuffer.VertexFloatArraySize; ++i3) { instancingVertices[vi] = vertices[vOffset]; ++vi; ++vOffset; } instancingVertices[vi] = i; ++vi; } } InstancingVertexBuffer = VertexBufferAPI.New(this, InstancingLayoutDesc, BufferUsages.Default, VertexBuffer.Topology, instancingVertices); int instanceIndexCount = (indexCount * classicInstanceCount); var instancingIndices = new int[instanceIndexCount]; int ii = 0, iOffset = 0; for (int i = 0; i != classicInstanceCount; ++i) { for (int i2 = 0; i2 != indexCount; ++i2) { instancingIndices[ii] = indices[i2] + iOffset; ++ii; } iOffset += VertexBuffer.VertexCount; } InstancingIndexBuffer = IndexBufferAPI.New(this, BufferUsages.Default, instancingIndices); } } catch (Exception e) { Dispose(); throw e; } }