-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathconvert_scene.gd
439 lines (406 loc) · 20 KB
/
convert_scene.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
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
# This file is part of Unidot Importer. See LICENSE.txt for full MIT license.
# Copyright (c) 2021-present Lyuma <[email protected]> and contributors
# SPDX-License-Identifier: MIT
@tool
extends Resource
const object_adapter_class := preload("./object_adapter.gd")
const scene_node_state_class := preload("./scene_node_state.gd")
func customComparison(a, b):
if typeof(a) != typeof(b):
return typeof(a) < typeof(b)
elif a.transform != null && b.transform != null:
return a.transform.rootOrder < b.transform.rootOrder
else:
return b.fileID < a.fileID
func smallestTransform(a, b):
if typeof(a) != typeof(b):
return typeof(a) < typeof(b)
elif a.transform != null && b.transform != null:
return a.transform.fileID < b.transform.fileID
else:
return b.fileID < a.fileID
func get_global_transform(node: Node):
if not (node is Node3D):
return Transform3D.IDENTITY
var n3d = node as Node3D
if node.get_parent() == null or n3d.top_level:
return n3d.transform
return get_global_transform(node.get_parent()) * n3d.transform
func recursive_print(pkgasset, node: Node, indent: String = ""):
var fnstr = "" if str(node.scene_file_path) == "" else (" (" + str(node.scene_file_path) + ")")
pkgasset.log_debug(indent + str(node.name) + ": owner=" + str(node.owner.name if node.owner != null else "") + fnstr)
#pkgasset.log_debug(indent + str(node.name) + str(node) + ": owner=" + str(node.owner.name if node.owner != null else "") + str(node.owner) + fnstr)
var new_indent: String = indent + " "
for c in node.get_children():
recursive_print(pkgasset, c, new_indent)
func pack_scene(pkgasset, is_prefab) -> PackedScene:
#for asset in pkgasset.parsed_asset.assets.values():
# if asset.type == "GameObject" and asset.toplevel:
# pass
var arr: Array = [].duplicate()
for asset in pkgasset.parsed_asset.assets.values():
if (asset.type == "GameObject" or asset.type == "PrefabInstance") and asset.toplevel:
if asset.is_non_stripped_prefab_reference:
continue # We don't want these.
if asset.is_stripped:
asset.log_fail("Stripped object " + asset.type + " would be added to arr " + str(asset.meta.guid) + "/" + str(asset.fileID))
if is_prefab:
if asset.type == "PrefabInstance":
var target_prefab_meta = asset.meta.lookup_meta(asset.source_prefab)
asset.meta.prefab_main_gameobject_id = pkgasset.parsed_meta.xor_or_stripped(target_prefab_meta.prefab_main_gameobject_id, asset.fileID)
asset.meta.prefab_main_transform_id = pkgasset.parsed_meta.xor_or_stripped(target_prefab_meta.prefab_main_transform_id, asset.fileID)
else:
asset.meta.prefab_main_gameobject_id = asset.fileID
asset.meta.prefab_main_transform_id = asset.transform.fileID
arr.push_back(asset)
if arr.is_empty():
pkgasset.log_fail("Scene " + pkgasset.pathname + " has no nodes.")
return
var env: Environment = null
var bakedlm: LightmapGI = null
var navregion: NavigationRegion3D = null
var occlusion: OccluderInstance3D = null
var dirlight: DirectionalLight3D = null
var scene_contents: Node3D = null
var node_map: Dictionary = {}
if is_prefab:
if len(arr) > 1:
pkgasset.log_warn("Prefab " + pkgasset.pathname + " has multiple roots. picking lowest.")
arr.sort_custom(smallestTransform)
arr = [arr[0]]
else:
scene_contents = Node3D.new()
scene_contents.name = "RootNode3D"
var world_env = WorldEnvironment.new()
world_env.name = "WorldEnvironment"
env = Environment.new()
world_env.environment = env
# TODO: We need to convert all PostProcessingProfile assets into godot Environment objects
# Then, store an array of list of node path, weight, asset and layer_id.
# Then, in prefab logic, concat the paths into the outer scene.
# finally, the actual scene will loop through all environments and add them by wieght
# then, we can assign the final world environment from all this.
scene_contents.add_child(world_env, true)
world_env.owner = scene_contents
bakedlm = LightmapGI.new()
bakedlm.name = "LightmapGI"
scene_contents.add_child(bakedlm, true)
bakedlm.owner = scene_contents
navregion = NavigationRegion3D.new()
navregion.name = "NavigationRegion3D"
scene_contents.add_child(navregion, true)
navregion.owner = scene_contents
occlusion = OccluderInstance3D.new()
occlusion.name = "OccluderInstance3D"
scene_contents.add_child(occlusion, true)
occlusion.owner = scene_contents
dirlight = DirectionalLight3D.new()
dirlight.name = "DirectionalLight3D"
scene_contents.add_child(dirlight, true)
dirlight.owner = scene_contents
dirlight.visible = false
pkgasset.parsed_meta.calculate_prefab_nodepaths_recursive()
var node_state: Object = scene_node_state_class.new(pkgasset.parsed_meta.get_database(), pkgasset.parsed_meta, scene_contents)
var ps: RefCounted = node_state.prefab_state
for asset in pkgasset.parsed_asset.assets.values():
var parent: RefCounted = null # UnidotTransform
if asset.is_stripped:
pass # Ignore stripped components.
elif asset.is_non_stripped_prefab_reference:
var prefab_instance_id: int = asset.prefab_instance[1]
var prefab_source_object: int = asset.prefab_source_object[1]
if not ps.non_stripped_prefab_references.has(prefab_instance_id):
ps.non_stripped_prefab_references[prefab_instance_id] = [].duplicate()
ps.non_stripped_prefab_references[prefab_instance_id].push_back(asset)
elif asset.type == "Transform" or asset.type == "PrefabInstance":
parent = asset.parent
if parent != null and parent.is_prefab_reference and (asset.type == "PrefabInstance" or not asset.is_prefab_reference):
var prefab_instance_id: int = parent.prefab_instance[1]
var prefab_source_object: int = parent.prefab_source_object[1]
if not ps.transforms_by_parented_prefab.has(prefab_instance_id):
ps.transforms_by_parented_prefab[prefab_instance_id] = {}.duplicate()
if not ps.child_transforms_by_stripped_id.has(parent.fileID):
ps.child_transforms_by_stripped_id[parent.fileID] = [].duplicate()
ps.transforms_by_parented_prefab.get(prefab_instance_id)[parent.fileID] = parent
#ps.transforms_by_parented_prefab_source_obj[str(prefab_instance_id) + "/" + str(prefab_source_object)] = parent
ps.child_transforms_by_stripped_id[parent.fileID].push_back(asset)
#elif parent != null and asset.type == "PrefabInstance":
# var uk: String = asset.parent.fileID
# if not ps.prefab_parents.has(uk):
# ps.prefab_parents[uk] = []
# ps.prefab_parents[uk].append(asset)
elif asset.type == "RenderSettings":
env.fog_enabled = (asset.keys.get("m_Fog", 0) == 1)
var c: Color = asset.keys.get("m_FogColor", Color.WHITE)
var max_c: float = max(c.r, max(c.g, c.b))
if max_c > 1.0:
c /= max_c
else:
max_c = 1.0
env.fog_light_color = c
env.fog_light_energy = max_c
if asset.keys.get("m_FogMode", 3) == 1: # Linear
const TARGET_FOG_DENSITY = 0.05
# It's not possible to emulate linear fog exactly with the level of control we currently have.
# Multiply by 0.1 because the fog seemed too aggressive.
env.fog_density = 0.1 * -log(TARGET_FOG_DENSITY) / asset.keys.get("m_LinearFogEnd", 0.0)
else:
env.fog_density = asset.keys.get("m_FogDensity", 0.0)
var sun: Array = asset.keys.get("m_Sun", [null, 0, null, null])
if sun[1] != 0:
scene_contents.remove_child(dirlight)
dirlight = null
var sky_material: Variant = pkgasset.parsed_meta.get_godot_resource(asset.keys.get("m_SkyboxMaterial"))
if sky_material == null and asset.keys.get("m_SkyboxMaterial")[1] != 0:
# Just use a default skybox for now...
sky_material = ProceduralSkyMaterial.new()
sky_material.sky_top_color = Color(0.454902, 0.678431, 0.87451, 1)
sky_material.sky_horizon_color = Color(0.894118, 0.952941, 1, 1)
sky_material.sky_curve = 0.0731028
sky_material.ground_bottom_color = Color(0.454902, 0.470588, 0.490196, 1)
sky_material.ground_horizon_color = Color(1, 1, 1, 1)
var ambient_mode: int = asset.keys.get("m_AmbientMode", 0)
if sky_material != null:
env.background_mode = Environment.BG_SKY
env.sky = Sky.new()
env.sky.sky_material = sky_material
if ambient_mode == 0 and sky_material == null:
env.background_mode = Environment.BG_COLOR
var ccol: Color = asset.keys.get("m_AmbientSkyColor", Color.BLACK)
var eng: float = max(ccol.r, max(ccol.g, ccol.b))
if eng > 1:
ccol /= eng
else:
eng = 1
env.background_color = ccol
env.background_energy_multiplier = eng
env.ambient_light_color = ccol
env.ambient_light_energy = eng
env.ambient_light_sky_contribution = 0
elif ambient_mode == 1 or ambient_mode == 2 or ambient_mode == 3:
# modes 1 or 2 are technically a gradient (2 is blank dropdown)
# mode 3 is solid color (same as 0 + null skybox material)
var ccol: Color = asset.keys.get("m_AmbientSkyColor", Color.BLACK)
var eng: float = max(ccol.r, max(ccol.g, ccol.b))
if eng > 1:
ccol /= eng
else:
eng = 1
env.ambient_light_color = ccol
env.ambient_light_energy = eng
env.ambient_light_sky_contribution = 0
elif asset.type == "LightmapSettings":
var lda: Array = asset.keys.get("m_LightingDataAsset", [null, 0, null, null])
if lda[1] == 0:
scene_contents.remove_child(bakedlm)
bakedlm = null
elif asset.type == "NavMeshSettings":
var nmd: Array = asset.keys.get("m_NavMeshData", [null, 0, null, null])
if nmd[1] == 0:
scene_contents.remove_child(navregion)
navregion = null
else:
navregion.navmesh = NavigationMesh.new()
elif asset.type == "OcclusionCullingSettings":
var ocd: Array = asset.keys.get("m_OcclusionCullingData", [null, 0, null, null])
if ocd[1] == 0:
scene_contents.remove_child(occlusion)
occlusion = null
else:
occlusion.occluder = SphereOccluder3D.new() # wrong type
elif asset.type != "GameObject":
# alternatively, is it a subclass of UnidotComponent?
parent = asset.gameObject
if parent != null and parent.is_prefab_reference:
var prefab_instance_id: int = parent.prefab_instance[1]
var prefab_source_object: int = parent.prefab_source_object[1]
if not ps.gameobjects_by_parented_prefab.has(prefab_instance_id):
ps.gameobjects_by_parented_prefab[prefab_instance_id] = {}.duplicate()
if not ps.components_by_stripped_id.has(parent.fileID):
ps.components_by_stripped_id[parent.fileID] = [].duplicate()
ps.gameobjects_by_parented_prefab.get(prefab_instance_id)[parent.fileID] = parent
#ps.gameobjects_by_parented_prefab_source_obj[str(prefab_instance_id) + "/" + str(prefab_source_object)] = parent
ps.components_by_stripped_id[parent.fileID].push_back(asset)
var skelleys_with_no_parent: Array = node_state.initialize_skelleys(pkgasset.parsed_asset.assets.values(), is_prefab)
if len(skelleys_with_no_parent) >= 1:
if scene_contents == null:
scene_contents = Node3D.new()
scene_contents.name = "RootNode3D"
node_state = node_state.state_with_owner(scene_contents)
for noparskel in skelleys_with_no_parent:
if noparskel.humanoid_avatar_meta != null:
node_state = node_state.state_with_avatar_meta(noparskel.humanoid_avatar_meta)
scene_contents.add_child(noparskel.godot_skeleton, true)
#var fileid_to_prefab_nodepath = {}.duplicate()
#var fileid_to_prefab_ref = {}.duplicate()
#pkgasset.parsed_meta.fileid_to_prefab_nodepath = {}
#pkgasset.parsed_meta.fileid_to_prefab_ref = {}
for asset in pkgasset.parsed_asset.assets.values():
if str(asset.type) == "SkinnedMeshRenderer":
var skelley: RefCounted = asset.get_skelley(node_state)
if skelley != null:
skelley.skinned_mesh_renderers.append(asset)
node_state.env = env
node_state.set_main_name_map(node_state.prefab_state.gameobject_name_map, node_state.prefab_state.prefab_gameobject_name_map)
arr.sort_custom(customComparison)
for asset in arr:
if asset.is_stripped:
asset.log_fail("Stripped object " + asset.type + " added to arr " + str(asset.meta.guid) + "/" + str(asset.fileID))
var skel: RefCounted = null
# FIXME: PrefabInstances pointing to a scene whose root node is a Skeleton may not work.
if asset.transform != null:
skel = node_state.fileID_to_skelley.get(asset.transform.fileID, null)
if skel != null:
if len(skelleys_with_no_parent) >= 1:
# If a toplevel node is part of a skeleton, insert the skeleton between the actual root and the toplevel node.
scene_contents.add_child(skel.godot_skeleton, true)
skel.owner = scene_contents
asset.create_skeleton_bone(node_state, skel)
else:
# asset.log_debug(str(asset) + " position " + str(asset.transform.godot_transform))
var new_root: Node3D = asset.create_godot_node(node_state, scene_contents)
if scene_contents == null:
assert(is_prefab)
scene_contents = new_root
node_state = node_state.state_with_owner(scene_contents)
else:
if is_prefab:
asset.log_fail("May be a prefab with multiple roots, or hit unusual case.")
pass
# assert(not is_prefab)
if asset.type == "PrefabInstance":
node_state.add_prefab_to_parent_transform(0, asset.fileID)
for skelley in skelleys_with_no_parent:
for smr in skelley.skinned_mesh_renderers:
var ret: Node = smr.create_skinned_mesh(node_state)
if ret != null:
smr.log_debug("Finally added SkinnedMeshRenderer " + str(smr) + " into top-level Skeleton " + str(scene_contents.get_path_to(ret)))
# scene_contents = node_state.owner
if scene_contents == null:
pkgasset.log_fail("Failed to parse scene " + pkgasset.pathname)
return null
process_lod_groups(scene_contents, ps)
if not is_prefab:
# Remove redundant directional light.
if dirlight != null:
var x: LightmapGI = null
var fileids: Array = [].duplicate()
for light in node_state.find_objects_of_type("Light"):
if light.lightType == 1: # Directional
scene_contents.remove_child(dirlight)
dirlight = null
var main_camera: Camera3D = null
var pp_layer_bits: int = 0
for camera_obj in node_state.find_objects_of_type("Camera"):
#if not (camera.get_parent() is Viewport) and camera.visible:
var go: RefCounted = node_state.get_gameobject(camera_obj)
var camera: Camera3D = null
if camera_obj.enabled and go.enabled:
# This is a main camera
camera = node_state.get_godot_node(camera_obj)
if camera != null:
main_camera = camera
if camera.environment != null:
env.background_mode = camera.environment.background_mode
env.background_color = camera.environment.background_color
env.background_energy_multiplier = camera.environment.background_energy_multiplier
for mono in node_state.get_components(camera_obj, "MonoBehaviour"):
if str(mono.monoscript[2]) == "948f4100a11a5c24981795d21301da5c": # PostProcessingLayer
pp_layer_bits = mono.keys.get("volumeLayer.m_Bits", mono.keys.get("volumeLayer", {}).get("m_Bits", 0))
break
break
for mono in node_state.find_objects_of_type("MonoBehaviour"):
if str(mono.monoscript[2]) == "8b9a305e18de0c04dbd257a21cd47087": # PostProcessingVolume
var go: RefCounted = node_state.get_gameobject(mono)
if not mono.enabled or not go.enabled:
continue
if ((1 << go.keys.get("m_Layer")) & pp_layer_bits) != 0:
# Enabled PostProcessingVolume with matching layer.
if mono.keys.get("isGlobal", 0) == 1 and mono.keys.get("weight", 0) > 0.0:
mono.log_debug("Would merge PostProcessingVolume profile " + str(mono.keys.get("sharedProfile")))
for plugin in pkgasset.parsed_meta.get_enabled_plugins():
plugin.setup_post_scene(pkgasset, arr, skelleys_with_no_parent, node_state, scene_contents)
var light_probes: Array[Node] = scene_contents.find_children("*", "LightmapProbe")
var heights = []
for probe in light_probes:
heights.append(get_global_transform(probe).origin.y)
# LightmapProbe operations are non-linear.
# Godot becomes very slow if you have 2600 probes, and it crashes with "Out of memory" if you have more than 2700
# (Out of memory due to resizing an array over 1 billion elements, not due to actual memory consumption)
# So we will limit probes scene-wide to 2500.
var did_warn_too_many: bool = false
const max_probe_before_sampling: int = 1500
const max_probe_after_sampling: int = 300
const probe_sample_denom: int = 13
var max_height: float = 1000.0
if len(heights) > max_probe_before_sampling:
heights.sort()
max_height = heights[max_probe_before_sampling]
if len(heights) > max_probe_after_sampling:
var min_x: int = min(len(heights), max_probe_before_sampling)
var probe_sample_num: int = max_probe_after_sampling * probe_sample_denom / min_x
pkgasset.log_warn("Deleting " + str(len(heights) - max_probe_before_sampling) + " light probes above y=" + str(max_height) + " to prevent engine bug.")
pkgasset.log_warn("Sampling " + str(min_x * probe_sample_num / probe_sample_denom) + " remaining light probes to prevent engine bug.")
var i: int = 0
for probe in light_probes:
var delete_probe: bool = get_global_transform(probe).origin.y >= max_height
if delete_probe:
pkgasset.log_debug("Deleting probe " + str(probe.get_parent().name + "/" + str(probe.name)) + " at " + str(probe.transform.origin))
else:
i += 1
if (i % probe_sample_denom) >= probe_sample_num:
delete_probe = true
pkgasset.log_debug("Deleting/sampling probe " + str(probe.get_parent().name + "/" + str(probe.name)) + " at " + str(probe.transform.origin))
if delete_probe:
if probe.owner != scene_contents:
pkgasset.log_fail("Unable to delete light probe at " + str(probe.transform.origin) + " because of scene instance. Please modify " + str(probe.owner.scene_file_path))
probe.owner = null
probe.get_parent().remove_child(probe)
probe.queue_free()
var packed_scene: PackedScene = PackedScene.new()
packed_scene.pack(scene_contents)
pkgasset.log_debug("Finished packing " + pkgasset.pathname + " with " + str(scene_contents.get_child_count()) + " nodes.")
recursive_print(pkgasset, scene_contents)
return packed_scene
func process_lod_groups(scene_contents: Node, ps: RefCounted):
for lod_group in ps.lod_groups:
var prev_distance_m: float = 0.0
var prev_fade_m: float = 0.0
# Formula: 0.65 / screenRelativeHeight * m_Size = distance_in_meters
# For example, 0.65 / 0.8125 * 5 = 4.0
# or 0.65 / 0.216 * 1 = 3.0
# LODGroup scale factor is confusingly aspect ratio dependent.
# We scale by another factor or 2 to account for 16:9 aspect ratio.
# It seems to make LOD switches less noticeable.
var size_m = lod_group.keys.get("m_Size", 1.0)
var animate_crossfade: bool = lod_group.keys.get("m_AnimateCrossFading", 0) == 1
for lod in lod_group.keys.get("m_LODs", []):
var screen_relative_height: float = lod.get("screenRelativeHeight", 1.0)
var distance_m = 2.0 * 0.65 / screen_relative_height * size_m
var fade_width: float = lod.get("fadeTransitionWidth", 0.0)
if animate_crossfade:
fade_width = 0.5 # Hardcode half a meter of fade overlap. No idea...
else:
fade_width = fade_width * (distance_m - prev_distance_m) # fraction of the length of this LOD.
for renderer_ref_dict in lod.get("renderers", []):
var renderer_ref: Array
if typeof(renderer_ref_dict) == TYPE_DICTIONARY:
renderer_ref = renderer_ref_dict["renderer"]
var np: NodePath = lod_group.meta.fileid_to_nodepath.get(renderer_ref[1], lod_group.meta.prefab_fileid_to_nodepath.get(renderer_ref[1], NodePath()))
if not np.is_empty():
var node: Node = scene_contents.get_node(np)
var visual_inst: VisualInstance3D = node as VisualInstance3D
if node != null and visual_inst == null:
visual_inst = node.get_child(0) as VisualInstance3D
if visual_inst != null:
visual_inst.visibility_range_begin = prev_distance_m
visual_inst.visibility_range_begin_margin = prev_fade_m
visual_inst.visibility_range_end = distance_m
visual_inst.visibility_range_end_margin = fade_width
# Dependencies seems buggy, so we use Self and don't set the visibility parent.
#visual_inst.visibility_range_fade_mode = GeometryInstance3D.VISIBILITY_RANGE_FADE_SELF
# Self is really hard to work with as it has overlap...
# So we'll use DISABLED for now... which should act like low-water mark / high-water mark.
visual_inst.visibility_range_fade_mode = GeometryInstance3D.VISIBILITY_RANGE_FADE_DISABLED
prev_distance_m = distance_m
prev_fade_m = fade_width