public void FillScene(Assimp.Scene scene, TEX1 textures, string fileDir) { textures.DumpTextures(fileDir); foreach (Material mat in m_Materials) { Assimp.Material assMat = new Assimp.Material(); if (mat.TextureIndices[0] != -1) { int texIndex = mat.TextureIndices[0]; //texIndex = m_TexRemapBlock[texIndex]; string texPath = Path.Combine(fileDir, textures[texIndex].Name + ".png"); Assimp.TextureSlot tex = new Assimp.TextureSlot(texPath, Assimp.TextureType.Diffuse, 0, Assimp.TextureMapping.FromUV, 0, 1.0f, Assimp.TextureOperation.Add, textures[texIndex].WrapS.ToAssImpWrapMode(), textures[texIndex].WrapT.ToAssImpWrapMode(), 0); assMat.AddMaterialTexture(ref tex); } if (mat.MaterialColors[0] != null) { assMat.ColorDiffuse = mat.MaterialColors[0].Value.ToColor4D(); } if (mat.AmbientColors[0] != null) { assMat.ColorAmbient = mat.AmbientColors[0].Value.ToColor4D(); } scene.Materials.Add(assMat); } }
public void Load(Assimp.TextureSlot texture, string modelDirectory) { Format = TextureFormats.RGBA32; AlphaSetting = 0; WrapS = texture.WrapModeU.ToGXWrapMode(); WrapT = texture.WrapModeV.ToGXWrapMode(); PaletteFormat = PaletteFormats.IA8; PaletteCount = 0; EmbeddedPaletteOffset = 0; MinFilter = FilterMode.Linear; MagFilter = FilterMode.Linear; MipMapCount = 0; LodBias = 0; Bitmap texData = null; if (File.Exists(texture.FilePath)) { texData = new Bitmap(texture.FilePath); Name = Path.GetFileNameWithoutExtension(texture.FilePath); } else { Console.WriteLine($"Texture was not found at path \"{ texture.FilePath }\". Searching the model's directory..."); string fileName = Path.GetFileName(texture.FilePath); string texPath = Path.Combine(modelDirectory, fileName); if (!File.Exists(texPath)) { Console.WriteLine($"Cannot find texture { fileName }. Using a checkboard texture instead..."); texData = new Bitmap(SuperBMD.Properties.Resources.default_checker); Name = "default_checker"; } else { texData = new Bitmap(texPath); Name = Path.GetFileNameWithoutExtension(texPath); } } Width = (ushort)texData.Width; Height = (ushort)texData.Height; m_rgbaImageData = new byte[Width * Height * 4]; BitmapData dat = texData.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); Marshal.Copy(dat.Scan0, m_rgbaImageData, 0, m_rgbaImageData.Length); texData.UnlockBits(dat); texData.Dispose(); }
private void SetMaterialTexture(Material material, Assimp.Material assimpMat) { Assimp.TextureSlot diffuseMap = assimpMat.GetTexture(Assimp.TextureType.Diffuse, 0); if (!String.IsNullOrEmpty(diffuseMap.FilePath)) { Texture diffuse; try { diffuse = m_content.LoadRelativeTo <Texture2D>(diffuseMap.FilePath, m_modelResource);// m_content.Load<Texture2D>(diffuseMap.FilePath); material.SetParameter("DiffuseMap", diffuse); } catch { // diffuse = m_content.Load<Texture2D>("notexture.tebo"); } } }
static void BindUniform(ref Action bind, Shader shader, Assimp.TextureSlot textureSlot, int index, ResourceManager resourceManager) { var uniformName = ShaderConstants.Texture + textureSlot.TextureType.ToString(); var location = GL.GetUniformLocation(shader.Program, uniformName); if (location >= 0 && !string.IsNullOrEmpty(textureSlot.FilePath)) { var textureName = Path.GetFileNameWithoutExtension(textureSlot.FilePath); var texture = resourceManager.Load <Texture>(textureName); var textureUnit = TextureUnit.Texture0 + index; bind += () => { GL.Uniform1(location, index); GL.ActiveTexture(textureUnit); GL.BindTexture(TextureTarget.Texture2D, texture.Id); }; } }
private TextureProvider[] InitTextures(Assimp.Scene aiScene, string filename) { var textureProviders = new TextureProvider[aiScene.MaterialCount]; // Extract the directory part from the file name string directory = new FileInfo(filename).DirectoryName; // Initialize the materials for (uint i = 0; i < aiScene.MaterialCount; i++) { Assimp.Material material = aiScene.Materials[i]; if (material.GetTextureCount(Assimp.TextureType.Diffuse) > 0) { Assimp.TextureSlot slot = material.GetTexture(Assimp.TextureType.Diffuse, 0); string fullname = Path.Combine(directory, slot.FilePath); var provider = new TextureProvider(fullname); textureProviders[i] = provider; } } return(textureProviders); }
private void GetTexture(Assimp.TextureSlot tslot, out Texture2D texture, out float useColorMap) { string fullpath = Path.IsPathRooted(tslot.FilePath) ? tslot.FilePath : directoryName + "\\" + tslot.FilePath; texture = new Texture2D(1, 1, TextureFormat.RGBA32, 0, true); useColorMap = 0f; if (File.Exists(fullpath)) { texture = GetOrCreateTextureFromFile(fullpath); useColorMap = 1f; } else { List <Assimp.EmbeddedTexture> texts = scene.Textures; Assimp.EmbeddedTexture diffuseTexture = texts.Find(x => x.Filename == tslot.FilePath); if (diffuseTexture != null) { byte[] data = diffuseTexture.CompressedData; texture.LoadImage(data); useColorMap = 1f; } } }
private void SetNormalMapMaterialTextures(Material material, Assimp.Material assimpMat) { Assimp.TextureSlot diffuseMap = assimpMat.GetTexture(Assimp.TextureType.Diffuse, 0); Assimp.TextureSlot normalMap = assimpMat.GetTexture(Assimp.TextureType.Normals, 0); Assimp.TextureSlot specularMap = assimpMat.GetTexture(Assimp.TextureType.Specular, 0); if (!String.IsNullOrEmpty(diffuseMap.FilePath)) { Texture diffuse; try { diffuse = m_content.LoadRelativeTo <Texture2D>(diffuseMap.FilePath, m_modelResource); //m_content.Load<Texture2D>(diffuseMap.FilePath); material.SetParameter("DiffuseMap", diffuse); } catch { // diffuse = m_content.Load<Texture2D>("notexture.tebo"); } } if (!String.IsNullOrEmpty(normalMap.FilePath)) { try { Texture normal = m_content.LoadRelativeTo <Texture2D>(normalMap.FilePath, m_modelResource); //m_content.Load<Texture2D>(normalMap.FilePath); material.SetParameter("NormalMap", normal); } catch { //Default normal map? } } if (!String.IsNullOrEmpty(specularMap.FilePath)) { try { Texture specular = m_content.LoadRelativeTo <Texture2D>(specularMap.FilePath, m_modelResource);//m_content.Load<Texture2D>(specularMap.FilePath); material.SetParameter("SpecularMap", specular); } catch { //Default specular map? } } }
private static void SetTexture(MaterialContent dstMaterial, string slot, Assimp.TextureSlot srcSlot) { var dstChannel = dstMaterial.UseChannel(slot); dstChannel.VertexIndexSet = srcSlot.UVIndex; }
private IEnumerator ImportMaterials() { int i = 0; Material opaqueMat = Resources.Load <Material>("Materials/ObjectOpaque"); Material transpMat = Resources.Load <Material>("Materials/ObjectTransparent"); foreach (Assimp.Material assimpMaterial in scene.Materials) { if (assimpMaterial.HasOpacity && assimpMaterial.Opacity < 0.99f) { materials.Add(new Material(transpMat)); } else { materials.Add(new Material(opaqueMat)); } var material = materials[i]; material.enableInstancing = true; material.SetFloat("_Metallic", 0.0f); material.SetFloat("_Roughness", 0.8f); if (assimpMaterial.IsTwoSided) { // does not work... material.SetInt("_DoubleSidedEnable", 1); material.EnableKeyword("_DOUBLESIDED_ON"); } material.name = assimpMaterial.Name; if (assimpMaterial.HasColorDiffuse) { Color baseColor = new Color(assimpMaterial.ColorDiffuse.R, assimpMaterial.ColorDiffuse.G, assimpMaterial.ColorDiffuse.B, assimpMaterial.ColorDiffuse.A); material.SetColor("_BaseColor", baseColor); } if (assimpMaterial.HasOpacity && assimpMaterial.Opacity < 1.0f) { material.SetFloat("_Opacity", assimpMaterial.Opacity); } if (assimpMaterial.HasTextureDiffuse) { Assimp.TextureSlot tslot = assimpMaterial.TextureDiffuse; string fullpath = Path.IsPathRooted(tslot.FilePath) ? tslot.FilePath : directoryName + "\\" + tslot.FilePath; if (File.Exists(fullpath)) { Texture2D texture = GetOrCreateTextureFromFile(fullpath); material.SetFloat("_UseColorMap", 1f); material.SetTexture("_ColorMap", texture); } else { Debug.LogError("File not found : " + tslot.FilePath); } } i++; if (!blocking) { yield return(null); } } }
private IEnumerator ImportMaterials() { int i = 0; Material opaqueMat = Resources.Load <Material>("Materials/ObjectOpaque"); Material transpMat = Resources.Load <Material>("Materials/ObjectTransparent"); foreach (Assimp.Material assimpMaterial in scene.Materials) { GlobalState.Instance.messageBox.ShowMessage("Importing Materials : " + i + " / " + scene.MaterialCount); if (assimpMaterial.HasOpacity && !assimpMaterial.HasColorTransparent) { materials.Add(new Material(transpMat)); } else { materials.Add(new Material(opaqueMat)); } var material = materials[i]; material.enableInstancing = true; material.SetFloat("_Metallic", 0.0f); material.SetFloat("_Roughness", 0.8f); if (assimpMaterial.IsTwoSided) { // does not work... material.SetInt("_DoubleSidedEnable", 1); material.EnableKeyword("_DOUBLESIDED_ON"); } material.name = assimpMaterial.Name; if (assimpMaterial.HasColorDiffuse) { Color baseColor = new Color(assimpMaterial.ColorDiffuse.R, assimpMaterial.ColorDiffuse.G, assimpMaterial.ColorDiffuse.B, assimpMaterial.ColorDiffuse.A); material.SetColor("_BaseColor", baseColor); } if (assimpMaterial.HasTextureOpacity) { Assimp.TextureSlot tslot = assimpMaterial.TextureOpacity; GetTexture(tslot, out Texture2D texture, out float useMap); material.SetFloat("_UseOpacityMap", useMap); material.SetTexture("_OpacityMap", texture); } if (assimpMaterial.HasOpacity && assimpMaterial.Opacity < 1.0f) { material.SetFloat("_Opacity", assimpMaterial.Opacity); } if (assimpMaterial.HasTextureDiffuse) { //Assimp.TextureSlot[] slots = assimpMaterial.GetAllMaterialTextures(); Assimp.TextureSlot tslot = assimpMaterial.TextureDiffuse; GetTexture(tslot, out Texture2D texture, out float useMap); material.SetFloat("_UseColorMap", useMap); material.SetTexture("_ColorMap", texture); } if (assimpMaterial.HasTextureNormal) { Assimp.TextureSlot tslot = assimpMaterial.TextureNormal; GetTexture(tslot, out Texture2D texture, out float useMap); material.SetFloat("_UseNormalMap", useMap); material.SetTexture("_NormalMap", texture); } if (assimpMaterial.HasTextureEmissive) { Assimp.TextureSlot tslot = assimpMaterial.TextureEmissive; GetTexture(tslot, out Texture2D texture, out float useMap); material.SetFloat("_UseEmissiveMap", useMap); material.SetTexture("_EmissiveMap", texture); } i++; if (!blocking) { yield return(null); } } }