-
Notifications
You must be signed in to change notification settings - Fork 2
/
bake_lighting.gd
388 lines (317 loc) · 10.8 KB
/
bake_lighting.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
tool
extends SceneTree
var bakeEnvironment = preload("res://BakeEnv.tscn")
var qodotSpatial
var qodotMap
var lightmap
var mapFilePath
var lightFilePath
var texturePath
func _usage():
print("Usage: godot -s bake_lighting.gd <map file> <target .lmbakex file> <game texture folder>")
func _clean_exit(exitCode):
call_deferred("_deferred_cleanup", exitCode)
func _cleanup_tmp():
var tmpDir = Directory.new()
var err = tmpDir.open("tmp")
if err == OK:
tmpDir.list_dir_begin(true)
var file_name = tmpDir.get_next()
while file_name != "":
err = tmpDir.remove(file_name)
if err != OK:
print("Could not remove file '%s': %d" % [file_name, err])
file_name = tmpDir.get_next()
else:
print("Could not open temp directory for removal: %d" % err)
func _deferred_cleanup(exitCode):
print("Cleaning up..")
# Remove all files from /tmp
# tmp is part of the project structure so we can't easily remove the folder from here
# and anyway it doesn't hurt anything by sticking around
_cleanup_tmp()
# Wipe out bake environment scene & quit
qodotSpatial.free()
quit(exitCode)
func _build_failed():
_clean_exit(1)
func _is_image_ext(extension: String) -> bool:
return extension == "bmp" || extension == "dds" || extension == "exr" || \
extension == "hdr" || extension == "jpg" || extension == "jpeg" || \
extension == "png" || extension == "tga" || extension == "svg" || \
extension == "svgz" || extension == "webp" || extension == "tres" || \
extension == "res"
func _build_game_texture_dir(textureAccum: Dictionary, relativePath: String) -> int:
var dir := Directory.new()
var file := File.new()
var dirPath := "%s/%s" % [texturePath,relativePath]
var err := dir.open(dirPath)
if err != OK:
return err
err = dir.list_dir_begin(true)
if err != OK:
return err
var file_name := dir.get_next()
while file_name != "":
if dir.current_is_dir():
err = _build_game_texture_dir(textureAccum, "%s/%s" % [relativePath, file_name])
if err != OK:
dir.list_dir_end()
return err
elif _is_image_ext(file_name.get_extension()):
var fileRelativePath := "%s/%s" % [relativePath, file_name]
var fileAbsolutePath := "%s/%s" % [dirPath, file_name]
textureAccum[fileRelativePath] = file.get_modified_time(fileAbsolutePath)
file_name = dir.get_next()
return OK
func _apply_game_texture_dir(textureAccum: Dictionary, waitAccum: Array, relativePath: String) -> int:
var dir := Directory.new()
var file := File.new()
var dirPath := "res://textures/%s" % relativePath
var err := dir.open(dirPath)
if err != OK:
return err
err = dir.list_dir_begin(true)
if err != OK:
return err
var file_name := dir.get_next()
while file_name != "":
if dir.current_is_dir():
err = _apply_game_texture_dir(textureAccum, waitAccum, "%s/%s" % [relativePath, file_name])
if err != OK:
dir.list_dir_end()
return err
elif _is_image_ext(file_name.get_extension()):
var fileRelativePath := "%s/%s" % [relativePath, file_name]
var fileAbsolutePath := "%s/%s" % [dirPath, file_name]
if !textureAccum.has(fileRelativePath):
print("Removing texture '%s'" % fileRelativePath)
err = dir.remove(fileAbsolutePath)
if err != OK:
dir.list_dir_end()
return err
else:
var gameModifiedTime = textureAccum[fileRelativePath]
textureAccum.erase(fileRelativePath)
var localModifiedTime = file.get_modified_time(fileAbsolutePath)
if localModifiedTime < gameModifiedTime:
print("Updating texture '%s'" % fileRelativePath)
err = dir.copy("%s/%s" % [texturePath, fileRelativePath], fileAbsolutePath)
if err != OK:
dir.list_dir_end()
return err
waitAccum.append(fileAbsolutePath)
file_name = dir.get_next()
return OK
func _cleanup_tex_folders(relativePath: String) -> Array:
var dir := Directory.new()
var dirPath := "res://textures/%s" % relativePath
var err := dir.open(dirPath)
if err != OK:
return [false, err]
err = dir.list_dir_begin(true)
if err != OK:
return [false, err]
var file_path = dir.get_next()
var delete_this_folder = true
while file_path != "":
if dir.current_is_dir():
var cleanup_result = _cleanup_tex_folders("%s/%s" % [relativePath, file_path])
if cleanup_result[1] != OK:
return [false, cleanup_result[1]]
if !cleanup_result[0]:
delete_this_folder = false
else:
delete_this_folder = false
file_path = dir.get_next()
if delete_this_folder:
err = dir.remove(dirPath)
if err != OK:
return [false, err]
return [true, OK]
func _update_textures(waitAccum: Array) -> int:
var textureAccum := {}
# Get paths & modified times for all textures in the game directory
var err = _build_game_texture_dir(textureAccum, "")
if err != OK:
return err
# Enum files in the local dir- delete any that aren't in the game directory
# and update any out of date
err = _apply_game_texture_dir(textureAccum, waitAccum, "")
if err != OK:
return err
# Enum files remaining in the game texture dictionary & move them over
# to local dir
var dir := Directory.new()
for path in textureAccum.keys():
var fromPath = "%s/%s" % [texturePath, path]
var toPath = "res://textures/%s" % path
var toDir = toPath.get_base_dir()
if !dir.dir_exists(toDir):
err = dir.make_dir_recursive(toDir)
if err != OK:
return err
print("Creating texture '%s'" % path)
err = dir.copy(fromPath, toPath)
if err != OK:
return err
waitAccum.append(toPath)
#Cleanup folders that have been left empty by texture removals
var cleanup_result = _cleanup_tex_folders("")
return cleanup_result[1]
func _write_mesh_to_dictionary(node, meshDict):
if node.is_class("MeshInstance"):
var mesh = node.get_mesh()
var name = mesh.resource_path
var surfaces = Array()
if !meshDict.has(name):
for i in mesh.get_surface_count():
var surfaceArrays = mesh.surface_get_arrays(i)
surfaces.append(surfaceArrays)
meshDict[name] = surfaces
for child in node.get_children():
_write_mesh_to_dictionary(child, meshDict)
func _fix_map_owners(node):
for child in node.get_children():
child.set_owner(qodotSpatial)
_fix_map_owners(child)
func _build_and_bake():
print("Loading Bake Environment...")
qodotSpatial = bakeEnvironment.instance()
get_root().add_child(qodotSpatial)
print("Loaded. Preparing scene.")
qodotMap = qodotSpatial.find_node("QodotMap")
lightmap = qodotSpatial.find_node("BakedLightmap")
qodotMap.map_file = mapFilePath
print(mapFilePath)
qodotMap.connect("build_failed", self, "_build_failed")
print("Scene ready...")
qodotMap.verify_and_build()
yield(qodotMap, "build_complete")
qodotMap.call_deferred("unwrap_uv2")
yield(qodotMap, "unwrap_uv2_complete")
var meshDict = Dictionary()
_write_mesh_to_dictionary(qodotMap, meshDict)
var meshFilePath = "%s.mesh" % lightFilePath.get_basename()
var lightmapFile = Directory.new()
if meshDict.size() > 0:
var meshFile = File.new()
meshFile.open_compressed("tmp/%s" % meshFilePath.get_file(), File.WRITE, File.COMPRESSION_GZIP)
meshFile.store_var(meshDict)
meshFile.close()
print("MESHES WRITTEN TO FILE")
var err = lightmapFile.rename("tmp/%s" % meshFilePath.get_file(), meshFilePath)
if err == OK:
print("MESHES MOVED TO OUTPUT PATH")
else:
print("Failed to move meshes to output path '%s': %d" % [meshFilePath, err])
else:
print("No meshes found to write to file")
_clean_exit(1)
return
_fix_map_owners(qodotMap)
yield(self, "idle_frame")
var err = lightmap.bake(null, "tmp/%s.lmbake" % mapFilePath.get_file().get_basename())
if err:
print("Lightmap bake failed: %d" % err)
_clean_exit(1)
return
print("LIGHTMAP BAKED SUCCESFULLY")
var tmpDir = Directory.new()
err = tmpDir.open("tmp")
if err == OK:
var targetDir = lightFilePath.get_base_dir()
var mapName = lightFilePath.get_file().get_basename()
tmpDir.list_dir_begin(true)
var file_name = tmpDir.get_next()
while file_name != "":
if file_name.get_extension() == "png" || file_name.get_extension() == "exr":
var targetFileName = file_name.get_file()
if file_name.get_extension() == "exr":
targetFileName = "%s.hdr" % file_name.get_file().get_basename()
if targetFileName.substr(0, len(mapName)) != mapName:
targetFileName = "%s-%s" % [mapName, targetFileName]
var targetPath = "%s/%s" % [targetDir, targetFileName]
if file_name.get_extension() == "png":
# PNGs can be loaded at runtime, so just copy it
err = tmpDir.copy("res://tmp/%s" % file_name, targetPath)
if err != OK:
print("Could not copy light texture '%s' to '%s': %d" % [file_name, targetPath, err])
_clean_exit(1)
return
else:
#EXRs cannot be loaded at runtime, so convert to HDR
var exrImg = Image.new()
err = exrImg.load("res://tmp/%s" % file_name)
if err != OK:
print("Could not load EXR texture '%s': %d" % [file_name, err])
_clean_exit(1)
return
err = HDRConverter.image_to_hdr(exrImg, targetPath)
if err != OK:
print("Could not convert EXR to HDR for file '%s': %d" % [targetPath, err])
_clean_exit(1)
return
print("COPIED LIGHTMAP '%s'" % targetPath)
file_name = tmpDir.get_next()
else:
print("Could not open temp directory to copy light textures: %d" % err)
_clean_exit(1)
return
var lmbakex = File.new()
err = lmbakex.open("%s.lmbakex" % lightFilePath.get_basename(), File.WRITE)
if err != OK:
print("Failed to open lmbakex file for writing: %d" % err)
_clean_exit(1)
return
var outputBake = BakedData.new()
outputBake.populate_bake_lightmap_data(lightFilePath, lightmap.light_data)
outputBake.save(lmbakex)
lmbakex.flush()
lmbakex.close()
print("LMBAKEX WRITTEN")
_clean_exit(0)
func _init():
var args = OS.get_cmdline_args()
if len(args) < 5:
_usage()
quit(1)
return
mapFilePath = args[2]
lightFilePath = args[3]
texturePath = args[4]
if mapFilePath.get_extension() != "map":
print("Map file '%s' is not a '.map' file" % mapFilePath)
_usage()
quit(1)
return
if lightFilePath.get_extension() != "lmbakex":
print("Light file '%s' must be a file path ending in '.lmbakex'" % lightFilePath)
_usage()
quit(1)
return
var workingDir = Directory.new()
if !workingDir.dir_exists(texturePath):
print("Game texture directory '%s' does not exist" % texturePath)
quit(1)
return
if !workingDir.dir_exists("res://tmp"):
print("Creating tmp directory for output")
workingDir.make_dir("res://tmp")
_cleanup_tmp()
print("Updating texture imports...")
var filesToWaitOn = []
var err = _update_textures(filesToWaitOn)
if err != OK:
print("Failed to update local textures: %d" % err)
quit(1)
return
var messagedAboutImport = false
for filePath in filesToWaitOn:
while !ResourceLoader.exists(filePath):
if !messagedAboutImport:
messagedAboutImport = true
print("Waiting on texture import...")
yield(self, "idle_frame")
print("TEXTURES UPDATED")
call_deferred("_build_and_bake")