-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcosmic_reach_model.js
287 lines (240 loc) · 11 KB
/
cosmic_reach_model.js
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
function sumArrays(a, b){
s = []
for(let i = 0; i < Math.min(a.length, b.length); i++){
s.push(a[i] + b[i])
}
return s
}
(() => {
let codec, format, export_action, import_action, dialog, properties
const id = "cosmic_reach_model"
const name = "Cosmic Reach Model"
const icon = "icon.png"
Plugin.register(id, {
title: name,
icon: "icon.png",
author: "Z. Hoeshin",
description: "A plugin that allows to create models for the game Cosmic Reach.",
tags: [],
version: "1.0.0",
creation_date: "2024-03-09",
onload() {
Codecs.java_block.load_filter.condition = (model) => {
return (model.parent || model.elements || model.textures) && (!model.cuboids);
}
dialog = new Dialog("cosmic_reach_model_errormessage", {
id: "cosmic_reach_model_dialog",
title: "Something went wrong...",
buttons: [],
lines: [],
})
codec = new Codec("cosmic_reach_block_model_codec", {
name: "Cosmic Reach",
extension: "json",
remember: false,
load_filter: {type: "json", extensions: ["json"],
condition: (model) => {
console.warn(model)
return model.cuboids || model.textures;
}
},
format: new ModelFormat("cosmic_reach_model", {
id: "cosmic_reach_model",
icon: null,
name: "Cosmic Reach Model",
description: "Model format used by the game Cosmic Reach",
show_on_start_screen: true,
target: ["json"],
vertex_color_ambient_occlusion: true,
/*rotate_cubes: true,
rotation_limit: true,
rotation_snap: true,*/
uv_rotation: true,
java_face_properties: true,
edit_mode: true,
new() {
newProject(this)
Project.texture_width = 16
Project.texture_height = 16
}
}),
compile(options={}){
let facenamesbb = ["up", "down", "north", "south", "east", "west"]
let facenamescr = ["localPosY", "localNegY", "localNegZ", "localPosZ", "localPosX", "localNegX"]
cuboids = []
texturesUsed = []
texturesFilesUsed = []
textures = {}
function compileCube(obj){
let uvs = {}
for(let f of Object.keys(obj.faces)){
let uv = obj.faces[f].uv
let texture = Texture.all.filter((x) => {return x.uuid == obj.faces[f].texture})[0]
texture = (texture === undefined) ? "empty.png" : texture.name
let face = obj.faces[f]
uvs[f] = [uv[0], uv[1], uv[2], uv[3], face, texture]
texturesUsed.push(texture)
}
let cube = {
"localBounds": [...obj.from, ...obj.to],
"faces":
{
"localNegX": {"uv": uvs.west.slice(0, 4), "ambientocclusion": uvs.west[4].tint === 0,
"cullFace": uvs.west[4].cullFace !== "", "texture": uvs.west[5]},
"localPosX": {"uv": uvs.east.slice(0, 4), "ambientocclusion": uvs.east[4].tint === 0,
"cullFace": uvs.east[4].cullFace !== "", "texture": uvs.east[5]},
"localNegY": {"uv": uvs.down.slice(0, 4), "ambientocclusion": uvs.down[4].tint === 0,
"cullFace": uvs.down[4].cullFace !== "", "texture": uvs.down[5]},
"localPosY": {"uv": uvs.up.slice(0, 4), "ambientocclusion": uvs.up[4].tint === 0,
"cullFace": uvs.up[4].cullFace !== "", "texture": uvs.up[5]},
"localNegZ": {"uv": uvs.north.slice(0, 4), "ambientocclusion": uvs.north[4].tint === 0,
"cullFace": uvs.north[4].cullFace !== "", "texture": uvs.north[5]},
"localPosZ": {"uv": uvs.south.slice(0, 4), "ambientocclusion": uvs.south[4].tint === 0,
"cullFace": uvs.south[4].cullFace !== "", "texture": uvs.south[5]}
}
}
for(let f = 0; f < 6; f++){
if(uvs[facenamesbb[f]][4].rotation > 0){
cube.faces[facenamescr[f]].uvRotation = uvs[facenamesbb[f]][4].rotation
}
}
cuboids.push(cube)
}
function compileGroup(group){
group.children.forEach(obj => {
if (obj instanceof Group) {
compileGroup(obj);
} else if (obj instanceof Cube) {
compileCube(obj)
}
})
}
Outliner.root.forEach(obj => {
if (obj instanceof Group) {
compileGroup(obj);
} else if (obj instanceof Cube) {
compileCube(obj)
}
})
for(let i = 0; i < texturesUsed.length; i++){
if(texturesUsed[i] == null){
continue
}
const name = texturesUsed[i]
textures[name] = { "fileName": name }
}
return JSON.stringify({"textures": textures, "cuboids": cuboids})
},
parse(rawJSONstring, path){
let loadedTextures = {}
let patharr = path.split(/[\\\/]/g)
patharr = patharr.slice(0, patharr.length - 1)
if(patharr.length > 1){
patharr = [...patharr.slice(0, patharr.length - 2), "textures", patharr.pop()]
}
let facenamesbb = ["up", "down", "north", "south", "east", "west"]
let facenamescr = ["localPosY", "localNegY", "localNegZ", "localPosZ", "localPosX", "localNegX"]
let allTexturesSpecified = false
let data = rawJSONstring instanceof String ? JSON.parse(rawJSONstring) : rawJSONstring
if(data.textures == undefined){
data.textures = {}
}
for(let t of Object.keys(data.textures)){
let newtexture = new Texture().fromPath([...patharr, data.textures[t].fileName].join("/"))
loadedTextures[t] = newtexture.add()
}
if(Texture.all.length > 0) {
setTimeout(() => {
Project.texture_width = Texture.all[0].width
Project.texture_height = Texture.all[0].height
}, 50);
}
if(data.textures["all"] != undefined){
allTexturesSpecified = true
}
function getFaceUV(cuboid, face, uv){
return cuboid.faces[face].uv[uv]
}
function setUVforFace(cube, cuboid, facenamebb, facenamecr){
texture = allTexturesSpecified ? data.textures["all"] : data.textures[cuboid.faces[facenamecr].texture]
cube.faces[facenamebb].uv =[getFaceUV(cuboid, facenamecr, 0),
getFaceUV(cuboid, facenamecr, 1),
getFaceUV(cuboid, facenamecr, 2),
getFaceUV(cuboid, facenamecr, 3)]
cube.faces[facenamebb].texture = Texture.all.filter((x) => {return x.name == texture.fileName})[0]
}
if(data.cuboids == undefined){
throw Error(`No cuboids found in file ${path}`)
}
for(let cuboid of data.cuboids){
let from = cuboid.localBounds.slice(0, 3)
let to = cuboid.localBounds.slice(3, 6)
let cube = new Cube({from: from, to: to})
for(let i = 0; i < 6; i++){
try{
setUVforFace(cube, cuboid, facenamesbb[i], facenamescr[i])
cube.faces[facenamesbb[i]].texture = loadedTextures[cuboid.faces[facenamescr[i]].texture]
}catch(error){
}
cube.faces[facenamesbb[i]].cullface = cuboid.faces[facenamescr[i]].cullFace ? facenamesbb[i] : ""
cube.faces[facenamesbb[i]].tint = cuboid.faces[facenamescr[i]].ambientocclusion ? 0 : -1
}
cube.addTo(Group.all.last()).init()
}
setTimeout(() => {
Canvas.updateAll()
}, 50);
return true;
}
})
import_action = new Action('import_cosmic_reach_model', {
name: 'Import Cosmic Reach Model',
description: '',
icon: null,
category: 'file',
click() {
Blockbench.import({
extensions: ['json'],
type: 'Cosmic Reach Model',
readtype: 'text',
resource_id: 'json'
}, files => {
try{
codec.parse(files[0].content, files[0].path);
Canvas.updateAll()
}catch(error){
dialog.lines = `<div>
<h1>Unable to import file.</h1>
<p>${error}</p>
</div>`.split("\n")
dialog.show()
}
})
}
})
export_action = new Action('export_cosmic_reach_model', {
name: 'Export Cosmic Reach Model',
description: '',
icon: null,
category: 'file',
click() {
try{
codec.export();
}catch(error){
dialog.lines = `<div>
<h1>Unable to export file.</h1>
<p>${error}</p>
</div>`.split("\n")
dialog.show()
}
}
})
MenuBar.addAction(import_action, 'file.import')
MenuBar.addAction(export_action, 'file.export')
},
onunload() {
import_action.delete();
export_action.delete();
}
})
})()