Path: blob/master/modules/gltf/structures/gltf_skin.cpp
10278 views
/**************************************************************************/1/* gltf_skin.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_skin.h"3132#include "../gltf_template_convert.h"3334#include "core/variant/typed_array.h"35#include "scene/resources/3d/skin.h"3637void GLTFSkin::_bind_methods() {38ClassDB::bind_method(D_METHOD("get_skin_root"), &GLTFSkin::get_skin_root);39ClassDB::bind_method(D_METHOD("set_skin_root", "skin_root"), &GLTFSkin::set_skin_root);40ClassDB::bind_method(D_METHOD("get_joints_original"), &GLTFSkin::get_joints_original);41ClassDB::bind_method(D_METHOD("set_joints_original", "joints_original"), &GLTFSkin::set_joints_original);42ClassDB::bind_method(D_METHOD("get_inverse_binds"), &GLTFSkin::get_inverse_binds);43ClassDB::bind_method(D_METHOD("set_inverse_binds", "inverse_binds"), &GLTFSkin::set_inverse_binds);44ClassDB::bind_method(D_METHOD("get_joints"), &GLTFSkin::get_joints);45ClassDB::bind_method(D_METHOD("set_joints", "joints"), &GLTFSkin::set_joints);46ClassDB::bind_method(D_METHOD("get_non_joints"), &GLTFSkin::get_non_joints);47ClassDB::bind_method(D_METHOD("set_non_joints", "non_joints"), &GLTFSkin::set_non_joints);48ClassDB::bind_method(D_METHOD("get_roots"), &GLTFSkin::get_roots);49ClassDB::bind_method(D_METHOD("set_roots", "roots"), &GLTFSkin::set_roots);50ClassDB::bind_method(D_METHOD("get_skeleton"), &GLTFSkin::get_skeleton);51ClassDB::bind_method(D_METHOD("set_skeleton", "skeleton"), &GLTFSkin::set_skeleton);52ClassDB::bind_method(D_METHOD("get_joint_i_to_bone_i"), &GLTFSkin::get_joint_i_to_bone_i);53ClassDB::bind_method(D_METHOD("set_joint_i_to_bone_i", "joint_i_to_bone_i"), &GLTFSkin::set_joint_i_to_bone_i);54ClassDB::bind_method(D_METHOD("get_joint_i_to_name"), &GLTFSkin::get_joint_i_to_name);55ClassDB::bind_method(D_METHOD("set_joint_i_to_name", "joint_i_to_name"), &GLTFSkin::set_joint_i_to_name);56ClassDB::bind_method(D_METHOD("get_godot_skin"), &GLTFSkin::get_godot_skin);57ClassDB::bind_method(D_METHOD("set_godot_skin", "godot_skin"), &GLTFSkin::set_godot_skin);5859ADD_PROPERTY(PropertyInfo(Variant::INT, "skin_root"), "set_skin_root", "get_skin_root"); // GLTFNodeIndex60ADD_PROPERTY(PropertyInfo(Variant::PACKED_INT32_ARRAY, "joints_original"), "set_joints_original", "get_joints_original"); // Vector<GLTFNodeIndex>61ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "inverse_binds", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_INTERNAL), "set_inverse_binds", "get_inverse_binds"); // Vector<Transform3D>62ADD_PROPERTY(PropertyInfo(Variant::PACKED_INT32_ARRAY, "joints"), "set_joints", "get_joints"); // Vector<GLTFNodeIndex>63ADD_PROPERTY(PropertyInfo(Variant::PACKED_INT32_ARRAY, "non_joints"), "set_non_joints", "get_non_joints"); // Vector<GLTFNodeIndex>64ADD_PROPERTY(PropertyInfo(Variant::PACKED_INT32_ARRAY, "roots"), "set_roots", "get_roots"); // Vector<GLTFNodeIndex>65ADD_PROPERTY(PropertyInfo(Variant::INT, "skeleton"), "set_skeleton", "get_skeleton"); // int66ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "joint_i_to_bone_i", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_INTERNAL), "set_joint_i_to_bone_i", "get_joint_i_to_bone_i"); // RBMap<int,67ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "joint_i_to_name", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_INTERNAL), "set_joint_i_to_name", "get_joint_i_to_name"); // RBMap<int,68ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "godot_skin", PROPERTY_HINT_RESOURCE_TYPE, "Skin"), "set_godot_skin", "get_godot_skin"); // Ref<Skin>69}7071GLTFNodeIndex GLTFSkin::get_skin_root() {72return skin_root;73}7475void GLTFSkin::set_skin_root(GLTFNodeIndex p_skin_root) {76skin_root = p_skin_root;77}7879Vector<GLTFNodeIndex> GLTFSkin::get_joints_original() {80return joints_original;81}8283void GLTFSkin::set_joints_original(Vector<GLTFNodeIndex> p_joints_original) {84joints_original = p_joints_original;85}8687TypedArray<Transform3D> GLTFSkin::get_inverse_binds() {88return GLTFTemplateConvert::to_array(inverse_binds);89}9091void GLTFSkin::set_inverse_binds(TypedArray<Transform3D> p_inverse_binds) {92GLTFTemplateConvert::set_from_array(inverse_binds, p_inverse_binds);93}9495Vector<GLTFNodeIndex> GLTFSkin::get_joints() {96return joints;97}9899void GLTFSkin::set_joints(Vector<GLTFNodeIndex> p_joints) {100joints = p_joints;101}102103Vector<GLTFNodeIndex> GLTFSkin::get_non_joints() {104return non_joints;105}106107void GLTFSkin::set_non_joints(Vector<GLTFNodeIndex> p_non_joints) {108non_joints = p_non_joints;109}110111Vector<GLTFNodeIndex> GLTFSkin::get_roots() {112return roots;113}114115void GLTFSkin::set_roots(Vector<GLTFNodeIndex> p_roots) {116roots = p_roots;117}118119int GLTFSkin::get_skeleton() {120return skeleton;121}122123void GLTFSkin::set_skeleton(int p_skeleton) {124skeleton = p_skeleton;125}126127Dictionary GLTFSkin::get_joint_i_to_bone_i() {128return GLTFTemplateConvert::to_dictionary(joint_i_to_bone_i);129}130131void GLTFSkin::set_joint_i_to_bone_i(Dictionary p_joint_i_to_bone_i) {132GLTFTemplateConvert::set_from_dictionary(joint_i_to_bone_i, p_joint_i_to_bone_i);133}134135Dictionary GLTFSkin::get_joint_i_to_name() {136Dictionary ret;137HashMap<int, StringName>::Iterator elem = joint_i_to_name.begin();138while (elem) {139ret[elem->key] = String(elem->value);140++elem;141}142return ret;143}144145void GLTFSkin::set_joint_i_to_name(Dictionary p_joint_i_to_name) {146joint_i_to_name = HashMap<int, StringName>();147for (const KeyValue<Variant, Variant> &kv : p_joint_i_to_name) {148joint_i_to_name[kv.key] = kv.value;149}150}151152Ref<Skin> GLTFSkin::get_godot_skin() {153return godot_skin;154}155156void GLTFSkin::set_godot_skin(Ref<Skin> p_godot_skin) {157godot_skin = p_godot_skin;158}159160Error GLTFSkin::from_dictionary(const Dictionary &dict) {161ERR_FAIL_COND_V(!dict.has("skin_root"), ERR_INVALID_DATA);162skin_root = dict["skin_root"];163164ERR_FAIL_COND_V(!dict.has("joints_original"), ERR_INVALID_DATA);165Array joints_original_array = dict["joints_original"];166joints_original.clear();167for (int i = 0; i < joints_original_array.size(); ++i) {168joints_original.push_back(joints_original_array[i]);169}170171ERR_FAIL_COND_V(!dict.has("inverse_binds"), ERR_INVALID_DATA);172Array inverse_binds_array = dict["inverse_binds"];173inverse_binds.clear();174for (int i = 0; i < inverse_binds_array.size(); ++i) {175ERR_FAIL_COND_V(inverse_binds_array[i].get_type() != Variant::TRANSFORM3D, ERR_INVALID_DATA);176inverse_binds.push_back(inverse_binds_array[i]);177}178179ERR_FAIL_COND_V(!dict.has("joints"), ERR_INVALID_DATA);180Array joints_array = dict["joints"];181joints.clear();182for (int i = 0; i < joints_array.size(); ++i) {183joints.push_back(joints_array[i]);184}185186ERR_FAIL_COND_V(!dict.has("non_joints"), ERR_INVALID_DATA);187Array non_joints_array = dict["non_joints"];188non_joints.clear();189for (int i = 0; i < non_joints_array.size(); ++i) {190non_joints.push_back(non_joints_array[i]);191}192193ERR_FAIL_COND_V(!dict.has("roots"), ERR_INVALID_DATA);194Array roots_array = dict["roots"];195roots.clear();196for (int i = 0; i < roots_array.size(); ++i) {197roots.push_back(roots_array[i]);198}199200ERR_FAIL_COND_V(!dict.has("skeleton"), ERR_INVALID_DATA);201skeleton = dict["skeleton"];202203ERR_FAIL_COND_V(!dict.has("joint_i_to_bone_i"), ERR_INVALID_DATA);204Dictionary joint_i_to_bone_i_dict = dict["joint_i_to_bone_i"];205joint_i_to_bone_i.clear();206for (const KeyValue<Variant, Variant> &kv : joint_i_to_bone_i_dict) {207int key = kv.key;208int value = kv.value;209joint_i_to_bone_i[key] = value;210}211212ERR_FAIL_COND_V(!dict.has("joint_i_to_name"), ERR_INVALID_DATA);213Dictionary joint_i_to_name_dict = dict["joint_i_to_name"];214joint_i_to_name.clear();215for (const KeyValue<Variant, Variant> &kv : joint_i_to_name_dict) {216int key = kv.key;217StringName value = kv.value;218joint_i_to_name[key] = value;219}220if (dict.has("godot_skin")) {221godot_skin = dict["godot_skin"];222}223return OK;224}225226Dictionary GLTFSkin::to_dictionary() {227Dictionary dict;228dict["skin_root"] = skin_root;229230Array joints_original_array;231for (int i = 0; i < joints_original.size(); ++i) {232joints_original_array.push_back(joints_original[i]);233}234dict["joints_original"] = joints_original_array;235236Array inverse_binds_array;237for (int i = 0; i < inverse_binds.size(); ++i) {238inverse_binds_array.push_back(inverse_binds[i]);239}240dict["inverse_binds"] = inverse_binds_array;241242Array joints_array;243for (int i = 0; i < joints.size(); ++i) {244joints_array.push_back(joints[i]);245}246dict["joints"] = joints_array;247248Array non_joints_array;249for (int i = 0; i < non_joints.size(); ++i) {250non_joints_array.push_back(non_joints[i]);251}252dict["non_joints"] = non_joints_array;253254Array roots_array;255for (int i = 0; i < roots.size(); ++i) {256roots_array.push_back(roots[i]);257}258dict["roots"] = roots_array;259260dict["skeleton"] = skeleton;261262Dictionary joint_i_to_bone_i_dict;263for (HashMap<int, int>::Iterator E = joint_i_to_bone_i.begin(); E; ++E) {264joint_i_to_bone_i_dict[E->key] = E->value;265}266dict["joint_i_to_bone_i"] = joint_i_to_bone_i_dict;267268Dictionary joint_i_to_name_dict;269for (HashMap<int, StringName>::Iterator E = joint_i_to_name.begin(); E; ++E) {270joint_i_to_name_dict[E->key] = E->value;271}272dict["joint_i_to_name"] = joint_i_to_name_dict;273274dict["godot_skin"] = godot_skin;275return dict;276}277278279