2020-01-31 19:52:37 +01:00
|
|
|
/*
|
|
|
|
Copyright (c) 2019-2020 Péter Magyar
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2019-07-13 22:03:23 +02:00
|
|
|
#include "voxel_mesher_cubic.h"
|
|
|
|
|
2020-04-06 13:41:45 +02:00
|
|
|
#include "../../world/default/voxel_chunk_default.h"
|
2020-03-12 23:23:38 +01:00
|
|
|
|
2020-05-23 10:34:47 +02:00
|
|
|
#include "../../defines.h"
|
|
|
|
|
|
|
|
#include visual_server_h
|
2020-04-09 12:34:39 +02:00
|
|
|
|
2020-04-02 21:28:19 +02:00
|
|
|
void VoxelMesherCubic::_add_chunk(Ref<VoxelChunk> p_chunk) {
|
|
|
|
Ref<VoxelChunkDefault> chunk = p_chunk;
|
2019-11-18 22:35:08 +01:00
|
|
|
|
2020-04-02 21:28:19 +02:00
|
|
|
ERR_FAIL_COND(!chunk.is_valid());
|
2019-11-18 22:35:08 +01:00
|
|
|
|
2020-08-21 20:02:22 +02:00
|
|
|
if (!chunk->get_channel(_channel_index_type)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (chunk->get_channel(VoxelChunkDefault::DEFAULT_CHANNEL_RANDOM_AO)) {
|
|
|
|
chunk->generate_ao();
|
|
|
|
}
|
|
|
|
|
2020-02-14 03:19:15 +01:00
|
|
|
int x_size = chunk->get_size_x();
|
|
|
|
int y_size = chunk->get_size_y();
|
|
|
|
int z_size = chunk->get_size_z();
|
2019-07-17 17:01:12 +02:00
|
|
|
|
|
|
|
float voxel_size = get_lod_size();
|
|
|
|
float voxel_scale = get_voxel_scale();
|
|
|
|
|
|
|
|
Ref<VoxelCubePoints> cube_points;
|
|
|
|
cube_points.instance();
|
2020-08-21 20:02:22 +02:00
|
|
|
cube_points->set_channel_index_type(_channel_index_type);
|
|
|
|
cube_points->set_channel_index_isolevel(_channel_index_isolevel);
|
2019-07-17 17:01:12 +02:00
|
|
|
|
2019-07-17 19:51:09 +02:00
|
|
|
Color base_light(_base_light_value, _base_light_value, _base_light_value);
|
2019-07-17 17:01:12 +02:00
|
|
|
|
2020-02-14 03:19:15 +01:00
|
|
|
for (int y = 0; y < y_size; ++y) {
|
|
|
|
for (int z = 0; z < z_size; ++z) {
|
|
|
|
for (int x = 0; x < x_size; ++x) {
|
2019-07-17 17:01:12 +02:00
|
|
|
|
2020-02-14 03:19:15 +01:00
|
|
|
cube_points->setup(chunk, x, y, z, 1);
|
2019-07-17 17:01:12 +02:00
|
|
|
|
|
|
|
if (!cube_points->has_points())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (int face = 0; face < VoxelCubePoints::VOXEL_FACE_COUNT; ++face) {
|
|
|
|
if (!cube_points->is_face_visible(face))
|
|
|
|
continue;
|
|
|
|
|
2020-08-21 12:10:15 +02:00
|
|
|
uint8_t type = cube_points->get_face_type(face);
|
|
|
|
|
|
|
|
Ref<VoxelSurface> surface = _library->get_voxel_surface(type);
|
|
|
|
|
|
|
|
if (!surface.is_valid())
|
|
|
|
continue;
|
|
|
|
|
2019-07-17 17:01:12 +02:00
|
|
|
add_indices(get_vertex_count() + 2);
|
|
|
|
add_indices(get_vertex_count() + 1);
|
|
|
|
add_indices(get_vertex_count() + 0);
|
|
|
|
add_indices(get_vertex_count() + 3);
|
|
|
|
add_indices(get_vertex_count() + 2);
|
|
|
|
add_indices(get_vertex_count() + 0);
|
|
|
|
|
|
|
|
Vector3 vertices[4] = {
|
|
|
|
cube_points->get_point_for_face(face, 0),
|
|
|
|
cube_points->get_point_for_face(face, 1),
|
|
|
|
cube_points->get_point_for_face(face, 2),
|
|
|
|
cube_points->get_point_for_face(face, 3)
|
|
|
|
};
|
|
|
|
|
|
|
|
Vector3 normals[4] = {
|
|
|
|
(vertices[3] - vertices[0]).cross(vertices[1] - vertices[0]),
|
|
|
|
(vertices[0] - vertices[1]).cross(vertices[2] - vertices[1]),
|
|
|
|
(vertices[1] - vertices[2]).cross(vertices[3] - vertices[2]),
|
|
|
|
(vertices[2] - vertices[3]).cross(vertices[0] - vertices[3])
|
|
|
|
};
|
|
|
|
|
|
|
|
Vector3 face_light_direction = cube_points->get_face_light_direction(face);
|
|
|
|
|
|
|
|
for (int i = 0; i < 4; ++i) {
|
2020-01-12 21:58:14 +01:00
|
|
|
add_normal(normals[i]);
|
|
|
|
|
2019-07-17 17:01:12 +02:00
|
|
|
Color light = cube_points->get_face_point_light_color(face, i);
|
|
|
|
light += base_light;
|
|
|
|
|
|
|
|
float NdotL = CLAMP(normals[i].dot(face_light_direction), 0, 1.0);
|
|
|
|
|
|
|
|
light *= NdotL;
|
|
|
|
|
|
|
|
light -= cube_points->get_face_point_ao_color(face, i) * _ao_strength;
|
|
|
|
|
|
|
|
light.r = CLAMP(light.r, 0, 1.0);
|
|
|
|
light.g = CLAMP(light.g, 0, 1.0);
|
|
|
|
light.b = CLAMP(light.b, 0, 1.0);
|
2020-08-21 12:34:56 +02:00
|
|
|
light.a = 1.0;
|
2019-07-17 17:01:12 +02:00
|
|
|
|
2019-07-18 18:56:42 +02:00
|
|
|
add_color(light);
|
|
|
|
|
2020-08-21 12:21:47 +02:00
|
|
|
Vector2 uv = cube_points->get_point_uv_direction(face, i) + Vector2(0.5, 0.5);
|
2020-08-21 12:10:15 +02:00
|
|
|
|
|
|
|
VoxelSurface::VoxelSurfaceSides side = VoxelSurface::VOXEL_SIDE_SIDE;
|
|
|
|
|
|
|
|
switch (face) {
|
|
|
|
case VoxelCubePoints::VOXEL_FACE_TOP:
|
|
|
|
side = VoxelSurface::VOXEL_SIDE_TOP;
|
|
|
|
break;
|
|
|
|
case VoxelCubePoints::VOXEL_FACE_BOTTOM:
|
|
|
|
side = VoxelSurface::VOXEL_SIDE_BOTTOM;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-08-21 12:19:18 +02:00
|
|
|
uv = surface->transform_uv_scaled(side, uv, x % get_texture_scale(), z % get_texture_scale(), get_texture_scale());
|
2020-08-21 12:10:15 +02:00
|
|
|
|
|
|
|
add_uv(uv);
|
|
|
|
add_uv2(uv);
|
2019-07-17 17:01:12 +02:00
|
|
|
|
|
|
|
add_vertex((vertices[i] * voxel_size + Vector3(x, y, z)) * voxel_scale);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-07-13 22:03:23 +02:00
|
|
|
|
|
|
|
VoxelMesherCubic::VoxelMesherCubic() {
|
2020-03-08 22:36:58 +01:00
|
|
|
_format = VisualServer::ARRAY_FORMAT_NORMAL | VisualServer::ARRAY_FORMAT_COLOR | VisualServer::ARRAY_FORMAT_TEX_UV;
|
2020-08-21 12:19:18 +02:00
|
|
|
|
|
|
|
_texture_scale = 1;
|
2019-07-13 22:03:23 +02:00
|
|
|
}
|
|
|
|
|
2019-07-17 17:01:12 +02:00
|
|
|
VoxelMesherCubic::~VoxelMesherCubic() {
|
|
|
|
}
|
2019-07-13 22:03:23 +02:00
|
|
|
|
|
|
|
void VoxelMesherCubic::_bind_methods() {
|
2020-01-12 21:58:14 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("_add_chunk", "buffer"), &VoxelMesherCubic::_add_chunk);
|
2019-07-13 22:03:23 +02:00
|
|
|
}
|