-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSkinPreviewer.cs
More file actions
583 lines (467 loc) · 19.6 KB
/
SkinPreviewer.cs
File metadata and controls
583 lines (467 loc) · 19.6 KB
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
using MC_Skin_Aseprite_Previewer;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using OpenTK.Input;
using OpenTK.Mathematics;
using OpenTK.Windowing.Common;
using OpenTK.Windowing.Common.Input;
using OpenTK.Windowing.Desktop;
using OpenTK.Windowing.GraphicsLibraryFramework;
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Threading;
using ErrorCode = OpenTK.Graphics.OpenGL.ErrorCode;
class SkinPreviewer : GameWindow
{
private int textureID;
// Need this to be based locally, or within aseprite? A set folder at least. Idk man let's just cheese it.
private string skinPath = "D:/Ryan/DesktopSH/Misc/Minecraft_Skins/skin.png"; // Change this!
private string triggerFile = "D:/Ryan/DesktopSH/Misc/Minecraft_Skins/preview_trigger.txt";
private DateTime lastFileCheck = DateTime.MinValue;
private OrbitalCamera camera = new OrbitalCamera();
private MouseState prevMouse;
private float AspectRatio;
public float windowWidth;
public float windowHeight;
BodyPartUIPanel bodyPanel = new BodyPartUIPanel();
Matrix4 uiProjection = new();
public SkinPreviewer(GameWindowSettings gws, NativeWindowSettings nws) : base(gws, nws)
{
VSync = VSyncMode.On;
}
protected override void OnLoad()
{
base.OnLoad();
ProjectUI();
GL.Enable(EnableCap.DepthTest);
GL.ClearColor(Color4.CornflowerBlue);
GL.DepthFunc(DepthFunction.Less);
GL.Enable(EnableCap.Blend);
GL.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha);
GL.Disable(EnableCap.CullFace);
GL.Enable(EnableCap.Texture2D);
textureID = LoadTexture(skinPath);
Console.WriteLine($"Loading texture from: {skinPath}");
if (textureID == 0 || textureID == -1)
Console.WriteLine("Failed to load texture!");
else
Console.WriteLine($"Texture loaded successfully with ID: {textureID}");
GL.MatrixMode(MatrixMode.Projection);
GL.LoadIdentity();
AspectRatio = Size.X / (float)Size.Y;
Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView(
MathHelper.DegreesToRadians(45f),
AspectRatio,
0.1f,
100f
);
GL.MultMatrix(ref projection);
GL.BindTexture(TextureTarget.Texture2D, textureID);
GL.MatrixMode(MatrixMode.Modelview);
GL.LoadIdentity();
}
protected override void OnUpdateFrame(FrameEventArgs e)
{
base.OnUpdateFrame(e);
// Check if the preview trigger file has been updated
if (File.Exists(triggerFile))
{
DateTime lastModified = File.GetLastWriteTime(triggerFile);
if (lastModified > lastFileCheck)
{
lastFileCheck = lastModified;
Console.WriteLine("Skin update detected. Reloading texture...");
ReloadTexture();
}
}
}
private void RenderUI()
{
// There's still bugs here.
// When all are not on AllVisible it either goes black or stops rendering entirely.
GL.MatrixMode(MatrixMode.Projection);
GL.PushMatrix();
GL.LoadMatrix(ref uiProjection);
GL.MatrixMode(MatrixMode.Modelview);
GL.PushMatrix();
GL.LoadIdentity();
GL.Disable(EnableCap.DepthTest);
bodyPanel.Render();
GL.Enable(EnableCap.DepthTest);
// Uh oh.
CheckGLError("UI");
GL.PopMatrix(); // Restore modelview
GL.MatrixMode(MatrixMode.Projection);
GL.PopMatrix(); // Restore projection
GL.MatrixMode(MatrixMode.Modelview);
}
protected override void OnMouseDown(MouseButtonEventArgs e)
{
base.OnMouseDown(e);
var mouse = MouseState;
if (e.Button != MouseButton.Left) return;
var mousePos = AdjustMousePosition(mouse);
Console.WriteLine($"CLICKED! Mouse pos: {mouse.Position}, GL Pos: {mousePos}");
Console.WriteLine($"Viewport: {Size.X}x{Size.Y}. Client Viewport: {windowWidth}x{windowHeight}.");
bodyPanel.HandleClick(mousePos);
}
Vector2 AdjustMousePosition(MouseState mouse)
{
Vector2 viewportDif = new Vector2(windowWidth - Size.X, windowHeight - Size.Y);
Vector2 newMousePos = new Vector2(mouse.X, mouse.Y - viewportDif.Y);
return newMousePos;
}
protected override void OnRenderFrame(FrameEventArgs args)
{
base.OnRenderFrame(args);
windowWidth = ClientSize.ToVector2().X;
windowHeight = ClientSize.ToVector2().Y;
GL.Viewport(0, 0, Size.X, Size.Y);
GL.ClearColor(0.4f, 0.8f, 1.0f, 1.0f);
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
GL.Color4(Color4.White);
GL.BindTexture(TextureTarget.Texture2D, textureID);
// Setup the model-view matrix
GL.MatrixMode(MatrixMode.Modelview);
GL.LoadIdentity();
MouseState mouse = MouseState.GetSnapshot();
camera.HandleInput(mouse, prevMouse, MouseState.ScrollDelta, AspectRatio);
prevMouse = mouse;
Matrix4 view = camera.GetViewMatrix();
GL.LoadMatrix(ref view);
UVMaps uvMaps = new UVMaps();
DrawCuboid(new Vector3(6, 16, 2), 8, 8, 8, ModelFaceUtil.GetModelFacesByGroup("Head"), uvMaps, name: "Head"); // Head
DrawCuboid(new Vector3(6, 6, 2), 8, 12, 4, ModelFaceUtil.GetModelFacesByGroup("Body"), uvMaps, name: "Body"); // Body
DrawCuboid(new Vector3(12, 6, 2), 4, 12, 4, ModelFaceUtil.GetModelFacesByGroup("ArmLeft"), uvMaps, name: "ArmLeft"); // Arm Left
DrawCuboid(new Vector3(0, 6, 2), 4, 12, 4, ModelFaceUtil.GetModelFacesByGroup("ArmRight"), uvMaps, name: "ArmRight"); // Arm Right
DrawCuboid(new Vector3(8, -6, 2), 4, 12, 4, ModelFaceUtil.GetModelFacesByGroup("LegLeft"), uvMaps, name: "LegLeft"); // Leg Left
DrawCuboid(new Vector3(4, -6, 2), 4, 12, 4, ModelFaceUtil.GetModelFacesByGroup("LegRight"), uvMaps, name: "LegRight"); // Leg Right
DrawCuboid(new Vector3(6f, 16f, 2f), 8.75f, 8.75f, 8.75f, ModelFaceUtil.GetModelFacesByGroup("OuterHead"), uvMaps, isOuter: true, name: "Head");
DrawCuboid(new Vector3(6f, 6f, 2f), 8.75f, 12.75f, 4.75f, ModelFaceUtil.GetModelFacesByGroup("OuterBody"), uvMaps, isOuter: true, name: "Body");
DrawCuboid(new Vector3(12f, 6f, 2f), 4.75f, 12.75f, 4.75f, ModelFaceUtil.GetModelFacesByGroup("OuterArmLeft"), uvMaps, isOuter: true, name: "ArmLeft");
DrawCuboid(new Vector3(0f, 6f, 2f), 4.75f, 12.75f, 4.75f, ModelFaceUtil.GetModelFacesByGroup("OuterArmRight"), uvMaps, isOuter: true, name: "ArmRight");
DrawCuboid(new Vector3(4f, -6f, 2f), 4.75f, 12.75f, 4.75f, ModelFaceUtil.GetModelFacesByGroup("OuterLegRight"), uvMaps, isOuter: true, name: "LegRight");
DrawCuboid(new Vector3(8f, -6f, 2f), 4.75f, 12.75f, 4.75f, ModelFaceUtil.GetModelFacesByGroup("OuterLegLeft"), uvMaps, isOuter: true, name: "LegLeft");
RenderUI();
SwapBuffers();
}
bool ShouldRender(string partName, RenderState state, bool isOuter)
{
return state switch
{
RenderState.AllVisible => true,
RenderState.InnerOnly => !isOuter,
RenderState.None => false,
_ => true
};
}
void CheckGLError(string context)
{
ErrorCode err;
while((err = GL.GetError()) != ErrorCode.NoError)
{
Console.WriteLine($"GL Error [{context}]: {err}");
}
}
private int LoadTexture(string path)
{
if (!File.Exists(path)) return 0;
int id = GL.GenTexture();
GL.BindTexture(TextureTarget.Texture2D, id);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest);
Bitmap bitmap = new Bitmap(path);
BitmapData data = bitmap.LockBits(
new Rectangle(0, 0, bitmap.Width, bitmap.Height),
ImageLockMode.ReadOnly,
System.Drawing.Imaging.PixelFormat.Format32bppArgb);
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, bitmap.Width, bitmap.Height, 0, OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0);
GL.GenerateMipmap(GenerateMipmapTarget.Texture2D);
bitmap.UnlockBits(data);
return id;
}
private void ReloadTexture()
{
if (File.Exists(skinPath))
{
try
{
Bitmap bitmap = new Bitmap(skinPath);
BitmapData data = bitmap.LockBits(
new Rectangle(0, 0, bitmap.Width, bitmap.Height),
ImageLockMode.ReadOnly,
System.Drawing.Imaging.PixelFormat.Format32bppArgb);
GL.BindTexture(TextureTarget.Texture2D, textureID);
GL.TexSubImage2D(TextureTarget.Texture2D, 0, 0, 0, bitmap.Width, bitmap.Height, OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0);
bitmap.UnlockBits(data);
bitmap.Dispose();
Console.WriteLine("Texture updated without recreation.");
}
catch(Exception e)
{
Console.WriteLine($"Whoops! Error: {e.Message}");
}
}
}
static void Main()
{
GameWindowSettings gws = GameWindowSettings.Default;
NativeWindowSettings nws = new NativeWindowSettings()
{
Size = new Vector2i(800, 600),
Title = "Minecraft Skin Previewer for Aseprite",
APIVersion = new Version(3, 3),
Profile = ContextProfile.Compatability,
Flags = ContextFlags.ForwardCompatible,
WindowBorder = WindowBorder.Resizable
};
using (var window = new SkinPreviewer(gws, nws))
{
window.Run();
}
}
private void ProjectUI()
{
uiProjection = Matrix4.CreateOrthographicOffCenter(0, Size.X, Size.Y, 0, -1, 1);
}
protected override void OnResize(ResizeEventArgs e)
{
base.OnResize(e);
ProjectUI();
GL.Viewport(0, 0, Size.X, Size.Y);
AspectRatio = Size.X / (float)Size.Y;
GL.MatrixMode(MatrixMode.Projection);
GL.LoadIdentity();
Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView(
MathHelper.DegreesToRadians(45f),
AspectRatio,
0.1f,
100f
);
GL.MultMatrix(ref projection);
GL.MatrixMode(MatrixMode.Modelview);
GL.LoadIdentity();
}
#region Drawing
void DrawCube(float x, float y, float z, float width, float height, float depth, float texX, float texY, float texWidth, float texHeight, float textureSize = 64f, string name = "Unnamed Cube")
{
Console.WriteLine($"Drawing {name}.");
float x1 = x, x2 = x + width;
float y1 = y, y2 = y + height;
float z1 = z, z2 = z + depth;
float u1 = texX / textureSize, u2 = (texX + texWidth) / textureSize;
float v1 = texY / textureSize, v2 = (texY + texHeight) / textureSize;
GL.Begin(PrimitiveType.Quads);
// Front Face
GL.TexCoord2(u1, v1); GL.Vertex3(x1, y1, z2);
GL.TexCoord2(u2, v1); GL.Vertex3(x2, y1, z2);
GL.TexCoord2(u2, v2); GL.Vertex3(x2, y2, z2);
GL.TexCoord2(u1, v2); GL.Vertex3(x1, y2, z2);
// Back Face
GL.TexCoord2(u2, v1); GL.Vertex3(x1, y1, z1);
GL.TexCoord2(u1, v1); GL.Vertex3(x2, y1, z1);
GL.TexCoord2(u1, v2); GL.Vertex3(x2, y2, z1);
GL.TexCoord2(u2, v2); GL.Vertex3(x1, y2, z1);
// Top Face
GL.TexCoord2(u1, v1); GL.Vertex3(x1, y2, z1);
GL.TexCoord2(u2, v1); GL.Vertex3(x2, y2, z1);
GL.TexCoord2(u2, v2); GL.Vertex3(x2, y2, z2);
GL.TexCoord2(u1, v2); GL.Vertex3(x1, y2, z2);
// Bottom Face
GL.TexCoord2(u1, v1); GL.Vertex3(x1, y1, z1);
GL.TexCoord2(u2, v1); GL.Vertex3(x2, y1, z1);
GL.TexCoord2(u2, v2); GL.Vertex3(x2, y1, z2);
GL.TexCoord2(u1, v2); GL.Vertex3(x1, y1, z2);
// Left Face
GL.TexCoord2(u1, v1); GL.Vertex3(x1, y1, z1);
GL.TexCoord2(u2, v1); GL.Vertex3(x1, y1, z2);
GL.TexCoord2(u2, v2); GL.Vertex3(x1, y2, z2);
GL.TexCoord2(u1, v2); GL.Vertex3(x1, y2, z1);
// Right Face
GL.TexCoord2(u1, v1); GL.Vertex3(x2, y1, z1);
GL.TexCoord2(u2, v1); GL.Vertex3(x2, y1, z2);
GL.TexCoord2(u2, v2); GL.Vertex3(x2, y2, z2);
GL.TexCoord2(u1, v2); GL.Vertex3(x2, y2, z1);
GL.End();
}
void DrawFace(ModelFace face, UVMaps uvMaps, float textureWidth, float textureHeight)
{
Vector2[] uvs = uvMaps.MapDict[face].GetNormalisedUVs(textureWidth, textureHeight);
}
void DrawCuboid(Vector3 position, float width, float height, float depth, ModelFace[] faceOrder, UVMaps uvMaps, float textureWidth = 64, float textureHeight = 64, string name = "Unnamed Cuboid", bool isOuter = false)
{
if (!ShouldRender(name, bodyPanel.GetRenderStates()[name], isOuter) && !isOuter) return;
if (!ShouldRender(name, bodyPanel.GetRenderStates()[name], isOuter)) return;
float hw = width / 2f;
float hh = height / 2f;
float hd = depth / 2f;
Vector3[][] faceVertices = new Vector3[][]
{
// Front (+Z)
new Vector3[] {
new Vector3( hw, hh, hd), // Top Right
new Vector3(-hw, hh, hd), // Top Left
new Vector3(-hw, -hh, hd), // Bottom Left
new Vector3( hw, -hh, hd), // Bottom Right
},
// Back (-Z)
new Vector3[] {
new Vector3( hw, hh, -hd), // Top Right
new Vector3( hw, -hh, -hd), // Bottom Right
new Vector3(-hw, -hh, -hd), // Bottom Left
new Vector3(-hw, hh, -hd), // Top Left
},
// Left (-X)
new Vector3[] {
new Vector3(-hw, hh, -hd), // Top Back
new Vector3(-hw, -hh, -hd), // Bottom Back
new Vector3(-hw, -hh, hd), // Bottom Front
new Vector3(-hw, hh, hd), // Top Front
},
// Right (+X)
new Vector3[] {
new Vector3( hw, hh, hd), // Top Front
new Vector3( hw, -hh, hd), // Bottom Front
new Vector3( hw, -hh, -hd), // Bottom Back
new Vector3( hw, hh, -hd), // Top Back
},
// Top (+Y)
new Vector3[] {
new Vector3(-hw, hh, -hd), // Back Left
new Vector3(-hw, hh, hd), // Front Left
new Vector3( hw, hh, hd), // Front Right
new Vector3( hw, hh, -hd), // Back Right
},
// Bottom (-Y)
new Vector3[] {
new Vector3(-hw, -hh, -hd), // Back Left
new Vector3( hw, -hh, -hd), // Back Right
new Vector3( hw, -hh, hd), // Front Right
new Vector3(-hw, -hh, hd) // Front Left
}
};
// Dude this sucks. I should have just adjusted the actual UV stuff. Big Cringe.
for (int i = 0; i < faceOrder.Length; i++)
{
UV faceUV = uvMaps.MapDict[faceOrder[i]];
Vector2[] uvs = faceUV.GetNormalisedUVs(textureWidth, textureHeight);
switch (faceOrder[i])
{
case ModelFace.Head_Front:
uvs = RotateUVs(uvs, 3);
break;
case ModelFace.Body_Front:
uvs = RotateUVs(uvs, 3);
break;
case ModelFace.LegLeft_Front:
uvs = RotateUVs(uvs, 3);
break;
case ModelFace.LegRight_Front:
uvs = RotateUVs(uvs, 3);
break;
case ModelFace.ArmRight_Front:
uvs = RotateUVs(uvs, 3);
break;
case ModelFace.ArmLeft_Front:
uvs = RotateUVs(uvs, 3);
break;
case ModelFace.Outer_Head_Front:
uvs = RotateUVs(uvs, 3);
break;
case ModelFace.Outer_Body_Front:
uvs = RotateUVs(uvs, 3);
break;
case ModelFace.Outer_LegLeft_Front:
uvs = RotateUVs(uvs, 3);
break;
case ModelFace.Outer_LegRight_Front:
uvs = RotateUVs(uvs, 3);
break;
case ModelFace.Outer_ArmRight_Front:
uvs = RotateUVs(uvs, 3);
break;
case ModelFace.Outer_ArmLeft_Front:
uvs = RotateUVs(uvs, 3);
break;
case ModelFace.Head_Bottom:
uvs = RotateUVs(uvs, 3);
break;
case ModelFace.Body_Bottom:
uvs = RotateUVs(uvs, 3);
break;
case ModelFace.LegLeft_Bottom:
uvs = RotateUVs(uvs, 3);
break;
case ModelFace.LegRight_Bottom:
uvs = RotateUVs(uvs, 3);
break;
case ModelFace.ArmRight_Bottom:
uvs = RotateUVs(uvs, 3);
break;
case ModelFace.ArmLeft_Bottom:
uvs = RotateUVs(uvs, 3);
break;
case ModelFace.Outer_Head_Bottom:
uvs = RotateUVs(uvs, 3);
break;
case ModelFace.Outer_Body_Bottom:
uvs = RotateUVs(uvs, 3);
break;
case ModelFace.Outer_LegLeft_Bottom:
uvs = RotateUVs(uvs, 3);
break;
case ModelFace.Outer_LegRight_Bottom:
uvs = RotateUVs(uvs, 3);
break;
case ModelFace.Outer_ArmRight_Bottom:
uvs = RotateUVs(uvs, 3);
break;
case ModelFace.Outer_ArmLeft_Bottom:
uvs = RotateUVs(uvs, 3);
break;
}
Vector3[] verts = faceVertices[i];
AddQuad(
position + verts[0],
position + verts[1],
position + verts[2],
position + verts[3],
uvs
);
}
//Console.WriteLine($"Drawing {name}.");
}
Vector2[] RotateUVs(Vector2[] uvs, int rotationSteps)
{
// rotationSteps: 0 = 0°, 1 = 90° CW, 2 = 180°, 3 = 270° CW
Vector2[] rotated = new Vector2[4];
for (int i = 0; i < 4; i++)
{
rotated[i] = uvs[(i + rotationSteps) % 4];
}
return rotated;
}
void AddQuad(Vector3 v0, Vector3 v1, Vector3 v2, Vector3 v3, Vector2[] uvs)
{
GL.Begin(PrimitiveType.Triangles);
CheckGLError("GL.Begin Quads");
// Triangle 1
AddVertex(v0, uvs[0]);
AddVertex(v1, uvs[1]);
AddVertex(v2, uvs[2]);
// Triangle 2
AddVertex(v0, uvs[0]);
AddVertex(v2, uvs[2]);
AddVertex(v3, uvs[3]);
GL.End();
}
void AddVertex(Vector3 pos, Vector2 uv)
{
GL.TexCoord2(uv.X, uv.Y);
GL.Vertex3(pos.X, pos.Y, pos.Z);
}
#endregion
}