-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexport_effects.py
More file actions
285 lines (244 loc) · 10.9 KB
/
Copy pathexport_effects.py
File metadata and controls
285 lines (244 loc) · 10.9 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
"""Export a zone's particle effects as data the Unity editor script
(RoseEffects.cs) turns into ParticleSystems.
ROSE attaches most effects to *objects*: a ZSC model carries "dummy points",
each with an effect index into the model's effect list (a .EFT) and a local
transform (the attach point + orientation). E.g. the fountain model has 6 dummies
pointing at bunsudae01.eft (Korean *bunsudae* = fountain) around its petal holes;
streetlights -> streetlight01l.eft, braziers -> _agit_fire01.eft, etc. We resolve
each placed object's dummies to world-space (pos + rotation) and parse the EFT ->
.PTL emitters. Standalone EFFECT-lump entries (not attached to a model) are added
too. Fountains additionally get a flat translucent basin pool (no particle effect
provides the standing water).
Output: <bundle>/Effects/effects.json (+ Effects/Textures/*.png). Coords are raw
ROSE; the Unity script parents under the map's "Objects" group so the
z-up->y-up + 0.01 map transform is inherited.
"""
from __future__ import annotations
import os
import re
import sys
import json
import numpy as np
from PIL import Image
_HERE = os.path.dirname(os.path.abspath(__file__))
for _p in (_HERE, os.path.dirname(_HERE)):
if _p not in sys.path:
sys.path.insert(0, _p)
import config
import zone as Z
import rose_ifo as RI
import rose_effect
from rose_zsc import read_zsc
_FOUNTAIN_RE = re.compile(r"fountain\d", re.I)
# ----------------------------------------------------------------- asset lookup
def _resolve(rel):
parts = [p for p in rel.replace("\\", "/").split("/") if p]
if parts and parts[0].lower() == "3ddata":
parts = parts[1:]
cur = config.ASSET_ROOT
for p in parts:
if not os.path.isdir(cur):
return None
m = [e for e in os.listdir(cur) if e.lower() == p.lower()]
if not m:
return None
cur = os.path.join(cur, m[0])
return cur if os.path.exists(cur) else None
# ----------------------------------------------------------------------- math
def _quat_matrix(qx, qy, qz, qw):
n = (qx * qx + qy * qy + qz * qz + qw * qw) ** 0.5 or 1.0
qx, qy, qz, qw = qx / n, qy / n, qz / n, qw / n
return np.array([
[1 - 2 * (qy * qy + qz * qz), 2 * (qx * qy - qz * qw), 2 * (qx * qz + qy * qw), 0],
[2 * (qx * qy + qz * qw), 1 - 2 * (qx * qx + qz * qz), 2 * (qy * qz - qx * qw), 0],
[2 * (qx * qz - qy * qw), 2 * (qy * qz + qx * qw), 1 - 2 * (qx * qx + qy * qy), 0],
[0, 0, 0, 1]], dtype=np.float64)
def _compose(pos, quat_xyzw, scale):
M = _quat_matrix(*quat_xyzw)
M[:3, 0] *= scale[0]; M[:3, 1] *= scale[1]; M[:3, 2] *= scale[2]
M[0, 3], M[1, 3], M[2, 3] = pos
return M
def _mat_to_quat_xyzw(M):
R = np.array(M, dtype=np.float64)[:3, :3].copy()
for c in range(3): # strip scale
n = np.linalg.norm(R[:, c]) or 1.0
R[:, c] /= n
t = R[0, 0] + R[1, 1] + R[2, 2]
if t > 0:
s = 0.5 / np.sqrt(t + 1.0)
w, x, y, z = 0.25 / s, (R[2, 1] - R[1, 2]) * s, (R[0, 2] - R[2, 0]) * s, (R[1, 0] - R[0, 1]) * s
elif R[0, 0] > R[1, 1] and R[0, 0] > R[2, 2]:
s = 2.0 * np.sqrt(1.0 + R[0, 0] - R[1, 1] - R[2, 2])
w, x, y, z = (R[2, 1] - R[1, 2]) / s, 0.25 * s, (R[0, 1] + R[1, 0]) / s, (R[0, 2] + R[2, 0]) / s
elif R[1, 1] > R[2, 2]:
s = 2.0 * np.sqrt(1.0 + R[1, 1] - R[0, 0] - R[2, 2])
w, x, y, z = (R[0, 2] - R[2, 0]) / s, (R[0, 1] + R[1, 0]) / s, 0.25 * s, (R[1, 2] + R[2, 1]) / s
else:
s = 2.0 * np.sqrt(1.0 + R[2, 2] - R[0, 0] - R[1, 1])
w, x, y, z = (R[1, 0] - R[0, 1]) / s, (R[0, 2] + R[2, 0]) / s, (R[1, 2] + R[2, 1]) / s, 0.25 * s
return [float(x), float(y), float(z), float(w)]
# ------------------------------------------------------------- emitter flatten
def _event(em, etype):
return next((e for e in em.get("events", []) if e.get("type") == etype), None)
def _emitter_params(em):
"""Flatten a parsed PTL emitter; `texture` is the raw asset path (the web
loads it via texUrl; the bundle build() rewrites it to a shipped .png)."""
size_ev = _event(em, rose_effect.EV_SIZE)
alpha_ev = _event(em, rose_effect.EV_ALPHA)
color_ev = _event(em, rose_effect.EV_COLOR)
vel_ev = _event(em, rose_effect.EV_VEL)
size = size_ev["size"] if size_ev else [20, 20, 20, 20]
alpha = alpha_ev["v"] if alpha_ev else [1.0, 1.0]
color = color_ev["color"] if color_ev else None
vel = vel_ev["vel"] if vel_ev else [0, 0, 0, 0, 0, 0]
src, dst, _op = em.get("blend", [5, 2, 1])
additive = not (src == 4 and dst == 5) # 4/5 = SRCALPHA/INVSRCALPHA -> normal alpha
return {
"texture": em.get("texture") or None,
"life": em.get("life", [50, 50]),
"emit_rate": em.get("emit_rate", [100, 100]),
"emit_radius": em.get("emit_radius", [0, 0, 0, 0, 0, 0]),
"gravity": em.get("gravity", [0, 0, 0, 0, 0, 0]),
"num_particles": em.get("num_particles", 40),
"size": size, "alpha": alpha, "color": color, "vel": vel,
"additive": additive,
}
def _ship_texture(rel, out_dir, texmap):
if not rel:
return None
key = rel.lower()
if key in texmap:
return texmap[key]
ab = _resolve(rel)
fn = os.path.splitext(os.path.basename(rel))[0] + ".png"
if ab:
try:
im = Image.open(ab); im.load()
im.convert("RGBA").save(os.path.join(out_dir, fn), "PNG")
texmap[key] = fn
return fn
except Exception:
pass
texmap[key] = None
return None
# ------------------------------------------------------------------- gathering
def _model_is_fountain(zsc, oid):
if not zsc or not (0 <= oid < len(zsc.models)):
return False
for p in zsc.models[oid].parts:
mesh = zsc.meshes[p.mesh_idx] if 0 <= p.mesh_idx < len(zsc.meshes) else ""
if _FOUNTAIN_RE.search(mesh or ""):
return True
return False
def compute(key):
"""Gather all of a zone's effect placements (object-dummy + standalone) and
fountains. Emitter `texture` fields hold raw asset paths. Used directly by the
web viewer; build() ships PNGs and rewrites the paths for the Unity bundle."""
z = Z.find_zone(key)
if not z:
raise KeyError(key)
placements, fountains, eft_cache = [], [], {}
packs = {}
for kind, rel in (("OBJECT", z.get("deco_pack")), ("CNST", z.get("cnst_pack"))):
ab = _resolve(rel) if rel else None
packs[kind] = read_zsc(ab) if ab else None
def emitters_for(eft_rel):
keyl = eft_rel.lower()
if keyl not in eft_cache:
ab = _resolve(eft_rel)
raw = rose_effect.parse_effect(ab, _resolve)["emitters"] if ab else []
eft_cache[keyl] = [_emitter_params(em) for em in raw]
return [dict(e) for e in eft_cache[keyl] if e["texture"]]
for x, y, stem in Z._tiles_in(z["dir"]):
try:
ifo = RI.read_ifo(stem + ".IFO")
except Exception:
continue
# 1. object-attached effects (ZSC dummy points) — the bulk of map FX
for kind, lt in (("OBJECT", RI.LUMP_OBJECT), ("CNST", RI.LUMP_CNST)):
lump = ifo.lumps.get(lt)
zsc = packs.get(kind)
if not lump or not zsc:
continue
for o in lump.objects:
oid = o.object_id
if not (0 <= oid < len(zsc.models)):
continue
model = zsc.models[oid]
ifoM = _compose(o.pos, o.rot, o.scale)
for d in model.dummies:
if not (0 <= d.effect_idx < len(zsc.effects)):
continue
eft_rel = zsc.effects[d.effect_idx]
if not eft_rel.lower().endswith(".eft"):
continue
r = d.rotate # ZSC quat (w,x,y,z)
dM = _compose(d.position, (r[1], r[2], r[3], r[0]), d.scale)
world = ifoM @ dM
ems = emitters_for(eft_rel)
if not ems:
continue
placements.append({
"pos": [float(v) for v in world[:3, 3]],
"rot": _mat_to_quat_xyzw(world),
"emitters": ems,
})
if _model_is_fountain(zsc, oid):
fountains.append({"pos": list(o.pos), "scale": (o.scale[2] if o.scale else 1.0)})
# 2. standalone EFFECT-lump effects (not attached to a model)
el = ifo.lumps.get(RI.LUMP_EFFECT)
if el:
for o in el.objects:
eft = o.extra.get("effect_file")
if not eft:
continue
ems = emitters_for(eft)
if not ems:
continue
placements.append({
"pos": list(o.pos),
"rot": list(o.rot), # IFO quat is already x,y,z,w
"emitters": ems,
})
return {"zone": key, "placements": placements, "fountains": fountains}
# ------------------------------------------------------------- bundle exporter
def build(key, bundle):
eff_dir = os.path.join(bundle, "Effects")
tex_dir = os.path.join(eff_dir, "Textures")
os.makedirs(tex_dir, exist_ok=True)
data = compute(key)
texmap = {}
for pl in data["placements"]:
for em in pl["emitters"]:
em["texture"] = _ship_texture(em["texture"], tex_dir, texmap)
# drop emitters whose texture failed to convert
for pl in data["placements"]:
pl["emitters"] = [e for e in pl["emitters"] if e["texture"]]
# generated soft sprite for the basin pool (no .EFT provides it)
soft = Image.new("RGBA", (64, 64), (0, 0, 0, 0))
sp = soft.load()
import math
for yy in range(64):
for xx in range(64):
d = math.hypot(xx - 32, yy - 32) / 32.0
a = max(0.0, 1.0 - d)
sp[xx, yy] = (210, 235, 255, int(255 * a * a))
soft.save(os.path.join(tex_dir, "fountain_soft.png"), "PNG")
manifest = {
"zone": key,
"rose_to_unity": {"rotate_x_deg": -90, "scale": 0.01},
"soft_sprite": "fountain_soft.png",
"placements": data["placements"],
"fountains": data["fountains"],
}
with open(os.path.join(eff_dir, "effects.json"), "w") as f:
json.dump(manifest, f, indent=1)
emitter_total = sum(len(p["emitters"]) for p in data["placements"])
return {"placements": len(data["placements"]), "emitters": emitter_total,
"fountains": len(data["fountains"]),
"textures": len([v for v in texmap.values() if v])}
if __name__ == "__main__":
k = sys.argv[1] if len(sys.argv) > 1 else "JPT01-1"
out = os.path.join(_HERE, "exports", "%s_fbx" % k)
os.makedirs(out, exist_ok=True)
print(json.dumps(build(k, out), indent=2))