2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* mesh_instance.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* http://www.godotengine.org */
|
|
|
|
/*************************************************************************/
|
2017-01-01 22:01:57 +01:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2014-02-10 02:10:30 +01: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. */
|
|
|
|
/*************************************************************************/
|
|
|
|
#include "mesh_instance.h"
|
|
|
|
|
|
|
|
#include "skeleton.h"
|
|
|
|
#include "physics_body.h"
|
2014-10-12 07:13:22 +02:00
|
|
|
#include "body_shape.h"
|
2016-05-27 19:18:40 +02:00
|
|
|
#include "scene/scene_string_names.h"
|
|
|
|
#include "core_string_names.h"
|
2014-02-10 02:10:30 +01:00
|
|
|
bool MeshInstance::_set(const StringName& p_name, const Variant& p_value) {
|
|
|
|
|
|
|
|
//this is not _too_ bad performance wise, really. it only arrives here if the property was not set anywhere else.
|
|
|
|
//add to it that it's probably found on first call to _set anyway.
|
|
|
|
|
|
|
|
if (!get_instance().is_valid())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
2017-01-12 12:34:00 +01:00
|
|
|
Map<StringName,BlendShapeTrack>::Element *E = blend_shape_tracks.find(p_name);
|
2016-05-27 19:18:40 +02:00
|
|
|
if (E) {
|
|
|
|
E->get().value=p_value;
|
2017-01-12 12:34:00 +01:00
|
|
|
VisualServer::get_singleton()->instance_set_blend_shape_weight(get_instance(),E->get().idx,E->get().value);
|
2016-05-27 19:18:40 +02:00
|
|
|
return true;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-05-27 19:18:40 +02:00
|
|
|
if (p_name.operator String().begins_with("material/")) {
|
|
|
|
int idx = p_name.operator String().get_slicec('/',1).to_int();
|
|
|
|
if (idx>=materials.size() || idx<0)
|
|
|
|
return false;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-05-27 19:18:40 +02:00
|
|
|
set_surface_material(idx,p_value);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool MeshInstance::_get(const StringName& p_name,Variant &r_ret) const {
|
|
|
|
|
|
|
|
|
|
|
|
if (!get_instance().is_valid())
|
|
|
|
return false;
|
|
|
|
|
2017-01-12 12:34:00 +01:00
|
|
|
const Map<StringName,BlendShapeTrack>::Element *E = blend_shape_tracks.find(p_name);
|
2016-05-27 19:18:40 +02:00
|
|
|
if (E) {
|
|
|
|
r_ret = E->get().value;
|
|
|
|
return true;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-05-27 19:18:40 +02:00
|
|
|
if (p_name.operator String().begins_with("material/")) {
|
|
|
|
int idx = p_name.operator String().get_slicec('/',1).to_int();
|
|
|
|
if (idx>=materials.size() || idx<0)
|
|
|
|
return false;
|
|
|
|
r_ret=materials[idx];
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void MeshInstance::_get_property_list( List<PropertyInfo> *p_list) const {
|
|
|
|
|
|
|
|
List<String> ls;
|
2017-01-12 12:34:00 +01:00
|
|
|
for(const Map<StringName,BlendShapeTrack>::Element *E=blend_shape_tracks.front();E;E=E->next()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
ls.push_back(E->key());
|
|
|
|
}
|
|
|
|
|
|
|
|
ls.sort();;
|
|
|
|
|
|
|
|
for(List<String>::Element *E=ls.front();E;E=E->next()) {
|
|
|
|
p_list->push_back( PropertyInfo(Variant::REAL,E->get(),PROPERTY_HINT_RANGE,"0,1,0.01"));
|
|
|
|
}
|
2016-05-27 19:18:40 +02:00
|
|
|
|
|
|
|
if (mesh.is_valid()) {
|
|
|
|
for(int i=0;i<mesh->get_surface_count();i++) {
|
|
|
|
p_list->push_back( PropertyInfo(Variant::OBJECT, "material/"+itos(i), PROPERTY_HINT_RESOURCE_TYPE, "Material"));
|
|
|
|
}
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void MeshInstance::set_mesh(const Ref<Mesh>& p_mesh) {
|
|
|
|
|
2016-05-27 19:18:40 +02:00
|
|
|
if (mesh==p_mesh)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (mesh.is_valid()) {
|
|
|
|
mesh->disconnect(CoreStringNames::get_singleton()->changed,this,SceneStringNames::get_singleton()->_mesh_changed);
|
|
|
|
materials.clear();
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
mesh=p_mesh;
|
|
|
|
|
2017-01-12 12:34:00 +01:00
|
|
|
blend_shape_tracks.clear();
|
2014-02-10 02:10:30 +01:00
|
|
|
if (mesh.is_valid()) {
|
|
|
|
|
|
|
|
|
2017-01-12 12:34:00 +01:00
|
|
|
for(int i=0;i<mesh->get_blend_shape_count();i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-01-12 12:34:00 +01:00
|
|
|
BlendShapeTrack mt;
|
2014-02-10 02:10:30 +01:00
|
|
|
mt.idx=i;
|
|
|
|
mt.value=0;
|
2017-01-12 12:34:00 +01:00
|
|
|
blend_shape_tracks["blend_shapes/"+String(mesh->get_blend_shape_name(i))]=mt;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2016-05-27 19:18:40 +02:00
|
|
|
|
|
|
|
mesh->connect(CoreStringNames::get_singleton()->changed,this,SceneStringNames::get_singleton()->_mesh_changed);
|
|
|
|
materials.resize(mesh->get_surface_count());
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
set_base(mesh->get_rid());
|
|
|
|
} else {
|
|
|
|
|
|
|
|
set_base(RID());
|
|
|
|
}
|
|
|
|
|
2016-05-27 19:18:40 +02:00
|
|
|
_change_notify();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
Ref<Mesh> MeshInstance::get_mesh() const {
|
|
|
|
|
|
|
|
return mesh;
|
|
|
|
}
|
|
|
|
|
2014-05-13 07:24:26 +02:00
|
|
|
void MeshInstance::_resolve_skeleton_path(){
|
|
|
|
|
|
|
|
if (skeleton_path.is_empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
Skeleton *skeleton=get_node(skeleton_path)?get_node(skeleton_path)->cast_to<Skeleton>():NULL;
|
|
|
|
if (skeleton)
|
|
|
|
VisualServer::get_singleton()->instance_attach_skeleton( get_instance(), skeleton->get_skeleton() );
|
|
|
|
}
|
|
|
|
|
|
|
|
void MeshInstance::set_skeleton_path(const NodePath &p_skeleton) {
|
|
|
|
|
|
|
|
skeleton_path = p_skeleton;
|
2014-11-06 01:20:42 +01:00
|
|
|
if (!is_inside_tree())
|
2014-05-13 07:24:26 +02:00
|
|
|
return;
|
|
|
|
_resolve_skeleton_path();
|
|
|
|
}
|
|
|
|
|
|
|
|
NodePath MeshInstance::get_skeleton_path() {
|
|
|
|
return skeleton_path;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-01-11 04:52:51 +01:00
|
|
|
Rect3 MeshInstance::get_aabb() const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (!mesh.is_null())
|
|
|
|
return mesh->get_aabb();
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-01-11 04:52:51 +01:00
|
|
|
return Rect3();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-01-07 22:25:37 +01:00
|
|
|
PoolVector<Face3> MeshInstance::get_faces(uint32_t p_usage_flags) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (!(p_usage_flags&(FACES_SOLID|FACES_ENCLOSING)))
|
2017-01-07 22:25:37 +01:00
|
|
|
return PoolVector<Face3>();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (mesh.is_null())
|
2017-01-07 22:25:37 +01:00
|
|
|
return PoolVector<Face3>();
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
return mesh->get_faces();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Node* MeshInstance::create_trimesh_collision_node() {
|
|
|
|
|
|
|
|
if (mesh.is_null())
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
Ref<Shape> shape = mesh->create_trimesh_shape();
|
|
|
|
if (shape.is_null())
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
StaticBody * static_body = memnew( StaticBody );
|
|
|
|
static_body->add_shape( shape );
|
|
|
|
return static_body;
|
|
|
|
|
2015-05-01 15:44:08 +02:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void MeshInstance::create_trimesh_collision() {
|
|
|
|
|
|
|
|
|
|
|
|
StaticBody* static_body = create_trimesh_collision_node()->cast_to<StaticBody>();
|
|
|
|
ERR_FAIL_COND(!static_body);
|
|
|
|
static_body->set_name( String(get_name()) + "_col" );
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
add_child(static_body);
|
|
|
|
if (get_owner())
|
|
|
|
static_body->set_owner( get_owner() );
|
2014-10-12 07:13:22 +02:00
|
|
|
CollisionShape *cshape = memnew( CollisionShape );
|
|
|
|
cshape->set_shape(static_body->get_shape(0));
|
|
|
|
static_body->add_child(cshape);
|
|
|
|
if (get_owner())
|
|
|
|
cshape->set_owner( get_owner() );
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Node* MeshInstance::create_convex_collision_node() {
|
|
|
|
|
|
|
|
if (mesh.is_null())
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
Ref<Shape> shape = mesh->create_convex_shape();
|
|
|
|
if (shape.is_null())
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
StaticBody * static_body = memnew( StaticBody );
|
|
|
|
static_body->add_shape( shape );
|
|
|
|
return static_body;
|
|
|
|
|
2015-05-01 15:44:08 +02:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void MeshInstance::create_convex_collision() {
|
|
|
|
|
|
|
|
|
|
|
|
StaticBody* static_body = create_convex_collision_node()->cast_to<StaticBody>();
|
|
|
|
ERR_FAIL_COND(!static_body);
|
|
|
|
static_body->set_name( String(get_name()) + "_col" );
|
|
|
|
|
|
|
|
add_child(static_body);
|
|
|
|
if (get_owner())
|
|
|
|
static_body->set_owner( get_owner() );
|
2014-10-12 07:13:22 +02:00
|
|
|
CollisionShape *cshape = memnew( CollisionShape );
|
|
|
|
cshape->set_shape(static_body->get_shape(0));
|
|
|
|
static_body->add_child(cshape);
|
|
|
|
if (get_owner())
|
|
|
|
cshape->set_owner( get_owner() );
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-05-13 07:24:26 +02:00
|
|
|
void MeshInstance::_notification(int p_what) {
|
|
|
|
|
2014-11-06 01:20:42 +01:00
|
|
|
if (p_what==NOTIFICATION_ENTER_TREE) {
|
2014-05-13 07:24:26 +02:00
|
|
|
_resolve_skeleton_path();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-05-27 19:18:40 +02:00
|
|
|
void MeshInstance::set_surface_material(int p_surface,const Ref<Material>& p_material) {
|
|
|
|
|
|
|
|
ERR_FAIL_INDEX(p_surface,materials.size());
|
|
|
|
|
|
|
|
materials[p_surface]=p_material;
|
|
|
|
|
|
|
|
if (materials[p_surface].is_valid())
|
|
|
|
VS::get_singleton()->instance_set_surface_material(get_instance(),p_surface,materials[p_surface]->get_rid());
|
|
|
|
else
|
|
|
|
VS::get_singleton()->instance_set_surface_material(get_instance(),p_surface,RID());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<Material> MeshInstance::get_surface_material(int p_surface) const {
|
|
|
|
|
|
|
|
ERR_FAIL_INDEX_V(p_surface,materials.size(),Ref<Material>());
|
|
|
|
|
|
|
|
return materials[p_surface];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MeshInstance::_mesh_changed() {
|
|
|
|
|
|
|
|
materials.resize( mesh->get_surface_count() );
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
void MeshInstance::_bind_methods() {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-01-03 03:03:46 +01:00
|
|
|
ClassDB::bind_method(_MD("set_mesh","mesh:Mesh"),&MeshInstance::set_mesh);
|
|
|
|
ClassDB::bind_method(_MD("get_mesh:Mesh"),&MeshInstance::get_mesh);
|
|
|
|
ClassDB::bind_method(_MD("set_skeleton_path","skeleton_path:NodePath"),&MeshInstance::set_skeleton_path);
|
|
|
|
ClassDB::bind_method(_MD("get_skeleton_path:NodePath"),&MeshInstance::get_skeleton_path);
|
|
|
|
ClassDB::bind_method(_MD("get_aabb"),&MeshInstance::get_aabb);
|
|
|
|
ClassDB::bind_method(_MD("create_trimesh_collision"),&MeshInstance::create_trimesh_collision);
|
|
|
|
ClassDB::set_method_flags("MeshInstance","create_trimesh_collision",METHOD_FLAGS_DEFAULT);
|
|
|
|
ClassDB::bind_method(_MD("create_convex_collision"),&MeshInstance::create_convex_collision);
|
|
|
|
ClassDB::set_method_flags("MeshInstance","create_convex_collision",METHOD_FLAGS_DEFAULT);
|
|
|
|
ClassDB::bind_method(_MD("_mesh_changed"),&MeshInstance::_mesh_changed);
|
2017-01-04 05:16:14 +01:00
|
|
|
|
|
|
|
ADD_PROPERTY( PropertyInfo( Variant::OBJECT, "mesh", PROPERTY_HINT_RESOURCE_TYPE, "Mesh" ), _SCS("set_mesh"), _SCS("get_mesh"));
|
|
|
|
ADD_PROPERTY( PropertyInfo (Variant::NODE_PATH, "skeleton"), _SCS("set_skeleton_path"), _SCS("get_skeleton_path"));
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
MeshInstance::MeshInstance()
|
|
|
|
{
|
2014-05-13 07:24:26 +02:00
|
|
|
skeleton_path=NodePath("..");
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MeshInstance::~MeshInstance() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|