diff --git a/data/terrain_2d_light.cpp b/data/terrain_2d_light.cpp index ef4ec25..083f9a3 100644 --- a/data/terrain_2d_light.cpp +++ b/data/terrain_2d_light.cpp @@ -28,16 +28,12 @@ _FORCE_INLINE_ int Terrain2DLight::get_world_position_x() const { _FORCE_INLINE_ int Terrain2DLight::get_world_position_y() const { return _world_position_y; } -_FORCE_INLINE_ int Terrain2DLight::get_world_position_z() const { - return _world_position_z; +Vector2 Terrain2DLight::get_world_position() { + return Vector2(_world_position_x, _world_position_y); } -Vector3 Terrain2DLight::get_world_position() { - return Vector3(_world_position_x, _world_position_y, _world_position_z); -} -void Terrain2DLight::set_world_position(const int x, const int y, const int z) { +void Terrain2DLight::set_world_position(const int x, const int y) { _world_position_x = x; _world_position_y = y; - _world_position_z = z; } _FORCE_INLINE_ Color Terrain2DLight::get_color() const { @@ -56,6 +52,8 @@ void Terrain2DLight::set_size(const float size) { Terrain2DLight::Terrain2DLight() { _size = 0; + _world_position_x = 0; + _world_position_y = 0; } Terrain2DLight::~Terrain2DLight() { @@ -64,8 +62,7 @@ Terrain2DLight::~Terrain2DLight() { void Terrain2DLight::_bind_methods() { ClassDB::bind_method(D_METHOD("get_world_position_x"), &Terrain2DLight::get_world_position_x); ClassDB::bind_method(D_METHOD("get_world_position_y"), &Terrain2DLight::get_world_position_y); - ClassDB::bind_method(D_METHOD("get_world_position_z"), &Terrain2DLight::get_world_position_z); - ClassDB::bind_method(D_METHOD("set_world_position", "x", "y", "z"), &Terrain2DLight::set_world_position); + ClassDB::bind_method(D_METHOD("set_world_position", "x", "y"), &Terrain2DLight::set_world_position); ClassDB::bind_method(D_METHOD("get_color"), &Terrain2DLight::get_color); ClassDB::bind_method(D_METHOD("set_color"), &Terrain2DLight::set_color); diff --git a/data/terrain_2d_light.h b/data/terrain_2d_light.h index 1dd799d..86a5031 100644 --- a/data/terrain_2d_light.h +++ b/data/terrain_2d_light.h @@ -45,9 +45,8 @@ class Terrain2DLight : public Reference { public: int get_world_position_x() const; int get_world_position_y() const; - int get_world_position_z() const; - Vector3 get_world_position(); - void set_world_position(const int x, const int y, const int z); + Vector2 get_world_position(); + void set_world_position(const int x, const int y); Color get_color() const; void set_color(const Color &color); @@ -62,13 +61,8 @@ private: static void _bind_methods(); private: - int _chunk_position_x; - int _chunk_position_y; - int _chunk_position_z; - int _world_position_x; int _world_position_y; - int _world_position_z; Color _color; int _size; diff --git a/meshers/blocky/terrain_2d_mesher_blocky.cpp b/meshers/blocky/terrain_2d_mesher_blocky.cpp index 8b24381..97a32a5 100644 --- a/meshers/blocky/terrain_2d_mesher_blocky.cpp +++ b/meshers/blocky/terrain_2d_mesher_blocky.cpp @@ -40,13 +40,7 @@ void Terrain2DMesherBlocky::_add_chunk(Ref p_chunk) { ERR_FAIL_COND(chunk->get_margin_end() < 1); ERR_FAIL_COND(chunk->get_margin_start() < 1); - if (_lod_index == 0) { - add_chunk_normal(chunk); - } else { - //todo give error message if the chunk is badly sized for the given lod index? - - add_chunk_lod(chunk); - } + add_chunk_normal(chunk); } void Terrain2DMesherBlocky::add_chunk_normal(Ref chunk) { @@ -56,8 +50,6 @@ void Terrain2DMesherBlocky::add_chunk_normal(Ref chunk) { int x_size = chunk->get_size_x(); int z_size = chunk->get_size_z(); - float world_height = chunk->get_world_height(); - float voxel_scale = get_voxel_scale(); uint8_t *channel_type = chunk->channel_get(_channel_index_type); @@ -65,11 +57,6 @@ void Terrain2DMesherBlocky::add_chunk_normal(Ref chunk) { if (!channel_type) return; - uint8_t *channel_isolevel = chunk->channel_get(_channel_index_isolevel); - - if (!channel_isolevel) - return; - uint8_t *channel_color_r = NULL; uint8_t *channel_color_g = NULL; uint8_t *channel_color_b = NULL; @@ -128,13 +115,6 @@ void Terrain2DMesherBlocky::add_chunk_normal(Ref chunk) { if (!surface.is_valid()) continue; - uint8_t isolevels[] = { - channel_isolevel[indexes[0]], - channel_isolevel[indexes[1]], - channel_isolevel[indexes[2]], - channel_isolevel[indexes[3]], - }; - if (use_lighting) { for (int i = 0; i < 4; ++i) { int indx = indexes[i]; @@ -179,23 +159,14 @@ void Terrain2DMesherBlocky::add_chunk_normal(Ref chunk) { surface->transform_uv_scaled(Terrain2DSurface::TERRAIN_2D_SIDE_TOP, Vector2(1, 1), x % get_texture_scale(), z % get_texture_scale(), get_texture_scale()) }; - Vector3 verts[] = { - Vector3(x + 1, isolevels[0] / 255.0 * world_height, z) * voxel_scale, - Vector3(x, isolevels[1] / 255.0 * world_height, z) * voxel_scale, - Vector3(x, isolevels[2] / 255.0 * world_height, z + 1) * voxel_scale, - Vector3(x + 1, isolevels[3] / 255.0 * world_height, z + 1) * voxel_scale - }; - - Vector3 normals[] = { - (verts[0] - verts[1]).cross(verts[0] - verts[2]).normalized(), - (verts[0] - verts[1]).cross(verts[1] - verts[2]).normalized(), - (verts[1] - verts[2]).cross(verts[2] - verts[0]).normalized(), - (verts[2] - verts[3]).cross(verts[3] - verts[0]).normalized(), + Vector2 verts[] = { + Vector2(x + 1, z) * voxel_scale, + Vector2(x, z) * voxel_scale, + Vector2(x, z + 1) * voxel_scale, + Vector2(x + 1, z + 1) * voxel_scale }; for (int i = 0; i < 4; ++i) { - add_normal(normals[i]); - if (use_lighting || _always_add_colors) add_color(light[i]); @@ -206,1017 +177,6 @@ void Terrain2DMesherBlocky::add_chunk_normal(Ref chunk) { } } -void Terrain2DMesherBlocky::add_chunk_lod(Ref chunk) { - //if ((get_build_flags() & Terrain2DChunkDefault::BUILD_FLAG_GENERATE_AO) != 0) - // if (!chunk->get_channel(Terrain2DChunkDefault::DEFAULT_CHANNEL_AO)) - // chunk->generate_ao(); - - create_margin_zmin(chunk); - create_margin_zmax(chunk); - create_margin_xmin(chunk); - create_margin_xmax(chunk); - create_margin_corners(chunk); - - int x_size = chunk->get_size_x(); - int z_size = chunk->get_size_z(); - float world_height = chunk->get_world_height(); - - float voxel_scale = get_voxel_scale(); - - uint8_t *channel_type = chunk->channel_get(_channel_index_type); - - if (!channel_type) - return; - - uint8_t *channel_isolevel = chunk->channel_get(_channel_index_isolevel); - - if (!channel_isolevel) - return; - - uint8_t *channel_color_r = NULL; - uint8_t *channel_color_g = NULL; - uint8_t *channel_color_b = NULL; - uint8_t *channel_ao = NULL; - uint8_t *channel_rao = NULL; - - Color base_light(_base_light_value, _base_light_value, _base_light_value); - Color light[4]{ Color(1, 1, 1), Color(1, 1, 1), Color(1, 1, 1), Color(1, 1, 1) }; - - bool use_lighting = (get_build_flags() & Terrain2DChunkDefault::BUILD_FLAG_USE_LIGHTING) != 0; - bool use_ao = (get_build_flags() & Terrain2DChunkDefault::BUILD_FLAG_USE_AO) != 0; - bool use_rao = (get_build_flags() & Terrain2DChunkDefault::BUILD_FLAG_USE_RAO) != 0; - - if (use_lighting) { - channel_color_r = chunk->channel_get_valid(Terrain2DChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_R); - channel_color_g = chunk->channel_get_valid(Terrain2DChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_G); - channel_color_b = chunk->channel_get_valid(Terrain2DChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_B); - - if (use_ao) - channel_ao = chunk->channel_get_valid(Terrain2DChunkDefault::DEFAULT_CHANNEL_AO); - - if (use_rao) - channel_rao = chunk->channel_get_valid(Terrain2DChunkDefault::DEFAULT_CHANNEL_RANDOM_AO); - } - - Ref mcache; - - if (chunk->material_cache_key_has()) { - mcache = _library->material_cache_get(chunk->material_cache_key_get()); - } - - //todo this should be calculated from size's factors - int lod_skip = _lod_index * 2; - //int margin_start = chunk->get_margin_start() ; - - int margin_start = chunk->get_margin_start(); - //z_size + margin_start is fine, x, and z are in data space. - for (int z = lod_skip; z < z_size + margin_start - lod_skip; z += lod_skip) { - for (int x = lod_skip; x < x_size + margin_start - lod_skip; x += lod_skip) { - int indexes[4] = { - chunk->get_data_index(x + lod_skip, z), - chunk->get_data_index(x, z), - chunk->get_data_index(x, z + lod_skip), - chunk->get_data_index(x + lod_skip, z + lod_skip) - }; - - uint8_t type = channel_type[indexes[0]]; - - if (type == 0) - continue; - - Ref surface; - - if (!mcache.is_valid()) { - surface = _library->terra_surface_get(type - 1); - } else { - surface = mcache->surface_id_get(type - 1); - } - - if (!surface.is_valid()) - continue; - - uint8_t isolevels[] = { - channel_isolevel[indexes[0]], - channel_isolevel[indexes[1]], - channel_isolevel[indexes[2]], - channel_isolevel[indexes[3]], - }; - - if (use_lighting) { - for (int i = 0; i < 4; ++i) { - int indx = indexes[i]; - - light[i] = Color(channel_color_r[indx] / 255.0, - channel_color_g[indx] / 255.0, - channel_color_b[indx] / 255.0); - - float ao = 0; - - if (use_ao) - ao = channel_ao[indx] / 255.0; - - if (use_rao) { - float rao = channel_rao[indx] / 255.0; - ao += rao; - } - - light[i] += base_light; - - if (ao > 0) - light[i] -= Color(ao, ao, ao) * _ao_strength; - - light[i].r = CLAMP(light[i].r, 0, 1.0); - light[i].g = CLAMP(light[i].g, 0, 1.0); - light[i].b = CLAMP(light[i].b, 0, 1.0); - } - } - - int vc = get_vertex_count(); - add_indices(vc + 2); - add_indices(vc + 1); - add_indices(vc + 0); - add_indices(vc + 3); - add_indices(vc + 2); - add_indices(vc + 0); - - Vector2 uvs[] = { - surface->transform_uv_scaled(Terrain2DSurface::TERRAIN_2D_SIDE_TOP, Vector2(1, 0), x % get_texture_scale(), z % get_texture_scale(), get_texture_scale()), - surface->transform_uv_scaled(Terrain2DSurface::TERRAIN_2D_SIDE_TOP, Vector2(0, 0), x % get_texture_scale(), z % get_texture_scale(), get_texture_scale()), - surface->transform_uv_scaled(Terrain2DSurface::TERRAIN_2D_SIDE_TOP, Vector2(0, 1), x % get_texture_scale(), z % get_texture_scale(), get_texture_scale()), - surface->transform_uv_scaled(Terrain2DSurface::TERRAIN_2D_SIDE_TOP, Vector2(1, 1), x % get_texture_scale(), z % get_texture_scale(), get_texture_scale()) - }; - - Vector3 verts[] = { - Vector3(x + lod_skip, isolevels[0] / 255.0 * world_height, z) * voxel_scale, - Vector3(x, isolevels[1] / 255.0 * world_height, z) * voxel_scale, - Vector3(x, isolevels[2] / 255.0 * world_height, z + lod_skip) * voxel_scale, - Vector3(x + lod_skip, isolevels[3] / 255.0 * world_height, z + lod_skip) * voxel_scale - }; - - Vector3 normals[] = { - (verts[0] - verts[1]).cross(verts[0] - verts[2]).normalized(), - (verts[0] - verts[1]).cross(verts[1] - verts[2]).normalized(), - (verts[1] - verts[2]).cross(verts[2] - verts[0]).normalized(), - (verts[2] - verts[3]).cross(verts[3] - verts[0]).normalized(), - }; - - for (int i = 0; i < 4; ++i) { - add_normal(normals[i]); - - if (use_lighting || _always_add_colors) - add_color(light[i]); - - add_uv(uvs[i]); - add_vertex(verts[i]); - } - } - } -} - -void Terrain2DMesherBlocky::create_margin_zmin(Ref chunk) { - //if ((get_build_flags() & Terrain2DChunkDefault::BUILD_FLAG_GENERATE_AO) != 0) - // if (!chunk->get_channel(Terrain2DChunkDefault::DEFAULT_CHANNEL_AO)) - // chunk->generate_ao(); - - int x_size = chunk->get_size_x(); - int x_data_size = chunk->get_data_size_x(); - float world_height = chunk->get_world_height(); - - float voxel_scale = get_voxel_scale(); - - uint8_t *channel_type = chunk->channel_get(_channel_index_type); - - if (!channel_type) - return; - - uint8_t *channel_isolevel = chunk->channel_get(_channel_index_isolevel); - - if (!channel_isolevel) - return; - - uint8_t *channel_color_r = NULL; - uint8_t *channel_color_g = NULL; - uint8_t *channel_color_b = NULL; - uint8_t *channel_ao = NULL; - uint8_t *channel_rao = NULL; - - Color base_light(_base_light_value, _base_light_value, _base_light_value); - Color light[4]{ Color(1, 1, 1), Color(1, 1, 1), Color(1, 1, 1), Color(1, 1, 1) }; - - bool use_lighting = (get_build_flags() & Terrain2DChunkDefault::BUILD_FLAG_USE_LIGHTING) != 0; - bool use_ao = (get_build_flags() & Terrain2DChunkDefault::BUILD_FLAG_USE_AO) != 0; - bool use_rao = (get_build_flags() & Terrain2DChunkDefault::BUILD_FLAG_USE_RAO) != 0; - - if (use_lighting) { - channel_color_r = chunk->channel_get_valid(Terrain2DChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_R); - channel_color_g = chunk->channel_get_valid(Terrain2DChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_G); - channel_color_b = chunk->channel_get_valid(Terrain2DChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_B); - - if (use_ao) - channel_ao = chunk->channel_get_valid(Terrain2DChunkDefault::DEFAULT_CHANNEL_AO); - - if (use_rao) - channel_rao = chunk->channel_get_valid(Terrain2DChunkDefault::DEFAULT_CHANNEL_RANDOM_AO); - } - - Ref mcache; - - if (chunk->material_cache_key_has()) { - mcache = _library->material_cache_get(chunk->material_cache_key_get()); - } - - int margin_start = chunk->get_margin_start(); - - int lod_skip = _lod_index * 2; - //z_size + margin_start is fine, x, and z are in data space. - int z = 1; - float oolod = 1.0 / static_cast(lod_skip); - - int lastz = lod_skip; - - for (int x = margin_start + 1; x < x_size + margin_start - 1; ++x) { - int prev_main_x = x - (x % lod_skip); - int next_main_x = prev_main_x + lod_skip; - next_main_x = CLAMP(next_main_x, 0, x_data_size); - - int indexes[4] = { - chunk->get_data_index(x + 1, z), //x + 1 - chunk->get_data_index(x, z), //x - //chunk->get_data_index(x + 1, z), //x + 1 - //chunk->get_data_index(x, z), //x - chunk->get_data_index(prev_main_x, lastz), - chunk->get_data_index(next_main_x, lastz), - }; - - uint8_t type = channel_type[indexes[0]]; - - if (type == 0) - continue; - - Ref surface; - - if (!mcache.is_valid()) { - surface = _library->terra_surface_get(type - 1); - } else { - surface = mcache->surface_id_get(type - 1); - } - - if (!surface.is_valid()) - continue; - - uint8_t isolevels[] = { - channel_isolevel[indexes[0]], - channel_isolevel[indexes[1]], - channel_isolevel[indexes[2]], - channel_isolevel[indexes[3]], - }; - - float x_interp = oolod * (x - prev_main_x); - float xp1_interp = 1.0 - (oolod * (next_main_x - (x + 1))); - - if (use_lighting) { - for (int i = 0; i < 4; ++i) { - int indx = indexes[i]; - - light[i] = Color(channel_color_r[indx] / 255.0, - channel_color_g[indx] / 255.0, - channel_color_b[indx] / 255.0); - - float ao = 0; - - if (use_ao) - ao = channel_ao[indx] / 255.0; - - if (use_rao) { - float rao = channel_rao[indx] / 255.0; - ao += rao; - } - - light[i] += base_light; - - if (ao > 0) - light[i] -= Color(ao, ao, ao) * _ao_strength; - - light[i].r = CLAMP(light[i].r, 0, 1.0); - light[i].g = CLAMP(light[i].g, 0, 1.0); - light[i].b = CLAMP(light[i].b, 0, 1.0); - } - } - - int vc = get_vertex_count(); - add_indices(vc + 2); - add_indices(vc + 1); - add_indices(vc + 0); - add_indices(vc + 3); - add_indices(vc + 2); - add_indices(vc + 0); - - Vector2 uvs[] = { - surface->transform_uv_scaled(Terrain2DSurface::TERRAIN_2D_SIDE_TOP, Vector2(1, 0), x % get_texture_scale(), z % get_texture_scale(), get_texture_scale()), - surface->transform_uv_scaled(Terrain2DSurface::TERRAIN_2D_SIDE_TOP, Vector2(0, 0), x % get_texture_scale(), z % get_texture_scale(), get_texture_scale()), - surface->transform_uv_scaled(Terrain2DSurface::TERRAIN_2D_SIDE_TOP, Vector2(0, 1), x % get_texture_scale(), z % get_texture_scale(), get_texture_scale()), - surface->transform_uv_scaled(Terrain2DSurface::TERRAIN_2D_SIDE_TOP, Vector2(1, 1), x % get_texture_scale(), z % get_texture_scale(), get_texture_scale()) - }; - - float vi0 = Math::lerp(isolevels[2], isolevels[3], x_interp); - float vi1 = Math::lerp(isolevels[2], isolevels[3], xp1_interp); - - Vector3 verts[] = { - Vector3(x + 1, isolevels[0] / 255.0 * world_height, z) * voxel_scale, - Vector3(x, isolevels[1] / 255.0 * world_height, z) * voxel_scale, - Vector3(x, vi0 / 255.0 * world_height, lastz) * voxel_scale, - Vector3(x + 1, vi1 / 255.0 * world_height, lastz) * voxel_scale - }; - - Vector3 normals[] = { - (verts[0] - verts[1]).cross(verts[0] - verts[2]).normalized(), - (verts[0] - verts[1]).cross(verts[1] - verts[2]).normalized(), - (verts[1] - verts[2]).cross(verts[2] - verts[0]).normalized(), - (verts[2] - verts[3]).cross(verts[3] - verts[0]).normalized(), - }; - - for (int i = 0; i < 4; ++i) { - add_normal(normals[i]); - - if (use_lighting || _always_add_colors) - add_color(light[i]); - - add_uv(uvs[i]); - add_vertex(verts[i]); - } - } -} - -void Terrain2DMesherBlocky::create_margin_zmax(Ref chunk) { - //if ((get_build_flags() & Terrain2DChunkDefault::BUILD_FLAG_GENERATE_AO) != 0) - // if (!chunk->get_channel(Terrain2DChunkDefault::DEFAULT_CHANNEL_AO)) - // chunk->generate_ao(); - - int x_size = chunk->get_size_x(); - int x_data_size = chunk->get_data_size_x(); - float world_height = chunk->get_world_height(); - - float voxel_scale = get_voxel_scale(); - - uint8_t *channel_type = chunk->channel_get(_channel_index_type); - - if (!channel_type) - return; - - uint8_t *channel_isolevel = chunk->channel_get(_channel_index_isolevel); - - if (!channel_isolevel) - return; - - uint8_t *channel_color_r = NULL; - uint8_t *channel_color_g = NULL; - uint8_t *channel_color_b = NULL; - uint8_t *channel_ao = NULL; - uint8_t *channel_rao = NULL; - - Color base_light(_base_light_value, _base_light_value, _base_light_value); - Color light[4]{ Color(1, 1, 1), Color(1, 1, 1), Color(1, 1, 1), Color(1, 1, 1) }; - - bool use_lighting = (get_build_flags() & Terrain2DChunkDefault::BUILD_FLAG_USE_LIGHTING) != 0; - bool use_ao = (get_build_flags() & Terrain2DChunkDefault::BUILD_FLAG_USE_AO) != 0; - bool use_rao = (get_build_flags() & Terrain2DChunkDefault::BUILD_FLAG_USE_RAO) != 0; - - if (use_lighting) { - channel_color_r = chunk->channel_get_valid(Terrain2DChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_R); - channel_color_g = chunk->channel_get_valid(Terrain2DChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_G); - channel_color_b = chunk->channel_get_valid(Terrain2DChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_B); - - if (use_ao) - channel_ao = chunk->channel_get_valid(Terrain2DChunkDefault::DEFAULT_CHANNEL_AO); - - if (use_rao) - channel_rao = chunk->channel_get_valid(Terrain2DChunkDefault::DEFAULT_CHANNEL_RANDOM_AO); - } - - Ref mcache; - - if (chunk->material_cache_key_has()) { - mcache = _library->material_cache_get(chunk->material_cache_key_get()); - } - - int margin_start = chunk->get_margin_start(); - - int lod_skip = _lod_index * 2; - //z_size + margin_start is fine, x, and z are in data space. - int z = chunk->get_size_z(); - float oolod = 1.0 / static_cast(lod_skip); - - for (int x = margin_start + 1; x < x_size + margin_start - 1; ++x) { - int prev_main_x = x - (x % lod_skip); - int next_main_x = prev_main_x + lod_skip; - next_main_x = CLAMP(next_main_x, 0, x_data_size); - - int indexes[4] = { - chunk->get_data_index(next_main_x, z), //x + 1 - chunk->get_data_index(prev_main_x, z), //x - //chunk->get_data_index(x + 1, z), //x + 1 - //chunk->get_data_index(x, z), //x - chunk->get_data_index(x, z + 1), - chunk->get_data_index(x + 1, z + 1), - }; - - uint8_t type = channel_type[indexes[0]]; - - if (type == 0) - continue; - - Ref surface; - - if (!mcache.is_valid()) { - surface = _library->terra_surface_get(type - 1); - } else { - surface = mcache->surface_id_get(type - 1); - } - - if (!surface.is_valid()) - continue; - - uint8_t isolevels[] = { - channel_isolevel[indexes[0]], - channel_isolevel[indexes[1]], - channel_isolevel[indexes[2]], - channel_isolevel[indexes[3]], - }; - - float x_interp = oolod * (x - prev_main_x); - float xp1_interp = 1.0 - (oolod * (next_main_x - (x + 1))); - - if (use_lighting) { - for (int i = 0; i < 4; ++i) { - int indx = indexes[i]; - - light[i] = Color(channel_color_r[indx] / 255.0, - channel_color_g[indx] / 255.0, - channel_color_b[indx] / 255.0); - - float ao = 0; - - if (use_ao) - ao = channel_ao[indx] / 255.0; - - if (use_rao) { - float rao = channel_rao[indx] / 255.0; - ao += rao; - } - - light[i] += base_light; - - if (ao > 0) - light[i] -= Color(ao, ao, ao) * _ao_strength; - - light[i].r = CLAMP(light[i].r, 0, 1.0); - light[i].g = CLAMP(light[i].g, 0, 1.0); - light[i].b = CLAMP(light[i].b, 0, 1.0); - } - } - - int vc = get_vertex_count(); - add_indices(vc + 2); - add_indices(vc + 1); - add_indices(vc + 0); - add_indices(vc + 3); - add_indices(vc + 2); - add_indices(vc + 0); - - Vector2 uvs[] = { - surface->transform_uv_scaled(Terrain2DSurface::TERRAIN_2D_SIDE_TOP, Vector2(1, 0), x % get_texture_scale(), z % get_texture_scale(), get_texture_scale()), - surface->transform_uv_scaled(Terrain2DSurface::TERRAIN_2D_SIDE_TOP, Vector2(0, 0), x % get_texture_scale(), z % get_texture_scale(), get_texture_scale()), - surface->transform_uv_scaled(Terrain2DSurface::TERRAIN_2D_SIDE_TOP, Vector2(0, 1), x % get_texture_scale(), z % get_texture_scale(), get_texture_scale()), - surface->transform_uv_scaled(Terrain2DSurface::TERRAIN_2D_SIDE_TOP, Vector2(1, 1), x % get_texture_scale(), z % get_texture_scale(), get_texture_scale()) - }; - - float vi0 = Math::lerp(isolevels[1], isolevels[0], x_interp); - float vi1 = Math::lerp(isolevels[1], isolevels[0], xp1_interp); - - Vector3 verts[] = { - Vector3(x + 1, vi1 / 255.0 * world_height, z) * voxel_scale, - Vector3(x, vi0 / 255.0 * world_height, z) * voxel_scale, - Vector3(x, isolevels[2] / 255.0 * world_height, z + 1) * voxel_scale, - Vector3(x + 1, isolevels[3] / 255.0 * world_height, z + 1) * voxel_scale - }; - - Vector3 normals[] = { - (verts[0] - verts[1]).cross(verts[0] - verts[2]).normalized(), - (verts[0] - verts[1]).cross(verts[1] - verts[2]).normalized(), - (verts[1] - verts[2]).cross(verts[2] - verts[0]).normalized(), - (verts[2] - verts[3]).cross(verts[3] - verts[0]).normalized(), - }; - - for (int i = 0; i < 4; ++i) { - add_normal(normals[i]); - - if (use_lighting || _always_add_colors) - add_color(light[i]); - - add_uv(uvs[i]); - add_vertex(verts[i]); - } - } -} - -void Terrain2DMesherBlocky::create_margin_xmin(Ref chunk) { - //if ((get_build_flags() & Terrain2DChunkDefault::BUILD_FLAG_GENERATE_AO) != 0) - // if (!chunk->get_channel(Terrain2DChunkDefault::DEFAULT_CHANNEL_AO)) - // chunk->generate_ao(); - - int z_size = chunk->get_size_x(); - int z_data_size = chunk->get_data_size_x(); - float world_height = chunk->get_world_height(); - - float voxel_scale = get_voxel_scale(); - - uint8_t *channel_type = chunk->channel_get(_channel_index_type); - - if (!channel_type) - return; - - uint8_t *channel_isolevel = chunk->channel_get(_channel_index_isolevel); - - if (!channel_isolevel) - return; - - uint8_t *channel_color_r = NULL; - uint8_t *channel_color_g = NULL; - uint8_t *channel_color_b = NULL; - uint8_t *channel_ao = NULL; - uint8_t *channel_rao = NULL; - - Color base_light(_base_light_value, _base_light_value, _base_light_value); - Color light[4]{ Color(1, 1, 1), Color(1, 1, 1), Color(1, 1, 1), Color(1, 1, 1) }; - - bool use_lighting = (get_build_flags() & Terrain2DChunkDefault::BUILD_FLAG_USE_LIGHTING) != 0; - bool use_ao = (get_build_flags() & Terrain2DChunkDefault::BUILD_FLAG_USE_AO) != 0; - bool use_rao = (get_build_flags() & Terrain2DChunkDefault::BUILD_FLAG_USE_RAO) != 0; - - if (use_lighting) { - channel_color_r = chunk->channel_get_valid(Terrain2DChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_R); - channel_color_g = chunk->channel_get_valid(Terrain2DChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_G); - channel_color_b = chunk->channel_get_valid(Terrain2DChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_B); - - if (use_ao) - channel_ao = chunk->channel_get_valid(Terrain2DChunkDefault::DEFAULT_CHANNEL_AO); - - if (use_rao) - channel_rao = chunk->channel_get_valid(Terrain2DChunkDefault::DEFAULT_CHANNEL_RANDOM_AO); - } - - Ref mcache; - - if (chunk->material_cache_key_has()) { - mcache = _library->material_cache_get(chunk->material_cache_key_get()); - } - - int margin_start = chunk->get_margin_start(); - - int lod_skip = _lod_index * 2; - //z_size + margin_start is fine, x, and z are in data space. - int x = 1; - float oolod = 1.0 / static_cast(lod_skip); - - int lastx = lod_skip; - - for (int z = margin_start + 1; z < z_size + margin_start - 1; ++z) { - int prev_main_z = z - (z % lod_skip); - int next_main_z = prev_main_z + lod_skip; - next_main_z = CLAMP(next_main_z, 0, z_data_size); - - int indexes[4] = { - chunk->get_data_index(lastx, prev_main_z), //x + 1 - chunk->get_data_index(x, z), //x - chunk->get_data_index(x, z + 1), - chunk->get_data_index(lastx, next_main_z), - }; - - uint8_t type = channel_type[indexes[0]]; - - if (type == 0) - continue; - - Ref surface; - - if (!mcache.is_valid()) { - surface = _library->terra_surface_get(type - 1); - } else { - surface = mcache->surface_id_get(type - 1); - } - - if (!surface.is_valid()) - continue; - - uint8_t isolevels[] = { - channel_isolevel[indexes[0]], - channel_isolevel[indexes[1]], - channel_isolevel[indexes[2]], - channel_isolevel[indexes[3]], - }; - - float z_interp = oolod * (z - prev_main_z); - float zp1_interp = 1.0 - (oolod * (next_main_z - (z + 1))); - - if (use_lighting) { - for (int i = 0; i < 4; ++i) { - int indx = indexes[i]; - - light[i] = Color(channel_color_r[indx] / 255.0, - channel_color_g[indx] / 255.0, - channel_color_b[indx] / 255.0); - - float ao = 0; - - if (use_ao) - ao = channel_ao[indx] / 255.0; - - if (use_rao) { - float rao = channel_rao[indx] / 255.0; - ao += rao; - } - - light[i] += base_light; - - if (ao > 0) - light[i] -= Color(ao, ao, ao) * _ao_strength; - - light[i].r = CLAMP(light[i].r, 0, 1.0); - light[i].g = CLAMP(light[i].g, 0, 1.0); - light[i].b = CLAMP(light[i].b, 0, 1.0); - } - } - - int vc = get_vertex_count(); - add_indices(vc + 2); - add_indices(vc + 1); - add_indices(vc + 0); - add_indices(vc + 3); - add_indices(vc + 2); - add_indices(vc + 0); - - Vector2 uvs[] = { - surface->transform_uv_scaled(Terrain2DSurface::TERRAIN_2D_SIDE_TOP, Vector2(1, 0), x % get_texture_scale(), z % get_texture_scale(), get_texture_scale()), - surface->transform_uv_scaled(Terrain2DSurface::TERRAIN_2D_SIDE_TOP, Vector2(0, 0), x % get_texture_scale(), z % get_texture_scale(), get_texture_scale()), - surface->transform_uv_scaled(Terrain2DSurface::TERRAIN_2D_SIDE_TOP, Vector2(0, 1), x % get_texture_scale(), z % get_texture_scale(), get_texture_scale()), - surface->transform_uv_scaled(Terrain2DSurface::TERRAIN_2D_SIDE_TOP, Vector2(1, 1), x % get_texture_scale(), z % get_texture_scale(), get_texture_scale()) - }; - - float vi0 = Math::lerp(isolevels[0], isolevels[3], z_interp); - float vi1 = Math::lerp(isolevels[0], isolevels[3], zp1_interp); - - Vector3 verts[] = { - Vector3(lastx, vi0 / 255.0 * world_height, z) * voxel_scale, - Vector3(x, isolevels[1] / 255.0 * world_height, z) * voxel_scale, - Vector3(x, isolevels[2] / 255.0 * world_height, z + 1) * voxel_scale, - Vector3(lastx, vi1 / 255.0 * world_height, z + 1) * voxel_scale - }; - - Vector3 normals[] = { - (verts[0] - verts[1]).cross(verts[0] - verts[2]).normalized(), - (verts[0] - verts[1]).cross(verts[1] - verts[2]).normalized(), - (verts[1] - verts[2]).cross(verts[2] - verts[0]).normalized(), - (verts[2] - verts[3]).cross(verts[3] - verts[0]).normalized(), - }; - - for (int i = 0; i < 4; ++i) { - add_normal(normals[i]); - - if (use_lighting || _always_add_colors) - add_color(light[i]); - - add_uv(uvs[i]); - add_vertex(verts[i]); - } - } -} - -void Terrain2DMesherBlocky::create_margin_xmax(Ref chunk) { - //if ((get_build_flags() & Terrain2DChunkDefault::BUILD_FLAG_GENERATE_AO) != 0) - // if (!chunk->get_channel(Terrain2DChunkDefault::DEFAULT_CHANNEL_AO)) - // chunk->generate_ao(); - - int z_size = chunk->get_size_x(); - int z_data_size = chunk->get_data_size_x(); - float world_height = chunk->get_world_height(); - - float voxel_scale = get_voxel_scale(); - - uint8_t *channel_type = chunk->channel_get(_channel_index_type); - - if (!channel_type) - return; - - uint8_t *channel_isolevel = chunk->channel_get(_channel_index_isolevel); - - if (!channel_isolevel) - return; - - uint8_t *channel_color_r = NULL; - uint8_t *channel_color_g = NULL; - uint8_t *channel_color_b = NULL; - uint8_t *channel_ao = NULL; - uint8_t *channel_rao = NULL; - - Color base_light(_base_light_value, _base_light_value, _base_light_value); - Color light[4]{ Color(1, 1, 1), Color(1, 1, 1), Color(1, 1, 1), Color(1, 1, 1) }; - - bool use_lighting = (get_build_flags() & Terrain2DChunkDefault::BUILD_FLAG_USE_LIGHTING) != 0; - bool use_ao = (get_build_flags() & Terrain2DChunkDefault::BUILD_FLAG_USE_AO) != 0; - bool use_rao = (get_build_flags() & Terrain2DChunkDefault::BUILD_FLAG_USE_RAO) != 0; - - if (use_lighting) { - channel_color_r = chunk->channel_get_valid(Terrain2DChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_R); - channel_color_g = chunk->channel_get_valid(Terrain2DChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_G); - channel_color_b = chunk->channel_get_valid(Terrain2DChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_B); - - if (use_ao) - channel_ao = chunk->channel_get_valid(Terrain2DChunkDefault::DEFAULT_CHANNEL_AO); - - if (use_rao) - channel_rao = chunk->channel_get_valid(Terrain2DChunkDefault::DEFAULT_CHANNEL_RANDOM_AO); - } - - Ref mcache; - - if (chunk->material_cache_key_has()) { - mcache = _library->material_cache_get(chunk->material_cache_key_get()); - } - - int margin_start = chunk->get_margin_start(); - - int lod_skip = _lod_index * 2; - //z_size + margin_start is fine, x, and z are in data space. - int x = chunk->get_size_x(); - float oolod = 1.0 / static_cast(lod_skip); - - for (int z = margin_start + 1; z < z_size + margin_start - 1; ++z) { - int prev_main_z = z - (z % lod_skip); - int next_main_z = prev_main_z + lod_skip; - next_main_z = CLAMP(next_main_z, 0, z_data_size); - - int indexes[4] = { - chunk->get_data_index(x + 1, z), //x + 1 - chunk->get_data_index(x, prev_main_z), //x - chunk->get_data_index(x, next_main_z), - chunk->get_data_index(x + 1, z + 1), - }; - - uint8_t type = channel_type[indexes[0]]; - - if (type == 0) - continue; - - Ref surface; - - if (!mcache.is_valid()) { - surface = _library->terra_surface_get(type - 1); - } else { - surface = mcache->surface_id_get(type - 1); - } - - if (!surface.is_valid()) - continue; - - uint8_t isolevels[] = { - channel_isolevel[indexes[0]], - channel_isolevel[indexes[1]], - channel_isolevel[indexes[2]], - channel_isolevel[indexes[3]], - }; - - float z_interp = oolod * (z - prev_main_z); - float zp1_interp = 1.0 - (oolod * (next_main_z - (z + 1))); - - if (use_lighting) { - for (int i = 0; i < 4; ++i) { - int indx = indexes[i]; - - light[i] = Color(channel_color_r[indx] / 255.0, - channel_color_g[indx] / 255.0, - channel_color_b[indx] / 255.0); - - float ao = 0; - - if (use_ao) - ao = channel_ao[indx] / 255.0; - - if (use_rao) { - float rao = channel_rao[indx] / 255.0; - ao += rao; - } - - light[i] += base_light; - - if (ao > 0) - light[i] -= Color(ao, ao, ao) * _ao_strength; - - light[i].r = CLAMP(light[i].r, 0, 1.0); - light[i].g = CLAMP(light[i].g, 0, 1.0); - light[i].b = CLAMP(light[i].b, 0, 1.0); - } - } - - int vc = get_vertex_count(); - add_indices(vc + 2); - add_indices(vc + 1); - add_indices(vc + 0); - add_indices(vc + 3); - add_indices(vc + 2); - add_indices(vc + 0); - - Vector2 uvs[] = { - surface->transform_uv_scaled(Terrain2DSurface::TERRAIN_2D_SIDE_TOP, Vector2(1, 0), x % get_texture_scale(), z % get_texture_scale(), get_texture_scale()), - surface->transform_uv_scaled(Terrain2DSurface::TERRAIN_2D_SIDE_TOP, Vector2(0, 0), x % get_texture_scale(), z % get_texture_scale(), get_texture_scale()), - surface->transform_uv_scaled(Terrain2DSurface::TERRAIN_2D_SIDE_TOP, Vector2(0, 1), x % get_texture_scale(), z % get_texture_scale(), get_texture_scale()), - surface->transform_uv_scaled(Terrain2DSurface::TERRAIN_2D_SIDE_TOP, Vector2(1, 1), x % get_texture_scale(), z % get_texture_scale(), get_texture_scale()) - }; - - float vi0 = Math::lerp(isolevels[1], isolevels[2], z_interp); - float vi1 = Math::lerp(isolevels[1], isolevels[2], zp1_interp); - - Vector3 verts[] = { - Vector3(x + 1, isolevels[0] / 255.0 * world_height, z) * voxel_scale, - Vector3(x, vi0 / 255.0 * world_height, z) * voxel_scale, - Vector3(x, vi1 / 255.0 * world_height, z + 1) * voxel_scale, - Vector3(x + 1, isolevels[3] / 255.0 * world_height, z + 1) * voxel_scale - }; - - Vector3 normals[] = { - (verts[0] - verts[1]).cross(verts[0] - verts[2]).normalized(), - (verts[0] - verts[1]).cross(verts[1] - verts[2]).normalized(), - (verts[1] - verts[2]).cross(verts[2] - verts[0]).normalized(), - (verts[2] - verts[3]).cross(verts[3] - verts[0]).normalized(), - }; - - for (int i = 0; i < 4; ++i) { - add_normal(normals[i]); - - if (use_lighting || _always_add_colors) - add_color(light[i]); - - add_uv(uvs[i]); - add_vertex(verts[i]); - } - } -} - -void Terrain2DMesherBlocky::create_margin_corners(Ref chunk) { - create_face(chunk, 1, 2, 1, 2); - create_face(chunk, 1, 2, chunk->get_size_z() + chunk->get_margin_start() - 1, chunk->get_size_z() + chunk->get_margin_start()); - create_face(chunk, chunk->get_size_x() + chunk->get_margin_start() - 1, chunk->get_size_x() + chunk->get_margin_start(), 1, 2); - create_face(chunk, chunk->get_size_x() + chunk->get_margin_start() - 1, chunk->get_size_x() + chunk->get_margin_start(), chunk->get_size_z() + chunk->get_margin_start() - 1, chunk->get_size_z() + chunk->get_margin_start()); -} - -void Terrain2DMesherBlocky::create_face(Ref chunk, int dataxmin, int dataxmax, int datazmin, int datazmax) { - //if ((get_build_flags() & Terrain2DChunkDefault::BUILD_FLAG_GENERATE_AO) != 0) - // if (!chunk->get_channel(Terrain2DChunkDefault::DEFAULT_CHANNEL_AO)) - // chunk->generate_ao(); - - float world_height = chunk->get_world_height(); - - float voxel_scale = get_voxel_scale(); - - uint8_t *channel_type = chunk->channel_get(_channel_index_type); - - if (!channel_type) - return; - - uint8_t *channel_isolevel = chunk->channel_get(_channel_index_isolevel); - - if (!channel_isolevel) - return; - - uint8_t *channel_color_r = NULL; - uint8_t *channel_color_g = NULL; - uint8_t *channel_color_b = NULL; - uint8_t *channel_ao = NULL; - uint8_t *channel_rao = NULL; - - Color base_light(_base_light_value, _base_light_value, _base_light_value); - Color light[4]{ Color(1, 1, 1), Color(1, 1, 1), Color(1, 1, 1), Color(1, 1, 1) }; - - bool use_lighting = (get_build_flags() & Terrain2DChunkDefault::BUILD_FLAG_USE_LIGHTING) != 0; - bool use_ao = (get_build_flags() & Terrain2DChunkDefault::BUILD_FLAG_USE_AO) != 0; - bool use_rao = (get_build_flags() & Terrain2DChunkDefault::BUILD_FLAG_USE_RAO) != 0; - - if (use_lighting) { - channel_color_r = chunk->channel_get_valid(Terrain2DChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_R); - channel_color_g = chunk->channel_get_valid(Terrain2DChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_G); - channel_color_b = chunk->channel_get_valid(Terrain2DChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_B); - - if (use_ao) - channel_ao = chunk->channel_get_valid(Terrain2DChunkDefault::DEFAULT_CHANNEL_AO); - - if (use_rao) - channel_rao = chunk->channel_get_valid(Terrain2DChunkDefault::DEFAULT_CHANNEL_RANDOM_AO); - } - - Ref mcache; - - if (chunk->material_cache_key_has()) { - mcache = _library->material_cache_get(chunk->material_cache_key_get()); - } - - int indexes[4] = { - chunk->get_data_index(dataxmax, datazmin), //x + 1 - chunk->get_data_index(dataxmin, datazmin), //x - chunk->get_data_index(dataxmin, datazmax), - chunk->get_data_index(dataxmax, datazmax), - }; - - uint8_t type = channel_type[indexes[0]]; - - if (type == 0) - return; - - Ref surface; - - if (!mcache.is_valid()) { - surface = _library->terra_surface_get(type - 1); - } else { - surface = mcache->surface_id_get(type - 1); - } - - if (!surface.is_valid()) - return; - - uint8_t isolevels[] = { - channel_isolevel[indexes[0]], - channel_isolevel[indexes[1]], - channel_isolevel[indexes[2]], - channel_isolevel[indexes[3]], - }; - - if (use_lighting) { - for (int i = 0; i < 4; ++i) { - int indx = indexes[i]; - - light[i] = Color(channel_color_r[indx] / 255.0, - channel_color_g[indx] / 255.0, - channel_color_b[indx] / 255.0); - - float ao = 0; - - if (use_ao) - ao = channel_ao[indx] / 255.0; - - if (use_rao) { - float rao = channel_rao[indx] / 255.0; - ao += rao; - } - - light[i] += base_light; - - if (ao > 0) - light[i] -= Color(ao, ao, ao) * _ao_strength; - - light[i].r = CLAMP(light[i].r, 0, 1.0); - light[i].g = CLAMP(light[i].g, 0, 1.0); - light[i].b = CLAMP(light[i].b, 0, 1.0); - } - } - - int vc = get_vertex_count(); - add_indices(vc + 2); - add_indices(vc + 1); - add_indices(vc + 0); - add_indices(vc + 3); - add_indices(vc + 2); - add_indices(vc + 0); - - Vector2 uvs[] = { - surface->transform_uv_scaled(Terrain2DSurface::TERRAIN_2D_SIDE_TOP, Vector2(1, 0), dataxmin % get_texture_scale(), datazmin % get_texture_scale(), get_texture_scale()), - surface->transform_uv_scaled(Terrain2DSurface::TERRAIN_2D_SIDE_TOP, Vector2(0, 0), dataxmin % get_texture_scale(), datazmin % get_texture_scale(), get_texture_scale()), - surface->transform_uv_scaled(Terrain2DSurface::TERRAIN_2D_SIDE_TOP, Vector2(0, 1), dataxmin % get_texture_scale(), datazmin % get_texture_scale(), get_texture_scale()), - surface->transform_uv_scaled(Terrain2DSurface::TERRAIN_2D_SIDE_TOP, Vector2(1, 1), dataxmin % get_texture_scale(), datazmin % get_texture_scale(), get_texture_scale()) - }; - - Vector3 verts[] = { - Vector3(dataxmax, isolevels[0] / 255.0 * world_height, datazmin) * voxel_scale, - Vector3(dataxmin, isolevels[1] / 255.0 * world_height, datazmin) * voxel_scale, - Vector3(dataxmin, isolevels[2] / 255.0 * world_height, datazmax) * voxel_scale, - Vector3(dataxmax, isolevels[3] / 255.0 * world_height, datazmax) * voxel_scale - }; - - Vector3 normals[] = { - (verts[0] - verts[1]).cross(verts[0] - verts[2]).normalized(), - (verts[0] - verts[1]).cross(verts[1] - verts[2]).normalized(), - (verts[1] - verts[2]).cross(verts[2] - verts[0]).normalized(), - (verts[2] - verts[3]).cross(verts[3] - verts[0]).normalized(), - }; - - for (int i = 0; i < 4; ++i) { - add_normal(normals[i]); - - if (use_lighting || _always_add_colors) - add_color(light[i]); - - add_uv(uvs[i]); - add_vertex(verts[i]); - } -} - Terrain2DMesherBlocky::Terrain2DMesherBlocky() { _always_add_colors = false; } diff --git a/meshers/blocky/terrain_2d_mesher_blocky.h b/meshers/blocky/terrain_2d_mesher_blocky.h index 2bc95d8..5e8e064 100644 --- a/meshers/blocky/terrain_2d_mesher_blocky.h +++ b/meshers/blocky/terrain_2d_mesher_blocky.h @@ -48,14 +48,6 @@ public: void add_chunk_normal(Ref chunk); - void add_chunk_lod(Ref chunk); - void create_margin_zmin(Ref chunk); - void create_margin_zmax(Ref chunk); - void create_margin_xmin(Ref chunk); - void create_margin_xmax(Ref chunk); - void create_margin_corners(Ref chunk); - void create_face(Ref chunk, int dataxmin, int dataxmax, int datazmin, int datazmax); - Terrain2DMesherBlocky(); ~Terrain2DMesherBlocky(); diff --git a/meshers/default/terrain_2d_mesher_default.cpp b/meshers/default/terrain_2d_mesher_default.cpp index 1dc480c..4461656 100644 --- a/meshers/default/terrain_2d_mesher_default.cpp +++ b/meshers/default/terrain_2d_mesher_default.cpp @@ -61,13 +61,13 @@ void Terrain2DMesherDefault::_bake_colors(Ref chunk) { for (int i = 0; i < _vertices.size(); ++i) { Vertex vertex = _vertices[i]; - Vector3 vert = vertex.vertex; + Vector2 vert = vertex.vertex; unsigned int x = (unsigned int)(vert.x / _voxel_scale); - unsigned int z = (unsigned int)(vert.z / _voxel_scale); + unsigned int y = (unsigned int)(vert.y / _voxel_scale); - if (chunk->validate_data_position(x, z)) { - int indx = chunk->get_data_index(x, z); + if (chunk->validate_data_position(x, y)) { + int indx = chunk->get_data_index(x, y); Color light = Color( channel_color_r[indx] / 255.0, @@ -122,19 +122,19 @@ void Terrain2DMesherDefault::_bake_liquid_colors(Ref chunk) { for (int i = 0; i < _vertices.size(); ++i) { Vertex vertex = _vertices[i]; - Vector3 vert = vertex.vertex; + Vector2 vert = vertex.vertex; //Is this needed? - if (vert.x < 0 || vert.y < 0 || vert.z < 0) { + if (vert.x < 0 || vert.y < 0) { continue; } unsigned int x = (unsigned int)(vert.x / _voxel_scale); //unsigned int y = (unsigned int)(vert.y / _voxel_scale); - unsigned int z = (unsigned int)(vert.z / _voxel_scale); + unsigned int y = (unsigned int)(vert.y / _voxel_scale); - if (chunk->validate_data_position(x, z)) { - int indx = chunk->get_data_index(x, z); + if (chunk->validate_data_position(x, y)) { + int indx = chunk->get_data_index(x, y); Color light = Color( channel_color_r[indx] / 255.0, diff --git a/meshers/terrain_2d_mesher.cpp b/meshers/terrain_2d_mesher.cpp index 308dae3..a32fd64 100644 --- a/meshers/terrain_2d_mesher.cpp +++ b/meshers/terrain_2d_mesher.cpp @@ -36,15 +36,6 @@ bool Terrain2DMesher::Vertex::operator==(const Vertex &p_vertex) const { if (uv != p_vertex.uv) return false; - if (uv2 != p_vertex.uv2) - return false; - - if (normal != p_vertex.normal) - return false; - - if (binormal != p_vertex.binormal) - return false; - if (color != p_vertex.color) return false; @@ -66,11 +57,7 @@ bool Terrain2DMesher::Vertex::operator==(const Vertex &p_vertex) const { uint32_t Terrain2DMesher::VertexHasher::hash(const Vertex &p_vtx) { uint32_t h = hash_djb2_buffer((const uint8_t *)&p_vtx.vertex, sizeof(real_t) * 3); - h = hash_djb2_buffer((const uint8_t *)&p_vtx.normal, sizeof(real_t) * 3, h); - h = hash_djb2_buffer((const uint8_t *)&p_vtx.binormal, sizeof(real_t) * 3, h); - h = hash_djb2_buffer((const uint8_t *)&p_vtx.tangent, sizeof(real_t) * 3, h); h = hash_djb2_buffer((const uint8_t *)&p_vtx.uv, sizeof(real_t) * 2, h); - h = hash_djb2_buffer((const uint8_t *)&p_vtx.uv2, sizeof(real_t) * 2, h); h = hash_djb2_buffer((const uint8_t *)&p_vtx.color, sizeof(real_t) * 4, h); h = hash_djb2_buffer((const uint8_t *)p_vtx.bones.ptr(), p_vtx.bones.size() * sizeof(int), h); h = hash_djb2_buffer((const uint8_t *)p_vtx.weights.ptr(), p_vtx.weights.size() * sizeof(float), h); @@ -84,13 +71,6 @@ void Terrain2DMesher::set_channel_index_type(const int value) { _channel_index_type = value; } -int Terrain2DMesher::get_channel_index_isolevel() const { - return _channel_index_isolevel; -} -void Terrain2DMesher::set_channel_index_isolevel(const int value) { - _channel_index_isolevel = value; -} - int Terrain2DMesher::get_mesher_index() const { return _mesher_index; } @@ -171,10 +151,10 @@ Array Terrain2DMesher::build_mesh() { } { - PoolVector array; + PoolVector array; array.resize(_vertices.size()); #if !GODOT4 - PoolVector::Write w = array.write(); + PoolVector::Write w = array.write(); #endif for (int i = 0; i < _vertices.size(); ++i) { @@ -192,31 +172,6 @@ Array Terrain2DMesher::build_mesh() { a[VisualServer::ARRAY_VERTEX] = array; } - if ((_format & VisualServer::ARRAY_FORMAT_NORMAL) == 0) { - generate_normals(); - } - - { - PoolVector array; - array.resize(_vertices.size()); -#if !GODOT4 - PoolVector::Write w = array.write(); -#endif - - for (int i = 0; i < _vertices.size(); ++i) { -#if !GODOT4 - w[i] = _vertices[i].normal; -#else - array.set(i, _vertices[i].normal); -#endif - } - -#if !GODOT4 - w.release(); -#endif - a[VisualServer::ARRAY_NORMAL] = array; - } - if ((_format & VisualServer::ARRAY_FORMAT_COLOR) != 0) { PoolVector array; array.resize(_vertices.size()); @@ -260,27 +215,6 @@ Array Terrain2DMesher::build_mesh() { a[VisualServer::ARRAY_TEX_UV] = array; } - if ((_format & VisualServer::ARRAY_FORMAT_TEX_UV2) != 0) { - PoolVector array; - array.resize(_vertices.size()); -#if !GODOT4 - PoolVector::Write w = array.write(); -#endif - - for (int i = 0; i < _vertices.size(); ++i) { -#if !GODOT4 - w[i] = _vertices[i].uv2; -#else - array.set(i, _vertices[i].uv2); -#endif - } - -#if !GODOT4 - w.release(); -#endif - a[VisualServer::ARRAY_TEX_UV2] = array; - } - if (_indices.size() > 0) { PoolVector array; array.resize(_indices.size()); @@ -323,38 +257,6 @@ void Terrain2DMesher::build_mesh_into(RID mesh) { VS::get_singleton()->mesh_surface_set_material(mesh, 0, _library->material_lod_get(0)->get_rid()); } -void Terrain2DMesher::generate_normals(bool p_flip) { - _format = _format | VisualServer::ARRAY_FORMAT_NORMAL; - - for (int i = 0; i < _indices.size(); i += 3) { - int i0 = _indices[i]; - int i1 = _indices[i + 1]; - int i2 = _indices[i + 2]; - - ERR_FAIL_INDEX(i0, _vertices.size()); - ERR_FAIL_INDEX(i1, _vertices.size()); - ERR_FAIL_INDEX(i2, _vertices.size()); - - Vertex v0 = _vertices.get(i0); - Vertex v1 = _vertices.get(i1); - Vertex v2 = _vertices.get(i2); - - Vector3 normal; - if (!p_flip) - normal = Plane(v0.vertex, v1.vertex, v2.vertex).normal; - else - normal = Plane(v2.vertex, v1.vertex, v0.vertex).normal; - - v0.normal = normal; - v1.normal = normal; - v2.normal = normal; - - _vertices.set(i0, v0); - _vertices.set(i1, v1); - _vertices.set(i2, v2); - } -} - void Terrain2DMesher::remove_doubles() { if (_vertices.size() == 0) return; @@ -458,12 +360,9 @@ void Terrain2DMesher::reset() { _indices.resize(0); _last_color = Color(); - _last_normal = Vector3(); _last_uv = Vector2(); - _last_uv2 = Vector2(); _last_bones.clear(); _last_weights.clear(); - _last_tangent = Plane(); } void Terrain2DMesher::add_chunk(Ref chunk) { @@ -475,19 +374,18 @@ void Terrain2DMesher::add_chunk(Ref chunk) { #ifdef MESH_DATA_RESOURCE_PRESENT void Terrain2DMesher::add_mesh_data_resource(Ref mesh, const Vector3 position, const Vector3 rotation, const Vector3 scale, const Rect2 uv_rect) { - Transform transform = Transform(Basis(rotation).scaled(scale), position); - - add_mesh_data_resource_transform(mesh, transform, uv_rect); + //Transform2D transform = Transform(Basis(rotation).scaled(scale), position); + //TODO + add_mesh_data_resource_transform(mesh, Transform2D(), uv_rect); } -void Terrain2DMesher::add_mesh_data_resource_transform(Ref mesh, const Transform transform, const Rect2 uv_rect) { +void Terrain2DMesher::add_mesh_data_resource_transform(Ref mesh, const Transform2D transform, const Rect2 uv_rect) { if (mesh->get_array().size() == 0) return; const Array &arr = mesh->get_array(); - PoolVector3Array vertices = arr[Mesh::ARRAY_VERTEX]; - PoolVector3Array normals = arr[Mesh::ARRAY_NORMAL]; + PoolVector2Array vertices = arr[Mesh::ARRAY_VERTEX]; PoolVector2Array uvs = arr[Mesh::ARRAY_TEX_UV]; PoolColorArray colors = arr[Mesh::ARRAY_COLOR]; PoolIntArray indices = arr[Mesh::ARRAY_INDEX]; @@ -498,10 +396,7 @@ void Terrain2DMesher::add_mesh_data_resource_transform(Ref mes int orig_vert_size = _vertices.size(); for (int i = 0; i < vertices.size(); ++i) { - if (normals.size() > 0) - add_normal(transform.basis.xform(normals[i])); - - if (normals.size() > 0) { + if (uvs.size() > 0) { Vector2 uv = uvs[i]; uv.x = uv_rect.size.width * uv.x + uv_rect.position.x; @@ -524,14 +419,13 @@ void Terrain2DMesher::add_mesh_data_resource_transform(Ref mes } } -void Terrain2DMesher::add_mesh_data_resource_transform_colored(Ref mesh, const Transform transform, const PoolColorArray &colors, const Rect2 uv_rect) { +void Terrain2DMesher::add_mesh_data_resource_transform_colored(Ref mesh, const Transform2D transform, const PoolColorArray &colors, const Rect2 uv_rect) { if (mesh->get_array().size() == 0) return; const Array &arr = mesh->get_array(); - PoolVector3Array vertices = arr[Mesh::ARRAY_VERTEX]; - PoolVector3Array normals = arr[Mesh::ARRAY_NORMAL]; + PoolVector2Array vertices = arr[Mesh::ARRAY_VERTEX]; PoolVector2Array uvs = arr[Mesh::ARRAY_TEX_UV]; PoolIntArray indices = arr[Mesh::ARRAY_INDEX]; @@ -541,10 +435,7 @@ void Terrain2DMesher::add_mesh_data_resource_transform_colored(Ref 0) - add_normal(transform.basis.xform(normals[i])); - - if (normals.size() > 0) { + if (uvs.size() > 0) { Vector2 uv = uvs[i]; uv.x = uv_rect.size.width * uv.x + uv_rect.position.x; @@ -605,8 +496,8 @@ void Terrain2DMesher::bake_liquid_colors(Ref chunk) { } } -PoolVector Terrain2DMesher::build_collider() const { - PoolVector face_points; +PoolVector Terrain2DMesher::build_collider() const { + PoolVector face_points; if (_vertices.size() == 0) return face_points; @@ -642,11 +533,10 @@ void Terrain2DMesher::bake_lights(MeshInstance *node, Vector for (int v = 0; v < _vertices.size(); ++v) { Vertex vertexv = _vertices.get(v); - Vector3 vet = vertexv.vertex; - Vector3 vertex = node->to_global(vet); - - //grab normal - Vector3 normal = vertexv.normal; + Vector2 vet = vertexv.vertex; + //TODO + //Vector2 vertex = node->to_global(vet); + Vector2 vertex = vet; Vector3 v_lightDiffuse; @@ -654,36 +544,19 @@ void Terrain2DMesher::bake_lights(MeshInstance *node, Vector for (int i = 0; i < lights.size(); ++i) { Ref light = lights.get(i); - Vector3 lightDir = light->get_world_position() - vertex; + Vector2 lightDir = light->get_world_position() - vertex; float dist2 = lightDir.dot(lightDir); //inverse sqrt lightDir *= (1.0 / sqrt(dist2)); - float NdotL = normal.dot(lightDir); - - if (NdotL > 1.0) { - NdotL = 1.0; - } else if (NdotL < 0.0) { - NdotL = 0.0; - } - Color cc = light->get_color(); Vector3 cv(cc.r, cc.g, cc.b); - Vector3 value = cv * (NdotL / (1.0 + dist2)); + Vector3 value = cv * (1.0 / (1.0 + dist2)); value *= light->get_size(); v_lightDiffuse += value; - - /* - float dist2 = Mathf.Clamp(Vector3.Distance(transformedLights[i], vertices), 0f, 15f); - dist2 /= 35f; - - Vector3 value = Vector3.one; - value *= ((float) lights[i].Strength) / 255f; - value *= (1 - dist2); - v_lightDiffuse += value;*/ } Color f = vertexv.color; @@ -722,8 +595,8 @@ void Terrain2DMesher::bake_lights(MeshInstance *node, Vector // } } -PoolVector Terrain2DMesher::get_vertices() const { - PoolVector arr; +PoolVector Terrain2DMesher::get_vertices() const { + PoolVector arr; arr.resize(_vertices.size()); for (int i = 0; i < _vertices.size(); ++i) { @@ -733,13 +606,13 @@ PoolVector Terrain2DMesher::get_vertices() const { return arr; } -void Terrain2DMesher::set_vertices(const PoolVector &values) { +void Terrain2DMesher::set_vertices(const PoolVector &values) { ERR_FAIL_COND(values.size() != _vertices.size()); for (int i = 0; i < _vertices.size(); ++i) { Vertex v = _vertices[i]; - v.normal = values[i]; + v.vertex = values[i]; _vertices.set(i, v); } @@ -749,23 +622,19 @@ int Terrain2DMesher::get_vertex_count() const { return _vertices.size(); } -void Terrain2DMesher::add_vertex(const Vector3 &vertex) { +void Terrain2DMesher::add_vertex(const Vector2 &vertex) { Vertex vtx; vtx.vertex = vertex; vtx.color = _last_color; - vtx.normal = _last_normal; vtx.uv = _last_uv; - vtx.uv2 = _last_uv2; // Todo? // vtx.weights = _last_weights; // vtx.bones = _last_bones; - // vtx.tangent = _last_tangent.normal; - // vtx.binormal = _last_normal.cross(_last_tangent.normal).normalized() * _last_tangent.d; _vertices.push_back(vtx); } -Vector3 Terrain2DMesher::get_vertex(const int idx) const { +Vector2 Terrain2DMesher::get_vertex(const int idx) const { return _vertices.get(idx).vertex; } @@ -773,37 +642,6 @@ void Terrain2DMesher::remove_vertex(const int idx) { _vertices.VREMOVE(idx); } -PoolVector Terrain2DMesher::get_normals() const { - PoolVector arr; - - arr.resize(_vertices.size()); - for (int i = 0; i < _vertices.size(); ++i) { - arr.set(i, _vertices.get(i).normal); - } - - return arr; -} - -void Terrain2DMesher::set_normals(const PoolVector &values) { - ERR_FAIL_COND(values.size() != _vertices.size()); - - for (int i = 0; i < _vertices.size(); ++i) { - Vertex v = _vertices[i]; - - v.normal = values[i]; - - _vertices.set(i, v); - } -} - -void Terrain2DMesher::add_normal(const Vector3 &normal) { - _last_normal = normal; -} - -Vector3 Terrain2DMesher::get_normal(int idx) const { - return _vertices.get(idx).normal; -} - PoolVector Terrain2DMesher::get_colors() const { PoolVector arr; @@ -866,37 +704,6 @@ Vector2 Terrain2DMesher::get_uv(const int idx) const { return _vertices.get(idx).uv; } -PoolVector Terrain2DMesher::get_uv2s() const { - PoolVector arr; - - arr.resize(_vertices.size()); - for (int i = 0; i < _vertices.size(); ++i) { - arr.set(i, _vertices.get(i).uv2); - } - - return arr; -} - -void Terrain2DMesher::set_uv2s(const PoolVector &values) { - ERR_FAIL_COND(values.size() != _vertices.size()); - - for (int i = 0; i < _vertices.size(); ++i) { - Vertex v = _vertices[i]; - - v.uv2 = values[i]; - - _vertices.set(i, v); - } -} - -void Terrain2DMesher::add_uv2(const Vector2 &uv) { - _last_uv2 = uv; -} - -Vector2 Terrain2DMesher::get_uv2(const int idx) const { - return _vertices.get(idx).uv2; -} - PoolVector Terrain2DMesher::get_indices() const { return _indices; } @@ -930,7 +737,6 @@ Terrain2DMesher::Terrain2DMesher(const Ref &library) { _base_light_value = 0.5; _uv_margin = Rect2(0, 0, 1, 1); _channel_index_type = 0; - _channel_index_isolevel = 0; _format = 0; _texture_scale = 1; @@ -944,7 +750,6 @@ Terrain2DMesher::Terrain2DMesher() { _uv_margin = Rect2(0, 0, 1, 1); _format = 0; _channel_index_type = 0; - _channel_index_isolevel = 0; _texture_scale = 1; _lod_index = 0; } @@ -970,10 +775,6 @@ void Terrain2DMesher::_bind_methods() { ClassDB::bind_method(D_METHOD("set_channel_index_type", "value"), &Terrain2DMesher::set_channel_index_type); ADD_PROPERTY(PropertyInfo(Variant::INT, "channel_index_type"), "set_channel_index_type", "get_channel_index_type"); - ClassDB::bind_method(D_METHOD("get_channel_index_isolevel"), &Terrain2DMesher::get_channel_index_isolevel); - ClassDB::bind_method(D_METHOD("set_channel_index_isolevel", "value"), &Terrain2DMesher::set_channel_index_isolevel); - ADD_PROPERTY(PropertyInfo(Variant::INT, "channel_index_isolevel"), "set_channel_index_isolevel", "get_channel_index_isolevel"); - ClassDB::bind_method(D_METHOD("get_mesher_index"), &Terrain2DMesher::get_mesher_index); ClassDB::bind_method(D_METHOD("set_mesher_index", "value"), &Terrain2DMesher::set_mesher_index); ADD_PROPERTY(PropertyInfo(Variant::INT, "mesher_index"), "set_mesher_index", "get_mesher_index"); @@ -1041,11 +842,6 @@ void Terrain2DMesher::_bind_methods() { ClassDB::bind_method(D_METHOD("remove_vertex", "idx"), &Terrain2DMesher::remove_vertex); ClassDB::bind_method(D_METHOD("add_vertex", "vertex"), &Terrain2DMesher::add_vertex); - ClassDB::bind_method(D_METHOD("get_normals"), &Terrain2DMesher::get_normals); - ClassDB::bind_method(D_METHOD("set_normals", "values"), &Terrain2DMesher::set_normals); - ClassDB::bind_method(D_METHOD("get_normal", "idx"), &Terrain2DMesher::get_normal); - ClassDB::bind_method(D_METHOD("add_normal", "normal"), &Terrain2DMesher::add_normal); - ClassDB::bind_method(D_METHOD("get_colors"), &Terrain2DMesher::get_colors); ClassDB::bind_method(D_METHOD("set_colors", "values"), &Terrain2DMesher::set_colors); ClassDB::bind_method(D_METHOD("get_color", "idx"), &Terrain2DMesher::get_color); @@ -1056,11 +852,6 @@ void Terrain2DMesher::_bind_methods() { ClassDB::bind_method(D_METHOD("get_uv", "idx"), &Terrain2DMesher::get_uv); ClassDB::bind_method(D_METHOD("add_uv", "uv"), &Terrain2DMesher::add_uv); - ClassDB::bind_method(D_METHOD("get_uv2s"), &Terrain2DMesher::get_uv2s); - ClassDB::bind_method(D_METHOD("set_uv2s", "values"), &Terrain2DMesher::set_uv2s); - ClassDB::bind_method(D_METHOD("get_uv2", "idx"), &Terrain2DMesher::get_uv2); - ClassDB::bind_method(D_METHOD("add_uv2", "uv"), &Terrain2DMesher::add_uv2); - ClassDB::bind_method(D_METHOD("get_indices"), &Terrain2DMesher::get_indices); ClassDB::bind_method(D_METHOD("set_indices", "values"), &Terrain2DMesher::set_indices); ClassDB::bind_method(D_METHOD("get_indices_count"), &Terrain2DMesher::get_indices_count); @@ -1076,8 +867,6 @@ void Terrain2DMesher::_bind_methods() { ClassDB::bind_method(D_METHOD("build_mesh_into", "mesh_rid"), &Terrain2DMesher::build_mesh_into); ClassDB::bind_method(D_METHOD("build_collider"), &Terrain2DMesher::build_collider); - ClassDB::bind_method(D_METHOD("generate_normals", "flip"), &Terrain2DMesher::generate_normals, DEFVAL(false)); - ClassDB::bind_method(D_METHOD("remove_doubles"), &Terrain2DMesher::remove_doubles); ClassDB::bind_method(D_METHOD("remove_doubles_hashed"), &Terrain2DMesher::remove_doubles_hashed); } diff --git a/meshers/terrain_2d_mesher.h b/meshers/terrain_2d_mesher.h index 1e71f83..b9b335a 100644 --- a/meshers/terrain_2d_mesher.h +++ b/meshers/terrain_2d_mesher.h @@ -69,13 +69,9 @@ public: const double PI = 3.141592653589793238463; struct Vertex { - Vector3 vertex; + Vector2 vertex; Color color; - Vector3 normal; // normal, binormal, tangent - Vector3 binormal; - Vector3 tangent; Vector2 uv; - Vector2 uv2; Vector bones; Vector weights; @@ -100,9 +96,6 @@ public: int get_channel_index_type() const; void set_channel_index_type(const int value); - int get_channel_index_isolevel() const; - void set_channel_index_isolevel(const int value); - int get_mesher_index() const; void set_mesher_index(const int value); @@ -139,8 +132,8 @@ public: #ifdef MESH_DATA_RESOURCE_PRESENT void add_mesh_data_resource(Ref mesh, const Vector3 position = Vector3(0, 0, 0), const Vector3 rotation = Vector3(0, 0, 0), const Vector3 scale = Vector3(1.0, 1.0, 1.0), const Rect2 uv_rect = Rect2(0, 0, 1, 1)); - void add_mesh_data_resource_transform(Ref mesh, const Transform transform, const Rect2 uv_rect = Rect2(0, 0, 1, 1)); - void add_mesh_data_resource_transform_colored(Ref mesh, const Transform transform, const PoolColorArray &colors, const Rect2 uv_rect = Rect2(0, 0, 1, 1)); + void add_mesh_data_resource_transform(Ref mesh, const Transform2D transform, const Rect2 uv_rect = Rect2(0, 0, 1, 1)); + void add_mesh_data_resource_transform_colored(Ref mesh, const Transform2D transform, const PoolColorArray &colors, const Rect2 uv_rect = Rect2(0, 0, 1, 1)); #endif void add_mesher(const Ref &mesher); @@ -149,28 +142,22 @@ public: void bake_colors(Ref chunk); void bake_liquid_colors(Ref chunk); - PoolVector build_collider() const; + PoolVector build_collider() const; void bake_lights(MeshInstance *node, Vector> &lights); Array build_mesh(); void build_mesh_into(RID mesh); - void generate_normals(bool p_flip = false); void remove_doubles(); void remove_doubles_hashed(); - PoolVector get_vertices() const; - void set_vertices(const PoolVector &values); + PoolVector get_vertices() const; + void set_vertices(const PoolVector &values); int get_vertex_count() const; - Vector3 get_vertex(const int idx) const; + Vector2 get_vertex(const int idx) const; void remove_vertex(const int idx); - void add_vertex(const Vector3 &vertex); - - PoolVector get_normals() const; - void set_normals(const PoolVector &values); - Vector3 get_normal(const int idx) const; - void add_normal(const Vector3 &normal); + void add_vertex(const Vector2 &vertex); PoolVector get_colors() const; void set_colors(const PoolVector &values); @@ -182,11 +169,6 @@ public: Vector2 get_uv(const int idx) const; void add_uv(const Vector2 &vector); - PoolVector get_uv2s() const; - void set_uv2s(const PoolVector &values); - Vector2 get_uv2(const int idx) const; - void add_uv2(const Vector2 &vector); - PoolVector get_indices() const; void set_indices(const PoolVector &values); int get_indices_count() const; @@ -209,7 +191,6 @@ protected: static void _bind_methods(); int _channel_index_type; - int _channel_index_isolevel; int _mesher_index; @@ -223,12 +204,9 @@ protected: PoolVector _indices; Color _last_color; - Vector3 _last_normal; Vector2 _last_uv; - Vector2 _last_uv2; Vector _last_bones; Vector _last_weights; - Plane _last_tangent; Ref _library; Ref _material; diff --git a/world/default/terrain_2d_chunk_default.cpp b/world/default/terrain_2d_chunk_default.cpp index a2c7e69..10607dc 100644 --- a/world/default/terrain_2d_chunk_default.cpp +++ b/world/default/terrain_2d_chunk_default.cpp @@ -367,6 +367,7 @@ void Terrain2DChunkDefault::meshes_free(const int mesh_index) { } void Terrain2DChunkDefault::colliders_create(const int mesh_index, const int layer_mask) { + /* ERR_FAIL_COND(_voxel_world == NULL); ERR_FAIL_COND(PhysicsServer::get_singleton()->is_flushing_queries()); //ERR_FAIL_COND(!get_voxel_world()->is_inside_tree()); @@ -400,8 +401,12 @@ void Terrain2DChunkDefault::colliders_create(const int mesh_index, const int lay m[MESH_TYPE_INDEX_SHAPE] = shape_rid; _rids[mesh_index] = m; + */ + +//todo } void Terrain2DChunkDefault::colliders_create_area(const int mesh_index, const int layer_mask) { + /* ERR_FAIL_COND(_voxel_world == NULL); ERR_FAIL_COND(PhysicsServer::get_singleton()->is_flushing_queries()); @@ -440,6 +445,7 @@ void Terrain2DChunkDefault::colliders_create_area(const int mesh_index, const in m[MESH_TYPE_INDEX_SHAPE] = shape_rid; _rids[mesh_index] = m; + */ } void Terrain2DChunkDefault::colliders_free(const int mesh_index) { @@ -452,13 +458,13 @@ void Terrain2DChunkDefault::colliders_free(const int mesh_index) { if (m.has(MESH_TYPE_INDEX_SHAPE)) { RID r = m[MESH_TYPE_INDEX_SHAPE]; - PhysicsServer::get_singleton()->free(r); + //PhysicsServer::get_singleton()->free(r); } if (m.has(MESH_TYPE_INDEX_BODY)) { RID r = m[MESH_TYPE_INDEX_BODY]; - PhysicsServer::get_singleton()->free(r); + //PhysicsServer::get_singleton()->free(r); } m.erase(MESH_TYPE_INDEX_SHAPE); @@ -502,20 +508,22 @@ void Terrain2DChunkDefault::update_transforms() { if (d.has(MESH_TYPE_INDEX_BODY)) { RID rid = d[MESH_TYPE_INDEX_BODY]; - if (rid != empty_rid) - PhysicsServer::get_singleton()->body_set_state(rid, PhysicsServer::BODY_STATE_TRANSFORM, t); + if (rid != empty_rid) { + //PhysicsServer::get_singleton()->body_set_state(rid, PhysicsServer::BODY_STATE_TRANSFORM, t); + } } if (d.has(MESH_TYPE_INDEX_AREA)) { RID rid = d[MESH_TYPE_INDEX_AREA]; - if (rid != empty_rid) - PhysicsServer::get_singleton()->area_set_shape_transform(rid, 0, t); + if (rid != empty_rid) { + //PhysicsServer::get_singleton()->area_set_shape_transform(rid, 0, t); + } } } for (int i = 0; i < collider_get_count(); ++i) { - PhysicsServer::get_singleton()->body_set_state(collider_get_body(i), PhysicsServer::BODY_STATE_TRANSFORM, get_transform() * collider_get_transform(i)); + //PhysicsServer::get_singleton()->body_set_state(collider_get_body(i), PhysicsServer::BODY_STATE_TRANSFORM, get_transform() * collider_get_transform(i)); } if (_debug_mesh_instance != RID()) { @@ -644,23 +652,23 @@ void Terrain2DChunkDefault::draw_debug_voxels(int max, Color color) { int64_t sx = static_cast(_size_x); int64_t sz = static_cast(_size_z); - for (int z = 0; z < sz; ++z) { - for (int x = 0; x < sx; ++x) { - int type = get_voxel(x, z, Terrain2DChunkDefault::DEFAULT_CHANNEL_TYPE); + for (int z = 0; z < sz; ++z) { + for (int x = 0; x < sx; ++x) { + int type = get_voxel(x, z, Terrain2DChunkDefault::DEFAULT_CHANNEL_TYPE); - if (type == 0) { - continue; - } + if (type == 0) { + continue; + } - draw_cross_voxels_fill(Vector3(x, 0, z), 1); + draw_cross_voxels_fill(Vector3(x, 0, z), 1); - ++a; + ++a; - if (a > max) { - break; - } + if (a > max) { + break; } } + } debug_mesh_send(); } @@ -678,7 +686,7 @@ void Terrain2DChunkDefault::draw_debug_voxel_lights() { Ref v = _lights[i]; int pos_x = v->get_world_position_x() - (_size_x * _position_x); - int pos_z = v->get_world_position_z() - (_size_z * _position_z); + int pos_z = v->get_world_position_y() - (_size_z * _position_z); draw_cross_voxels_fill(Vector3(pos_x, 0, pos_z), 1.0); } @@ -692,14 +700,15 @@ void Terrain2DChunkDefault::draw_debug_mdr_colliders() { } for (int i = 0; i < collider_get_count(); ++i) { - Ref shape = collider_get_shape(i); + Ref shape = collider_get_shape(i); if (!shape.is_valid()) continue; - Transform t = collider_get_transform(i); + Transform2D t = collider_get_transform(i); - shape->add_vertices_to_array(_debug_mesh_array, t); + //shape->add_vertices_to_array(_debug_mesh_array, t); + //draw } } @@ -759,13 +768,11 @@ void Terrain2DChunkDefault::_bake_light(Ref light) { int size = light->get_size(); int local_x = light->get_world_position_x() - (_position_x * _size_x); - int local_y = light->get_world_position_y(); - int local_z = light->get_world_position_z() - (_position_z * _size_z); + int local_z = light->get_world_position_y() - (_position_z * _size_z); ERR_FAIL_COND(size < 0); int64_t dsx = static_cast(_data_size_x); - int64_t dsy = static_cast(_world_height); int64_t dsz = static_cast(_data_size_z); uint8_t *channel_color_r = channel_get(Terrain2DChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_R); @@ -774,51 +781,45 @@ void Terrain2DChunkDefault::_bake_light(Ref light) { ERR_FAIL_COND(channel_color_r == NULL || channel_color_g == NULL || channel_color_b == NULL); - for (int y = local_y - size; y <= local_y + size; ++y) { - if (y < 0 || y >= dsy) + for (int z = local_z - size; z <= local_z + size; ++z) { + if (z < 0 || z >= dsz) continue; - for (int z = local_z - size; z <= local_z + size; ++z) { - if (z < 0 || z >= dsz) + for (int x = local_x - size; x <= local_x + size; ++x) { + if (x < 0 || x >= dsx) continue; - for (int x = local_x - size; x <= local_x + size; ++x) { - if (x < 0 || x >= dsx) - continue; + int lx = x - local_x; + int lz = z - local_z; - int lx = x - local_x; - int ly = y - local_y; - int lz = z - local_z; + float str = size - (((float)lx * lx + lz * lz)); + str /= size; - float str = size - (((float)lx * lx + ly * ly + lz * lz)); - str /= size; + if (str < 0) + continue; - if (str < 0) - continue; + int index = get_data_index(x, z); - int index = get_data_index(x, z); + int r = color.r * str * 255.0; + int g = color.g * str * 255.0; + int b = color.b * str * 255.0; - int r = color.r * str * 255.0; - int g = color.g * str * 255.0; - int b = color.b * str * 255.0; + r += channel_color_r[index]; + g += channel_color_g[index]; + b += channel_color_b[index]; - r += channel_color_r[index]; - g += channel_color_g[index]; - b += channel_color_b[index]; + if (r > 255) + r = 255; - if (r > 255) - r = 255; + if (g > 255) + g = 255; - if (g > 255) - g = 255; + if (b > 255) + b = 255; - if (b > 255) - b = 255; - - channel_color_r[index] = r; - channel_color_g[index] = g; - channel_color_b[index] = b; - } + channel_color_r[index] = r; + channel_color_g[index] = g; + channel_color_b[index] = b; } } } diff --git a/world/jobs/terrain_2d_prop_job.cpp b/world/jobs/terrain_2d_prop_job.cpp index 13fdee0..d4e175e 100644 --- a/world/jobs/terrain_2d_prop_job.cpp +++ b/world/jobs/terrain_2d_prop_job.cpp @@ -60,6 +60,8 @@ void Terrain2DProp2DJob::phase_physics_process() { chunk->colliders_clear(); #ifdef MESH_DATA_RESOURCE_PRESENT +//TODO +/* for (int i = 0; i < chunk->mesh_data_resource_get_count(); ++i) { Ref mdr = chunk->mesh_data_resource_get(i); @@ -95,6 +97,7 @@ void Terrain2DProp2DJob::phase_physics_process() { chunk->collider_add(transform, shape, shape->get_rid(), body); } } + */ #endif #if TOOLS_ENABLED @@ -147,6 +150,8 @@ void Terrain2DProp2DJob::phase_prop() { } if (should_do()) { + //TODO + /* if ((chunk->get_build_flags() & Terrain2DChunkDefault::BUILD_FLAG_USE_LIGHTING) != 0) { Terrain2DWorldDefault *world = Object::cast_to(chunk->get_voxel_world()); @@ -178,6 +183,7 @@ void Terrain2DProp2DJob::phase_prop() { } } } + */ } if (get_prop_mesher()->get_vertex_count() == 0) { diff --git a/world/jobs/terrain_2d_prop_job.h b/world/jobs/terrain_2d_prop_job.h index 82a7f9a..132933c 100644 --- a/world/jobs/terrain_2d_prop_job.h +++ b/world/jobs/terrain_2d_prop_job.h @@ -55,7 +55,7 @@ protected: Ref _prop_mesher; - PoolVector temp_arr_collider; + PoolVector temp_arr_collider; Array temp_mesh_arr; }; diff --git a/world/jobs/terrain_2d_terrain_job.h b/world/jobs/terrain_2d_terrain_job.h index 82f7760..df492d5 100644 --- a/world/jobs/terrain_2d_terrain_job.h +++ b/world/jobs/terrain_2d_terrain_job.h @@ -67,8 +67,8 @@ protected: Ref _mesher; Ref _liquid_mesher; - PoolVector temp_arr_collider; - PoolVector temp_arr_collider_liquid; + PoolVector temp_arr_collider; + PoolVector temp_arr_collider_liquid; Array temp_mesh_arr; }; diff --git a/world/terrain_2d_chunk.cpp b/world/terrain_2d_chunk.cpp index a144b74..ca489b6 100644 --- a/world/terrain_2d_chunk.cpp +++ b/world/terrain_2d_chunk.cpp @@ -720,7 +720,7 @@ void Terrain2DChunk::clear_baked_lights() { } #if PROPS_2D_PRESENT -void Terrain2DChunk::prop_add(const Transform &tarnsform, const Ref &prop) { +void Terrain2DChunk::prop_add(const Transform2D &tarnsform, const Ref &prop) { ERR_FAIL_COND(!prop.is_valid()); Prop2DDataStore s; @@ -734,8 +734,8 @@ Ref Terrain2DChunk::prop_get(int index) { return _props.get(index).prop; } -Transform Terrain2DChunk::prop_get_tarnsform(const int index) { - ERR_FAIL_INDEX_V(index, _props.size(), Transform()); +Transform2D Terrain2DChunk::prop_get_tarnsform(const int index) { + ERR_FAIL_INDEX_V(index, _props.size(), Transform2D()); return _props.get(index).transform; } @@ -753,47 +753,57 @@ void Terrain2DChunk::props_clear() { #endif #if MESH_DATA_RESOURCE_PRESENT -int Terrain2DChunk::mesh_data_resource_addv(const Vector3 &local_data_pos, const Ref &mesh, const Ref &texture, const Color &color, const bool apply_voxel_scale) { - ERR_FAIL_COND_V(!mesh.is_valid(), 0); +int Terrain2DChunk::mesh_data_resource_addv(const Vector2 &local_data_pos, const Ref &mesh, const Ref &texture, const Color &color, const bool apply_voxel_scale) { + //TODO + /* - int index = _mesh_data_resources.size(); + ERR_FAIL_COND_V(!mesh.is_valid(), 0); - MeshDataResourceEntry e; + int index = _mesh_data_resources.size(); - if (apply_voxel_scale) { - e.transform = Transform(Basis().scaled(Vector3(_voxel_scale, _voxel_scale, _voxel_scale))); - e.transform.origin = local_data_pos * _voxel_scale; - } else { - e.transform.origin = local_data_pos; - } + MeshDataResourceEntry e; - e.mesh = mesh; - e.texture = texture; - e.color = color; + if (apply_voxel_scale) { + e.transform = Transform(Basis().scaled(Vector3(_voxel_scale, _voxel_scale, _voxel_scale))); + e.transform.origin = local_data_pos * _voxel_scale; + } else { + e.transform.origin = local_data_pos; + } - AABB aabb = AABB(Vector3(), get_world_size()); - AABB mesh_aabb = e.transform.xform(mesh->get_aabb()); - e.is_inside = aabb.encloses(mesh_aabb); + e.mesh = mesh; + e.texture = texture; + e.color = color; -#if PROPS_2D_PRESENT - if (get_library().is_valid() && texture.is_valid()) { - e.uv_rect = get_library()->get_prop_uv_rect(texture); - } else { + AABB aabb = AABB(Vector3(), get_world_size()); + AABB mesh_aabb = e.transform.xform(mesh->get_aabb()); + e.is_inside = aabb.encloses(mesh_aabb); + + #if PROPS_2D_PRESENT + if (get_library().is_valid() && texture.is_valid()) { + e.uv_rect = get_library()->get_prop_uv_rect(texture); + } else { + e.uv_rect = Rect2(0, 0, 1, 1); + } + #else e.uv_rect = Rect2(0, 0, 1, 1); - } -#else - e.uv_rect = Rect2(0, 0, 1, 1); -#endif + #endif - _mesh_data_resources.push_back(e); + _mesh_data_resources.push_back(e); - if (has_method("_mesh_data_resource_added")) - call("_mesh_data_resource_added", index); + if (has_method("_mesh_data_resource_added")) + call("_mesh_data_resource_added", index); - return index; + return index; + + */ + + return 0; } -int Terrain2DChunk::mesh_data_resource_add(const Transform &local_transform, const Ref &mesh, const Ref &texture, const Color &color, const bool apply_voxel_scale) { +int Terrain2DChunk::mesh_data_resource_add(const Transform2D &local_transform, const Ref &mesh, const Ref &texture, const Color &color, const bool apply_voxel_scale) { + //TODO + + /* ERR_FAIL_COND_V(!mesh.is_valid(), 0); int index = _mesh_data_resources.size(); @@ -830,6 +840,9 @@ int Terrain2DChunk::mesh_data_resource_add(const Transform &local_transform, con call("_mesh_data_resource_added", index); return index; + */ + + return 0; } Ref Terrain2DChunk::mesh_data_resource_get(const int index) { @@ -875,12 +888,12 @@ void Terrain2DChunk::mesh_data_resource_set_uv_rect(const int index, const Rect2 _mesh_data_resources.write[index].uv_rect = uv_rect; } -Transform Terrain2DChunk::mesh_data_resource_get_transform(const int index) { - ERR_FAIL_INDEX_V(index, _mesh_data_resources.size(), Transform()); +Transform2D Terrain2DChunk::mesh_data_resource_get_transform(const int index) { + ERR_FAIL_INDEX_V(index, _mesh_data_resources.size(), Transform2D()); return _mesh_data_resources.write[index].transform; } -void Terrain2DChunk::mesh_data_resource_set_transform(const int index, const Transform &transform) { +void Terrain2DChunk::mesh_data_resource_set_transform(const int index, const Transform2D &transform) { ERR_FAIL_INDEX(index, _mesh_data_resources.size()); _mesh_data_resources.write[index].transform = transform; @@ -911,7 +924,7 @@ void Terrain2DChunk::mesh_data_resource_clear() { #endif -int Terrain2DChunk::collider_add(const Transform &local_transform, const Ref &shape, const RID &shape_rid, const RID &body) { +int Terrain2DChunk::collider_add(const Transform2D &local_transform, const Ref &shape, const RID &shape_rid, const RID &body) { ERR_FAIL_COND_V(!shape.is_valid() && shape_rid == RID(), 0); int index = _colliders.size(); @@ -927,24 +940,24 @@ int Terrain2DChunk::collider_add(const Transform &local_transform, const Ref Terrain2DChunk::collider_get_shape(const int index) { - ERR_FAIL_INDEX_V(index, _colliders.size(), Ref()); +Ref Terrain2DChunk::collider_get_shape(const int index) { + ERR_FAIL_INDEX_V(index, _colliders.size(), Ref()); return _colliders[index].shape; } -void Terrain2DChunk::collider_set_shape(const int index, const Ref &shape) { +void Terrain2DChunk::collider_set_shape(const int index, const Ref &shape) { ERR_FAIL_INDEX(index, _colliders.size()); _colliders.write[index].shape = shape; @@ -1130,7 +1143,7 @@ Terrain2DChunk::~Terrain2DChunk() { } for (int i = 0; i < _colliders.size(); ++i) { - PhysicsServer::get_singleton()->free(_colliders[i].body); + //PhysicsServer::get_singleton()->free(_colliders[i].body); } _colliders.clear(); diff --git a/world/terrain_2d_chunk.h b/world/terrain_2d_chunk.h index da06fdb..a188f5f 100644 --- a/world/terrain_2d_chunk.h +++ b/world/terrain_2d_chunk.h @@ -64,6 +64,8 @@ include_pool_vector #define Texture Texture2D #endif +#include "scene/resources/shape_2d.h" + #include "../library/terrain_2d_surface.h" #include "../library/terrain_2d_library.h" ; //hackfix for a clang format issue @@ -234,17 +236,17 @@ public: void clear_baked_lights(); #if PROPS_2D_PRESENT - void prop_add(const Transform &tarnsform, const Ref &prop); + void prop_add(const Transform2D &tarnsform, const Ref &prop); Ref prop_get(const int index); - Transform prop_get_tarnsform(const int index); + Transform2D prop_get_tarnsform(const int index); int prop_get_count() const; void prop_remove(const int index); void props_clear(); #endif #if MESH_DATA_RESOURCE_PRESENT - int mesh_data_resource_addv(const Vector3 &local_data_pos, const Ref &mesh, const Ref &texture = Ref(), const Color &color = Color(1, 1, 1, 1), const bool apply_voxel_scale = true); - int mesh_data_resource_add(const Transform &local_transform, const Ref &mesh, const Ref &texture = Ref(), const Color &color = Color(1, 1, 1, 1), const bool apply_voxel_scale = true); + int mesh_data_resource_addv(const Vector2 &local_data_pos, const Ref &mesh, const Ref &texture = Ref(), const Color &color = Color(1, 1, 1, 1), const bool apply_voxel_scale = true); + int mesh_data_resource_add(const Transform2D &local_transform, const Ref &mesh, const Ref &texture = Ref(), const Color &color = Color(1, 1, 1, 1), const bool apply_voxel_scale = true); Ref mesh_data_resource_get(const int index); void mesh_data_resource_set(const int index, const Ref &mesh); @@ -258,8 +260,8 @@ public: Rect2 mesh_data_resource_get_uv_rect(const int index); void mesh_data_resource_set_uv_rect(const int index, const Rect2 &uv_rect); - Transform mesh_data_resource_get_transform(const int index); - void mesh_data_resource_set_transform(const int index, const Transform &transform); + Transform2D mesh_data_resource_get_transform(const int index); + void mesh_data_resource_set_transform(const int index, const Transform2D &transform); bool mesh_data_resource_get_is_inside(const int index); void mesh_data_resource_set_is_inside(const int index, const bool inside); @@ -270,13 +272,13 @@ public: #endif //Colliders - int collider_add(const Transform &local_transform, const Ref &shape, const RID &shape_rid = RID(), const RID &body = RID()); + int collider_add(const Transform2D &local_transform, const Ref &shape, const RID &shape_rid = RID(), const RID &body = RID()); - Transform collider_get_transform(const int index); - void collider_set_transform(const int index, const Transform &transform); + Transform2D collider_get_transform(const int index); + void collider_set_transform(const int index, const Transform2D &transform); - Ref collider_get_shape(const int index); - void collider_set_shape(const int index, const Ref &shape); + Ref collider_get_shape(const int index); + void collider_set_shape(const int index, const Ref &shape); RID collider_get_shape_rid(const int index); void collider_set_shape_rid(const int index, const RID &rid); @@ -321,7 +323,7 @@ protected: protected: #if PROPS_2D_PRESENT struct Prop2DDataStore { - Transform transform; + Transform2D transform; Ref prop; }; #endif @@ -332,15 +334,15 @@ protected: Ref texture; Color color; Rect2 uv_rect; - Transform transform; + Transform2D transform; bool is_inside; }; #endif struct ColliderBody { - Transform transform; + Transform2D transform; RID body; - Ref shape; + Ref shape; RID shape_rid; }; diff --git a/world/terrain_2d_world.cpp b/world/terrain_2d_world.cpp index 3ea2816..23edc87 100644 --- a/world/terrain_2d_world.cpp +++ b/world/terrain_2d_world.cpp @@ -588,6 +588,8 @@ int Terrain2DWorld::generation_get_size() const { #if PROPS_2D_PRESENT void Terrain2DWorld::prop_add(Transform transform, const Ref &prop, const bool apply_voxel_scale) { + //TODO + /* ERR_FAIL_COND(!prop.is_valid()); if (apply_voxel_scale) { @@ -652,7 +654,7 @@ void Terrain2DWorld::prop_add(Transform transform, const Ref &prop, Ref light; light.INSTANCE(); - light->set_world_position(wp.x / get_voxel_scale(), wp.y / get_voxel_scale(), wp.z / get_voxel_scale()); + light->set_world_position(wp.x / get_voxel_scale(), wp.y / get_voxel_scale()); light->set_color(light_data->get_light_color()); light->set_size(light_data->get_light_size()); @@ -680,6 +682,8 @@ void Terrain2DWorld::prop_add(Transform transform, const Ref &prop, } #endif } + + */ } #endif