Path: blob/master/modules/gltf/tests/test_gltf_extras.h
10278 views
/**************************************************************************/1/* test_gltf_extras.h */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#pragma once3132#include "test_gltf.h"33#include "tests/test_macros.h"3435#ifdef TOOLS_ENABLED3637#include "core/os/os.h"38#include "editor/import/3d/resource_importer_scene.h"39#include "scene/3d/mesh_instance_3d.h"40#include "scene/3d/skeleton_3d.h"41#include "scene/main/window.h"42#include "scene/resources/3d/primitive_meshes.h"43#include "scene/resources/material.h"44#include "scene/resources/packed_scene.h"4546#include "modules/gltf/editor/editor_scene_importer_gltf.h"47#include "modules/gltf/gltf_document.h"48#include "modules/gltf/gltf_state.h"4950namespace TestGltf {5152TEST_CASE("[SceneTree][Node] GLTF test mesh and material meta export and import") {53init("gltf_mesh_material_extras");54// Setup scene.55Ref<StandardMaterial3D> original_material = memnew(StandardMaterial3D);56original_material->set_albedo(Color(1.0, .0, .0));57original_material->set_name("material");58Dictionary material_dict;59material_dict["node_type"] = "material";60original_material->set_meta("extras", material_dict);6162Ref<PlaneMesh> original_meshdata = memnew(PlaneMesh);63original_meshdata->set_name("planemesh");64Dictionary meshdata_dict;65meshdata_dict["node_type"] = "planemesh";66original_meshdata->set_meta("extras", meshdata_dict);67original_meshdata->surface_set_material(0, original_material);6869MeshInstance3D *original_mesh_instance = memnew(MeshInstance3D);70original_mesh_instance->set_mesh(original_meshdata);71original_mesh_instance->set_name("mesh_instance_3d");72Dictionary mesh_instance_dict;73mesh_instance_dict["node_type"] = "mesh_instance_3d";74original_mesh_instance->set_meta("extras", mesh_instance_dict);7576Node3D *original = memnew(Node3D);77SceneTree::get_singleton()->get_root()->add_child(original);78original->add_child(original_mesh_instance);79original->set_name("node3d");80Dictionary node_dict;81node_dict["node_type"] = "node3d";82original->set_meta("extras", node_dict);83original->set_meta("meta_not_nested_under_extras", "should not propagate");8485original->set_owner(SceneTree::get_singleton()->get_root());86original_mesh_instance->set_owner(SceneTree::get_singleton()->get_root());8788// Convert to GLFT and back.89Node *loaded = gltf_export_then_import(original, "gltf_extras");9091// Compare the results.92CHECK(loaded->get_name() == "node3d");93CHECK(Dictionary(loaded->get_meta("extras")).size() == 1);94CHECK(Dictionary(loaded->get_meta("extras"))["node_type"] == "node3d");95CHECK_FALSE(loaded->has_meta("meta_not_nested_under_extras"));96CHECK_FALSE(Dictionary(loaded->get_meta("extras")).has("meta_not_nested_under_extras"));9798MeshInstance3D *mesh_instance_3d = Object::cast_to<MeshInstance3D>(loaded->find_child("mesh_instance_3d", false, true));99CHECK(mesh_instance_3d->get_name() == "mesh_instance_3d");100CHECK(Dictionary(mesh_instance_3d->get_meta("extras"))["node_type"] == "mesh_instance_3d");101102Ref<Mesh> mesh = mesh_instance_3d->get_mesh();103CHECK(Dictionary(mesh->get_meta("extras"))["node_type"] == "planemesh");104105Ref<Material> material = mesh->surface_get_material(0);106CHECK(material->get_name() == "material");107CHECK(Dictionary(material->get_meta("extras"))["node_type"] == "material");108109memdelete(original_mesh_instance);110memdelete(original);111memdelete(loaded);112}113114TEST_CASE("[SceneTree][Node] GLTF test skeleton and bone export and import") {115init("gltf_skeleton_extras");116// Setup scene.117Skeleton3D *skeleton = memnew(Skeleton3D);118skeleton->set_name("skeleton");119Dictionary skeleton_extras;120skeleton_extras["node_type"] = "skeleton";121skeleton->set_meta("extras", skeleton_extras);122123skeleton->add_bone("parent");124skeleton->set_bone_rest(0, Transform3D());125Dictionary parent_bone_extras;126parent_bone_extras["bone"] = "i_am_parent_bone";127skeleton->set_bone_meta(0, "extras", parent_bone_extras);128129skeleton->add_bone("child");130skeleton->set_bone_rest(1, Transform3D());131skeleton->set_bone_parent(1, 0);132Dictionary child_bone_extras;133child_bone_extras["bone"] = "i_am_child_bone";134skeleton->set_bone_meta(1, "extras", child_bone_extras);135136// We have to have a mesh to link with skeleton or it will not get imported.137Ref<PlaneMesh> meshdata = memnew(PlaneMesh);138meshdata->set_name("planemesh");139140MeshInstance3D *mesh = memnew(MeshInstance3D);141mesh->set_mesh(meshdata);142mesh->set_name("mesh_instance_3d");143144Node3D *original = memnew(Node3D);145SceneTree::get_singleton()->get_root()->add_child(original);146original->add_child(skeleton);147original->add_child(mesh);148original->set_name("node3d");149150// Now that both skeleton and mesh are part of scene, link them.151mesh->set_skeleton_path(mesh->get_path_to(skeleton));152153mesh->set_owner(SceneTree::get_singleton()->get_root());154original->set_owner(SceneTree::get_singleton()->get_root());155156// Convert to GLFT and back.157Node *loaded = gltf_export_then_import(original, "gltf_bone_extras");158159// Compare the results.160CHECK(loaded->get_name() == "node3d");161Skeleton3D *result = Object::cast_to<Skeleton3D>(loaded->find_child("Skeleton3D", false, true));162CHECK(result->get_bone_name(0) == "parent");163CHECK(Dictionary(result->get_bone_meta(0, "extras"))["bone"] == "i_am_parent_bone");164CHECK(result->get_bone_name(1) == "child");165CHECK(Dictionary(result->get_bone_meta(1, "extras"))["bone"] == "i_am_child_bone");166167memdelete(skeleton);168memdelete(mesh);169memdelete(original);170memdelete(loaded);171}172} //namespace TestGltf173174#endif // TOOLS_ENABLED175176177