BlueGit

timfa raytracer

Files

raytracer / src / ppda / loadPpda.js

loadPpda.js

loadPpda.js 4.4 KiB Download Raw
1/**
2 * Strict-enough .ppda loader for formats 1–5 (Paperdoll AtlasSet consumer).
3 */
4var PpdaLoader =
5{
6 SUPPORTED_FORMATS: [1, 2, 3, 4, 5],
7
8 parse: function (jsonText, options)
9 {
10 options = options || {};
11 var doc;
12 try
13 { doc = typeof jsonText === "string" ? JSON.parse(jsonText) : jsonText; }
14 catch (e)
15 { throw new Error("ppda: ongeldige JSON (" + e.message + ")"); }
16
17 if (!doc || typeof doc !== "object")
18 { throw new Error("ppda: root moet een object zijn"); }
19
20 var ver = doc.format_version;
21 if (this.SUPPORTED_FORMATS.indexOf(ver) < 0)
22 { throw new Error("ppda: format_version " + ver + " niet ondersteund (wel: " + this.SUPPORTED_FORMATS.join(",") + ")"); }
23
24 if (!doc.sex || !doc.settype)
25 { throw new Error("ppda: sex/settype verplicht"); }
26 if (!doc.fps || doc.fps < 1)
27 { throw new Error("ppda: fps ongeldig"); }
28 if (!doc.cell_width || !doc.cell_height)
29 { throw new Error("ppda: cell_width/height verplicht"); }
30 if (!Array.isArray(doc.pitches) || !Array.isArray(doc.yaws) || !doc.pitches.length || !doc.yaws.length)
31 { throw new Error("ppda: pitches/yaws verplicht"); }
32 if (!doc.maps || typeof doc.maps !== "object")
33 { throw new Error("ppda: maps verplicht"); }
34 ["diffuse", "depth"].forEach(function (k)
35 {
36 if (!doc.maps[k] || !doc.maps[k].file)
37 { throw new Error("ppda: maps." + k + ".file verplicht"); }
38 if (String(doc.maps[k].file).indexOf("..") >= 0 || String(doc.maps[k].file).indexOf("/") >= 0 || String(doc.maps[k].file).indexOf("\\") >= 0)
39 { throw new Error("ppda: maps." + k + ".file mag geen pad bevatten"); }
40 });
41 if (!Array.isArray(doc.clips) || !doc.clips.length)
42 { throw new Error("ppda: clips verplicht"); }
43
44 for (var ci = 0; ci < doc.clips.length; ci++)
45 {
46 var clip = doc.clips[ci];
47 if (!clip.frames || clip.frames.length !== clip.frame_count)
48 { throw new Error("ppda: clip '" + (clip.name || ci) + "' frames≠frame_count"); }
49 for (var fi = 0; fi < clip.frames.length; fi++)
50 {
51 var fr = clip.frames[fi];
52 if (!fr.views || !fr.views.length)
53 { throw new Error("ppda: frame zonder views"); }
54 for (var vi = 0; vi < fr.views.length; vi++)
55 {
56 var v = fr.views[vi];
57 if (v.w == null || v.h == null || v.x == null || v.y == null)
58 { throw new Error("ppda: view mist x/y/w/h"); }
59 }
60 }
61 }
62
63 var encoding = doc.depth_encoding;
64 var bits = doc.depth_bits;
65 if (!encoding)
66 {
67 // v1 / legacy long text in identity.depth → gray16
68 encoding = "gray";
69 bits = bits || 16;
70 }
71 else if (/gray/i.test(encoding) && encoding.indexOf("rgb_") < 0)
72 {
73 encoding = "gray";
74 }
75 else if (encoding.indexOf("rgb_bands") >= 0)
76 { encoding = "rgb_bands"; }
77 else if (encoding.indexOf("rgb_packed") >= 0)
78 { encoding = "rgb_packed"; }
79
80 var near = doc.depth_near != null ? doc.depth_near : (doc.identity && doc.identity.near);
81 var far = doc.depth_far != null ? doc.depth_far : (doc.identity && doc.identity.far);
82 if (near == null || far == null)
83 { throw new Error("ppda: depth_near/far of identity.near/far verplicht"); }
84
85 var fmt = DepthDecode.parseFormat(encoding, bits || 16);
86
87 return {
88 format_version: ver,
89 sex: doc.sex,
90 settype: doc.settype,
91 fps: doc.fps,
92 cell_width: doc.cell_width,
93 cell_height: doc.cell_height,
94 pitches: doc.pitches.slice(),
95 yaws: doc.yaws.slice(),
96 maps: doc.maps,
97 clips: doc.clips,
98 identity: doc.identity || null,
99 project: doc.project || null,
100 depth_near: Number(near),
101 depth_far: Number(far),
102 depth_format: fmt,
103 camera_distance: doc.identity && doc.identity.camera_distance != null
104 ? Number(doc.identity.camera_distance)
105 : 10,
106 height_milli: doc.identity && doc.identity.framing
107 ? doc.identity.framing.height_milli
108 : 2600,
109 raw: doc
110 };
111 },
112
113 compatKey: function (ppda)
114 {
115 var id = ppda.identity || {};
116 return {
117 sex: ppda.sex,
118 body_model_sha256: id.body_model_sha256 || "",
119 fps: ppda.fps,
120 cell_width: ppda.cell_width,
121 cell_height: ppda.cell_height,
122 pitches: ppda.pitches.join(","),
123 yaws: ppda.yaws.join(","),
124 near: ppda.depth_near,
125 far: ppda.depth_far
126 };
127 },
128
129 assertCompatible: function (a, b)
130 {
131 var ka = this.compatKey(a);
132 var kb = this.compatKey(b);
133 var keys = Object.keys(ka);
134 for (var i = 0; i < keys.length; i++)
135 {
136 var k = keys[i];
137 if (String(ka[k]) !== String(kb[k]))
138 { throw new Error("ppda incompatible: " + k + " (" + ka[k] + " vs " + kb[k] + ")"); }
139 }
140 }
141};
142
143if (typeof module !== "undefined" && module.exports)
144{ module.exports = PpdaLoader; }