mirror of
https://github.com/Relintai/voxelman.git
synced 2024-11-14 10:17:20 +01:00
Ported merge_mesh_array and merge_mesh_array from Broken Seals.
This commit is contained in:
parent
4a3c9836ff
commit
4691e2aaef
@ -414,6 +414,88 @@ void VoxelChunk::clear() {
|
||||
call("_clear");
|
||||
}
|
||||
|
||||
Array VoxelChunk::merge_mesh_array(Array arr) const {
|
||||
ERR_FAIL_COND_V(arr.size() != VisualServer::ARRAY_MAX, arr);
|
||||
|
||||
PoolVector3Array verts = arr[VisualServer::ARRAY_VERTEX];
|
||||
PoolVector3Array normals = arr[VisualServer::ARRAY_NORMAL];
|
||||
PoolVector2Array uvs = arr[VisualServer::ARRAY_TEX_UV];
|
||||
PoolColorArray colors = arr[VisualServer::ARRAY_COLOR];
|
||||
PoolIntArray indices = arr[VisualServer::ARRAY_INDEX];
|
||||
|
||||
int i = 0;
|
||||
while (i < verts.size()) {
|
||||
Vector3 v = verts[i];
|
||||
|
||||
Array equals;
|
||||
for (int j = i + 1; j < verts.size(); ++j) {
|
||||
Vector3 vc = verts[j];
|
||||
|
||||
if (Math::is_equal_approx(v.x, vc.x) && Math::is_equal_approx(v.y, vc.y) && Math::is_equal_approx(v.z, vc.z))
|
||||
equals.push_back(j);
|
||||
}
|
||||
|
||||
for (int k = 0; k < equals.size(); ++k) {
|
||||
int rem = equals[k];
|
||||
int remk = rem - k;
|
||||
|
||||
verts.remove(remk);
|
||||
normals.remove(remk);
|
||||
uvs.remove(remk);
|
||||
colors.remove(remk);
|
||||
|
||||
for (int j = 0; j < indices.size(); ++j) {
|
||||
int indx = indices[j];
|
||||
|
||||
if (indx == remk)
|
||||
indices.set(j, i);
|
||||
else if (indx > remk)
|
||||
indices.set(j, indx - 1);
|
||||
}
|
||||
}
|
||||
|
||||
++i;
|
||||
}
|
||||
|
||||
arr[VisualServer::ARRAY_VERTEX] = verts;
|
||||
arr[VisualServer::ARRAY_NORMAL] = normals;
|
||||
arr[VisualServer::ARRAY_TEX_UV] = uvs;
|
||||
arr[VisualServer::ARRAY_COLOR] = colors;
|
||||
arr[VisualServer::ARRAY_INDEX] = indices;
|
||||
|
||||
return arr;
|
||||
}
|
||||
Array VoxelChunk::bake_mesh_array_uv(Array arr, Ref<Texture> tex, float mul_color) const {
|
||||
ERR_FAIL_COND_V(arr.size() != VisualServer::ARRAY_MAX, arr);
|
||||
ERR_FAIL_COND_V(!tex.is_valid(), arr);
|
||||
|
||||
Ref<Image> img = tex->get_data();
|
||||
|
||||
ERR_FAIL_COND_V(!img.is_valid(), arr);
|
||||
|
||||
Vector2 imgsize = img->get_size();
|
||||
|
||||
PoolVector2Array uvs = arr[VisualServer::ARRAY_TEX_UV];
|
||||
PoolColorArray colors = arr[VisualServer::ARRAY_COLOR];
|
||||
|
||||
img->lock();
|
||||
|
||||
for (int i = 0; i < uvs.size(); ++i) {
|
||||
Vector2 uv = uvs[i];
|
||||
uv *= imgsize;
|
||||
|
||||
Color c = img->get_pixelv(uv);
|
||||
|
||||
colors[i] = colors[i] * c * mul_color;
|
||||
}
|
||||
|
||||
img->unlock();
|
||||
|
||||
arr[VisualServer::ARRAY_COLOR] = colors;
|
||||
|
||||
return arr;
|
||||
}
|
||||
|
||||
void VoxelChunk::add_lights(Array lights) {
|
||||
for (int i = 0; i < lights.size(); ++i) {
|
||||
Ref<VoxelLight> light = Ref<VoxelLight>(lights.get(i));
|
||||
@ -745,6 +827,9 @@ void VoxelChunk::_bind_methods() {
|
||||
ClassDB::bind_method(D_METHOD("get_data_index", "x", "y", "z"), &VoxelChunk::get_data_index);
|
||||
ClassDB::bind_method(D_METHOD("get_data_size"), &VoxelChunk::get_data_size);
|
||||
|
||||
ClassDB::bind_method(D_METHOD("merge_mesh_array", "arr"), &VoxelChunk::merge_mesh_array);
|
||||
ClassDB::bind_method(D_METHOD("bake_mesh_array_uv", "arr", "tex", "mul_color"), &VoxelChunk::bake_mesh_array_uv, DEFVAL(0.7));
|
||||
|
||||
//Meshes
|
||||
ClassDB::bind_method(D_METHOD("add_lights", "lights"), &VoxelChunk::add_lights);
|
||||
ClassDB::bind_method(D_METHOD("add_voxel_light", "light"), &VoxelChunk::add_voxel_light);
|
||||
|
@ -155,11 +155,12 @@ public:
|
||||
|
||||
//Meshing
|
||||
void create_meshers();
|
||||
|
||||
void build(bool immediate = false);
|
||||
|
||||
void clear();
|
||||
|
||||
Array merge_mesh_array(Array arr) const;
|
||||
Array bake_mesh_array_uv(Array arr, Ref<Texture> tex, float mul_color = 0.7) const;
|
||||
|
||||
//lights
|
||||
void add_lights(Array lights);
|
||||
void add_voxel_light(Ref<VoxelLight> light);
|
||||
|
Loading…
Reference in New Issue
Block a user