-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_maps.gd
314 lines (306 loc) · 11.6 KB
/
generate_maps.gd
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
extends Control
const TEX_SIZE: int = 512
var draw_data: = {}
var min_point = Vector3()
var max_point = Vector3()
var min_normal = Vector3()
var max_normal = Vector3()
var maps = {}
var vert_indices = {}
onready var characters = [load("res://characters/female_2018.escn"), load("res://characters/male_2018.escn")]
func find_mesh_name(base: Node, mesh_name: String) -> MeshInstance:
var queue = [base]
var mi: MeshInstance
while queue.size() > 0:
var item = queue[0]
queue.pop_front()
if item is MeshInstance && item.name == mesh_name:
mi = item
break
for c in item.get_children():
queue.push_back(c)
return mi
func find_same_verts():
for chdata in range(characters.size()):
var ch_scene = characters[chdata].instance()
var bmesh = find_mesh_name(ch_scene, "body")
if !vert_indices.has(chdata):
vert_indices[chdata] = {}
for surface in range(bmesh.mesh.get_surface_count()):
var arrays: Array = bmesh.mesh.surface_get_arrays(surface).duplicate(true)
for index1 in range(arrays[ArrayMesh.ARRAY_VERTEX].size()):
var v1: Vector3 = arrays[ArrayMesh.ARRAY_VERTEX][index1]
var ok = false
for rk in vert_indices[chdata].keys():
if (v1 - rk).length() < 0.001:
ok = true
vert_indices[chdata][rk].push_back(index1)
if !ok:
vert_indices[chdata][v1] = [index1]
func find_min_max(mesh: ArrayMesh):
min_point = mesh.surface_get_blend_shape_arrays(0)[0][ArrayMesh.ARRAY_VERTEX][0] - mesh.surface_get_arrays(0)[ArrayMesh.ARRAY_VERTEX][0]
max_point = mesh.surface_get_blend_shape_arrays(0)[0][ArrayMesh.ARRAY_VERTEX][0] - mesh.surface_get_arrays(0)[ArrayMesh.ARRAY_VERTEX][0]
for sc in range(mesh.get_surface_count()):
var bshapes: Array = mesh.surface_get_blend_shape_arrays(sc).duplicate(true)
var arrays: Array = mesh.surface_get_arrays(sc).duplicate(true)
for src in bshapes:
for index in range(arrays[ArrayMesh.ARRAY_VERTEX].size()):
var v: Vector3 = src[ArrayMesh.ARRAY_VERTEX][index] - arrays[ArrayMesh.ARRAY_VERTEX][index]
var n: Vector3 = src[ArrayMesh.ARRAY_NORMAL][index] - arrays[ArrayMesh.ARRAY_NORMAL][index]
for ipos in range(3):
if min_point[ipos] > v[ipos]:
min_point[ipos] = v[ipos]
if max_point[ipos] < v[ipos]:
max_point[ipos] = v[ipos]
if min_normal[ipos] > n[ipos]:
min_normal[ipos] = n[ipos]
if max_normal[ipos] < n[ipos]:
max_normal[ipos] = n[ipos]
print("min: ", min_point, "max: ", max_point)
func check_triangle(verts: Array) -> bool:
var uv1 = verts[0].uv
var uv2 = verts[1].uv
var uv3 = verts[2].uv
var v1 = uv1 - uv3
var v2 = uv2 - uv3
var v3 = v2 - v1
if v1.length() * TEX_SIZE < 1.2:
return false
if v2.length() * TEX_SIZE < 1.2:
return false
if v3.length() * TEX_SIZE < 1.2:
return false
var sumdata = Vector3()
for k in range(2):
for ks in range(verts[k].shape.size()):
sumdata += verts[k].shape[ks]
if sumdata.length() < 0.001:
return false
return true
func pad_morphs(morphs: Dictionary, nshapes: Dictionary):
for mesh in morphs.keys():
for m in morphs[mesh].keys():
var ns = nshapes[mesh][m]
for t in range(morphs[mesh][m].size()):
for v in range(morphs[mesh][m][t].size()):
# print(morphs[m][t][v])
for s in range(morphs[mesh][m][t][v].shape.size()):
for u in range(2):
var cd : float = max_point[u] - min_point[u]
var ncd : float = max_normal[u] - min_normal[u]
var d = morphs[mesh][m][t][v].shape[s][u]
morphs[mesh][m][t][v].shape[s][u] = (d - min_point[u]) / cd
var ew = morphs[mesh][m][t][v].shape[s][u] * cd + min_point[u]
assert abs(ew - d) < 0.001
morphs[mesh][m][t][v].normal[s][u] = (morphs[mesh][m][t][v].normal[s][u] - min_normal[u]) / ncd
func fill_draw_data(morphs: Dictionary, draw_data: Dictionary, morph_names: Dictionary, nshapes: Dictionary, rects: Dictionary):
var offset : = 0
for mesh in morphs.keys():
for m in morphs[mesh].keys():
if !draw_data.has(m):
draw_data[m] = {}
var ns = nshapes[mesh][m]
for sh in range(ns):
print(morph_names[mesh][m][sh], ": ", m, " ", sh + offset)
draw_data[m][sh + offset] = {"name": morph_names[mesh][m][sh], "triangles": [], "rects": []}
for t in range(morphs[mesh][m].size()):
var tri : = []
var midp = Vector2()
var sp = Vector3()
for v in range(morphs[mesh][m][t].size()):
midp += morphs[mesh][m][t][v].uv
midp /= 3.0
for v in range(morphs[mesh][m][t].size()):
var pt = morphs[mesh][m][t][v].uv - midp
var dpt = pt.normalized() * (3.5 / TEX_SIZE)
tri.push_back({"uv": morphs[mesh][m][t][v].uv + dpt, "shape": morphs[mesh][m][t][v].shape[sh], "normal": morphs[mesh][m][t][v].normal[sh]})
draw_data[m][sh + offset].triangles.push_back(tri)
draw_data[m][sh + offset].rect = rects[mesh][m][sh]
offset = draw_data[m].keys().size()
var common = [
load("res://characters/common_part1.escn"),
load("res://characters/common_part2.escn"),
load("res://characters/common_part3.escn"),
load("res://characters/common_part4.escn")
]
func update_rects(arrays: Array, bshapes: Array) -> Dictionary:
var rects = {}
for idx in range(0, arrays[ArrayMesh.ARRAY_INDEX].size(), 3):
for t in range(3):
var index_base = arrays[ArrayMesh.ARRAY_INDEX][idx + t]
var vertex_base = arrays[ArrayMesh.ARRAY_VERTEX][index_base]
var normal_base = arrays[ArrayMesh.ARRAY_NORMAL][index_base]
var uv_base = arrays[ArrayMesh.ARRAY_TEX_UV][index_base]
for bsc in range(bshapes.size()):
if !rects.has(bsc):
rects[bsc] = Rect2(uv_base, Vector2())
var vertex_mod = bshapes[bsc][ArrayMesh.ARRAY_VERTEX][index_base] - vertex_base
if vertex_mod.length() > 0.0001:
rects[bsc] = rects[bsc].expand(uv_base)
return rects
func update_triangles(arrays: Array, bshapes: Array) -> Array:
var triangles: Array = []
var ntriangles : = 0
var skipped : = 0
for idx in range(0, arrays[ArrayMesh.ARRAY_INDEX].size(), 3):
var verts = []
for t in range(3):
var index_base = arrays[ArrayMesh.ARRAY_INDEX][idx + t]
var vertex_base = arrays[ArrayMesh.ARRAY_VERTEX][index_base]
var normal_base = arrays[ArrayMesh.ARRAY_NORMAL][index_base]
var uv_base = arrays[ArrayMesh.ARRAY_TEX_UV][index_base]
var index_shape = []
var vertex_shape = []
var normal_shape = []
var uv_shape = []
for bsc in range(bshapes.size()):
index_shape.push_back(bshapes[bsc][ArrayMesh.ARRAY_INDEX][idx + t])
var index = index_shape[index_shape.size() - 1]
if index != index_base:
print("index mismatch", bsc, " ", index_base, " ", index)
var vertex_mod = bshapes[bsc][ArrayMesh.ARRAY_VERTEX][index] - vertex_base
var normal_mod = bshapes[bsc][ArrayMesh.ARRAY_NORMAL][index] - normal_base
vertex_shape.push_back(vertex_mod)
normal_shape.push_back(normal_mod)
uv_shape.push_back(bshapes[bsc][ArrayMesh.ARRAY_TEX_UV][index])
if (uv_shape[uv_shape.size() - 1] - uv_base).length() != 0:
print("uv mismatch", bsc, " ", idx)
var vdata = {}
vdata.shape = vertex_shape
vdata.normal = normal_shape
vdata.uv = uv_base
verts.push_back(vdata)
if check_triangle(verts):
triangles.push_back(verts)
ntriangles += 1
else:
skipped += 1
if skipped > 0:
print("ntriangles: ", ntriangles, " skipped: ", skipped)
return triangles
func get_shape_names(mesh: ArrayMesh) -> PoolStringArray:
var shape_names: Array = []
for r in range(mesh.get_blend_shape_count()):
shape_names.push_back(mesh.get_blend_shape_name(r))
return PoolStringArray(shape_names)
func process_morph_meshes(mesh: ArrayMesh, morphs: Dictionary, rects: Dictionary, mesh_data: Dictionary, nshapes: Dictionary):
for sc in range(mesh.get_surface_count()):
if !morphs.has(sc):
morphs[sc] = []
mesh_data[sc] = PoolStringArray()
rects[sc] = {}
var bshapes: Array = mesh.surface_get_blend_shape_arrays(sc)
var arrays: Array = mesh.surface_get_arrays(sc)
print("vertices: ", arrays[ArrayMesh.ARRAY_VERTEX].size())
print("indices: ", arrays[ArrayMesh.ARRAY_INDEX].size())
print("surf: ", sc, " shapes: ", bshapes.size())
var shape_names : = get_shape_names(mesh)
rects[sc] = update_rects(arrays, bshapes)
var triangles : = update_triangles(arrays, bshapes)
morphs[sc] += triangles
mesh_data[sc] += shape_names
nshapes[sc] = bshapes.size()
func extract_offsets(mesh: ArrayMesh, mesh_cloth: ArrayMesh):
pass
func _ready():
var morphs = {}
var mesh_data = {}
var nshapes = {}
var rects = {}
for mesh_no in range(common.size()):
# var skipped : = 0
# var ntriangles : = 0
var ch: Node = common[mesh_no].instance()
# add_child(ch)
var mi: MeshInstance = find_mesh_name(ch, "base")
if !mi:
return
var mesh: ArrayMesh = mi.mesh
if !mesh:
return
find_min_max(mesh)
if !morphs.has(mesh_no):
morphs[mesh_no] = {}
mesh_data[mesh_no] = {}
nshapes[mesh_no] = {}
rects[mesh_no] = {}
process_morph_meshes(mesh, morphs[mesh_no], rects[mesh_no], mesh_data[mesh_no], nshapes[mesh_no])
for mesh_no in range(common.size()):
var ch: Node = common[mesh_no].instance()
var mi: MeshInstance = find_mesh_name(ch, "base")
var mi_skirt: MeshInstance = find_mesh_name(ch, "skirt_helper")
assert mi != null && mi_skirt != null
var mesh: ArrayMesh = mi.mesh
var mesh_skirt: ArrayMesh = mi_skirt.mesh
extract_offsets(mesh, mesh_skirt)
pad_morphs(morphs, nshapes)
fill_draw_data(morphs, draw_data, mesh_data, nshapes, rects)
print("data count: ", draw_data.keys(), " ", draw_data[0].keys())
$gen/drawable.triangles = draw_data[0][0].triangles
$gen/drawable.min_point = min_point
$gen/drawable.max_point = max_point
$gen/drawable.normals = false
# print("done ", mesh.get_surface_count(), " ", mesh.get_blend_shape_count(), " ", min_point, " ", max_point, " added: ", ntriangles, " skipped: ", skipped)
var surface : = 0
var shape : = 0
var exit_delay : = 3.0
var draw_delay : = 2.0
func save_viewport(shape_name: String, rect: Rect2):
var viewport: Viewport = $gen
var vtex : = viewport.get_texture()
var tex_img : = vtex.get_data()
var fn = ""
if !maps.has(shape_name):
maps[shape_name] = {}
maps[shape_name].width = tex_img.get_width()
maps[shape_name].height = tex_img.get_height()
maps[shape_name].format = tex_img.get_format()
if $gen/drawable.normals:
maps[shape_name].image_normal_data = tex_img.duplicate(true).get_data()
else:
maps[shape_name].image_data = tex_img.duplicate(true).get_data()
maps[shape_name].rect = rect.grow(0.003)
func _process(delta):
if surface == draw_data.size():
if exit_delay > 0:
exit_delay -= delta
print(exit_delay)
else:
print("generating same vert indices...")
find_same_verts()
var fd = File.new()
fd.open("res://config.bin", File.WRITE)
fd.store_var(min_point)
fd.store_var(max_point)
fd.store_var(min_normal)
fd.store_var(max_normal)
fd.store_var(maps)
fd.store_var(vert_indices)
fd.close()
get_tree().change_scene("res://map_test.tscn")
elif shape == draw_data[surface].size():
shape = 0
surface += 1
draw_delay = 1.0
$gen_maps/ProgressBar.value = 100.0
else:
$gen_maps/ProgressBar.value = 100.0 * shape / draw_data[surface].size()
print("value ", $gen_maps/ProgressBar.value)
if draw_delay > 0:
draw_delay -= delta
else:
save_viewport(draw_data[surface][shape].name, draw_data[surface][shape].rect)
if $gen/drawable.normals:
shape += 1
draw_delay = 1.0
print("shape ", shape)
if shape < draw_data[surface].size():
$gen/drawable.normals = !$gen/drawable.normals
if $gen/drawable.normals:
$gen/drawable.min_point = min_normal
$gen/drawable.max_point = max_normal
else:
$gen/drawable.min_point = min_point
$gen/drawable.max_point = max_point
$gen/drawable.triangles = draw_data[surface][shape].triangles
$gen/drawable.update()