Skip to content

Move glTF scene loading to core to allow for run-time loading #3273

Closed
@fire

Description

Describe the project you are working on

Godot Engine and 3d multiplayer vr game

Describe the problem or limitation you are having in your project

glTF2 in core is needed for Virtual Reality and other uses in runtime asset import / export.

From Reduz

We got quite a lot of demand to make the GLTF importer part of core, given it is needed for VR and other stuff, so I have the feeling we should optimize it for performance (which we should do anyway) and have an option to load actual meshes instead of EditorImporterMesh.. so that means we would need to provide a function to parse the mesh node into a scene node and the editor supplies, otherwise it uses Mesh as a default

Describe the feature / enhancement and how it helps to overcome the problem or limitation

By moving gltf2 to core we can access these functions in game templates and in editor.

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

  • Add Error import_scene_buffer(const PackedByteArray &p_bytes, uint32_t p_flags, int32_t p_bake_fps, Ref<GLTFState> r_state, List<String> *r_missing_deps, Error *r_err);
  • Add a concept of a base path string that when empty fail early returns the code.
  • Change EditorSceneImporterMeshNode3D and EditorSceneImporterMesh to use function pointers that allow switching based on function pointer validity to MeshInstance3D
  • Remove PackedSceneGLTF
  • Add save_buffer()
  • Extensions for the GLTFDocument Add an extension system for the GLTFDocument class #3305

See also:

Future work.

  • Optimize for performance
  • Optimize for performance for export by not duplicating exported textures that match exactly
  • Add explicit security checks and migrate any URI resolving code for textures & buffers & etc. into a common helper which has explicit "" check that returns ERR_FAIL_
  • Blend shape key frames should have processing limits requested in Gltf export blend shape godot#48947

Unify GLTFDocument

	Error load_from_file(String p_path, uint32_t p_flags, int32_t p_bake_fps, Ref<GLTFState> r_state);
	Error load_from_buffer(Array p_bytes, String p_base_path, uint32_t p_flags, int32_t p_bake_fps, Ref<GLTFState> r_state);
	Error load_from_scene(Node *p_node, uint32_t p_flags, int32_t p_bake_fps, Ref<GLTFState> r_state);
	Node *write_to_scene(Ref<GLTFState> state, float p_bake_fps);
	Error write_to_filesystem(Ref<GLTFState> state, const String &p_path);
	Array write_to_buffer(Ref<GLTFState> state);
  • Reading a gltf to a scene
Ref<GLTFDocument> doc;
doc.instantiate();
Ref<GLTFState> state;
state.instantiate();
Error err = doc->load_from_file(p_path, p_flags, p_bake_fps, state);
if (err != OK) {
*r_err = err;
return nullptr;
}
Node *root = doc->write_to_scene(state, p_bake_fps);
  • Writing a gltf to the filesystem
Ref<GLTFDocument> doc;
doc.instantiate();
Ref<GLTFState> state;
state.instantiate();
int32_t flags = 0;
flags |= EditorSceneFormatImporter::IMPORT_USE_NAMED_SKIN_BINDS;
Error err = doc->load_from_scene(root, flags, 30.0f, state);
if (err != OK) {
ERR_PRINT(vformat("glTF2 save scene error %s.", itos(err)));
}
err = doc->write_to_filesystem(state, p_file);
if (err != OK) {
ERR_PRINT(vformat("glTF2 save scene error %s.", itos(err)));
}
  • Reading a gltf buffer to a scene
  • Writing a gltf to a buffer
  • doc.get_state() -> GLTFState
  • doc.set_state(Ref) ->state

If this enhancement will not be used often, can it be worked around with a few lines of script?

Not trivial to work around in a few lines of code. This is core.

Is there a reason why this should be core and not an add-on in the asset library?

Many cases such as VR usage to implement controller model loading require this.

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions