initial commit, 4.5 stable
Some checks failed
🔗 GHA / 📊 Static checks (push) Has been cancelled
🔗 GHA / 🤖 Android (push) Has been cancelled
🔗 GHA / 🍏 iOS (push) Has been cancelled
🔗 GHA / 🐧 Linux (push) Has been cancelled
🔗 GHA / 🍎 macOS (push) Has been cancelled
🔗 GHA / 🏁 Windows (push) Has been cancelled
🔗 GHA / 🌐 Web (push) Has been cancelled
Some checks failed
🔗 GHA / 📊 Static checks (push) Has been cancelled
🔗 GHA / 🤖 Android (push) Has been cancelled
🔗 GHA / 🍏 iOS (push) Has been cancelled
🔗 GHA / 🐧 Linux (push) Has been cancelled
🔗 GHA / 🍎 macOS (push) Has been cancelled
🔗 GHA / 🏁 Windows (push) Has been cancelled
🔗 GHA / 🌐 Web (push) Has been cancelled
This commit is contained in:
13
modules/gltf/extensions/SCsub
Normal file
13
modules/gltf/extensions/SCsub
Normal file
@@ -0,0 +1,13 @@
|
||||
#!/usr/bin/env python
|
||||
from misc.utility.scons_hints import *
|
||||
|
||||
Import("env")
|
||||
Import("env_modules")
|
||||
|
||||
env_gltf = env_modules.Clone()
|
||||
|
||||
# Godot source files
|
||||
|
||||
env_gltf.add_source_files(env.modules_sources, "*.cpp")
|
||||
if not env["disable_physics_3d"]:
|
||||
env_gltf.add_source_files(env.modules_sources, "physics/*.cpp")
|
233
modules/gltf/extensions/gltf_document_extension.cpp
Normal file
233
modules/gltf/extensions/gltf_document_extension.cpp
Normal file
@@ -0,0 +1,233 @@
|
||||
/**************************************************************************/
|
||||
/* gltf_document_extension.cpp */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#include "gltf_document_extension.h"
|
||||
|
||||
void GLTFDocumentExtension::_bind_methods() {
|
||||
// Import process.
|
||||
GDVIRTUAL_BIND(_import_preflight, "state", "extensions");
|
||||
GDVIRTUAL_BIND(_get_supported_extensions);
|
||||
GDVIRTUAL_BIND(_parse_node_extensions, "state", "gltf_node", "extensions");
|
||||
GDVIRTUAL_BIND(_parse_image_data, "state", "image_data", "mime_type", "ret_image");
|
||||
GDVIRTUAL_BIND(_get_image_file_extension);
|
||||
GDVIRTUAL_BIND(_parse_texture_json, "state", "texture_json", "ret_gltf_texture");
|
||||
GDVIRTUAL_BIND(_import_object_model_property, "state", "split_json_pointer", "partial_paths");
|
||||
GDVIRTUAL_BIND(_import_post_parse, "state");
|
||||
GDVIRTUAL_BIND(_import_pre_generate, "state");
|
||||
GDVIRTUAL_BIND(_generate_scene_node, "state", "gltf_node", "scene_parent");
|
||||
GDVIRTUAL_BIND(_import_node, "state", "gltf_node", "json", "node");
|
||||
GDVIRTUAL_BIND(_import_post, "state", "root");
|
||||
// Export process.
|
||||
GDVIRTUAL_BIND(_export_preflight, "state", "root");
|
||||
GDVIRTUAL_BIND(_convert_scene_node, "state", "gltf_node", "scene_node");
|
||||
GDVIRTUAL_BIND(_export_post_convert, "state", "root");
|
||||
GDVIRTUAL_BIND(_export_preserialize, "state");
|
||||
GDVIRTUAL_BIND(_export_object_model_property, "state", "node_path", "godot_node", "gltf_node_index", "target_object", "target_depth");
|
||||
GDVIRTUAL_BIND(_get_saveable_image_formats);
|
||||
GDVIRTUAL_BIND(_serialize_image_to_bytes, "state", "image", "image_dict", "image_format", "lossy_quality");
|
||||
GDVIRTUAL_BIND(_save_image_at_path, "state", "image", "file_path", "image_format", "lossy_quality");
|
||||
GDVIRTUAL_BIND(_serialize_texture_json, "state", "texture_json", "gltf_texture", "image_format");
|
||||
GDVIRTUAL_BIND(_export_node, "state", "gltf_node", "json", "node");
|
||||
GDVIRTUAL_BIND(_export_post, "state");
|
||||
}
|
||||
|
||||
// Import process.
|
||||
Error GLTFDocumentExtension::import_preflight(Ref<GLTFState> p_state, Vector<String> p_extensions) {
|
||||
ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);
|
||||
Error err = OK;
|
||||
GDVIRTUAL_CALL(_import_preflight, p_state, p_extensions, err);
|
||||
return err;
|
||||
}
|
||||
|
||||
Vector<String> GLTFDocumentExtension::get_supported_extensions() {
|
||||
Vector<String> ret;
|
||||
GDVIRTUAL_CALL(_get_supported_extensions, ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
Error GLTFDocumentExtension::parse_node_extensions(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Dictionary &p_extensions) {
|
||||
ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);
|
||||
ERR_FAIL_COND_V(p_gltf_node.is_null(), ERR_INVALID_PARAMETER);
|
||||
Error err = OK;
|
||||
GDVIRTUAL_CALL(_parse_node_extensions, p_state, p_gltf_node, p_extensions, err);
|
||||
return err;
|
||||
}
|
||||
|
||||
Error GLTFDocumentExtension::parse_image_data(Ref<GLTFState> p_state, const PackedByteArray &p_image_data, const String &p_mime_type, Ref<Image> r_image) {
|
||||
ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);
|
||||
ERR_FAIL_COND_V(r_image.is_null(), ERR_INVALID_PARAMETER);
|
||||
Error err = OK;
|
||||
GDVIRTUAL_CALL(_parse_image_data, p_state, p_image_data, p_mime_type, r_image, err);
|
||||
return err;
|
||||
}
|
||||
|
||||
String GLTFDocumentExtension::get_image_file_extension() {
|
||||
String ret;
|
||||
GDVIRTUAL_CALL(_get_image_file_extension, ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
Error GLTFDocumentExtension::parse_texture_json(Ref<GLTFState> p_state, const Dictionary &p_texture_json, Ref<GLTFTexture> r_gltf_texture) {
|
||||
ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);
|
||||
ERR_FAIL_COND_V(r_gltf_texture.is_null(), ERR_INVALID_PARAMETER);
|
||||
Error err = OK;
|
||||
GDVIRTUAL_CALL(_parse_texture_json, p_state, p_texture_json, r_gltf_texture, err);
|
||||
return err;
|
||||
}
|
||||
|
||||
Ref<GLTFObjectModelProperty> GLTFDocumentExtension::import_object_model_property(Ref<GLTFState> p_state, const PackedStringArray &p_split_json_pointer, const TypedArray<NodePath> &p_partial_paths) {
|
||||
Ref<GLTFObjectModelProperty> ret;
|
||||
ERR_FAIL_COND_V(p_state.is_null(), ret);
|
||||
GDVIRTUAL_CALL(_import_object_model_property, p_state, p_split_json_pointer, p_partial_paths, ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
Error GLTFDocumentExtension::import_post_parse(Ref<GLTFState> p_state) {
|
||||
ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);
|
||||
Error err = OK;
|
||||
GDVIRTUAL_CALL(_import_post_parse, p_state, err);
|
||||
return err;
|
||||
}
|
||||
|
||||
Error GLTFDocumentExtension::import_pre_generate(Ref<GLTFState> p_state) {
|
||||
ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);
|
||||
Error err = OK;
|
||||
GDVIRTUAL_CALL(_import_pre_generate, p_state, err);
|
||||
return err;
|
||||
}
|
||||
|
||||
Node3D *GLTFDocumentExtension::generate_scene_node(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Node *p_scene_parent) {
|
||||
ERR_FAIL_COND_V(p_state.is_null(), nullptr);
|
||||
ERR_FAIL_COND_V(p_gltf_node.is_null(), nullptr);
|
||||
Node3D *ret_node = nullptr;
|
||||
GDVIRTUAL_CALL(_generate_scene_node, p_state, p_gltf_node, p_scene_parent, ret_node);
|
||||
return ret_node;
|
||||
}
|
||||
|
||||
Error GLTFDocumentExtension::import_node(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Dictionary &r_dict, Node *p_node) {
|
||||
ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);
|
||||
ERR_FAIL_COND_V(p_gltf_node.is_null(), ERR_INVALID_PARAMETER);
|
||||
ERR_FAIL_NULL_V(p_node, ERR_INVALID_PARAMETER);
|
||||
Error err = OK;
|
||||
GDVIRTUAL_CALL(_import_node, p_state, p_gltf_node, r_dict, p_node, err);
|
||||
return err;
|
||||
}
|
||||
|
||||
Error GLTFDocumentExtension::import_post(Ref<GLTFState> p_state, Node *p_root) {
|
||||
ERR_FAIL_NULL_V(p_root, ERR_INVALID_PARAMETER);
|
||||
ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);
|
||||
Error err = OK;
|
||||
GDVIRTUAL_CALL(_import_post, p_state, p_root, err);
|
||||
return err;
|
||||
}
|
||||
|
||||
// Export process.
|
||||
Error GLTFDocumentExtension::export_preflight(Ref<GLTFState> p_state, Node *p_root) {
|
||||
ERR_FAIL_NULL_V(p_root, ERR_INVALID_PARAMETER);
|
||||
Error err = OK;
|
||||
GDVIRTUAL_CALL(_export_preflight, p_state, p_root, err);
|
||||
return err;
|
||||
}
|
||||
|
||||
void GLTFDocumentExtension::convert_scene_node(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Node *p_scene_node) {
|
||||
ERR_FAIL_COND(p_state.is_null());
|
||||
ERR_FAIL_COND(p_gltf_node.is_null());
|
||||
ERR_FAIL_NULL(p_scene_node);
|
||||
GDVIRTUAL_CALL(_convert_scene_node, p_state, p_gltf_node, p_scene_node);
|
||||
}
|
||||
|
||||
Error GLTFDocumentExtension::export_post_convert(Ref<GLTFState> p_state, Node *p_root) {
|
||||
ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);
|
||||
ERR_FAIL_NULL_V(p_root, ERR_INVALID_PARAMETER);
|
||||
Error err = OK;
|
||||
GDVIRTUAL_CALL(_export_post_convert, p_state, p_root, err);
|
||||
return err;
|
||||
}
|
||||
|
||||
Error GLTFDocumentExtension::export_preserialize(Ref<GLTFState> p_state) {
|
||||
ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);
|
||||
Error err = OK;
|
||||
GDVIRTUAL_CALL(_export_preserialize, p_state, err);
|
||||
return err;
|
||||
}
|
||||
|
||||
Ref<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) {
|
||||
Ref<GLTFObjectModelProperty> ret;
|
||||
ERR_FAIL_COND_V(p_state.is_null(), ret);
|
||||
ERR_FAIL_NULL_V(p_godot_node, ret);
|
||||
ERR_FAIL_NULL_V(p_target_object, ret);
|
||||
GDVIRTUAL_CALL(_export_object_model_property, p_state, p_node_path, p_godot_node, p_gltf_node_index, p_target_object, p_target_depth, ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
Vector<String> GLTFDocumentExtension::get_saveable_image_formats() {
|
||||
Vector<String> ret;
|
||||
GDVIRTUAL_CALL(_get_saveable_image_formats, ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
PackedByteArray 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) {
|
||||
PackedByteArray ret;
|
||||
ERR_FAIL_COND_V(p_state.is_null(), ret);
|
||||
ERR_FAIL_COND_V(p_image.is_null(), ret);
|
||||
GDVIRTUAL_CALL(_serialize_image_to_bytes, p_state, p_image, p_image_dict, p_image_format, p_lossy_quality, ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
Error 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) {
|
||||
ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);
|
||||
ERR_FAIL_COND_V(p_image.is_null(), ERR_INVALID_PARAMETER);
|
||||
Error ret = OK;
|
||||
GDVIRTUAL_CALL(_save_image_at_path, p_state, p_image, p_file_path, p_image_format, p_lossy_quality, ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
Error GLTFDocumentExtension::serialize_texture_json(Ref<GLTFState> p_state, Dictionary p_texture_json, Ref<GLTFTexture> p_gltf_texture, const String &p_image_format) {
|
||||
ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);
|
||||
ERR_FAIL_COND_V(p_gltf_texture.is_null(), ERR_INVALID_PARAMETER);
|
||||
Error err = OK;
|
||||
GDVIRTUAL_CALL(_serialize_texture_json, p_state, p_texture_json, p_gltf_texture, p_image_format, err);
|
||||
return err;
|
||||
}
|
||||
|
||||
Error GLTFDocumentExtension::export_node(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Dictionary &r_dict, Node *p_node) {
|
||||
ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);
|
||||
ERR_FAIL_COND_V(p_gltf_node.is_null(), ERR_INVALID_PARAMETER);
|
||||
Error err = OK;
|
||||
GDVIRTUAL_CALL(_export_node, p_state, p_gltf_node, r_dict, p_node, err);
|
||||
return err;
|
||||
}
|
||||
|
||||
Error GLTFDocumentExtension::export_post(Ref<GLTFState> p_state) {
|
||||
ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);
|
||||
Error err = OK;
|
||||
GDVIRTUAL_CALL(_export_post, p_state, err);
|
||||
return err;
|
||||
}
|
95
modules/gltf/extensions/gltf_document_extension.h
Normal file
95
modules/gltf/extensions/gltf_document_extension.h
Normal file
@@ -0,0 +1,95 @@
|
||||
/**************************************************************************/
|
||||
/* gltf_document_extension.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../gltf_state.h"
|
||||
|
||||
#include "scene/3d/node_3d.h"
|
||||
|
||||
class GLTFDocumentExtension : public Resource {
|
||||
GDCLASS(GLTFDocumentExtension, Resource);
|
||||
|
||||
protected:
|
||||
static void _bind_methods();
|
||||
|
||||
public:
|
||||
// Import process.
|
||||
virtual Error import_preflight(Ref<GLTFState> p_state, Vector<String> p_extensions);
|
||||
virtual Vector<String> get_supported_extensions();
|
||||
virtual Error parse_node_extensions(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Dictionary &p_extensions);
|
||||
virtual Error parse_image_data(Ref<GLTFState> p_state, const PackedByteArray &p_image_data, const String &p_mime_type, Ref<Image> r_image);
|
||||
virtual String get_image_file_extension();
|
||||
virtual Error parse_texture_json(Ref<GLTFState> p_state, const Dictionary &p_texture_json, Ref<GLTFTexture> r_gltf_texture);
|
||||
virtual Ref<GLTFObjectModelProperty> import_object_model_property(Ref<GLTFState> p_state, const PackedStringArray &p_split_json_pointer, const TypedArray<NodePath> &p_partial_paths);
|
||||
virtual Error import_post_parse(Ref<GLTFState> p_state);
|
||||
virtual Error import_pre_generate(Ref<GLTFState> p_state);
|
||||
virtual Node3D *generate_scene_node(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Node *p_scene_parent);
|
||||
virtual Error import_node(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Dictionary &r_json, Node *p_node);
|
||||
virtual Error import_post(Ref<GLTFState> p_state, Node *p_node);
|
||||
// Export process.
|
||||
virtual Error export_preflight(Ref<GLTFState> p_state, Node *p_root);
|
||||
virtual void convert_scene_node(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Node *p_scene_node);
|
||||
virtual Error export_post_convert(Ref<GLTFState> p_state, Node *p_root);
|
||||
virtual Error export_preserialize(Ref<GLTFState> p_state);
|
||||
virtual Ref<GLTFObjectModelProperty> 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);
|
||||
virtual Vector<String> get_saveable_image_formats();
|
||||
virtual PackedByteArray 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);
|
||||
virtual Error 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);
|
||||
virtual Error serialize_texture_json(Ref<GLTFState> p_state, Dictionary p_texture_json, Ref<GLTFTexture> p_gltf_texture, const String &p_image_format);
|
||||
virtual Error export_node(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Dictionary &r_json, Node *p_node);
|
||||
virtual Error export_post(Ref<GLTFState> p_state);
|
||||
|
||||
// Import process.
|
||||
GDVIRTUAL2R(Error, _import_preflight, Ref<GLTFState>, Vector<String>);
|
||||
GDVIRTUAL0R(Vector<String>, _get_supported_extensions);
|
||||
GDVIRTUAL3R(Error, _parse_node_extensions, Ref<GLTFState>, Ref<GLTFNode>, Dictionary);
|
||||
GDVIRTUAL4R(Error, _parse_image_data, Ref<GLTFState>, PackedByteArray, String, Ref<Image>);
|
||||
GDVIRTUAL0R(String, _get_image_file_extension);
|
||||
GDVIRTUAL3R(Error, _parse_texture_json, Ref<GLTFState>, Dictionary, Ref<GLTFTexture>);
|
||||
GDVIRTUAL3R(Ref<GLTFObjectModelProperty>, _import_object_model_property, Ref<GLTFState>, PackedStringArray, TypedArray<NodePath>);
|
||||
GDVIRTUAL1R(Error, _import_post_parse, Ref<GLTFState>);
|
||||
GDVIRTUAL1R(Error, _import_pre_generate, Ref<GLTFState>);
|
||||
GDVIRTUAL3R(Node3D *, _generate_scene_node, Ref<GLTFState>, Ref<GLTFNode>, Node *);
|
||||
GDVIRTUAL4R(Error, _import_node, Ref<GLTFState>, Ref<GLTFNode>, Dictionary, Node *);
|
||||
GDVIRTUAL2R(Error, _import_post, Ref<GLTFState>, Node *);
|
||||
// Export process.
|
||||
GDVIRTUAL2R(Error, _export_preflight, Ref<GLTFState>, Node *);
|
||||
GDVIRTUAL3(_convert_scene_node, Ref<GLTFState>, Ref<GLTFNode>, Node *);
|
||||
GDVIRTUAL2R(Error, _export_post_convert, Ref<GLTFState>, Node *);
|
||||
GDVIRTUAL1R(Error, _export_preserialize, Ref<GLTFState>);
|
||||
GDVIRTUAL6R(Ref<GLTFObjectModelProperty>, _export_object_model_property, Ref<GLTFState>, NodePath, const Node *, GLTFNodeIndex, const Object *, int);
|
||||
GDVIRTUAL0R(Vector<String>, _get_saveable_image_formats);
|
||||
GDVIRTUAL5R(PackedByteArray, _serialize_image_to_bytes, Ref<GLTFState>, Ref<Image>, Dictionary, String, float);
|
||||
GDVIRTUAL5R(Error, _save_image_at_path, Ref<GLTFState>, Ref<Image>, String, String, float);
|
||||
GDVIRTUAL4R(Error, _serialize_texture_json, Ref<GLTFState>, Dictionary, Ref<GLTFTexture>, String);
|
||||
GDVIRTUAL4R(Error, _export_node, Ref<GLTFState>, Ref<GLTFNode>, Dictionary, Node *);
|
||||
GDVIRTUAL1R(Error, _export_post, Ref<GLTFState>);
|
||||
};
|
@@ -0,0 +1,96 @@
|
||||
/**************************************************************************/
|
||||
/* gltf_document_extension_convert_importer_mesh.cpp */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#include "gltf_document_extension_convert_importer_mesh.h"
|
||||
|
||||
#include "scene/3d/importer_mesh_instance_3d.h"
|
||||
#include "scene/3d/mesh_instance_3d.h"
|
||||
#include "scene/resources/3d/importer_mesh.h"
|
||||
|
||||
void GLTFDocumentExtensionConvertImporterMesh::_copy_meta(Object *p_src_object, Object *p_dst_object) {
|
||||
List<StringName> meta_list;
|
||||
p_src_object->get_meta_list(&meta_list);
|
||||
for (const StringName &meta_key : meta_list) {
|
||||
Variant meta_value = p_src_object->get_meta(meta_key);
|
||||
p_dst_object->set_meta(meta_key, meta_value);
|
||||
}
|
||||
}
|
||||
|
||||
MeshInstance3D *GLTFDocumentExtensionConvertImporterMesh::convert_importer_mesh_instance_3d(ImporterMeshInstance3D *p_importer_mesh_instance_3d) {
|
||||
// Convert the node itself first.
|
||||
MeshInstance3D *mesh_instance_node_3d = memnew(MeshInstance3D);
|
||||
ERR_FAIL_NULL_V(p_importer_mesh_instance_3d, mesh_instance_node_3d);
|
||||
mesh_instance_node_3d->set_name(p_importer_mesh_instance_3d->get_name());
|
||||
mesh_instance_node_3d->set_transform(p_importer_mesh_instance_3d->get_transform());
|
||||
mesh_instance_node_3d->set_skin(p_importer_mesh_instance_3d->get_skin());
|
||||
mesh_instance_node_3d->set_skeleton_path(p_importer_mesh_instance_3d->get_skeleton_path());
|
||||
mesh_instance_node_3d->set_visible(p_importer_mesh_instance_3d->is_visible());
|
||||
p_importer_mesh_instance_3d->replace_by(mesh_instance_node_3d);
|
||||
_copy_meta(p_importer_mesh_instance_3d, mesh_instance_node_3d);
|
||||
// Convert the mesh data in the mesh resource.
|
||||
Ref<ImporterMesh> importer_mesh = p_importer_mesh_instance_3d->get_mesh();
|
||||
if (importer_mesh.is_valid()) {
|
||||
Ref<ArrayMesh> array_mesh = importer_mesh->get_mesh();
|
||||
mesh_instance_node_3d->set_mesh(array_mesh);
|
||||
_copy_meta(importer_mesh.ptr(), array_mesh.ptr());
|
||||
} else {
|
||||
WARN_PRINT("glTF: ImporterMeshInstance3D does not have a valid mesh. This should not happen. Continuing anyway.");
|
||||
}
|
||||
return mesh_instance_node_3d;
|
||||
}
|
||||
|
||||
Error GLTFDocumentExtensionConvertImporterMesh::import_post(Ref<GLTFState> p_state, Node *p_root) {
|
||||
ERR_FAIL_NULL_V(p_root, ERR_INVALID_PARAMETER);
|
||||
ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);
|
||||
List<Node *> queue;
|
||||
queue.push_back(p_root);
|
||||
List<Node *> delete_queue;
|
||||
while (!queue.is_empty()) {
|
||||
List<Node *>::Element *E = queue.front();
|
||||
Node *node = E->get();
|
||||
ImporterMeshInstance3D *importer_mesh_3d = Object::cast_to<ImporterMeshInstance3D>(node);
|
||||
if (importer_mesh_3d) {
|
||||
delete_queue.push_back(importer_mesh_3d);
|
||||
node = convert_importer_mesh_instance_3d(importer_mesh_3d);
|
||||
}
|
||||
int child_count = node->get_child_count();
|
||||
for (int i = 0; i < child_count; i++) {
|
||||
queue.push_back(node->get_child(i));
|
||||
}
|
||||
queue.pop_front();
|
||||
}
|
||||
while (!delete_queue.is_empty()) {
|
||||
List<Node *>::Element *E = delete_queue.front();
|
||||
Node *node = E->get();
|
||||
memdelete(node);
|
||||
delete_queue.pop_front();
|
||||
}
|
||||
return OK;
|
||||
}
|
@@ -0,0 +1,46 @@
|
||||
/**************************************************************************/
|
||||
/* gltf_document_extension_convert_importer_mesh.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "gltf_document_extension.h"
|
||||
|
||||
class MeshInstance3D;
|
||||
|
||||
class GLTFDocumentExtensionConvertImporterMesh : public GLTFDocumentExtension {
|
||||
GDCLASS(GLTFDocumentExtensionConvertImporterMesh, GLTFDocumentExtension);
|
||||
|
||||
protected:
|
||||
static void _copy_meta(Object *p_src_object, Object *p_dst_object);
|
||||
|
||||
public:
|
||||
static MeshInstance3D *convert_importer_mesh_instance_3d(ImporterMeshInstance3D *p_importer_mesh_instance_3d);
|
||||
Error import_post(Ref<GLTFState> p_state, Node *p_root) override;
|
||||
};
|
@@ -0,0 +1,66 @@
|
||||
/**************************************************************************/
|
||||
/* gltf_document_extension_texture_ktx.cpp */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#include "gltf_document_extension_texture_ktx.h"
|
||||
|
||||
// Import process.
|
||||
Error GLTFDocumentExtensionTextureKTX::import_preflight(Ref<GLTFState> p_state, Vector<String> p_extensions) {
|
||||
if (!p_extensions.has("KHR_texture_basisu")) {
|
||||
return ERR_SKIP;
|
||||
}
|
||||
return OK;
|
||||
}
|
||||
|
||||
Vector<String> GLTFDocumentExtensionTextureKTX::get_supported_extensions() {
|
||||
Vector<String> ret;
|
||||
ret.push_back("KHR_texture_basisu");
|
||||
return ret;
|
||||
}
|
||||
|
||||
Error GLTFDocumentExtensionTextureKTX::parse_image_data(Ref<GLTFState> p_state, const PackedByteArray &p_image_data, const String &p_mime_type, Ref<Image> r_image) {
|
||||
if (p_mime_type == "image/ktx2") {
|
||||
return r_image->load_ktx_from_buffer(p_image_data);
|
||||
}
|
||||
return OK;
|
||||
}
|
||||
|
||||
Error GLTFDocumentExtensionTextureKTX::parse_texture_json(Ref<GLTFState> p_state, const Dictionary &p_texture_json, Ref<GLTFTexture> r_gltf_texture) {
|
||||
if (!p_texture_json.has("extensions")) {
|
||||
return OK;
|
||||
}
|
||||
const Dictionary &extensions = p_texture_json["extensions"];
|
||||
if (!extensions.has("KHR_texture_basisu")) {
|
||||
return OK;
|
||||
}
|
||||
const Dictionary &texture_ktx = extensions["KHR_texture_basisu"];
|
||||
ERR_FAIL_COND_V(!texture_ktx.has("source"), ERR_PARSE_ERROR);
|
||||
r_gltf_texture->set_src_image(texture_ktx["source"]);
|
||||
return OK;
|
||||
}
|
@@ -0,0 +1,44 @@
|
||||
/**************************************************************************/
|
||||
/* gltf_document_extension_texture_ktx.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "gltf_document_extension.h"
|
||||
|
||||
class GLTFDocumentExtensionTextureKTX : public GLTFDocumentExtension {
|
||||
GDCLASS(GLTFDocumentExtensionTextureKTX, GLTFDocumentExtension);
|
||||
|
||||
public:
|
||||
// Import process.
|
||||
Error import_preflight(Ref<GLTFState> p_state, Vector<String> p_extensions) override;
|
||||
Vector<String> get_supported_extensions() override;
|
||||
Error parse_image_data(Ref<GLTFState> p_state, const PackedByteArray &p_image_data, const String &p_mime_type, Ref<Image> r_image) override;
|
||||
Error parse_texture_json(Ref<GLTFState> p_state, const Dictionary &p_texture_json, Ref<GLTFTexture> r_gltf_texture) override;
|
||||
};
|
109
modules/gltf/extensions/gltf_document_extension_texture_webp.cpp
Normal file
109
modules/gltf/extensions/gltf_document_extension_texture_webp.cpp
Normal file
@@ -0,0 +1,109 @@
|
||||
/**************************************************************************/
|
||||
/* gltf_document_extension_texture_webp.cpp */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#include "gltf_document_extension_texture_webp.h"
|
||||
|
||||
// Import process.
|
||||
Error GLTFDocumentExtensionTextureWebP::import_preflight(Ref<GLTFState> p_state, Vector<String> p_extensions) {
|
||||
if (!p_extensions.has("EXT_texture_webp")) {
|
||||
return ERR_SKIP;
|
||||
}
|
||||
return OK;
|
||||
}
|
||||
|
||||
Vector<String> GLTFDocumentExtensionTextureWebP::get_supported_extensions() {
|
||||
Vector<String> ret;
|
||||
ret.push_back("EXT_texture_webp");
|
||||
return ret;
|
||||
}
|
||||
|
||||
Error GLTFDocumentExtensionTextureWebP::parse_image_data(Ref<GLTFState> p_state, const PackedByteArray &p_image_data, const String &p_mime_type, Ref<Image> r_image) {
|
||||
if (p_mime_type == "image/webp") {
|
||||
return r_image->load_webp_from_buffer(p_image_data);
|
||||
}
|
||||
return OK;
|
||||
}
|
||||
|
||||
String GLTFDocumentExtensionTextureWebP::get_image_file_extension() {
|
||||
return ".webp";
|
||||
}
|
||||
|
||||
Error GLTFDocumentExtensionTextureWebP::parse_texture_json(Ref<GLTFState> p_state, const Dictionary &p_texture_json, Ref<GLTFTexture> r_gltf_texture) {
|
||||
if (!p_texture_json.has("extensions")) {
|
||||
return OK;
|
||||
}
|
||||
const Dictionary &extensions = p_texture_json["extensions"];
|
||||
if (!extensions.has("EXT_texture_webp")) {
|
||||
return OK;
|
||||
}
|
||||
const Dictionary &texture_webp = extensions["EXT_texture_webp"];
|
||||
ERR_FAIL_COND_V(!texture_webp.has("source"), ERR_PARSE_ERROR);
|
||||
r_gltf_texture->set_src_image(texture_webp["source"]);
|
||||
return OK;
|
||||
}
|
||||
|
||||
Vector<String> GLTFDocumentExtensionTextureWebP::get_saveable_image_formats() {
|
||||
Vector<String> ret;
|
||||
ret.push_back("Lossless WebP");
|
||||
ret.push_back("Lossy WebP");
|
||||
return ret;
|
||||
}
|
||||
|
||||
PackedByteArray GLTFDocumentExtensionTextureWebP::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) {
|
||||
if (p_image_format == "Lossless WebP") {
|
||||
p_image_dict["mimeType"] = "image/webp";
|
||||
return p_image->save_webp_to_buffer(false);
|
||||
} else if (p_image_format == "Lossy WebP") {
|
||||
p_image_dict["mimeType"] = "image/webp";
|
||||
return p_image->save_webp_to_buffer(true, p_lossy_quality);
|
||||
}
|
||||
ERR_FAIL_V(PackedByteArray());
|
||||
}
|
||||
|
||||
Error GLTFDocumentExtensionTextureWebP::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) {
|
||||
if (p_image_format == "Lossless WebP") {
|
||||
p_image->save_webp(p_file_path, false);
|
||||
return OK;
|
||||
} else if (p_image_format == "Lossy WebP") {
|
||||
p_image->save_webp(p_file_path, true, p_lossy_quality);
|
||||
return OK;
|
||||
}
|
||||
return ERR_INVALID_PARAMETER;
|
||||
}
|
||||
|
||||
Error GLTFDocumentExtensionTextureWebP::serialize_texture_json(Ref<GLTFState> p_state, Dictionary p_texture_json, Ref<GLTFTexture> p_gltf_texture, const String &p_image_format) {
|
||||
Dictionary ext_texture_webp;
|
||||
ext_texture_webp["source"] = p_gltf_texture->get_src_image();
|
||||
Dictionary texture_extensions;
|
||||
texture_extensions["EXT_texture_webp"] = ext_texture_webp;
|
||||
p_texture_json["extensions"] = texture_extensions;
|
||||
p_state->add_used_extension("EXT_texture_webp", true);
|
||||
return OK;
|
||||
}
|
@@ -0,0 +1,50 @@
|
||||
/**************************************************************************/
|
||||
/* gltf_document_extension_texture_webp.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "gltf_document_extension.h"
|
||||
|
||||
class GLTFDocumentExtensionTextureWebP : public GLTFDocumentExtension {
|
||||
GDCLASS(GLTFDocumentExtensionTextureWebP, GLTFDocumentExtension);
|
||||
|
||||
public:
|
||||
// Import process.
|
||||
Error import_preflight(Ref<GLTFState> p_state, Vector<String> p_extensions) override;
|
||||
Vector<String> get_supported_extensions() override;
|
||||
Error parse_image_data(Ref<GLTFState> p_state, const PackedByteArray &p_image_data, const String &p_mime_type, Ref<Image> r_image) override;
|
||||
String get_image_file_extension() override;
|
||||
Error parse_texture_json(Ref<GLTFState> p_state, const Dictionary &p_texture_json, Ref<GLTFTexture> r_gltf_texture) override;
|
||||
// Export process.
|
||||
Vector<String> get_saveable_image_formats() override;
|
||||
PackedByteArray 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) override;
|
||||
Error save_image_at_path(Ref<GLTFState> p_state, Ref<Image> p_image, const String &p_full_path, const String &p_image_format, float p_lossy_quality) override;
|
||||
Error serialize_texture_json(Ref<GLTFState> p_state, Dictionary p_texture_json, Ref<GLTFTexture> p_gltf_texture, const String &p_image_format) override;
|
||||
};
|
255
modules/gltf/extensions/gltf_light.cpp
Normal file
255
modules/gltf/extensions/gltf_light.cpp
Normal file
@@ -0,0 +1,255 @@
|
||||
/**************************************************************************/
|
||||
/* gltf_light.cpp */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#include "gltf_light.h"
|
||||
|
||||
#include "../structures/gltf_object_model_property.h"
|
||||
#include "scene/3d/light_3d.h"
|
||||
|
||||
void GLTFLight::_bind_methods() {
|
||||
ClassDB::bind_static_method("GLTFLight", D_METHOD("from_node", "light_node"), &GLTFLight::from_node);
|
||||
ClassDB::bind_method(D_METHOD("to_node"), &GLTFLight::to_node);
|
||||
|
||||
ClassDB::bind_static_method("GLTFLight", D_METHOD("from_dictionary", "dictionary"), &GLTFLight::from_dictionary);
|
||||
ClassDB::bind_method(D_METHOD("to_dictionary"), &GLTFLight::to_dictionary);
|
||||
|
||||
ClassDB::bind_method(D_METHOD("get_color"), &GLTFLight::get_color);
|
||||
ClassDB::bind_method(D_METHOD("set_color", "color"), &GLTFLight::set_color);
|
||||
ClassDB::bind_method(D_METHOD("get_intensity"), &GLTFLight::get_intensity);
|
||||
ClassDB::bind_method(D_METHOD("set_intensity", "intensity"), &GLTFLight::set_intensity);
|
||||
ClassDB::bind_method(D_METHOD("get_light_type"), &GLTFLight::get_light_type);
|
||||
ClassDB::bind_method(D_METHOD("set_light_type", "light_type"), &GLTFLight::set_light_type);
|
||||
ClassDB::bind_method(D_METHOD("get_range"), &GLTFLight::get_range);
|
||||
ClassDB::bind_method(D_METHOD("set_range", "range"), &GLTFLight::set_range);
|
||||
ClassDB::bind_method(D_METHOD("get_inner_cone_angle"), &GLTFLight::get_inner_cone_angle);
|
||||
ClassDB::bind_method(D_METHOD("set_inner_cone_angle", "inner_cone_angle"), &GLTFLight::set_inner_cone_angle);
|
||||
ClassDB::bind_method(D_METHOD("get_outer_cone_angle"), &GLTFLight::get_outer_cone_angle);
|
||||
ClassDB::bind_method(D_METHOD("set_outer_cone_angle", "outer_cone_angle"), &GLTFLight::set_outer_cone_angle);
|
||||
ClassDB::bind_method(D_METHOD("get_additional_data", "extension_name"), &GLTFLight::get_additional_data);
|
||||
ClassDB::bind_method(D_METHOD("set_additional_data", "extension_name", "additional_data"), &GLTFLight::set_additional_data);
|
||||
|
||||
ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_color", "get_color"); // Color
|
||||
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "intensity"), "set_intensity", "get_intensity"); // float
|
||||
ADD_PROPERTY(PropertyInfo(Variant::STRING, "light_type"), "set_light_type", "get_light_type"); // String
|
||||
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "range"), "set_range", "get_range"); // float
|
||||
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "inner_cone_angle"), "set_inner_cone_angle", "get_inner_cone_angle"); // float
|
||||
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "outer_cone_angle"), "set_outer_cone_angle", "get_outer_cone_angle"); // float
|
||||
}
|
||||
|
||||
void GLTFLight::set_cone_inner_attenuation_conversion_expressions(Ref<GLTFObjectModelProperty> &r_obj_model_prop) {
|
||||
// Expression to convert glTF innerConeAngle to Godot spot_angle_attenuation.
|
||||
Ref<Expression> gltf_to_godot_expr;
|
||||
gltf_to_godot_expr.instantiate();
|
||||
PackedStringArray gltf_to_godot_args = { "inner_cone_angle" };
|
||||
gltf_to_godot_expr->parse("0.2 / (1.0 - inner_cone_angle / spot_angle) - 0.1", gltf_to_godot_args);
|
||||
r_obj_model_prop->set_gltf_to_godot_expression(gltf_to_godot_expr);
|
||||
// Expression to convert Godot spot_angle_attenuation to glTF innerConeAngle.
|
||||
Ref<Expression> godot_to_gltf_expr;
|
||||
godot_to_gltf_expr.instantiate();
|
||||
PackedStringArray godot_to_gltf_args = { "godot_spot_angle_att" };
|
||||
godot_to_gltf_expr->parse("spot_angle * maxf(0.0, 1.0 - (0.2 / (0.1 + godot_spot_angle_att)))", godot_to_gltf_args);
|
||||
r_obj_model_prop->set_godot_to_gltf_expression(godot_to_gltf_expr);
|
||||
}
|
||||
|
||||
Color GLTFLight::get_color() {
|
||||
return color;
|
||||
}
|
||||
|
||||
void GLTFLight::set_color(Color p_color) {
|
||||
color = p_color;
|
||||
}
|
||||
|
||||
float GLTFLight::get_intensity() {
|
||||
return intensity;
|
||||
}
|
||||
|
||||
void GLTFLight::set_intensity(float p_intensity) {
|
||||
intensity = p_intensity;
|
||||
}
|
||||
|
||||
String GLTFLight::get_light_type() {
|
||||
return light_type;
|
||||
}
|
||||
|
||||
void GLTFLight::set_light_type(String p_light_type) {
|
||||
light_type = p_light_type;
|
||||
}
|
||||
|
||||
float GLTFLight::get_range() {
|
||||
return range;
|
||||
}
|
||||
|
||||
void GLTFLight::set_range(float p_range) {
|
||||
range = p_range;
|
||||
}
|
||||
|
||||
float GLTFLight::get_inner_cone_angle() {
|
||||
return inner_cone_angle;
|
||||
}
|
||||
|
||||
void GLTFLight::set_inner_cone_angle(float p_inner_cone_angle) {
|
||||
inner_cone_angle = p_inner_cone_angle;
|
||||
}
|
||||
|
||||
float GLTFLight::get_outer_cone_angle() {
|
||||
return outer_cone_angle;
|
||||
}
|
||||
|
||||
void GLTFLight::set_outer_cone_angle(float p_outer_cone_angle) {
|
||||
outer_cone_angle = p_outer_cone_angle;
|
||||
}
|
||||
|
||||
Ref<GLTFLight> GLTFLight::from_node(const Light3D *p_light) {
|
||||
Ref<GLTFLight> l;
|
||||
l.instantiate();
|
||||
ERR_FAIL_NULL_V_MSG(p_light, l, "Tried to create a GLTFLight from a Light3D node, but the given node was null.");
|
||||
l->color = p_light->get_color().srgb_to_linear();
|
||||
if (cast_to<DirectionalLight3D>(p_light)) {
|
||||
l->light_type = "directional";
|
||||
const DirectionalLight3D *light = cast_to<const DirectionalLight3D>(p_light);
|
||||
l->intensity = light->get_param(DirectionalLight3D::PARAM_ENERGY);
|
||||
l->range = FLT_MAX; // Range for directional lights is infinite in Godot.
|
||||
} else if (cast_to<const OmniLight3D>(p_light)) {
|
||||
l->light_type = "point";
|
||||
const OmniLight3D *light = cast_to<const OmniLight3D>(p_light);
|
||||
l->range = light->get_param(OmniLight3D::PARAM_RANGE);
|
||||
l->intensity = light->get_param(OmniLight3D::PARAM_ENERGY);
|
||||
} else if (cast_to<const SpotLight3D>(p_light)) {
|
||||
l->light_type = "spot";
|
||||
const SpotLight3D *light = cast_to<const SpotLight3D>(p_light);
|
||||
l->range = light->get_param(SpotLight3D::PARAM_RANGE);
|
||||
l->intensity = light->get_param(SpotLight3D::PARAM_ENERGY);
|
||||
l->outer_cone_angle = Math::deg_to_rad(light->get_param(SpotLight3D::PARAM_SPOT_ANGLE));
|
||||
// This equation is the inverse of the import equation (which has a desmos link).
|
||||
float angle_ratio = 1 - (0.2 / (0.1 + light->get_param(SpotLight3D::PARAM_SPOT_ATTENUATION)));
|
||||
angle_ratio = MAX(0, angle_ratio);
|
||||
l->inner_cone_angle = l->outer_cone_angle * angle_ratio;
|
||||
}
|
||||
return l;
|
||||
}
|
||||
|
||||
Light3D *GLTFLight::to_node() const {
|
||||
Light3D *light = nullptr;
|
||||
if (light_type == "directional") {
|
||||
DirectionalLight3D *dir_light = memnew(DirectionalLight3D);
|
||||
dir_light->set_param(Light3D::PARAM_ENERGY, intensity);
|
||||
light = dir_light;
|
||||
} else if (light_type == "point") {
|
||||
OmniLight3D *omni_light = memnew(OmniLight3D);
|
||||
omni_light->set_param(OmniLight3D::PARAM_ENERGY, intensity);
|
||||
omni_light->set_param(OmniLight3D::PARAM_RANGE, CLAMP(range, 0, 4096));
|
||||
light = omni_light;
|
||||
} else if (light_type == "spot") {
|
||||
SpotLight3D *spot_light = memnew(SpotLight3D);
|
||||
spot_light->set_param(SpotLight3D::PARAM_ENERGY, intensity);
|
||||
spot_light->set_param(SpotLight3D::PARAM_RANGE, CLAMP(range, 0, 4096));
|
||||
spot_light->set_param(SpotLight3D::PARAM_SPOT_ANGLE, Math::rad_to_deg(outer_cone_angle));
|
||||
// Line of best fit derived from guessing, see https://www.desmos.com/calculator/biiflubp8b
|
||||
// The points in desmos are not exact, except for (1, infinity).
|
||||
float angle_ratio = inner_cone_angle / outer_cone_angle;
|
||||
float angle_attenuation = 0.2 / (1 - angle_ratio) - 0.1;
|
||||
spot_light->set_param(SpotLight3D::PARAM_SPOT_ATTENUATION, angle_attenuation);
|
||||
light = spot_light;
|
||||
} else {
|
||||
ERR_PRINT("Failed to create a Light3D node from GLTFLight, unknown light type '" + light_type + "'.");
|
||||
return nullptr;
|
||||
}
|
||||
light->set_color(color.linear_to_srgb());
|
||||
light->set_param(Light3D::PARAM_ATTENUATION, 2.0);
|
||||
return light;
|
||||
}
|
||||
|
||||
Ref<GLTFLight> GLTFLight::from_dictionary(const Dictionary p_dictionary) {
|
||||
ERR_FAIL_COND_V_MSG(!p_dictionary.has("type"), Ref<GLTFLight>(), "Failed to parse glTF light, missing required field 'type'.");
|
||||
Ref<GLTFLight> light;
|
||||
light.instantiate();
|
||||
const String &type = p_dictionary["type"];
|
||||
light->light_type = type;
|
||||
|
||||
if (p_dictionary.has("color")) {
|
||||
const Array &arr = p_dictionary["color"];
|
||||
if (arr.size() == 3) {
|
||||
light->color = Color(arr[0], arr[1], arr[2]);
|
||||
} else {
|
||||
ERR_PRINT("Error parsing glTF light: The color must have exactly 3 numbers.");
|
||||
}
|
||||
}
|
||||
if (p_dictionary.has("intensity")) {
|
||||
light->intensity = p_dictionary["intensity"];
|
||||
}
|
||||
if (p_dictionary.has("range")) {
|
||||
light->range = p_dictionary["range"];
|
||||
}
|
||||
if (type == "spot") {
|
||||
const Dictionary &spot = p_dictionary["spot"];
|
||||
light->inner_cone_angle = spot["innerConeAngle"];
|
||||
light->outer_cone_angle = spot["outerConeAngle"];
|
||||
if (light->inner_cone_angle >= light->outer_cone_angle) {
|
||||
ERR_PRINT("Error parsing glTF light: The inner angle must be smaller than the outer angle.");
|
||||
}
|
||||
} else if (type != "point" && type != "directional") {
|
||||
ERR_PRINT("Error parsing glTF light: Light type '" + type + "' is unknown.");
|
||||
}
|
||||
return light;
|
||||
}
|
||||
|
||||
Dictionary GLTFLight::to_dictionary() const {
|
||||
Dictionary d;
|
||||
if (color != Color(1.0f, 1.0f, 1.0f)) {
|
||||
Array color_array;
|
||||
color_array.resize(3);
|
||||
color_array[0] = color.r;
|
||||
color_array[1] = color.g;
|
||||
color_array[2] = color.b;
|
||||
d["color"] = color_array;
|
||||
}
|
||||
if (intensity != 1.0f) {
|
||||
d["intensity"] = intensity;
|
||||
}
|
||||
if (light_type != "directional" && range != Math::INF) {
|
||||
d["range"] = range;
|
||||
}
|
||||
if (light_type == "spot") {
|
||||
Dictionary spot_dict;
|
||||
spot_dict["innerConeAngle"] = inner_cone_angle;
|
||||
spot_dict["outerConeAngle"] = outer_cone_angle;
|
||||
d["spot"] = spot_dict;
|
||||
}
|
||||
d["type"] = light_type;
|
||||
return d;
|
||||
}
|
||||
|
||||
Variant GLTFLight::get_additional_data(const StringName &p_extension_name) {
|
||||
return additional_data[p_extension_name];
|
||||
}
|
||||
|
||||
void GLTFLight::set_additional_data(const StringName &p_extension_name, Variant p_additional_data) {
|
||||
additional_data[p_extension_name] = p_additional_data;
|
||||
}
|
85
modules/gltf/extensions/gltf_light.h
Normal file
85
modules/gltf/extensions/gltf_light.h
Normal file
@@ -0,0 +1,85 @@
|
||||
/**************************************************************************/
|
||||
/* gltf_light.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "core/io/resource.h"
|
||||
|
||||
class GLTFObjectModelProperty;
|
||||
class Light3D;
|
||||
|
||||
// https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Khronos/KHR_lights_punctual
|
||||
|
||||
class GLTFLight : public Resource {
|
||||
GDCLASS(GLTFLight, Resource)
|
||||
friend class GLTFDocument;
|
||||
|
||||
protected:
|
||||
static void _bind_methods();
|
||||
|
||||
private:
|
||||
Color color = Color(1.0f, 1.0f, 1.0f);
|
||||
float intensity = 1.0f;
|
||||
String light_type;
|
||||
float range = Math::INF;
|
||||
float inner_cone_angle = 0.0f;
|
||||
float outer_cone_angle = Math::TAU / 8.0f;
|
||||
Dictionary additional_data;
|
||||
|
||||
public:
|
||||
static void set_cone_inner_attenuation_conversion_expressions(Ref<GLTFObjectModelProperty> &r_obj_model_prop);
|
||||
|
||||
Color get_color();
|
||||
void set_color(Color p_color);
|
||||
|
||||
float get_intensity();
|
||||
void set_intensity(float p_intensity);
|
||||
|
||||
String get_light_type();
|
||||
void set_light_type(String p_light_type);
|
||||
|
||||
float get_range();
|
||||
void set_range(float p_range);
|
||||
|
||||
float get_inner_cone_angle();
|
||||
void set_inner_cone_angle(float p_inner_cone_angle);
|
||||
|
||||
float get_outer_cone_angle();
|
||||
void set_outer_cone_angle(float p_outer_cone_angle);
|
||||
|
||||
static Ref<GLTFLight> from_node(const Light3D *p_light);
|
||||
Light3D *to_node() const;
|
||||
|
||||
static Ref<GLTFLight> from_dictionary(const Dictionary p_dictionary);
|
||||
Dictionary to_dictionary() const;
|
||||
|
||||
Variant get_additional_data(const StringName &p_extension_name);
|
||||
void set_additional_data(const StringName &p_extension_name, Variant p_additional_data);
|
||||
};
|
92
modules/gltf/extensions/gltf_spec_gloss.cpp
Normal file
92
modules/gltf/extensions/gltf_spec_gloss.cpp
Normal file
@@ -0,0 +1,92 @@
|
||||
/**************************************************************************/
|
||||
/* gltf_spec_gloss.cpp */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#include "gltf_spec_gloss.h"
|
||||
|
||||
#include "core/io/image.h"
|
||||
|
||||
void GLTFSpecGloss::_bind_methods() {
|
||||
ClassDB::bind_method(D_METHOD("get_diffuse_img"), &GLTFSpecGloss::get_diffuse_img);
|
||||
ClassDB::bind_method(D_METHOD("set_diffuse_img", "diffuse_img"), &GLTFSpecGloss::set_diffuse_img);
|
||||
ClassDB::bind_method(D_METHOD("get_diffuse_factor"), &GLTFSpecGloss::get_diffuse_factor);
|
||||
ClassDB::bind_method(D_METHOD("set_diffuse_factor", "diffuse_factor"), &GLTFSpecGloss::set_diffuse_factor);
|
||||
ClassDB::bind_method(D_METHOD("get_gloss_factor"), &GLTFSpecGloss::get_gloss_factor);
|
||||
ClassDB::bind_method(D_METHOD("set_gloss_factor", "gloss_factor"), &GLTFSpecGloss::set_gloss_factor);
|
||||
ClassDB::bind_method(D_METHOD("get_specular_factor"), &GLTFSpecGloss::get_specular_factor);
|
||||
ClassDB::bind_method(D_METHOD("set_specular_factor", "specular_factor"), &GLTFSpecGloss::set_specular_factor);
|
||||
ClassDB::bind_method(D_METHOD("get_spec_gloss_img"), &GLTFSpecGloss::get_spec_gloss_img);
|
||||
ClassDB::bind_method(D_METHOD("set_spec_gloss_img", "spec_gloss_img"), &GLTFSpecGloss::set_spec_gloss_img);
|
||||
|
||||
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "diffuse_img"), "set_diffuse_img", "get_diffuse_img"); // Ref<Image>
|
||||
ADD_PROPERTY(PropertyInfo(Variant::COLOR, "diffuse_factor"), "set_diffuse_factor", "get_diffuse_factor"); // Color
|
||||
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "gloss_factor"), "set_gloss_factor", "get_gloss_factor"); // float
|
||||
ADD_PROPERTY(PropertyInfo(Variant::COLOR, "specular_factor"), "set_specular_factor", "get_specular_factor"); // Color
|
||||
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "spec_gloss_img"), "set_spec_gloss_img", "get_spec_gloss_img"); // Ref<Image>
|
||||
}
|
||||
|
||||
Ref<Image> GLTFSpecGloss::get_diffuse_img() {
|
||||
return diffuse_img;
|
||||
}
|
||||
|
||||
void GLTFSpecGloss::set_diffuse_img(Ref<Image> p_diffuse_img) {
|
||||
diffuse_img = p_diffuse_img;
|
||||
}
|
||||
|
||||
Color GLTFSpecGloss::get_diffuse_factor() {
|
||||
return diffuse_factor;
|
||||
}
|
||||
|
||||
void GLTFSpecGloss::set_diffuse_factor(Color p_diffuse_factor) {
|
||||
diffuse_factor = p_diffuse_factor;
|
||||
}
|
||||
|
||||
float GLTFSpecGloss::get_gloss_factor() {
|
||||
return gloss_factor;
|
||||
}
|
||||
|
||||
void GLTFSpecGloss::set_gloss_factor(float p_gloss_factor) {
|
||||
gloss_factor = p_gloss_factor;
|
||||
}
|
||||
|
||||
Color GLTFSpecGloss::get_specular_factor() {
|
||||
return specular_factor;
|
||||
}
|
||||
|
||||
void GLTFSpecGloss::set_specular_factor(Color p_specular_factor) {
|
||||
specular_factor = p_specular_factor;
|
||||
}
|
||||
|
||||
Ref<Image> GLTFSpecGloss::get_spec_gloss_img() {
|
||||
return spec_gloss_img;
|
||||
}
|
||||
|
||||
void GLTFSpecGloss::set_spec_gloss_img(Ref<Image> p_spec_gloss_img) {
|
||||
spec_gloss_img = p_spec_gloss_img;
|
||||
}
|
71
modules/gltf/extensions/gltf_spec_gloss.h
Normal file
71
modules/gltf/extensions/gltf_spec_gloss.h
Normal file
@@ -0,0 +1,71 @@
|
||||
/**************************************************************************/
|
||||
/* gltf_spec_gloss.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "core/io/resource.h"
|
||||
|
||||
class Image;
|
||||
|
||||
// KHR_materials_pbrSpecularGlossiness is an archived GLTF extension.
|
||||
// This means that it is deprecated and not recommended for new files.
|
||||
// However, it is still supported for loading old files.
|
||||
// https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Archived/KHR_materials_pbrSpecularGlossiness
|
||||
|
||||
class GLTFSpecGloss : public Resource {
|
||||
GDCLASS(GLTFSpecGloss, Resource);
|
||||
friend class GLTFDocument;
|
||||
|
||||
private:
|
||||
Ref<Image> diffuse_img = nullptr;
|
||||
Color diffuse_factor = Color(1.0f, 1.0f, 1.0f);
|
||||
float gloss_factor = 1.0f;
|
||||
Color specular_factor = Color(1.0f, 1.0f, 1.0f);
|
||||
Ref<Image> spec_gloss_img = nullptr;
|
||||
|
||||
protected:
|
||||
static void _bind_methods();
|
||||
|
||||
public:
|
||||
Ref<Image> get_diffuse_img();
|
||||
void set_diffuse_img(Ref<Image> p_diffuse_img);
|
||||
|
||||
Color get_diffuse_factor();
|
||||
void set_diffuse_factor(Color p_diffuse_factor);
|
||||
|
||||
float get_gloss_factor();
|
||||
void set_gloss_factor(float p_gloss_factor);
|
||||
|
||||
Color get_specular_factor();
|
||||
void set_specular_factor(Color p_specular_factor);
|
||||
|
||||
Ref<Image> get_spec_gloss_img();
|
||||
void set_spec_gloss_img(Ref<Image> p_spec_gloss_img);
|
||||
};
|
@@ -0,0 +1,730 @@
|
||||
/**************************************************************************/
|
||||
/* gltf_document_extension_physics.cpp */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#include "gltf_document_extension_physics.h"
|
||||
|
||||
#include "scene/3d/physics/area_3d.h"
|
||||
#include "scene/3d/physics/rigid_body_3d.h"
|
||||
#include "scene/3d/physics/static_body_3d.h"
|
||||
|
||||
using GLTFShapeIndex = int64_t;
|
||||
|
||||
// Import process.
|
||||
Error GLTFDocumentExtensionPhysics::import_preflight(Ref<GLTFState> p_state, Vector<String> p_extensions) {
|
||||
if (!p_extensions.has("OMI_collider") && !p_extensions.has("OMI_physics_body") && !p_extensions.has("OMI_physics_shape")) {
|
||||
return ERR_SKIP;
|
||||
}
|
||||
Dictionary state_json = p_state->get_json();
|
||||
if (state_json.has("extensions")) {
|
||||
Dictionary state_extensions = state_json["extensions"];
|
||||
if (state_extensions.has("OMI_physics_shape")) {
|
||||
Dictionary omi_physics_shape_ext = state_extensions["OMI_physics_shape"];
|
||||
if (omi_physics_shape_ext.has("shapes")) {
|
||||
Array state_shape_dicts = omi_physics_shape_ext["shapes"];
|
||||
if (state_shape_dicts.size() > 0) {
|
||||
Array state_shapes;
|
||||
for (int i = 0; i < state_shape_dicts.size(); i++) {
|
||||
state_shapes.push_back(GLTFPhysicsShape::from_dictionary(state_shape_dicts[i]));
|
||||
}
|
||||
p_state->set_additional_data(StringName("GLTFPhysicsShapes"), state_shapes);
|
||||
}
|
||||
}
|
||||
#ifndef DISABLE_DEPRECATED
|
||||
} else if (state_extensions.has("OMI_collider")) {
|
||||
Dictionary omi_collider_ext = state_extensions["OMI_collider"];
|
||||
if (omi_collider_ext.has("colliders")) {
|
||||
Array state_collider_dicts = omi_collider_ext["colliders"];
|
||||
if (state_collider_dicts.size() > 0) {
|
||||
Array state_colliders;
|
||||
for (int i = 0; i < state_collider_dicts.size(); i++) {
|
||||
state_colliders.push_back(GLTFPhysicsShape::from_dictionary(state_collider_dicts[i]));
|
||||
}
|
||||
p_state->set_additional_data(StringName("GLTFPhysicsShapes"), state_colliders);
|
||||
}
|
||||
}
|
||||
#endif // DISABLE_DEPRECATED
|
||||
}
|
||||
}
|
||||
return OK;
|
||||
}
|
||||
|
||||
Vector<String> GLTFDocumentExtensionPhysics::get_supported_extensions() {
|
||||
Vector<String> ret;
|
||||
ret.push_back("OMI_collider");
|
||||
ret.push_back("OMI_physics_body");
|
||||
ret.push_back("OMI_physics_shape");
|
||||
return ret;
|
||||
}
|
||||
|
||||
Error GLTFDocumentExtensionPhysics::parse_node_extensions(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Dictionary &p_extensions) {
|
||||
#ifndef DISABLE_DEPRECATED
|
||||
if (p_extensions.has("OMI_collider")) {
|
||||
Dictionary node_collider_ext = p_extensions["OMI_collider"];
|
||||
if (node_collider_ext.has("collider")) {
|
||||
// "collider" is the index of the collider in the state colliders array.
|
||||
int node_collider_index = node_collider_ext["collider"];
|
||||
Array state_colliders = p_state->get_additional_data(StringName("GLTFPhysicsShapes"));
|
||||
ERR_FAIL_INDEX_V_MSG(node_collider_index, state_colliders.size(), Error::ERR_FILE_CORRUPT, "glTF Physics: On node " + p_gltf_node->get_name() + ", the collider index " + itos(node_collider_index) + " is not in the state colliders (size: " + itos(state_colliders.size()) + ").");
|
||||
p_gltf_node->set_additional_data(StringName("GLTFPhysicsShape"), state_colliders[node_collider_index]);
|
||||
} else {
|
||||
p_gltf_node->set_additional_data(StringName("GLTFPhysicsShape"), GLTFPhysicsShape::from_dictionary(node_collider_ext));
|
||||
}
|
||||
}
|
||||
#endif // DISABLE_DEPRECATED
|
||||
if (p_extensions.has("OMI_physics_body")) {
|
||||
Dictionary physics_body_ext = p_extensions["OMI_physics_body"];
|
||||
if (physics_body_ext.has("collider")) {
|
||||
Dictionary node_collider = physics_body_ext["collider"];
|
||||
// "shape" is the index of the shape in the state shapes array.
|
||||
int node_shape_index = node_collider.get("shape", -1);
|
||||
if (node_shape_index != -1) {
|
||||
Array state_shapes = p_state->get_additional_data(StringName("GLTFPhysicsShapes"));
|
||||
ERR_FAIL_INDEX_V_MSG(node_shape_index, state_shapes.size(), Error::ERR_FILE_CORRUPT, "glTF Physics: On node " + p_gltf_node->get_name() + ", the shape index " + itos(node_shape_index) + " is not in the state shapes (size: " + itos(state_shapes.size()) + ").");
|
||||
p_gltf_node->set_additional_data(StringName("GLTFPhysicsColliderShape"), state_shapes[node_shape_index]);
|
||||
p_gltf_node->set_additional_data(StringName("GLTFPhysicsColliderShapeIndex"), node_shape_index);
|
||||
} else {
|
||||
// If this node is a collider but does not have a collider
|
||||
// shape, then it only serves to combine together shapes.
|
||||
p_gltf_node->set_additional_data(StringName("GLTFPhysicsCompoundCollider"), true);
|
||||
}
|
||||
}
|
||||
if (physics_body_ext.has("trigger")) {
|
||||
Dictionary node_trigger = physics_body_ext["trigger"];
|
||||
// "shape" is the index of the shape in the state shapes array.
|
||||
int node_shape_index = node_trigger.get("shape", -1);
|
||||
if (node_shape_index != -1) {
|
||||
Array state_shapes = p_state->get_additional_data(StringName("GLTFPhysicsShapes"));
|
||||
ERR_FAIL_INDEX_V_MSG(node_shape_index, state_shapes.size(), Error::ERR_FILE_CORRUPT, "glTF Physics: On node " + p_gltf_node->get_name() + ", the shape index " + itos(node_shape_index) + " is not in the state shapes (size: " + itos(state_shapes.size()) + ").");
|
||||
p_gltf_node->set_additional_data(StringName("GLTFPhysicsTriggerShape"), state_shapes[node_shape_index]);
|
||||
p_gltf_node->set_additional_data(StringName("GLTFPhysicsTriggerShapeIndex"), node_shape_index);
|
||||
} else {
|
||||
// If this node is a trigger but does not have a trigger shape,
|
||||
// then it's a trigger body, what Godot calls an Area3D node.
|
||||
Ref<GLTFPhysicsBody> trigger_body;
|
||||
trigger_body.instantiate();
|
||||
trigger_body->set_body_type("trigger");
|
||||
p_gltf_node->set_additional_data(StringName("GLTFPhysicsBody"), trigger_body);
|
||||
}
|
||||
// If this node defines explicit member shape nodes, save this information.
|
||||
if (node_trigger.has("nodes")) {
|
||||
Array compound_trigger_nodes = node_trigger["nodes"];
|
||||
p_gltf_node->set_additional_data(StringName("GLTFPhysicsCompoundTriggerNodes"), compound_trigger_nodes);
|
||||
}
|
||||
}
|
||||
if (physics_body_ext.has("motion") || physics_body_ext.has("type")) {
|
||||
p_gltf_node->set_additional_data(StringName("GLTFPhysicsBody"), GLTFPhysicsBody::from_dictionary(physics_body_ext));
|
||||
}
|
||||
}
|
||||
return OK;
|
||||
}
|
||||
|
||||
bool _will_gltf_shape_become_subnode(Ref<GLTFState> p_state, const Ref<GLTFNode> p_gltf_node, GLTFNodeIndex p_gltf_node_index) {
|
||||
if (p_gltf_node->has_additional_data(StringName("GLTFPhysicsBody"))) {
|
||||
return true;
|
||||
}
|
||||
const TypedArray<GLTFNode> state_gltf_nodes = p_state->get_nodes();
|
||||
const GLTFNodeIndex parent_index = p_gltf_node->get_parent();
|
||||
if (parent_index == -1 || parent_index >= state_gltf_nodes.size()) {
|
||||
return true;
|
||||
}
|
||||
const Ref<GLTFNode> parent_gltf_node = state_gltf_nodes[parent_index];
|
||||
const Variant parent_body_maybe = parent_gltf_node->get_additional_data(StringName("GLTFPhysicsBody"));
|
||||
if (parent_body_maybe.get_type() != Variant::NIL) {
|
||||
Ref<GLTFPhysicsBody> parent_body = parent_body_maybe;
|
||||
// If the parent matches the triggerness, then this node will be generated as a shape (CollisionShape3D).
|
||||
// Otherwise, if there is a mismatch, a body will be generated for this node, and a subnode will also be generated for the shape.
|
||||
if (parent_body->get_body_type() == "trigger") {
|
||||
return p_gltf_node->has_additional_data(StringName("GLTFPhysicsColliderShape"));
|
||||
} else {
|
||||
return p_gltf_node->has_additional_data(StringName("GLTFPhysicsTriggerShape"));
|
||||
}
|
||||
}
|
||||
if (parent_gltf_node->has_additional_data(StringName("GLTFPhysicsColliderShape"))) {
|
||||
return false;
|
||||
}
|
||||
if (parent_gltf_node->has_additional_data(StringName("GLTFPhysicsTriggerShape"))) {
|
||||
return false;
|
||||
}
|
||||
Variant compound_trigger_maybe = parent_gltf_node->has_additional_data(StringName("GLTFPhysicsCompoundTriggerNodes"));
|
||||
if (compound_trigger_maybe.get_type() != Variant::NIL) {
|
||||
Array compound_trigger_nodes = compound_trigger_maybe;
|
||||
// Remember, JSON only has numbers, not integers, so must cast to double.
|
||||
return !compound_trigger_nodes.has((double)p_gltf_node_index);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
NodePath _get_scene_node_path_for_shape_index(Ref<GLTFState> p_state, const GLTFNodeIndex p_shape_index) {
|
||||
TypedArray<GLTFNode> state_gltf_nodes = p_state->get_nodes();
|
||||
for (GLTFNodeIndex node_index = 0; node_index < state_gltf_nodes.size(); node_index++) {
|
||||
const Ref<GLTFNode> gltf_node = state_gltf_nodes[node_index];
|
||||
ERR_CONTINUE(gltf_node.is_null());
|
||||
// Check if this node has a shape index and if it matches the one we are looking for.
|
||||
Variant shape_index_maybe = gltf_node->get_additional_data(StringName("GLTFPhysicsColliderShapeIndex"));
|
||||
if (shape_index_maybe.get_type() != Variant::INT) {
|
||||
shape_index_maybe = gltf_node->get_additional_data(StringName("GLTFPhysicsTriggerShapeIndex"));
|
||||
if (shape_index_maybe.get_type() != Variant::INT) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
const GLTFShapeIndex shape_index = shape_index_maybe;
|
||||
if (shape_index != p_shape_index) {
|
||||
continue;
|
||||
}
|
||||
NodePath node_path = gltf_node->get_scene_node_path(p_state);
|
||||
// At this point, we have found a node with the shape index we were looking for.
|
||||
if (_will_gltf_shape_become_subnode(p_state, gltf_node, node_index)) {
|
||||
Vector<StringName> sname_path = node_path.get_names();
|
||||
sname_path.append(gltf_node->get_name() + "Shape");
|
||||
node_path = NodePath(sname_path, false);
|
||||
}
|
||||
return node_path;
|
||||
}
|
||||
return NodePath();
|
||||
}
|
||||
|
||||
Ref<GLTFObjectModelProperty> GLTFDocumentExtensionPhysics::import_object_model_property(Ref<GLTFState> p_state, const PackedStringArray &p_split_json_pointer, const TypedArray<NodePath> &p_partial_paths) {
|
||||
Ref<GLTFObjectModelProperty> ret;
|
||||
if (p_split_json_pointer.size() != 6) {
|
||||
// The only properties this class cares about are exactly 6 levels deep.
|
||||
return ret;
|
||||
}
|
||||
ret.instantiate();
|
||||
const String &prop_name = p_split_json_pointer[5];
|
||||
if (p_split_json_pointer[0] == "extensions" && p_split_json_pointer[2] == "shapes") {
|
||||
if (p_split_json_pointer[1] == "OMI_physics_shape" || p_split_json_pointer[1] == "KHR_collision_shapes") {
|
||||
const GLTFNodeIndex shape_index = p_split_json_pointer[3].to_int();
|
||||
NodePath node_path = _get_scene_node_path_for_shape_index(p_state, shape_index);
|
||||
if (node_path.is_empty()) {
|
||||
return ret;
|
||||
}
|
||||
String godot_prop_name = prop_name;
|
||||
if (prop_name == "size") {
|
||||
ret->set_types(Variant::VECTOR3, GLTFObjectModelProperty::GLTF_OBJECT_MODEL_TYPE_FLOAT3);
|
||||
} else if (prop_name == "height" || prop_name == "radius") {
|
||||
ret->set_types(Variant::FLOAT, GLTFObjectModelProperty::GLTF_OBJECT_MODEL_TYPE_FLOAT);
|
||||
} else if (prop_name == "radiusBottom" || prop_name == "radiusTop") {
|
||||
godot_prop_name = "radius";
|
||||
ret->set_types(Variant::FLOAT, GLTFObjectModelProperty::GLTF_OBJECT_MODEL_TYPE_FLOAT);
|
||||
} else {
|
||||
// Not something we handle, return without appending a NodePath.
|
||||
return ret;
|
||||
}
|
||||
// Example: `A/B/C/CollisionShape3D:shape:radius`.
|
||||
Vector<StringName> subnames;
|
||||
subnames.append("shape");
|
||||
subnames.append(godot_prop_name);
|
||||
node_path = NodePath(node_path.get_names(), subnames, false);
|
||||
ret->append_node_path(node_path);
|
||||
}
|
||||
} else if (p_split_json_pointer[0] == "nodes" && p_split_json_pointer[2] == "extensions" && p_split_json_pointer[4] == "motion") {
|
||||
if (p_split_json_pointer[3] == "OMI_physics_body" || p_split_json_pointer[3] == "KHR_physics_rigid_bodies") {
|
||||
const GLTFNodeIndex node_index = p_split_json_pointer[1].to_int();
|
||||
const TypedArray<GLTFNode> all_gltf_nodes = p_state->get_nodes();
|
||||
ERR_FAIL_INDEX_V_MSG(node_index, all_gltf_nodes.size(), ret, "GLTF Physics: The node index " + itos(node_index) + " is not in the state nodes (size: " + itos(all_gltf_nodes.size()) + ").");
|
||||
const Ref<GLTFNode> gltf_node = all_gltf_nodes[node_index];
|
||||
NodePath node_path;
|
||||
if (p_partial_paths.is_empty()) {
|
||||
node_path = gltf_node->get_scene_node_path(p_state);
|
||||
} else {
|
||||
// The path is already computed for us, just grab it.
|
||||
node_path = p_partial_paths[0];
|
||||
}
|
||||
if (prop_name == "mass") {
|
||||
ret->append_path_to_property(node_path, "mass");
|
||||
ret->set_types(Variant::FLOAT, GLTFObjectModelProperty::GLTF_OBJECT_MODEL_TYPE_FLOAT);
|
||||
} else if (prop_name == "linearVelocity") {
|
||||
ret->append_path_to_property(node_path, "linear_velocity");
|
||||
ret->set_types(Variant::VECTOR3, GLTFObjectModelProperty::GLTF_OBJECT_MODEL_TYPE_FLOAT3);
|
||||
} else if (prop_name == "angularVelocity") {
|
||||
ret->append_path_to_property(node_path, "angular_velocity");
|
||||
ret->set_types(Variant::VECTOR3, GLTFObjectModelProperty::GLTF_OBJECT_MODEL_TYPE_FLOAT3);
|
||||
} else if (prop_name == "centerOfMass") {
|
||||
ret->append_path_to_property(node_path, "center_of_mass");
|
||||
ret->set_types(Variant::VECTOR3, GLTFObjectModelProperty::GLTF_OBJECT_MODEL_TYPE_FLOAT3);
|
||||
} else if (prop_name == "inertiaDiagonal") {
|
||||
ret->append_path_to_property(node_path, "inertia");
|
||||
ret->set_types(Variant::VECTOR3, GLTFObjectModelProperty::GLTF_OBJECT_MODEL_TYPE_FLOAT3);
|
||||
} else if (prop_name == "inertiaOrientation") {
|
||||
WARN_PRINT("GLTF Physics: The 'inertiaOrientation' property is not supported by Godot.");
|
||||
} else {
|
||||
// Not something we handle, return without appending a NodePath.
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
void _setup_shape_mesh_resource_from_index_if_needed(Ref<GLTFState> p_state, Ref<GLTFPhysicsShape> p_gltf_shape) {
|
||||
GLTFMeshIndex shape_mesh_index = p_gltf_shape->get_mesh_index();
|
||||
if (shape_mesh_index == -1) {
|
||||
return; // No mesh for this shape.
|
||||
}
|
||||
Ref<ImporterMesh> importer_mesh = p_gltf_shape->get_importer_mesh();
|
||||
if (importer_mesh.is_valid()) {
|
||||
return; // The mesh resource is already set up.
|
||||
}
|
||||
TypedArray<GLTFMesh> state_meshes = p_state->get_meshes();
|
||||
ERR_FAIL_INDEX_MSG(shape_mesh_index, state_meshes.size(), "glTF Physics: When importing '" + p_state->get_scene_name() + "', the shape mesh index " + itos(shape_mesh_index) + " is not in the state meshes (size: " + itos(state_meshes.size()) + ").");
|
||||
Ref<GLTFMesh> gltf_mesh = state_meshes[shape_mesh_index];
|
||||
ERR_FAIL_COND(gltf_mesh.is_null());
|
||||
importer_mesh = gltf_mesh->get_mesh();
|
||||
ERR_FAIL_COND(importer_mesh.is_null());
|
||||
p_gltf_shape->set_importer_mesh(importer_mesh);
|
||||
}
|
||||
|
||||
#ifndef DISABLE_DEPRECATED
|
||||
CollisionObject3D *_generate_shape_with_body(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Ref<GLTFPhysicsShape> p_physics_shape, Ref<GLTFPhysicsBody> p_physics_body) {
|
||||
print_verbose("glTF: Creating shape with body for: " + p_gltf_node->get_name());
|
||||
bool is_trigger = p_physics_shape->get_is_trigger();
|
||||
// This method is used for the case where we must generate a parent body.
|
||||
// This is can happen for multiple reasons. One possibility is that this
|
||||
// glTF file is using OMI_collider but not OMI_physics_body, or at least
|
||||
// this particular node is not using it. Another possibility is that the
|
||||
// physics body information is set up on the same glTF node, not a parent.
|
||||
CollisionObject3D *body;
|
||||
if (p_physics_body.is_valid()) {
|
||||
// This code is run when the physics body is on the same glTF node.
|
||||
body = p_physics_body->to_node();
|
||||
if (is_trigger && (p_physics_body->get_body_type() != "trigger")) {
|
||||
// Edge case: If the body's trigger and the collider's trigger
|
||||
// are in disagreement, we need to create another new body.
|
||||
CollisionObject3D *child = _generate_shape_with_body(p_state, p_gltf_node, p_physics_shape, nullptr);
|
||||
child->set_name(p_gltf_node->get_name() + (is_trigger ? String("Trigger") : String("Solid")));
|
||||
body->add_child(child);
|
||||
return body;
|
||||
}
|
||||
} else if (is_trigger) {
|
||||
body = memnew(Area3D);
|
||||
} else {
|
||||
body = memnew(StaticBody3D);
|
||||
}
|
||||
CollisionShape3D *shape = p_physics_shape->to_node();
|
||||
shape->set_name(p_gltf_node->get_name() + "Shape");
|
||||
body->add_child(shape);
|
||||
return body;
|
||||
}
|
||||
#endif // DISABLE_DEPRECATED
|
||||
|
||||
CollisionObject3D *_get_ancestor_collision_object(Node *p_scene_parent) {
|
||||
// Note: Despite the name of the method, at the moment this only checks
|
||||
// the direct parent. Only check more later if Godot adds support for it.
|
||||
if (p_scene_parent) {
|
||||
CollisionObject3D *co = Object::cast_to<CollisionObject3D>(p_scene_parent);
|
||||
if (likely(co)) {
|
||||
return co;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Node3D *_generate_shape_node_and_body_if_needed(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Ref<GLTFPhysicsShape> p_physics_shape, CollisionObject3D *p_col_object, bool p_is_trigger) {
|
||||
// If we need to generate a body node, do so.
|
||||
CollisionObject3D *body_node = nullptr;
|
||||
if (p_is_trigger || p_physics_shape->get_is_trigger()) {
|
||||
// If the shape wants to be a trigger but it doesn't
|
||||
// have an Area3D parent, we need to make one.
|
||||
if (!Object::cast_to<Area3D>(p_col_object)) {
|
||||
body_node = memnew(Area3D);
|
||||
}
|
||||
} else {
|
||||
if (!Object::cast_to<PhysicsBody3D>(p_col_object)) {
|
||||
body_node = memnew(StaticBody3D);
|
||||
}
|
||||
}
|
||||
// Generate the shape node.
|
||||
_setup_shape_mesh_resource_from_index_if_needed(p_state, p_physics_shape);
|
||||
CollisionShape3D *shape_node = p_physics_shape->to_node(true);
|
||||
if (body_node) {
|
||||
shape_node->set_name(p_gltf_node->get_name() + "Shape");
|
||||
body_node->add_child(shape_node);
|
||||
return body_node;
|
||||
}
|
||||
return shape_node;
|
||||
}
|
||||
|
||||
// Either add the child to the parent, or return the child if there is no parent.
|
||||
Node3D *_add_physics_node_to_given_node(Node3D *p_current_node, Node3D *p_child, Ref<GLTFNode> p_gltf_node) {
|
||||
if (!p_current_node) {
|
||||
return p_child;
|
||||
}
|
||||
String suffix;
|
||||
if (Object::cast_to<CollisionShape3D>(p_child)) {
|
||||
suffix = "Shape";
|
||||
} else if (Object::cast_to<Area3D>(p_child)) {
|
||||
suffix = "Trigger";
|
||||
} else {
|
||||
suffix = "Collider";
|
||||
}
|
||||
p_child->set_name(p_gltf_node->get_name() + suffix);
|
||||
p_current_node->add_child(p_child);
|
||||
return p_current_node;
|
||||
}
|
||||
|
||||
Array _get_ancestor_compound_trigger_nodes(Ref<GLTFState> p_state, TypedArray<GLTFNode> p_state_nodes, CollisionObject3D *p_ancestor_col_obj) {
|
||||
GLTFNodeIndex ancestor_index = p_state->get_node_index(p_ancestor_col_obj);
|
||||
ERR_FAIL_INDEX_V(ancestor_index, p_state_nodes.size(), Array());
|
||||
Ref<GLTFNode> ancestor_gltf_node = p_state_nodes[ancestor_index];
|
||||
Variant compound_trigger_nodes = ancestor_gltf_node->get_additional_data(StringName("GLTFPhysicsCompoundTriggerNodes"));
|
||||
if (compound_trigger_nodes.is_array()) {
|
||||
return compound_trigger_nodes;
|
||||
}
|
||||
Array ret;
|
||||
ancestor_gltf_node->set_additional_data(StringName("GLTFPhysicsCompoundTriggerNodes"), ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
Node3D *GLTFDocumentExtensionPhysics::generate_scene_node(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Node *p_scene_parent) {
|
||||
Ref<GLTFPhysicsBody> gltf_physics_body = p_gltf_node->get_additional_data(StringName("GLTFPhysicsBody"));
|
||||
#ifndef DISABLE_DEPRECATED
|
||||
// This deprecated code handles OMI_collider (which we internally name "GLTFPhysicsShape").
|
||||
Ref<GLTFPhysicsShape> gltf_physics_shape = p_gltf_node->get_additional_data(StringName("GLTFPhysicsShape"));
|
||||
if (gltf_physics_shape.is_valid()) {
|
||||
_setup_shape_mesh_resource_from_index_if_needed(p_state, gltf_physics_shape);
|
||||
// If this glTF node specifies both a shape and a body, generate both.
|
||||
if (gltf_physics_body.is_valid()) {
|
||||
return _generate_shape_with_body(p_state, p_gltf_node, gltf_physics_shape, gltf_physics_body);
|
||||
}
|
||||
CollisionObject3D *ancestor_col_obj = _get_ancestor_collision_object(p_scene_parent);
|
||||
if (gltf_physics_shape->get_is_trigger()) {
|
||||
// If the shape wants to be a trigger and it already has a
|
||||
// trigger parent, we only need to make the shape node.
|
||||
if (Object::cast_to<Area3D>(ancestor_col_obj)) {
|
||||
return gltf_physics_shape->to_node(true);
|
||||
}
|
||||
} else if (ancestor_col_obj != nullptr) {
|
||||
// If the shape has a valid parent, only make the shape node.
|
||||
return gltf_physics_shape->to_node(true);
|
||||
}
|
||||
// Otherwise, we need to create a new body.
|
||||
return _generate_shape_with_body(p_state, p_gltf_node, gltf_physics_shape, nullptr);
|
||||
}
|
||||
#endif // DISABLE_DEPRECATED
|
||||
Node3D *ret = nullptr;
|
||||
CollisionObject3D *ancestor_col_obj = nullptr;
|
||||
Ref<GLTFPhysicsShape> gltf_physics_collider_shape = p_gltf_node->get_additional_data(StringName("GLTFPhysicsColliderShape"));
|
||||
Ref<GLTFPhysicsShape> gltf_physics_trigger_shape = p_gltf_node->get_additional_data(StringName("GLTFPhysicsTriggerShape"));
|
||||
if (gltf_physics_body.is_valid()) {
|
||||
ancestor_col_obj = gltf_physics_body->to_node();
|
||||
ret = ancestor_col_obj;
|
||||
} else {
|
||||
ancestor_col_obj = _get_ancestor_collision_object(p_scene_parent);
|
||||
if (Object::cast_to<Area3D>(ancestor_col_obj) && gltf_physics_trigger_shape.is_valid()) {
|
||||
// At this point, we found an ancestor Area3D node. But do we want to use it for this trigger shape?
|
||||
TypedArray<GLTFNode> state_nodes = p_state->get_nodes();
|
||||
GLTFNodeIndex self_index = state_nodes.find(p_gltf_node);
|
||||
Array compound_trigger_nodes = _get_ancestor_compound_trigger_nodes(p_state, state_nodes, ancestor_col_obj);
|
||||
// Check if the ancestor specifies compound trigger nodes, and if this node is in there.
|
||||
// Remember that JSON does not have integers, only "number", aka double-precision floats.
|
||||
if (compound_trigger_nodes.size() > 0 && !compound_trigger_nodes.has(double(self_index))) {
|
||||
// If the compound trigger we found is not the intended user of
|
||||
// this shape node, then we need to create a new Area3D node.
|
||||
ancestor_col_obj = memnew(Area3D);
|
||||
ret = ancestor_col_obj;
|
||||
}
|
||||
} else if (!Object::cast_to<PhysicsBody3D>(ancestor_col_obj)) {
|
||||
if (p_gltf_node->get_additional_data(StringName("GLTFPhysicsCompoundCollider"))) {
|
||||
// If the glTF file wants this node to group solid shapes together,
|
||||
// and there is no parent body, we need to create a static body.
|
||||
ancestor_col_obj = memnew(StaticBody3D);
|
||||
ret = ancestor_col_obj;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Add the shapes to the tree. When an ancestor body is present, use it.
|
||||
// If an explicit body was specified, it has already been generated and
|
||||
// set above. If there is no ancestor body, we will either generate an
|
||||
// Area3D or StaticBody3D implicitly, so prefer an Area3D as the base
|
||||
// node for best compatibility with signal connections to this node.
|
||||
bool is_ancestor_col_obj_solid = Object::cast_to<PhysicsBody3D>(ancestor_col_obj);
|
||||
if (is_ancestor_col_obj_solid && gltf_physics_collider_shape.is_valid()) {
|
||||
Node3D *child = _generate_shape_node_and_body_if_needed(p_state, p_gltf_node, gltf_physics_collider_shape, ancestor_col_obj, false);
|
||||
ret = _add_physics_node_to_given_node(ret, child, p_gltf_node);
|
||||
}
|
||||
if (gltf_physics_trigger_shape.is_valid()) {
|
||||
Node3D *child = _generate_shape_node_and_body_if_needed(p_state, p_gltf_node, gltf_physics_trigger_shape, ancestor_col_obj, true);
|
||||
ret = _add_physics_node_to_given_node(ret, child, p_gltf_node);
|
||||
}
|
||||
if (!is_ancestor_col_obj_solid && gltf_physics_collider_shape.is_valid()) {
|
||||
Node3D *child = _generate_shape_node_and_body_if_needed(p_state, p_gltf_node, gltf_physics_collider_shape, ancestor_col_obj, false);
|
||||
ret = _add_physics_node_to_given_node(ret, child, p_gltf_node);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Export process.
|
||||
bool _are_all_faces_equal(const Vector<Face3> &p_a, const Vector<Face3> &p_b) {
|
||||
if (p_a.size() != p_b.size()) {
|
||||
return false;
|
||||
}
|
||||
for (int i = 0; i < p_a.size(); i++) {
|
||||
const Vector3 *a_vertices = p_a[i].vertex;
|
||||
const Vector3 *b_vertices = p_b[i].vertex;
|
||||
for (int j = 0; j < 3; j++) {
|
||||
if (!a_vertices[j].is_equal_approx(b_vertices[j])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
GLTFMeshIndex _get_or_insert_mesh_in_state(Ref<GLTFState> p_state, Ref<ImporterMesh> p_mesh) {
|
||||
ERR_FAIL_COND_V(p_mesh.is_null(), -1);
|
||||
TypedArray<GLTFMesh> state_meshes = p_state->get_meshes();
|
||||
Vector<Face3> mesh_faces = p_mesh->get_faces();
|
||||
// De-duplication: If the state already has the mesh we need, use that one.
|
||||
for (GLTFMeshIndex i = 0; i < state_meshes.size(); i++) {
|
||||
Ref<GLTFMesh> state_gltf_mesh = state_meshes[i];
|
||||
ERR_CONTINUE(state_gltf_mesh.is_null());
|
||||
Ref<ImporterMesh> state_importer_mesh = state_gltf_mesh->get_mesh();
|
||||
ERR_CONTINUE(state_importer_mesh.is_null());
|
||||
if (state_importer_mesh == p_mesh) {
|
||||
return i;
|
||||
}
|
||||
if (_are_all_faces_equal(state_importer_mesh->get_faces(), mesh_faces)) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
// After the loop, we have checked that the mesh is not equal to any of the
|
||||
// meshes in the state. So we insert a new mesh into the state mesh array.
|
||||
Ref<GLTFMesh> gltf_mesh;
|
||||
gltf_mesh.instantiate();
|
||||
gltf_mesh->set_mesh(p_mesh);
|
||||
GLTFMeshIndex mesh_index = state_meshes.size();
|
||||
state_meshes.push_back(gltf_mesh);
|
||||
p_state->set_meshes(state_meshes);
|
||||
return mesh_index;
|
||||
}
|
||||
|
||||
void GLTFDocumentExtensionPhysics::convert_scene_node(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Node *p_scene_node) {
|
||||
if (cast_to<CollisionShape3D>(p_scene_node)) {
|
||||
CollisionShape3D *godot_shape = Object::cast_to<CollisionShape3D>(p_scene_node);
|
||||
Ref<GLTFPhysicsShape> gltf_shape = GLTFPhysicsShape::from_node(godot_shape);
|
||||
ERR_FAIL_COND_MSG(gltf_shape.is_null(), "glTF Physics: Could not convert CollisionShape3D to GLTFPhysicsShape. Does it have a valid Shape3D?");
|
||||
{
|
||||
Ref<ImporterMesh> importer_mesh = gltf_shape->get_importer_mesh();
|
||||
if (importer_mesh.is_valid()) {
|
||||
gltf_shape->set_mesh_index(_get_or_insert_mesh_in_state(p_state, importer_mesh));
|
||||
}
|
||||
}
|
||||
CollisionObject3D *ancestor_col_obj = _get_ancestor_collision_object(p_scene_node->get_parent());
|
||||
if (cast_to<Area3D>(ancestor_col_obj)) {
|
||||
p_gltf_node->set_additional_data(StringName("GLTFPhysicsTriggerShape"), gltf_shape);
|
||||
// Write explicit member shape nodes to the ancestor compound trigger node.
|
||||
TypedArray<GLTFNode> state_nodes = p_state->get_nodes();
|
||||
GLTFNodeIndex self_index = state_nodes.size(); // The current p_gltf_node will be inserted next.
|
||||
Array compound_trigger_nodes = _get_ancestor_compound_trigger_nodes(p_state, p_state->get_nodes(), ancestor_col_obj);
|
||||
compound_trigger_nodes.push_back(double(self_index));
|
||||
} else {
|
||||
p_gltf_node->set_additional_data(StringName("GLTFPhysicsColliderShape"), gltf_shape);
|
||||
}
|
||||
} else if (cast_to<CollisionObject3D>(p_scene_node)) {
|
||||
CollisionObject3D *godot_body = Object::cast_to<CollisionObject3D>(p_scene_node);
|
||||
p_gltf_node->set_additional_data(StringName("GLTFPhysicsBody"), GLTFPhysicsBody::from_node(godot_body));
|
||||
}
|
||||
}
|
||||
|
||||
Array _get_or_create_state_shapes_in_state(Ref<GLTFState> p_state) {
|
||||
Dictionary state_json = p_state->get_json();
|
||||
Dictionary state_extensions;
|
||||
if (state_json.has("extensions")) {
|
||||
state_extensions = state_json["extensions"];
|
||||
} else {
|
||||
state_json["extensions"] = state_extensions;
|
||||
}
|
||||
Dictionary omi_physics_shape_ext;
|
||||
if (state_extensions.has("OMI_physics_shape")) {
|
||||
omi_physics_shape_ext = state_extensions["OMI_physics_shape"];
|
||||
} else {
|
||||
state_extensions["OMI_physics_shape"] = omi_physics_shape_ext;
|
||||
p_state->add_used_extension("OMI_physics_shape");
|
||||
}
|
||||
Array state_shapes;
|
||||
if (omi_physics_shape_ext.has("shapes")) {
|
||||
state_shapes = omi_physics_shape_ext["shapes"];
|
||||
} else {
|
||||
omi_physics_shape_ext["shapes"] = state_shapes;
|
||||
}
|
||||
return state_shapes;
|
||||
}
|
||||
|
||||
GLTFShapeIndex _export_node_shape(Ref<GLTFState> p_state, Ref<GLTFPhysicsShape> p_physics_shape) {
|
||||
Array state_shapes = _get_or_create_state_shapes_in_state(p_state);
|
||||
GLTFShapeIndex size = state_shapes.size();
|
||||
Dictionary shape_property;
|
||||
Dictionary shape_dict = p_physics_shape->to_dictionary();
|
||||
for (GLTFShapeIndex i = 0; i < size; i++) {
|
||||
Dictionary other = state_shapes[i];
|
||||
if (other == shape_dict) {
|
||||
// De-duplication: If we already have an identical shape,
|
||||
// set the shape index to the existing one and return.
|
||||
return i;
|
||||
}
|
||||
}
|
||||
// If we don't have an identical shape, add it to the array.
|
||||
state_shapes.push_back(shape_dict);
|
||||
return size;
|
||||
}
|
||||
|
||||
Error GLTFDocumentExtensionPhysics::export_preserialize(Ref<GLTFState> p_state) {
|
||||
// Note: Need to do _export_node_shape before exporting animations, so export_node is too late.
|
||||
TypedArray<GLTFNode> state_gltf_nodes = p_state->get_nodes();
|
||||
for (Ref<GLTFNode> gltf_node : state_gltf_nodes) {
|
||||
Ref<GLTFPhysicsShape> collider_shape = gltf_node->get_additional_data(StringName("GLTFPhysicsColliderShape"));
|
||||
if (collider_shape.is_valid()) {
|
||||
GLTFShapeIndex collider_shape_index = _export_node_shape(p_state, collider_shape);
|
||||
gltf_node->set_additional_data(StringName("GLTFPhysicsColliderShapeIndex"), collider_shape_index);
|
||||
}
|
||||
Ref<GLTFPhysicsShape> trigger_shape = gltf_node->get_additional_data(StringName("GLTFPhysicsTriggerShape"));
|
||||
if (trigger_shape.is_valid()) {
|
||||
GLTFShapeIndex trigger_shape_index = _export_node_shape(p_state, trigger_shape);
|
||||
gltf_node->set_additional_data(StringName("GLTFPhysicsTriggerShapeIndex"), trigger_shape_index);
|
||||
}
|
||||
}
|
||||
return OK;
|
||||
}
|
||||
|
||||
Ref<GLTFObjectModelProperty> GLTFDocumentExtensionPhysics::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) {
|
||||
Ref<GLTFObjectModelProperty> ret;
|
||||
const Vector<StringName> &path_subnames = p_node_path.get_subnames();
|
||||
if (path_subnames.is_empty()) {
|
||||
return ret;
|
||||
}
|
||||
ret.instantiate();
|
||||
const StringName &node_prop = path_subnames[0];
|
||||
if (Object::cast_to<RigidBody3D>(p_target_object)) {
|
||||
if (path_subnames.size() != 1) {
|
||||
return ret;
|
||||
}
|
||||
// Example: `/nodes/0/extensions/OMI_physics_body/motion/mass`
|
||||
PackedStringArray split_json_pointer;
|
||||
split_json_pointer.append("nodes");
|
||||
split_json_pointer.append(itos(p_gltf_node_index));
|
||||
split_json_pointer.append("extensions");
|
||||
split_json_pointer.append("OMI_physics_body");
|
||||
split_json_pointer.append("motion");
|
||||
if (node_prop == StringName("mass")) {
|
||||
split_json_pointer.append("mass");
|
||||
ret->set_types(Variant::FLOAT, GLTFObjectModelProperty::GLTF_OBJECT_MODEL_TYPE_FLOAT);
|
||||
} else if (node_prop == StringName("linear_velocity")) {
|
||||
split_json_pointer.append("linearVelocity");
|
||||
ret->set_types(Variant::VECTOR3, GLTFObjectModelProperty::GLTF_OBJECT_MODEL_TYPE_FLOAT3);
|
||||
} else if (node_prop == StringName("angular_velocity")) {
|
||||
split_json_pointer.append("angularVelocity");
|
||||
ret->set_types(Variant::VECTOR3, GLTFObjectModelProperty::GLTF_OBJECT_MODEL_TYPE_FLOAT3);
|
||||
} else if (node_prop == StringName("center_of_mass")) {
|
||||
split_json_pointer.append("centerOfMass");
|
||||
ret->set_types(Variant::VECTOR3, GLTFObjectModelProperty::GLTF_OBJECT_MODEL_TYPE_FLOAT3);
|
||||
} else if (node_prop == StringName("inertia")) {
|
||||
split_json_pointer.append("inertiaDiagonal");
|
||||
ret->set_types(Variant::VECTOR3, GLTFObjectModelProperty::GLTF_OBJECT_MODEL_TYPE_FLOAT3);
|
||||
} else {
|
||||
// Not something we handle, return without setting the JSON pointer.
|
||||
return ret;
|
||||
}
|
||||
ret->set_json_pointers({ split_json_pointer });
|
||||
} else if (Object::cast_to<CollisionShape3D>(p_godot_node)) {
|
||||
if (path_subnames.size() != 2) {
|
||||
return ret;
|
||||
}
|
||||
// Example: `/extensions/OMI_physics_shape/shapes/0/box/size`
|
||||
PackedStringArray split_json_pointer;
|
||||
split_json_pointer.append("extensions");
|
||||
split_json_pointer.append("OMI_physics_shape");
|
||||
split_json_pointer.append("shapes");
|
||||
TypedArray<GLTFNode> state_gltf_nodes = p_state->get_nodes();
|
||||
ERR_FAIL_INDEX_V(p_gltf_node_index, state_gltf_nodes.size(), ret);
|
||||
Ref<GLTFNode> gltf_node = state_gltf_nodes[p_gltf_node_index];
|
||||
Variant shape_index_maybe = gltf_node->get_additional_data(StringName("GLTFPhysicsColliderShapeIndex"));
|
||||
String shape_type;
|
||||
if (shape_index_maybe.get_type() == Variant::INT) {
|
||||
Ref<GLTFPhysicsShape> collider_shape = gltf_node->get_additional_data(StringName("GLTFPhysicsColliderShape"));
|
||||
shape_type = collider_shape->get_shape_type();
|
||||
} else {
|
||||
shape_index_maybe = gltf_node->get_additional_data(StringName("GLTFPhysicsTriggerShapeIndex"));
|
||||
if (shape_index_maybe.get_type() == Variant::INT) {
|
||||
Ref<GLTFPhysicsShape> trigger_shape = gltf_node->get_additional_data(StringName("GLTFPhysicsTriggerShape"));
|
||||
shape_type = trigger_shape->get_shape_type();
|
||||
}
|
||||
}
|
||||
ERR_FAIL_COND_V(shape_index_maybe.get_type() != Variant::INT, ret);
|
||||
GLTFShapeIndex shape_index = shape_index_maybe;
|
||||
split_json_pointer.append(itos(shape_index));
|
||||
split_json_pointer.append(shape_type);
|
||||
const StringName &shape_prop = path_subnames[1];
|
||||
if (shape_prop == StringName("size")) {
|
||||
split_json_pointer.append("size");
|
||||
ret->set_types(Variant::VECTOR3, GLTFObjectModelProperty::GLTF_OBJECT_MODEL_TYPE_FLOAT3);
|
||||
} else if (shape_prop == StringName("radius")) {
|
||||
split_json_pointer.append("radius");
|
||||
ret->set_types(Variant::FLOAT, GLTFObjectModelProperty::GLTF_OBJECT_MODEL_TYPE_FLOAT);
|
||||
} else if (shape_prop == StringName("height")) {
|
||||
split_json_pointer.append("height");
|
||||
ret->set_types(Variant::FLOAT, GLTFObjectModelProperty::GLTF_OBJECT_MODEL_TYPE_FLOAT);
|
||||
} else {
|
||||
// Not something we handle, return without setting the JSON pointer.
|
||||
return ret;
|
||||
}
|
||||
ret->set_json_pointers({ split_json_pointer });
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
Error GLTFDocumentExtensionPhysics::export_node(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Dictionary &r_node_json, Node *p_node) {
|
||||
Dictionary physics_body_ext;
|
||||
Ref<GLTFPhysicsBody> physics_body = p_gltf_node->get_additional_data(StringName("GLTFPhysicsBody"));
|
||||
if (physics_body.is_valid()) {
|
||||
physics_body_ext = physics_body->to_dictionary();
|
||||
Variant compound_trigger_nodes = p_gltf_node->get_additional_data(StringName("GLTFPhysicsCompoundTriggerNodes"));
|
||||
if (compound_trigger_nodes.is_array()) {
|
||||
Dictionary trigger_property = physics_body_ext.get_or_add("trigger", {});
|
||||
trigger_property["nodes"] = compound_trigger_nodes;
|
||||
}
|
||||
}
|
||||
Variant collider_shape_index = p_gltf_node->get_additional_data(StringName("GLTFPhysicsColliderShapeIndex"));
|
||||
if (collider_shape_index.get_type() == Variant::INT) {
|
||||
Dictionary collider_dict;
|
||||
collider_dict["shape"] = collider_shape_index;
|
||||
physics_body_ext["collider"] = collider_dict;
|
||||
}
|
||||
Variant trigger_shape_index = p_gltf_node->get_additional_data(StringName("GLTFPhysicsTriggerShapeIndex"));
|
||||
if (trigger_shape_index.get_type() == Variant::INT) {
|
||||
Dictionary trigger_dict = physics_body_ext.get_or_add("trigger", {});
|
||||
trigger_dict["shape"] = trigger_shape_index;
|
||||
}
|
||||
if (!physics_body_ext.is_empty()) {
|
||||
Dictionary node_extensions = r_node_json["extensions"];
|
||||
node_extensions["OMI_physics_body"] = physics_body_ext;
|
||||
p_state->add_used_extension("OMI_physics_body");
|
||||
}
|
||||
return OK;
|
||||
}
|
@@ -0,0 +1,52 @@
|
||||
/**************************************************************************/
|
||||
/* gltf_document_extension_physics.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../gltf_document_extension.h"
|
||||
#include "gltf_physics_body.h"
|
||||
#include "gltf_physics_shape.h"
|
||||
|
||||
class GLTFDocumentExtensionPhysics : public GLTFDocumentExtension {
|
||||
GDCLASS(GLTFDocumentExtensionPhysics, GLTFDocumentExtension);
|
||||
|
||||
public:
|
||||
// Import process.
|
||||
Error import_preflight(Ref<GLTFState> p_state, Vector<String> p_extensions) override;
|
||||
Vector<String> get_supported_extensions() override;
|
||||
Error parse_node_extensions(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Dictionary &p_extensions) override;
|
||||
Ref<GLTFObjectModelProperty> import_object_model_property(Ref<GLTFState> p_state, const PackedStringArray &p_split_json_pointer, const TypedArray<NodePath> &p_partial_paths) override;
|
||||
Node3D *generate_scene_node(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Node *p_scene_parent) override;
|
||||
// Export process.
|
||||
void convert_scene_node(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Node *p_scene_node) override;
|
||||
Error export_preserialize(Ref<GLTFState> p_state) override;
|
||||
Ref<GLTFObjectModelProperty> 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) override;
|
||||
Error export_node(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Dictionary &r_node_json, Node *p_scene_node) override;
|
||||
};
|
405
modules/gltf/extensions/physics/gltf_physics_body.cpp
Normal file
405
modules/gltf/extensions/physics/gltf_physics_body.cpp
Normal file
@@ -0,0 +1,405 @@
|
||||
/**************************************************************************/
|
||||
/* gltf_physics_body.cpp */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#include "gltf_physics_body.h"
|
||||
|
||||
#include "scene/3d/physics/animatable_body_3d.h"
|
||||
#include "scene/3d/physics/area_3d.h"
|
||||
#include "scene/3d/physics/character_body_3d.h"
|
||||
#include "scene/3d/physics/static_body_3d.h"
|
||||
#include "scene/3d/physics/vehicle_body_3d.h"
|
||||
|
||||
void GLTFPhysicsBody::_bind_methods() {
|
||||
ClassDB::bind_static_method("GLTFPhysicsBody", D_METHOD("from_node", "body_node"), &GLTFPhysicsBody::from_node);
|
||||
ClassDB::bind_method(D_METHOD("to_node"), &GLTFPhysicsBody::to_node);
|
||||
|
||||
ClassDB::bind_static_method("GLTFPhysicsBody", D_METHOD("from_dictionary", "dictionary"), &GLTFPhysicsBody::from_dictionary);
|
||||
ClassDB::bind_method(D_METHOD("to_dictionary"), &GLTFPhysicsBody::to_dictionary);
|
||||
|
||||
ClassDB::bind_method(D_METHOD("get_body_type"), &GLTFPhysicsBody::get_body_type);
|
||||
ClassDB::bind_method(D_METHOD("set_body_type", "body_type"), &GLTFPhysicsBody::set_body_type);
|
||||
ClassDB::bind_method(D_METHOD("get_mass"), &GLTFPhysicsBody::get_mass);
|
||||
ClassDB::bind_method(D_METHOD("set_mass", "mass"), &GLTFPhysicsBody::set_mass);
|
||||
ClassDB::bind_method(D_METHOD("get_linear_velocity"), &GLTFPhysicsBody::get_linear_velocity);
|
||||
ClassDB::bind_method(D_METHOD("set_linear_velocity", "linear_velocity"), &GLTFPhysicsBody::set_linear_velocity);
|
||||
ClassDB::bind_method(D_METHOD("get_angular_velocity"), &GLTFPhysicsBody::get_angular_velocity);
|
||||
ClassDB::bind_method(D_METHOD("set_angular_velocity", "angular_velocity"), &GLTFPhysicsBody::set_angular_velocity);
|
||||
ClassDB::bind_method(D_METHOD("get_center_of_mass"), &GLTFPhysicsBody::get_center_of_mass);
|
||||
ClassDB::bind_method(D_METHOD("set_center_of_mass", "center_of_mass"), &GLTFPhysicsBody::set_center_of_mass);
|
||||
ClassDB::bind_method(D_METHOD("get_inertia_diagonal"), &GLTFPhysicsBody::get_inertia_diagonal);
|
||||
ClassDB::bind_method(D_METHOD("set_inertia_diagonal", "inertia_diagonal"), &GLTFPhysicsBody::set_inertia_diagonal);
|
||||
ClassDB::bind_method(D_METHOD("get_inertia_orientation"), &GLTFPhysicsBody::get_inertia_orientation);
|
||||
ClassDB::bind_method(D_METHOD("set_inertia_orientation", "inertia_orientation"), &GLTFPhysicsBody::set_inertia_orientation);
|
||||
#ifndef DISABLE_DEPRECATED
|
||||
ClassDB::bind_method(D_METHOD("get_inertia_tensor"), &GLTFPhysicsBody::get_inertia_tensor);
|
||||
ClassDB::bind_method(D_METHOD("set_inertia_tensor", "inertia_tensor"), &GLTFPhysicsBody::set_inertia_tensor);
|
||||
#endif // DISABLE_DEPRECATED
|
||||
|
||||
ADD_PROPERTY(PropertyInfo(Variant::STRING, "body_type"), "set_body_type", "get_body_type");
|
||||
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "mass"), "set_mass", "get_mass");
|
||||
ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "linear_velocity"), "set_linear_velocity", "get_linear_velocity");
|
||||
ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "angular_velocity"), "set_angular_velocity", "get_angular_velocity");
|
||||
ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "center_of_mass"), "set_center_of_mass", "get_center_of_mass");
|
||||
ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "inertia_diagonal"), "set_inertia_diagonal", "get_inertia_diagonal");
|
||||
ADD_PROPERTY(PropertyInfo(Variant::QUATERNION, "inertia_orientation"), "set_inertia_orientation", "get_inertia_orientation");
|
||||
#ifndef DISABLE_DEPRECATED
|
||||
ADD_PROPERTY(PropertyInfo(Variant::BASIS, "inertia_tensor"), "set_inertia_tensor", "get_inertia_tensor");
|
||||
#endif // DISABLE_DEPRECATED
|
||||
}
|
||||
|
||||
String GLTFPhysicsBody::get_body_type() const {
|
||||
switch (body_type) {
|
||||
case PhysicsBodyType::STATIC:
|
||||
return "static";
|
||||
case PhysicsBodyType::ANIMATABLE:
|
||||
return "animatable";
|
||||
case PhysicsBodyType::CHARACTER:
|
||||
return "character";
|
||||
case PhysicsBodyType::RIGID:
|
||||
return "rigid";
|
||||
case PhysicsBodyType::VEHICLE:
|
||||
return "vehicle";
|
||||
case PhysicsBodyType::TRIGGER:
|
||||
return "trigger";
|
||||
}
|
||||
// Unreachable, the switch cases handle all values the enum can take.
|
||||
// Omitting this works on Clang but not GCC or MSVC. If reached, it's UB.
|
||||
return "rigid";
|
||||
}
|
||||
|
||||
void GLTFPhysicsBody::set_body_type(String p_body_type) {
|
||||
if (p_body_type == "static") {
|
||||
body_type = PhysicsBodyType::STATIC;
|
||||
} else if (p_body_type == "animatable") {
|
||||
body_type = PhysicsBodyType::ANIMATABLE;
|
||||
} else if (p_body_type == "character") {
|
||||
body_type = PhysicsBodyType::CHARACTER;
|
||||
} else if (p_body_type == "rigid") {
|
||||
body_type = PhysicsBodyType::RIGID;
|
||||
} else if (p_body_type == "vehicle") {
|
||||
body_type = PhysicsBodyType::VEHICLE;
|
||||
} else if (p_body_type == "trigger") {
|
||||
body_type = PhysicsBodyType::TRIGGER;
|
||||
} else {
|
||||
ERR_PRINT("Error setting glTF physics body type: The body type must be one of \"static\", \"animatable\", \"character\", \"rigid\", \"vehicle\", or \"trigger\".");
|
||||
}
|
||||
}
|
||||
|
||||
GLTFPhysicsBody::PhysicsBodyType GLTFPhysicsBody::get_physics_body_type() const {
|
||||
return body_type;
|
||||
}
|
||||
|
||||
void GLTFPhysicsBody::set_physics_body_type(PhysicsBodyType p_body_type) {
|
||||
body_type = p_body_type;
|
||||
}
|
||||
|
||||
real_t GLTFPhysicsBody::get_mass() const {
|
||||
return mass;
|
||||
}
|
||||
|
||||
void GLTFPhysicsBody::set_mass(real_t p_mass) {
|
||||
mass = p_mass;
|
||||
}
|
||||
|
||||
Vector3 GLTFPhysicsBody::get_linear_velocity() const {
|
||||
return linear_velocity;
|
||||
}
|
||||
|
||||
void GLTFPhysicsBody::set_linear_velocity(Vector3 p_linear_velocity) {
|
||||
linear_velocity = p_linear_velocity;
|
||||
}
|
||||
|
||||
Vector3 GLTFPhysicsBody::get_angular_velocity() const {
|
||||
return angular_velocity;
|
||||
}
|
||||
|
||||
void GLTFPhysicsBody::set_angular_velocity(Vector3 p_angular_velocity) {
|
||||
angular_velocity = p_angular_velocity;
|
||||
}
|
||||
|
||||
Vector3 GLTFPhysicsBody::get_center_of_mass() const {
|
||||
return center_of_mass;
|
||||
}
|
||||
|
||||
void GLTFPhysicsBody::set_center_of_mass(const Vector3 &p_center_of_mass) {
|
||||
center_of_mass = p_center_of_mass;
|
||||
}
|
||||
|
||||
Vector3 GLTFPhysicsBody::get_inertia_diagonal() const {
|
||||
return inertia_diagonal;
|
||||
}
|
||||
|
||||
void GLTFPhysicsBody::set_inertia_diagonal(const Vector3 &p_inertia_diagonal) {
|
||||
inertia_diagonal = p_inertia_diagonal;
|
||||
}
|
||||
|
||||
Quaternion GLTFPhysicsBody::get_inertia_orientation() const {
|
||||
return inertia_orientation;
|
||||
}
|
||||
|
||||
void GLTFPhysicsBody::set_inertia_orientation(const Quaternion &p_inertia_orientation) {
|
||||
inertia_orientation = p_inertia_orientation;
|
||||
}
|
||||
|
||||
#ifndef DISABLE_DEPRECATED
|
||||
Basis GLTFPhysicsBody::get_inertia_tensor() const {
|
||||
return Basis::from_scale(inertia_diagonal);
|
||||
}
|
||||
|
||||
void GLTFPhysicsBody::set_inertia_tensor(Basis p_inertia_tensor) {
|
||||
inertia_diagonal = p_inertia_tensor.get_main_diagonal();
|
||||
}
|
||||
#endif // DISABLE_DEPRECATED
|
||||
|
||||
Ref<GLTFPhysicsBody> GLTFPhysicsBody::from_node(const CollisionObject3D *p_body_node) {
|
||||
Ref<GLTFPhysicsBody> physics_body;
|
||||
physics_body.instantiate();
|
||||
ERR_FAIL_NULL_V_MSG(p_body_node, physics_body, "Tried to create a GLTFPhysicsBody from a CollisionObject3D node, but the given node was null.");
|
||||
if (cast_to<CharacterBody3D>(p_body_node)) {
|
||||
physics_body->body_type = PhysicsBodyType::CHARACTER;
|
||||
} else if (cast_to<AnimatableBody3D>(p_body_node)) {
|
||||
physics_body->body_type = PhysicsBodyType::ANIMATABLE;
|
||||
} else if (cast_to<RigidBody3D>(p_body_node)) {
|
||||
const RigidBody3D *body = cast_to<const RigidBody3D>(p_body_node);
|
||||
physics_body->mass = body->get_mass();
|
||||
physics_body->linear_velocity = body->get_linear_velocity();
|
||||
physics_body->angular_velocity = body->get_angular_velocity();
|
||||
physics_body->center_of_mass = body->get_center_of_mass();
|
||||
physics_body->inertia_diagonal = body->get_inertia();
|
||||
if (cast_to<VehicleBody3D>(p_body_node)) {
|
||||
physics_body->body_type = PhysicsBodyType::VEHICLE;
|
||||
} else {
|
||||
physics_body->body_type = PhysicsBodyType::RIGID;
|
||||
}
|
||||
} else if (cast_to<StaticBody3D>(p_body_node)) {
|
||||
physics_body->body_type = PhysicsBodyType::STATIC;
|
||||
} else if (cast_to<Area3D>(p_body_node)) {
|
||||
physics_body->body_type = PhysicsBodyType::TRIGGER;
|
||||
}
|
||||
return physics_body;
|
||||
}
|
||||
|
||||
CollisionObject3D *GLTFPhysicsBody::to_node() const {
|
||||
switch (body_type) {
|
||||
case PhysicsBodyType::CHARACTER: {
|
||||
CharacterBody3D *body = memnew(CharacterBody3D);
|
||||
return body;
|
||||
}
|
||||
case PhysicsBodyType::ANIMATABLE: {
|
||||
AnimatableBody3D *body = memnew(AnimatableBody3D);
|
||||
return body;
|
||||
}
|
||||
case PhysicsBodyType::VEHICLE: {
|
||||
VehicleBody3D *body = memnew(VehicleBody3D);
|
||||
body->set_mass(mass);
|
||||
body->set_linear_velocity(linear_velocity);
|
||||
body->set_angular_velocity(angular_velocity);
|
||||
body->set_inertia(inertia_diagonal);
|
||||
body->set_center_of_mass_mode(RigidBody3D::CENTER_OF_MASS_MODE_CUSTOM);
|
||||
body->set_center_of_mass(center_of_mass);
|
||||
return body;
|
||||
}
|
||||
case PhysicsBodyType::RIGID: {
|
||||
RigidBody3D *body = memnew(RigidBody3D);
|
||||
body->set_mass(mass);
|
||||
body->set_linear_velocity(linear_velocity);
|
||||
body->set_angular_velocity(angular_velocity);
|
||||
body->set_inertia(inertia_diagonal);
|
||||
body->set_center_of_mass_mode(RigidBody3D::CENTER_OF_MASS_MODE_CUSTOM);
|
||||
body->set_center_of_mass(center_of_mass);
|
||||
return body;
|
||||
}
|
||||
case PhysicsBodyType::STATIC: {
|
||||
StaticBody3D *body = memnew(StaticBody3D);
|
||||
return body;
|
||||
}
|
||||
case PhysicsBodyType::TRIGGER: {
|
||||
Area3D *body = memnew(Area3D);
|
||||
return body;
|
||||
}
|
||||
}
|
||||
// Unreachable, the switch cases handle all values the enum can take.
|
||||
// Omitting this works on Clang but not GCC or MSVC. If reached, it's UB.
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Ref<GLTFPhysicsBody> GLTFPhysicsBody::from_dictionary(const Dictionary p_dictionary) {
|
||||
Ref<GLTFPhysicsBody> physics_body;
|
||||
physics_body.instantiate();
|
||||
Dictionary motion;
|
||||
if (p_dictionary.has("motion")) {
|
||||
motion = p_dictionary["motion"];
|
||||
#ifndef DISABLE_DEPRECATED
|
||||
} else {
|
||||
motion = p_dictionary;
|
||||
#endif // DISABLE_DEPRECATED
|
||||
}
|
||||
if (motion.has("type")) {
|
||||
// Read the body type. This representation sits between glTF's and Godot's physics nodes.
|
||||
// While we may only read "static", "kinematic", or "dynamic" from a valid glTF file, we
|
||||
// want to allow another extension to override this to another Godot node type mid-import.
|
||||
// For example, a vehicle extension may want to override the body type to "vehicle"
|
||||
// so Godot generates a VehicleBody3D node. Therefore we distinguish by importing
|
||||
// "dynamic" as "rigid", and "kinematic" as "animatable", in the GLTFPhysicsBody code.
|
||||
String body_type_string = motion["type"];
|
||||
if (body_type_string == "static") {
|
||||
physics_body->body_type = PhysicsBodyType::STATIC;
|
||||
} else if (body_type_string == "kinematic") {
|
||||
physics_body->body_type = PhysicsBodyType::ANIMATABLE;
|
||||
} else if (body_type_string == "dynamic") {
|
||||
physics_body->body_type = PhysicsBodyType::RIGID;
|
||||
#ifndef DISABLE_DEPRECATED
|
||||
} else if (body_type_string == "character") {
|
||||
physics_body->body_type = PhysicsBodyType::CHARACTER;
|
||||
} else if (body_type_string == "rigid") {
|
||||
physics_body->body_type = PhysicsBodyType::RIGID;
|
||||
} else if (body_type_string == "vehicle") {
|
||||
physics_body->body_type = PhysicsBodyType::VEHICLE;
|
||||
} else if (body_type_string == "trigger") {
|
||||
physics_body->body_type = PhysicsBodyType::TRIGGER;
|
||||
#endif // DISABLE_DEPRECATED
|
||||
} else {
|
||||
ERR_PRINT("Error parsing glTF physics body: The body type in the glTF file \"" + body_type_string + "\" was not recognized.");
|
||||
}
|
||||
}
|
||||
if (motion.has("mass")) {
|
||||
physics_body->mass = motion["mass"];
|
||||
}
|
||||
if (motion.has("linearVelocity")) {
|
||||
const Array &arr = motion["linearVelocity"];
|
||||
if (arr.size() == 3) {
|
||||
physics_body->set_linear_velocity(Vector3(arr[0], arr[1], arr[2]));
|
||||
} else {
|
||||
ERR_PRINT("Error parsing glTF physics body: The linear velocity vector must have exactly 3 numbers.");
|
||||
}
|
||||
}
|
||||
if (motion.has("angularVelocity")) {
|
||||
const Array &arr = motion["angularVelocity"];
|
||||
if (arr.size() == 3) {
|
||||
physics_body->set_angular_velocity(Vector3(arr[0], arr[1], arr[2]));
|
||||
} else {
|
||||
ERR_PRINT("Error parsing glTF physics body: The angular velocity vector must have exactly 3 numbers.");
|
||||
}
|
||||
}
|
||||
if (motion.has("centerOfMass")) {
|
||||
const Array &arr = motion["centerOfMass"];
|
||||
if (arr.size() == 3) {
|
||||
physics_body->set_center_of_mass(Vector3(arr[0], arr[1], arr[2]));
|
||||
} else {
|
||||
ERR_PRINT("Error parsing glTF physics body: The center of mass vector must have exactly 3 numbers.");
|
||||
}
|
||||
}
|
||||
if (motion.has("inertiaDiagonal")) {
|
||||
const Array &arr = motion["inertiaDiagonal"];
|
||||
if (arr.size() == 3) {
|
||||
physics_body->set_inertia_diagonal(Vector3(arr[0], arr[1], arr[2]));
|
||||
} else {
|
||||
ERR_PRINT("Error parsing glTF physics body: The inertia diagonal vector must have exactly 3 numbers.");
|
||||
}
|
||||
}
|
||||
if (motion.has("inertiaOrientation")) {
|
||||
const Array &arr = motion["inertiaOrientation"];
|
||||
if (arr.size() == 4) {
|
||||
physics_body->set_inertia_orientation(Quaternion(arr[0], arr[1], arr[2], arr[3]));
|
||||
} else {
|
||||
ERR_PRINT("Error parsing glTF physics body: The inertia orientation quaternion must have exactly 4 numbers.");
|
||||
}
|
||||
}
|
||||
return physics_body;
|
||||
}
|
||||
|
||||
Dictionary GLTFPhysicsBody::to_dictionary() const {
|
||||
Dictionary ret;
|
||||
if (body_type == PhysicsBodyType::TRIGGER) {
|
||||
// The equivalent of a Godot Area3D node in glTF is a node that
|
||||
// defines that it is a trigger, but does not have a shape.
|
||||
Dictionary trigger;
|
||||
ret["trigger"] = trigger;
|
||||
return ret;
|
||||
}
|
||||
// All non-trigger body types are defined using the motion property.
|
||||
Dictionary motion;
|
||||
// When stored in memory, the body type can correspond to a Godot
|
||||
// node type. However, when exporting to glTF, we need to squash
|
||||
// this down to one of "static", "kinematic", or "dynamic".
|
||||
if (body_type == PhysicsBodyType::STATIC) {
|
||||
motion["type"] = "static";
|
||||
} else if (body_type == PhysicsBodyType::ANIMATABLE || body_type == PhysicsBodyType::CHARACTER) {
|
||||
motion["type"] = "kinematic";
|
||||
} else {
|
||||
motion["type"] = "dynamic";
|
||||
}
|
||||
if (mass != 1.0) {
|
||||
motion["mass"] = mass;
|
||||
}
|
||||
if (linear_velocity != Vector3()) {
|
||||
Array velocity_array;
|
||||
velocity_array.resize(3);
|
||||
velocity_array[0] = linear_velocity.x;
|
||||
velocity_array[1] = linear_velocity.y;
|
||||
velocity_array[2] = linear_velocity.z;
|
||||
motion["linearVelocity"] = velocity_array;
|
||||
}
|
||||
if (angular_velocity != Vector3()) {
|
||||
Array velocity_array;
|
||||
velocity_array.resize(3);
|
||||
velocity_array[0] = angular_velocity.x;
|
||||
velocity_array[1] = angular_velocity.y;
|
||||
velocity_array[2] = angular_velocity.z;
|
||||
motion["angularVelocity"] = velocity_array;
|
||||
}
|
||||
if (center_of_mass != Vector3()) {
|
||||
Array center_of_mass_array;
|
||||
center_of_mass_array.resize(3);
|
||||
center_of_mass_array[0] = center_of_mass.x;
|
||||
center_of_mass_array[1] = center_of_mass.y;
|
||||
center_of_mass_array[2] = center_of_mass.z;
|
||||
motion["centerOfMass"] = center_of_mass_array;
|
||||
}
|
||||
if (inertia_diagonal != Vector3()) {
|
||||
Array inertia_array;
|
||||
inertia_array.resize(3);
|
||||
inertia_array[0] = inertia_diagonal[0];
|
||||
inertia_array[1] = inertia_diagonal[1];
|
||||
inertia_array[2] = inertia_diagonal[2];
|
||||
motion["inertiaDiagonal"] = inertia_array;
|
||||
}
|
||||
if (inertia_orientation != Quaternion()) {
|
||||
Array inertia_array;
|
||||
inertia_array.resize(4);
|
||||
inertia_array[0] = inertia_orientation[0];
|
||||
inertia_array[1] = inertia_orientation[1];
|
||||
inertia_array[2] = inertia_orientation[2];
|
||||
inertia_array[3] = inertia_orientation[3];
|
||||
motion["inertiaDiagonal"] = inertia_array;
|
||||
}
|
||||
ret["motion"] = motion;
|
||||
return ret;
|
||||
}
|
104
modules/gltf/extensions/physics/gltf_physics_body.h
Normal file
104
modules/gltf/extensions/physics/gltf_physics_body.h
Normal file
@@ -0,0 +1,104 @@
|
||||
/**************************************************************************/
|
||||
/* gltf_physics_body.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "scene/3d/physics/physics_body_3d.h"
|
||||
|
||||
// GLTFPhysicsBody is an intermediary between Godot's physics body nodes
|
||||
// and the OMI_physics_body extension.
|
||||
// https://github.com/omigroup/gltf-extensions/tree/main/extensions/2.0/OMI_physics_body
|
||||
|
||||
class GLTFPhysicsBody : public Resource {
|
||||
GDCLASS(GLTFPhysicsBody, Resource)
|
||||
|
||||
public:
|
||||
// These values map to Godot's physics body types.
|
||||
// When importing, the body type will be set to the closest match, and
|
||||
// user code can change this to make Godot generate a different node type.
|
||||
// When exporting, this will be squashed down to one of "static",
|
||||
// "kinematic", or "dynamic" motion types, or the "trigger" property.
|
||||
enum class PhysicsBodyType {
|
||||
STATIC,
|
||||
ANIMATABLE,
|
||||
CHARACTER,
|
||||
RIGID,
|
||||
VEHICLE,
|
||||
TRIGGER,
|
||||
};
|
||||
|
||||
protected:
|
||||
static void _bind_methods();
|
||||
|
||||
private:
|
||||
PhysicsBodyType body_type = PhysicsBodyType::RIGID;
|
||||
real_t mass = 1.0;
|
||||
Vector3 linear_velocity;
|
||||
Vector3 angular_velocity;
|
||||
Vector3 center_of_mass;
|
||||
Vector3 inertia_diagonal;
|
||||
Quaternion inertia_orientation;
|
||||
|
||||
public:
|
||||
String get_body_type() const;
|
||||
void set_body_type(String p_body_type);
|
||||
|
||||
PhysicsBodyType get_physics_body_type() const;
|
||||
void set_physics_body_type(PhysicsBodyType p_body_type);
|
||||
|
||||
real_t get_mass() const;
|
||||
void set_mass(real_t p_mass);
|
||||
|
||||
Vector3 get_linear_velocity() const;
|
||||
void set_linear_velocity(Vector3 p_linear_velocity);
|
||||
|
||||
Vector3 get_angular_velocity() const;
|
||||
void set_angular_velocity(Vector3 p_angular_velocity);
|
||||
|
||||
Vector3 get_center_of_mass() const;
|
||||
void set_center_of_mass(const Vector3 &p_center_of_mass);
|
||||
|
||||
Vector3 get_inertia_diagonal() const;
|
||||
void set_inertia_diagonal(const Vector3 &p_inertia_diagonal);
|
||||
|
||||
Quaternion get_inertia_orientation() const;
|
||||
void set_inertia_orientation(const Quaternion &p_inertia_orientation);
|
||||
|
||||
#ifndef DISABLE_DEPRECATED
|
||||
Basis get_inertia_tensor() const;
|
||||
void set_inertia_tensor(Basis p_inertia_tensor);
|
||||
#endif // DISABLE_DEPRECATED
|
||||
|
||||
static Ref<GLTFPhysicsBody> from_node(const CollisionObject3D *p_body_node);
|
||||
CollisionObject3D *to_node() const;
|
||||
|
||||
static Ref<GLTFPhysicsBody> from_dictionary(const Dictionary p_dictionary);
|
||||
Dictionary to_dictionary() const;
|
||||
};
|
338
modules/gltf/extensions/physics/gltf_physics_shape.cpp
Normal file
338
modules/gltf/extensions/physics/gltf_physics_shape.cpp
Normal file
@@ -0,0 +1,338 @@
|
||||
/**************************************************************************/
|
||||
/* gltf_physics_shape.cpp */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#include "gltf_physics_shape.h"
|
||||
|
||||
#include "../../gltf_state.h"
|
||||
|
||||
#include "core/math/convex_hull.h"
|
||||
#include "scene/3d/physics/area_3d.h"
|
||||
#include "scene/resources/3d/box_shape_3d.h"
|
||||
#include "scene/resources/3d/capsule_shape_3d.h"
|
||||
#include "scene/resources/3d/concave_polygon_shape_3d.h"
|
||||
#include "scene/resources/3d/convex_polygon_shape_3d.h"
|
||||
#include "scene/resources/3d/cylinder_shape_3d.h"
|
||||
#include "scene/resources/3d/importer_mesh.h"
|
||||
#include "scene/resources/3d/sphere_shape_3d.h"
|
||||
|
||||
void GLTFPhysicsShape::_bind_methods() {
|
||||
ClassDB::bind_static_method("GLTFPhysicsShape", D_METHOD("from_node", "shape_node"), &GLTFPhysicsShape::from_node);
|
||||
ClassDB::bind_method(D_METHOD("to_node", "cache_shapes"), &GLTFPhysicsShape::to_node, DEFVAL(false));
|
||||
|
||||
ClassDB::bind_static_method("GLTFPhysicsShape", D_METHOD("from_resource", "shape_resource"), &GLTFPhysicsShape::from_resource);
|
||||
ClassDB::bind_method(D_METHOD("to_resource", "cache_shapes"), &GLTFPhysicsShape::to_resource, DEFVAL(false));
|
||||
|
||||
ClassDB::bind_static_method("GLTFPhysicsShape", D_METHOD("from_dictionary", "dictionary"), &GLTFPhysicsShape::from_dictionary);
|
||||
ClassDB::bind_method(D_METHOD("to_dictionary"), &GLTFPhysicsShape::to_dictionary);
|
||||
|
||||
ClassDB::bind_method(D_METHOD("get_shape_type"), &GLTFPhysicsShape::get_shape_type);
|
||||
ClassDB::bind_method(D_METHOD("set_shape_type", "shape_type"), &GLTFPhysicsShape::set_shape_type);
|
||||
ClassDB::bind_method(D_METHOD("get_size"), &GLTFPhysicsShape::get_size);
|
||||
ClassDB::bind_method(D_METHOD("set_size", "size"), &GLTFPhysicsShape::set_size);
|
||||
ClassDB::bind_method(D_METHOD("get_radius"), &GLTFPhysicsShape::get_radius);
|
||||
ClassDB::bind_method(D_METHOD("set_radius", "radius"), &GLTFPhysicsShape::set_radius);
|
||||
ClassDB::bind_method(D_METHOD("get_height"), &GLTFPhysicsShape::get_height);
|
||||
ClassDB::bind_method(D_METHOD("set_height", "height"), &GLTFPhysicsShape::set_height);
|
||||
ClassDB::bind_method(D_METHOD("get_is_trigger"), &GLTFPhysicsShape::get_is_trigger);
|
||||
ClassDB::bind_method(D_METHOD("set_is_trigger", "is_trigger"), &GLTFPhysicsShape::set_is_trigger);
|
||||
ClassDB::bind_method(D_METHOD("get_mesh_index"), &GLTFPhysicsShape::get_mesh_index);
|
||||
ClassDB::bind_method(D_METHOD("set_mesh_index", "mesh_index"), &GLTFPhysicsShape::set_mesh_index);
|
||||
ClassDB::bind_method(D_METHOD("get_importer_mesh"), &GLTFPhysicsShape::get_importer_mesh);
|
||||
ClassDB::bind_method(D_METHOD("set_importer_mesh", "importer_mesh"), &GLTFPhysicsShape::set_importer_mesh);
|
||||
|
||||
ADD_PROPERTY(PropertyInfo(Variant::STRING, "shape_type"), "set_shape_type", "get_shape_type");
|
||||
ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "size"), "set_size", "get_size");
|
||||
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "radius"), "set_radius", "get_radius");
|
||||
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "height"), "set_height", "get_height");
|
||||
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "is_trigger"), "set_is_trigger", "get_is_trigger");
|
||||
ADD_PROPERTY(PropertyInfo(Variant::INT, "mesh_index"), "set_mesh_index", "get_mesh_index");
|
||||
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "importer_mesh", PROPERTY_HINT_RESOURCE_TYPE, "ImporterMesh"), "set_importer_mesh", "get_importer_mesh");
|
||||
}
|
||||
|
||||
String GLTFPhysicsShape::get_shape_type() const {
|
||||
return shape_type;
|
||||
}
|
||||
|
||||
void GLTFPhysicsShape::set_shape_type(String p_shape_type) {
|
||||
shape_type = p_shape_type;
|
||||
}
|
||||
|
||||
Vector3 GLTFPhysicsShape::get_size() const {
|
||||
return size;
|
||||
}
|
||||
|
||||
void GLTFPhysicsShape::set_size(Vector3 p_size) {
|
||||
size = p_size;
|
||||
}
|
||||
|
||||
real_t GLTFPhysicsShape::get_radius() const {
|
||||
return radius;
|
||||
}
|
||||
|
||||
void GLTFPhysicsShape::set_radius(real_t p_radius) {
|
||||
radius = p_radius;
|
||||
}
|
||||
|
||||
real_t GLTFPhysicsShape::get_height() const {
|
||||
return height;
|
||||
}
|
||||
|
||||
void GLTFPhysicsShape::set_height(real_t p_height) {
|
||||
height = p_height;
|
||||
}
|
||||
|
||||
bool GLTFPhysicsShape::get_is_trigger() const {
|
||||
return is_trigger;
|
||||
}
|
||||
|
||||
void GLTFPhysicsShape::set_is_trigger(bool p_is_trigger) {
|
||||
is_trigger = p_is_trigger;
|
||||
}
|
||||
|
||||
GLTFMeshIndex GLTFPhysicsShape::get_mesh_index() const {
|
||||
return mesh_index;
|
||||
}
|
||||
|
||||
void GLTFPhysicsShape::set_mesh_index(GLTFMeshIndex p_mesh_index) {
|
||||
mesh_index = p_mesh_index;
|
||||
}
|
||||
|
||||
Ref<ImporterMesh> GLTFPhysicsShape::get_importer_mesh() const {
|
||||
return importer_mesh;
|
||||
}
|
||||
|
||||
void GLTFPhysicsShape::set_importer_mesh(Ref<ImporterMesh> p_importer_mesh) {
|
||||
importer_mesh = p_importer_mesh;
|
||||
}
|
||||
|
||||
Ref<ImporterMesh> _convert_hull_points_to_mesh(const Vector<Vector3> &p_hull_points) {
|
||||
Ref<ImporterMesh> importer_mesh;
|
||||
ERR_FAIL_COND_V_MSG(p_hull_points.size() < 3, importer_mesh, "GLTFPhysicsShape: Convex hull has fewer points (" + itos(p_hull_points.size()) + ") than the minimum of 3. At least 3 points are required in order to save to glTF, since it uses a mesh to represent convex hulls.");
|
||||
if (p_hull_points.size() > 255) {
|
||||
WARN_PRINT("GLTFPhysicsShape: Convex hull has more points (" + itos(p_hull_points.size()) + ") than the recommended maximum of 255. This may not load correctly in other engines.");
|
||||
}
|
||||
// Convert the convex hull points into an array of faces.
|
||||
Geometry3D::MeshData md;
|
||||
Error err = ConvexHullComputer::convex_hull(p_hull_points, md);
|
||||
ERR_FAIL_COND_V_MSG(err != OK, importer_mesh, "GLTFPhysicsShape: Failed to compute convex hull.");
|
||||
Vector<Vector3> face_vertices;
|
||||
for (uint32_t i = 0; i < md.faces.size(); i++) {
|
||||
uint32_t index_count = md.faces[i].indices.size();
|
||||
for (uint32_t j = 1; j < index_count - 1; j++) {
|
||||
face_vertices.append(p_hull_points[md.faces[i].indices[0]]);
|
||||
face_vertices.append(p_hull_points[md.faces[i].indices[j]]);
|
||||
face_vertices.append(p_hull_points[md.faces[i].indices[j + 1]]);
|
||||
}
|
||||
}
|
||||
// Create an ImporterMesh from the faces.
|
||||
importer_mesh.instantiate();
|
||||
Array surface_array;
|
||||
surface_array.resize(Mesh::ArrayType::ARRAY_MAX);
|
||||
surface_array[Mesh::ArrayType::ARRAY_VERTEX] = face_vertices;
|
||||
importer_mesh->add_surface(Mesh::PRIMITIVE_TRIANGLES, surface_array);
|
||||
return importer_mesh;
|
||||
}
|
||||
|
||||
Ref<GLTFPhysicsShape> GLTFPhysicsShape::from_node(const CollisionShape3D *p_godot_shape_node) {
|
||||
Ref<GLTFPhysicsShape> gltf_shape;
|
||||
ERR_FAIL_NULL_V_MSG(p_godot_shape_node, gltf_shape, "Tried to create a GLTFPhysicsShape from a CollisionShape3D node, but the given node was null.");
|
||||
Ref<Shape3D> shape_resource = p_godot_shape_node->get_shape();
|
||||
ERR_FAIL_COND_V_MSG(shape_resource.is_null(), gltf_shape, "Tried to create a GLTFPhysicsShape from a CollisionShape3D node, but the given node had a null shape.");
|
||||
gltf_shape = from_resource(shape_resource);
|
||||
// Check if the shape is part of a trigger.
|
||||
Node *parent = p_godot_shape_node->get_parent();
|
||||
if (cast_to<const Area3D>(parent)) {
|
||||
gltf_shape->set_is_trigger(true);
|
||||
}
|
||||
return gltf_shape;
|
||||
}
|
||||
|
||||
CollisionShape3D *GLTFPhysicsShape::to_node(bool p_cache_shapes) {
|
||||
CollisionShape3D *godot_shape_node = memnew(CollisionShape3D);
|
||||
to_resource(p_cache_shapes); // Sets `_shape_cache`.
|
||||
godot_shape_node->set_shape(_shape_cache);
|
||||
return godot_shape_node;
|
||||
}
|
||||
|
||||
Ref<GLTFPhysicsShape> GLTFPhysicsShape::from_resource(const Ref<Shape3D> &p_shape_resource) {
|
||||
Ref<GLTFPhysicsShape> gltf_shape;
|
||||
gltf_shape.instantiate();
|
||||
ERR_FAIL_COND_V_MSG(p_shape_resource.is_null(), gltf_shape, "Tried to create a GLTFPhysicsShape from a Shape3D resource, but the given resource was null.");
|
||||
if (cast_to<BoxShape3D>(p_shape_resource.ptr())) {
|
||||
gltf_shape->shape_type = "box";
|
||||
Ref<BoxShape3D> box = p_shape_resource;
|
||||
gltf_shape->set_size(box->get_size());
|
||||
} else if (cast_to<const CapsuleShape3D>(p_shape_resource.ptr())) {
|
||||
gltf_shape->shape_type = "capsule";
|
||||
Ref<CapsuleShape3D> capsule = p_shape_resource;
|
||||
gltf_shape->set_radius(capsule->get_radius());
|
||||
gltf_shape->set_height(capsule->get_height());
|
||||
} else if (cast_to<const CylinderShape3D>(p_shape_resource.ptr())) {
|
||||
gltf_shape->shape_type = "cylinder";
|
||||
Ref<CylinderShape3D> cylinder = p_shape_resource;
|
||||
gltf_shape->set_radius(cylinder->get_radius());
|
||||
gltf_shape->set_height(cylinder->get_height());
|
||||
} else if (cast_to<const SphereShape3D>(p_shape_resource.ptr())) {
|
||||
gltf_shape->shape_type = "sphere";
|
||||
Ref<SphereShape3D> sphere = p_shape_resource;
|
||||
gltf_shape->set_radius(sphere->get_radius());
|
||||
} else if (cast_to<const ConvexPolygonShape3D>(p_shape_resource.ptr())) {
|
||||
gltf_shape->shape_type = "convex";
|
||||
Ref<ConvexPolygonShape3D> convex = p_shape_resource;
|
||||
Vector<Vector3> hull_points = convex->get_points();
|
||||
Ref<ImporterMesh> importer_mesh = _convert_hull_points_to_mesh(hull_points);
|
||||
ERR_FAIL_COND_V_MSG(importer_mesh.is_null(), gltf_shape, "GLTFPhysicsShape: Failed to convert convex hull points to a mesh.");
|
||||
gltf_shape->set_importer_mesh(importer_mesh);
|
||||
} else if (cast_to<const ConcavePolygonShape3D>(p_shape_resource.ptr())) {
|
||||
gltf_shape->shape_type = "trimesh";
|
||||
Ref<ConcavePolygonShape3D> concave = p_shape_resource;
|
||||
Ref<ImporterMesh> importer_mesh;
|
||||
importer_mesh.instantiate();
|
||||
Array surface_array;
|
||||
surface_array.resize(Mesh::ArrayType::ARRAY_MAX);
|
||||
surface_array[Mesh::ArrayType::ARRAY_VERTEX] = concave->get_faces();
|
||||
importer_mesh->add_surface(Mesh::PRIMITIVE_TRIANGLES, surface_array);
|
||||
gltf_shape->set_importer_mesh(importer_mesh);
|
||||
} else {
|
||||
ERR_PRINT("Tried to create a GLTFPhysicsShape from a Shape3D, but the given shape '" + String(Variant(p_shape_resource)) +
|
||||
"' had an unsupported shape type. Only BoxShape3D, CapsuleShape3D, CylinderShape3D, SphereShape3D, ConcavePolygonShape3D, and ConvexPolygonShape3D are supported.");
|
||||
}
|
||||
gltf_shape->_shape_cache = p_shape_resource;
|
||||
return gltf_shape;
|
||||
}
|
||||
|
||||
Ref<Shape3D> GLTFPhysicsShape::to_resource(bool p_cache_shapes) {
|
||||
if (!p_cache_shapes || _shape_cache.is_null()) {
|
||||
if (shape_type == "box") {
|
||||
Ref<BoxShape3D> box;
|
||||
box.instantiate();
|
||||
box->set_size(size);
|
||||
_shape_cache = box;
|
||||
} else if (shape_type == "capsule") {
|
||||
Ref<CapsuleShape3D> capsule;
|
||||
capsule.instantiate();
|
||||
capsule->set_radius(radius);
|
||||
capsule->set_height(height);
|
||||
_shape_cache = capsule;
|
||||
} else if (shape_type == "cylinder") {
|
||||
Ref<CylinderShape3D> cylinder;
|
||||
cylinder.instantiate();
|
||||
cylinder->set_radius(radius);
|
||||
cylinder->set_height(height);
|
||||
_shape_cache = cylinder;
|
||||
} else if (shape_type == "sphere") {
|
||||
Ref<SphereShape3D> sphere;
|
||||
sphere.instantiate();
|
||||
sphere->set_radius(radius);
|
||||
_shape_cache = sphere;
|
||||
} else if (shape_type == "convex") {
|
||||
ERR_FAIL_COND_V_MSG(importer_mesh.is_null(), _shape_cache, "GLTFPhysicsShape: Error converting convex hull shape to a shape resource: The mesh resource is null.");
|
||||
Ref<ConvexPolygonShape3D> convex = importer_mesh->get_mesh()->create_convex_shape();
|
||||
_shape_cache = convex;
|
||||
} else if (shape_type == "trimesh") {
|
||||
ERR_FAIL_COND_V_MSG(importer_mesh.is_null(), _shape_cache, "GLTFPhysicsShape: Error converting concave mesh shape to a shape resource: The mesh resource is null.");
|
||||
Ref<ConcavePolygonShape3D> concave = importer_mesh->create_trimesh_shape();
|
||||
_shape_cache = concave;
|
||||
} else {
|
||||
ERR_PRINT("GLTFPhysicsShape: Error converting to a shape resource: Shape type '" + shape_type + "' is unknown.");
|
||||
}
|
||||
}
|
||||
return _shape_cache;
|
||||
}
|
||||
|
||||
Ref<GLTFPhysicsShape> GLTFPhysicsShape::from_dictionary(const Dictionary p_dictionary) {
|
||||
ERR_FAIL_COND_V_MSG(!p_dictionary.has("type"), Ref<GLTFPhysicsShape>(), "Failed to parse GLTFPhysicsShape, missing required field 'type'.");
|
||||
Ref<GLTFPhysicsShape> gltf_shape;
|
||||
gltf_shape.instantiate();
|
||||
String shape_type = p_dictionary["type"];
|
||||
if (shape_type == "hull") {
|
||||
shape_type = "convex";
|
||||
}
|
||||
gltf_shape->shape_type = shape_type;
|
||||
if (shape_type != "box" && shape_type != "capsule" && shape_type != "cylinder" && shape_type != "sphere" && shape_type != "convex" && shape_type != "trimesh") {
|
||||
ERR_PRINT("GLTFPhysicsShape: Error parsing unknown shape type '" + shape_type + "'. Only box, capsule, cylinder, sphere, convex, and trimesh are supported.");
|
||||
}
|
||||
Dictionary properties;
|
||||
if (p_dictionary.has(shape_type)) {
|
||||
properties = p_dictionary[shape_type];
|
||||
} else {
|
||||
properties = p_dictionary;
|
||||
}
|
||||
if (properties.has("radius")) {
|
||||
gltf_shape->set_radius(properties["radius"]);
|
||||
}
|
||||
if (properties.has("height")) {
|
||||
gltf_shape->set_height(properties["height"]);
|
||||
}
|
||||
if (properties.has("size")) {
|
||||
const Array &arr = properties["size"];
|
||||
if (arr.size() == 3) {
|
||||
gltf_shape->set_size(Vector3(arr[0], arr[1], arr[2]));
|
||||
} else {
|
||||
ERR_PRINT("GLTFPhysicsShape: Error parsing the size, it must have exactly 3 numbers.");
|
||||
}
|
||||
}
|
||||
if (properties.has("isTrigger")) {
|
||||
gltf_shape->set_is_trigger(properties["isTrigger"]);
|
||||
}
|
||||
if (properties.has("mesh")) {
|
||||
gltf_shape->set_mesh_index(properties["mesh"]);
|
||||
}
|
||||
if (unlikely(gltf_shape->get_mesh_index() < 0 && (shape_type == "convex" || shape_type == "trimesh"))) {
|
||||
ERR_PRINT("Error parsing GLTFPhysicsShape: The mesh-based shape type '" + shape_type + "' does not have a valid mesh index.");
|
||||
}
|
||||
return gltf_shape;
|
||||
}
|
||||
|
||||
Dictionary GLTFPhysicsShape::to_dictionary() const {
|
||||
Dictionary gltf_shape;
|
||||
gltf_shape["type"] = shape_type;
|
||||
Dictionary sub;
|
||||
if (shape_type == "box") {
|
||||
Array size_array;
|
||||
size_array.resize(3);
|
||||
size_array[0] = size.x;
|
||||
size_array[1] = size.y;
|
||||
size_array[2] = size.z;
|
||||
sub["size"] = size_array;
|
||||
} else if (shape_type == "capsule") {
|
||||
sub["radius"] = get_radius();
|
||||
sub["height"] = get_height();
|
||||
} else if (shape_type == "cylinder") {
|
||||
sub["radius"] = get_radius();
|
||||
sub["height"] = get_height();
|
||||
} else if (shape_type == "sphere") {
|
||||
sub["radius"] = get_radius();
|
||||
} else if (shape_type == "trimesh" || shape_type == "convex") {
|
||||
sub["mesh"] = get_mesh_index();
|
||||
}
|
||||
gltf_shape[shape_type] = sub;
|
||||
return gltf_shape;
|
||||
}
|
90
modules/gltf/extensions/physics/gltf_physics_shape.h
Normal file
90
modules/gltf/extensions/physics/gltf_physics_shape.h
Normal file
@@ -0,0 +1,90 @@
|
||||
/**************************************************************************/
|
||||
/* gltf_physics_shape.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../../gltf_defines.h"
|
||||
|
||||
#include "scene/3d/physics/collision_shape_3d.h"
|
||||
|
||||
class ImporterMesh;
|
||||
|
||||
// GLTFPhysicsShape is an intermediary between Godot's collision shape nodes
|
||||
// and the OMI_physics_shape extension.
|
||||
// https://github.com/omigroup/gltf-extensions/tree/main/extensions/2.0/OMI_physics_shape
|
||||
|
||||
class GLTFPhysicsShape : public Resource {
|
||||
GDCLASS(GLTFPhysicsShape, Resource)
|
||||
|
||||
protected:
|
||||
static void _bind_methods();
|
||||
|
||||
private:
|
||||
String shape_type;
|
||||
Vector3 size = Vector3(1.0, 1.0, 1.0);
|
||||
real_t radius = 0.5;
|
||||
real_t height = 2.0;
|
||||
bool is_trigger = false;
|
||||
GLTFMeshIndex mesh_index = -1;
|
||||
Ref<ImporterMesh> importer_mesh = nullptr;
|
||||
// Internal only, for caching Godot shape resources. Used in `to_resource` and `to_node`.
|
||||
Ref<Shape3D> _shape_cache = nullptr;
|
||||
|
||||
public:
|
||||
String get_shape_type() const;
|
||||
void set_shape_type(String p_shape_type);
|
||||
|
||||
Vector3 get_size() const;
|
||||
void set_size(Vector3 p_size);
|
||||
|
||||
real_t get_radius() const;
|
||||
void set_radius(real_t p_radius);
|
||||
|
||||
real_t get_height() const;
|
||||
void set_height(real_t p_height);
|
||||
|
||||
bool get_is_trigger() const;
|
||||
void set_is_trigger(bool p_is_trigger);
|
||||
|
||||
GLTFMeshIndex get_mesh_index() const;
|
||||
void set_mesh_index(GLTFMeshIndex p_mesh_index);
|
||||
|
||||
Ref<ImporterMesh> get_importer_mesh() const;
|
||||
void set_importer_mesh(Ref<ImporterMesh> p_importer_mesh);
|
||||
|
||||
static Ref<GLTFPhysicsShape> from_node(const CollisionShape3D *p_shape_node);
|
||||
CollisionShape3D *to_node(bool p_cache_shapes = false);
|
||||
|
||||
static Ref<GLTFPhysicsShape> from_resource(const Ref<Shape3D> &p_shape_resource);
|
||||
Ref<Shape3D> to_resource(bool p_cache_shapes = false);
|
||||
|
||||
static Ref<GLTFPhysicsShape> from_dictionary(const Dictionary p_dictionary);
|
||||
Dictionary to_dictionary() const;
|
||||
};
|
Reference in New Issue
Block a user