using System; using System.Collections.Generic; using System.IO; namespace AssetStudio { public interface IImported { ImportedFrame RootFrame { get; } List MeshList { get; } List MaterialList { get; } List TextureList { get; } List AnimationList { get; } List MorphList { get; } } public class ImportedFrame { public string Name { get; set; } public Vector3 LocalRotation { get; set; } public Vector3 LocalPosition { get; set; } public Vector3 LocalScale { get; set; } public ImportedFrame Parent { get; set; } private List children; public ImportedFrame this[int i] => children[i]; public int Count => children.Count; public string Path { get { var frame = this; var path = frame.Name; while (frame.Parent != null) { frame = frame.Parent; path = frame.Name + "/" + path; } return path; } } public ImportedFrame(int childrenCount = 0) { children = new List(childrenCount); } public void AddChild(ImportedFrame obj) { children.Add(obj); obj.Parent?.Remove(obj); obj.Parent = this; } public void Remove(ImportedFrame frame) { children.Remove(frame); } public ImportedFrame FindFrameByPath(string path) { var name = path.Substring(path.LastIndexOf('/') + 1); foreach (var frame in FindChilds(name)) { if (frame.Path.EndsWith(path, StringComparison.Ordinal)) { return frame; } } return null; } public ImportedFrame FindFrame(string name) { if (Name == name) { return this; } foreach (var child in children) { var frame = child.FindFrame(name); if (frame != null) { return frame; } } return null; } public ImportedFrame FindRelativeFrameWithPath(string path) { var subs = path.Split(new[] { '/' }, 2); foreach (var child in children) { if (child.Name == subs[0]) { if (subs.Length == 1) { return child; } else { var result = child.FindRelativeFrameWithPath(subs[1]); if (result != null) return result; } } } return null; } public ImportedFrame FindChild(string name, bool recursive = true) { foreach (var child in children) { if (recursive) { var frame = child.FindFrame(name); if (frame != null) { return frame; } } else { if (child.Name == name) { return child; } } } return null; } public IEnumerable FindChilds(string name) { if (Name == name) { yield return this; } foreach (var child in children) { foreach (var item in child.FindChilds(name)) { yield return item; } } } } public class ImportedMesh { public string Path { get; set; } public List VertexList { get; set; } public List SubmeshList { get; set; } public List BoneList { get; set; } public bool hasNormal { get; set; } public bool[] hasUV { get; set; } public bool hasTangent { get; set; } public bool hasColor { get; set; } } public class ImportedSubmesh { public List FaceList { get; set; } public string Material { get; set; } public int BaseVertex { get; set; } } public class ImportedVertex { public Vector3 Vertex { get; set; } public Vector3 Normal { get; set; } public float[][] UV { get; set; } public Vector4 Tangent { get; set; } public Color Color { get; set; } public float[] Weights { get; set; } public int[] BoneIndices { get; set; } } public class ImportedFace { public int[] VertexIndices { get; set; } } public class ImportedBone { public string Path { get; set; } public Matrix4x4 Matrix { get; set; } } public class ImportedMaterial { public string Name { get; set; } public Color Diffuse { get; set; } public Color Ambient { get; set; } public Color Specular { get; set; } public Color Emissive { get; set; } public Color Reflection { get; set; } public float Shininess { get; set; } public float Transparency { get; set; } public List Textures { get; set; } } public class ImportedMaterialTexture { public string Name { get; set; } public int Dest { get; set; } public Vector2 Offset { get; set; } public Vector2 Scale { get; set; } } public class ImportedTexture { public string Name { get; set; } public byte[] Data { get; set; } public ImportedTexture(MemoryStream stream, string name) { Name = name; Data = stream.ToArray(); } } public class ImportedKeyframedAnimation { public string Name { get; set; } public float SampleRate { get; set; } public List TrackList { get; set; } public ImportedAnimationKeyframedTrack FindTrack(string path) { var track = TrackList.Find(x => x.Path == path); if (track == null) { track = new ImportedAnimationKeyframedTrack { Path = path }; TrackList.Add(track); } return track; } } public class ImportedAnimationKeyframedTrack { public string Path { get; set; } public List> Scalings = new List>(); public List> Rotations = new List>(); public List> Translations = new List>(); public ImportedBlendShape BlendShape; } public class ImportedKeyframe { public float time { get; set; } public T value { get; set; } public ImportedKeyframe(float time, T value) { this.time = time; this.value = value; } } public class ImportedBlendShape { public string ChannelName; public List> Keyframes = new List>(); } public class ImportedMorph { public string Path { get; set; } public List Channels { get; set; } } public class ImportedMorphChannel { public string Name { get; set; } public List KeyframeList { get; set; } } public class ImportedMorphKeyframe { public bool hasNormals { get; set; } public bool hasTangents { get; set; } public float Weight { get; set; } public List VertexList { get; set; } } public class ImportedMorphVertex { public uint Index { get; set; } public ImportedVertex Vertex { get; set; } } public static class ImportedHelpers { public static ImportedMesh FindMesh(string path, List importedMeshList) { foreach (var mesh in importedMeshList) { if (mesh.Path == path) { return mesh; } } return null; } public static ImportedMaterial FindMaterial(string name, List importedMats) { foreach (var mat in importedMats) { if (mat.Name == name) { return mat; } } return null; } public static ImportedTexture FindTexture(string name, List importedTextureList) { if (string.IsNullOrEmpty(name)) { return null; } foreach (var tex in importedTextureList) { if (tex.Name == name) { return tex; } } return null; } } }