public void SetTexture(string texture, TextureType type, int id) { if (type == TextureType.Albedo) { TextureAlbedo = new GeoTexture() { Filename = texture, OpenGLID = id, Type = type, UVMapIndex = 0, UVTransform = new Vector2(TextureAlbedo.UVTransform.X == 0 ? 1 : TextureAlbedo.UVTransform.X, TextureAlbedo.UVTransform.Y == 0 ? 1 : TextureAlbedo.UVTransform.Y) }; } else if (type == TextureType.Emissive) { TextureEmissive = new GeoTexture() { Filename = texture, OpenGLID = id, Type = type, UVMapIndex = 0, UVTransform = new Vector2(1, 1) }; } else if (type == TextureType.Light) { TextureLight = new GeoTexture() { Filename = texture, OpenGLID = id, Type = type, UVMapIndex = 1, UVTransform = new Vector2(1, 1) }; } else if (type == TextureType.Metalness) { TextureMetalness = new GeoTexture() { Filename = texture, OpenGLID = id, Type = type, UVMapIndex = 0, UVTransform = new Vector2(1, 1) }; } else if (type == TextureType.Roughness) { TextureRoughness = new GeoTexture() { Filename = texture, OpenGLID = id, Type = type, UVMapIndex = 0, UVTransform = new Vector2(1, 1) }; } else if (type == TextureType.Normal) { TextureNormal = new GeoTexture() { Filename = texture, OpenGLID = id, Type = type, UVMapIndex = 0, UVTransform = new Vector2(1, 1) }; } else { throw new System.Exception("Unknown texture type for current material " + Name); } }
private static bool CheckIfOtherModelsShareTexture(string texture, string path, out GeoTexture sharedTex) { sharedTex = new GeoTexture(); foreach (string key in KWEngine.Models.Keys) { GeoModel m = KWEngine.Models[key]; if (m.Path == path) { foreach (string texKey in m.Textures.Keys) { if (texKey == texture) { sharedTex = m.Textures[texKey]; return(true); } } } } return(false); }
private void EditTextureObject(ref GeoTexture tex, int texId, TextureType type, string name) { if (type == TextureType.Diffuse) { tex.Filename = name; tex.Type = GeoTexture.TexType.Diffuse; tex.OpenGLID = texId; } else if (type == TextureType.Normal) { tex.Filename = name; tex.Type = GeoTexture.TexType.Normal; tex.OpenGLID = texId; } else { tex.Filename = name; tex.Type = GeoTexture.TexType.Specular; tex.OpenGLID = texId; } }
private static void ProcessMaterialsForMesh(Scene scene, Mesh mesh, ref GeoModel model, ref GeoMesh geoMesh, bool isKWCube = false) { GeoMaterial geoMaterial = new GeoMaterial(); Material material = null; if (isKWCube) { if (mesh.MaterialIndex >= 0) { material = scene.Materials[mesh.MaterialIndex]; geoMaterial.Name = model.Filename == "kwcube.obj" ? "KWCube" : material.Name; geoMaterial.BlendMode = material.BlendMode == BlendMode.Default ? OpenTK.Graphics.OpenGL4.BlendingFactor.OneMinusSrcAlpha : OpenTK.Graphics.OpenGL4.BlendingFactor.One; // TODO: Check if this is correct! geoMaterial.ColorDiffuse = new Vector4(1, 1, 1, 1); geoMaterial.ColorEmissive = new Vector4(0, 0, 0, 1); } else { geoMaterial.Name = "kw-undefined."; geoMaterial.BlendMode = OpenTK.Graphics.OpenGL4.BlendingFactor.OneMinusSrcAlpha; geoMaterial.ColorDiffuse = new Vector4(1, 1, 1, 1); geoMaterial.ColorEmissive = new Vector4(0, 0, 0, 1); } geoMaterial.SpecularArea = 1024; geoMaterial.SpecularPower = 0; } else { if (mesh.MaterialIndex >= 0) { material = scene.Materials[mesh.MaterialIndex]; geoMaterial.Name = material.Name; if (material.Name == "DefaultMaterial") { geoMaterial.BlendMode = OpenTK.Graphics.OpenGL4.BlendingFactor.OneMinusSrcAlpha; geoMaterial.ColorDiffuse = new Vector4(1, 1, 1, 1); geoMaterial.ColorEmissive = new Vector4(0, 0, 0, 1); geoMaterial.SpecularPower = 0; geoMaterial.SpecularArea = 1024; geoMaterial.TextureSpecularIsRoughness = false; if (mesh.Name != null && mesh.Name.ToLower().Contains("_invisible")) { geoMaterial.Opacity = 0; } } else { geoMaterial.BlendMode = material.BlendMode == BlendMode.Default ? OpenTK.Graphics.OpenGL4.BlendingFactor.OneMinusSrcAlpha : OpenTK.Graphics.OpenGL4.BlendingFactor.One; // TODO: Check if this is correct! if (model.AssemblyMode == AssemblyMode.Internal && material.Name == "System") { geoMaterial.ColorDiffuse = new Vector4(1, 1, 1, 1); } else if (model.AssemblyMode == AssemblyMode.Internal && material.Name == "X") { geoMaterial.ColorDiffuse = new Vector4(1, 0, 0, 1); } else if (model.AssemblyMode == AssemblyMode.Internal && material.Name == "Y") { geoMaterial.ColorDiffuse = new Vector4(0, 1, 0, 1); } else if (model.AssemblyMode == AssemblyMode.Internal && material.Name == "Z") { geoMaterial.ColorDiffuse = new Vector4(0, 0, 1, 1); } else { geoMaterial.ColorDiffuse = material.HasColorDiffuse ? new Vector4(material.ColorDiffuse.R, material.ColorDiffuse.G, material.ColorDiffuse.B, material.ColorDiffuse.A) : new Vector4(1, 1, 1, 1); } geoMaterial.ColorEmissive = material.HasColorEmissive ? new Vector4(material.ColorEmissive.R, material.ColorEmissive.G, material.ColorEmissive.B, material.ColorEmissive.A) : new Vector4(0, 0, 0, 1); geoMaterial.SpecularPower = material.ShininessStrength; geoMaterial.SpecularArea = material.Shininess; geoMaterial.TextureSpecularIsRoughness = false; geoMaterial.Opacity = material.HasOpacity ? material.Opacity : 1; if (mesh.Name != null && mesh.Name.ToLower().Contains("_invisible")) { geoMaterial.Opacity = 0; } } } else { geoMaterial.Name = "kw-undefined."; geoMaterial.BlendMode = OpenTK.Graphics.OpenGL4.BlendingFactor.OneMinusSrcAlpha; geoMaterial.ColorDiffuse = new Vector4(1, 1, 1, 1); geoMaterial.ColorEmissive = new Vector4(0, 0, 0, 1); geoMaterial.SpecularArea = 1024; geoMaterial.SpecularPower = 0; geoMaterial.TextureSpecularIsRoughness = false; if (mesh.Name != null && mesh.Name.ToLower().Contains("_invisible")) { geoMaterial.Opacity = 0; } } } // Process Textures: if (material != null) { bool roughnessUsed = false; TextureSlot[] texturesOfMaterial = material.GetAllMaterialTextures(); foreach (TextureSlot slot in texturesOfMaterial) { if (slot.TextureType == TextureType.Shininess) // this is PBR Roughness { GeoTexture tex = new GeoTexture(); tex.UVTransform = new OpenTK.Vector2(1, 1); tex.Filename = slot.FilePath; tex.UVMapIndex = slot.UVIndex; if (model.Textures.ContainsKey(tex.Filename)) { tex.OpenGLID = model.Textures[tex.Filename].OpenGLID; } else { if (model.AssemblyMode == AssemblyMode.File) { tex.OpenGLID = HelperTexture.LoadTextureForModelExternal( FindTextureInSubs(StripPathFromFile(tex.Filename), model.PathAbsolute), true ); } else { string path = StripFileNameFromAssemblyPath(model.PathAbsolute).Substring(model.PathAbsolute.IndexOf('.') + 1) + StripPathFromFile(tex.Filename); tex.OpenGLID = HelperTexture.LoadTextureForModelInternal(path, true); } if (tex.OpenGLID > 0) { tex.Type = GeoTexture.TexType.Specular; model.Textures.Add(tex.Filename, tex); geoMaterial.TextureSpecular = tex; geoMaterial.TextureSpecularIsRoughness = true; roughnessUsed = true; } else { geoMaterial.TextureSpecular = tex; geoMaterial.TextureSpecularIsRoughness = false; tex.OpenGLID = KWEngine.TextureBlack; } } break; } } // Diffuse texture if (material.HasTextureDiffuse) { GeoTexture tex = new GeoTexture(); tex.UVTransform = new OpenTK.Vector2(1, 1); tex.Filename = material.TextureDiffuse.FilePath; tex.UVMapIndex = material.TextureDiffuse.UVIndex; tex.Type = GeoTexture.TexType.Diffuse; if (model.Textures.ContainsKey(tex.Filename)) { tex.OpenGLID = model.Textures[tex.Filename].OpenGLID; geoMaterial.TextureDiffuse = tex; } else if (CheckIfOtherModelsShareTexture(tex.Filename, model.Path, out GeoTexture sharedTexture)) { geoMaterial.TextureDiffuse = sharedTexture; } else { if (model.AssemblyMode == AssemblyMode.File) { tex.OpenGLID = HelperTexture.LoadTextureForModelExternal( FindTextureInSubs(StripPathFromFile(tex.Filename), model.PathAbsolute) ); } else { string path = StripFileNameFromAssemblyPath(model.PathAbsolute).Substring(model.PathAbsolute.IndexOf('.') + 1) + StripPathFromFile(tex.Filename); tex.OpenGLID = HelperTexture.LoadTextureForModelInternal(path, true); } if (tex.OpenGLID > 0) { geoMaterial.TextureDiffuse = tex; model.Textures.Add(tex.Filename, tex); } else { tex.OpenGLID = KWEngine.TextureDefault; geoMaterial.TextureDiffuse = tex; } } } // Normal map texture if (material.HasTextureNormal) { GeoTexture tex = new GeoTexture(); tex.UVTransform = new OpenTK.Vector2(1, 1); tex.Filename = material.TextureNormal.FilePath; tex.UVMapIndex = material.TextureNormal.UVIndex; tex.Type = GeoTexture.TexType.Normal; if (model.Textures.ContainsKey(tex.Filename)) { tex.OpenGLID = model.Textures[tex.Filename].OpenGLID; geoMaterial.TextureNormal = tex; } else if (CheckIfOtherModelsShareTexture(tex.Filename, model.Path, out GeoTexture sharedTexture)) { geoMaterial.TextureNormal = sharedTexture; } else { if (model.AssemblyMode == AssemblyMode.File) { tex.OpenGLID = HelperTexture.LoadTextureForModelExternal( FindTextureInSubs(StripPathFromFile(tex.Filename), model.PathAbsolute) ); } else { string path = StripFileNameFromAssemblyPath(model.PathAbsolute).Substring(model.PathAbsolute.IndexOf('.') + 1) + StripPathFromFile(tex.Filename); tex.OpenGLID = HelperTexture.LoadTextureForModelInternal(path, true); } if (tex.OpenGLID > 0) { model.Textures.Add(tex.Filename, tex); geoMaterial.TextureNormal = tex; } else { tex.OpenGLID = KWEngine.TextureBlack; //geoMaterial.TextureNormal = tex; } } } // Specular map texture if (material.HasTextureSpecular && roughnessUsed == false) { GeoTexture tex = new GeoTexture(); tex.UVTransform = new OpenTK.Vector2(1, 1); tex.Filename = material.TextureSpecular.FilePath; tex.UVMapIndex = material.TextureSpecular.UVIndex; tex.Type = GeoTexture.TexType.Specular; if (model.Textures.ContainsKey(tex.Filename)) { tex.OpenGLID = model.Textures[tex.Filename].OpenGLID; geoMaterial.TextureSpecular = tex; } else if (CheckIfOtherModelsShareTexture(tex.Filename, model.Path, out GeoTexture sharedTexture)) { geoMaterial.TextureSpecular = sharedTexture; } else { if (model.AssemblyMode == AssemblyMode.File) { tex.OpenGLID = HelperTexture.LoadTextureForModelExternal( FindTextureInSubs(StripPathFromFile(tex.Filename), model.PathAbsolute) ); } else { string path = StripFileNameFromAssemblyPath(model.PathAbsolute).Substring(model.PathAbsolute.IndexOf('.') + 1) + StripPathFromFile(tex.Filename); tex.OpenGLID = HelperTexture.LoadTextureForModelInternal(path, true); } if (tex.OpenGLID > 0) { geoMaterial.TextureSpecular = tex; model.Textures.Add(tex.Filename, tex); } else { tex.OpenGLID = KWEngine.TextureBlack; geoMaterial.TextureSpecular = tex; } } } else { if (material.HasTextureSpecular && roughnessUsed) { Debug.WriteLine("Skipping specular texture for " + model.Filename + " because roughness texture was found."); } } // Emissive map texture if (material.HasTextureEmissive) { GeoTexture tex = new GeoTexture(); tex.UVTransform = new OpenTK.Vector2(1, 1); tex.Filename = material.TextureEmissive.FilePath; tex.UVMapIndex = material.TextureEmissive.UVIndex; tex.Type = GeoTexture.TexType.Emissive; if (model.Textures.ContainsKey(tex.Filename)) { tex.OpenGLID = model.Textures[tex.Filename].OpenGLID; geoMaterial.TextureEmissive = tex; } else if (CheckIfOtherModelsShareTexture(tex.Filename, model.Path, out GeoTexture sharedTexture)) { geoMaterial.TextureEmissive = sharedTexture; } else { if (model.AssemblyMode == AssemblyMode.File) { tex.OpenGLID = HelperTexture.LoadTextureForModelExternal( FindTextureInSubs(StripPathFromFile(tex.Filename), model.PathAbsolute) ); } else { string path = StripFileNameFromAssemblyPath(model.PathAbsolute).Substring(model.PathAbsolute.IndexOf('.') + 1) + StripPathFromFile(tex.Filename); tex.OpenGLID = HelperTexture.LoadTextureForModelInternal(path, true); } if (tex.OpenGLID > 0) { geoMaterial.TextureEmissive = tex; model.Textures.Add(tex.Filename, tex); } else { tex.OpenGLID = KWEngine.TextureBlack; geoMaterial.TextureEmissive = tex; } } } // Light map texture if (material.HasTextureLightMap) { GeoTexture tex = new GeoTexture(); tex.UVTransform = new OpenTK.Vector2(1, 1); tex.Filename = material.TextureLightMap.FilePath; tex.UVMapIndex = material.TextureLightMap.UVIndex; tex.Type = GeoTexture.TexType.Light; if (model.Textures.ContainsKey(tex.Filename)) { tex.OpenGLID = model.Textures[tex.Filename].OpenGLID; geoMaterial.TextureLight = tex; } else if (CheckIfOtherModelsShareTexture(tex.Filename, model.Path, out GeoTexture sharedTexture)) { geoMaterial.TextureLight = sharedTexture; } else { if (model.AssemblyMode == AssemblyMode.File) { tex.OpenGLID = HelperTexture.LoadTextureForModelExternal( FindTextureInSubs(StripPathFromFile(tex.Filename), model.PathAbsolute) ); } else { string path = StripFileNameFromAssemblyPath(model.PathAbsolute).Substring(model.PathAbsolute.IndexOf('.') + 1) + StripPathFromFile(tex.Filename); tex.OpenGLID = HelperTexture.LoadTextureForModelInternal(path, true); } if (tex.OpenGLID > 0) { model.Textures.Add(tex.Filename, tex); geoMaterial.TextureLight = tex; } else { tex.OpenGLID = KWEngine.TextureBlack; //geoMaterial.TextureLight = tex; } } } } geoMesh.Material = geoMaterial; }
private void EditTextureObject(ref GeoTexture tex, float x, float y) { tex.UVTransform = new OpenTK.Vector2(x, y); }