From 53070437514e448c87f6cb31cf5b27a3839dbfa1 Mon Sep 17 00:00:00 2001 From: Juan Linietsky Date: Wed, 5 Sep 2018 19:14:39 -0300 Subject: Fixes to multimesh, added custom data and checks to avoid reconfiguring if instance count > 0. Fixes #12595 --- scene/resources/multimesh.cpp | 88 ++++++++++++++++++++++++++++++++++++------- scene/resources/multimesh.h | 18 +++++++++ 2 files changed, 92 insertions(+), 14 deletions(-) (limited to 'scene') diff --git a/scene/resources/multimesh.cpp b/scene/resources/multimesh.cpp index 4d0a14e3aa..e8e19fdb60 100644 --- a/scene/resources/multimesh.cpp +++ b/scene/resources/multimesh.cpp @@ -33,8 +33,6 @@ void MultiMesh::_set_transform_array(const PoolVector &p_array) { - int instance_count = get_instance_count(); - PoolVector xforms = p_array; int len = xforms.size(); ERR_FAIL_COND((len / 4) != instance_count); @@ -57,8 +55,6 @@ void MultiMesh::_set_transform_array(const PoolVector &p_array) { PoolVector MultiMesh::_get_transform_array() const { - int instance_count = get_instance_count(); - if (instance_count == 0) return PoolVector(); @@ -81,13 +77,11 @@ PoolVector MultiMesh::_get_transform_array() const { void MultiMesh::_set_color_array(const PoolVector &p_array) { - int instance_count = get_instance_count(); - PoolVector colors = p_array; int len = colors.size(); - ERR_FAIL_COND(len != instance_count); if (len == 0) return; + ERR_FAIL_COND(len != instance_count); PoolVector::Read r = colors.read(); @@ -99,9 +93,7 @@ void MultiMesh::_set_color_array(const PoolVector &p_array) { PoolVector MultiMesh::_get_color_array() const { - int instance_count = get_instance_count(); - - if (instance_count == 0) + if (instance_count == 0 || color_format == COLOR_NONE) return PoolVector(); PoolVector colors; @@ -115,6 +107,37 @@ PoolVector MultiMesh::_get_color_array() const { return colors; } +void MultiMesh::_set_custom_data_array(const PoolVector &p_array) { + + PoolVector custom_datas = p_array; + int len = custom_datas.size(); + if (len == 0) + return; + ERR_FAIL_COND(len != instance_count); + + PoolVector::Read r = custom_datas.read(); + + for (int i = 0; i < len; i++) { + + set_instance_custom_data(i, r[i]); + } +} + +PoolVector MultiMesh::_get_custom_data_array() const { + + if (instance_count == 0 || custom_data_format == CUSTOM_DATA_NONE) + return PoolVector(); + + PoolVector custom_datas; + custom_datas.resize(instance_count); + + for (int i = 0; i < instance_count; i++) { + + custom_datas.set(i, get_instance_custom_data(i)); + } + + return custom_datas; +} void MultiMesh::set_mesh(const Ref &p_mesh) { mesh = p_mesh; @@ -130,12 +153,13 @@ Ref MultiMesh::get_mesh() const { } void MultiMesh::set_instance_count(int p_count) { - - VisualServer::get_singleton()->multimesh_allocate(multimesh, p_count, VS::MultimeshTransformFormat(transform_format), VS::MultimeshColorFormat(color_format)); + ERR_FAIL_COND(p_count < 0); + VisualServer::get_singleton()->multimesh_allocate(multimesh, p_count, VS::MultimeshTransformFormat(transform_format), VS::MultimeshColorFormat(color_format), VS::MultimeshCustomDataFormat(custom_data_format)); + instance_count = p_count; } int MultiMesh::get_instance_count() const { - return VisualServer::get_singleton()->multimesh_get_instance_count(multimesh); + return instance_count; } void MultiMesh::set_instance_transform(int p_instance, const Transform &p_transform) { @@ -156,6 +180,15 @@ Color MultiMesh::get_instance_color(int p_instance) const { return VisualServer::get_singleton()->multimesh_instance_get_color(multimesh, p_instance); } +void MultiMesh::set_instance_custom_data(int p_instance, const Color &p_custom_data) { + + VisualServer::get_singleton()->multimesh_instance_set_custom_data(multimesh, p_instance, p_custom_data); +} +Color MultiMesh::get_instance_custom_data(int p_instance) const { + + return VisualServer::get_singleton()->multimesh_instance_get_custom_data(multimesh, p_instance); +} + AABB MultiMesh::get_aabb() const { return VisualServer::get_singleton()->multimesh_get_aabb(multimesh); @@ -168,6 +201,7 @@ RID MultiMesh::get_rid() const { void MultiMesh::set_color_format(ColorFormat p_color_format) { + ERR_FAIL_COND(instance_count > 0); color_format = p_color_format; } @@ -176,8 +210,20 @@ MultiMesh::ColorFormat MultiMesh::get_color_format() const { return color_format; } +void MultiMesh::set_custom_data_format(CustomDataFormat p_custom_data_format) { + + ERR_FAIL_COND(instance_count > 0); + custom_data_format = p_custom_data_format; +} + +MultiMesh::CustomDataFormat MultiMesh::get_custom_data_format() const { + + return custom_data_format; +} + void MultiMesh::set_transform_format(TransformFormat p_transform_format) { + ERR_FAIL_COND(instance_count > 0); transform_format = p_transform_format; } MultiMesh::TransformFormat MultiMesh::get_transform_format() const { @@ -191,6 +237,8 @@ void MultiMesh::_bind_methods() { ClassDB::bind_method(D_METHOD("get_mesh"), &MultiMesh::get_mesh); ClassDB::bind_method(D_METHOD("set_color_format", "format"), &MultiMesh::set_color_format); ClassDB::bind_method(D_METHOD("get_color_format"), &MultiMesh::get_color_format); + ClassDB::bind_method(D_METHOD("set_custom_data_format", "format"), &MultiMesh::set_custom_data_format); + ClassDB::bind_method(D_METHOD("get_custom_data_format"), &MultiMesh::get_custom_data_format); ClassDB::bind_method(D_METHOD("set_transform_format", "format"), &MultiMesh::set_transform_format); ClassDB::bind_method(D_METHOD("get_transform_format"), &MultiMesh::get_transform_format); @@ -200,19 +248,25 @@ void MultiMesh::_bind_methods() { ClassDB::bind_method(D_METHOD("get_instance_transform", "instance"), &MultiMesh::get_instance_transform); ClassDB::bind_method(D_METHOD("set_instance_color", "instance", "color"), &MultiMesh::set_instance_color); ClassDB::bind_method(D_METHOD("get_instance_color", "instance"), &MultiMesh::get_instance_color); + ClassDB::bind_method(D_METHOD("set_instance_custom_data", "instance", "custom_data"), &MultiMesh::set_instance_custom_data); + ClassDB::bind_method(D_METHOD("get_instance_custom_data", "instance"), &MultiMesh::get_instance_custom_data); ClassDB::bind_method(D_METHOD("get_aabb"), &MultiMesh::get_aabb); ClassDB::bind_method(D_METHOD("_set_transform_array"), &MultiMesh::_set_transform_array); ClassDB::bind_method(D_METHOD("_get_transform_array"), &MultiMesh::_get_transform_array); ClassDB::bind_method(D_METHOD("_set_color_array"), &MultiMesh::_set_color_array); ClassDB::bind_method(D_METHOD("_get_color_array"), &MultiMesh::_get_color_array); + ClassDB::bind_method(D_METHOD("_set_custom_data_array"), &MultiMesh::_set_custom_data_array); + ClassDB::bind_method(D_METHOD("_get_custom_data_array"), &MultiMesh::_get_custom_data_array); ADD_PROPERTY(PropertyInfo(Variant::INT, "color_format", PROPERTY_HINT_ENUM, "None,Byte,Float"), "set_color_format", "get_color_format"); ADD_PROPERTY(PropertyInfo(Variant::INT, "transform_format", PROPERTY_HINT_ENUM, "2D,3D"), "set_transform_format", "get_transform_format"); - ADD_PROPERTY(PropertyInfo(Variant::INT, "instance_count", PROPERTY_HINT_RANGE, "0,16384,1"), "set_instance_count", "get_instance_count"); + ADD_PROPERTY(PropertyInfo(Variant::INT, "custom_data_format", PROPERTY_HINT_ENUM, "None,Byte,Float"), "set_custom_data_format", "get_custom_data_format"); + ADD_PROPERTY(PropertyInfo(Variant::INT, "instance_count", PROPERTY_HINT_RANGE, "0,16384,1,or_greater"), "set_instance_count", "get_instance_count"); ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "mesh", PROPERTY_HINT_RESOURCE_TYPE, "Mesh"), "set_mesh", "get_mesh"); ADD_PROPERTY(PropertyInfo(Variant::POOL_VECTOR3_ARRAY, "transform_array", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "_set_transform_array", "_get_transform_array"); ADD_PROPERTY(PropertyInfo(Variant::POOL_COLOR_ARRAY, "color_array", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "_set_color_array", "_get_color_array"); + ADD_PROPERTY(PropertyInfo(Variant::POOL_COLOR_ARRAY, "custom_data_array", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "_set_custom_data_array", "_get_custom_data_array"); BIND_ENUM_CONSTANT(TRANSFORM_2D); BIND_ENUM_CONSTANT(TRANSFORM_3D); @@ -220,13 +274,19 @@ void MultiMesh::_bind_methods() { BIND_ENUM_CONSTANT(COLOR_NONE); BIND_ENUM_CONSTANT(COLOR_8BIT); BIND_ENUM_CONSTANT(COLOR_FLOAT); + + BIND_ENUM_CONSTANT(CUSTOM_DATA_NONE); + BIND_ENUM_CONSTANT(CUSTOM_DATA_8BIT); + BIND_ENUM_CONSTANT(CUSTOM_DATA_FLOAT); } MultiMesh::MultiMesh() { multimesh = VisualServer::get_singleton()->multimesh_create(); color_format = COLOR_NONE; + custom_data_format = CUSTOM_DATA_NONE; transform_format = TRANSFORM_2D; + instance_count = 0; } MultiMesh::~MultiMesh() { diff --git a/scene/resources/multimesh.h b/scene/resources/multimesh.h index 0875d6d06d..ec5a5ca4d5 100644 --- a/scene/resources/multimesh.h +++ b/scene/resources/multimesh.h @@ -51,11 +51,19 @@ public: COLOR_FLOAT = VS::MULTIMESH_COLOR_FLOAT, }; + enum CustomDataFormat { + CUSTOM_DATA_NONE, + CUSTOM_DATA_8BIT, + CUSTOM_DATA_FLOAT, + }; + private: Ref mesh; RID multimesh; TransformFormat transform_format; ColorFormat color_format; + CustomDataFormat custom_data_format; + int instance_count; protected: static void _bind_methods(); @@ -66,6 +74,9 @@ protected: void _set_color_array(const PoolVector &p_array); PoolVector _get_color_array() const; + void _set_custom_data_array(const PoolVector &p_array); + PoolVector _get_custom_data_array() const; + public: void set_mesh(const Ref &p_mesh); Ref get_mesh() const; @@ -73,6 +84,9 @@ public: void set_color_format(ColorFormat p_color_format); ColorFormat get_color_format() const; + void set_custom_data_format(CustomDataFormat p_custom_data_format); + CustomDataFormat get_custom_data_format() const; + void set_transform_format(TransformFormat p_transform_format); TransformFormat get_transform_format() const; @@ -85,6 +99,9 @@ public: void set_instance_color(int p_instance, const Color &p_color); Color get_instance_color(int p_instance) const; + void set_instance_custom_data(int p_instance, const Color &p_custom_data); + Color get_instance_custom_data(int p_instance) const; + virtual AABB get_aabb() const; virtual RID get_rid() const; @@ -95,5 +112,6 @@ public: VARIANT_ENUM_CAST(MultiMesh::TransformFormat); VARIANT_ENUM_CAST(MultiMesh::ColorFormat); +VARIANT_ENUM_CAST(MultiMesh::CustomDataFormat); #endif // MULTI_MESH_H -- cgit v1.2.3