forked from AerysBat/XNALara
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBoneSelector.cs
242 lines (213 loc) · 9.36 KB
/
BoneSelector.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace XNALara
{
public class BoneSelector
{
private const double BoneProximityThreshold = 30.0;
private Game game;
private GraphicsDevice graphicsDevice;
private List<Model> models = new List<Model>();
private CameraTurnTable camera;
private BasicEffect basicEffect;
private VertexDeclaration vertexDeclaration;
private Matrix viewMatrix;
private Matrix projectionMatrix;
private int screenWidth;
private int screenHeight;
private List<Armature.Bone> activeBones = new List<Armature.Bone>();
private List<Armature.Bone> visibleBones = new List<Armature.Bone>();
private List<Vector3> visibleBonesProjections = new List<Vector3>();
private int renderedBonesCount;
private int selectedBoneIndex = -1;
private SpriteBatch spriteBatch;
private SpriteFont font;
public BoneSelector(Game game) {
this.game = game;
this.graphicsDevice = game.GraphicsDevice;
this.camera = game.Camera;
basicEffect = new BasicEffect(graphicsDevice, null);
vertexDeclaration = new VertexDeclaration(graphicsDevice, VertexPositionColor.VertexElements);
viewMatrix = Matrix.CreateLookAt(Vector3.UnitZ, Vector3.Zero, Vector3.Up);
camera.CameraEvent += HandleCameraEvent;
HandleWindowSizeChanged(graphicsDevice.Viewport.Width, graphicsDevice.Viewport.Height);
spriteBatch = new SpriteBatch(game.GraphicsDevice);
try {
font = game.Content.Load<SpriteFont>("BoneSelectorFont");
}
catch (Exception ex) {
string currDir = Directory.GetCurrentDirectory();
string contentDir = currDir + @"\Content";
string fontFile = contentDir + @"\BoneSelectorFont.spritefont";
MessageBox.Show(ex.Message + "\n\n" +
"Current directory: " + currDir + "\n" +
"folder \"Content\": " + (Directory.Exists(contentDir) ? "FOUND" : "NOT FOUND")+ "\n"+
"file \"Content\\BoneSelectorFont.spritefont\": " + (File.Exists(fontFile) ? "FOUND" : "NOT FOUND"),
"Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
Environment.Exit(0);
}
}
public void AddModel(Model model) {
models.Add(model);
Armature armature = model.Armature;
foreach (Armature.Bone bone in armature.Bones) {
if (bone.name.StartsWith("unused")) {
continue;
}
activeBones.Add(bone);
if (model.IsVisible) {
visibleBones.Add(bone);
visibleBonesProjections.Add(Vector3.Zero);
}
}
armature.ArmatureEvent += HandleArmatureEvent;
RecalculateBonePositions();
}
public void RemoveModel(Model model) {
models.Remove(model);
Armature armature = model.Armature;
int i = 0;
while (i < activeBones.Count) {
Armature.Bone bone = activeBones[i];
if (bone.armature == armature) {
activeBones.RemoveAt(i);
if (model.IsVisible) {
int j = visibleBones.IndexOf(bone);
visibleBones.RemoveAt(j);
visibleBonesProjections.RemoveAt(j);
}
}
else {
i++;
}
}
armature.ArmatureEvent -= HandleArmatureEvent;
selectedBoneIndex = -1;
}
public Armature.Bone SelectedBone {
get { return selectedBoneIndex < 0 ? null : visibleBones[selectedBoneIndex]; }
}
public void HandleModelVisibilityChanged(Model model) {
visibleBones.Clear();
visibleBonesProjections.Clear();
foreach (Armature.Bone bone in activeBones) {
if (!bone.armature.Model.IsVisible) {
continue;
}
visibleBones.Add(bone);
visibleBonesProjections.Add(Vector3.Zero);
}
selectedBoneIndex = -1;
RecalculateBonePositions();
}
public void HandleWindowSizeChanged(int width, int height) {
screenWidth = width;
screenHeight = height;
projectionMatrix = Matrix.CreateOrthographicOffCenter(0, screenWidth, 0, screenHeight, 1.0f, 100.0f);
RecalculateBonePositions();
}
public void HandleCameraEvent() {
RecalculateBonePositions();
}
public void HandleArmatureEvent() {
RecalculateBonePositions();
}
public void HandleMouseMoved(int mouseX, int mouseY) {
mouseY = screenHeight - 1 - mouseY;
double minDistance = double.PositiveInfinity;
selectedBoneIndex = -1;
for (int i = 0; i < visibleBonesProjections.Count; i++) {
Vector3 boneProj = visibleBonesProjections[i];
if (boneProj.Z < 0) {
continue;
}
double dx = boneProj.X - mouseX;
double dy = boneProj.Y - mouseY;
double distance = Math.Sqrt(dx * dx + dy * dy);
if (distance < BoneProximityThreshold && distance < minDistance) {
minDistance = distance;
selectedBoneIndex = i;
}
}
}
public void Render() {
if (selectedBoneIndex < 0 || renderedBonesCount == 0) {
return;
}
Color DefaultBoneColor = new Color(Color.White, 0.35f);
Armature.Bone selectedBone = null;
Vector3 selectedBoneProj = Vector3.Zero;
VertexPositionColor[] points = new VertexPositionColor[renderedBonesCount];
int j = 0;
for (int i = 0; i < visibleBones.Count; i++) {
Vector3 boneProj = visibleBonesProjections[i];
if (boneProj.Z < 0) {
continue;
}
Color color;
if (i == selectedBoneIndex) {
color = Color.Red;
selectedBone = visibleBones[i];
selectedBoneProj = boneProj;
}
else {
color = DefaultBoneColor;
}
points[j] = new VertexPositionColor(boneProj, color);
j++;
}
graphicsDevice.RenderState.PointSize = 5;
graphicsDevice.VertexDeclaration = vertexDeclaration;
graphicsDevice.RenderState.AlphaBlendEnable = true;
graphicsDevice.RenderState.SourceBlend = Blend.SourceAlpha;
graphicsDevice.RenderState.DestinationBlend = Blend.InverseSourceAlpha;
basicEffect.VertexColorEnabled = true;
basicEffect.World = Matrix.Identity;
basicEffect.View = viewMatrix;
basicEffect.Projection = projectionMatrix;
basicEffect.Begin();
foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes) {
pass.Begin();
graphicsDevice.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.PointList, points, 0, points.Length);
pass.End();
}
basicEffect.End();
graphicsDevice.RenderState.AlphaBlendEnable = false;
if (game.DisplayBoneNames && selectedBone != null) {
spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Deferred, SaveStateMode.SaveState);
int x = (int)Math.Round(selectedBoneProj.X);
int y = (int)Math.Round(screenHeight - 1 - selectedBoneProj.Y - font.LineSpacing - 3);
string label = selectedBone.name;
spriteBatch.DrawString(font, label, new Vector2(x + 1, y + 1), Color.Black);
spriteBatch.DrawString(font, label, new Vector2(x, y), Color.Yellow);
spriteBatch.End();
}
}
private void RecalculateBonePositions() {
Viewport viewport = graphicsDevice.Viewport;
Matrix projectionMatrix = camera.ProjectionMatrix;
Matrix viewMatrix = camera.ViewMatrix;
float cameraNear = camera.NearPlane;
renderedBonesCount = 0;
for (int i = 0; i < visibleBones.Count; i++) {
Armature.Bone bone = visibleBones[i];
Matrix worldMatrix = bone.armature.WorldMatrix;
Vector3 absPos = bone.absTransform.Translation;
Vector3 projected = viewport.Project(absPos, projectionMatrix, viewMatrix, worldMatrix);
projected.Y = viewport.Height - 1 - projected.Y;
if (projected.Z > 0 && projected.Z < 1) {
projected.Z = 0;
renderedBonesCount++;
}
else {
projected.Z = -1;
}
visibleBonesProjections[i] = projected;
}
}
}
}