/// <summary> /// Loads the shader as an asset. /// </summary> /// <param name="context"></param> /// <param name="stream"></param> /// <param name="extensionHint"></param> protected override void Load(AssetLoadContext context, System.IO.Stream stream, string extensionHint) { using (var reader = new System.IO.StreamReader(stream)) { this.Load(reader.ReadToEnd()); } }
/// <summary> /// Loads the model. /// </summary> protected override void Load(AssetLoadContext context, Stream stream, string extensionHint) { using (var importer = new AssimpImporter()) { var scene = importer.ImportFileFromStream(stream, postProcessing, extensionHint); this.Load(context, scene); } }
/// <summary> /// Loads the blob. /// </summary> protected override void Load(AssetLoadContext manager, Stream stream, string extensionHint) { if (stream == null) { throw new ArgumentNullException("stream"); } using (var memory = new MemoryStream()) { stream.CopyTo(memory); this.data = memory.ToArray(); } }
private static Texture2D LoadTexture(AssetLoadContext context, TextureSlot slot) { if (string.IsNullOrWhiteSpace(slot.FilePath)) { return(null); } string textureFilePath = Path.GetDirectoryName(slot.FilePath) + "/" + Path.GetFileNameWithoutExtension(slot.FilePath); if (Path.IsPathRooted(textureFilePath)) { textureFilePath = Path.GetFileNameWithoutExtension(slot.FilePath); } return(context.AssetManager.Load <Texture2D>(context.Directory + textureFilePath)); }
/// <summary> /// Loads the localization definition. /// </summary> protected override void Load(AssetLoadContext context, System.IO.Stream stream, string extensionHint) { var culture = CultureInfo.CurrentCulture; Log.WriteLine("Loading localization for {0} - {1}", culture.ThreeLetterISOLanguageName, culture.EnglishName); XmlDocument doc = new XmlDocument(); doc.Load(stream); this.translations.Clear(); foreach (XmlElement localized in doc.GetElementsByTagName("localized")) { string id = localized.GetAttribute("id"); if (string.IsNullOrWhiteSpace(id)) { Log.WriteLine("Localization {0} missing an element id.", context.Name); continue; } XmlElement translation = localized[culture.ThreeLetterISOLanguageName]; if (translation == null) { Log.WriteLine("Localization {0}.{1} is missing a translation. Using default translation.", context.Name, id); this.translations.Add(id, "<" + id + ">"); } else { this.translations.Add(id, translation.InnerText); } } // TODO: Set properties foreach (var property in this.GetType().GetProperties()) { if (!property.CanWrite) { continue; } var attribs = (IDAttribute[])property.GetCustomAttributes(typeof(IDAttribute), false); if (attribs.Length != 1) { continue; } property.SetValue(this, this[attribs[0].ID], new object[0]); } }
/// <summary> /// Loads a new asset synchronous /// </summary> /// <typeparam name="T">Type of the asset to be loaded</typeparam> /// <param name="context">The load context.</param> /// <param name="stream">The stream that is used for loading the asset</param> /// <param name="extensionHint">The extension of the asset to be loaded.</param> /// <remarks>The asset is not instantly loaded. Wait for Asset.IsLoaded to be true.</remarks> /// <returns>New asset.</returns> public static T LoadSync <T>(AssetLoadContext context, Stream stream, string extensionHint) where T : Asset { T a = Activator.CreateInstance <T>(); a.IsLoading = true; a.Load(context, stream, extensionHint); a.IsLoading = false; a.IsLoaded = true; if (stream != null) { stream.Close(); } if (a.Loaded != null) { a.Loaded(a, EventArgs.Empty); } return(a); }
// +X, +Y, +Z, -X, -Y und -Z /// <summary> /// Loads the cube texture. /// </summary> /// <param name="manager"></param> /// <param name="stream"></param> /// <param name="extensionHint"></param> protected override void Load(AssetLoadContext manager, System.IO.Stream stream, string extensionHint) { using (var bmp = new Bitmap(stream)) { this.Bind(); LoadSide(bmp, 0, TextureTarget.TextureCubeMapPositiveX); LoadSide(bmp, 1, TextureTarget.TextureCubeMapPositiveY); LoadSide(bmp, 2, TextureTarget.TextureCubeMapPositiveZ); LoadSide(bmp, 3, TextureTarget.TextureCubeMapNegativeX); LoadSide(bmp, 4, TextureTarget.TextureCubeMapNegativeY); LoadSide(bmp, 5, TextureTarget.TextureCubeMapNegativeZ); } OpenGL.Invoke(() => { this.Bind(); GL.TexParameter(this.Target, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear); GL.TexParameter(this.Target, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear); }); }
/// <summary> /// Loads the 2d texture. /// </summary> /// <param name="context"></param> /// <param name="stream"></param> /// <param name="extensionHint"></param> protected override void Load(AssetLoadContext context, Stream stream, string extensionHint) { if (extensionHint == ".dds") { Log.WriteLine(LocalizedStrings.LoadingUncompressedBitmapFromStream); OpenGL.Invoke(() => { if (LoadDDS(stream) != this.Target) { throw new InvalidDataException("Could not load texture: Invalid texture format."); } }); } else { Log.WriteLine(LocalizedStrings.LoadingUncompressedBitmapFromStream); using (var bmp = new Bitmap(stream)) this.Load(bmp); } this.Filter = Filter.LinearMipMapped; }
/// <summary> /// Loads the asset. /// </summary> /// <param name="context">The loading context.</param> /// <param name="stream">The stream that contains the asset.</param> /// <param name="extensionHint">Contains the extension of the asset file.</param> protected abstract void Load(AssetLoadContext context, Stream stream, string extensionHint);
private void Load(AssetLoadContext context, Scene scene) { Node[] nodes = new[] { scene.RootNode }; if (scene.RootNode.ChildCount > 0) { nodes = scene.RootNode.Children; } List <ModelMesh> meshList = new List <ModelMesh>(); for (int i = 0; i < nodes.Length; i++) { Node node = nodes[i]; if (!node.HasMeshes) { continue; } for (int j = 0; j < node.MeshCount; j++) { Mesh mesh = scene.Meshes[node.MeshIndices[j]]; Vertex[] vertices = new Vertex[mesh.VertexCount]; Assimp.Material assimpMaterial = null; Texture2D diffuseTexture = null; Texture2D specularTexture = null; Texture2D emissiveTexture = null; Texture2D normalMap = null; if (scene.HasMaterials && mesh.MaterialIndex >= 0) { assimpMaterial = scene.Materials[mesh.MaterialIndex]; diffuseTexture = LoadTexture(context, assimpMaterial.GetTexture(Assimp.TextureType.Diffuse, 0)); specularTexture = LoadTexture(context, assimpMaterial.GetTexture(Assimp.TextureType.Specular, 0)); emissiveTexture = LoadTexture(context, assimpMaterial.GetTexture(Assimp.TextureType.Emissive, 0)); bool usedSRGB = Texture.UseSRGB; Texture.UseSRGB = false; // We need "default" texture loading because normal maps are already linear space normalMap = LoadTexture(context, assimpMaterial.GetTexture(Assimp.TextureType.Normals, 0)); Texture.UseSRGB = usedSRGB; } Vector3D[] texcoord0 = null; Vector3D[] texcoord1 = null; if (mesh.HasTextureCoords(0)) { texcoord0 = mesh.GetTextureCoords(0); } if (mesh.HasTextureCoords(1)) { texcoord1 = mesh.GetTextureCoords(1); } uint[] indices = mesh.GetIndices(); for (int k = 0; k < mesh.VertexCount; k++) { Vertex vertex = new Vertex(); vertex.Position = new Vector3( mesh.Vertices[k].X, mesh.Vertices[k].Y, mesh.Vertices[k].Z); if (mesh.HasNormals) { vertex.Normal = new Vector3( mesh.Normals[k].X, mesh.Normals[k].Y, mesh.Normals[k].Z); } if (texcoord0 != null) { vertex.UV = new Vector2( texcoord0[k].X, texcoord0[k].Y); } if (texcoord1 != null) { vertex.UV2 = new Vector2( texcoord1[k].X, texcoord1[k].Y); } if (mesh.HasTangentBasis) { vertex.Tangent = new Vector3( mesh.Tangents[k].X, mesh.Tangents[k].Y, mesh.Tangents[k].Z); vertex.BiTangent = new Vector3( mesh.BiTangents[k].X, mesh.BiTangents[k].Y, mesh.BiTangents[k].Z); } vertices[k] = vertex; } OpenGL.Invoke(() => { ModelMesh modelMesh = new ModelMesh(indices, vertices); modelMesh.DiffuseTexture = diffuseTexture; modelMesh.SpecularTexture = specularTexture; modelMesh.EmissiveTexture = emissiveTexture; modelMesh.NormalMap = normalMap; meshList.Add(modelMesh); }); } } this.meshes = meshList.ToArray(); }