-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathModelJSON.java
More file actions
422 lines (340 loc) · 9.88 KB
/
Copy pathModelJSON.java
File metadata and controls
422 lines (340 loc) · 9.88 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
package javaforce.gl.model;
import java.io.*;
import javaforce.*;
import static javaforce.JSON.Element;
import javaforce.gl.*;
/** JSON Model reader.
*
* see https://minecraft.fandom.com/wiki/Tutorials/Models
*
* Notes:
* - xyz coords and uv texture coords are based on pixels
*
* @author pquiring
*/
public class ModelJSON implements Model_IO {
private static boolean debug = false;
public float scale = 16.0f;
private float tw, th;
private float width, height, depth;
private boolean mirror;
public ModelJSON() {
this.scale = 16f;
}
public ModelJSON(float scale) {
this.scale = scale;
}
public Model load(String filename) {
try {
InputStream is = new FileInputStream(filename);
Model model = load(is);
is.close();
return model;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public Model load(InputStream is) {
Model model = new Model();
int mapidx = 0;
try {
String json = new String(is.readAllBytes());
JSON.debug = debug;
Element root = JSON.parse(json);
if (debug) {
root.print();
}
for(Element child : root.children) {
if (child.key.startsWith("geometry.")) {
Element geo = child;
//TODO : find entity based on name
String ename = geo.key.substring(9);
int idx = ename.indexOf('.');
if (idx != -1) {
ename = ename.substring(0, idx);
}
Element etw = geo.getChild("texturewidth");
tw = getValue(etw.value);
Element eth = geo.getChild("textureheight");
th = getValue(eth.value);
Element bones = geo.getChild("bones");
for(Element box : bones.children) {
Element name = box.getChild("name");
Element pvt = box.getChild("pivot");
float[] pvts = getValues(pvt);
//Element rot = box.getChild("rotation"); //optional
//float[] rots = getValues(rot);
Element cubes = box.getChild("cubes");
if (cubes == null) continue;
for(Element cube : cubes.children) {
Element org = cube.getChild("origin");
//origin is lowest coord of cube in all 3 dims
float[] orgs = getValues(org);
Element size = cube.getChild("size");
float[] sizes = getValues(size);
Element uv = cube.getChild("uv");
float[] uvs = getValues(uv);
//text coords have 0,0 at top left corner
float u = uvs[0];
float v = uvs[1];
Element _inflate = cube.getChild("inflate");
if (_inflate != null) {
float inflate = getValue(_inflate.value) / scale;
float inflate_2 = inflate * 2.0f;
sizes[0] += inflate_2;
sizes[1] += inflate_2;
sizes[2] += inflate_2;
orgs[0] -= inflate;
orgs[1] -= inflate;
orgs[2] -= inflate;
}
Element _mirror = cube.getChild("mirror");
mirror = _mirror != null && _mirror.value.equals("true");
float x = orgs[0];
float y = orgs[1];
float z = orgs[2];
width = sizes[0]; //x
height = sizes[1]; //y
depth = sizes[2]; //z
{
Object3 obj = new Object3();
obj.type = GL.GL_QUADS;
UVMap map = obj.createUVMap();
map.name = "map-" + mapidx++;
model.addObject(obj);
//set origin (pivot)
obj.org.x = pvts[0] / scale;
obj.org.y = pvts[1] / scale;
obj.org.z = pvts[2] / scale;
obj.name = name.value;
//each face starts at bottom left corner of texture
//start @ origin (x,y,z)
//uv starts high, bring down to first face (E)
v += depth + height;
//move to E
x += width;
z += depth;
if (mirror)
addFaceE(obj, x, y, z, u + depth + width, v);
else
addFaceE(obj, x, y, z, u, v);
//move to N
z -= depth;
u += depth;
addFaceN(obj, x, y, z, u, v);
if (mirror) mirror(obj);
//move to W
x -= width;
u += width;
if (mirror)
addFaceW(obj, x, y, z, u - depth - width, v);
else
addFaceW(obj, x, y, z, u, v);
//move to S
z += depth;
u += depth;
addFaceS(obj, x, y, z, u, v);
if (mirror) mirror(obj);
//move to B (two steps)
v -= height;
u -= depth;
addFaceB(obj, x, y, z, u, v);
if (mirror) mirror(obj);
//move to A
y += height;
u -= width;
addFaceA(obj, x, y, z, u, v);
if (mirror) mirror(obj);
}
}
}
}
}
return model;
} catch (Exception e) {
JFLog.log(e);
}
return null;
}
private float[] xyz = new float[3];
private float[] uv = new float[2];
private int[] pts = new int[4];
private void addFaceW(Object3 obj, float x, float y, float z, float u, float v) {
int vidx = obj.getVertexCount();
//add 4 vertex in CCW order starting at origin
addVertex(obj, x, y, z, u, v);
z += depth;
u += depth;
addVertex(obj, x, y, z, u, v);
y += height;
v -= height;
addVertex(obj, x, y, z, u, v);
z -= depth;
u -= depth;
addVertex(obj, x, y, z, u, v);
for(int a=0;a<4;a++) {
pts[a] = vidx++;
}
obj.addPoly(pts);
}
private void addFaceS(Object3 obj, float x, float y, float z, float u, float v) {
int vidx = obj.getVertexCount();
//add 4 vertex in CCW order
addVertex(obj, x, y, z, u, v);
x += width;
u += width;
addVertex(obj, x, y, z, u, v);
y += height;
v -= height;
addVertex(obj, x, y, z, u, v);
x -= width;
u -= width;
addVertex(obj, x, y, z, u, v);
for(int a=0;a<4;a++) {
pts[a] = vidx++;
}
obj.addPoly(pts);
}
private void addFaceE(Object3 obj, float x, float y, float z, float u, float v) {
int vidx = obj.getVertexCount();
//add 4 vertex in CCW order
addVertex(obj, x, y, z, u, v);
z -= depth;
u += depth;
addVertex(obj, x, y, z, u, v);
y += height;
v -= height;
addVertex(obj, x, y, z, u, v);
z += depth;
u -= depth;
addVertex(obj, x, y, z, u, v);
for(int a=0;a<4;a++) {
pts[a] = vidx++;
}
obj.addPoly(pts);
}
private void addFaceN(Object3 obj, float x, float y, float z, float u, float v) {
int vidx = obj.getVertexCount();
//add 4 vertex in CCW order
addVertex(obj, x, y, z, u, v);
x -= width;
u += width;
addVertex(obj, x, y, z, u, v);
y += height;
v -= height;
addVertex(obj, x, y, z, u, v);
x += width;
u -= width;
addVertex(obj, x, y, z, u, v);
for(int a=0;a<4;a++) {
pts[a] = vidx++;
}
obj.addPoly(pts);
}
//TODO : not sure if this needs to be mirrored in the x axis
private void addFaceB(Object3 obj, float x, float y, float z, float u, float v) {
int vidx = obj.getVertexCount();
//add 4 vertex in CCW order
addVertex(obj, x, y, z, u, v);
z -= depth;
v -= depth;
addVertex(obj, x, y, z, u, v);
x += width;
u += width;
addVertex(obj, x, y, z, u, v);
z += depth;
v += depth;
addVertex(obj, x, y, z, u, v);
for(int a=0;a<4;a++) {
pts[a] = vidx++;
}
obj.addPoly(pts);
}
private void addFaceA(Object3 obj, float x, float y, float z, float u, float v) {
int vidx = obj.getVertexCount();
//add 4 vertex in CCW order
addVertex(obj, x, y, z, u, v);
x += width;
u += width;
addVertex(obj, x, y, z, u, v);
z -= depth;
v -= depth;
addVertex(obj, x, y, z, u, v);
x -= width;
u -= width;
addVertex(obj, x, y, z, u, v);
for(int a=0;a<4;a++) {
pts[a] = vidx++;
}
obj.addPoly(pts);
}
private void mirror(Object3 obj) {
//mirror u text coord in vertex 1 <-> 2 and 3 <-> 4
float[] uv = obj.getUVMap(0).uvl.getBuffer();
int idx = obj.getVertexCount();
int v1 = (idx - 4) * 2;
int v2 = (idx - 3) * 2;
int v3 = (idx - 2) * 2;
int v4 = (idx - 1) * 2;
float tmp;
//swap 1 <-> 2
tmp = uv[v1];
uv[v1] = uv[v2];
uv[v2] = tmp;
//swap 3 <-> 4
tmp = uv[v3];
uv[v3] = uv[v4];
uv[v4] = tmp;
}
private void addVertex(Object3 obj, float x, float y, float z, float u, float v) {
//coords are based on pixels, the org game is based on 8x textures
xyz[0] = x / scale;
xyz[1] = y / scale;
xyz[2] = z / scale;
//scale uv : 0-1
uv[0] = u / tw;
uv[1] = v / th;
obj.addVertex(xyz, uv);
}
private float getValue(String e) {
return Float.valueOf(e);
}
private float[] getValues(Element array) {
int cnt = array.getChildCount();
float[] values = new float[cnt];
for(int i=0;i<cnt;i++) {
Element child = array.getChild(i);
values[i] = Float.valueOf(child.value);
}
return values;
}
public boolean save(Model model, OutputStream os) {
return false;
}
}
/*
JSON format:
{
"format_version": "",
"geometry_*": {
"texturewidth": "*",
"textureheight": "*",
"bones": [
{
"name": "*",
"pivot": [ x, y, z ],
"rotation": [ x, y, z ],
"cubes": [
{
"origin": [ x, y, z ],
"size": [ x, y, z ],
"uv": [ u ,v ]
}
]
},
...
]
}
}
*/