Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- diff --git a/editor/import/resource_importer_scene.cpp b/editor/import/resource_importer_scene.cpp
- index 3ae69d8..89ded65 100644
- --- a/editor/import/resource_importer_scene.cpp
- +++ b/editor/import/resource_importer_scene.cpp
- @@ -236,55 +236,12 @@ String ResourceImporterScene::get_preset_name(int p_idx) const {
- return "";
- }
- -static bool _teststr(const String &p_what, const String &p_str) {
- -
- - String what = p_what;
- -
- - //remove trailing spaces and numbers, some apps like blender add ".number" to duplicates so also compensate for this
- - while (what.length() && ((what[what.length() - 1] >= '0' && what[what.length() - 1] <= '9') || what[what.length() - 1] <= 32 || what[what.length() - 1] == '.')) {
- -
- - what = what.substr(0, what.length() - 1);
- - }
- -
- - if (what.findn("$" + p_str) != -1) //blender and other stuff
- - return true;
- - if (what.to_lower().ends_with("-" + p_str)) //collada only supports "_" and "-" besides letters
- - return true;
- - if (what.to_lower().ends_with("_" + p_str)) //collada only supports "_" and "-" besides letters
- - return true;
- - return false;
- -}
- -
- -static String _fixstr(const String &p_what, const String &p_str) {
- -
- - String what = p_what;
- -
- - //remove trailing spaces and numbers, some apps like blender add ".number" to duplicates so also compensate for this
- - while (what.length() && ((what[what.length() - 1] >= '0' && what[what.length() - 1] <= '9') || what[what.length() - 1] <= 32 || what[what.length() - 1] == '.')) {
- -
- - what = what.substr(0, what.length() - 1);
- - }
- -
- - String end = p_what.substr(what.length(), p_what.length() - what.length());
- -
- - if (what.findn("$" + p_str) != -1) //blender and other stuff
- - return what.replace("$" + p_str, "") + end;
- - if (what.to_lower().ends_with("-" + p_str)) //collada only supports "_" and "-" besides letters
- - return what.substr(0, what.length() - (p_str.length() + 1)) + end;
- - if (what.to_lower().ends_with("_" + p_str)) //collada only supports "_" and "-" besides letters
- - return what.substr(0, what.length() - (p_str.length() + 1)) + end;
- - return what;
- -}
- -
- -static void _gen_shape_list(const Ref<Mesh> &mesh, List<Ref<Shape> > &r_shape_list, bool p_convex) {
- -
- +void ResourceImporterScene::_gen_shape_list(const Ref<Mesh> &p_mesh, List<Ref<Shape>> &r_shape_list, bool p_convex) {
- if (!p_convex) {
- -
- - Ref<Shape> shape = mesh->create_trimesh_shape();
- + Ref<Shape> shape = p_mesh->create_trimesh_shape();
- r_shape_list.push_back(shape);
- } else {
- -
- - Vector<Ref<Shape> > cd = mesh->convex_decompose();
- + Vector<Ref<Shape>> cd = p_mesh->convex_decompose();
- if (cd.size()) {
- for (int i = 0; i < cd.size(); i++) {
- r_shape_list.push_back(cd[i]);
- @@ -293,182 +250,100 @@ static void _gen_shape_list(const Ref<Mesh> &mesh, List<Ref<Shape> > &r_shape_li
- }
- }
- -Node *ResourceImporterScene::_fix_node(Node *p_node, Node *p_root, Map<Ref<Mesh>, List<Ref<Shape> > > &collision_map, LightBakeMode p_light_bake_mode) {
- -
- +Node *ResourceImporterScene::_fix_node(Node *p_node, Node *p_root, Map<Ref<Mesh>, List<Ref<Shape>>> &collision_map, LightBakeMode p_light_bake_mode) {
- // children first
- for (int i = 0; i < p_node->get_child_count(); i++) {
- -
- Node *r = _fix_node(p_node->get_child(i), p_root, collision_map, p_light_bake_mode);
- if (!r) {
- i--; //was erased
- }
- }
- - String name = p_node->get_name();
- -
- - bool isroot = p_node == p_root;
- -
- - if (!isroot && _teststr(name, "noimp")) {
- + const auto [node_name, node_hint] = _get_node_import_hint(p_node->get_name());
- + const bool is_root = p_node == p_root;
- + if (!is_root && node_hint == NODE_HINT_NO_IMPORT) {
- memdelete(p_node);
- - return NULL;
- + return nullptr;
- }
- - if (Object::cast_to<MeshInstance>(p_node)) {
- -
- - MeshInstance *mi = Object::cast_to<MeshInstance>(p_node);
- -
- - Ref<ArrayMesh> m = mi->get_mesh();
- -
- - if (m.is_valid()) {
- -
- - for (int i = 0; i < m->get_surface_count(); i++) {
- -
- - Ref<SpatialMaterial> mat = m->surface_get_material(i);
- - if (!mat.is_valid())
- - continue;
- -
- - if (_teststr(mat->get_name(), "alpha")) {
- -
- - mat->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
- - mat->set_name(_fixstr(mat->get_name(), "alpha"));
- - }
- - if (_teststr(mat->get_name(), "vcol")) {
- -
- - mat->set_flag(SpatialMaterial::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
- - mat->set_flag(SpatialMaterial::FLAG_SRGB_VERTEX_COLOR, true);
- - mat->set_name(_fixstr(mat->get_name(), "vcol"));
- - }
- - }
- - }
- -
- - if (p_light_bake_mode != LIGHT_BAKE_DISABLED) {
- -
- - mi->set_flag(GeometryInstance::FLAG_USE_BAKED_LIGHT, true);
- - }
- + if (auto *ap = Object::cast_to<AnimationPlayer>(p_node); ap) {
- + _fix_animations(ap);
- + return p_node;
- }
- - if (Object::cast_to<AnimationPlayer>(p_node)) {
- - //remove animations referencing non-importable nodes
- - AnimationPlayer *ap = Object::cast_to<AnimationPlayer>(p_node);
- -
- - List<StringName> anims;
- - ap->get_animation_list(&anims);
- - for (List<StringName>::Element *E = anims.front(); E; E = E->next()) {
- -
- - Ref<Animation> anim = ap->get_animation(E->get());
- - ERR_CONTINUE(anim.is_null());
- - for (int i = 0; i < anim->get_track_count(); i++) {
- - NodePath path = anim->track_get_path(i);
- -
- - for (int j = 0; j < path.get_name_count(); j++) {
- - String node = path.get_name(j);
- - if (_teststr(node, "noimp")) {
- - anim->remove_track(i);
- - i--;
- - break;
- - }
- - }
- - }
- - }
- + auto *mesh_instance = Object::cast_to<MeshInstance>(p_node);
- + if (mesh_instance) {
- + _fix_materials(mesh_instance, p_light_bake_mode);
- }
- - if (_teststr(name, "colonly") || _teststr(name, "convcolonly")) {
- -
- - if (isroot)
- + if (node_hint == NODE_HINT_COLLISION_ONLY || node_hint == NODE_HINT_CONVEX_COLLISION_ONLY || node_hint == NODE_HINT_SHAPE_ONLY || node_hint == NODE_HINT_CONVEX_SHAPE_ONLY) {
- + if (is_root) {
- return p_node;
- - MeshInstance *mi = Object::cast_to<MeshInstance>(p_node);
- - if (mi) {
- - Ref<Mesh> mesh = mi->get_mesh();
- + }
- + if (mesh_instance) {
- + Ref<Mesh> mesh = mesh_instance->get_mesh();
- if (mesh.is_valid()) {
- - List<Ref<Shape> > shapes;
- - String fixed_name;
- + List<Ref<Shape>> shapes;
- if (collision_map.has(mesh)) {
- shapes = collision_map[mesh];
- - } else if (_teststr(name, "colonly")) {
- - _gen_shape_list(mesh, shapes, false);
- - collision_map[mesh] = shapes;
- - } else if (_teststr(name, "convcolonly")) {
- - _gen_shape_list(mesh, shapes, true);
- + } else {
- + _gen_shape_list(mesh, shapes, node_hint == NODE_HINT_CONVEX_COLLISION_ONLY || node_hint == NODE_HINT_CONVEX_SHAPE_ONLY);
- collision_map[mesh] = shapes;
- }
- - if (_teststr(name, "colonly")) {
- - fixed_name = _fixstr(name, "colonly");
- - } else if (_teststr(name, "convcolonly")) {
- - fixed_name = _fixstr(name, "convcolonly");
- - }
- -
- - ERR_FAIL_COND_V(fixed_name == String(), NULL);
- + ERR_FAIL_COND_V(node_name == String(), nullptr);
- if (shapes.size()) {
- -
- - StaticBody *col = memnew(StaticBody);
- - col->set_transform(mi->get_transform());
- - col->set_name(fixed_name);
- - p_node->replace_by(col);
- - memdelete(p_node);
- - p_node = col;
- -
- - int idx = 0;
- - for (List<Ref<Shape> >::Element *E = shapes.front(); E; E = E->next()) {
- -
- - CollisionShape *cshape = memnew(CollisionShape);
- - cshape->set_shape(E->get());
- - col->add_child(cshape);
- -
- - cshape->set_name("shape" + itos(idx));
- - cshape->set_owner(col->get_owner());
- - idx++;
- + if (node_hint == NODE_HINT_COLLISION_ONLY || node_hint == NODE_HINT_CONVEX_COLLISION_ONLY) {
- + StaticBody *static_body = memnew(StaticBody);
- + static_body->set_transform(mesh_instance->get_transform());
- + static_body->set_name(node_name);
- + p_node->replace_by(static_body);
- + memdelete(p_node);
- + p_node = static_body;
- +
- + _add_shapes(static_body, shapes);
- + } else {
- + _add_shapes(p_root, shapes, node_name);
- + memdelete(p_node);
- + p_node = nullptr;
- }
- }
- }
- - } else if (p_node->has_meta("empty_draw_type")) {
- - String empty_draw_type = String(p_node->get_meta("empty_draw_type"));
- - StaticBody *sb = memnew(StaticBody);
- - sb->set_name(_fixstr(name, "colonly"));
- - Object::cast_to<Spatial>(sb)->set_transform(Object::cast_to<Spatial>(p_node)->get_transform());
- - p_node->replace_by(sb);
- - memdelete(p_node);
- - p_node = NULL;
- - CollisionShape *colshape = memnew(CollisionShape);
- - if (empty_draw_type == "CUBE") {
- - BoxShape *boxShape = memnew(BoxShape);
- - boxShape->set_extents(Vector3(1, 1, 1));
- - colshape->set_shape(boxShape);
- - colshape->set_name("BoxShape");
- - } else if (empty_draw_type == "SINGLE_ARROW") {
- - RayShape *rayShape = memnew(RayShape);
- - rayShape->set_length(1);
- - colshape->set_shape(rayShape);
- - colshape->set_name("RayShape");
- - Object::cast_to<Spatial>(sb)->rotate_x(Math_PI / 2);
- - } else if (empty_draw_type == "IMAGE") {
- - PlaneShape *planeShape = memnew(PlaneShape);
- - colshape->set_shape(planeShape);
- - colshape->set_name("PlaneShape");
- + } else if (CollisionShape *colshape = _shape_from_empty_meta(p_node); colshape) {
- + if (node_hint == NODE_HINT_COLLISION_ONLY || node_hint == NODE_HINT_CONVEX_COLLISION_ONLY) {
- + StaticBody *static_body = memnew(StaticBody);
- + static_body->set_name(node_name);
- + p_node->replace_by(static_body);
- + memdelete(p_node);
- + p_node = static_body;
- +
- + static_body->set_transform(Object::cast_to<Spatial>(p_node)->get_transform());
- + if (Object::cast_to<RayShape>(*colshape->get_shape())) {
- + static_body->rotate_x(Math_PI / 2);
- + }
- +
- + static_body->add_child(colshape);
- + colshape->set_owner(static_body->get_owner());
- } else {
- - SphereShape *sphereShape = memnew(SphereShape);
- - sphereShape->set_radius(1);
- - colshape->set_shape(sphereShape);
- - colshape->set_name("SphereShape");
- + p_node->replace_by(colshape);
- + memdelete(p_node);
- + p_node = colshape;
- }
- - sb->add_child(colshape);
- - colshape->set_owner(sb->get_owner());
- }
- - } else if (_teststr(name, "rigid") && Object::cast_to<MeshInstance>(p_node)) {
- -
- - if (isroot)
- + } else if (node_hint == NODE_HINT_RIGID && mesh_instance) {
- + if (is_root) {
- return p_node;
- + }
- - MeshInstance *mi = Object::cast_to<MeshInstance>(p_node);
- - Ref<Mesh> mesh = mi->get_mesh();
- + Ref<Mesh> mesh = mesh_instance->get_mesh();
- if (mesh.is_valid()) {
- - List<Ref<Shape> > shapes;
- + List<Ref<Shape>> shapes;
- if (collision_map.has(mesh)) {
- shapes = collision_map[mesh];
- } else {
- @@ -476,177 +351,138 @@ Node *ResourceImporterScene::_fix_node(Node *p_node, Node *p_root, Map<Ref<Mesh>
- }
- RigidBody *rigid_body = memnew(RigidBody);
- - rigid_body->set_name(_fixstr(name, "rigid"));
- + rigid_body->set_name(node_name);
- p_node->replace_by(rigid_body);
- - rigid_body->set_transform(mi->get_transform());
- + rigid_body->set_transform(mesh_instance->get_transform());
- p_node = rigid_body;
- - mi->set_name("mesh");
- - mi->set_transform(Transform());
- - rigid_body->add_child(mi);
- - mi->set_owner(rigid_body->get_owner());
- -
- - int idx = 0;
- - for (List<Ref<Shape> >::Element *E = shapes.front(); E; E = E->next()) {
- -
- - CollisionShape *cshape = memnew(CollisionShape);
- - cshape->set_shape(E->get());
- - rigid_body->add_child(cshape);
- + mesh_instance->set_transform(Transform());
- + rigid_body->add_child(mesh_instance);
- + mesh_instance->set_owner(rigid_body->get_owner());
- - cshape->set_name("shape" + itos(idx));
- - cshape->set_owner(p_node->get_owner());
- - idx++;
- - }
- + _add_shapes(rigid_body, shapes);
- }
- - } else if ((_teststr(name, "col") || (_teststr(name, "convcol"))) && Object::cast_to<MeshInstance>(p_node)) {
- -
- - MeshInstance *mi = Object::cast_to<MeshInstance>(p_node);
- -
- - Ref<Mesh> mesh = mi->get_mesh();
- + } else if ((node_hint == NODE_HINT_COLLISION || node_hint == NODE_HINT_CONVEX_COLLISION || node_hint == NODE_HINT_SHAPE || node_hint == NODE_HINT_CONVEX_SHAPE) && mesh_instance) {
- + Ref<Mesh> mesh = mesh_instance->get_mesh();
- if (mesh.is_valid()) {
- - List<Ref<Shape> > shapes;
- - String fixed_name;
- + List<Ref<Shape>> shapes;
- if (collision_map.has(mesh)) {
- shapes = collision_map[mesh];
- - } else if (_teststr(name, "col")) {
- - _gen_shape_list(mesh, shapes, false);
- - collision_map[mesh] = shapes;
- - } else if (_teststr(name, "convcol")) {
- - _gen_shape_list(mesh, shapes, true);
- + } else {
- + _gen_shape_list(mesh, shapes, node_hint == NODE_HINT_CONVEX_COLLISION || node_hint == NODE_HINT_CONVEX_SHAPE);
- collision_map[mesh] = shapes;
- }
- - if (_teststr(name, "col")) {
- - fixed_name = _fixstr(name, "col");
- - } else if (_teststr(name, "convcol")) {
- - fixed_name = _fixstr(name, "convcol");
- - }
- -
- - if (fixed_name != String()) {
- - if (mi->get_parent() && !mi->get_parent()->has_node(fixed_name)) {
- - mi->set_name(fixed_name);
- + if (node_name != String()) {
- + if (mesh_instance->get_parent()) {
- + mesh_instance->set_name(node_name);
- }
- }
- if (shapes.size()) {
- - StaticBody *col = memnew(StaticBody);
- - col->set_name("static_collision");
- - mi->add_child(col);
- - col->set_owner(mi->get_owner());
- -
- - int idx = 0;
- - for (List<Ref<Shape> >::Element *E = shapes.front(); E; E = E->next()) {
- -
- - CollisionShape *cshape = memnew(CollisionShape);
- - cshape->set_shape(E->get());
- - col->add_child(cshape);
- -
- - cshape->set_name("shape" + itos(idx));
- - cshape->set_owner(p_node->get_owner());
- -
- - idx++;
- + if (node_hint == NODE_HINT_COLLISION || node_hint == NODE_HINT_CONVEX_COLLISION) {
- + StaticBody *static_body = memnew(StaticBody);
- + mesh_instance->add_child(static_body);
- + static_body->set_owner(mesh_instance->get_owner());
- +
- + _add_shapes(static_body, shapes);
- + } else {
- + switch (ProjectSettings::get_singleton()->get("node/name_casing").operator int()) {
- + case Node::NAME_CASING_PASCAL_CASE:
- + case Node::NAME_CASING_CAMEL_CASE:
- + _add_shapes(p_root, shapes, node_name + "Shape");
- + break;
- + case Node::NAME_CASING_SNAKE_CASE:
- + _add_shapes(p_root, shapes, node_name + "_shape");
- + break;
- + }
- }
- }
- }
- - } else if (_teststr(name, "navmesh") && Object::cast_to<MeshInstance>(p_node)) {
- -
- - if (isroot)
- + } else if (node_hint == NODE_HINT_NAVMESH && mesh_instance) {
- + if (is_root) {
- return p_node;
- + }
- - MeshInstance *mi = Object::cast_to<MeshInstance>(p_node);
- -
- - Ref<ArrayMesh> mesh = mi->get_mesh();
- - ERR_FAIL_COND_V(mesh.is_null(), NULL);
- + Ref<ArrayMesh> mesh = mesh_instance->get_mesh();
- + ERR_FAIL_COND_V(mesh.is_null(), nullptr);
- NavigationMeshInstance *nmi = memnew(NavigationMeshInstance);
- - nmi->set_name(_fixstr(name, "navmesh"));
- + nmi->set_name(node_name);
- Ref<NavigationMesh> nmesh = memnew(NavigationMesh);
- nmesh->create_from_mesh(mesh);
- nmi->set_navigation_mesh(nmesh);
- - Object::cast_to<Spatial>(nmi)->set_transform(mi->get_transform());
- + Object::cast_to<Spatial>(nmi)->set_transform(mesh_instance->get_transform());
- p_node->replace_by(nmi);
- memdelete(p_node);
- p_node = nmi;
- - } else if (_teststr(name, "vehicle")) {
- -
- - if (isroot)
- + } else if (node_hint == NODE_HINT_VEHICLE) {
- + if (p_node != p_root) {
- return p_node;
- + }
- Node *owner = p_node->get_owner();
- - Spatial *s = Object::cast_to<Spatial>(p_node);
- - VehicleBody *bv = memnew(VehicleBody);
- - String n = _fixstr(p_node->get_name(), "vehicle");
- - bv->set_name(n);
- - p_node->replace_by(bv);
- - p_node->set_name(n);
- - bv->add_child(p_node);
- - bv->set_owner(owner);
- + Spatial *spatial = Object::cast_to<Spatial>(p_node);
- + VehicleBody *vehicle_body = memnew(VehicleBody);
- + vehicle_body->set_name(node_name);
- + p_node->replace_by(vehicle_body);
- + p_node->set_name(node_name);
- + vehicle_body->add_child(p_node);
- + vehicle_body->set_owner(owner);
- p_node->set_owner(owner);
- - bv->set_transform(s->get_transform());
- - s->set_transform(Transform());
- + vehicle_body->set_transform(spatial->get_transform());
- + spatial->set_transform(Transform());
- - p_node = bv;
- + p_node = vehicle_body;
- - } else if (_teststr(name, "wheel")) {
- -
- - if (isroot)
- + } else if (node_hint == NODE_HINT_WHEEL) {
- + if (is_root) {
- return p_node;
- + }
- Node *owner = p_node->get_owner();
- - Spatial *s = Object::cast_to<Spatial>(p_node);
- - VehicleWheel *bv = memnew(VehicleWheel);
- - String n = _fixstr(p_node->get_name(), "wheel");
- - bv->set_name(n);
- - p_node->replace_by(bv);
- - p_node->set_name(n);
- - bv->add_child(p_node);
- - bv->set_owner(owner);
- + Spatial *spatial = Object::cast_to<Spatial>(p_node);
- + VehicleWheel *vehicle_wheel = memnew(VehicleWheel);
- + vehicle_wheel->set_name(node_name);
- + p_node->replace_by(vehicle_wheel);
- + p_node->set_name(node_name);
- + vehicle_wheel->add_child(p_node);
- + vehicle_wheel->set_owner(owner);
- p_node->set_owner(owner);
- - bv->set_transform(s->get_transform());
- - s->set_transform(Transform());
- + vehicle_wheel->set_transform(spatial->get_transform());
- + spatial->set_transform(Transform());
- - p_node = bv;
- -
- - } else if (Object::cast_to<MeshInstance>(p_node)) {
- + p_node = vehicle_wheel;
- + } else if (mesh_instance) {
- //last attempt, maybe collision inside the mesh data
- -
- - MeshInstance *mi = Object::cast_to<MeshInstance>(p_node);
- -
- - Ref<ArrayMesh> mesh = mi->get_mesh();
- + Ref<ArrayMesh> mesh = mesh_instance->get_mesh();
- if (!mesh.is_null()) {
- -
- - List<Ref<Shape> > shapes;
- + auto [mesh_name, mesh_hint] = _get_node_import_hint(mesh->get_name());
- + List<Ref<Shape>> shapes;
- if (collision_map.has(mesh)) {
- shapes = collision_map[mesh];
- - } else if (_teststr(mesh->get_name(), "col")) {
- + } else if (mesh_hint == NODE_HINT_COLLISION) {
- _gen_shape_list(mesh, shapes, false);
- collision_map[mesh] = shapes;
- - mesh->set_name(_fixstr(mesh->get_name(), "col"));
- - } else if (_teststr(mesh->get_name(), "convcol")) {
- + mesh->set_name(mesh_name);
- + } else if (mesh_hint == NODE_HINT_CONVEX_COLLISION) {
- _gen_shape_list(mesh, shapes, true);
- collision_map[mesh] = shapes;
- - mesh->set_name(_fixstr(mesh->get_name(), "convcol"));
- + mesh->set_name(node_name);
- }
- if (shapes.size()) {
- - StaticBody *col = memnew(StaticBody);
- - col->set_name("static_collision");
- - p_node->add_child(col);
- - col->set_owner(p_node->get_owner());
- -
- - int idx = 0;
- - for (List<Ref<Shape> >::Element *E = shapes.front(); E; E = E->next()) {
- -
- - CollisionShape *cshape = memnew(CollisionShape);
- - cshape->set_shape(E->get());
- - col->add_child(cshape);
- -
- - cshape->set_name("shape" + itos(idx));
- - cshape->set_owner(p_node->get_owner());
- - idx++;
- + if (mesh_hint == NODE_HINT_COLLISION || mesh_hint == NODE_HINT_CONVEX_COLLISION) {
- + StaticBody *static_body = memnew(StaticBody);
- + p_node->add_child(static_body);
- + static_body->set_owner(p_node->get_owner());
- +
- + _add_shapes(static_body, shapes);
- + } else {
- + _add_shapes(p_root, shapes);
- }
- }
- }
- @@ -1192,6 +1028,98 @@ void ResourceImporterScene::get_import_options(List<ImportOption> *r_options, in
- }
- }
- +String ResourceImporterScene::_import_hint_string(ResourceImporterScene::NodeHints p_hint) {
- + switch (p_hint) {
- + case NODE_HINT_NONE:
- + return "";
- + case NODE_HINT_NO_IMPORT:
- + return "noimp";
- + case NODE_HINT_COLLISION:
- + return "col";
- + case NODE_HINT_CONVEX_COLLISION:
- + return "convcol";
- + case NODE_HINT_COLLISION_ONLY:
- + return "colonly";
- + case NODE_HINT_CONVEX_COLLISION_ONLY:
- + return "convcolonly";
- + case NODE_HINT_SHAPE:
- + return "shape";
- + case NODE_HINT_CONVEX_SHAPE:
- + return "convshape";
- + case NODE_HINT_SHAPE_ONLY:
- + return "shapeonly";
- + case NODE_HINT_CONVEX_SHAPE_ONLY:
- + return "convshapeonly";
- + case NODE_HINT_RIGID:
- + return "rigid";
- + case NODE_HINT_NAVMESH:
- + return "navmesh";
- + case NODE_HINT_VEHICLE:
- + return "vehicle";
- + case NODE_HINT_WHEEL:
- + return "wheel";
- + default:
- + ERR_FAIL_V_MSG(String(), "Node import hint out of range");
- + }
- +}
- +
- +String ResourceImporterScene::_import_hint_string(ResourceImporterScene::MaterialHints p_hint) {
- + switch (p_hint) {
- + case MATERIAL_HINT_ALPHA:
- + return "alpha";
- + case MATERIAL_HINT_VERTEX_COLOR:
- + return "vcol";
- + default:
- + ERR_FAIL_V_MSG(String(), "Material import hint out of range");
- + }
- +}
- +
- +Pair<String, ResourceImporterScene::NodeHints> ResourceImporterScene::_get_node_import_hint(String p_name) {
- + for (int enum_index = 1; enum_index < NODE_HINT_LAST; ++enum_index) {
- + auto hint = static_cast<NodeHints>(enum_index);
- + String hint_suffix = '-' + _import_hint_string(hint);
- + if (p_name.ends_with(hint_suffix)) {
- + p_name.erase(p_name.length() - hint_suffix.length(), hint_suffix.size() - 1);
- + return { p_name, hint };
- + }
- + }
- + return { p_name, NODE_HINT_NONE };
- +}
- +
- +Pair<String, Vector<ResourceImporterScene::MaterialHints>> ResourceImporterScene::_get_material_import_hints(const String &p_name) {
- + Vector<MaterialHints> hints;
- + Vector<String> name_parts = p_name.split("-");
- + for (int enum_index = 1; enum_index < MATERIAL_HINT_LAST; ++enum_index) {
- + auto hint = static_cast<MaterialHints>(enum_index);
- + int part_index = name_parts.find(_import_hint_string(hint));
- + if (part_index != -1) {
- + hints.push_back(hint);
- + name_parts.remove(part_index);
- + }
- + }
- +
- + String fixed_name;
- + for (int i = 0; i < name_parts.size(); ++i) {
- + fixed_name += name_parts[i];
- + if (i != 0)
- + fixed_name += '-';
- + }
- +
- + return { fixed_name, hints };
- +}
- +
- +void ResourceImporterScene::_add_shapes(Node *p_node, List<Ref<Shape>> p_shapes, const String &basename) {
- + for (List<Ref<Shape>>::Element *E = p_shapes.front(); E; E = E->next()) {
- + CollisionShape *cshape = memnew(CollisionShape);
- + cshape->set_shape(E->get());
- + if (!basename.empty())
- + cshape->set_name(basename);
- +
- + p_node->add_child(cshape);
- + cshape->set_owner(p_node->get_owner() ? p_node->get_owner() : p_node);
- + }
- +}
- +
- void ResourceImporterScene::_replace_owner(Node *p_node, Node *p_scene, Node *p_new_owner) {
- if (p_node != p_new_owner && p_node->get_owner() == p_scene) {
- @@ -1204,6 +1132,86 @@ void ResourceImporterScene::_replace_owner(Node *p_node, Node *p_scene, Node *p_
- }
- }
- +CollisionShape *ResourceImporterScene::_shape_from_empty_meta(Node *p_node) {
- + if (!p_node->has_meta("empty_draw_type")) {
- + return nullptr;
- + }
- +
- + const String empty_draw_type = p_node->get_meta("empty_draw_type");
- + CollisionShape *colshape = memnew(CollisionShape);
- + if (empty_draw_type == "CUBE") {
- + BoxShape *box_shape = memnew(BoxShape);
- + box_shape->set_extents(Vector3(1, 1, 1));
- + colshape->set_shape(box_shape);
- + } else if (empty_draw_type == "SINGLE_ARROW") {
- + RayShape *ray_shape = memnew(RayShape);
- + ray_shape->set_length(1);
- + colshape->set_shape(ray_shape);
- + } else if (empty_draw_type == "IMAGE") {
- + PlaneShape *plane_shape = memnew(PlaneShape);
- + colshape->set_shape(plane_shape);
- + } else {
- + SphereShape *sphere_shape = memnew(SphereShape);
- + sphere_shape->set_radius(1);
- + colshape->set_shape(sphere_shape);
- + }
- +
- + return colshape;
- +}
- +
- +void ResourceImporterScene::_fix_materials(MeshInstance *p_mesh, LightBakeMode p_light_bake_mode) {
- + Ref<ArrayMesh> array_mesh = p_mesh->get_mesh();
- + if (array_mesh.is_valid()) {
- + for (int surface_index = 0; surface_index < array_mesh->get_surface_count(); ++surface_index) {
- + Ref<SpatialMaterial> mat = array_mesh->surface_get_material(surface_index);
- + if (!mat.is_valid()) {
- + continue;
- + }
- +
- + auto [material_name, material_hints] = _get_material_import_hints(mat->get_name());
- + for (int hint_index = 0; hint_index < material_hints.size(); ++hint_index) {
- + switch (material_hints[hint_index]) {
- + case MATERIAL_HINT_ALPHA:
- + mat->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
- + break;
- + case MATERIAL_HINT_VERTEX_COLOR:
- + mat->set_flag(SpatialMaterial::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
- + mat->set_flag(SpatialMaterial::FLAG_SRGB_VERTEX_COLOR, true);
- + break;
- + default:
- + break;
- + }
- + }
- + mat->set_name(material_name);
- + }
- + }
- +
- + if (p_light_bake_mode != LIGHT_BAKE_DISABLED) {
- + p_mesh->set_flag(GeometryInstance::FLAG_USE_BAKED_LIGHT, true);
- + }
- +}
- +
- +void ResourceImporterScene::_fix_animations(AnimationPlayer *p_player) {
- + //remove animations referencing non-importable nodes
- + List<StringName> anims;
- + p_player->get_animation_list(&anims);
- + for (List<StringName>::Element *E = anims.front(); E; E = E->next()) {
- + Ref<Animation> anim = p_player->get_animation(E->get());
- + ERR_CONTINUE(anim.is_null());
- + for (int i = 0; i < anim->get_track_count(); i++) {
- + NodePath path = anim->track_get_path(i);
- +
- + for (int j = 0; j < path.get_name_count(); j++) {
- + if (_get_node_import_hint(path.get_name(j)).second == NODE_HINT_NO_IMPORT) {
- + anim->remove_track(i);
- + i--;
- + break;
- + }
- + }
- + }
- + }
- +}
- +
- Node *ResourceImporterScene::import_scene_from_other_importer(EditorSceneImporter *p_exception, const String &p_path, uint32_t p_flags, int p_bake_fps) {
- Ref<EditorSceneImporter> importer;
- diff --git a/editor/import/resource_importer_scene.h b/editor/import/resource_importer_scene.h
- index 20e7af1..03e0d29 100644
- --- a/editor/import/resource_importer_scene.h
- +++ b/editor/import/resource_importer_scene.h
- @@ -36,7 +36,10 @@
- #include "scene/resources/mesh.h"
- #include "scene/resources/shape.h"
- +class AnimationPlayer;
- class Material;
- +class MeshInstance;
- +class CollisionShape;
- class EditorSceneImporter : public Reference {
- @@ -120,7 +123,44 @@ class ResourceImporterScene : public ResourceImporter {
- LIGHT_BAKE_LIGHTMAPS
- };
- - void _replace_owner(Node *p_node, Node *p_scene, Node *p_new_owner);
- + enum NodeHints {
- + NODE_HINT_NONE,
- + NODE_HINT_NO_IMPORT,
- + NODE_HINT_COLLISION,
- + NODE_HINT_CONVEX_COLLISION,
- + NODE_HINT_COLLISION_ONLY,
- + NODE_HINT_CONVEX_COLLISION_ONLY,
- + NODE_HINT_SHAPE,
- + NODE_HINT_CONVEX_SHAPE,
- + NODE_HINT_SHAPE_ONLY,
- + NODE_HINT_CONVEX_SHAPE_ONLY,
- + NODE_HINT_RIGID,
- + NODE_HINT_NAVMESH,
- + NODE_HINT_VEHICLE,
- + NODE_HINT_WHEEL,
- + NODE_HINT_LAST,
- + };
- +
- + enum MaterialHints {
- + MATERIAL_HINT_NONE,
- + MATERIAL_HINT_ALPHA,
- + MATERIAL_HINT_VERTEX_COLOR,
- + MATERIAL_HINT_LAST,
- + };
- +
- + static String _import_hint_string(NodeHints p_hint);
- + static String _import_hint_string(MaterialHints p_hint);
- +
- + static Pair<String, NodeHints> _get_node_import_hint(String p_name);
- + static Pair<String, Vector<MaterialHints>> _get_material_import_hints(const String &p_name);
- +
- + static void _gen_shape_list(const Ref<Mesh> &p_mesh, List<Ref<Shape>> &r_shape_list, bool p_convex);
- + static void _add_shapes(Node *p_node, List<Ref<Shape>> p_shapes, const String &basename = {});
- + static void _replace_owner(Node *p_node, Node *p_scene, Node *p_new_owner);
- + static CollisionShape *_shape_from_empty_meta(Node *p_node);
- +
- + static void _fix_materials(MeshInstance *p_mesh, LightBakeMode p_light_bake_mode);
- + static void _fix_animations(AnimationPlayer *p_player);
- public:
- static ResourceImporterScene *get_singleton() { return singleton; }
- @@ -147,7 +187,7 @@ public:
- void _make_external_resources(Node *p_node, const String &p_base_path, bool p_make_animations, bool p_animations_as_text, bool p_keep_animations, bool p_make_materials, bool p_materials_as_text, bool p_keep_materials, bool p_make_meshes, bool p_meshes_as_text, Map<Ref<Animation>, Ref<Animation> > &p_animations, Map<Ref<Material>, Ref<Material> > &p_materials, Map<Ref<ArrayMesh>, Ref<ArrayMesh> > &p_meshes);
- - Node *_fix_node(Node *p_node, Node *p_root, Map<Ref<Mesh>, List<Ref<Shape> > > &collision_map, LightBakeMode p_light_bake_mode);
- + Node *_fix_node(Node *p_node, Node *p_root, Map<Ref<Mesh>, List<Ref<Shape>>> &collision_map, LightBakeMode p_light_bake_mode);
- void _create_clips(Node *scene, const Array &p_clips, bool p_bake_all);
- void _filter_anim_tracks(Ref<Animation> anim, Set<String> &keep);
- diff --git a/scene/main/node.h b/scene/main/node.h
- index 6f5544d..3017759 100644
- --- a/scene/main/node.h
- +++ b/scene/main/node.h
- @@ -46,6 +46,8 @@ class Node : public Object {
- GDCLASS(Node, Object);
- OBJ_CATEGORY("Nodes");
- + friend class ResourceImporterScene;
- +
- public:
- enum PauseMode {
Add Comment
Please, Sign In to add comment