2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* multimesh.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 14:16:55 +02:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
2017-01-01 22:01:57 +01:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2017-04-08 00:11:42 +02:00
|
|
|
/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
|
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 "multimesh.h"
|
|
|
|
#include "servers/visual_server.h"
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void MultiMesh::_set_transform_array(const PoolVector<Vector3> &p_array) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
int instance_count = get_instance_count();
|
|
|
|
|
2017-01-07 22:25:37 +01:00
|
|
|
PoolVector<Vector3> xforms = p_array;
|
2017-03-05 16:44:50 +01:00
|
|
|
int len = xforms.size();
|
|
|
|
ERR_FAIL_COND((len / 4) != instance_count);
|
|
|
|
if (len == 0)
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
|
|
|
|
2017-01-07 22:25:37 +01:00
|
|
|
PoolVector<Vector3>::Read r = xforms.read();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < len / 4; i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
Transform t;
|
2017-03-05 16:44:50 +01:00
|
|
|
t.basis[0] = r[i * 4 + 0];
|
|
|
|
t.basis[1] = r[i * 4 + 1];
|
|
|
|
t.basis[2] = r[i * 4 + 2];
|
|
|
|
t.origin = r[i * 4 + 3];
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
set_instance_transform(i, t);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-07 22:25:37 +01:00
|
|
|
PoolVector<Vector3> MultiMesh::_get_transform_array() const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
int instance_count = get_instance_count();
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (instance_count == 0)
|
2017-01-07 22:25:37 +01:00
|
|
|
return PoolVector<Vector3>();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-01-07 22:25:37 +01:00
|
|
|
PoolVector<Vector3> xforms;
|
2017-03-05 16:44:50 +01:00
|
|
|
xforms.resize(instance_count * 4);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-01-07 22:25:37 +01:00
|
|
|
PoolVector<Vector3>::Write w = xforms.write();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < instance_count; i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Transform t = get_instance_transform(i);
|
|
|
|
w[i * 4 + 0] = t.basis[0];
|
|
|
|
w[i * 4 + 1] = t.basis[1];
|
|
|
|
w[i * 4 + 2] = t.basis[2];
|
|
|
|
w[i * 4 + 3] = t.origin;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return xforms;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void MultiMesh::_set_color_array(const PoolVector<Color> &p_array) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
int instance_count = get_instance_count();
|
|
|
|
|
2017-01-07 22:25:37 +01:00
|
|
|
PoolVector<Color> colors = p_array;
|
2017-03-05 16:44:50 +01:00
|
|
|
int len = colors.size();
|
2014-02-10 02:10:30 +01:00
|
|
|
ERR_FAIL_COND(len != instance_count);
|
2017-03-05 16:44:50 +01:00
|
|
|
if (len == 0)
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
|
|
|
|
2017-01-07 22:25:37 +01:00
|
|
|
PoolVector<Color>::Read r = colors.read();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < len; i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
set_instance_color(i, r[i]);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-07 22:25:37 +01:00
|
|
|
PoolVector<Color> MultiMesh::_get_color_array() const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
int instance_count = get_instance_count();
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (instance_count == 0)
|
2017-01-07 22:25:37 +01:00
|
|
|
return PoolVector<Color>();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-01-07 22:25:37 +01:00
|
|
|
PoolVector<Color> colors;
|
2014-02-10 02:10:30 +01:00
|
|
|
colors.resize(instance_count);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < instance_count; i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
colors.set(i, get_instance_color(i));
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return colors;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void MultiMesh::set_mesh(const Ref<Mesh> &p_mesh) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
mesh = p_mesh;
|
2014-02-10 02:10:30 +01:00
|
|
|
if (!mesh.is_null())
|
2017-03-05 16:44:50 +01:00
|
|
|
VisualServer::get_singleton()->multimesh_set_mesh(multimesh, mesh->get_rid());
|
2014-02-10 02:10:30 +01:00
|
|
|
else
|
2017-03-05 16:44:50 +01:00
|
|
|
VisualServer::get_singleton()->multimesh_set_mesh(multimesh, RID());
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Ref<Mesh> MultiMesh::get_mesh() const {
|
|
|
|
|
|
|
|
return mesh;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MultiMesh::set_instance_count(int p_count) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
VisualServer::get_singleton()->multimesh_allocate(multimesh, p_count, VS::MultimeshTransformFormat(transform_format), VS::MultimeshColorFormat(color_format));
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
int MultiMesh::get_instance_count() const {
|
|
|
|
|
|
|
|
return VisualServer::get_singleton()->multimesh_get_instance_count(multimesh);
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void MultiMesh::set_instance_transform(int p_instance, const Transform &p_transform) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
VisualServer::get_singleton()->multimesh_instance_set_transform(multimesh, p_instance, p_transform);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
Transform MultiMesh::get_instance_transform(int p_instance) const {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
return VisualServer::get_singleton()->multimesh_instance_get_transform(multimesh, p_instance);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void MultiMesh::set_instance_color(int p_instance, const Color &p_color) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
VisualServer::get_singleton()->multimesh_instance_set_color(multimesh, p_instance, p_color);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
Color MultiMesh::get_instance_color(int p_instance) const {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
return VisualServer::get_singleton()->multimesh_instance_get_color(multimesh, p_instance);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-01-11 04:52:51 +01:00
|
|
|
Rect3 MultiMesh::get_aabb() const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-11-22 05:26:56 +01:00
|
|
|
return VisualServer::get_singleton()->multimesh_get_aabb(multimesh);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
RID MultiMesh::get_rid() const {
|
|
|
|
|
|
|
|
return multimesh;
|
|
|
|
}
|
|
|
|
|
2016-10-03 21:33:42 +02:00
|
|
|
void MultiMesh::set_color_format(ColorFormat p_color_format) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
color_format = p_color_format;
|
2016-10-03 21:33:42 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
MultiMesh::ColorFormat MultiMesh::get_color_format() const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-10-03 21:33:42 +02:00
|
|
|
return color_format;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void MultiMesh::set_transform_format(TransformFormat p_transform_format) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
transform_format = p_transform_format;
|
2016-10-03 21:33:42 +02:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
MultiMesh::TransformFormat MultiMesh::get_transform_format() const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-10-03 21:33:42 +02:00
|
|
|
return transform_format;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void MultiMesh::_bind_methods() {
|
|
|
|
|
2017-08-09 13:19:41 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("set_mesh", "mesh"), &MultiMesh::set_mesh);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_mesh"), &MultiMesh::get_mesh);
|
2017-03-05 16:44:50 +01:00
|
|
|
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_transform_format", "format"), &MultiMesh::set_transform_format);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_transform_format"), &MultiMesh::get_transform_format);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("set_instance_count", "count"), &MultiMesh::set_instance_count);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_instance_count"), &MultiMesh::get_instance_count);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_instance_transform", "instance", "transform"), &MultiMesh::set_instance_transform);
|
|
|
|
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("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);
|
|
|
|
|
|
|
|
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::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), "_set_transform_array", "_get_transform_array");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::POOL_COLOR_ARRAY, "color_array", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "_set_color_array", "_get_color_array");
|
|
|
|
|
2017-08-20 17:45:01 +02:00
|
|
|
BIND_ENUM_CONSTANT(TRANSFORM_2D);
|
|
|
|
BIND_ENUM_CONSTANT(TRANSFORM_3D);
|
|
|
|
|
|
|
|
BIND_ENUM_CONSTANT(COLOR_NONE);
|
|
|
|
BIND_ENUM_CONSTANT(COLOR_8BIT);
|
|
|
|
BIND_ENUM_CONSTANT(COLOR_FLOAT);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
MultiMesh::MultiMesh() {
|
|
|
|
|
|
|
|
multimesh = VisualServer::get_singleton()->multimesh_create();
|
2017-03-05 16:44:50 +01:00
|
|
|
color_format = COLOR_NONE;
|
|
|
|
transform_format = TRANSFORM_2D;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
MultiMesh::~MultiMesh() {
|
|
|
|
|
|
|
|
VisualServer::get_singleton()->free(multimesh);
|
|
|
|
}
|