Path: blob/master/modules/gltf/structures/gltf_node.cpp
10279 views
/**************************************************************************/1/* gltf_node.cpp */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#include "gltf_node.h"3132#include "../gltf_state.h"3334void GLTFNode::_bind_methods() {35ClassDB::bind_method(D_METHOD("get_original_name"), &GLTFNode::get_original_name);36ClassDB::bind_method(D_METHOD("set_original_name", "original_name"), &GLTFNode::set_original_name);37ClassDB::bind_method(D_METHOD("get_parent"), &GLTFNode::get_parent);38ClassDB::bind_method(D_METHOD("set_parent", "parent"), &GLTFNode::set_parent);39ClassDB::bind_method(D_METHOD("get_height"), &GLTFNode::get_height);40ClassDB::bind_method(D_METHOD("set_height", "height"), &GLTFNode::set_height);41ClassDB::bind_method(D_METHOD("get_xform"), &GLTFNode::get_xform);42ClassDB::bind_method(D_METHOD("set_xform", "xform"), &GLTFNode::set_xform);43ClassDB::bind_method(D_METHOD("get_mesh"), &GLTFNode::get_mesh);44ClassDB::bind_method(D_METHOD("set_mesh", "mesh"), &GLTFNode::set_mesh);45ClassDB::bind_method(D_METHOD("get_camera"), &GLTFNode::get_camera);46ClassDB::bind_method(D_METHOD("set_camera", "camera"), &GLTFNode::set_camera);47ClassDB::bind_method(D_METHOD("get_skin"), &GLTFNode::get_skin);48ClassDB::bind_method(D_METHOD("set_skin", "skin"), &GLTFNode::set_skin);49ClassDB::bind_method(D_METHOD("get_skeleton"), &GLTFNode::get_skeleton);50ClassDB::bind_method(D_METHOD("set_skeleton", "skeleton"), &GLTFNode::set_skeleton);51ClassDB::bind_method(D_METHOD("get_position"), &GLTFNode::get_position);52ClassDB::bind_method(D_METHOD("set_position", "position"), &GLTFNode::set_position);53ClassDB::bind_method(D_METHOD("get_rotation"), &GLTFNode::get_rotation);54ClassDB::bind_method(D_METHOD("set_rotation", "rotation"), &GLTFNode::set_rotation);55ClassDB::bind_method(D_METHOD("get_scale"), &GLTFNode::get_scale);56ClassDB::bind_method(D_METHOD("set_scale", "scale"), &GLTFNode::set_scale);57ClassDB::bind_method(D_METHOD("get_children"), &GLTFNode::get_children);58ClassDB::bind_method(D_METHOD("set_children", "children"), &GLTFNode::set_children);59ClassDB::bind_method(D_METHOD("append_child_index", "child_index"), &GLTFNode::append_child_index);60ClassDB::bind_method(D_METHOD("get_light"), &GLTFNode::get_light);61ClassDB::bind_method(D_METHOD("set_light", "light"), &GLTFNode::set_light);62ClassDB::bind_method(D_METHOD("get_visible"), &GLTFNode::get_visible);63ClassDB::bind_method(D_METHOD("set_visible", "visible"), &GLTFNode::set_visible);64ClassDB::bind_method(D_METHOD("get_additional_data", "extension_name"), &GLTFNode::get_additional_data);65ClassDB::bind_method(D_METHOD("set_additional_data", "extension_name", "additional_data"), &GLTFNode::set_additional_data);66ClassDB::bind_method(D_METHOD("get_scene_node_path", "gltf_state", "handle_skeletons"), &GLTFNode::get_scene_node_path, DEFVAL(true));6768ADD_PROPERTY(PropertyInfo(Variant::STRING, "original_name"), "set_original_name", "get_original_name"); // String69ADD_PROPERTY(PropertyInfo(Variant::INT, "parent"), "set_parent", "get_parent"); // GLTFNodeIndex70ADD_PROPERTY(PropertyInfo(Variant::INT, "height"), "set_height", "get_height"); // int71ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM3D, "xform"), "set_xform", "get_xform"); // Transform3D72ADD_PROPERTY(PropertyInfo(Variant::INT, "mesh"), "set_mesh", "get_mesh"); // GLTFMeshIndex73ADD_PROPERTY(PropertyInfo(Variant::INT, "camera"), "set_camera", "get_camera"); // GLTFCameraIndex74ADD_PROPERTY(PropertyInfo(Variant::INT, "skin"), "set_skin", "get_skin"); // GLTFSkinIndex75ADD_PROPERTY(PropertyInfo(Variant::INT, "skeleton"), "set_skeleton", "get_skeleton"); // GLTFSkeletonIndex76ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "position"), "set_position", "get_position"); // Vector377ADD_PROPERTY(PropertyInfo(Variant::QUATERNION, "rotation"), "set_rotation", "get_rotation"); // Quaternion78ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "scale"), "set_scale", "get_scale"); // Vector379ADD_PROPERTY(PropertyInfo(Variant::PACKED_INT32_ARRAY, "children"), "set_children", "get_children"); // Vector<int>80ADD_PROPERTY(PropertyInfo(Variant::INT, "light"), "set_light", "get_light"); // GLTFLightIndex81ADD_PROPERTY(PropertyInfo(Variant::BOOL, "visible"), "set_visible", "get_visible"); // bool82}8384String GLTFNode::get_original_name() {85return original_name;86}87void GLTFNode::set_original_name(String p_name) {88original_name = p_name;89}9091GLTFNodeIndex GLTFNode::get_parent() {92return parent;93}9495void GLTFNode::set_parent(GLTFNodeIndex p_parent) {96parent = p_parent;97}9899int GLTFNode::get_height() {100return height;101}102103void GLTFNode::set_height(int p_height) {104height = p_height;105}106107Transform3D GLTFNode::get_xform() {108return transform;109}110111void GLTFNode::set_xform(Transform3D p_xform) {112transform = p_xform;113}114115GLTFMeshIndex GLTFNode::get_mesh() {116return mesh;117}118119void GLTFNode::set_mesh(GLTFMeshIndex p_mesh) {120mesh = p_mesh;121}122123GLTFCameraIndex GLTFNode::get_camera() {124return camera;125}126127void GLTFNode::set_camera(GLTFCameraIndex p_camera) {128camera = p_camera;129}130131GLTFSkinIndex GLTFNode::get_skin() {132return skin;133}134135void GLTFNode::set_skin(GLTFSkinIndex p_skin) {136skin = p_skin;137}138139GLTFSkeletonIndex GLTFNode::get_skeleton() {140return skeleton;141}142143void GLTFNode::set_skeleton(GLTFSkeletonIndex p_skeleton) {144skeleton = p_skeleton;145}146147Vector3 GLTFNode::get_position() {148return transform.origin;149}150151void GLTFNode::set_position(Vector3 p_position) {152transform.origin = p_position;153}154155Quaternion GLTFNode::get_rotation() {156return transform.basis.get_rotation_quaternion();157}158159void GLTFNode::set_rotation(Quaternion p_rotation) {160transform.basis.set_quaternion_scale(p_rotation, transform.basis.get_scale());161}162163Vector3 GLTFNode::get_scale() {164return transform.basis.get_scale();165}166167void GLTFNode::set_scale(Vector3 p_scale) {168transform.basis = transform.basis.orthonormalized() * Basis::from_scale(p_scale);169}170171Vector<int> GLTFNode::get_children() {172return children;173}174175void GLTFNode::set_children(Vector<int> p_children) {176children = p_children;177}178179void GLTFNode::append_child_index(int p_child_index) {180children.append(p_child_index);181}182183GLTFLightIndex GLTFNode::get_light() {184return light;185}186187void GLTFNode::set_light(GLTFLightIndex p_light) {188light = p_light;189}190191bool GLTFNode::get_visible() {192return visible;193}194195void GLTFNode::set_visible(bool p_visible) {196visible = p_visible;197}198199Variant GLTFNode::get_additional_data(const StringName &p_extension_name) {200return additional_data[p_extension_name];201}202203bool GLTFNode::has_additional_data(const StringName &p_extension_name) {204return additional_data.has(p_extension_name);205}206207void GLTFNode::set_additional_data(const StringName &p_extension_name, Variant p_additional_data) {208additional_data[p_extension_name] = p_additional_data;209}210211NodePath GLTFNode::get_scene_node_path(Ref<GLTFState> p_state, bool p_handle_skeletons) {212Vector<StringName> path;213Vector<StringName> subpath;214Ref<GLTFNode> current_gltf_node = this;215const int gltf_node_count = p_state->nodes.size();216if (p_handle_skeletons && skeleton != -1) {217// Special case for skeleton nodes, skip all bones so that the path is to the Skeleton3D node.218// A path that would otherwise be `A/B/C/Bone1/Bone2/Bone3` becomes `A/B/C/Skeleton3D:Bone3`.219subpath.append(get_name());220// The generated Skeleton3D node will be named Skeleton3D, so add it to the path.221path.append("Skeleton3D");222do {223const int parent_index = current_gltf_node->get_parent();224ERR_FAIL_INDEX_V(parent_index, gltf_node_count, NodePath());225current_gltf_node = p_state->nodes[parent_index];226} while (current_gltf_node->skeleton != -1);227}228const bool is_godot_single_root = p_state->extensions_used.has("GODOT_single_root");229while (true) {230const int parent_index = current_gltf_node->get_parent();231if (is_godot_single_root && parent_index == -1) {232// For GODOT_single_root scenes, the root glTF node becomes the Godot scene root, so it233// should not be included in the path. Ex: A/B/C, A is single root, we want B/C only.234break;235}236path.insert(0, current_gltf_node->get_name());237if (!is_godot_single_root && parent_index == -1) {238break;239}240ERR_FAIL_INDEX_V(parent_index, gltf_node_count, NodePath());241current_gltf_node = p_state->nodes[parent_index];242}243if (unlikely(path.is_empty())) {244path.append(".");245}246return NodePath(path, subpath, false);247}248249250