/// <summary> /// Add a texture to the TextureSet. This is the only place where /// Texture instances are actually created. The actual image for textures /// added to the set is loaded using a background thread. /// </summary> /// <param name="path">Texture's given path or, in case an embedded data source /// is specified for the texture, an arbitrary but unique value to identify /// the texture (preferably not a file path) /// </param> /// <param name="embeddedDataSource">Optional parameter that specifies /// an in-memory, embedded data source for the texture. </param> public void Add(string path, Assimp.EmbeddedTexture embeddedDataSource = null) { if (_dict.ContainsKey(path)) { return; } Texture.CompletionCallback closure = self => { lock (_loaded) { if (_disposed) { return; } Debug.Assert(_dict.ContainsKey(path)); _loaded.Add(path); // If this texture is being used as replacement for another texture, // we need to invoke callbacks for its ID too // TODO obviously, all the replacement code needs a re-design. foreach (var kv in _replacements) { if (kv.Value.Value == path) { for (int i = 0, e = _textureCallbacks.Count; i < e;) { var callback = _textureCallbacks[i]; if (!callback(kv.Value.Key, self)) { _textureCallbacks.RemoveAt(i); --e; continue; } ++i; } } } for (int i = 0, e = _textureCallbacks.Count; i < e;) { var callback = _textureCallbacks[i]; if (!callback(path, self)) { _textureCallbacks.RemoveAt(i); --e; continue; } ++i; } } }; _dict.Add(path, embeddedDataSource == null ? new Texture(path, _baseDir, closure) : new Texture(embeddedDataSource, path, closure)); }
private void LoadTexturesFromScene(Assimp.Scene scene, string model_directory, Arguments cmdargs) { foreach (Assimp.Mesh mesh in scene.Meshes) { Console.Write(mesh.Name); Assimp.Material mat = scene.Materials[mesh.MaterialIndex]; if (mat.HasTextureDiffuse) { string texname = System.IO.Path.GetFileNameWithoutExtension(mat.TextureDiffuse.FilePath); bool isEmbedded = false; int embeddedIndex = -1; if (mat.TextureDiffuse.FilePath.StartsWith("*")) { string index = mat.TextureDiffuse.FilePath.Substring(1, mat.TextureDiffuse.FilePath.Length);; isEmbedded = int.TryParse(index, out embeddedIndex); texname = String.Format("embedded_tex{0}", embeddedIndex); } bool already_exists = false; foreach (BinaryTextureImage image in Textures) { if (image.Name == texname) { already_exists = true; break; } } if (already_exists) { continue; } BinaryTextureImage img = new BinaryTextureImage(); if (isEmbedded) { Assimp.EmbeddedTexture embeddedTexture = scene.Textures[embeddedIndex]; img.Load(mat.TextureDiffuse, embeddedTexture); } else { img.Load(mat.TextureDiffuse, model_directory, cmdargs.readMipmaps); } Textures.Add(img); } else { Console.WriteLine(" -> Has No Textures"); } } }
private static Texture LoadTexture(Scene scene, TextureSlot textureSlot) { Texture diffuse; // For FBX, textures can be embedded and have a filepath value in the format * followed by a number which is the index for the texture in the scene if (Regex.IsMatch(textureSlot.FilePath, @"\*\d+")) { int embeddedTextureIndex = int.Parse(textureSlot.FilePath.TrimStart('*')); Assimp.EmbeddedTexture tex = scene.Textures[embeddedTextureIndex]; diffuse = Texture.LoadTextureFromEmbeddedTexture(tex); } else { diffuse = Texture.LoadTextureFromPath("Assets\\" + Path.GetFileName(textureSlot.FilePath)); } return(diffuse); }