forked from AerysBat/XNALara
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathModel.cs
161 lines (139 loc) · 5.39 KB
/
Model.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
using System.Collections.Generic;
using Microsoft.Xna.Framework.Graphics;
namespace XNALara
{
public class Model
{
private GraphicsDevice graphicsDevice;
private TextureManager textureManager;
private Armature armature;
private bool isVisible = true;
private Dictionary<string, MeshDesc> meshDescDict = new Dictionary<string, MeshDesc>();
private Dictionary<string, Mesh> meshDict = new Dictionary<string, Mesh>();
private Dictionary<string, List<Mesh>> groupDict = new Dictionary<string, List<Mesh>>();
private Dictionary<string, object> visibleGroups = new Dictionary<string, object>();
private static List<Mesh> emptyGroup = new List<Mesh>();
public Model(Game game) {
graphicsDevice = game.GraphicsDevice;
textureManager = game.TextureManager;
}
public Armature Armature {
set { armature = value; }
get { return armature; }
}
public bool IsVisible {
set { isVisible = value; }
get { return isVisible; }
}
public ICollection<MeshDesc> MeshDescs {
get { return meshDescDict.Values; }
}
public ICollection<Mesh> Meshes {
get { return meshDict.Values; }
}
public void AddMeshDesc(MeshDesc meshDesc) {
meshDescDict[meshDesc.name] = meshDesc;
}
public void RemoveMeshDesc(string meshName) {
meshDescDict.Remove(meshName);
}
public Mesh InitMesh(string meshName, VertexFormat vertexFormat) {
MeshDesc meshDesc = meshDescDict[meshName];
Mesh mesh = new Mesh(graphicsDevice, textureManager, meshDesc, vertexFormat);
meshDict[meshName] = mesh;
return mesh;
}
public void InitMeshes(VertexFormat vertexFormat) {
foreach (KeyValuePair<string, MeshDesc> entry in meshDescDict) {
string meshName = entry.Key;
MeshDesc meshDesc = entry.Value;
Mesh mesh = new Mesh(graphicsDevice, textureManager, meshDesc, vertexFormat);
meshDict[meshName] = mesh;
}
}
public MeshDesc GetMeshDesc(string meshName) {
MeshDesc meshDesc;
if (!meshDescDict.TryGetValue(meshName, out meshDesc)) {
return null;
}
return meshDesc;
}
public Mesh GetMesh(string meshName) {
Mesh mesh;
if (!meshDict.TryGetValue(meshName, out mesh)) {
return null;
}
return mesh;
}
public void DefineMeshGroup(string groupName, params string[] meshNames) {
List<Mesh> group = new List<Mesh>();
foreach (string meshName in meshNames) {
Mesh mesh;
if (!meshDict.TryGetValue(meshName, out mesh)) {
//throw new DictionaryKeyNotFoundException(meshName);
continue;
}
group.Add(mesh);
}
groupDict[groupName] = group;
visibleGroups[groupName] = null;
}
public ICollection<string> GetMeshGroupNames() {
return groupDict.Keys;
}
public List<Mesh> GetMeshGroup(string groupName) {
List<Mesh> group;
if (!groupDict.TryGetValue(groupName, out group)) {
return emptyGroup;
}
return group;
}
public void SetMeshGroupVisible(string groupName, bool isVisible) {
if (!groupDict.ContainsKey(groupName)) {
return;
}
List<Mesh> meshes = GetMeshGroup(groupName);
foreach (Mesh mesh in meshes) {
mesh.IsVisible = isVisible;
}
if (isVisible) {
visibleGroups[groupName] = null;
}
else {
visibleGroups.Remove(groupName);
}
}
public bool IsMeshGroupVisible(string groupName) {
return visibleGroups.ContainsKey(groupName);
}
public Dictionary<Texture2D, Texture2D> ReloadTextures() {
Dictionary<Texture2D, Texture2D> mapping = new Dictionary<Texture2D, Texture2D>();
foreach (Mesh mesh in meshDict.Values) {
Texture2D[] textures = mesh.Textures;
for (int i = 0; i < textures.Length; i++) {
Texture2D oldTexture = textures[i];
if (oldTexture == null) {
continue;
}
Texture2D newTexture = null;
if (!mapping.TryGetValue(oldTexture, out newTexture)) {
newTexture = textureManager.ReloadTexture(oldTexture);
mapping[oldTexture] = newTexture;
}
textures[i] = newTexture;
}
}
return mapping;
}
public void ReplaceTexture(Texture2D oldTexture, Texture2D newTexture) {
foreach (Mesh mesh in meshDict.Values) {
Texture2D[] textures = mesh.Textures;
for (int i = 0; i < textures.Length; i++) {
if (textures[i] == oldTexture) {
textures[i] = newTexture;
}
}
}
}
}
}