-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmeshy.js
More file actions
366 lines (318 loc) · 13.4 KB
/
Copy pathmeshy.js
File metadata and controls
366 lines (318 loc) · 13.4 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
(function() {
const pluginInfo = {"name":"Meshy","id":"meshy","version":"1.0.4","repository":"https://github.com/Shadowkitten47/Meshy"};
const pluginSettings = [
{
id: 'meshy_normalized_mesh_uvs',
name: 'Normalize Mesh UVs',
description: 'Normalize UVs of polymeshes',
category: 'export',
value: true,
plugin: pluginInfo.id,
},
{
id: 'meshy_skip_mesh_normals',
name: 'Skip Mesh Normals',
description: 'Skips normal claculation on polymeshes',
category: 'export',
value: false,
plugin: pluginInfo.id,
},
{
//Force disable single texture on bedrock formats having more than one texture with meshes is pretty useful
id: 'meshy_force_textures',
name: 'Force Multi-Textures',
description: 'Forces bedrock formats to use allow more than one texture ( You will need to stitch the textures )',
category: 'edit',
value: false,
plugin: pluginInfo.id,
onChange: (value) => {
Formats['bedrock'].single_texture = !value;
Formats['bedrock_old'].single_texture = !value;
},
},
];
//Function names for events to remove
const listOfFunctions = [ meshyOnParseEvent.name, meshyOnCompileEvent.name, meshyOnBedrockCompileEvent.name];
Plugin.register(pluginInfo.id, {
title: pluginInfo.name, //Meshy
author: 'Shadowkitten47',
creation_date: '2024-09-28',
icon: 'diamond',
description: 'Enables the use of a meshes in bedrock formats and to export them to Minecraft Bedrock',
variant: 'both',
version: pluginInfo.version, //1.0.4
tags: ['Minecraft: Bedrock Edition', 'Entity Models', 'Mesh'],
has_changelog: true,
min_version: '4.10.4',
repository: pluginInfo.repository, //Link
onload() {
let bedrock_old = Formats['bedrock_old'];
let bedrock = Formats['bedrock'];
bedrock.meshes = true;
bedrock_old.meshes = true;
for (let s of pluginSettings) {
if (!settings[s.id]) {
new Setting(s.id, s);
}
}
bedrock.single_texture = !settings['meshy_force_textures']?.value;
bedrock_old.single_texture = !settings['meshy_force_textures']?.value;
var codec = Codecs['bedrock'];
purgeEvents(codec); //Removes all of events that match the function names used so that duplicates don't occur
codec.on('parsed', meshyOnParseEvent);
codec.on('compile', meshyOnBedrockCompileEvent);
codec = Codecs['bedrock_old'];
purgeEvents(codec);
codec.on('parsed', meshyOnParseEvent);
codec.on('compile', meshyOnCompileEvent);
//pivot_tool = Toolbars["tools"].children.find((t) => t.id == 'pivot_tool').condition = () => { return false; };
},
onunload() {
let bedrock_old = Formats['bedrock_old'];
let bedrock = Formats['bedrock'];
bedrock.meshes = false;
bedrock_old.meshes = false;
bedrock.single_texture = true;
bedrock_old.single_texture = true;
for (let s of pluginSettings) {
if (settings[s.id]) {
settings[s.id].delete();
}
}
var codec = Codecs['bedrock'];
purgeEvents(codec);
codec = Codecs['bedrock_old'];
purgeEvents(codec);
},
});
//Beaware: Function zone below
function meshyOnCompileEvent({model, options}) {
var groups = getAllGroups();
var loose_elements = [];
Outliner.root.forEach((obj) => {
if (obj instanceof OutlinerElement) {
loose_elements.push(obj);
}
});
if (loose_elements.length) {
let group = new Group({
name: 'bb_main',
});
group.children.push(...loose_elements);
group.is_catch_bone = true;
group.createUniqueName();
groups.splice(0, 0, group);
}
for (let i = 0; i < groups.length; i++) {
const g = groups[i];
if (g.type !== 'group' || g.export == false) return;
if (!settings.export_empty_groups.value && !g.children.find((child) => child.export)) return;
const bone = model.bones.find((b) => b.name === g.name);
let poly_mesh = null;
for (var obj of g.children) {
if (obj instanceof Mesh) {
poly_mesh = compileMesh(poly_mesh, obj);
}
}
if (poly_mesh !== null) {
bone.poly_mesh = poly_mesh;
}
}
}
function meshyOnParseEvent({model}) {
for (let i = 0; i < model.bones.length; i++) {
const bone = model.bones[i];
//The groups and bone should be parrel but to make sure it works in all cases below os freaky
const group = Project.groups[i].name === bone.name ? Project.groups[i] : Project.groups.find((g) => g.name === bone.name) ?? new Group({name: bone.name});
if (bone.poly_mesh != null) {
parseMesh(bone.poly_mesh, group);
}
}
}
function meshyOnBedrockCompileEvent({model, options}) { //Extra step for non-legacy bedrock
model = model['minecraft:geometry'][0];
meshyOnCompileEvent({model, options});
}
//Ensures every function is removed with the names used here. This is so that if duplicates are created it doesn't cause issues
//The regular version does not do this
function purgeEvents(codec) {
for (let i = 0; i < codec.events['parsed']?.length; i++) {
if (listOfFunctions.includes(codec.events['parsed'][i].name)) {
codec.events['parsed'].splice(i, 1);
}
}
for (let i = 0; i < codec.events['compile']?.length; i++) {
if (listOfFunctions.includes(codec.events['compile'][i].name)) {
codec.events['compile'].splice(i, 1);
}
}
}
/**
* Converts a mesh to a polymesh.
* @param {Object} polyMesh The polymesh to save to. If not defined, a new polymesh will be created.
* @param {Mesh} mesh The mesh to save.
* @returns {Object} The polymesh with the mesh saved to it.
*/
function compileMesh(polyMesh, mesh) {
polyMesh ??=
{
normalized_uvs: settings["meshy_normalized_mesh_uvs"].value,
positions: [],
normals: [],
uvs: [],
polys: []
};
//vertex keys -> value
const postionMap = new Map();
const normalMap = new Map();
const uvMap = new Map();
const vertexFacesMap = new Map();
//normal arr -> value
const normals = new Map();
//Make a map of faces a vertex is appart of
for (let faceKey in mesh.faces) {
let face = mesh.faces[faceKey];
for (let vertexKey of face.vertices) {
if (!vertexFacesMap.has(vertexKey)) {
vertexFacesMap.set(vertexKey, []);
}
vertexFacesMap.get(vertexKey).push(faceKey);
}
}
for (let [key, pos] of getVertices(mesh)) {
postionMap.set(key, polyMesh.positions.length);
polyMesh.positions.push(pos);
const normal = getVertexNormal(mesh, key, vertexFacesMap);
if (!normals.has(normal.toString())) {
normalMap.set(key, polyMesh.normals.length);
normals.set(normal.toString(), polyMesh.normals.length);
polyMesh.normals.push(normal);
}
else normalMap.set(key, normals.get(normal.toString()))
}
let polys = Object.values(mesh.faces).map((face) => {
const poly = face.getSortedVertices().map((vertexKey) => {
const uv = uvOnSave(...face.uv[vertexKey]);
const uIndex = uvMap.get(uv.toString()) ?? (() => {
const index = polyMesh.uvs.length;
polyMesh.uvs.push(uv);
uvMap.set(uv.toString(), index);
return index;
})();
return [postionMap.get(vertexKey), normalMap.get(vertexKey), uIndex];
});
if (poly.length < 4)
//Fill the poly with the first vertex if less than a quad change: Support for less than 3 vertices
return poly.concat(Array(4 - poly.length).fill(poly[0]));
return poly;
});
//Spread opertator fails here so we loop for each
for (let poly of polys) polyMesh.polys.push(poly);
return polyMesh;
}
function parseMesh(polyMesh, group) {
const mesh = new Mesh({name: "mesh", autouv: 0, color: group.color, vertices: []});
const uniquePoints = new Set();
for ( let face of polyMesh.polys ) {
const unique = new Set();
const vertices = []
const uvs = {}
for (let point of face ) {
if (unique.has(point.toString())) continue;
unique.add(point.toString());
if ( !uniquePoints.has(point[0])) {
uniquePoints.add(point[0]);
let postion = polyMesh.positions[point[0]]
postion[0] *= -1;
mesh.vertices[`v${point[0]}`] = postion;
}
vertices.push(`v${point[0]}`);
const uv = [...polyMesh.uvs[point[2]]]
if (polyMesh.normalized_uvs) {
uv.V2_multiply(Project.texture_width, Project.texture_height)
}
uv[1] = Project.texture_height - uv[1] //Invert y axis
uvs[`v${point[0]}`] = uv;
}
mesh.addFaces(new MeshFace(mesh, { uv: uvs, vertices }));
}
mesh.addTo(group).init();
}
function uvOnSave(...uv) {
uv[1] = Project.texture_height - uv[1] //Invert y axis
if (!settings["meshy_normalized_mesh_uvs"].value) return uv
uv[0] /= Project.texture_width
uv[1] /= Project.texture_height
return uv
}
//gets vertices of a Mesh and applys transformations to the points so that they can be exported
function getVertices(mesh) {
const verts = Object.entries(mesh.vertices).map( ( [key, point ]) => {
//Generate a copy of the point so that it won't effect the original point
let p = [...point]
p.V3_add(mesh.origin)
p = rotatePoint(p, mesh.origin, mesh.rotation)
p[0] *= -1;
return [ key, p ]
})
return verts;
}
/**
* Gets the vertex normal of a mesh
* @param {Mesh} mesh The mesh to get the vertex normal from
* @param {string} vertexKey The key of the vertex
* @param {Map} vertexFacesMap The map of vertex faces
* The vertexFacesMap is used to get the faces of the vertex
* This so we don't have to loop through the faces for each vertex
*/
function getVertexNormal(mesh, vertexKey, vertexFacesMap) {
if (settings["meshy_skip_mesh_normals"].value) return [ 0,1,0 ];
let normalSum = [0, 0, 0];
let faceCount = 0;
const faces = vertexFacesMap.get(vertexKey) || []
for (let face of faces) {
face = mesh.faces[face];
let faceNormal = face.getNormal();
normalSum[0] += faceNormal[0];
normalSum[1] += faceNormal[1];
normalSum[2] += faceNormal[2];
faceCount++;
}
let normalLength = Math.sqrt(normalSum[0] * normalSum[0] + normalSum[1] * normalSum[1] + normalSum[2] * normalSum[2]);
if (normalLength === 0) {
return [0, 1, 0]; // Default to up vector if normal is zero
}
return [
normalSum[0] / normalLength,
normalSum[1] / normalLength,
normalSum[2] / normalLength
];
}
function multiplyScalar(vec, scalar) {
return vec.map((coord) => coord * scalar);
}
function rotatePoint(point, center, rotation) {
// Convert rotation angles to radians
const [rx, ry, rz] = rotation.map(Math.degToRad);
// Translate point to origin
let [x, y, z] = point.map((coord, i) => coord - center[i]);
// Rotate around X-axis
let temp = y;
y = y * Math.cos(rx) - z * Math.sin(rx);
z = temp * Math.sin(rx) + z * Math.cos(rx);
// Rotate around Y-axis
temp = x;
x = x * Math.cos(ry) + z * Math.sin(ry);
z = -temp * Math.sin(ry) + z * Math.cos(ry);
// Rotate around Z-axis
temp = x;
x = x * Math.cos(rz) - y * Math.sin(rz);
y = temp * Math.sin(rz) + y * Math.cos(rz);
// Translate back
return [
x + center[0],
y + center[1],
z + center[2]
];
}
})();