Path: blob/master/modules/gltf/extensions/gltf_document_extension.cpp
10278 views
/**************************************************************************/1/* gltf_document_extension.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_document_extension.h"3132void GLTFDocumentExtension::_bind_methods() {33// Import process.34GDVIRTUAL_BIND(_import_preflight, "state", "extensions");35GDVIRTUAL_BIND(_get_supported_extensions);36GDVIRTUAL_BIND(_parse_node_extensions, "state", "gltf_node", "extensions");37GDVIRTUAL_BIND(_parse_image_data, "state", "image_data", "mime_type", "ret_image");38GDVIRTUAL_BIND(_get_image_file_extension);39GDVIRTUAL_BIND(_parse_texture_json, "state", "texture_json", "ret_gltf_texture");40GDVIRTUAL_BIND(_import_object_model_property, "state", "split_json_pointer", "partial_paths");41GDVIRTUAL_BIND(_import_post_parse, "state");42GDVIRTUAL_BIND(_import_pre_generate, "state");43GDVIRTUAL_BIND(_generate_scene_node, "state", "gltf_node", "scene_parent");44GDVIRTUAL_BIND(_import_node, "state", "gltf_node", "json", "node");45GDVIRTUAL_BIND(_import_post, "state", "root");46// Export process.47GDVIRTUAL_BIND(_export_preflight, "state", "root");48GDVIRTUAL_BIND(_convert_scene_node, "state", "gltf_node", "scene_node");49GDVIRTUAL_BIND(_export_post_convert, "state", "root");50GDVIRTUAL_BIND(_export_preserialize, "state");51GDVIRTUAL_BIND(_export_object_model_property, "state", "node_path", "godot_node", "gltf_node_index", "target_object", "target_depth");52GDVIRTUAL_BIND(_get_saveable_image_formats);53GDVIRTUAL_BIND(_serialize_image_to_bytes, "state", "image", "image_dict", "image_format", "lossy_quality");54GDVIRTUAL_BIND(_save_image_at_path, "state", "image", "file_path", "image_format", "lossy_quality");55GDVIRTUAL_BIND(_serialize_texture_json, "state", "texture_json", "gltf_texture", "image_format");56GDVIRTUAL_BIND(_export_node, "state", "gltf_node", "json", "node");57GDVIRTUAL_BIND(_export_post, "state");58}5960// Import process.61Error GLTFDocumentExtension::import_preflight(Ref<GLTFState> p_state, Vector<String> p_extensions) {62ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);63Error err = OK;64GDVIRTUAL_CALL(_import_preflight, p_state, p_extensions, err);65return err;66}6768Vector<String> GLTFDocumentExtension::get_supported_extensions() {69Vector<String> ret;70GDVIRTUAL_CALL(_get_supported_extensions, ret);71return ret;72}7374Error GLTFDocumentExtension::parse_node_extensions(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Dictionary &p_extensions) {75ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);76ERR_FAIL_COND_V(p_gltf_node.is_null(), ERR_INVALID_PARAMETER);77Error err = OK;78GDVIRTUAL_CALL(_parse_node_extensions, p_state, p_gltf_node, p_extensions, err);79return err;80}8182Error GLTFDocumentExtension::parse_image_data(Ref<GLTFState> p_state, const PackedByteArray &p_image_data, const String &p_mime_type, Ref<Image> r_image) {83ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);84ERR_FAIL_COND_V(r_image.is_null(), ERR_INVALID_PARAMETER);85Error err = OK;86GDVIRTUAL_CALL(_parse_image_data, p_state, p_image_data, p_mime_type, r_image, err);87return err;88}8990String GLTFDocumentExtension::get_image_file_extension() {91String ret;92GDVIRTUAL_CALL(_get_image_file_extension, ret);93return ret;94}9596Error GLTFDocumentExtension::parse_texture_json(Ref<GLTFState> p_state, const Dictionary &p_texture_json, Ref<GLTFTexture> r_gltf_texture) {97ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);98ERR_FAIL_COND_V(r_gltf_texture.is_null(), ERR_INVALID_PARAMETER);99Error err = OK;100GDVIRTUAL_CALL(_parse_texture_json, p_state, p_texture_json, r_gltf_texture, err);101return err;102}103104Ref<GLTFObjectModelProperty> GLTFDocumentExtension::import_object_model_property(Ref<GLTFState> p_state, const PackedStringArray &p_split_json_pointer, const TypedArray<NodePath> &p_partial_paths) {105Ref<GLTFObjectModelProperty> ret;106ERR_FAIL_COND_V(p_state.is_null(), ret);107GDVIRTUAL_CALL(_import_object_model_property, p_state, p_split_json_pointer, p_partial_paths, ret);108return ret;109}110111Error GLTFDocumentExtension::import_post_parse(Ref<GLTFState> p_state) {112ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);113Error err = OK;114GDVIRTUAL_CALL(_import_post_parse, p_state, err);115return err;116}117118Error GLTFDocumentExtension::import_pre_generate(Ref<GLTFState> p_state) {119ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);120Error err = OK;121GDVIRTUAL_CALL(_import_pre_generate, p_state, err);122return err;123}124125Node3D *GLTFDocumentExtension::generate_scene_node(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Node *p_scene_parent) {126ERR_FAIL_COND_V(p_state.is_null(), nullptr);127ERR_FAIL_COND_V(p_gltf_node.is_null(), nullptr);128Node3D *ret_node = nullptr;129GDVIRTUAL_CALL(_generate_scene_node, p_state, p_gltf_node, p_scene_parent, ret_node);130return ret_node;131}132133Error GLTFDocumentExtension::import_node(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Dictionary &r_dict, Node *p_node) {134ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);135ERR_FAIL_COND_V(p_gltf_node.is_null(), ERR_INVALID_PARAMETER);136ERR_FAIL_NULL_V(p_node, ERR_INVALID_PARAMETER);137Error err = OK;138GDVIRTUAL_CALL(_import_node, p_state, p_gltf_node, r_dict, p_node, err);139return err;140}141142Error GLTFDocumentExtension::import_post(Ref<GLTFState> p_state, Node *p_root) {143ERR_FAIL_NULL_V(p_root, ERR_INVALID_PARAMETER);144ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);145Error err = OK;146GDVIRTUAL_CALL(_import_post, p_state, p_root, err);147return err;148}149150// Export process.151Error GLTFDocumentExtension::export_preflight(Ref<GLTFState> p_state, Node *p_root) {152ERR_FAIL_NULL_V(p_root, ERR_INVALID_PARAMETER);153Error err = OK;154GDVIRTUAL_CALL(_export_preflight, p_state, p_root, err);155return err;156}157158void GLTFDocumentExtension::convert_scene_node(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Node *p_scene_node) {159ERR_FAIL_COND(p_state.is_null());160ERR_FAIL_COND(p_gltf_node.is_null());161ERR_FAIL_NULL(p_scene_node);162GDVIRTUAL_CALL(_convert_scene_node, p_state, p_gltf_node, p_scene_node);163}164165Error GLTFDocumentExtension::export_post_convert(Ref<GLTFState> p_state, Node *p_root) {166ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);167ERR_FAIL_NULL_V(p_root, ERR_INVALID_PARAMETER);168Error err = OK;169GDVIRTUAL_CALL(_export_post_convert, p_state, p_root, err);170return err;171}172173Error GLTFDocumentExtension::export_preserialize(Ref<GLTFState> p_state) {174ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);175Error err = OK;176GDVIRTUAL_CALL(_export_preserialize, p_state, err);177return err;178}179180Ref<GLTFObjectModelProperty> GLTFDocumentExtension::export_object_model_property(Ref<GLTFState> p_state, const NodePath &p_node_path, const Node *p_godot_node, GLTFNodeIndex p_gltf_node_index, const Object *p_target_object, int p_target_depth) {181Ref<GLTFObjectModelProperty> ret;182ERR_FAIL_COND_V(p_state.is_null(), ret);183ERR_FAIL_NULL_V(p_godot_node, ret);184ERR_FAIL_NULL_V(p_target_object, ret);185GDVIRTUAL_CALL(_export_object_model_property, p_state, p_node_path, p_godot_node, p_gltf_node_index, p_target_object, p_target_depth, ret);186return ret;187}188189Vector<String> GLTFDocumentExtension::get_saveable_image_formats() {190Vector<String> ret;191GDVIRTUAL_CALL(_get_saveable_image_formats, ret);192return ret;193}194195PackedByteArray GLTFDocumentExtension::serialize_image_to_bytes(Ref<GLTFState> p_state, Ref<Image> p_image, Dictionary p_image_dict, const String &p_image_format, float p_lossy_quality) {196PackedByteArray ret;197ERR_FAIL_COND_V(p_state.is_null(), ret);198ERR_FAIL_COND_V(p_image.is_null(), ret);199GDVIRTUAL_CALL(_serialize_image_to_bytes, p_state, p_image, p_image_dict, p_image_format, p_lossy_quality, ret);200return ret;201}202203Error GLTFDocumentExtension::save_image_at_path(Ref<GLTFState> p_state, Ref<Image> p_image, const String &p_file_path, const String &p_image_format, float p_lossy_quality) {204ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);205ERR_FAIL_COND_V(p_image.is_null(), ERR_INVALID_PARAMETER);206Error ret = OK;207GDVIRTUAL_CALL(_save_image_at_path, p_state, p_image, p_file_path, p_image_format, p_lossy_quality, ret);208return ret;209}210211Error GLTFDocumentExtension::serialize_texture_json(Ref<GLTFState> p_state, Dictionary p_texture_json, Ref<GLTFTexture> p_gltf_texture, const String &p_image_format) {212ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);213ERR_FAIL_COND_V(p_gltf_texture.is_null(), ERR_INVALID_PARAMETER);214Error err = OK;215GDVIRTUAL_CALL(_serialize_texture_json, p_state, p_texture_json, p_gltf_texture, p_image_format, err);216return err;217}218219Error GLTFDocumentExtension::export_node(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Dictionary &r_dict, Node *p_node) {220ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);221ERR_FAIL_COND_V(p_gltf_node.is_null(), ERR_INVALID_PARAMETER);222Error err = OK;223GDVIRTUAL_CALL(_export_node, p_state, p_gltf_node, r_dict, p_node, err);224return err;225}226227Error GLTFDocumentExtension::export_post(Ref<GLTFState> p_state) {228ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);229Error err = OK;230GDVIRTUAL_CALL(_export_post, p_state, err);231return err;232}233234235