/// <summary> /// Try to add an image to the result content /// </summary> /// <param name="controllerName">Controller name</param> /// <param name="res">Result content</param> private void TryAddImage(string textureName, ref ModelContent res) { if (string.IsNullOrWhiteSpace(textureName)) { return; } if (res.Images.ContainsKey(textureName)) { return; } res.Images.Add(textureName, this.Images[textureName]); }
/// <summary> /// Try to add a skin to the result content /// </summary> /// <param name="controllerName">Controller name</param> /// <param name="res">Result content</param> private void TryAddSkin(string armatureName, ref ModelContent res) { if (string.IsNullOrWhiteSpace(armatureName)) { return; } if (res.SkinningInfo.ContainsKey(armatureName)) { return; } var s = this.SkinningInfo[armatureName]; res.SkinningInfo.Add(armatureName, s); //Add animations var animations = this.Animations.Where(g => { return(s.Skeleton.GetJointNames() .Any(j => g.Key.StartsWith($"{j}_pose_matrix", StringComparison.OrdinalIgnoreCase))); }); if (animations.Any()) { foreach (var a in animations) { res.Animations.Add(a.Key, a.Value); } } var clips = this.Animations.Definition.Clips .Where(c => string.Equals(armatureName, c.Skeleton, StringComparison.OrdinalIgnoreCase)); if (clips.Any()) { if (res.Animations.Definition == null) { res.Animations.Definition = new AnimationDescription(); } res.Animations.Definition.Clips.AddRange(clips); } }
/// <summary> /// Loads a model content list from resources /// </summary> /// <param name="contentFolder">Content folder</param> /// <param name="fileName">File name</param> /// <param name="transform">Transform</param> /// <returns>Returns a list of model contents</returns> private IEnumerable <ModelContent> Load(string contentFolder, string fileName, Matrix transform) { var modelList = ContentManager.FindContent(contentFolder, fileName); if (modelList.Any()) { List <ModelContent> res = new List <ModelContent>(); foreach (var model in modelList) { LoadObj(model, transform, out var vertices, out var indices); res.Add(ModelContent.GenerateTriangleList(vertices, indices)); } return(res.ToArray()); } else { throw new EngineException(string.Format("Model not found: {0}", fileName)); } }
/// <summary> /// Try to add the controller to the result content /// </summary> /// <param name="controllerName">Controller name</param> /// <param name="res">Result content</param> private void TryAddController(string controllerName, ref ModelContent res) { if (string.IsNullOrWhiteSpace(controllerName)) { return; } if (res.SkinningInfo.ContainsKey(controllerName)) { return; } var c = this.Controllers[controllerName]; res.Controllers.Add(controllerName, c); //Add skins TryAddSkin(c.Armature, ref res); //Add meshes TryAddGeometry(c.Skin, ref res); }
/// <summary> /// Creates a new content filtering with the specified geometry name /// </summary> /// <param name="geometryName">Geometry name</param> /// <returns>Returns a new content instance with the referenced geometry, materials, images, ...</returns> public ModelContent Filter(string geometryName) { var geo = this.Geometry.Where(g => string.Equals(g.Key, geometryName + "-mesh", StringComparison.OrdinalIgnoreCase)); if (geo.Any()) { var res = new ModelContent { Images = this.Images, Materials = this.Materials, }; foreach (var g in geo) { res.Geometry.Add(g.Key, g.Value); } return(res); } return(null); }
/// <summary> /// Try to add geometry to the result content /// </summary> /// <param name="controllerName">Controller name</param> /// <param name="res">Result content</param> private void TryAddGeometry(string meshName, ref ModelContent res) { if (string.IsNullOrWhiteSpace(meshName)) { return; } if (res.Geometry.ContainsKey(meshName)) { return; } var g = this.Geometry[meshName]; res.Geometry.Add(meshName, g); foreach (var sm in g.Values) { //Add materials TryAddMaterial(sm.Material, ref res); } }
/// <summary> /// Generates a new model content from an height map /// </summary> /// <param name="contentFolder">Content folder</param> /// <param name="heightMap">Height map</param> /// <param name="textures">Texture list</param> /// <param name="cellSize">Cell size</param> /// <param name="cellHeight">Cell height</param> /// <returns>Returns a new model content</returns> public static ModelContent FromHeightmap(string contentFolder, string heightMap, IEnumerable <string> textures, float cellSize, float cellHeight) { ModelContent modelContent = new ModelContent(); string texureName = "texture"; string materialName = "material"; string geoName = "geometry"; ImageContent heightMapImage = new ImageContent() { Streams = ContentManager.FindContent(contentFolder, heightMap), }; ImageContent textureImage = new ImageContent() { Streams = ContentManager.FindContent(contentFolder, textures), }; MaterialContent material = MaterialContent.Default; material.DiffuseTexture = texureName; HeightMap hm = HeightMap.FromStream(heightMapImage.Stream, null); hm.BuildGeometry(cellSize, cellHeight, out var vertices, out var indices); SubMeshContent geo = new SubMeshContent(Topology.TriangleList, materialName, true, false); geo.SetVertices(vertices); geo.SetIndices(indices); modelContent.Images.Add(texureName, textureImage); modelContent.Materials.Add(materialName, material); modelContent.Geometry.Add(geoName, materialName, geo); return(modelContent); }
/// <summary> /// Creates a new content filtering with the specified geometry name mask list /// </summary> /// <param name="masks">Name mask list</param> /// <returns>Returns a new content instance with the referenced geometry, materials, images, ...</returns> public ModelContent FilterMask(IEnumerable <string> masks) { ModelContent res = null; if (masks?.Any() == true) { foreach (var mask in masks) { if (string.IsNullOrWhiteSpace(mask)) { continue; } if (FilterMaskByController(mask, ref res)) { continue; } FilterMaskByMesh(mask, ref res); } } return(res); }
/// <summary> /// Generate model content from scratch /// </summary> /// <param name="topology">Topology</param> /// <param name="vertices">Vertex list</param> /// <param name="indices">Index list</param> /// <param name="material">Material</param> /// <returns>Returns new model content</returns> private static ModelContent Generate(Topology topology, IEnumerable <VertexData> vertices, IEnumerable <uint> indices, MaterialContent material = null) { ModelContent modelContent = new ModelContent(); if (material != null) { modelContent.Images.Import(ref material); modelContent.Materials.Add(ModelContent.DefaultMaterial, material); } var materialName = material != null ? ModelContent.DefaultMaterial : ModelContent.NoMaterial; var textured = modelContent.Materials[materialName].DiffuseTexture != null; SubMeshContent geo = new SubMeshContent(topology, materialName, textured, false); geo.SetVertices(vertices); geo.SetIndices(indices); modelContent.Geometry.Add(ModelContent.StaticMesh, material != null ? ModelContent.DefaultMaterial : ModelContent.NoMaterial, geo); modelContent.Optimize(); return(modelContent); }
/// <summary> /// Try to add the lights to the result content /// </summary> /// <param name="controllerName">Controller name</param> /// <param name="res">Result content</param> private void TryAddLights(string mask, ref ModelContent res) { if (string.IsNullOrWhiteSpace(mask)) { return; } var lights = this.Lights.Where(l => l.Key.StartsWith(mask, StringComparison.OrdinalIgnoreCase) && l.Key.EndsWith("-light", StringComparison.OrdinalIgnoreCase)); if (lights.Any()) { foreach (var l in lights) { if (res.Lights.ContainsKey(l.Key)) { continue; } res.Lights.Add(l.Key, l.Value); } } }
/// <summary> /// Try to add materials to the result content /// </summary> /// <param name="controllerName">Controller name</param> /// <param name="res">Result content</param> private void TryAddMaterial(string materialName, ref ModelContent res) { if (string.IsNullOrWhiteSpace(materialName)) { return; } if (res.Materials.ContainsKey(materialName)) { return; } var mat = this.Materials[materialName]; res.Materials.Add(materialName, mat); //Add textures TryAddImage(mat.AmbientTexture, ref res); TryAddImage(mat.DiffuseTexture, ref res); TryAddImage(mat.EmissionTexture, ref res); TryAddImage(mat.NormalMapTexture, ref res); TryAddImage(mat.ReflectiveTexture, ref res); TryAddImage(mat.SpecularTexture, ref res); }