2017-03-05 22:47:28 +08:00
|
|
|
/*************************************************************************/
|
|
|
|
/* resource_importer_scene.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* http://www.godotengine.org */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2017-04-08 06:11:42 +08:00
|
|
|
/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
|
2017-03-05 22:47:28 +08:00
|
|
|
/* */
|
|
|
|
/* 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. */
|
|
|
|
/*************************************************************************/
|
2017-02-04 20:48:04 +08:00
|
|
|
#include "resource_importer_scene.h"
|
|
|
|
|
2017-03-05 21:21:25 +08:00
|
|
|
#include "editor/editor_node.h"
|
2017-03-05 23:44:50 +08:00
|
|
|
#include "io/resource_saver.h"
|
|
|
|
#include "scene/resources/packed_scene.h"
|
2017-02-04 20:48:04 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
#include "scene/3d/body_shape.h"
|
2017-02-04 20:48:04 +08:00
|
|
|
#include "scene/3d/mesh_instance.h"
|
|
|
|
#include "scene/3d/navigation.h"
|
|
|
|
#include "scene/3d/physics_body.h"
|
|
|
|
#include "scene/3d/portal.h"
|
2017-03-05 23:44:50 +08:00
|
|
|
#include "scene/3d/room_instance.h"
|
2017-02-04 20:48:04 +08:00
|
|
|
#include "scene/3d/vehicle_body.h"
|
|
|
|
#include "scene/resources/box_shape.h"
|
|
|
|
#include "scene/resources/plane_shape.h"
|
2017-03-05 23:44:50 +08:00
|
|
|
#include "scene/resources/ray_shape.h"
|
|
|
|
#include "scene/resources/sphere_shape.h"
|
2017-02-04 20:48:04 +08:00
|
|
|
|
|
|
|
void EditorScenePostImport::_bind_methods() {
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
BIND_VMETHOD(MethodInfo("post_import", PropertyInfo(Variant::OBJECT, "scene")));
|
2017-02-04 20:48:04 +08:00
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
Node *EditorScenePostImport::post_import(Node *p_scene) {
|
2017-02-04 20:48:04 +08:00
|
|
|
|
|
|
|
if (get_script_instance())
|
2017-03-05 23:44:50 +08:00
|
|
|
return get_script_instance()->call("post_import", p_scene);
|
2017-02-04 20:48:04 +08:00
|
|
|
|
|
|
|
return p_scene;
|
|
|
|
}
|
|
|
|
|
|
|
|
EditorScenePostImport::EditorScenePostImport() {
|
|
|
|
}
|
|
|
|
|
|
|
|
String ResourceImporterScene::get_importer_name() const {
|
|
|
|
|
|
|
|
return "scene";
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
String ResourceImporterScene::get_visible_name() const {
|
2017-02-04 20:48:04 +08:00
|
|
|
|
|
|
|
return "Scene";
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
void ResourceImporterScene::get_recognized_extensions(List<String> *p_extensions) const {
|
2017-02-04 20:48:04 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
for (Set<Ref<EditorSceneImporter> >::Element *E = importers.front(); E; E = E->next()) {
|
2017-02-04 20:48:04 +08:00
|
|
|
E->get()->get_extensions(p_extensions);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
String ResourceImporterScene::get_save_extension() const {
|
|
|
|
return "scn";
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
String ResourceImporterScene::get_resource_type() const {
|
2017-02-04 20:48:04 +08:00
|
|
|
|
|
|
|
return "PackedScene";
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
bool ResourceImporterScene::get_option_visibility(const String &p_option, const Map<StringName, Variant> &p_options) const {
|
2017-02-04 20:48:04 +08:00
|
|
|
|
|
|
|
if (p_option.begins_with("animation/")) {
|
2017-03-05 23:44:50 +08:00
|
|
|
if (p_option != "animation/import" && !bool(p_options["animation/import"]))
|
2017-02-04 20:48:04 +08:00
|
|
|
return false;
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
if (p_option.begins_with("animation/optimizer/") && p_option != "animation/optimizer/enabled" && !bool(p_options["animation/optimizer/enabled"]))
|
2017-02-04 20:48:04 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (p_option.begins_with("animation/clip_")) {
|
|
|
|
int max_clip = p_options["animation/clips/amount"];
|
2017-03-05 23:44:50 +08:00
|
|
|
int clip = p_option.get_slice("/", 1).get_slice("_", 1).to_int() - 1;
|
|
|
|
if (clip >= max_clip)
|
2017-02-04 20:48:04 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ResourceImporterScene::get_preset_count() const {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
String ResourceImporterScene::get_preset_name(int p_idx) const {
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
static bool _teststr(const String &p_what, const String &p_str) {
|
2017-02-04 20:48:04 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
if (p_what.findn("$" + p_str) != -1) //blender and other stuff
|
2017-02-04 20:48:04 +08:00
|
|
|
return true;
|
2017-03-05 23:44:50 +08:00
|
|
|
if (p_what.to_lower().ends_with("-" + p_str)) //collada only supports "_" and "-" besides letters
|
2017-02-04 20:48:04 +08:00
|
|
|
return true;
|
2017-03-05 23:44:50 +08:00
|
|
|
if (p_what.to_lower().ends_with("_" + p_str)) //collada only supports "_" and "-" besides letters
|
2017-02-04 20:48:04 +08:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
static String _fixstr(const String &p_what, const String &p_str) {
|
2017-02-04 20:48:04 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
if (p_what.findn("$" + p_str) != -1) //blender and other stuff
|
|
|
|
return p_what.replace("$" + p_str, "");
|
|
|
|
if (p_what.to_lower().ends_with("-" + p_str)) //collada only supports "_" and "-" besides letters
|
|
|
|
return p_what.substr(0, p_what.length() - (p_str.length() + 1));
|
|
|
|
if (p_what.to_lower().ends_with("_" + p_str)) //collada only supports "_" and "-" besides letters
|
|
|
|
return p_what.substr(0, p_what.length() - (p_str.length() + 1));
|
2017-02-04 20:48:04 +08:00
|
|
|
return p_what;
|
|
|
|
}
|
|
|
|
|
2017-06-08 05:18:55 +08:00
|
|
|
Node *ResourceImporterScene::_fix_node(Node *p_node, Node *p_root, Map<Ref<ArrayMesh>, Ref<Shape> > &collision_map) {
|
2017-02-04 20:48:04 +08:00
|
|
|
|
|
|
|
// children first..
|
2017-03-05 23:44:50 +08:00
|
|
|
for (int i = 0; i < p_node->get_child_count(); i++) {
|
2017-02-04 20:48:04 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
Node *r = _fix_node(p_node->get_child(i), p_root, collision_map);
|
2017-02-04 20:48:04 +08:00
|
|
|
if (!r) {
|
|
|
|
print_line("was erased..");
|
|
|
|
i--; //was erased
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
String name = p_node->get_name();
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
bool isroot = p_node == p_root;
|
2017-02-04 20:48:04 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
if (!isroot && _teststr(name, "noimp")) {
|
2017-02-04 20:48:04 +08:00
|
|
|
|
|
|
|
memdelete(p_node);
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-04-07 10:36:37 +08:00
|
|
|
#if 0
|
2017-02-04 20:48:04 +08:00
|
|
|
if (p_node->cast_to<MeshInstance>()) {
|
|
|
|
|
|
|
|
MeshInstance *mi = p_node->cast_to<MeshInstance>();
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
bool bb = false;
|
2017-02-04 20:48:04 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
if ((_teststr(name, "bb"))) {
|
|
|
|
bb = true;
|
|
|
|
} else if (mi->get_mesh().is_valid() && (_teststr(mi->get_mesh()->get_name(), "bb"))) {
|
|
|
|
bb = true;
|
2017-02-04 20:48:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (bb) {
|
2017-03-05 23:44:50 +08:00
|
|
|
mi->set_flag(GeometryInstance::FLAG_BILLBOARD, true);
|
2017-02-04 20:48:04 +08:00
|
|
|
if (mi->get_mesh().is_valid()) {
|
|
|
|
|
2017-06-08 05:18:55 +08:00
|
|
|
Ref<ArrayMesh> m = mi->get_mesh();
|
2017-03-05 23:44:50 +08:00
|
|
|
for (int i = 0; i < m->get_surface_count(); i++) {
|
2017-02-04 20:48:04 +08:00
|
|
|
|
2017-04-07 10:36:37 +08:00
|
|
|
Ref<SpatialMaterial> fm = m->surface_get_material(i);
|
2017-02-04 20:48:04 +08:00
|
|
|
if (fm.is_valid()) {
|
|
|
|
//fm->set_flag(Material::FLAG_UNSHADED,true);
|
|
|
|
//fm->set_flag(Material::FLAG_DOUBLE_SIDED,true);
|
|
|
|
//fm->set_depth_draw_mode(Material::DEPTH_DRAW_NEVER);
|
2017-04-07 10:36:37 +08:00
|
|
|
//fm->set_fixed_flag(SpatialMaterial::FLAG_USE_ALPHA,true);
|
2017-02-04 20:48:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-04-07 10:36:37 +08:00
|
|
|
#endif
|
2017-02-04 20:48:04 +08:00
|
|
|
if (p_node->cast_to<MeshInstance>()) {
|
|
|
|
|
|
|
|
MeshInstance *mi = p_node->cast_to<MeshInstance>();
|
|
|
|
|
2017-06-08 05:18:55 +08:00
|
|
|
Ref<ArrayMesh> m = mi->get_mesh();
|
2017-02-04 20:48:04 +08:00
|
|
|
|
|
|
|
if (m.is_valid()) {
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
for (int i = 0; i < m->get_surface_count(); i++) {
|
2017-02-04 20:48:04 +08:00
|
|
|
|
2017-04-07 10:36:37 +08:00
|
|
|
Ref<SpatialMaterial> mat = m->surface_get_material(i);
|
2017-02-04 20:48:04 +08:00
|
|
|
if (!mat.is_valid())
|
|
|
|
continue;
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
if (_teststr(mat->get_name(), "alpha")) {
|
2017-02-04 20:48:04 +08:00
|
|
|
|
2017-04-07 10:36:37 +08:00
|
|
|
mat->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
|
2017-03-05 23:44:50 +08:00
|
|
|
mat->set_name(_fixstr(mat->get_name(), "alpha"));
|
2017-02-04 20:48:04 +08:00
|
|
|
}
|
2017-03-05 23:44:50 +08:00
|
|
|
if (_teststr(mat->get_name(), "vcol")) {
|
2017-02-04 20:48:04 +08:00
|
|
|
|
2017-04-07 10:36:37 +08:00
|
|
|
mat->set_flag(SpatialMaterial::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
|
|
|
|
mat->set_flag(SpatialMaterial::FLAG_SRGB_VERTEX_COLOR, true);
|
2017-03-05 23:44:50 +08:00
|
|
|
mat->set_name(_fixstr(mat->get_name(), "vcol"));
|
2017-02-04 20:48:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p_node->cast_to<AnimationPlayer>()) {
|
|
|
|
//remove animations referencing non-importable nodes
|
|
|
|
AnimationPlayer *ap = p_node->cast_to<AnimationPlayer>();
|
|
|
|
|
|
|
|
List<StringName> anims;
|
|
|
|
ap->get_animation_list(&anims);
|
2017-03-05 23:44:50 +08:00
|
|
|
for (List<StringName>::Element *E = anims.front(); E; E = E->next()) {
|
2017-02-04 20:48:04 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
Ref<Animation> anim = ap->get_animation(E->get());
|
2017-02-04 20:48:04 +08:00
|
|
|
ERR_CONTINUE(anim.is_null());
|
2017-03-05 23:44:50 +08:00
|
|
|
for (int i = 0; i < anim->get_track_count(); i++) {
|
2017-02-04 20:48:04 +08:00
|
|
|
NodePath path = anim->track_get_path(i);
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
for (int j = 0; j < path.get_name_count(); j++) {
|
2017-02-04 20:48:04 +08:00
|
|
|
String node = path.get_name(j);
|
2017-03-05 23:44:50 +08:00
|
|
|
if (_teststr(node, "noimp")) {
|
2017-02-04 20:48:04 +08:00
|
|
|
anim->remove_track(i);
|
|
|
|
i--;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-04-07 10:36:37 +08:00
|
|
|
#if 0
|
2017-02-04 20:48:04 +08:00
|
|
|
if (p_node->cast_to<MeshInstance>()) {
|
|
|
|
|
|
|
|
MeshInstance *mi = p_node->cast_to<MeshInstance>();
|
|
|
|
|
|
|
|
String str;
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
if ((_teststr(name, "imp"))) {
|
|
|
|
str = name;
|
|
|
|
} else if (mi->get_mesh().is_valid() && (_teststr(mi->get_mesh()->get_name(), "imp"))) {
|
|
|
|
str = mi->get_mesh()->get_name();
|
2017-02-04 20:48:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (p_node->get_parent() && p_node->get_parent()->cast_to<MeshInstance>()) {
|
|
|
|
MeshInstance *mi = p_node->cast_to<MeshInstance>();
|
|
|
|
MeshInstance *mip = p_node->get_parent()->cast_to<MeshInstance>();
|
2017-03-05 23:44:50 +08:00
|
|
|
String d = str.substr(str.find("imp") + 3, str.length());
|
|
|
|
if (d != "") {
|
|
|
|
if ((d[0] < '0' || d[0] > '9'))
|
|
|
|
d = d.substr(1, d.length());
|
|
|
|
if (d.length() && d[0] >= '0' && d[0] <= '9') {
|
2017-02-04 20:48:04 +08:00
|
|
|
float dist = d.to_double();
|
2017-03-05 23:44:50 +08:00
|
|
|
mi->set_flag(GeometryInstance::FLAG_BILLBOARD, true);
|
|
|
|
mi->set_flag(GeometryInstance::FLAG_BILLBOARD_FIX_Y, true);
|
2017-02-04 20:48:04 +08:00
|
|
|
//mi->set_draw_range_begin(dist);
|
|
|
|
//mi->set_draw_range_end(100000);
|
|
|
|
|
|
|
|
//mip->set_draw_range_begin(0);
|
|
|
|
//mip->set_draw_range_end(dist);
|
|
|
|
|
|
|
|
if (mi->get_mesh().is_valid()) {
|
|
|
|
|
2017-06-08 05:18:55 +08:00
|
|
|
Ref<ArrayMesh> m = mi->get_mesh();
|
2017-03-05 23:44:50 +08:00
|
|
|
for (int i = 0; i < m->get_surface_count(); i++) {
|
2017-02-04 20:48:04 +08:00
|
|
|
|
2017-04-07 10:36:37 +08:00
|
|
|
Ref<SpatialMaterial> fm = m->surface_get_material(i);
|
2017-02-04 20:48:04 +08:00
|
|
|
if (fm.is_valid()) {
|
|
|
|
//fm->set_flag(Material::FLAG_UNSHADED,true);
|
|
|
|
//fm->set_flag(Material::FLAG_DOUBLE_SIDED,true);
|
|
|
|
//fm->set_depth_draw_mode(Material::DEPTH_DRAW_NEVER);
|
2017-04-07 10:36:37 +08:00
|
|
|
//fm->set_fixed_flag(SpatialMaterial::FLAG_USE_ALPHA,true);
|
2017-02-04 20:48:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-04-07 10:36:37 +08:00
|
|
|
|
|
|
|
#endif
|
2017-02-04 20:48:04 +08:00
|
|
|
#if 0
|
|
|
|
if (p_flags&SCENE_FLAG_CREATE_LODS && p_node->cast_to<MeshInstance>()) {
|
|
|
|
|
|
|
|
MeshInstance *mi = p_node->cast_to<MeshInstance>();
|
|
|
|
|
|
|
|
String str;
|
|
|
|
|
|
|
|
if ((_teststr(name,"lod"))) {
|
|
|
|
str=name;
|
|
|
|
} else if (mi->get_mesh().is_valid() && (_teststr(mi->get_mesh()->get_name(),"lod"))) {
|
|
|
|
str=mi->get_mesh()->get_name();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (p_node->get_parent() && p_node->get_parent()->cast_to<MeshInstance>()) {
|
|
|
|
MeshInstance *mi = p_node->cast_to<MeshInstance>();
|
|
|
|
MeshInstance *mip = p_node->get_parent()->cast_to<MeshInstance>();
|
|
|
|
String d=str.substr(str.find("lod")+3,str.length());
|
|
|
|
if (d!="") {
|
|
|
|
if ((d[0]<'0' || d[0]>'9'))
|
|
|
|
d=d.substr(1,d.length());
|
|
|
|
if (d.length() && d[0]>='0' && d[0]<='9') {
|
|
|
|
float dist = d.to_double();
|
|
|
|
/// mi->set_draw_range_begin(dist);
|
|
|
|
// mi->set_draw_range_end(100000);
|
|
|
|
|
|
|
|
// mip->set_draw_range_begin(0);
|
|
|
|
// mip->set_draw_range_end(dist);
|
|
|
|
|
|
|
|
/*if (mi->get_mesh().is_valid()) {
|
|
|
|
|
2017-06-08 05:18:55 +08:00
|
|
|
Ref<ArrayMesh> m = mi->get_mesh();
|
2017-02-04 20:48:04 +08:00
|
|
|
for(int i=0;i<m->get_surface_count();i++) {
|
|
|
|
|
2017-04-07 10:36:37 +08:00
|
|
|
Ref<SpatialMaterial> fm = m->surface_get_material(i);
|
2017-02-04 20:48:04 +08:00
|
|
|
if (fm.is_valid()) {
|
|
|
|
fm->set_flag(Material::FLAG_UNSHADED,true);
|
|
|
|
fm->set_flag(Material::FLAG_DOUBLE_SIDED,true);
|
|
|
|
fm->set_hint(Material::HINT_NO_DEPTH_DRAW,true);
|
2017-04-07 10:36:37 +08:00
|
|
|
fm->set_fixed_flag(SpatialMaterial::FLAG_USE_ALPHA,true);
|
2017-02-04 20:48:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (p_flags&SCENE_FLAG_DETECT_LIGHTMAP_LAYER && _teststr(name,"lm") && p_node->cast_to<MeshInstance>()) {
|
|
|
|
|
|
|
|
MeshInstance *mi = p_node->cast_to<MeshInstance>();
|
|
|
|
|
|
|
|
String str=name;
|
|
|
|
int layer = str.substr(str.find("lm")+3,str.length()).to_int();
|
|
|
|
//mi->set_baked_light_texture_id(layer);
|
|
|
|
}
|
|
|
|
#endif
|
2017-03-05 23:44:50 +08:00
|
|
|
if (_teststr(name, "colonly")) {
|
2017-02-04 20:48:04 +08:00
|
|
|
|
|
|
|
if (isroot)
|
|
|
|
return p_node;
|
|
|
|
|
|
|
|
if (p_node->cast_to<MeshInstance>()) {
|
|
|
|
MeshInstance *mi = p_node->cast_to<MeshInstance>();
|
2017-03-05 23:44:50 +08:00
|
|
|
Node *col = mi->create_trimesh_collision_node();
|
|
|
|
ERR_FAIL_COND_V(!col, NULL);
|
2017-02-04 20:48:04 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
col->set_name(_fixstr(name, "colonly"));
|
2017-02-04 20:48:04 +08:00
|
|
|
col->cast_to<Spatial>()->set_transform(mi->get_transform());
|
|
|
|
p_node->replace_by(col);
|
|
|
|
memdelete(p_node);
|
2017-03-05 23:44:50 +08:00
|
|
|
p_node = col;
|
2017-02-04 20:48:04 +08:00
|
|
|
|
|
|
|
StaticBody *sb = col->cast_to<StaticBody>();
|
2017-03-05 23:44:50 +08:00
|
|
|
CollisionShape *colshape = memnew(CollisionShape);
|
2017-02-04 20:48:04 +08:00
|
|
|
colshape->set_shape(sb->get_shape(0));
|
|
|
|
colshape->set_name("shape");
|
|
|
|
sb->add_child(colshape);
|
|
|
|
colshape->set_owner(p_node->get_owner());
|
|
|
|
} else if (p_node->has_meta("empty_draw_type")) {
|
|
|
|
String empty_draw_type = String(p_node->get_meta("empty_draw_type"));
|
|
|
|
print_line(empty_draw_type);
|
2017-03-05 23:44:50 +08:00
|
|
|
StaticBody *sb = memnew(StaticBody);
|
|
|
|
sb->set_name(_fixstr(name, "colonly"));
|
2017-02-04 20:48:04 +08:00
|
|
|
sb->cast_to<Spatial>()->set_transform(p_node->cast_to<Spatial>()->get_transform());
|
|
|
|
p_node->replace_by(sb);
|
|
|
|
memdelete(p_node);
|
2017-03-05 23:44:50 +08:00
|
|
|
CollisionShape *colshape = memnew(CollisionShape);
|
2017-02-04 20:48:04 +08:00
|
|
|
if (empty_draw_type == "CUBE") {
|
2017-03-05 23:44:50 +08:00
|
|
|
BoxShape *boxShape = memnew(BoxShape);
|
2017-02-04 20:48:04 +08:00
|
|
|
boxShape->set_extents(Vector3(1, 1, 1));
|
|
|
|
colshape->set_shape(boxShape);
|
|
|
|
colshape->set_name("BoxShape");
|
|
|
|
} else if (empty_draw_type == "SINGLE_ARROW") {
|
2017-03-05 23:44:50 +08:00
|
|
|
RayShape *rayShape = memnew(RayShape);
|
2017-02-04 20:48:04 +08:00
|
|
|
rayShape->set_length(1);
|
|
|
|
colshape->set_shape(rayShape);
|
|
|
|
colshape->set_name("RayShape");
|
|
|
|
sb->cast_to<Spatial>()->rotate_x(Math_PI / 2);
|
|
|
|
} else if (empty_draw_type == "IMAGE") {
|
2017-03-05 23:44:50 +08:00
|
|
|
PlaneShape *planeShape = memnew(PlaneShape);
|
2017-02-04 20:48:04 +08:00
|
|
|
colshape->set_shape(planeShape);
|
|
|
|
colshape->set_name("PlaneShape");
|
|
|
|
} else {
|
2017-03-05 23:44:50 +08:00
|
|
|
SphereShape *sphereShape = memnew(SphereShape);
|
2017-02-04 20:48:04 +08:00
|
|
|
sphereShape->set_radius(1);
|
|
|
|
colshape->set_shape(sphereShape);
|
|
|
|
colshape->set_name("SphereShape");
|
|
|
|
}
|
|
|
|
sb->add_child(colshape);
|
|
|
|
colshape->set_owner(sb->get_owner());
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
} else if (_teststr(name, "rigid") && p_node->cast_to<MeshInstance>()) {
|
2017-02-04 20:48:04 +08:00
|
|
|
|
|
|
|
if (isroot)
|
|
|
|
return p_node;
|
|
|
|
|
|
|
|
// get mesh instance and bounding box
|
|
|
|
MeshInstance *mi = p_node->cast_to<MeshInstance>();
|
|
|
|
Rect3 aabb = mi->get_aabb();
|
|
|
|
|
|
|
|
// create a new rigid body collision node
|
2017-03-05 23:44:50 +08:00
|
|
|
RigidBody *rigid_body = memnew(RigidBody);
|
|
|
|
Node *col = rigid_body;
|
|
|
|
ERR_FAIL_COND_V(!col, NULL);
|
2017-02-04 20:48:04 +08:00
|
|
|
|
|
|
|
// remove node name postfix
|
2017-03-05 23:44:50 +08:00
|
|
|
col->set_name(_fixstr(name, "rigid"));
|
2017-02-04 20:48:04 +08:00
|
|
|
// get mesh instance xform matrix to the rigid body collision node
|
|
|
|
col->cast_to<Spatial>()->set_transform(mi->get_transform());
|
|
|
|
// save original node by duplicating it into a new instance and correcting the name
|
2017-03-05 23:44:50 +08:00
|
|
|
Node *mesh = p_node->duplicate();
|
|
|
|
mesh->set_name(_fixstr(name, "rigid"));
|
2017-02-04 20:48:04 +08:00
|
|
|
// reset the xform matrix of the duplicated node so it can inherit parent node xform
|
|
|
|
mesh->cast_to<Spatial>()->set_transform(Transform(Basis()));
|
|
|
|
// reparent the new mesh node to the rigid body collision node
|
|
|
|
p_node->add_child(mesh);
|
|
|
|
mesh->set_owner(p_node->get_owner());
|
|
|
|
// replace the original node with the rigid body collision node
|
|
|
|
p_node->replace_by(col);
|
|
|
|
memdelete(p_node);
|
2017-03-05 23:44:50 +08:00
|
|
|
p_node = col;
|
2017-02-04 20:48:04 +08:00
|
|
|
|
|
|
|
// create an alias for the rigid body collision node
|
|
|
|
RigidBody *rb = col->cast_to<RigidBody>();
|
|
|
|
// create a new Box collision shape and set the right extents
|
2017-03-05 23:44:50 +08:00
|
|
|
Ref<BoxShape> shape = memnew(BoxShape);
|
2017-02-04 20:48:04 +08:00
|
|
|
shape->set_extents(aabb.get_size() * 0.5);
|
2017-03-05 23:44:50 +08:00
|
|
|
CollisionShape *colshape = memnew(CollisionShape);
|
2017-02-04 20:48:04 +08:00
|
|
|
colshape->set_name("shape");
|
|
|
|
colshape->set_shape(shape);
|
|
|
|
// reparent the new collision shape to the rigid body collision node
|
|
|
|
rb->add_child(colshape);
|
|
|
|
colshape->set_owner(p_node->get_owner());
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
} else if (_teststr(name, "col") && p_node->cast_to<MeshInstance>()) {
|
2017-02-04 20:48:04 +08:00
|
|
|
|
|
|
|
MeshInstance *mi = p_node->cast_to<MeshInstance>();
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
mi->set_name(_fixstr(name, "col"));
|
|
|
|
Node *col = mi->create_trimesh_collision_node();
|
|
|
|
ERR_FAIL_COND_V(!col, NULL);
|
2017-02-04 20:48:04 +08:00
|
|
|
|
|
|
|
col->set_name("col");
|
|
|
|
p_node->add_child(col);
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
StaticBody *sb = col->cast_to<StaticBody>();
|
|
|
|
CollisionShape *colshape = memnew(CollisionShape);
|
2017-02-04 20:48:04 +08:00
|
|
|
colshape->set_shape(sb->get_shape(0));
|
|
|
|
colshape->set_name("shape");
|
|
|
|
col->add_child(colshape);
|
|
|
|
colshape->set_owner(p_node->get_owner());
|
|
|
|
sb->set_owner(p_node->get_owner());
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
} else if (_teststr(name, "navmesh") && p_node->cast_to<MeshInstance>()) {
|
2017-02-04 20:48:04 +08:00
|
|
|
|
|
|
|
if (isroot)
|
|
|
|
return p_node;
|
|
|
|
|
|
|
|
MeshInstance *mi = p_node->cast_to<MeshInstance>();
|
|
|
|
|
2017-06-08 05:18:55 +08:00
|
|
|
Ref<ArrayMesh> mesh = mi->get_mesh();
|
2017-03-05 23:44:50 +08:00
|
|
|
ERR_FAIL_COND_V(mesh.is_null(), NULL);
|
|
|
|
NavigationMeshInstance *nmi = memnew(NavigationMeshInstance);
|
2017-02-04 20:48:04 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
nmi->set_name(_fixstr(name, "navmesh"));
|
|
|
|
Ref<NavigationMesh> nmesh = memnew(NavigationMesh);
|
2017-02-04 20:48:04 +08:00
|
|
|
nmesh->create_from_mesh(mesh);
|
|
|
|
nmi->set_navigation_mesh(nmesh);
|
|
|
|
nmi->cast_to<Spatial>()->set_transform(mi->get_transform());
|
|
|
|
p_node->replace_by(nmi);
|
|
|
|
memdelete(p_node);
|
2017-03-05 23:44:50 +08:00
|
|
|
p_node = nmi;
|
|
|
|
} else if (_teststr(name, "vehicle")) {
|
2017-02-04 20:48:04 +08:00
|
|
|
|
|
|
|
if (isroot)
|
|
|
|
return p_node;
|
|
|
|
|
|
|
|
Node *owner = p_node->get_owner();
|
|
|
|
Spatial *s = p_node->cast_to<Spatial>();
|
2017-03-05 23:44:50 +08:00
|
|
|
VehicleBody *bv = memnew(VehicleBody);
|
|
|
|
String n = _fixstr(p_node->get_name(), "vehicle");
|
2017-02-04 20:48:04 +08:00
|
|
|
bv->set_name(n);
|
|
|
|
p_node->replace_by(bv);
|
|
|
|
p_node->set_name(n);
|
|
|
|
bv->add_child(p_node);
|
|
|
|
bv->set_owner(owner);
|
|
|
|
p_node->set_owner(owner);
|
|
|
|
bv->set_transform(s->get_transform());
|
|
|
|
s->set_transform(Transform());
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
p_node = bv;
|
2017-02-04 20:48:04 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
} else if (_teststr(name, "wheel")) {
|
2017-02-04 20:48:04 +08:00
|
|
|
|
|
|
|
if (isroot)
|
|
|
|
return p_node;
|
|
|
|
|
|
|
|
Node *owner = p_node->get_owner();
|
|
|
|
Spatial *s = p_node->cast_to<Spatial>();
|
2017-03-05 23:44:50 +08:00
|
|
|
VehicleWheel *bv = memnew(VehicleWheel);
|
|
|
|
String n = _fixstr(p_node->get_name(), "wheel");
|
2017-02-04 20:48:04 +08:00
|
|
|
bv->set_name(n);
|
|
|
|
p_node->replace_by(bv);
|
|
|
|
p_node->set_name(n);
|
|
|
|
bv->add_child(p_node);
|
|
|
|
bv->set_owner(owner);
|
|
|
|
p_node->set_owner(owner);
|
|
|
|
bv->set_transform(s->get_transform());
|
|
|
|
s->set_transform(Transform());
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
p_node = bv;
|
2017-02-04 20:48:04 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
} else if (_teststr(name, "room") && p_node->cast_to<MeshInstance>()) {
|
2017-02-04 20:48:04 +08:00
|
|
|
|
|
|
|
if (isroot)
|
|
|
|
return p_node;
|
|
|
|
|
|
|
|
MeshInstance *mi = p_node->cast_to<MeshInstance>();
|
|
|
|
PoolVector<Face3> faces = mi->get_faces(VisualInstance::FACES_SOLID);
|
|
|
|
|
|
|
|
BSP_Tree bsptree(faces);
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
Ref<RoomBounds> area = memnew(RoomBounds);
|
2017-02-04 20:48:04 +08:00
|
|
|
//area->set_bounds(faces);
|
|
|
|
//area->set_geometry_hint(faces);
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
Room *room = memnew(Room);
|
|
|
|
room->set_name(_fixstr(name, "room"));
|
2017-02-04 20:48:04 +08:00
|
|
|
room->set_transform(mi->get_transform());
|
|
|
|
room->set_room(area);
|
|
|
|
|
|
|
|
p_node->replace_by(room);
|
|
|
|
memdelete(p_node);
|
2017-03-05 23:44:50 +08:00
|
|
|
p_node = room;
|
2017-02-04 20:48:04 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
} else if (_teststr(name, "room")) {
|
2017-02-04 20:48:04 +08:00
|
|
|
|
|
|
|
if (isroot)
|
|
|
|
return p_node;
|
|
|
|
|
|
|
|
Spatial *dummy = p_node->cast_to<Spatial>();
|
2017-03-05 23:44:50 +08:00
|
|
|
ERR_FAIL_COND_V(!dummy, NULL);
|
2017-02-04 20:48:04 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
Room *room = memnew(Room);
|
|
|
|
room->set_name(_fixstr(name, "room"));
|
2017-02-04 20:48:04 +08:00
|
|
|
room->set_transform(dummy->get_transform());
|
|
|
|
|
|
|
|
p_node->replace_by(room);
|
|
|
|
memdelete(p_node);
|
2017-03-05 23:44:50 +08:00
|
|
|
p_node = room;
|
2017-02-04 20:48:04 +08:00
|
|
|
|
|
|
|
//room->compute_room_from_subtree();
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
} else if (_teststr(name, "portal") && p_node->cast_to<MeshInstance>()) {
|
2017-02-04 20:48:04 +08:00
|
|
|
|
|
|
|
if (isroot)
|
|
|
|
return p_node;
|
|
|
|
|
|
|
|
MeshInstance *mi = p_node->cast_to<MeshInstance>();
|
|
|
|
PoolVector<Face3> faces = mi->get_faces(VisualInstance::FACES_SOLID);
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
ERR_FAIL_COND_V(faces.size() == 0, NULL);
|
2017-02-04 20:48:04 +08:00
|
|
|
//step 1 compute the plane
|
|
|
|
Set<Vector3> points;
|
|
|
|
Plane plane;
|
|
|
|
|
|
|
|
Vector3 center;
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
for (int i = 0; i < faces.size(); i++) {
|
2017-02-04 20:48:04 +08:00
|
|
|
|
|
|
|
Face3 f = faces.get(i);
|
|
|
|
Plane p = f.get_plane();
|
2017-03-05 23:44:50 +08:00
|
|
|
plane.normal += p.normal;
|
|
|
|
plane.d += p.d;
|
2017-02-04 20:48:04 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
for (int i = 0; i < 3; i++) {
|
2017-02-04 20:48:04 +08:00
|
|
|
|
|
|
|
Vector3 v = f.vertex[i].snapped(0.01);
|
|
|
|
if (!points.has(v)) {
|
|
|
|
points.insert(v);
|
2017-03-05 23:44:50 +08:00
|
|
|
center += v;
|
2017-02-04 20:48:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
plane.normal.normalize();
|
2017-03-05 23:44:50 +08:00
|
|
|
plane.d /= faces.size();
|
|
|
|
center /= points.size();
|
2017-02-04 20:48:04 +08:00
|
|
|
|
|
|
|
//step 2, create points
|
|
|
|
|
|
|
|
Transform t;
|
|
|
|
t.basis.from_z(plane.normal);
|
|
|
|
t.basis.transpose();
|
2017-03-05 23:44:50 +08:00
|
|
|
t.origin = center;
|
2017-02-04 20:48:04 +08:00
|
|
|
|
|
|
|
Vector<Point2> portal_points;
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
for (Set<Vector3>::Element *E = points.front(); E; E = E->next()) {
|
2017-02-04 20:48:04 +08:00
|
|
|
|
|
|
|
Vector3 local = t.xform_inv(E->get());
|
2017-03-05 23:44:50 +08:00
|
|
|
portal_points.push_back(Point2(local.x, local.y));
|
2017-02-04 20:48:04 +08:00
|
|
|
}
|
|
|
|
// step 3 bubbly sort points
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
int swaps = 0;
|
2017-02-04 20:48:04 +08:00
|
|
|
|
|
|
|
do {
|
2017-03-05 23:44:50 +08:00
|
|
|
swaps = 0;
|
2017-02-04 20:48:04 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
for (int i = 0; i < portal_points.size() - 1; i++) {
|
2017-02-04 20:48:04 +08:00
|
|
|
|
|
|
|
float a = portal_points[i].angle();
|
2017-03-05 23:44:50 +08:00
|
|
|
float b = portal_points[i + 1].angle();
|
2017-02-04 20:48:04 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
if (a > b) {
|
|
|
|
SWAP(portal_points[i], portal_points[i + 1]);
|
2017-02-04 20:48:04 +08:00
|
|
|
swaps++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
} while (swaps);
|
2017-02-04 20:48:04 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
Portal *portal = memnew(Portal);
|
2017-02-04 20:48:04 +08:00
|
|
|
|
|
|
|
portal->set_shape(portal_points);
|
2017-03-05 23:44:50 +08:00
|
|
|
portal->set_transform(mi->get_transform() * t);
|
2017-02-04 20:48:04 +08:00
|
|
|
|
|
|
|
p_node->replace_by(portal);
|
|
|
|
memdelete(p_node);
|
2017-03-05 23:44:50 +08:00
|
|
|
p_node = portal;
|
2017-02-04 20:48:04 +08:00
|
|
|
|
|
|
|
} else if (p_node->cast_to<MeshInstance>()) {
|
|
|
|
|
|
|
|
//last attempt, maybe collision insde the mesh data
|
|
|
|
|
|
|
|
MeshInstance *mi = p_node->cast_to<MeshInstance>();
|
|
|
|
|
2017-06-08 05:18:55 +08:00
|
|
|
Ref<ArrayMesh> mesh = mi->get_mesh();
|
2017-02-04 20:48:04 +08:00
|
|
|
if (!mesh.is_null()) {
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
if (_teststr(mesh->get_name(), "col")) {
|
2017-02-04 20:48:04 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
mesh->set_name(_fixstr(mesh->get_name(), "col"));
|
2017-02-04 20:48:04 +08:00
|
|
|
Ref<Shape> shape;
|
|
|
|
|
|
|
|
if (collision_map.has(mesh)) {
|
|
|
|
shape = collision_map[mesh];
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
shape = mesh->create_trimesh_shape();
|
|
|
|
if (!shape.is_null())
|
2017-03-05 23:44:50 +08:00
|
|
|
collision_map[mesh] = shape;
|
2017-02-04 20:48:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!shape.is_null()) {
|
|
|
|
#if 0
|
|
|
|
StaticBody* static_body = memnew( StaticBody );
|
|
|
|
ERR_FAIL_COND_V(!static_body,NULL);
|
|
|
|
static_body->set_name( String(mesh->get_name()) + "_col" );
|
|
|
|
shape->set_name(static_body->get_name());
|
|
|
|
static_body->add_shape(shape);
|
|
|
|
|
|
|
|
mi->add_child(static_body);
|
|
|
|
if (mi->get_owner())
|
|
|
|
static_body->set_owner( mi->get_owner() );
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
for (int i = 0; i < mesh->get_surface_count(); i++) {
|
2017-02-04 20:48:04 +08:00
|
|
|
|
2017-04-07 10:36:37 +08:00
|
|
|
Ref<SpatialMaterial> fm = mesh->surface_get_material(i);
|
2017-02-04 20:48:04 +08:00
|
|
|
if (fm.is_valid()) {
|
|
|
|
String name = fm->get_name();
|
2017-03-05 23:44:50 +08:00
|
|
|
/* if (_teststr(name,"alpha")) {
|
2017-04-07 10:36:37 +08:00
|
|
|
fm->set_fixed_flag(SpatialMaterial::FLAG_USE_ALPHA,true);
|
2017-02-04 20:48:04 +08:00
|
|
|
name=_fixstr(name,"alpha");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_teststr(name,"vcol")) {
|
2017-04-07 10:36:37 +08:00
|
|
|
fm->set_fixed_flag(SpatialMaterial::FLAG_USE_COLOR_ARRAY,true);
|
2017-02-04 20:48:04 +08:00
|
|
|
name=_fixstr(name,"vcol");
|
|
|
|
}*/
|
|
|
|
fm->set_name(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return p_node;
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
void ResourceImporterScene::_create_clips(Node *scene, const Array &p_clips, bool p_bake_all) {
|
2017-02-04 20:48:04 +08:00
|
|
|
|
|
|
|
if (!scene->has_node(String("AnimationPlayer")))
|
|
|
|
return;
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
Node *n = scene->get_node(String("AnimationPlayer"));
|
2017-02-04 20:48:04 +08:00
|
|
|
ERR_FAIL_COND(!n);
|
|
|
|
AnimationPlayer *anim = n->cast_to<AnimationPlayer>();
|
|
|
|
ERR_FAIL_COND(!anim);
|
|
|
|
|
|
|
|
if (!anim->has_animation("default"))
|
|
|
|
return;
|
|
|
|
|
|
|
|
Ref<Animation> default_anim = anim->get_animation("default");
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
for (int i = 0; i < p_clips.size(); i += 4) {
|
2017-02-04 20:48:04 +08:00
|
|
|
|
|
|
|
String name = p_clips[i];
|
2017-03-05 23:44:50 +08:00
|
|
|
float from = p_clips[i + 1];
|
|
|
|
float to = p_clips[i + 2];
|
|
|
|
bool loop = p_clips[i + 3];
|
|
|
|
if (from >= to)
|
2017-02-04 20:48:04 +08:00
|
|
|
continue;
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
Ref<Animation> new_anim = memnew(Animation);
|
2017-02-04 20:48:04 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
for (int j = 0; j < default_anim->get_track_count(); j++) {
|
2017-02-04 20:48:04 +08:00
|
|
|
|
|
|
|
List<float> keys;
|
|
|
|
int kc = default_anim->track_get_key_count(j);
|
2017-03-05 23:44:50 +08:00
|
|
|
int dtrack = -1;
|
|
|
|
for (int k = 0; k < kc; k++) {
|
2017-02-04 20:48:04 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
float kt = default_anim->track_get_key_time(j, k);
|
|
|
|
if (kt >= from && kt < to) {
|
2017-02-04 20:48:04 +08:00
|
|
|
|
|
|
|
//found a key within range, so create track
|
2017-03-05 23:44:50 +08:00
|
|
|
if (dtrack == -1) {
|
2017-02-04 20:48:04 +08:00
|
|
|
new_anim->add_track(default_anim->track_get_type(j));
|
2017-03-05 23:44:50 +08:00
|
|
|
dtrack = new_anim->get_track_count() - 1;
|
|
|
|
new_anim->track_set_path(dtrack, default_anim->track_get_path(j));
|
2017-02-04 20:48:04 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
if (kt > (from + 0.01) && k > 0) {
|
2017-02-04 20:48:04 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
if (default_anim->track_get_type(j) == Animation::TYPE_TRANSFORM) {
|
2017-02-04 20:48:04 +08:00
|
|
|
Quat q;
|
|
|
|
Vector3 p;
|
|
|
|
Vector3 s;
|
2017-03-05 23:44:50 +08:00
|
|
|
default_anim->transform_track_interpolate(j, from, &p, &q, &s);
|
|
|
|
new_anim->transform_track_insert_key(dtrack, 0, p, q, s);
|
2017-02-04 20:48:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
if (default_anim->track_get_type(j) == Animation::TYPE_TRANSFORM) {
|
2017-02-04 20:48:04 +08:00
|
|
|
Quat q;
|
|
|
|
Vector3 p;
|
|
|
|
Vector3 s;
|
2017-03-05 23:44:50 +08:00
|
|
|
default_anim->transform_track_get_key(j, k, &p, &q, &s);
|
|
|
|
new_anim->transform_track_insert_key(dtrack, kt - from, p, q, s);
|
2017-02-04 20:48:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
if (dtrack != -1 && kt >= to) {
|
2017-02-04 20:48:04 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
if (default_anim->track_get_type(j) == Animation::TYPE_TRANSFORM) {
|
2017-02-04 20:48:04 +08:00
|
|
|
Quat q;
|
|
|
|
Vector3 p;
|
|
|
|
Vector3 s;
|
2017-03-05 23:44:50 +08:00
|
|
|
default_anim->transform_track_interpolate(j, to, &p, &q, &s);
|
|
|
|
new_anim->transform_track_insert_key(dtrack, to - from, p, q, s);
|
2017-02-04 20:48:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
if (dtrack == -1 && p_bake_all) {
|
2017-02-04 20:48:04 +08:00
|
|
|
new_anim->add_track(default_anim->track_get_type(j));
|
2017-03-05 23:44:50 +08:00
|
|
|
dtrack = new_anim->get_track_count() - 1;
|
|
|
|
new_anim->track_set_path(dtrack, default_anim->track_get_path(j));
|
|
|
|
if (default_anim->track_get_type(j) == Animation::TYPE_TRANSFORM) {
|
2017-02-04 20:48:04 +08:00
|
|
|
|
|
|
|
Quat q;
|
|
|
|
Vector3 p;
|
|
|
|
Vector3 s;
|
2017-03-05 23:44:50 +08:00
|
|
|
default_anim->transform_track_interpolate(j, from, &p, &q, &s);
|
|
|
|
new_anim->transform_track_insert_key(dtrack, 0, p, q, s);
|
|
|
|
default_anim->transform_track_interpolate(j, to, &p, &q, &s);
|
|
|
|
new_anim->transform_track_insert_key(dtrack, to - from, p, q, s);
|
2017-02-04 20:48:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
new_anim->set_loop(loop);
|
2017-03-05 23:44:50 +08:00
|
|
|
new_anim->set_length(to - from);
|
|
|
|
anim->add_animation(name, new_anim);
|
2017-02-04 20:48:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
anim->remove_animation("default"); //remove default (no longer needed)
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
void ResourceImporterScene::_filter_anim_tracks(Ref<Animation> anim, Set<String> &keep) {
|
2017-02-04 20:48:04 +08:00
|
|
|
|
|
|
|
Ref<Animation> a = anim;
|
|
|
|
ERR_FAIL_COND(!a.is_valid());
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
print_line("From Anim " + anim->get_name() + ":");
|
2017-02-04 20:48:04 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
for (int j = 0; j < a->get_track_count(); j++) {
|
2017-02-04 20:48:04 +08:00
|
|
|
|
|
|
|
String path = a->track_get_path(j);
|
|
|
|
|
|
|
|
if (!keep.has(path)) {
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
print_line("Remove: " + path);
|
2017-02-04 20:48:04 +08:00
|
|
|
a->remove_track(j);
|
|
|
|
j--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
void ResourceImporterScene::_filter_tracks(Node *scene, const String &p_text) {
|
2017-02-04 20:48:04 +08:00
|
|
|
|
|
|
|
if (!scene->has_node(String("AnimationPlayer")))
|
|
|
|
return;
|
2017-03-05 23:44:50 +08:00
|
|
|
Node *n = scene->get_node(String("AnimationPlayer"));
|
2017-02-04 20:48:04 +08:00
|
|
|
ERR_FAIL_COND(!n);
|
|
|
|
AnimationPlayer *anim = n->cast_to<AnimationPlayer>();
|
|
|
|
ERR_FAIL_COND(!anim);
|
|
|
|
|
|
|
|
Vector<String> strings = p_text.split("\n");
|
2017-03-05 23:44:50 +08:00
|
|
|
for (int i = 0; i < strings.size(); i++) {
|
2017-02-04 20:48:04 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
strings[i] = strings[i].strip_edges();
|
2017-02-04 20:48:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
List<StringName> anim_names;
|
|
|
|
anim->get_animation_list(&anim_names);
|
2017-03-05 23:44:50 +08:00
|
|
|
for (List<StringName>::Element *E = anim_names.front(); E; E = E->next()) {
|
2017-02-04 20:48:04 +08:00
|
|
|
|
|
|
|
String name = E->get();
|
2017-03-05 23:44:50 +08:00
|
|
|
bool valid_for_this = false;
|
|
|
|
bool valid = false;
|
2017-02-04 20:48:04 +08:00
|
|
|
|
|
|
|
Set<String> keep;
|
|
|
|
Set<String> keep_local;
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
for (int i = 0; i < strings.size(); i++) {
|
2017-02-04 20:48:04 +08:00
|
|
|
|
|
|
|
if (strings[i].begins_with("@")) {
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
valid_for_this = false;
|
|
|
|
for (Set<String>::Element *F = keep_local.front(); F; F = F->next()) {
|
2017-02-04 20:48:04 +08:00
|
|
|
keep.insert(F->get());
|
|
|
|
}
|
|
|
|
keep_local.clear();
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
Vector<String> filters = strings[i].substr(1, strings[i].length()).split(",");
|
|
|
|
for (int j = 0; j < filters.size(); j++) {
|
2017-02-04 20:48:04 +08:00
|
|
|
|
|
|
|
String fname = filters[j].strip_edges();
|
2017-03-05 23:44:50 +08:00
|
|
|
if (fname == "")
|
2017-02-04 20:48:04 +08:00
|
|
|
continue;
|
|
|
|
int fc = fname[0];
|
|
|
|
bool plus;
|
2017-03-05 23:44:50 +08:00
|
|
|
if (fc == '+')
|
|
|
|
plus = true;
|
|
|
|
else if (fc == '-')
|
|
|
|
plus = false;
|
2017-02-04 20:48:04 +08:00
|
|
|
else
|
|
|
|
continue;
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
String filter = fname.substr(1, fname.length()).strip_edges();
|
2017-02-04 20:48:04 +08:00
|
|
|
|
|
|
|
if (!name.matchn(filter))
|
|
|
|
continue;
|
2017-03-05 23:44:50 +08:00
|
|
|
valid_for_this = plus;
|
2017-02-04 20:48:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (valid_for_this)
|
2017-03-05 23:44:50 +08:00
|
|
|
valid = true;
|
2017-02-04 20:48:04 +08:00
|
|
|
|
|
|
|
} else if (valid_for_this) {
|
|
|
|
|
|
|
|
Ref<Animation> a = anim->get_animation(name);
|
|
|
|
if (!a.is_valid())
|
|
|
|
continue;
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
for (int j = 0; j < a->get_track_count(); j++) {
|
2017-02-04 20:48:04 +08:00
|
|
|
|
|
|
|
String path = a->track_get_path(j);
|
|
|
|
|
|
|
|
String tname = strings[i];
|
2017-03-05 23:44:50 +08:00
|
|
|
if (tname == "")
|
2017-02-04 20:48:04 +08:00
|
|
|
continue;
|
|
|
|
int fc = tname[0];
|
|
|
|
bool plus;
|
2017-03-05 23:44:50 +08:00
|
|
|
if (fc == '+')
|
|
|
|
plus = true;
|
|
|
|
else if (fc == '-')
|
|
|
|
plus = false;
|
2017-02-04 20:48:04 +08:00
|
|
|
else
|
|
|
|
continue;
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
String filter = tname.substr(1, tname.length()).strip_edges();
|
2017-02-04 20:48:04 +08:00
|
|
|
|
|
|
|
if (!path.matchn(filter))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (plus)
|
|
|
|
keep_local.insert(path);
|
|
|
|
else if (!keep.has(path)) {
|
|
|
|
keep_local.erase(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (valid) {
|
2017-03-05 23:44:50 +08:00
|
|
|
for (Set<String>::Element *F = keep_local.front(); F; F = F->next()) {
|
2017-02-04 20:48:04 +08:00
|
|
|
keep.insert(F->get());
|
|
|
|
}
|
2017-03-05 23:44:50 +08:00
|
|
|
_filter_anim_tracks(anim->get_animation(name), keep);
|
2017-02-04 20:48:04 +08:00
|
|
|
} else {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
void ResourceImporterScene::_optimize_animations(Node *scene, float p_max_lin_error, float p_max_ang_error, float p_max_angle) {
|
2017-02-04 20:48:04 +08:00
|
|
|
|
|
|
|
if (!scene->has_node(String("AnimationPlayer")))
|
|
|
|
return;
|
2017-03-05 23:44:50 +08:00
|
|
|
Node *n = scene->get_node(String("AnimationPlayer"));
|
2017-02-04 20:48:04 +08:00
|
|
|
ERR_FAIL_COND(!n);
|
|
|
|
AnimationPlayer *anim = n->cast_to<AnimationPlayer>();
|
|
|
|
ERR_FAIL_COND(!anim);
|
|
|
|
|
|
|
|
List<StringName> anim_names;
|
|
|
|
anim->get_animation_list(&anim_names);
|
2017-03-05 23:44:50 +08:00
|
|
|
for (List<StringName>::Element *E = anim_names.front(); E; E = E->next()) {
|
2017-02-04 20:48:04 +08:00
|
|
|
|
|
|
|
Ref<Animation> a = anim->get_animation(E->get());
|
2017-03-05 23:44:50 +08:00
|
|
|
a->optimize(p_max_lin_error, p_max_ang_error, Math::deg2rad(p_max_angle));
|
2017-02-04 20:48:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
static String _make_extname(const String &p_str) {
|
2017-02-04 20:48:04 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
String ext_name = p_str.replace(".", "_");
|
|
|
|
ext_name = ext_name.replace(":", "_");
|
|
|
|
ext_name = ext_name.replace("\"", "_");
|
|
|
|
ext_name = ext_name.replace("<", "_");
|
|
|
|
ext_name = ext_name.replace(">", "_");
|
|
|
|
ext_name = ext_name.replace("/", "_");
|
|
|
|
ext_name = ext_name.replace("|", "_");
|
|
|
|
ext_name = ext_name.replace("\\", "_");
|
|
|
|
ext_name = ext_name.replace("?", "_");
|
|
|
|
ext_name = ext_name.replace("*", "_");
|
2017-02-05 07:31:15 +08:00
|
|
|
|
|
|
|
return ext_name;
|
|
|
|
}
|
|
|
|
|
2017-06-08 05:18:55 +08:00
|
|
|
void ResourceImporterScene::_make_external_resources(Node *p_node, const String &p_base_path, bool p_make_materials, bool p_make_meshes, Map<Ref<Material>, Ref<Material> > &p_materials, Map<Ref<ArrayMesh>, Ref<ArrayMesh> > &p_meshes) {
|
2017-02-05 07:31:15 +08:00
|
|
|
|
|
|
|
List<PropertyInfo> pi;
|
|
|
|
|
|
|
|
p_node->get_property_list(&pi);
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
for (List<PropertyInfo>::Element *E = pi.front(); E; E = E->next()) {
|
2017-02-05 07:31:15 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
if (E->get().type == Variant::OBJECT) {
|
2017-02-05 07:31:15 +08:00
|
|
|
|
|
|
|
Ref<Material> mat = p_node->get(E->get().name);
|
2017-03-05 23:44:50 +08:00
|
|
|
if (p_make_materials && mat.is_valid() && mat->get_name() != "") {
|
2017-02-05 07:31:15 +08:00
|
|
|
|
|
|
|
if (!p_materials.has(mat)) {
|
|
|
|
|
2017-06-17 02:42:18 +08:00
|
|
|
String ext_name = p_base_path + "." + _make_extname(mat->get_name()) + ".material";
|
2017-02-05 07:31:15 +08:00
|
|
|
if (FileAccess::exists(ext_name)) {
|
|
|
|
//if exists, use it
|
|
|
|
Ref<Material> existing = ResourceLoader::load(ext_name);
|
2017-03-05 23:44:50 +08:00
|
|
|
p_materials[mat] = existing;
|
2017-02-05 07:31:15 +08:00
|
|
|
} else {
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
ResourceSaver::save(ext_name, mat, ResourceSaver::FLAG_CHANGE_PATH);
|
|
|
|
p_materials[mat] = mat;
|
2017-02-05 07:31:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
if (p_materials[mat] != mat) {
|
2017-02-05 07:31:15 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
p_node->set(E->get().name, p_materials[mat]);
|
2017-02-05 07:31:15 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
2017-06-08 05:18:55 +08:00
|
|
|
Ref<ArrayMesh> mesh = p_node->get(E->get().name);
|
2017-02-05 07:31:15 +08:00
|
|
|
|
|
|
|
if (mesh.is_valid()) {
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
bool mesh_just_added = false;
|
2017-02-05 07:31:15 +08:00
|
|
|
|
|
|
|
if (p_make_meshes) {
|
|
|
|
|
|
|
|
if (!p_meshes.has(mesh)) {
|
|
|
|
|
2017-06-17 02:42:18 +08:00
|
|
|
String ext_name = p_base_path + "." + _make_extname(mesh->get_name()) + ".mesh";
|
2017-02-05 07:31:15 +08:00
|
|
|
if (FileAccess::exists(ext_name)) {
|
|
|
|
//if exists, use it
|
2017-06-08 05:18:55 +08:00
|
|
|
Ref<ArrayMesh> existing = ResourceLoader::load(ext_name);
|
2017-03-05 23:44:50 +08:00
|
|
|
p_meshes[mesh] = existing;
|
2017-02-05 07:31:15 +08:00
|
|
|
} else {
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
ResourceSaver::save(ext_name, mesh, ResourceSaver::FLAG_CHANGE_PATH);
|
|
|
|
p_meshes[mesh] = mesh;
|
|
|
|
mesh_just_added = true;
|
2017-02-05 07:31:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
if (p_make_materials) {
|
2017-02-05 07:31:15 +08:00
|
|
|
|
|
|
|
if (mesh_just_added || !p_meshes.has(mesh)) {
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
for (int i = 0; i < mesh->get_surface_count(); i++) {
|
|
|
|
mat = mesh->surface_get_material(i);
|
|
|
|
if (!mat.is_valid() || mat->get_name() == "")
|
2017-02-05 07:31:15 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!p_materials.has(mat)) {
|
|
|
|
|
2017-06-17 02:42:18 +08:00
|
|
|
String ext_name = p_base_path + "." + _make_extname(mat->get_name()) + ".material";
|
2017-02-05 07:31:15 +08:00
|
|
|
if (FileAccess::exists(ext_name)) {
|
|
|
|
//if exists, use it
|
|
|
|
Ref<Material> existing = ResourceLoader::load(ext_name);
|
2017-03-05 23:44:50 +08:00
|
|
|
p_materials[mat] = existing;
|
2017-02-05 07:31:15 +08:00
|
|
|
} else {
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
ResourceSaver::save(ext_name, mat, ResourceSaver::FLAG_CHANGE_PATH);
|
|
|
|
p_materials[mat] = mat;
|
2017-02-05 07:31:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
if (p_materials[mat] != mat) {
|
2017-02-05 07:31:15 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
mesh->surface_set_material(i, p_materials[mat]);
|
2017-02-05 07:31:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
if (!p_make_meshes) {
|
2017-06-08 05:18:55 +08:00
|
|
|
p_meshes[mesh] = Ref<ArrayMesh>(); //save it anyway, so it won't be checked again
|
2017-02-05 07:31:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
for (int i = 0; i < p_node->get_child_count(); i++) {
|
2017-02-05 07:31:15 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
_make_external_resources(p_node->get_child(i), p_base_path, p_make_materials, p_make_meshes, p_materials, p_meshes);
|
2017-02-05 07:31:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
void ResourceImporterScene::get_import_options(List<ImportOption> *r_options, int p_preset) const {
|
2017-02-05 07:31:15 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
r_options->push_back(ImportOption(PropertyInfo(Variant::STRING, "nodes/root_type", PROPERTY_HINT_TYPE_STRING, "Node"), "Spatial"));
|
|
|
|
r_options->push_back(ImportOption(PropertyInfo(Variant::STRING, "nodes/root_name"), "Scene Root"));
|
2017-02-04 20:48:04 +08:00
|
|
|
|
|
|
|
List<String> script_extentions;
|
2017-03-05 23:44:50 +08:00
|
|
|
ResourceLoader::get_recognized_extensions_for_type("Script", &script_extentions);
|
2017-02-04 20:48:04 +08:00
|
|
|
|
|
|
|
String script_ext_hint;
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
for (List<String>::Element *E = script_extentions.front(); E; E = E->next()) {
|
|
|
|
if (script_ext_hint != "")
|
|
|
|
script_ext_hint += ",";
|
|
|
|
script_ext_hint += "*." + E->get();
|
2017-02-04 20:48:04 +08:00
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
r_options->push_back(ImportOption(PropertyInfo(Variant::STRING, "nodes/custom_script", PROPERTY_HINT_FILE, script_ext_hint), ""));
|
|
|
|
r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "materials/location", PROPERTY_HINT_ENUM, "Node,Mesh"), 0));
|
|
|
|
r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "materials/storage", PROPERTY_HINT_ENUM, "Bult-In,Files"), 0));
|
|
|
|
r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "geometry/compress"), true));
|
|
|
|
r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "geometry/ensure_tangents"), true));
|
|
|
|
r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "geometry/storage", PROPERTY_HINT_ENUM, "Built-In,Files"), 0));
|
|
|
|
r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "animation/import", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), true));
|
|
|
|
r_options->push_back(ImportOption(PropertyInfo(Variant::REAL, "animation/fps", PROPERTY_HINT_RANGE, "1,120,1"), 15));
|
|
|
|
r_options->push_back(ImportOption(PropertyInfo(Variant::STRING, "animation/filter_script", PROPERTY_HINT_MULTILINE_TEXT), ""));
|
|
|
|
r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "animation/optimizer/enabled", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), true));
|
|
|
|
r_options->push_back(ImportOption(PropertyInfo(Variant::REAL, "animation/optimizer/max_linear_error"), 0.05));
|
|
|
|
r_options->push_back(ImportOption(PropertyInfo(Variant::REAL, "animation/optimizer/max_angular_error"), 0.01));
|
|
|
|
r_options->push_back(ImportOption(PropertyInfo(Variant::REAL, "animation/optimizer/max_angle"), 22));
|
|
|
|
r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "animation/optimizer/remove_unused_tracks"), true));
|
|
|
|
r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "animation/clips/amount", PROPERTY_HINT_RANGE, "0,256,1", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), 0));
|
|
|
|
for (int i = 0; i < 256; i++) {
|
|
|
|
r_options->push_back(ImportOption(PropertyInfo(Variant::STRING, "animation/clip_" + itos(i + 1) + "/name"), ""));
|
|
|
|
r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "animation/clip_" + itos(i + 1) + "/start_frame"), 0));
|
|
|
|
r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "animation/clip_" + itos(i + 1) + "/end_frame"), 0));
|
|
|
|
r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "animation/clip_" + itos(i + 1) + "/loops"), false));
|
2017-02-04 20:48:04 +08:00
|
|
|
}
|
|
|
|
}
|
2017-02-05 07:31:15 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
Error ResourceImporterScene::import(const String &p_source_file, const String &p_save_path, const Map<StringName, Variant> &p_options, List<String> *r_platform_variants, List<String> *r_gen_files) {
|
2017-02-04 20:48:04 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
String src_path = p_source_file;
|
2017-02-04 20:48:04 +08:00
|
|
|
|
|
|
|
Ref<EditorSceneImporter> importer;
|
2017-03-05 23:44:50 +08:00
|
|
|
String ext = src_path.get_extension().to_lower();
|
2017-02-04 20:48:04 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
EditorProgress progress("import", TTR("Import Scene"), 104);
|
|
|
|
progress.step(TTR("Importing Scene.."), 0);
|
2017-02-04 20:48:04 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
for (Set<Ref<EditorSceneImporter> >::Element *E = importers.front(); E; E = E->next()) {
|
2017-02-04 20:48:04 +08:00
|
|
|
|
|
|
|
List<String> extensions;
|
|
|
|
E->get()->get_extensions(&extensions);
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
for (List<String>::Element *F = extensions.front(); F; F = F->next()) {
|
2017-02-04 20:48:04 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
if (F->get().to_lower() == ext) {
|
2017-02-04 20:48:04 +08:00
|
|
|
|
|
|
|
importer = E->get();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (importer.is_valid())
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
ERR_FAIL_COND_V(!importer.is_valid(), ERR_FILE_UNRECOGNIZED);
|
2017-02-04 20:48:04 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
float fps = p_options["animation/fps"];
|
2017-02-04 20:48:04 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
int import_flags = EditorSceneImporter::IMPORT_ANIMATION_DETECT_LOOP;
|
2017-02-04 20:48:04 +08:00
|
|
|
if (!bool(p_options["animation/optimizer/remove_unused_tracks"]))
|
2017-03-05 23:44:50 +08:00
|
|
|
import_flags |= EditorSceneImporter::IMPORT_ANIMATION_FORCE_ALL_TRACKS_IN_ALL_CLIPS;
|
2017-02-04 20:48:04 +08:00
|
|
|
|
|
|
|
if (bool(p_options["animation/import"]))
|
2017-03-05 23:44:50 +08:00
|
|
|
import_flags |= EditorSceneImporter::IMPORT_ANIMATION;
|
2017-02-04 20:48:04 +08:00
|
|
|
|
|
|
|
if (bool(p_options["geometry/ensure_tangents"]))
|
2017-03-05 23:44:50 +08:00
|
|
|
import_flags |= EditorSceneImporter::IMPORT_GENERATE_TANGENT_ARRAYS;
|
2017-02-04 20:48:04 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
if (int(p_options["materials/location"]) == 0)
|
|
|
|
import_flags |= EditorSceneImporter::IMPORT_MATERIALS_IN_INSTANCES;
|
2017-02-04 20:48:04 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
Error err = OK;
|
2017-02-04 20:48:04 +08:00
|
|
|
List<String> missing_deps; // for now, not much will be done with this
|
2017-03-05 23:44:50 +08:00
|
|
|
Node *scene = importer->import_scene(src_path, import_flags, fps, &missing_deps, &err);
|
|
|
|
if (!scene || err != OK) {
|
2017-02-04 20:48:04 +08:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
String root_type = p_options["nodes/root_type"];
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
if (scene->get_class() != root_type) {
|
2017-02-04 20:48:04 +08:00
|
|
|
Object *base = ClassDB::instance(root_type);
|
|
|
|
Node *base_node = NULL;
|
|
|
|
if (base)
|
2017-03-05 23:44:50 +08:00
|
|
|
base_node = base->cast_to<Node>();
|
2017-02-04 20:48:04 +08:00
|
|
|
|
|
|
|
if (base_node) {
|
|
|
|
|
|
|
|
scene->replace_by(base_node);
|
|
|
|
memdelete(scene);
|
2017-03-05 23:44:50 +08:00
|
|
|
scene = base_node;
|
2017-02-04 20:48:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
scene->set_name(p_options["nodes/root_name"]);
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
err = OK;
|
2017-02-04 20:48:04 +08:00
|
|
|
|
|
|
|
String animation_filter = String(p_options["animation/filter_script"]).strip_edges();
|
|
|
|
|
|
|
|
bool use_optimizer = p_options["animation/optimizer/enabled"];
|
2017-03-05 23:44:50 +08:00
|
|
|
float anim_optimizer_linerr = p_options["animation/optimizer/max_linear_error"];
|
|
|
|
float anim_optimizer_angerr = p_options["animation/optimizer/max_angular_error"];
|
|
|
|
float anim_optimizer_maxang = p_options["animation/optimizer/max_angle"];
|
2017-02-04 20:48:04 +08:00
|
|
|
|
2017-06-08 05:18:55 +08:00
|
|
|
Map<Ref<ArrayMesh>, Ref<Shape> > collision_map;
|
2017-02-04 20:48:04 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
scene = _fix_node(scene, scene, collision_map);
|
2017-02-04 20:48:04 +08:00
|
|
|
|
|
|
|
if (use_optimizer) {
|
2017-03-05 23:44:50 +08:00
|
|
|
_optimize_animations(scene, anim_optimizer_linerr, anim_optimizer_angerr, anim_optimizer_maxang);
|
2017-02-04 20:48:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Array animation_clips;
|
|
|
|
{
|
|
|
|
|
|
|
|
int clip_count = p_options["animation/clips/amount"];
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
for (int i = 0; i < clip_count; i++) {
|
|
|
|
String name = p_options["animation/clip_" + itos(i + 1) + "/name"];
|
|
|
|
int from_frame = p_options["animation/clip_" + itos(i + 1) + "/start_frame"];
|
|
|
|
int end_frame = p_options["animation/clip_" + itos(i + 1) + "/end_frame"];
|
|
|
|
bool loop = p_options["animation/clip_" + itos(i + 1) + "/loops"];
|
2017-02-04 20:48:04 +08:00
|
|
|
|
|
|
|
animation_clips.push_back(name);
|
2017-03-05 23:44:50 +08:00
|
|
|
animation_clips.push_back(from_frame / fps);
|
|
|
|
animation_clips.push_back(end_frame / fps);
|
2017-02-04 20:48:04 +08:00
|
|
|
animation_clips.push_back(loop);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (animation_clips.size()) {
|
2017-03-05 23:44:50 +08:00
|
|
|
_create_clips(scene, animation_clips, !bool(p_options["animation/optimizer/remove_unused_tracks"]));
|
2017-02-04 20:48:04 +08:00
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
if (animation_filter != "") {
|
|
|
|
_filter_tracks(scene, animation_filter);
|
2017-02-04 20:48:04 +08:00
|
|
|
}
|
|
|
|
|
2017-02-05 07:31:15 +08:00
|
|
|
bool external_materials = p_options["materials/storage"];
|
|
|
|
bool external_meshes = p_options["geometry/storage"];
|
|
|
|
|
|
|
|
if (external_materials || external_meshes) {
|
|
|
|
Map<Ref<Material>, Ref<Material> > mat_map;
|
2017-06-08 05:18:55 +08:00
|
|
|
Map<Ref<ArrayMesh>, Ref<ArrayMesh> > mesh_map;
|
2017-03-05 23:44:50 +08:00
|
|
|
_make_external_resources(scene, p_source_file.get_basename(), external_materials, external_meshes, mat_map, mesh_map);
|
2017-02-05 07:31:15 +08:00
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
progress.step(TTR("Running Custom Script.."), 2);
|
2017-02-04 20:48:04 +08:00
|
|
|
|
|
|
|
String post_import_script_path = p_options["nodes/custom_script"];
|
2017-03-05 23:44:50 +08:00
|
|
|
Ref<EditorScenePostImport> post_import_script;
|
2017-02-04 20:48:04 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
if (post_import_script_path != "") {
|
|
|
|
post_import_script_path = post_import_script_path; // FIXME: is there a good reason for this?
|
2017-02-04 20:48:04 +08:00
|
|
|
Ref<Script> scr = ResourceLoader::load(post_import_script_path);
|
|
|
|
if (!scr.is_valid()) {
|
2017-03-05 23:44:50 +08:00
|
|
|
EditorNode::add_io_error(TTR("Couldn't load post-import script:") + " " + post_import_script_path);
|
2017-02-04 20:48:04 +08:00
|
|
|
} else {
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
post_import_script = Ref<EditorScenePostImport>(memnew(EditorScenePostImport));
|
2017-02-04 20:48:04 +08:00
|
|
|
post_import_script->set_script(scr.get_ref_ptr());
|
|
|
|
if (!post_import_script->get_script_instance()) {
|
2017-03-05 23:44:50 +08:00
|
|
|
EditorNode::add_io_error(TTR("Invalid/broken script for post-import (check console):") + " " + post_import_script_path);
|
2017-02-04 20:48:04 +08:00
|
|
|
post_import_script.unref();
|
|
|
|
return ERR_CANT_CREATE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (post_import_script.is_valid()) {
|
|
|
|
scene = post_import_script->post_import(scene);
|
|
|
|
if (!scene) {
|
2017-03-05 23:44:50 +08:00
|
|
|
EditorNode::add_io_error(TTR("Error running post-import script:") + " " + post_import_script_path);
|
2017-02-04 20:48:04 +08:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
progress.step(TTR("Saving.."), 104);
|
2017-02-04 20:48:04 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
Ref<PackedScene> packer = memnew(PackedScene);
|
2017-02-04 20:48:04 +08:00
|
|
|
packer->pack(scene);
|
2017-03-05 23:44:50 +08:00
|
|
|
print_line("SAVING TO: " + p_save_path + ".scn");
|
|
|
|
err = ResourceSaver::save(p_save_path + ".scn", packer); //do not take over, let the changed files reload themselves
|
2017-02-04 20:48:04 +08:00
|
|
|
|
|
|
|
memdelete(scene);
|
|
|
|
|
|
|
|
EditorNode::get_singleton()->reload_scene(p_source_file);
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
ResourceImporterScene *ResourceImporterScene::singleton = NULL;
|
2017-02-04 20:48:04 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
ResourceImporterScene::ResourceImporterScene() {
|
|
|
|
singleton = this;
|
2017-02-04 20:48:04 +08:00
|
|
|
}
|