add runtime object loader (gltf only)
This commit is contained in:
70
scripts/utils/runtimeloader.gd
Normal file
70
scripts/utils/runtimeloader.gd
Normal file
@@ -0,0 +1,70 @@
|
||||
extends Node
|
||||
|
||||
func _input(event):
|
||||
if event is InputEventKey and event.is_pressed():
|
||||
if event.keycode == KEY_T:
|
||||
load_gltf("runtime_loader_test.glb")
|
||||
|
||||
func load_gltf(path, hasCollision = false, trimesh = false):
|
||||
var gltf := GLTFDocument.new()
|
||||
var gltf_state := GLTFState.new()
|
||||
|
||||
if(OS.has_feature("editor")):
|
||||
path = "/home/kookroach/Documents/Projects/Scoom_test/"+path
|
||||
else:
|
||||
path = OS.get_executable_path() + "/"+path
|
||||
|
||||
var snd_file = FileAccess.open(path, FileAccess.READ)
|
||||
|
||||
var fileBytes = PackedByteArray()
|
||||
fileBytes = snd_file.get_buffer(snd_file.get_length())
|
||||
|
||||
gltf.append_from_buffer(fileBytes, "", gltf_state)
|
||||
|
||||
print("Loading ", path)
|
||||
var node = gltf.generate_scene(gltf_state)
|
||||
var entity_count = 0;
|
||||
|
||||
for o in node.get_children():
|
||||
if o.name.begins_with("entity_"):
|
||||
print("Loading entity ",o.name)
|
||||
if loadEntity(o):
|
||||
entity_count += 1;
|
||||
else:
|
||||
if(hasCollision):
|
||||
var meshInstance = node.get_child(0) as MeshInstance3D
|
||||
if(trimesh):
|
||||
meshInstance.create_trimesh_collision()
|
||||
else:
|
||||
meshInstance.create_convex_collision()
|
||||
|
||||
if(entity_count > 0):
|
||||
print("Loaded gltf/glb file with ", entity_count, " entities.")
|
||||
add_child(node)
|
||||
return node
|
||||
|
||||
func loadEntity(node):
|
||||
var path = ""
|
||||
if(OS.has_feature("editor")):
|
||||
path = "/home/kookroach/Documents/Projects/Scoom_test/"
|
||||
else:
|
||||
path = OS.get_executable_path() + "/"
|
||||
|
||||
var config = ConfigFile.new()
|
||||
var err = config.load(path+"/entity_test.cfg")
|
||||
|
||||
if err != OK:
|
||||
printerr("COULD NOT LOAD ENTITY : ", node.name)
|
||||
return false
|
||||
|
||||
var x = config.get_section_keys(node.name)
|
||||
if(x.size() < 1):
|
||||
printerr("NO CONFIG FOUND FOR ENTITY : ", node.name)
|
||||
return false
|
||||
|
||||
var entity = load_gltf(config.get_value(node.name, "model")) as Node3D
|
||||
entity.transform.origin = node.transform.origin
|
||||
entity.scale = (Vector3(1.5,1.5,1.5))
|
||||
|
||||
node.queue_free()
|
||||
return true
|
||||
Reference in New Issue
Block a user