-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmmodding.js
More file actions
169 lines (155 loc) · 6.65 KB
/
mmodding.js
File metadata and controls
169 lines (155 loc) · 6.65 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
/** @typedef {import("blockbench-types/generated/plugin_loader").Plugin} Plugin */
/** Inspired by https://github.com/JannisX11/blockbench/blob/8fe8d9d9568de8233d77cd592744acad495d46b0/js/formats/java/modded_entity.js **/
(function() {
let mmodding_entity_model;
let codec = new Codec("mmodding_entity_model", {
name: "MModding Entity Model JSON File",
extension: "json",
remember: "true",
support_partial_export: true,
load_filter: {
type: "text",
extensions: ["json"]
},
compile(options) {
let groups = getAllGroups();
let leftover_collector = [];
let leftovers;
Cube.all.forEach(cube => {
if (cube.export === false) return;
if (cube.parent === "root") leftover_collector.push(cube)
})
if (leftover_collector.length) {
leftovers = new Group({ name: "leftovers" }); // what is named "bb_model" in Java Entity Model
leftovers.is_catch_bone = true;
leftovers.createUniqueName()
leftovers.children.replace(leftover_collector)
groups.push(leftovers)
}
groups.slice().forEach(group => { // like in Java Entity Model
if (group.export === false) return;
let subgroups = [];
let group_i = groups.indexOf(group);
group.children.forEachReverse(cube => {
if (!(cube instanceof Cube) || !cube.export) return;
if (!cube.rotation.allEqual(0)) {
let sub = subgroups.find(s => {
if (!s.rotation.equals(cube.rotation)) return false;
if (s.rotation.filter(n => n).length > 1) {
return s.origin.equals(cube.origin)
} else {
for (let i = 0; i < 3; i++) {
if (s.rotation[i] === 0 && s.origin[i] !== cube.origin[i]) return false;
}
return true;
}
})
if (!sub) {
sub = new Group({
rotation: cube.rotation,
origin: cube.origin,
name: `rotated_${cube.name}_0`
})
sub.parent = group;
sub.is_rotation_subgroup = true;
sub.createUniqueName(groups)
subgroups.push(sub)
group_i++;
groups.splice(group_i, 0, sub);
}
sub.children.push(cube);
}
})
})
let hierarchy = {};
let direct_children = [];
groups.slice().forEach(group => {
if (group.export === false) return;
if (group.parent === "root") {
direct_children.push(group);
}
else {
if (hierarchy[group.parent.name] == null) hierarchy[group.parent.name] = [];
hierarchy[group.parent.name].push(group);
}
});
let result = { parts: {}, texture_size: [Project.texture_width, Project.texture_height] };
function apply_to(definition, group, flip_y) {
if ((group instanceof Group || group.is_catch_bone) && group.export) {
definition.pose = {
rotation: [Math.degToRad(-group.rotation[0]), Math.degToRad(-group.rotation[1]), Math.degToRad(group.rotation[2])]
}
let origin = group.origin.slice();
if (group.parent instanceof Group) {
origin.V3_subtract(group.parent.origin)
}
origin[0] *= -1;
if (flip_y) {
origin[1] *= -1;
if (!(group.parent instanceof Group)) {
origin[1] += 24
}
}
definition.pose.offset = [origin[0], origin[1], origin[2]];
definition.cubes = [];
for (let cube of group.children) {
if (cube instanceof Cube && cube.export && (cube.rotation.allEqual(0) || group.is_rotation_subgroup)) {
let encoded = { texture_offset: [cube.uv_offset[0], cube.uv_offset[1]] };
if (cube.mirror) encoded.mirror = true;
if (flip_y) {
encoded.anchor = [
group.origin[0] - cube.to[0],
-cube.from[1] - cube.size(1) + group.origin[1],
cube.from[2] - group.origin[2]
]
}
else {
encoded.anchor = [
group.origin[0] - cube.to[0],
cube.from[1] - group.origin[1],
cube.from[2] - group.origin[2]
]
}
encoded.size = [cube.size(0, false), cube.size(1, false), cube.size(2, false)]
definition.cubes.push(encoded);
}
}
if (hierarchy[group.name] != null) {
definition.children = {};
for (let inner of hierarchy[group.name]) {
definition.children[inner.name] = {};
apply_to(definition.children[inner.name], inner, flip_y);
}
}
}
}
direct_children.slice().forEach(direct => {
result.parts[direct.name] = {}
apply_to(result.parts[direct.name], direct, Project.modded_entity_flip_y);
})
return autoStringify(result);
}
})
Plugin.register("mmodding", {
title: "MModding Blockbench Plugin",
author: "MModding Team",
tags: ["Entity Models", "Block Entity Models"],
description: "Additional tools provided to help development with the MModding Library",
version: "0.0.1",
min_version: "4.9.0",
variant: "both",
onload() {
mmodding_entity_model = new Action("mmodding_entity_model", {
name: "Export MModding Entity Model",
description: "Export the entity model as a MModding Entity Model JSON File",
icon: "favicon_beta",
condition: () => Format.id === "modded_entity",
click: () => codec.export()
});
MenuBar.addAction(mmodding_entity_model, "file.export.0");
},
onunload() {
mmodding_entity_model.delete();
}
});
})();