-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
PartCustomization.java
402 lines (344 loc) · 12.8 KB
/
PartCustomization.java
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
package org.figuramc.figura.model;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.datafixers.util.Pair;
import org.figuramc.figura.math.matrix.FiguraMat3;
import org.figuramc.figura.math.matrix.FiguraMat4;
import org.figuramc.figura.math.vector.FiguraVec3;
import org.figuramc.figura.model.rendering.texture.FiguraTextureSet;
import org.figuramc.figura.model.rendering.texture.RenderTypes;
import java.util.Stack;
public class PartCustomization {
// -- Matrix thingies --//
/**
* Boolean exists because blockbench sucks and uses a different rotation
* formula for meshes than other types of items. Meshes rotate in XYZ order,
* while literally everything else is in ZYX order.
*/
public PartType partType = PartType.GROUP;
public final FiguraMat4 positionMatrix = FiguraMat4.of();
public final FiguraMat3 uvMatrix = FiguraMat3.of();
public final FiguraMat3 normalMatrix = FiguraMat3.of();
public boolean needsMatrixRecalculation = false;
public Boolean visible = null;
public Boolean vanillaVisible = null;
private final FiguraVec3 position = FiguraVec3.of();
private final FiguraVec3 rotation = FiguraVec3.of();
private final FiguraVec3 scale = FiguraVec3.of(1, 1, 1);
private final FiguraVec3 pivot = FiguraVec3.of();
// The "offset" values are for vanilla part scaling. The offset pivot and rot can be get and set from script.
private final FiguraVec3 offsetPivot = FiguraVec3.of();
private final FiguraVec3 offsetPos = FiguraVec3.of();
private final FiguraVec3 offsetRot = FiguraVec3.of();
private final FiguraVec3 offsetScale = FiguraVec3.of(1, 1, 1);
// These values are set by animation players. They can be queried, though not set, by script.
private final FiguraVec3 animPos = FiguraVec3.of();
private final FiguraVec3 animRot = FiguraVec3.of();
private final FiguraVec3 animScale = FiguraVec3.of(1, 1, 1);
public final FiguraVec3 stackScale = FiguraVec3.of(1, 1, 1);
public final FiguraVec3 color = FiguraVec3.of(1, 1, 1);
public final FiguraVec3 color2 = FiguraVec3.of(1, 1, 1);
public Float alpha = null;
public Integer light = null;
public Integer overlay = null;
private RenderTypes primaryRenderType, secondaryRenderType;
public TextureCustomization primaryTexture, secondaryTexture;
public void applyToStack(PoseStack stack) {
stack.mulPoseMatrix(positionMatrix.toMatrix4f());
stack.last().normal().mul(normalMatrix.toMatrix3f());
}
/**
* Recalculates the matrix if necessary.
*/
public void recalculate() {
if (!needsMatrixRecalculation)
return;
positionMatrix.reset();
// Position the pivot point at 0, 0, 0, and translate the part
positionMatrix.translate(
offsetPos.x - pivot.x - offsetPivot.x,
offsetPos.y - pivot.y - offsetPivot.y,
offsetPos.z - pivot.z - offsetPivot.z
);
// Scale the model part around the pivot
stackScale.set(
offsetScale.x * scale.x * animScale.x,
offsetScale.y * scale.y * animScale.y,
offsetScale.z * scale.z * animScale.z
);
positionMatrix.scale(stackScale);
// Rotate the model part around the pivot
if (partType == PartType.MESH) {
positionMatrix.rotateZ(rotation.z + offsetRot.z + animRot.z);
positionMatrix.rotateY(rotation.y + offsetRot.y + animRot.y);
positionMatrix.rotateX(rotation.x + offsetRot.x + animRot.x);
} else {
positionMatrix.rotateZYX(
rotation.x + offsetRot.x + animRot.x,
rotation.y + offsetRot.y + animRot.y,
rotation.z + offsetRot.z + animRot.z
);
}
// Undo the effects of the pivot translation
positionMatrix.translate(
position.x + animPos.x + pivot.x + offsetPivot.x,
position.y + animPos.y + pivot.y + offsetPivot.y,
position.z + animPos.z + pivot.z + offsetPivot.z
);
// Set up the normal matrix as well
normalMatrix.reset();
double x = scale.x * animScale.x;
double y = scale.y * animScale.y;
double z = scale.z * animScale.z;
double c = Math.cbrt(x * y * z);
normalMatrix.scale(
c == 0 && x == 0 ? 1 : c / x,
c == 0 && y == 0 ? 1 : c / y,
c == 0 && z == 0 ? 1 : c / z
);
// Perform rotation of normals
if (partType == PartType.MESH) {
normalMatrix.rotateZ(rotation.z + offsetRot.z + animRot.z);
normalMatrix.rotateY(rotation.y + offsetRot.y + animRot.y);
normalMatrix.rotateX(rotation.x + offsetRot.x + animRot.x);
} else {
normalMatrix.rotateZYX(
rotation.x + offsetRot.x + animRot.x,
rotation.y + offsetRot.y + animRot.y,
rotation.z + offsetRot.z + animRot.z
);
}
needsMatrixRecalculation = false;
}
public void setPos(FiguraVec3 pos) {
setPos(pos.x, pos.y, pos.z);
}
public void setPos(double x, double y, double z) {
position.set(x, y, z);
needsMatrixRecalculation = true;
}
public FiguraVec3 getPos() {
return position.copy();
}
public void setRot(FiguraVec3 rot) {
setRot(rot.x, rot.y, rot.z);
}
public void setRot(double x, double y, double z) {
rotation.set(x, y, z);
needsMatrixRecalculation = true;
}
public FiguraVec3 getRot() {
return rotation.copy();
}
public void setScale(FiguraVec3 scale) {
setScale(scale.x, scale.y, scale.z);
}
public void setScale(double x, double y, double z) {
scale.set(x, y, z);
needsMatrixRecalculation = true;
}
public FiguraVec3 getScale() {
return scale.copy();
}
public void setPivot(FiguraVec3 pivot) {
setPivot(pivot.x, pivot.y, pivot.z);
}
public void setPivot(double x, double y, double z) {
pivot.set(x, y, z);
needsMatrixRecalculation = true;
}
public FiguraVec3 getPivot() {
return pivot.copy();
}
public void offsetPivot(FiguraVec3 pivot) {
offsetPivot(pivot.x, pivot.y, pivot.z);
}
public void offsetPivot(double x, double y, double z) {
offsetPivot.set(x, y, z);
needsMatrixRecalculation = true;
}
public FiguraVec3 getOffsetPivot() {
return offsetPivot.copy();
}
public void offsetPos(FiguraVec3 pos) {
offsetPos(pos.x, pos.y, pos.z);
}
public void offsetPos(double x, double y, double z) {
offsetPos.set(x, y, z);
needsMatrixRecalculation = true;
}
public FiguraVec3 getOffsetPos() {
return offsetPos.copy();
}
public void offsetRot(FiguraVec3 rot) {
offsetRot(rot.x, rot.y, rot.z);
}
public void offsetRot(double x, double y, double z) {
offsetRot.set(x, y, z);
needsMatrixRecalculation = true;
}
public FiguraVec3 getOffsetRot() {
return offsetRot.copy();
}
public void offsetScale(FiguraVec3 scale) {
offsetScale(scale.x, scale.y, scale.z);
}
public void offsetScale(double x, double y, double z) {
offsetScale.set(x, y, z);
needsMatrixRecalculation = true;
}
public FiguraVec3 getOffsetScale() {
return offsetScale.copy();
}
public void setAnimPos(double x, double y, double z) {
animPos.set(x, y, z);
needsMatrixRecalculation = true;
}
public FiguraVec3 getAnimPos() {
return animPos.copy();
}
public void setAnimRot(double x, double y, double z) {
animRot.set(x, y, z);
needsMatrixRecalculation = true;
}
public FiguraVec3 getAnimRot() {
return animRot.copy();
}
public void setAnimScale(double x, double y, double z) {
animScale.set(x, y, z);
needsMatrixRecalculation = true;
}
public FiguraVec3 getAnimScale() {
return animScale.copy();
}
public void setMatrix(FiguraMat4 matrix) {
positionMatrix.set(matrix);
FiguraMat3 temp = matrix.deaugmented();
temp.invert();
temp.transpose();
normalMatrix.set(temp);
needsMatrixRecalculation = false;
}
public void setPositionMatrix(FiguraMat4 matrix) {
positionMatrix.set(matrix);
}
public void setNormalMatrix(FiguraMat3 matrix) {
normalMatrix.set(matrix);
}
public FiguraMat4 getPositionMatrix() {
FiguraMat4 result = FiguraMat4.of();
result.set(positionMatrix);
return result;
}
public FiguraMat3 getNormalMatrix() {
FiguraMat3 result = new FiguraMat3();
result.set(normalMatrix);
return result;
}
// -- Render type thingies --//
public void setPrimaryRenderType(RenderTypes type) {
primaryRenderType = type;
}
public RenderTypes getPrimaryRenderType() {
return primaryRenderType;
}
public void setSecondaryRenderType(RenderTypes type) {
secondaryRenderType = type;
}
public RenderTypes getSecondaryRenderType() {
return secondaryRenderType;
}
public void copyTo(PartCustomization target) {
target.partType = partType;
target.positionMatrix.set(positionMatrix);
target.uvMatrix.set(uvMatrix);
target.normalMatrix.set(normalMatrix);
target.setPos(position);
target.setRot(rotation);
target.setScale(scale);
target.setPivot(pivot);
target.offsetPivot(offsetPivot);
target.offsetPos(offsetPos);
target.offsetRot(offsetRot);
target.offsetScale(offsetScale);
target.stackScale.set(stackScale);
target.color.set(color);
target.color2.set(color2);
target.alpha = alpha;
target.light = light;
target.overlay = overlay;
target.needsMatrixRecalculation = needsMatrixRecalculation;
target.visible = visible;
target.vanillaVisible = vanillaVisible;
target.setPrimaryRenderType(primaryRenderType);
target.setSecondaryRenderType(secondaryRenderType);
target.primaryTexture = primaryTexture;
target.secondaryTexture = secondaryTexture;
}
// Modify this object using the information contained in the other object
public void modify(PartCustomization other) {
positionMatrix.rightMultiply(other.positionMatrix);
uvMatrix.rightMultiply(other.uvMatrix);
normalMatrix.rightMultiply(other.normalMatrix);
if (other.primaryRenderType != null)
setPrimaryRenderType(other.primaryRenderType);
if (other.secondaryRenderType != null)
setSecondaryRenderType(other.secondaryRenderType);
if (other.visible != null)
visible = other.visible;
if (other.vanillaVisible != null)
vanillaVisible = other.vanillaVisible;
if (other.light != null)
light = other.light;
if (other.overlay != null)
overlay = other.overlay;
if (other.alpha != null) {
if (alpha != null)
alpha *= other.alpha;
else
alpha = other.alpha;
}
stackScale.multiply(other.stackScale);
color.multiply(other.color);
color2.multiply(other.color2);
if (other.primaryTexture != null)
primaryTexture = other.primaryTexture;
if (other.secondaryTexture != null)
secondaryTexture = other.secondaryTexture;
needsMatrixRecalculation = false;
}
public static final PoseStack GLOBAL_CUSTOMIZATION_POSE_STACK = new PoseStack();
public PoseStack copyIntoGlobalPoseStack() {
recalculate();
positionMatrix.copyDataTo(GLOBAL_CUSTOMIZATION_POSE_STACK.last().pose());
normalMatrix.copyDataTo(GLOBAL_CUSTOMIZATION_POSE_STACK.last().normal());
return GLOBAL_CUSTOMIZATION_POSE_STACK;
}
public enum PartType {
MESH,
CUBE,
GROUP
}
public static class PartCustomizationStack {
private final Stack<PartCustomization> stack = new Stack<>() {{
add(new PartCustomization());
}};
public void push(PartCustomization customization) {
// copy stack
PartCustomization newCustomization = new PartCustomization();
stack.peek().copyTo(newCustomization);
// modify
newCustomization.modify(customization);
// add
stack.push(newCustomization);
}
public void pop() {
stack.pop();
}
public PartCustomization peek() {
return stack.peek();
}
public boolean isEmpty() {
return stack.size() == 1;
}
}
}