feat: updated engine version to 4.4-rc1
This commit is contained in:
parent
ee00efde1f
commit
21ba8e33af
5459 changed files with 1128836 additions and 198305 deletions
|
|
@ -1,4 +1,5 @@
|
|||
#!/usr/bin/env python
|
||||
from misc.utility.scons_hints import *
|
||||
|
||||
Import("env")
|
||||
Import("env_modules")
|
||||
|
|
|
|||
|
|
@ -38,14 +38,18 @@ void GLTFDocumentExtension::_bind_methods() {
|
|||
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(_generate_scene_node, "state", "gltf_node", "scene_parent");
|
||||
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");
|
||||
|
|
@ -56,7 +60,7 @@ void GLTFDocumentExtension::_bind_methods() {
|
|||
|
||||
// Import process.
|
||||
Error GLTFDocumentExtension::import_preflight(Ref<GLTFState> p_state, Vector<String> p_extensions) {
|
||||
ERR_FAIL_NULL_V(p_state, ERR_INVALID_PARAMETER);
|
||||
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;
|
||||
|
|
@ -69,16 +73,16 @@ Vector<String> GLTFDocumentExtension::get_supported_extensions() {
|
|||
}
|
||||
|
||||
Error GLTFDocumentExtension::parse_node_extensions(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Dictionary &p_extensions) {
|
||||
ERR_FAIL_NULL_V(p_state, ERR_INVALID_PARAMETER);
|
||||
ERR_FAIL_NULL_V(p_gltf_node, ERR_INVALID_PARAMETER);
|
||||
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_NULL_V(p_state, ERR_INVALID_PARAMETER);
|
||||
ERR_FAIL_NULL_V(r_image, ERR_INVALID_PARAMETER);
|
||||
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;
|
||||
|
|
@ -91,31 +95,45 @@ String GLTFDocumentExtension::get_image_file_extension() {
|
|||
}
|
||||
|
||||
Error GLTFDocumentExtension::parse_texture_json(Ref<GLTFState> p_state, const Dictionary &p_texture_json, Ref<GLTFTexture> r_gltf_texture) {
|
||||
ERR_FAIL_NULL_V(p_state, ERR_INVALID_PARAMETER);
|
||||
ERR_FAIL_NULL_V(r_gltf_texture, ERR_INVALID_PARAMETER);
|
||||
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;
|
||||
}
|
||||
|
||||
Node3D *GLTFDocumentExtension::generate_scene_node(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Node *p_scene_parent) {
|
||||
ERR_FAIL_NULL_V(p_state, nullptr);
|
||||
ERR_FAIL_NULL_V(p_gltf_node, nullptr);
|
||||
Node3D *ret_node = nullptr;
|
||||
GDVIRTUAL_CALL(_generate_scene_node, p_state, p_gltf_node, p_scene_parent, ret_node);
|
||||
return ret_node;
|
||||
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_NULL_V(p_state, ERR_INVALID_PARAMETER);
|
||||
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_NULL_V(p_state, ERR_INVALID_PARAMETER);
|
||||
ERR_FAIL_NULL_V(p_gltf_node, ERR_INVALID_PARAMETER);
|
||||
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);
|
||||
|
|
@ -124,7 +142,7 @@ Error GLTFDocumentExtension::import_node(Ref<GLTFState> p_state, Ref<GLTFNode> p
|
|||
|
||||
Error GLTFDocumentExtension::import_post(Ref<GLTFState> p_state, Node *p_root) {
|
||||
ERR_FAIL_NULL_V(p_root, ERR_INVALID_PARAMETER);
|
||||
ERR_FAIL_NULL_V(p_state, 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;
|
||||
|
|
@ -139,19 +157,36 @@ Error GLTFDocumentExtension::export_preflight(Ref<GLTFState> p_state, Node *p_ro
|
|||
}
|
||||
|
||||
void GLTFDocumentExtension::convert_scene_node(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Node *p_scene_node) {
|
||||
ERR_FAIL_NULL(p_state);
|
||||
ERR_FAIL_NULL(p_gltf_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_NULL_V(p_state, ERR_INVALID_PARAMETER);
|
||||
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);
|
||||
|
|
@ -160,38 +195,38 @@ Vector<String> GLTFDocumentExtension::get_saveable_image_formats() {
|
|||
|
||||
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_NULL_V(p_state, ret);
|
||||
ERR_FAIL_NULL_V(p_image, 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_NULL_V(p_state, ERR_INVALID_PARAMETER);
|
||||
ERR_FAIL_NULL_V(p_image, ERR_INVALID_PARAMETER);
|
||||
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_NULL_V(p_state, ERR_INVALID_PARAMETER);
|
||||
ERR_FAIL_NULL_V(p_gltf_texture, ERR_INVALID_PARAMETER);
|
||||
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_NULL_V(p_state, ERR_INVALID_PARAMETER);
|
||||
ERR_FAIL_NULL_V(p_gltf_node, ERR_INVALID_PARAMETER);
|
||||
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_NULL_V(p_state, ERR_INVALID_PARAMETER);
|
||||
ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);
|
||||
Error err = OK;
|
||||
GDVIRTUAL_CALL(_export_post, p_state, err);
|
||||
return err;
|
||||
|
|
|
|||
|
|
@ -49,14 +49,18 @@ public:
|
|||
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);
|
||||
|
|
@ -71,14 +75,18 @@ public:
|
|||
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(Node3D *, _generate_scene_node, Ref<GLTFState>, Ref<GLTFNode>, Node *);
|
||||
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);
|
||||
|
|
|
|||
|
|
@ -34,9 +34,6 @@
|
|||
#include "scene/3d/mesh_instance_3d.h"
|
||||
#include "scene/resources/3d/importer_mesh.h"
|
||||
|
||||
void GLTFDocumentExtensionConvertImporterMesh::_bind_methods() {
|
||||
}
|
||||
|
||||
void GLTFDocumentExtensionConvertImporterMesh::_copy_meta(Object *p_src_object, Object *p_dst_object) {
|
||||
List<StringName> meta_list;
|
||||
p_src_object->get_meta_list(&meta_list);
|
||||
|
|
@ -46,34 +43,42 @@ void GLTFDocumentExtensionConvertImporterMesh::_copy_meta(Object *p_src_object,
|
|||
}
|
||||
}
|
||||
|
||||
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_NULL_V(p_state, 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 *mesh_3d = cast_to<ImporterMeshInstance3D>(node);
|
||||
if (mesh_3d) {
|
||||
MeshInstance3D *mesh_instance_node_3d = memnew(MeshInstance3D);
|
||||
Ref<ImporterMesh> mesh = mesh_3d->get_mesh();
|
||||
if (mesh.is_valid()) {
|
||||
Ref<ArrayMesh> array_mesh = mesh->get_mesh();
|
||||
mesh_instance_node_3d->set_name(node->get_name());
|
||||
mesh_instance_node_3d->set_transform(mesh_3d->get_transform());
|
||||
mesh_instance_node_3d->set_mesh(array_mesh);
|
||||
mesh_instance_node_3d->set_skin(mesh_3d->get_skin());
|
||||
mesh_instance_node_3d->set_skeleton_path(mesh_3d->get_skeleton_path());
|
||||
node->replace_by(mesh_instance_node_3d);
|
||||
_copy_meta(mesh_3d, mesh_instance_node_3d);
|
||||
_copy_meta(mesh.ptr(), array_mesh.ptr());
|
||||
delete_queue.push_back(node);
|
||||
node = mesh_instance_node_3d;
|
||||
} else {
|
||||
memdelete(mesh_instance_node_3d);
|
||||
}
|
||||
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++) {
|
||||
|
|
|
|||
|
|
@ -33,14 +33,16 @@
|
|||
|
||||
#include "gltf_document_extension.h"
|
||||
|
||||
class MeshInstance3D;
|
||||
|
||||
class GLTFDocumentExtensionConvertImporterMesh : public GLTFDocumentExtension {
|
||||
GDCLASS(GLTFDocumentExtensionConvertImporterMesh, GLTFDocumentExtension);
|
||||
|
||||
protected:
|
||||
static void _bind_methods();
|
||||
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;
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -30,6 +30,7 @@
|
|||
|
||||
#include "gltf_light.h"
|
||||
|
||||
#include "../structures/gltf_object_model_property.h"
|
||||
#include "scene/3d/light_3d.h"
|
||||
|
||||
void GLTFLight::_bind_methods() {
|
||||
|
|
@ -62,6 +63,21 @@ void GLTFLight::_bind_methods() {
|
|||
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;
|
||||
}
|
||||
|
|
@ -170,7 +186,7 @@ Light3D *GLTFLight::to_node() const {
|
|||
}
|
||||
|
||||
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'.");
|
||||
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"];
|
||||
|
|
@ -181,7 +197,7 @@ Ref<GLTFLight> GLTFLight::from_dictionary(const Dictionary p_dictionary) {
|
|||
if (arr.size() == 3) {
|
||||
light->color = Color(arr[0], arr[1], arr[2]).linear_to_srgb();
|
||||
} else {
|
||||
ERR_PRINT("Error parsing GLTF light: The color must have exactly 3 numbers.");
|
||||
ERR_PRINT("Error parsing glTF light: The color must have exactly 3 numbers.");
|
||||
}
|
||||
}
|
||||
if (p_dictionary.has("intensity")) {
|
||||
|
|
@ -195,31 +211,37 @@ Ref<GLTFLight> GLTFLight::from_dictionary(const Dictionary p_dictionary) {
|
|||
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.");
|
||||
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.");
|
||||
ERR_PRINT("Error parsing glTF light: Light type '" + type + "' is unknown.");
|
||||
}
|
||||
return light;
|
||||
}
|
||||
|
||||
Dictionary GLTFLight::to_dictionary() const {
|
||||
Dictionary d;
|
||||
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;
|
||||
d["type"] = light_type;
|
||||
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 != INFINITY) {
|
||||
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["intensity"] = intensity;
|
||||
d["range"] = range;
|
||||
d["type"] = light_type;
|
||||
return d;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -33,6 +33,7 @@
|
|||
|
||||
#include "core/io/resource.h"
|
||||
|
||||
class GLTFObjectModelProperty;
|
||||
class Light3D;
|
||||
|
||||
// https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Khronos/KHR_lights_punctual
|
||||
|
|
@ -54,6 +55,8 @@ private:
|
|||
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);
|
||||
|
||||
|
|
|
|||
|
|
@ -31,8 +31,11 @@
|
|||
#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")) {
|
||||
|
|
@ -88,7 +91,7 @@ Error GLTFDocumentExtensionPhysics::parse_node_extensions(Ref<GLTFState> p_state
|
|||
// "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()) + ").");
|
||||
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));
|
||||
|
|
@ -103,8 +106,9 @@ Error GLTFDocumentExtensionPhysics::parse_node_extensions(Ref<GLTFState> p_state
|
|||
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()) + ").");
|
||||
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.
|
||||
|
|
@ -117,8 +121,9 @@ Error GLTFDocumentExtensionPhysics::parse_node_extensions(Ref<GLTFState> p_state
|
|||
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()) + ").");
|
||||
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.
|
||||
|
|
@ -129,8 +134,8 @@ Error GLTFDocumentExtensionPhysics::parse_node_extensions(Ref<GLTFState> p_state
|
|||
}
|
||||
// If this node defines explicit member shape nodes, save this information.
|
||||
if (node_trigger.has("nodes")) {
|
||||
Array node_trigger_nodes = node_trigger["nodes"];
|
||||
p_gltf_node->set_additional_data(StringName("GLTFPhysicsCompoundTriggerNodes"), node_trigger_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")) {
|
||||
|
|
@ -140,6 +145,144 @@ Error GLTFDocumentExtensionPhysics::parse_node_extensions(Ref<GLTFState> p_state
|
|||
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) {
|
||||
|
|
@ -150,7 +293,7 @@ void _setup_shape_mesh_resource_from_index_if_needed(Ref<GLTFState> p_state, Ref
|
|||
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()) + ").");
|
||||
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();
|
||||
|
|
@ -164,12 +307,12 @@ CollisionObject3D *_generate_shape_with_body(Ref<GLTFState> p_state, Ref<GLTFNod
|
|||
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
|
||||
// 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.
|
||||
// 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.
|
||||
// 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
|
||||
|
|
@ -266,7 +409,7 @@ Node3D *GLTFDocumentExtensionPhysics::generate_scene_node(Ref<GLTFState> p_state
|
|||
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 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);
|
||||
}
|
||||
|
|
@ -309,7 +452,7 @@ Node3D *GLTFDocumentExtensionPhysics::generate_scene_node(Ref<GLTFState> p_state
|
|||
}
|
||||
} 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,
|
||||
// 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;
|
||||
|
|
@ -386,7 +529,7 @@ void GLTFDocumentExtensionPhysics::convert_scene_node(Ref<GLTFState> p_state, Re
|
|||
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?");
|
||||
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()) {
|
||||
|
|
@ -434,24 +577,126 @@ Array _get_or_create_state_shapes_in_state(Ref<GLTFState> p_state) {
|
|||
return state_shapes;
|
||||
}
|
||||
|
||||
Dictionary _export_node_shape(Ref<GLTFState> p_state, Ref<GLTFPhysicsShape> p_physics_shape) {
|
||||
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);
|
||||
int size = state_shapes.size();
|
||||
GLTFShapeIndex size = state_shapes.size();
|
||||
Dictionary shape_property;
|
||||
Dictionary shape_dict = p_physics_shape->to_dictionary();
|
||||
for (int i = 0; i < size; i++) {
|
||||
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.
|
||||
shape_property["shape"] = i;
|
||||
return shape_property;
|
||||
return i;
|
||||
}
|
||||
}
|
||||
// If we don't have an identical shape, add it to the array.
|
||||
state_shapes.push_back(shape_dict);
|
||||
shape_property["shape"] = size;
|
||||
return shape_property;
|
||||
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) {
|
||||
|
|
@ -465,13 +710,16 @@ Error GLTFDocumentExtensionPhysics::export_node(Ref<GLTFState> p_state, Ref<GLTF
|
|||
trigger_property["nodes"] = compound_trigger_nodes;
|
||||
}
|
||||
}
|
||||
Ref<GLTFPhysicsShape> collider_shape = p_gltf_node->get_additional_data(StringName("GLTFPhysicsColliderShape"));
|
||||
if (collider_shape.is_valid()) {
|
||||
physics_body_ext["collider"] = _export_node_shape(p_state, collider_shape);
|
||||
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;
|
||||
}
|
||||
Ref<GLTFPhysicsShape> trigger_shape = p_gltf_node->get_additional_data(StringName("GLTFPhysicsTriggerShape"));
|
||||
if (trigger_shape.is_valid()) {
|
||||
physics_body_ext["trigger"] = _export_node_shape(p_state, trigger_shape);
|
||||
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"];
|
||||
|
|
|
|||
|
|
@ -43,9 +43,12 @@ public:
|
|||
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;
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -108,7 +108,7 @@ void GLTFPhysicsBody::set_body_type(String p_body_type) {
|
|||
} 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\".");
|
||||
ERR_PRINT("Error setting glTF physics body type: The body type must be one of \"static\", \"animatable\", \"character\", \"rigid\", \"vehicle\", or \"trigger\".");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -193,9 +193,6 @@ Ref<GLTFPhysicsBody> GLTFPhysicsBody::from_node(const CollisionObject3D *p_body_
|
|||
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 (body->get_center_of_mass() != Vector3()) {
|
||||
WARN_PRINT("GLTFPhysicsBody: This rigid body has a center of mass offset from the origin, which will be ignored when exporting to GLTF.");
|
||||
}
|
||||
if (cast_to<VehicleBody3D>(p_body_node)) {
|
||||
physics_body->body_type = PhysicsBodyType::VEHICLE;
|
||||
} else {
|
||||
|
|
@ -289,7 +286,7 @@ Ref<GLTFPhysicsBody> GLTFPhysicsBody::from_dictionary(const Dictionary p_diction
|
|||
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.");
|
||||
ERR_PRINT("Error parsing glTF physics body: The body type in the glTF file \"" + body_type_string + "\" was not recognized.");
|
||||
}
|
||||
}
|
||||
if (motion.has("mass")) {
|
||||
|
|
@ -300,7 +297,7 @@ Ref<GLTFPhysicsBody> GLTFPhysicsBody::from_dictionary(const Dictionary p_diction
|
|||
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.");
|
||||
ERR_PRINT("Error parsing glTF physics body: The linear velocity vector must have exactly 3 numbers.");
|
||||
}
|
||||
}
|
||||
if (motion.has("angularVelocity")) {
|
||||
|
|
@ -308,7 +305,7 @@ Ref<GLTFPhysicsBody> GLTFPhysicsBody::from_dictionary(const Dictionary p_diction
|
|||
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.");
|
||||
ERR_PRINT("Error parsing glTF physics body: The angular velocity vector must have exactly 3 numbers.");
|
||||
}
|
||||
}
|
||||
if (motion.has("centerOfMass")) {
|
||||
|
|
@ -316,7 +313,7 @@ Ref<GLTFPhysicsBody> GLTFPhysicsBody::from_dictionary(const Dictionary p_diction
|
|||
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.");
|
||||
ERR_PRINT("Error parsing glTF physics body: The center of mass vector must have exactly 3 numbers.");
|
||||
}
|
||||
}
|
||||
if (motion.has("inertiaDiagonal")) {
|
||||
|
|
@ -324,7 +321,7 @@ Ref<GLTFPhysicsBody> GLTFPhysicsBody::from_dictionary(const Dictionary p_diction
|
|||
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.");
|
||||
ERR_PRINT("Error parsing glTF physics body: The inertia diagonal vector must have exactly 3 numbers.");
|
||||
}
|
||||
}
|
||||
if (motion.has("inertiaOrientation")) {
|
||||
|
|
@ -332,7 +329,7 @@ Ref<GLTFPhysicsBody> GLTFPhysicsBody::from_dictionary(const Dictionary p_diction
|
|||
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.");
|
||||
ERR_PRINT("Error parsing glTF physics body: The inertia orientation quaternion must have exactly 4 numbers.");
|
||||
}
|
||||
}
|
||||
return physics_body;
|
||||
|
|
|
|||
|
|
@ -134,7 +134,7 @@ void GLTFPhysicsShape::set_importer_mesh(Ref<ImporterMesh> 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.");
|
||||
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.");
|
||||
}
|
||||
|
|
@ -229,7 +229,7 @@ Ref<GLTFPhysicsShape> GLTFPhysicsShape::from_resource(const Ref<Shape3D> &p_shap
|
|||
}
|
||||
|
||||
Ref<Shape3D> GLTFPhysicsShape::to_resource(bool p_cache_shapes) {
|
||||
if (!p_cache_shapes || _shape_cache == nullptr) {
|
||||
if (!p_cache_shapes || _shape_cache.is_null()) {
|
||||
if (shape_type == "box") {
|
||||
Ref<BoxShape3D> box;
|
||||
box.instantiate();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue