Skip to content

Commit

Permalink
Remove packed scene gltf
Browse files Browse the repository at this point in the history
We determined through discussion that composing the packed scene from a node tree was a better design because it removed duplication of code.
  • Loading branch information
fire committed Sep 10, 2021
1 parent 729461b commit 523b2d9
Show file tree
Hide file tree
Showing 13 changed files with 123 additions and 194 deletions.
1 change: 0 additions & 1 deletion modules/gltf/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ def get_doc_classes():
"GLTFSpecGloss",
"GLTFState",
"GLTFTexture",
"PackedSceneGLTF",
]


Expand Down
22 changes: 22 additions & 0 deletions modules/gltf/doc_classes/GLTFDocument.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,28 @@
<tutorials>
</tutorials>
<methods>
<method name="import_scene">
<return type="Node" />
<argument index="0" name="path" type="String" />
<argument index="1" name="flags" type="int" default="0" />
<argument index="2" name="bake_fps" type="int" default="30" />
<argument index="3" name="state" type="GLTFState" default="null" />
<description>
Import a scene from glTF2 ".gltf" or ".glb" file.
</description>
</method>
<method name="save_scene">
<return type="int" enum="Error" />
<argument index="0" name="node" type="Node" />
<argument index="1" name="path" type="String" />
<argument index="2" name="src_path" type="String" />
<argument index="3" name="flags" type="int" default="0" />
<argument index="4" name="bake_fps" type="float" default="30" />
<argument index="5" name="state" type="GLTFState" default="null" />
<description>
Save a scene as a glTF2 ".glb" or ".gltf" file.
</description>
</method>
</methods>
<constants>
</constants>
Expand Down
43 changes: 0 additions & 43 deletions modules/gltf/doc_classes/PackedSceneGLTF.xml

This file was deleted.

11 changes: 8 additions & 3 deletions modules/gltf/editor_scene_exporter_gltf_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@

#include "editor_scene_exporter_gltf_plugin.h"
#include "core/config/project_settings.h"
#include "core/error/error_list.h"
#include "core/object/object.h"
#include "core/templates/vector.h"
#include "editor/editor_file_system.h"
#include "gltf_document.h"
#include "scene/3d/mesh_instance_3d.h"
#include "scene/gui/check_box.h"
#include "scene/main/node.h"
Expand All @@ -49,7 +51,6 @@ bool SceneExporterGLTFPlugin::has_main_screen() const {

SceneExporterGLTFPlugin::SceneExporterGLTFPlugin(EditorNode *p_node) {
editor = p_node;
convert_gltf2.instantiate();
file_export_lib = memnew(EditorFileDialog);
editor->get_gui_base()->add_child(file_export_lib);
file_export_lib->connect("file_selected", callable_mp(this, &SceneExporterGLTFPlugin::_gltf2_dialog_action));
Expand All @@ -71,8 +72,12 @@ void SceneExporterGLTFPlugin::_gltf2_dialog_action(String p_file) {
return;
}
List<String> deps;
convert_gltf2->save_scene(root, p_file, p_file, 0, 1000.0f, &deps);
EditorFileSystem::get_singleton()->scan_changes();
Ref<GLTFDocument> doc;
doc.instantiate();
Error err = doc->save_scene(root, p_file, p_file, 0, 30.0f, Ref<GLTFState>());
if (err != OK) {
ERR_PRINT(vformat("glTF2 save scene error %s.", itos(err)));
}
}

void SceneExporterGLTFPlugin::convert_scene_to_gltf2() {
Expand Down
1 change: 0 additions & 1 deletion modules/gltf/editor_scene_exporter_gltf_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
class SceneExporterGLTFPlugin : public EditorPlugin {
GDCLASS(SceneExporterGLTFPlugin, EditorPlugin);

Ref<PackedSceneGLTF> convert_gltf2;
EditorNode *editor = nullptr;
EditorFileDialog *file_export_lib = nullptr;
void _gltf2_dialog_action(String p_file);
Expand Down
117 changes: 3 additions & 114 deletions modules/gltf/editor_scene_importer_gltf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,124 +50,13 @@ Node *EditorSceneImporterGLTF::import_scene(const String &p_path,
uint32_t p_flags, int p_bake_fps,
List<String> *r_missing_deps,
Error *r_err) {
Ref<PackedSceneGLTF> importer;
importer.instantiate();
return importer->import_scene(p_path, p_flags, p_bake_fps, r_missing_deps, r_err, Ref<GLTFState>());
Ref<GLTFDocument> doc;
doc.instantiate();
return doc->import_scene_gltf(p_path, p_flags, p_bake_fps, Ref<GLTFState>(), r_missing_deps, r_err);
}

Ref<Animation> EditorSceneImporterGLTF::import_animation(const String &p_path,
uint32_t p_flags,
int p_bake_fps) {
return Ref<Animation>();
}

void PackedSceneGLTF::_bind_methods() {
ClassDB::bind_method(
D_METHOD("export_gltf", "node", "path", "flags", "bake_fps"),
&PackedSceneGLTF::export_gltf, DEFVAL(0), DEFVAL(1000.0f));
ClassDB::bind_method(D_METHOD("pack_gltf", "path", "flags", "bake_fps", "state"),
&PackedSceneGLTF::pack_gltf, DEFVAL(0), DEFVAL(1000.0f), DEFVAL(Ref<GLTFState>()));
ClassDB::bind_method(D_METHOD("import_gltf_scene", "path", "flags", "bake_fps", "state"),
&PackedSceneGLTF::import_gltf_scene, DEFVAL(0), DEFVAL(1000.0f), DEFVAL(Ref<GLTFState>()));
}
Node *PackedSceneGLTF::import_gltf_scene(const String &p_path, uint32_t p_flags, float p_bake_fps, Ref<GLTFState> r_state) {
Error err = FAILED;
List<String> deps;
return import_scene(p_path, p_flags, p_bake_fps, &deps, &err, r_state);
}

Node *PackedSceneGLTF::import_scene(const String &p_path, uint32_t p_flags,
int p_bake_fps,
List<String> *r_missing_deps,
Error *r_err,
Ref<GLTFState> r_state) {
if (r_state == Ref<GLTFState>()) {
r_state.instantiate();
}
r_state->use_named_skin_binds =
p_flags & EditorSceneImporter::IMPORT_USE_NAMED_SKIN_BINDS;

Ref<GLTFDocument> gltf_document;
gltf_document.instantiate();
Error err = gltf_document->parse(r_state, p_path);
if (r_err) {
*r_err = err;
}
ERR_FAIL_COND_V(err != Error::OK, nullptr);

Node3D *root = memnew(Node3D);
for (int32_t root_i = 0; root_i < r_state->root_nodes.size(); root_i++) {
gltf_document->_generate_scene_node(r_state, root, root, r_state->root_nodes[root_i]);
}
gltf_document->_process_mesh_instances(r_state, root);
if (r_state->animations.size()) {
AnimationPlayer *ap = memnew(AnimationPlayer);
root->add_child(ap);
ap->set_owner(root);
for (int i = 0; i < r_state->animations.size(); i++) {
gltf_document->_import_animation(r_state, ap, i, p_bake_fps);
}
}

return cast_to<Node3D>(root);
}

void PackedSceneGLTF::pack_gltf(String p_path, int32_t p_flags,
real_t p_bake_fps, Ref<GLTFState> r_state) {
Error err = FAILED;
List<String> deps;
Node *root = import_scene(p_path, p_flags, p_bake_fps, &deps, &err, r_state);
ERR_FAIL_COND(err != OK);
pack(root);
}

void PackedSceneGLTF::save_scene(Node *p_node, const String &p_path,
const String &p_src_path, uint32_t p_flags,
int p_bake_fps, List<String> *r_missing_deps,
Error *r_err) {
Error err = FAILED;
if (r_err) {
*r_err = err;
}
Ref<GLTFDocument> gltf_document;
gltf_document.instantiate();
Ref<GLTFState> state;
state.instantiate();
err = gltf_document->serialize(state, p_node, p_path);
if (r_err) {
*r_err = err;
}
}

void PackedSceneGLTF::_build_parent_hierachy(Ref<GLTFState> state) {
// build the hierarchy
for (GLTFNodeIndex node_i = 0; node_i < state->nodes.size(); node_i++) {
for (int j = 0; j < state->nodes[node_i]->children.size(); j++) {
GLTFNodeIndex child_i = state->nodes[node_i]->children[j];
ERR_FAIL_INDEX(child_i, state->nodes.size());
if (state->nodes.write[child_i]->parent != -1) {
continue;
}
state->nodes.write[child_i]->parent = node_i;
}
}
}

Error PackedSceneGLTF::export_gltf(Node *p_root, String p_path,
int32_t p_flags,
real_t p_bake_fps) {
ERR_FAIL_COND_V(!p_root, FAILED);
List<String> deps;
Error err;
String path = p_path;
int32_t flags = p_flags;
real_t baked_fps = p_bake_fps;
Ref<PackedSceneGLTF> exporter;
exporter.instantiate();
exporter->save_scene(p_root, path, "", flags, baked_fps, &deps, &err);
int32_t error_code = err;
if (error_code != 0) {
return Error(error_code);
}
return OK;
}
28 changes: 1 addition & 27 deletions modules/gltf/editor_scene_importer_gltf.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,35 +46,9 @@ class EditorSceneImporterGLTF : public EditorSceneImporter {
public:
virtual uint32_t get_import_flags() const override;
virtual void get_extensions(List<String> *r_extensions) const override;
virtual Node *import_scene(const String &p_path, uint32_t p_flags,
int p_bake_fps,
List<String> *r_missing_deps = nullptr,
Error *r_err = nullptr) override;
virtual Node *import_scene(const String &p_path, uint32_t p_flags, int p_bake_fps, List<String> *r_missing_deps, Error *r_err = nullptr) override;
virtual Ref<Animation> import_animation(const String &p_path,
uint32_t p_flags, int p_bake_fps) override;
};
#endif

class PackedSceneGLTF : public PackedScene {
GDCLASS(PackedSceneGLTF, PackedScene);

protected:
static void _bind_methods();

public:
virtual void save_scene(Node *p_node, const String &p_path, const String &p_src_path,
uint32_t p_flags, int p_bake_fps,
List<String> *r_missing_deps, Error *r_err = nullptr);
virtual void _build_parent_hierachy(Ref<GLTFState> state);
virtual Error export_gltf(Node *p_root, String p_path, int32_t p_flags = 0,
real_t p_bake_fps = 1000.0f);
virtual Node *import_scene(const String &p_path, uint32_t p_flags,
int p_bake_fps,
List<String> *r_missing_deps,
Error *r_err,
Ref<GLTFState> r_state);
virtual Node *import_gltf_scene(const String &p_path, uint32_t p_flags, float p_bake_fps, Ref<GLTFState> r_state = Ref<GLTFState>());
virtual void pack_gltf(String p_path, int32_t p_flags = 0,
real_t p_bake_fps = 1000.0f, Ref<GLTFState> r_state = Ref<GLTFState>());
};
#endif // EDITOR_SCENE_IMPORTER_GLTF_H
3 changes: 1 addition & 2 deletions modules/gltf/gltf_accessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ struct GLTFAccessor : public Resource {
int component_type = 0;
bool normalized = false;
int count = 0;
GLTFDocument::GLTFType
type = GLTFDocument::TYPE_SCALAR;
GLTFDocument::GLTFType type = GLTFDocument::TYPE_SCALAR;
Vector<double> min;
Vector<double> max;
int sparse_count = 0;
Expand Down
76 changes: 76 additions & 0 deletions modules/gltf/gltf_document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
#include "scene/resources/surface_tool.h"

#include "modules/modules_enabled.gen.h"
#include <cstdint>
#ifdef MODULE_CSG_ENABLED
#include "modules/csg/csg_shape.h"
#endif // MODULE_CSG_ENABLED
Expand Down Expand Up @@ -6630,3 +6631,78 @@ Error GLTFDocument::_serialize_file(Ref<GLTFState> state, const String p_path) {
}
return err;
}

Error GLTFDocument::save_scene(Node *p_node, const String &p_path,
const String &p_src_path, uint32_t p_flags,
float p_bake_fps, Ref<GLTFState> r_state) {
Ref<GLTFDocument> gltf_document;
gltf_document.instantiate();
if (r_state == Ref<GLTFState>()) {
r_state.instantiate();
}
return gltf_document->serialize(r_state, p_node, p_path);
}

Node *GLTFDocument::import_scene_gltf(const String &p_path, uint32_t p_flags, int32_t p_bake_fps, Ref<GLTFState> r_state, List<String> *r_missing_deps, Error *r_err) {
// TODO Add missing texture and missing .bin file paths to r_missing_deps 2021-09-10 fire
if (r_state == Ref<GLTFState>()) {
r_state.instantiate();
}
r_state->use_named_skin_binds =
p_flags & EditorSceneImporter::IMPORT_USE_NAMED_SKIN_BINDS;

Ref<GLTFDocument> gltf_document;
gltf_document.instantiate();
Error err = gltf_document->parse(r_state, p_path);
if (r_err) {
*r_err = err;
}
ERR_FAIL_COND_V(err != Error::OK, nullptr);

Node3D *root = memnew(Node3D);
for (int32_t root_i = 0; root_i < r_state->root_nodes.size(); root_i++) {
gltf_document->_generate_scene_node(r_state, root, root, r_state->root_nodes[root_i]);
}
gltf_document->_process_mesh_instances(r_state, root);
if (r_state->animations.size()) {
AnimationPlayer *ap = memnew(AnimationPlayer);
root->add_child(ap);
ap->set_owner(root);
for (int i = 0; i < r_state->animations.size(); i++) {
gltf_document->_import_animation(r_state, ap, i, p_bake_fps);
}
}

return root;
}

void GLTFDocument::_bind_methods() {
ClassDB::bind_method(D_METHOD("save_scene", "node", "path", "src_path", "flags", "bake_fps", "state"),
&GLTFDocument::save_scene, DEFVAL(0), DEFVAL(30), DEFVAL(Ref<GLTFState>()));
ClassDB::bind_method(D_METHOD("import_scene", "path", "flags", "bake_fps", "state"),
&GLTFDocument::import_scene, DEFVAL(0), DEFVAL(30), DEFVAL(Ref<GLTFState>()));
}

void GLTFDocument::_build_parent_hierachy(Ref<GLTFState> state) {
// build the hierarchy
for (GLTFNodeIndex node_i = 0; node_i < state->nodes.size(); node_i++) {
for (int j = 0; j < state->nodes[node_i]->children.size(); j++) {
GLTFNodeIndex child_i = state->nodes[node_i]->children[j];
ERR_FAIL_INDEX(child_i, state->nodes.size());
if (state->nodes.write[child_i]->parent != -1) {
continue;
}
state->nodes.write[child_i]->parent = node_i;
}
}
}

Node *GLTFDocument::import_scene(const String &p_path, uint32_t p_flags, int32_t p_bake_fps, Ref<GLTFState> r_state) {
Error err = FAILED;
List<String> deps;
Node *node = import_scene_gltf(p_path, p_flags, p_bake_fps, r_state, &deps, &err);
if (err != OK) {
return nullptr;
}
return node;
}
Loading

0 comments on commit 523b2d9

Please sign in to comment.