diff --git a/level_generator/terraman_level_generator_flat.cpp b/level_generator/terraman_level_generator_flat.cpp index 814897f..69b9606 100644 --- a/level_generator/terraman_level_generator_flat.cpp +++ b/level_generator/terraman_level_generator_flat.cpp @@ -39,6 +39,7 @@ void TerramanLevelGeneratorFlat::set_channel_map(const Dictionary &map) { } void TerramanLevelGeneratorFlat::_generate_chunk(Ref chunk) { + /* int dymin = chunk->get_position_y() * chunk->get_size_y(); int dymax = dymin + chunk->get_size_y() + chunk->get_margin_end(); @@ -80,6 +81,7 @@ void TerramanLevelGeneratorFlat::_generate_chunk(Ref chunk) { keyptr = _channel_map.next(keyptr); } + */ } TerramanLevelGeneratorFlat::TerramanLevelGeneratorFlat() { diff --git a/meshers/blocky/terra_mesher_blocky.cpp b/meshers/blocky/terra_mesher_blocky.cpp index 5904ca8..67c1487 100644 --- a/meshers/blocky/terra_mesher_blocky.cpp +++ b/meshers/blocky/terra_mesher_blocky.cpp @@ -41,7 +41,6 @@ void TerraMesherBlocky::_add_chunk(Ref p_chunk) { // chunk->generate_ao(); int x_size = chunk->get_size_x(); - int y_size = chunk->get_size_y(); int z_size = chunk->get_size_z(); float voxel_scale = get_voxel_scale(); @@ -86,407 +85,340 @@ void TerraMesherBlocky::_add_chunk(Ref p_chunk) { liquids.push_back(static_cast(i + 1)); } - for (int y = chunk->get_margin_start(); y < y_size + chunk->get_margin_start(); ++y) { - for (int z = chunk->get_margin_start(); z < z_size + chunk->get_margin_start(); ++z) { - for (int x = chunk->get_margin_start(); x < x_size + chunk->get_margin_start(); ++x) { + for (int z = chunk->get_margin_start(); z < z_size + chunk->get_margin_start(); ++z) { + for (int x = chunk->get_margin_start(); x < x_size + chunk->get_margin_start(); ++x) { - int index = chunk->get_data_index(x, y, z); - int indexxp = chunk->get_data_index(x + 1, y, z); - int indexxn = chunk->get_data_index(x - 1, y, z); - int indexyp = chunk->get_data_index(x, y + 1, z); - int indexyn = chunk->get_data_index(x, y - 1, z); - int indexzp = chunk->get_data_index(x, y, z + 1); - int indexzn = chunk->get_data_index(x, y, z - 1); + int index = chunk->get_data_index(x, z); + int indexxp = chunk->get_data_index(x + 1, z); + int indexxn = chunk->get_data_index(x - 1, z); + int indexzp = chunk->get_data_index(x, z + 1); + int indexzn = chunk->get_data_index(x, z - 1); - uint8_t type = channel_type[index]; + uint8_t type = channel_type[index]; - if (type == 0) - continue; + if (type == 0) + continue; - if (liquids.find(type) != -1) - continue; + if (liquids.find(type) != -1) + continue; - Ref surface = _library->voxel_surface_get(type - 1); + Ref surface = _library->voxel_surface_get(type - 1); - if (!surface.is_valid()) - continue; + if (!surface.is_valid()) + continue; - uint8_t neighbours[] = { - channel_type[indexxp], - channel_type[indexxn], - channel_type[indexyp], - channel_type[indexyn], - channel_type[indexzp], - channel_type[indexzn], + uint8_t neighbours[] = { + channel_type[indexxp], + channel_type[indexxn], + channel_type[indexzp], + channel_type[indexzn], + }; + + for (int i = 0; i < 6; ++i) { + if (liquids.find(neighbours[i]) != -1) { + neighbours[i] = 0; + } + } + + //x + 1 + if (neighbours[0] == 0) { + if (use_lighting) { + light = Color(channel_color_r[indexxp] / 255.0, + channel_color_g[indexxp] / 255.0, + channel_color_b[indexxp] / 255.0); + + float ao = 0; + + if (use_ao) + ao = channel_ao[indexxp] / 255.0; + + if (use_rao) { + float rao = channel_rao[indexxp] / 255.0; + ao += rao; + } + + light += base_light; + + if (ao > 0) + light -= Color(ao, ao, ao) * _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); + } + + 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(TerraSurface::TERRA_SIDE_SIDE, Vector2(0, 1), x % get_texture_scale(), z % get_texture_scale(), get_texture_scale()), + surface->transform_uv_scaled(TerraSurface::TERRA_SIDE_SIDE, Vector2(0, 0), x % get_texture_scale(), z % get_texture_scale(), get_texture_scale()), + surface->transform_uv_scaled(TerraSurface::TERRA_SIDE_SIDE, Vector2(1, 0), x % get_texture_scale(), z % get_texture_scale(), get_texture_scale()), + surface->transform_uv_scaled(TerraSurface::TERRA_SIDE_SIDE, Vector2(1, 1), x % get_texture_scale(), z % get_texture_scale(), get_texture_scale()) }; - for (int i = 0; i < 6; ++i) { - if (liquids.find(neighbours[i]) != -1) { - neighbours[i] = 0; - } + Vector3 verts[] = { + Vector3(1, 0, 0) * voxel_scale + Vector3(x - 1, -1, z - 1) * voxel_scale, + Vector3(1, 1, 0) * voxel_scale + Vector3(x - 1, -1, z - 1) * voxel_scale, + Vector3(1, 1, 1) * voxel_scale + Vector3(x - 1, -1, z - 1) * voxel_scale, + Vector3(1, 0, 1) * voxel_scale + Vector3(x - 1, -1, z - 1) * voxel_scale + }; + + for (int i = 0; i < 4; ++i) { + add_normal(Vector3(1, 0, 0)); + + if (use_lighting || _always_add_colors) + add_color(light); + + add_uv(uvs[i]); + add_vertex(verts[i]); } + } - //x + 1 - if (neighbours[0] == 0) { - if (use_lighting) { - light = Color(channel_color_r[indexxp] / 255.0, - channel_color_g[indexxp] / 255.0, - channel_color_b[indexxp] / 255.0); + //x - 1 + if (neighbours[1] == 0) { + if (use_lighting) { + light = Color(channel_color_r[indexxn] / 255.0, + channel_color_g[indexxn] / 255.0, + channel_color_b[indexxn] / 255.0); - float ao = 0; + float ao = 0; - if (use_ao) - ao = channel_ao[indexxp] / 255.0; + if (use_ao) + ao = channel_ao[indexxn] / 255.0; - if (use_rao) { - float rao = channel_rao[indexxp] / 255.0; - ao += rao; - } - - light += base_light; - - if (ao > 0) - light -= Color(ao, ao, ao) * _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); + if (use_rao) { + float rao = channel_rao[indexxn] / 255.0; + ao += rao; } - 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); + light += base_light; - Vector2 uvs[] = { - surface->transform_uv_scaled(TerraSurface::TERRA_SIDE_SIDE, Vector2(0, 1), x % get_texture_scale(), z % get_texture_scale(), get_texture_scale()), - surface->transform_uv_scaled(TerraSurface::TERRA_SIDE_SIDE, Vector2(0, 0), x % get_texture_scale(), z % get_texture_scale(), get_texture_scale()), - surface->transform_uv_scaled(TerraSurface::TERRA_SIDE_SIDE, Vector2(1, 0), x % get_texture_scale(), z % get_texture_scale(), get_texture_scale()), - surface->transform_uv_scaled(TerraSurface::TERRA_SIDE_SIDE, Vector2(1, 1), x % get_texture_scale(), z % get_texture_scale(), get_texture_scale()) - }; - - Vector3 verts[] = { - Vector3(1, 0, 0) * voxel_scale + Vector3(x - 1, y - 1, z - 1) * voxel_scale, - Vector3(1, 1, 0) * voxel_scale + Vector3(x - 1, y - 1, z - 1) * voxel_scale, - Vector3(1, 1, 1) * voxel_scale + Vector3(x - 1, y - 1, z - 1) * voxel_scale, - Vector3(1, 0, 1) * voxel_scale + Vector3(x - 1, y - 1, z - 1) * voxel_scale - }; - - for (int i = 0; i < 4; ++i) { - add_normal(Vector3(1, 0, 0)); - - if (use_lighting || _always_add_colors) - add_color(light); - - add_uv(uvs[i]); - add_vertex(verts[i]); - } - } - - //x - 1 - if (neighbours[1] == 0) { - if (use_lighting) { - light = Color(channel_color_r[indexxn] / 255.0, - channel_color_g[indexxn] / 255.0, - channel_color_b[indexxn] / 255.0); - - float ao = 0; - - if (use_ao) - ao = channel_ao[indexxn] / 255.0; - - if (use_rao) { - float rao = channel_rao[indexxn] / 255.0; - ao += rao; - } - - light += base_light; - - if (ao > 0) - light -= Color(ao, ao, ao) * _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); - } - - int vc = get_vertex_count(); - add_indices(vc + 0); - add_indices(vc + 1); - add_indices(vc + 2); - - add_indices(vc + 0); - add_indices(vc + 2); - add_indices(vc + 3); - - Vector2 uvs[] = { - surface->transform_uv_scaled(TerraSurface::TERRA_SIDE_SIDE, Vector2(0, 1), x % get_texture_scale(), z % get_texture_scale(), get_texture_scale()), - surface->transform_uv_scaled(TerraSurface::TERRA_SIDE_SIDE, Vector2(0, 0), x % get_texture_scale(), z % get_texture_scale(), get_texture_scale()), - surface->transform_uv_scaled(TerraSurface::TERRA_SIDE_SIDE, Vector2(1, 0), x % get_texture_scale(), z % get_texture_scale(), get_texture_scale()), - surface->transform_uv_scaled(TerraSurface::TERRA_SIDE_SIDE, Vector2(1, 1), x % get_texture_scale(), z % get_texture_scale(), get_texture_scale()) - }; - - Vector3 verts[] = { - Vector3(0, 0, 0) * voxel_scale + Vector3(x - 1, y - 1, z - 1) * voxel_scale, - Vector3(0, 1, 0) * voxel_scale + Vector3(x - 1, y - 1, z - 1) * voxel_scale, - Vector3(0, 1, 1) * voxel_scale + Vector3(x - 1, y - 1, z - 1) * voxel_scale, - Vector3(0, 0, 1) * voxel_scale + Vector3(x - 1, y - 1, z - 1) * voxel_scale - }; - - for (int i = 0; i < 4; ++i) { - add_normal(Vector3(-1, 0, 0)); - - if (use_lighting || _always_add_colors) - add_color(light); - - add_uv(uvs[i]); - add_vertex(verts[i]); - } - } - - //y + 1 - if (neighbours[2] == 0) { - if (use_lighting) { - light = Color(channel_color_r[indexyp] / 255.0, - channel_color_g[indexyp] / 255.0, - channel_color_b[indexyp] / 255.0); - - float ao = 0; - - if (use_ao) - ao = channel_ao[indexyp] / 255.0; - - if (use_rao) { - float rao = channel_rao[indexyp] / 255.0; - ao += rao; - } - - light += base_light; - - if (ao > 0) - light -= Color(ao, ao, ao) * _ao_strength; - } + if (ao > 0) + light -= Color(ao, ao, ao) * _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); - - 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(TerraSurface::TERRA_SIDE_SIDE, Vector2(0, 1), x % get_texture_scale(), z % get_texture_scale(), get_texture_scale()), - surface->transform_uv_scaled(TerraSurface::TERRA_SIDE_SIDE, Vector2(0, 0), x % get_texture_scale(), z % get_texture_scale(), get_texture_scale()), - surface->transform_uv_scaled(TerraSurface::TERRA_SIDE_SIDE, Vector2(1, 0), x % get_texture_scale(), z % get_texture_scale(), get_texture_scale()), - surface->transform_uv_scaled(TerraSurface::TERRA_SIDE_SIDE, Vector2(1, 1), x % get_texture_scale(), z % get_texture_scale(), get_texture_scale()) - }; - - Vector3 verts[] = { - Vector3(1, 1, 0) * voxel_scale + Vector3(x - 1, y - 1, z - 1) * voxel_scale, - Vector3(0, 1, 0) * voxel_scale + Vector3(x - 1, y - 1, z - 1) * voxel_scale, - Vector3(0, 1, 1) * voxel_scale + Vector3(x - 1, y - 1, z - 1) * voxel_scale, - Vector3(1, 1, 1) * voxel_scale + Vector3(x - 1, y - 1, z - 1) * voxel_scale - }; - - for (int i = 0; i < 4; ++i) { - add_normal(Vector3(0, 1, 0)); - - if (use_lighting || _always_add_colors) - add_color(light); - - add_uv(uvs[i]); - add_vertex(verts[i]); - } } - //y - 1 - if (neighbours[3] == 0) { - if (use_lighting) { - light = Color(channel_color_r[indexyn] / 255.0, - channel_color_g[indexyn] / 255.0, - channel_color_b[indexyn] / 255.0); + int vc = get_vertex_count(); + add_indices(vc + 0); + add_indices(vc + 1); + add_indices(vc + 2); - float ao = 0; + add_indices(vc + 0); + add_indices(vc + 2); + add_indices(vc + 3); - if (use_ao) - ao = channel_ao[indexyn] / 255.0; + Vector2 uvs[] = { + surface->transform_uv_scaled(TerraSurface::TERRA_SIDE_SIDE, Vector2(0, 1), x % get_texture_scale(), z % get_texture_scale(), get_texture_scale()), + surface->transform_uv_scaled(TerraSurface::TERRA_SIDE_SIDE, Vector2(0, 0), x % get_texture_scale(), z % get_texture_scale(), get_texture_scale()), + surface->transform_uv_scaled(TerraSurface::TERRA_SIDE_SIDE, Vector2(1, 0), x % get_texture_scale(), z % get_texture_scale(), get_texture_scale()), + surface->transform_uv_scaled(TerraSurface::TERRA_SIDE_SIDE, Vector2(1, 1), x % get_texture_scale(), z % get_texture_scale(), get_texture_scale()) + }; - if (use_rao) { - float rao = channel_rao[indexyn] / 255.0; - ao += rao; - } + Vector3 verts[] = { + Vector3(0, 0, 0) * voxel_scale + Vector3(x - 1, -1, z - 1) * voxel_scale, + Vector3(0, 1, 0) * voxel_scale + Vector3(x - 1, -1, z - 1) * voxel_scale, + Vector3(0, 1, 1) * voxel_scale + Vector3(x - 1, -1, z - 1) * voxel_scale, + Vector3(0, 0, 1) * voxel_scale + Vector3(x - 1, -1, z - 1) * voxel_scale + }; - light += base_light; + for (int i = 0; i < 4; ++i) { + add_normal(Vector3(-1, 0, 0)); - if (ao > 0) - light -= Color(ao, ao, ao) * _ao_strength; + if (use_lighting || _always_add_colors) + add_color(light); + + add_uv(uvs[i]); + add_vertex(verts[i]); + } + } + /* + //y + 1 + if (neighbours[2] == 0) { + if (use_lighting) { + light = Color(channel_color_r[indexyp] / 255.0, + channel_color_g[indexyp] / 255.0, + channel_color_b[indexyp] / 255.0); + + float ao = 0; + + if (use_ao) + ao = channel_ao[indexyp] / 255.0; + + if (use_rao) { + float rao = channel_rao[indexyp] / 255.0; + ao += rao; } + light += base_light; + + if (ao > 0) + light -= Color(ao, ao, ao) * _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); + + 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(TerraSurface::TERRA_SIDE_SIDE, Vector2(0, 1), x % get_texture_scale(), z % get_texture_scale(), get_texture_scale()), + surface->transform_uv_scaled(TerraSurface::TERRA_SIDE_SIDE, Vector2(0, 0), x % get_texture_scale(), z % get_texture_scale(), get_texture_scale()), + surface->transform_uv_scaled(TerraSurface::TERRA_SIDE_SIDE, Vector2(1, 0), x % get_texture_scale(), z % get_texture_scale(), get_texture_scale()), + surface->transform_uv_scaled(TerraSurface::TERRA_SIDE_SIDE, Vector2(1, 1), x % get_texture_scale(), z % get_texture_scale(), get_texture_scale()) + }; + + Vector3 verts[] = { + Vector3(1, 1, 0) * voxel_scale + Vector3(x - 1, y - 1, z - 1) * voxel_scale, + Vector3(0, 1, 0) * voxel_scale + Vector3(x - 1, y - 1, z - 1) * voxel_scale, + Vector3(0, 1, 1) * voxel_scale + Vector3(x - 1, y - 1, z - 1) * voxel_scale, + Vector3(1, 1, 1) * voxel_scale + Vector3(x - 1, y - 1, z - 1) * voxel_scale + }; + + for (int i = 0; i < 4; ++i) { + add_normal(Vector3(0, 1, 0)); + + if (use_lighting || _always_add_colors) + add_color(light); + + add_uv(uvs[i]); + add_vertex(verts[i]); + } + } +*/ + //z + 1 + if (neighbours[4] == 0) { + if (use_lighting) { + light = Color(channel_color_r[indexzp] / 255.0, + channel_color_g[indexzp] / 255.0, + channel_color_b[indexzp] / 255.0); + + float ao = 0; + + if (use_ao) + ao = channel_ao[indexzp] / 255.0; + + if (use_rao) { + float rao = channel_rao[indexzp] / 255.0; + ao += rao; + } + + light += base_light; + + if (ao > 0) + light -= Color(ao, ao, ao) * _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); - - int vc = get_vertex_count(); - add_indices(vc + 0); - add_indices(vc + 1); - add_indices(vc + 2); - - add_indices(vc + 0); - add_indices(vc + 2); - add_indices(vc + 3); - - Vector2 uvs[] = { - surface->transform_uv_scaled(TerraSurface::TERRA_SIDE_SIDE, Vector2(0, 1), x % get_texture_scale(), z % get_texture_scale(), get_texture_scale()), - surface->transform_uv_scaled(TerraSurface::TERRA_SIDE_SIDE, Vector2(0, 0), x % get_texture_scale(), z % get_texture_scale(), get_texture_scale()), - surface->transform_uv_scaled(TerraSurface::TERRA_SIDE_SIDE, Vector2(1, 0), x % get_texture_scale(), z % get_texture_scale(), get_texture_scale()), - surface->transform_uv_scaled(TerraSurface::TERRA_SIDE_SIDE, Vector2(1, 1), x % get_texture_scale(), z % get_texture_scale(), get_texture_scale()) - }; - - Vector3 verts[] = { - Vector3(1, 0, 0) * voxel_scale + Vector3(x - 1, y - 1, z - 1) * voxel_scale, - Vector3(0, 0, 0) * voxel_scale + Vector3(x - 1, y - 1, z - 1) * voxel_scale, - Vector3(0, 0, 1) * voxel_scale + Vector3(x - 1, y - 1, z - 1) * voxel_scale, - Vector3(1, 0, 1) * voxel_scale + Vector3(x - 1, y - 1, z - 1) * voxel_scale - }; - - for (int i = 0; i < 4; ++i) { - add_normal(Vector3(0, -1, 0)); - - if (use_lighting || _always_add_colors) - add_color(light); - - add_uv(uvs[i]); - add_vertex(verts[i]); - } } - //z + 1 - if (neighbours[4] == 0) { - if (use_lighting) { - light = Color(channel_color_r[indexzp] / 255.0, - channel_color_g[indexzp] / 255.0, - channel_color_b[indexzp] / 255.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); - float ao = 0; + Vector2 uvs[] = { + surface->transform_uv_scaled(TerraSurface::TERRA_SIDE_SIDE, Vector2(0, 1), x % get_texture_scale(), z % get_texture_scale(), get_texture_scale()), + surface->transform_uv_scaled(TerraSurface::TERRA_SIDE_SIDE, Vector2(0, 0), x % get_texture_scale(), z % get_texture_scale(), get_texture_scale()), + surface->transform_uv_scaled(TerraSurface::TERRA_SIDE_SIDE, Vector2(1, 0), x % get_texture_scale(), z % get_texture_scale(), get_texture_scale()), + surface->transform_uv_scaled(TerraSurface::TERRA_SIDE_SIDE, Vector2(1, 1), x % get_texture_scale(), z % get_texture_scale(), get_texture_scale()) + }; - if (use_ao) - ao = channel_ao[indexzp] / 255.0; + Vector3 verts[] = { + Vector3(1, 0, 1) * voxel_scale + Vector3(x - 1, -1, z - 1) * voxel_scale, + Vector3(1, 1, 1) * voxel_scale + Vector3(x - 1, -1, z - 1) * voxel_scale, + Vector3(0, 1, 1) * voxel_scale + Vector3(x - 1, -1, z - 1) * voxel_scale, + Vector3(0, 0, 1) * voxel_scale + Vector3(x - 1, -1, z - 1) * voxel_scale + }; - if (use_rao) { - float rao = channel_rao[indexzp] / 255.0; - ao += rao; - } + for (int i = 0; i < 4; ++i) { + add_normal(Vector3(0, 0, 1)); - light += base_light; + if (use_lighting || _always_add_colors) + add_color(light); - if (ao > 0) - light -= Color(ao, ao, ao) * _ao_strength; + add_uv(uvs[i]); + add_vertex(verts[i]); + } + } - light.r = CLAMP(light.r, 0, 1.0); - light.g = CLAMP(light.g, 0, 1.0); - light.b = CLAMP(light.b, 0, 1.0); + //z - 1 + if (neighbours[5] == 0) { + if (use_lighting) { + light = Color(channel_color_r[indexzn] / 255.0, + channel_color_g[indexzn] / 255.0, + channel_color_b[indexzn] / 255.0); + + float ao = 0; + + if (use_ao) + ao = channel_ao[indexzn] / 255.0; + + if (use_rao) { + float rao = channel_rao[indexzn] / 255.0; + ao += rao; } - 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); + light += base_light; - Vector2 uvs[] = { - surface->transform_uv_scaled(TerraSurface::TERRA_SIDE_SIDE, Vector2(0, 1), x % get_texture_scale(), z % get_texture_scale(), get_texture_scale()), - surface->transform_uv_scaled(TerraSurface::TERRA_SIDE_SIDE, Vector2(0, 0), x % get_texture_scale(), z % get_texture_scale(), get_texture_scale()), - surface->transform_uv_scaled(TerraSurface::TERRA_SIDE_SIDE, Vector2(1, 0), x % get_texture_scale(), z % get_texture_scale(), get_texture_scale()), - surface->transform_uv_scaled(TerraSurface::TERRA_SIDE_SIDE, Vector2(1, 1), x % get_texture_scale(), z % get_texture_scale(), get_texture_scale()) - }; + if (ao > 0) + light -= Color(ao, ao, ao) * _ao_strength; - Vector3 verts[] = { - Vector3(1, 0, 1) * voxel_scale + Vector3(x - 1, y - 1, z - 1) * voxel_scale, - Vector3(1, 1, 1) * voxel_scale + Vector3(x - 1, y - 1, z - 1) * voxel_scale, - Vector3(0, 1, 1) * voxel_scale + Vector3(x - 1, y - 1, z - 1) * voxel_scale, - Vector3(0, 0, 1) * voxel_scale + Vector3(x - 1, y - 1, z - 1) * voxel_scale - }; - - for (int i = 0; i < 4; ++i) { - add_normal(Vector3(0, 0, 1)); - - if (use_lighting || _always_add_colors) - add_color(light); - - add_uv(uvs[i]); - add_vertex(verts[i]); - } + light.r = CLAMP(light.r, 0, 1.0); + light.g = CLAMP(light.g, 0, 1.0); + light.b = CLAMP(light.b, 0, 1.0); } - //z - 1 - if (neighbours[5] == 0) { - if (use_lighting) { - light = Color(channel_color_r[indexzn] / 255.0, - channel_color_g[indexzn] / 255.0, - channel_color_b[indexzn] / 255.0); + int vc = get_vertex_count(); + add_indices(vc + 0); + add_indices(vc + 1); + add_indices(vc + 2); - float ao = 0; + add_indices(vc + 0); + add_indices(vc + 2); + add_indices(vc + 3); - if (use_ao) - ao = channel_ao[indexzn] / 255.0; + Vector2 uvs[] = { + surface->transform_uv_scaled(TerraSurface::TERRA_SIDE_SIDE, Vector2(0, 1), x % get_texture_scale(), z % get_texture_scale(), get_texture_scale()), + surface->transform_uv_scaled(TerraSurface::TERRA_SIDE_SIDE, Vector2(0, 0), x % get_texture_scale(), z % get_texture_scale(), get_texture_scale()), + surface->transform_uv_scaled(TerraSurface::TERRA_SIDE_SIDE, Vector2(1, 0), x % get_texture_scale(), z % get_texture_scale(), get_texture_scale()), + surface->transform_uv_scaled(TerraSurface::TERRA_SIDE_SIDE, Vector2(1, 1), x % get_texture_scale(), z % get_texture_scale(), get_texture_scale()) + }; - if (use_rao) { - float rao = channel_rao[indexzn] / 255.0; - ao += rao; - } + Vector3 verts[] = { + Vector3(1, 0, 0) * voxel_scale + Vector3(x - 1, -1, z - 1) * voxel_scale, + Vector3(1, 1, 0) * voxel_scale + Vector3(x - 1, -1, z - 1) * voxel_scale, + Vector3(0, 1, 0) * voxel_scale + Vector3(x - 1, -1, z - 1) * voxel_scale, + Vector3(0, 0, 0) * voxel_scale + Vector3(x - 1, -1, z - 1) * voxel_scale + }; - light += base_light; + for (int i = 0; i < 4; ++i) { + add_normal(Vector3(0, 0, -1)); - if (ao > 0) - light -= Color(ao, ao, ao) * _ao_strength; + if (use_lighting || _always_add_colors) + add_color(light); - light.r = CLAMP(light.r, 0, 1.0); - light.g = CLAMP(light.g, 0, 1.0); - light.b = CLAMP(light.b, 0, 1.0); - } - - int vc = get_vertex_count(); - add_indices(vc + 0); - add_indices(vc + 1); - add_indices(vc + 2); - - add_indices(vc + 0); - add_indices(vc + 2); - add_indices(vc + 3); - - Vector2 uvs[] = { - surface->transform_uv_scaled(TerraSurface::TERRA_SIDE_SIDE, Vector2(0, 1), x % get_texture_scale(), z % get_texture_scale(), get_texture_scale()), - surface->transform_uv_scaled(TerraSurface::TERRA_SIDE_SIDE, Vector2(0, 0), x % get_texture_scale(), z % get_texture_scale(), get_texture_scale()), - surface->transform_uv_scaled(TerraSurface::TERRA_SIDE_SIDE, Vector2(1, 0), x % get_texture_scale(), z % get_texture_scale(), get_texture_scale()), - surface->transform_uv_scaled(TerraSurface::TERRA_SIDE_SIDE, Vector2(1, 1), x % get_texture_scale(), z % get_texture_scale(), get_texture_scale()) - }; - - Vector3 verts[] = { - Vector3(1, 0, 0) * voxel_scale + Vector3(x - 1, y - 1, z - 1) * voxel_scale, - Vector3(1, 1, 0) * voxel_scale + Vector3(x - 1, y - 1, z - 1) * voxel_scale, - Vector3(0, 1, 0) * voxel_scale + Vector3(x - 1, y - 1, z - 1) * voxel_scale, - Vector3(0, 0, 0) * voxel_scale + Vector3(x - 1, y - 1, z - 1) * voxel_scale - }; - - for (int i = 0; i < 4; ++i) { - add_normal(Vector3(0, 0, -1)); - - if (use_lighting || _always_add_colors) - add_color(light); - - add_uv(uvs[i]); - add_vertex(verts[i]); - } + add_uv(uvs[i]); + add_vertex(verts[i]); } } } diff --git a/meshers/blocky/terra_mesher_liquid_blocky.cpp b/meshers/blocky/terra_mesher_liquid_blocky.cpp index 0ae1219..61a4f8d 100644 --- a/meshers/blocky/terra_mesher_liquid_blocky.cpp +++ b/meshers/blocky/terra_mesher_liquid_blocky.cpp @@ -33,7 +33,6 @@ void TerraMesherLiquidBlocky::_add_chunk(Ref p_chunk) { // chunk->generate_ao(); int x_size = chunk->get_size_x(); - int y_size = chunk->get_size_y(); int z_size = chunk->get_size_z(); float voxel_scale = get_voxel_scale(); @@ -78,401 +77,396 @@ void TerraMesherLiquidBlocky::_add_chunk(Ref p_chunk) { liquids.push_back(static_cast(i + 1)); } - for (int y = chunk->get_margin_start(); y < y_size + chunk->get_margin_start(); ++y) { - for (int z = chunk->get_margin_start(); z < z_size + chunk->get_margin_start(); ++z) { - for (int x = chunk->get_margin_start(); x < x_size + chunk->get_margin_start(); ++x) { + for (int z = chunk->get_margin_start(); z < z_size + chunk->get_margin_start(); ++z) { + for (int x = chunk->get_margin_start(); x < x_size + chunk->get_margin_start(); ++x) { - int index = chunk->get_data_index(x, y, z); - int indexxp = chunk->get_data_index(x + 1, y, z); - int indexxn = chunk->get_data_index(x - 1, y, z); - int indexyp = chunk->get_data_index(x, y + 1, z); - int indexyn = chunk->get_data_index(x, y - 1, z); - int indexzp = chunk->get_data_index(x, y, z + 1); - int indexzn = chunk->get_data_index(x, y, z - 1); + int index = chunk->get_data_index(x, z); + int indexxp = chunk->get_data_index(x + 1, z); + int indexxn = chunk->get_data_index(x - 1, z); + int indexzp = chunk->get_data_index(x, z + 1); + int indexzn = chunk->get_data_index(x, z - 1); - uint8_t type = channel_type[index]; + uint8_t type = channel_type[index]; - if (type == 0) - continue; + if (type == 0) + continue; - if (liquids.find(type) == -1) - continue; + if (liquids.find(type) == -1) + continue; - Ref surface = _library->voxel_surface_get(type - 1); + Ref surface = _library->voxel_surface_get(type - 1); - if (!surface.is_valid()) - continue; + if (!surface.is_valid()) + continue; - uint8_t neighbours[] = { - channel_type[indexxp], - channel_type[indexxn], - channel_type[indexyp], - channel_type[indexyn], - channel_type[indexzp], - channel_type[indexzn], + uint8_t neighbours[] = { + channel_type[indexxp], + channel_type[indexxn], + channel_type[indexzp], + channel_type[indexzn], + }; + + //x + 1 + if (neighbours[0] == 0) { + if (use_lighting) { + light = Color(channel_color_r[indexxp] / 255.0, + channel_color_g[indexxp] / 255.0, + channel_color_b[indexxp] / 255.0); + + float ao = 0; + + if (use_ao) + ao = channel_ao[indexxp] / 255.0; + + if (use_rao) { + float rao = channel_rao[indexxp] / 255.0; + ao += rao; + } + + light += base_light; + + if (ao > 0) + light -= Color(ao, ao, ao) * _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); + } + + 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(TerraSurface::TERRA_SIDE_SIDE, Vector2(0, 1)), + surface->transform_uv(TerraSurface::TERRA_SIDE_SIDE, Vector2(0, 0)), + surface->transform_uv(TerraSurface::TERRA_SIDE_SIDE, Vector2(1, 0)), + surface->transform_uv(TerraSurface::TERRA_SIDE_SIDE, Vector2(1, 1)) }; - //x + 1 - if (neighbours[0] == 0) { - if (use_lighting) { - light = Color(channel_color_r[indexxp] / 255.0, - channel_color_g[indexxp] / 255.0, - channel_color_b[indexxp] / 255.0); + Vector3 verts[] = { + Vector3(1, 0, 0) * voxel_scale + Vector3(x - 1, - 1, z - 1) * voxel_scale, + Vector3(1, 1, 0) * voxel_scale + Vector3(x - 1, - 1, z - 1) * voxel_scale, + Vector3(1, 1, 1) * voxel_scale + Vector3(x - 1, - 1, z - 1) * voxel_scale, + Vector3(1, 0, 1) * voxel_scale + Vector3(x - 1, - 1, z - 1) * voxel_scale + }; - float ao = 0; + for (int i = 0; i < 4; ++i) { + add_normal(Vector3(1, 0, 0)); - if (use_ao) - ao = channel_ao[indexxp] / 255.0; + if (use_lighting) + add_color(light); - if (use_rao) { - float rao = channel_rao[indexxp] / 255.0; - ao += rao; - } - - light += base_light; - - if (ao > 0) - light -= Color(ao, ao, ao) * _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); - } - - 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(TerraSurface::TERRA_SIDE_SIDE, Vector2(0, 1)), - surface->transform_uv(TerraSurface::TERRA_SIDE_SIDE, Vector2(0, 0)), - surface->transform_uv(TerraSurface::TERRA_SIDE_SIDE, Vector2(1, 0)), - surface->transform_uv(TerraSurface::TERRA_SIDE_SIDE, Vector2(1, 1)) - }; - - Vector3 verts[] = { - Vector3(1, 0, 0) * voxel_scale + Vector3(x - 1, y - 1, z - 1) * voxel_scale, - Vector3(1, 1, 0) * voxel_scale + Vector3(x - 1, y - 1, z - 1) * voxel_scale, - Vector3(1, 1, 1) * voxel_scale + Vector3(x - 1, y - 1, z - 1) * voxel_scale, - Vector3(1, 0, 1) * voxel_scale + Vector3(x - 1, y - 1, z - 1) * voxel_scale - }; - - for (int i = 0; i < 4; ++i) { - add_normal(Vector3(1, 0, 0)); - - if (use_lighting) - add_color(light); - - add_uv(uvs[i]); - add_vertex(verts[i]); - } + add_uv(uvs[i]); + add_vertex(verts[i]); } + } - //x - 1 - if (neighbours[1] == 0) { - if (use_lighting) { - light = Color(channel_color_r[indexxn] / 255.0, - channel_color_g[indexxn] / 255.0, - channel_color_b[indexxn] / 255.0); + //x - 1 + if (neighbours[1] == 0) { + if (use_lighting) { + light = Color(channel_color_r[indexxn] / 255.0, + channel_color_g[indexxn] / 255.0, + channel_color_b[indexxn] / 255.0); - float ao = 0; + float ao = 0; - if (use_ao) - ao = channel_ao[indexxn] / 255.0; + if (use_ao) + ao = channel_ao[indexxn] / 255.0; - if (use_rao) { - float rao = channel_rao[indexxn] / 255.0; - ao += rao; - } - - light += base_light; - - if (ao > 0) - light -= Color(ao, ao, ao) * _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); + if (use_rao) { + float rao = channel_rao[indexxn] / 255.0; + ao += rao; } - int vc = get_vertex_count(); - add_indices(vc + 0); - add_indices(vc + 1); - add_indices(vc + 2); + light += base_light; - add_indices(vc + 0); - add_indices(vc + 2); - add_indices(vc + 3); - - Vector2 uvs[] = { - surface->transform_uv(TerraSurface::TERRA_SIDE_SIDE, Vector2(0, 1)), - surface->transform_uv(TerraSurface::TERRA_SIDE_SIDE, Vector2(0, 0)), - surface->transform_uv(TerraSurface::TERRA_SIDE_SIDE, Vector2(1, 0)), - surface->transform_uv(TerraSurface::TERRA_SIDE_SIDE, Vector2(1, 1)) - }; - - Vector3 verts[] = { - Vector3(0, 0, 0) * voxel_scale + Vector3(x - 1, y - 1, z - 1) * voxel_scale, - Vector3(0, 1, 0) * voxel_scale + Vector3(x - 1, y - 1, z - 1) * voxel_scale, - Vector3(0, 1, 1) * voxel_scale + Vector3(x - 1, y - 1, z - 1) * voxel_scale, - Vector3(0, 0, 1) * voxel_scale + Vector3(x - 1, y - 1, z - 1) * voxel_scale - }; - - for (int i = 0; i < 4; ++i) { - add_normal(Vector3(-1, 0, 0)); - - if (use_lighting) - add_color(light); - - add_uv(uvs[i]); - add_vertex(verts[i]); - } - } - - //y + 1 - if (neighbours[2] == 0) { - if (use_lighting) { - light = Color(channel_color_r[indexyp] / 255.0, - channel_color_g[indexyp] / 255.0, - channel_color_b[indexyp] / 255.0); - - float ao = 0; - - if (use_ao) - ao = channel_ao[indexyp] / 255.0; - - if (use_rao) { - float rao = channel_rao[indexyp] / 255.0; - ao += rao; - } - - light += base_light; - - if (ao > 0) - light -= Color(ao, ao, ao) * _ao_strength; - } + if (ao > 0) + light -= Color(ao, ao, ao) * _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); - - 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(TerraSurface::TERRA_SIDE_TOP, Vector2(0, 1)), - surface->transform_uv(TerraSurface::TERRA_SIDE_TOP, Vector2(0, 0)), - surface->transform_uv(TerraSurface::TERRA_SIDE_TOP, Vector2(1, 0)), - surface->transform_uv(TerraSurface::TERRA_SIDE_TOP, Vector2(1, 1)) - }; - - Vector3 verts[] = { - Vector3(1, 1, 0) * voxel_scale + Vector3(x - 1, y - 1, z - 1) * voxel_scale, - Vector3(0, 1, 0) * voxel_scale + Vector3(x - 1, y - 1, z - 1) * voxel_scale, - Vector3(0, 1, 1) * voxel_scale + Vector3(x - 1, y - 1, z - 1) * voxel_scale, - Vector3(1, 1, 1) * voxel_scale + Vector3(x - 1, y - 1, z - 1) * voxel_scale - }; - - for (int i = 0; i < 4; ++i) { - add_normal(Vector3(0, 1, 0)); - - if (use_lighting) - add_color(light); - - add_uv(uvs[i]); - add_vertex(verts[i]); - } } - //y - 1 - if (neighbours[3] == 0) { - if (use_lighting) { - light = Color(channel_color_r[indexyn] / 255.0, - channel_color_g[indexyn] / 255.0, - channel_color_b[indexyn] / 255.0); + int vc = get_vertex_count(); + add_indices(vc + 0); + add_indices(vc + 1); + add_indices(vc + 2); - float ao = 0; + add_indices(vc + 0); + add_indices(vc + 2); + add_indices(vc + 3); - if (use_ao) - ao = channel_ao[indexyn] / 255.0; + Vector2 uvs[] = { + surface->transform_uv(TerraSurface::TERRA_SIDE_SIDE, Vector2(0, 1)), + surface->transform_uv(TerraSurface::TERRA_SIDE_SIDE, Vector2(0, 0)), + surface->transform_uv(TerraSurface::TERRA_SIDE_SIDE, Vector2(1, 0)), + surface->transform_uv(TerraSurface::TERRA_SIDE_SIDE, Vector2(1, 1)) + }; - if (use_rao) { - float rao = channel_rao[indexyn] / 255.0; - ao += rao; - } + Vector3 verts[] = { + Vector3(0, 0, 0) * voxel_scale + Vector3(x - 1, - 1, z - 1) * voxel_scale, + Vector3(0, 1, 0) * voxel_scale + Vector3(x - 1, - 1, z - 1) * voxel_scale, + Vector3(0, 1, 1) * voxel_scale + Vector3(x - 1, - 1, z - 1) * voxel_scale, + Vector3(0, 0, 1) * voxel_scale + Vector3(x - 1, - 1, z - 1) * voxel_scale + }; - light += base_light; + for (int i = 0; i < 4; ++i) { + add_normal(Vector3(-1, 0, 0)); - if (ao > 0) - light -= Color(ao, ao, ao) * _ao_strength; + if (use_lighting) + add_color(light); + + add_uv(uvs[i]); + add_vertex(verts[i]); + } + } +/* + //y + 1 + if (neighbours[2] == 0) { + if (use_lighting) { + light = Color(channel_color_r[indexyp] / 255.0, + channel_color_g[indexyp] / 255.0, + channel_color_b[indexyp] / 255.0); + + float ao = 0; + + if (use_ao) + ao = channel_ao[indexyp] / 255.0; + + if (use_rao) { + float rao = channel_rao[indexyp] / 255.0; + ao += rao; } + light += base_light; + + if (ao > 0) + light -= Color(ao, ao, ao) * _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); + + 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(TerraSurface::TERRA_SIDE_TOP, Vector2(0, 1)), + surface->transform_uv(TerraSurface::TERRA_SIDE_TOP, Vector2(0, 0)), + surface->transform_uv(TerraSurface::TERRA_SIDE_TOP, Vector2(1, 0)), + surface->transform_uv(TerraSurface::TERRA_SIDE_TOP, Vector2(1, 1)) + }; + + Vector3 verts[] = { + Vector3(1, 1, 0) * voxel_scale + Vector3(x - 1, y - 1, z - 1) * voxel_scale, + Vector3(0, 1, 0) * voxel_scale + Vector3(x - 1, y - 1, z - 1) * voxel_scale, + Vector3(0, 1, 1) * voxel_scale + Vector3(x - 1, y - 1, z - 1) * voxel_scale, + Vector3(1, 1, 1) * voxel_scale + Vector3(x - 1, y - 1, z - 1) * voxel_scale + }; + + for (int i = 0; i < 4; ++i) { + add_normal(Vector3(0, 1, 0)); + + if (use_lighting) + add_color(light); + + add_uv(uvs[i]); + add_vertex(verts[i]); + } + } +*/ +/* + //y - 1 + if (neighbours[3] == 0) { + if (use_lighting) { + light = Color(channel_color_r[indexyn] / 255.0, + channel_color_g[indexyn] / 255.0, + channel_color_b[indexyn] / 255.0); + + float ao = 0; + + if (use_ao) + ao = channel_ao[indexyn] / 255.0; + + if (use_rao) { + float rao = channel_rao[indexyn] / 255.0; + ao += rao; + } + + light += base_light; + + if (ao > 0) + light -= Color(ao, ao, ao) * _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); + + int vc = get_vertex_count(); + add_indices(vc + 0); + add_indices(vc + 1); + add_indices(vc + 2); + + add_indices(vc + 0); + add_indices(vc + 2); + add_indices(vc + 3); + + Vector2 uvs[] = { + surface->transform_uv(TerraSurface::TERRA_SIDE_BOTTOM, Vector2(0, 1)), + surface->transform_uv(TerraSurface::TERRA_SIDE_BOTTOM, Vector2(0, 0)), + surface->transform_uv(TerraSurface::TERRA_SIDE_BOTTOM, Vector2(1, 0)), + surface->transform_uv(TerraSurface::TERRA_SIDE_BOTTOM, Vector2(1, 1)) + }; + + Vector3 verts[] = { + Vector3(1, 0, 0) * voxel_scale + Vector3(x - 1, y - 1, z - 1) * voxel_scale, + Vector3(0, 0, 0) * voxel_scale + Vector3(x - 1, y - 1, z - 1) * voxel_scale, + Vector3(0, 0, 1) * voxel_scale + Vector3(x - 1, y - 1, z - 1) * voxel_scale, + Vector3(1, 0, 1) * voxel_scale + Vector3(x - 1, y - 1, z - 1) * voxel_scale + }; + + for (int i = 0; i < 4; ++i) { + add_normal(Vector3(0, -1, 0)); + + if (use_lighting) + add_color(light); + + add_uv(uvs[i]); + add_vertex(verts[i]); + } + } +*/ + //z + 1 + if (neighbours[4] == 0) { + if (use_lighting) { + light = Color(channel_color_r[indexzp] / 255.0, + channel_color_g[indexzp] / 255.0, + channel_color_b[indexzp] / 255.0); + + float ao = 0; + + if (use_ao) + ao = channel_ao[indexzp] / 255.0; + + if (use_rao) { + float rao = channel_rao[indexzp] / 255.0; + ao += rao; + } + + light += base_light; + + if (ao > 0) + light -= Color(ao, ao, ao) * _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); - - int vc = get_vertex_count(); - add_indices(vc + 0); - add_indices(vc + 1); - add_indices(vc + 2); - - add_indices(vc + 0); - add_indices(vc + 2); - add_indices(vc + 3); - - Vector2 uvs[] = { - surface->transform_uv(TerraSurface::TERRA_SIDE_BOTTOM, Vector2(0, 1)), - surface->transform_uv(TerraSurface::TERRA_SIDE_BOTTOM, Vector2(0, 0)), - surface->transform_uv(TerraSurface::TERRA_SIDE_BOTTOM, Vector2(1, 0)), - surface->transform_uv(TerraSurface::TERRA_SIDE_BOTTOM, Vector2(1, 1)) - }; - - Vector3 verts[] = { - Vector3(1, 0, 0) * voxel_scale + Vector3(x - 1, y - 1, z - 1) * voxel_scale, - Vector3(0, 0, 0) * voxel_scale + Vector3(x - 1, y - 1, z - 1) * voxel_scale, - Vector3(0, 0, 1) * voxel_scale + Vector3(x - 1, y - 1, z - 1) * voxel_scale, - Vector3(1, 0, 1) * voxel_scale + Vector3(x - 1, y - 1, z - 1) * voxel_scale - }; - - for (int i = 0; i < 4; ++i) { - add_normal(Vector3(0, -1, 0)); - - if (use_lighting) - add_color(light); - - add_uv(uvs[i]); - add_vertex(verts[i]); - } } - //z + 1 - if (neighbours[4] == 0) { - if (use_lighting) { - light = Color(channel_color_r[indexzp] / 255.0, - channel_color_g[indexzp] / 255.0, - channel_color_b[indexzp] / 255.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); - float ao = 0; + Vector2 uvs[] = { + surface->transform_uv(TerraSurface::TERRA_SIDE_SIDE, Vector2(0, 1)), + surface->transform_uv(TerraSurface::TERRA_SIDE_SIDE, Vector2(0, 0)), + surface->transform_uv(TerraSurface::TERRA_SIDE_SIDE, Vector2(1, 0)), + surface->transform_uv(TerraSurface::TERRA_SIDE_SIDE, Vector2(1, 1)) + }; - if (use_ao) - ao = channel_ao[indexzp] / 255.0; + Vector3 verts[] = { + Vector3(1, 0, 1) * voxel_scale + Vector3(x - 1, - 1, z - 1) * voxel_scale, + Vector3(1, 1, 1) * voxel_scale + Vector3(x - 1, - 1, z - 1) * voxel_scale, + Vector3(0, 1, 1) * voxel_scale + Vector3(x - 1, - 1, z - 1) * voxel_scale, + Vector3(0, 0, 1) * voxel_scale + Vector3(x - 1, - 1, z - 1) * voxel_scale + }; - if (use_rao) { - float rao = channel_rao[indexzp] / 255.0; - ao += rao; - } + for (int i = 0; i < 4; ++i) { + add_normal(Vector3(0, 0, 1)); - light += base_light; + if (use_lighting) + add_color(light); - if (ao > 0) - light -= Color(ao, ao, ao) * _ao_strength; + add_uv(uvs[i]); + add_vertex(verts[i]); + } + } - light.r = CLAMP(light.r, 0, 1.0); - light.g = CLAMP(light.g, 0, 1.0); - light.b = CLAMP(light.b, 0, 1.0); + //z - 1 + if (neighbours[5] == 0) { + if (use_lighting) { + light = Color(channel_color_r[indexzn] / 255.0, + channel_color_g[indexzn] / 255.0, + channel_color_b[indexzn] / 255.0); + + float ao = 0; + + if (use_ao) + ao = channel_ao[indexzn] / 255.0; + + if (use_rao) { + float rao = channel_rao[indexzn] / 255.0; + ao += rao; } - 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); + light += base_light; - Vector2 uvs[] = { - surface->transform_uv(TerraSurface::TERRA_SIDE_SIDE, Vector2(0, 1)), - surface->transform_uv(TerraSurface::TERRA_SIDE_SIDE, Vector2(0, 0)), - surface->transform_uv(TerraSurface::TERRA_SIDE_SIDE, Vector2(1, 0)), - surface->transform_uv(TerraSurface::TERRA_SIDE_SIDE, Vector2(1, 1)) - }; + if (ao > 0) + light -= Color(ao, ao, ao) * _ao_strength; - Vector3 verts[] = { - Vector3(1, 0, 1) * voxel_scale + Vector3(x - 1, y - 1, z - 1) * voxel_scale, - Vector3(1, 1, 1) * voxel_scale + Vector3(x - 1, y - 1, z - 1) * voxel_scale, - Vector3(0, 1, 1) * voxel_scale + Vector3(x - 1, y - 1, z - 1) * voxel_scale, - Vector3(0, 0, 1) * voxel_scale + Vector3(x - 1, y - 1, z - 1) * voxel_scale - }; - - for (int i = 0; i < 4; ++i) { - add_normal(Vector3(0, 0, 1)); - - if (use_lighting) - add_color(light); - - add_uv(uvs[i]); - add_vertex(verts[i]); - } + light.r = CLAMP(light.r, 0, 1.0); + light.g = CLAMP(light.g, 0, 1.0); + light.b = CLAMP(light.b, 0, 1.0); } - //z - 1 - if (neighbours[5] == 0) { - if (use_lighting) { - light = Color(channel_color_r[indexzn] / 255.0, - channel_color_g[indexzn] / 255.0, - channel_color_b[indexzn] / 255.0); + int vc = get_vertex_count(); + add_indices(vc + 0); + add_indices(vc + 1); + add_indices(vc + 2); - float ao = 0; + add_indices(vc + 0); + add_indices(vc + 2); + add_indices(vc + 3); - if (use_ao) - ao = channel_ao[indexzn] / 255.0; + Vector2 uvs[] = { + surface->transform_uv(TerraSurface::TERRA_SIDE_SIDE, Vector2(0, 1)), + surface->transform_uv(TerraSurface::TERRA_SIDE_SIDE, Vector2(0, 0)), + surface->transform_uv(TerraSurface::TERRA_SIDE_SIDE, Vector2(1, 0)), + surface->transform_uv(TerraSurface::TERRA_SIDE_SIDE, Vector2(1, 1)) + }; - if (use_rao) { - float rao = channel_rao[indexzn] / 255.0; - ao += rao; - } + Vector3 verts[] = { + Vector3(1, 0, 0) * voxel_scale + Vector3(x - 1, - 1, z - 1) * voxel_scale, + Vector3(1, 1, 0) * voxel_scale + Vector3(x - 1, - 1, z - 1) * voxel_scale, + Vector3(0, 1, 0) * voxel_scale + Vector3(x - 1, - 1, z - 1) * voxel_scale, + Vector3(0, 0, 0) * voxel_scale + Vector3(x - 1, - 1, z - 1) * voxel_scale + }; - light += base_light; + for (int i = 0; i < 4; ++i) { + add_normal(Vector3(0, 0, -1)); - if (ao > 0) - light -= Color(ao, ao, ao) * _ao_strength; + if (use_lighting) + add_color(light); - light.r = CLAMP(light.r, 0, 1.0); - light.g = CLAMP(light.g, 0, 1.0); - light.b = CLAMP(light.b, 0, 1.0); - } - - int vc = get_vertex_count(); - add_indices(vc + 0); - add_indices(vc + 1); - add_indices(vc + 2); - - add_indices(vc + 0); - add_indices(vc + 2); - add_indices(vc + 3); - - Vector2 uvs[] = { - surface->transform_uv(TerraSurface::TERRA_SIDE_SIDE, Vector2(0, 1)), - surface->transform_uv(TerraSurface::TERRA_SIDE_SIDE, Vector2(0, 0)), - surface->transform_uv(TerraSurface::TERRA_SIDE_SIDE, Vector2(1, 0)), - surface->transform_uv(TerraSurface::TERRA_SIDE_SIDE, Vector2(1, 1)) - }; - - Vector3 verts[] = { - Vector3(1, 0, 0) * voxel_scale + Vector3(x - 1, y - 1, z - 1) * voxel_scale, - Vector3(1, 1, 0) * voxel_scale + Vector3(x - 1, y - 1, z - 1) * voxel_scale, - Vector3(0, 1, 0) * voxel_scale + Vector3(x - 1, y - 1, z - 1) * voxel_scale, - Vector3(0, 0, 0) * voxel_scale + Vector3(x - 1, y - 1, z - 1) * voxel_scale - }; - - for (int i = 0; i < 4; ++i) { - add_normal(Vector3(0, 0, -1)); - - if (use_lighting) - add_color(light); - - add_uv(uvs[i]); - add_vertex(verts[i]); - } + add_uv(uvs[i]); + add_vertex(verts[i]); } } } diff --git a/meshers/default/terra_mesher_default.cpp b/meshers/default/terra_mesher_default.cpp index fff4d8b..fb9854c 100644 --- a/meshers/default/terra_mesher_default.cpp +++ b/meshers/default/terra_mesher_default.cpp @@ -63,17 +63,16 @@ void TerraMesherDefault::_bake_colors(Ref chunk) { } 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); - if (chunk->validate_data_position(x, y, z)) { + if (chunk->validate_data_position(x, z)) { Color light = Color( - chunk->get_voxel(x, y, z, TerraChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_R) / 255.0, - chunk->get_voxel(x, y, z, TerraChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_G) / 255.0, - chunk->get_voxel(x, y, z, TerraChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_B) / 255.0); + chunk->get_voxel(x, z, TerraChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_R) / 255.0, + chunk->get_voxel(x, z, TerraChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_G) / 255.0, + chunk->get_voxel(x, z, TerraChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_B) / 255.0); - float ao = (chunk->get_voxel(x, y, z, TerraChunkDefault::DEFAULT_CHANNEL_AO) / 255.0) * _ao_strength; - float rao = chunk->get_voxel(x, y, z, TerraChunkDefault::DEFAULT_CHANNEL_RANDOM_AO) / 255.0; + float ao = (chunk->get_voxel(x, z, TerraChunkDefault::DEFAULT_CHANNEL_AO) / 255.0) * _ao_strength; + float rao = chunk->get_voxel(x, z, TerraChunkDefault::DEFAULT_CHANNEL_RANDOM_AO) / 255.0; ao += rao; @@ -122,17 +121,17 @@ void TerraMesherDefault::_bake_liquid_colors(Ref chunk) { } unsigned int x = (unsigned int)(vert.x / _voxel_scale); - unsigned int y = (unsigned int)(vert.y / _voxel_scale); + //unsigned int y = (unsigned int)(vert.y / _voxel_scale); unsigned int z = (unsigned int)(vert.z / _voxel_scale); - if (chunk->validate_data_position(x, y, z)) { + if (chunk->validate_data_position(x, z)) { Color light = Color( - chunk->get_voxel(x, y, z, TerraChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_R) / 255.0, - chunk->get_voxel(x, y, z, TerraChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_G) / 255.0, - chunk->get_voxel(x, y, z, TerraChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_B) / 255.0); + chunk->get_voxel(x, z, TerraChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_R) / 255.0, + chunk->get_voxel(x, z, TerraChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_G) / 255.0, + chunk->get_voxel(x, z, TerraChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_B) / 255.0); - float ao = (chunk->get_voxel(x, y, z, TerraChunkDefault::DEFAULT_CHANNEL_AO) / 255.0) * _ao_strength; - float rao = chunk->get_voxel(x, y, z, TerraChunkDefault::DEFAULT_CHANNEL_RANDOM_AO) / 255.0; + float ao = (chunk->get_voxel(x, z, TerraChunkDefault::DEFAULT_CHANNEL_AO) / 255.0) * _ao_strength; + float rao = chunk->get_voxel(x, z, TerraChunkDefault::DEFAULT_CHANNEL_RANDOM_AO) / 255.0; ao += rao; diff --git a/world/blocky/terra_world_blocky.cpp b/world/blocky/terra_world_blocky.cpp index 8019e23..5d6cc8c 100644 --- a/world/blocky/terra_world_blocky.cpp +++ b/world/blocky/terra_world_blocky.cpp @@ -30,7 +30,7 @@ SOFTWARE. #include "../jobs/terra_prop_job.h" #include "../jobs/terra_terrarin_job.h" -Ref TerraWorldBlocky::_create_chunk(int x, int y, int z, Ref chunk) { +Ref TerraWorldBlocky::_create_chunk(int x, int z, Ref chunk) { if (!chunk.is_valid()) { chunk = Ref(memnew(TerraChunkBlocky)); @@ -55,7 +55,7 @@ Ref TerraWorldBlocky::_create_chunk(int x, int y, int z, Refjob_add(pj); } - return TerraWorld::_create_chunk(x, y, z, chunk); + return TerraWorld::_create_chunk(x, z, chunk); } TerraWorldBlocky::TerraWorldBlocky() { diff --git a/world/blocky/terra_world_blocky.h b/world/blocky/terra_world_blocky.h index e321741..2883ec4 100644 --- a/world/blocky/terra_world_blocky.h +++ b/world/blocky/terra_world_blocky.h @@ -33,7 +33,7 @@ public: ~TerraWorldBlocky(); protected: - Ref _create_chunk(int x, int y, int z, Ref p_chunk); + Ref _create_chunk(int x, int z, Ref p_chunk); static void _bind_methods(); }; diff --git a/world/default/terra_chunk_default.cpp b/world/default/terra_chunk_default.cpp index 8d98584..96c7bba 100644 --- a/world/default/terra_chunk_default.cpp +++ b/world/default/terra_chunk_default.cpp @@ -640,19 +640,17 @@ void TerraChunkDefault::draw_debug_voxels(int max, Color color) { int a = 0; int64_t sx = static_cast(_size_x); - int64_t sy = static_cast(_size_y); - int64_t sz = static_cast(_size_y); + int64_t sz = static_cast(_size_z); - for (int y = 0; y < sy; ++y) { for (int z = 0; z < sz; ++z) { for (int x = 0; x < sx; ++x) { - int type = get_voxel(x, y, z, TerraChunkDefault::DEFAULT_CHANNEL_TYPE); + int type = get_voxel(x, z, TerraChunkDefault::DEFAULT_CHANNEL_TYPE); if (type == 0) { continue; } - draw_cross_voxels_fill(Vector3(x, y, z), get_voxel(x, y, z, TerraChunkDefault::DEFAULT_CHANNEL_ISOLEVEL) / 255.0 * get_voxel_scale() * 2.0); + draw_cross_voxels_fill(Vector3(x, get_voxel(x, z, TerraChunkDefault::DEFAULT_CHANNEL_ISOLEVEL), z), get_voxel(x, z, TerraChunkDefault::DEFAULT_CHANNEL_ISOLEVEL) / 255.0 * get_voxel_scale() * 2.0); ++a; @@ -661,7 +659,6 @@ void TerraChunkDefault::draw_debug_voxels(int max, Color color) { } } } - } debug_mesh_send(); } @@ -679,10 +676,9 @@ void TerraChunkDefault::draw_debug_voxel_lights() { Ref v = _lights[i]; int pos_x = v->get_world_position_x() - (_size_x * _position_x); - int pos_y = v->get_world_position_y() - (_size_y * _position_y); int pos_z = v->get_world_position_z() - (_size_z * _position_z); - draw_cross_voxels_fill(Vector3(pos_x, pos_y, pos_z), 1.0); + draw_cross_voxels_fill(Vector3(pos_x, 0, pos_z), 1.0); } debug_mesh_send(); @@ -756,13 +752,13 @@ void TerraChunkDefault::_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() - (_position_y * _size_y); + int local_y = light->get_world_position_y(); int local_z = light->get_world_position_z() - (_position_z * _size_z); ERR_FAIL_COND(size < 0); int64_t dsx = static_cast(_data_size_x); - int64_t dsy = static_cast(_data_size_y); + int64_t dsy = static_cast(_world_height); int64_t dsz = static_cast(_data_size_z); uint8_t *channel_color_r = channel_get(TerraChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_R); @@ -793,7 +789,7 @@ void TerraChunkDefault::_bake_light(Ref light) { if (str < 0) continue; - int index = get_data_index(x, y, z); + int index = get_data_index(x, z); int r = color.r * str * 255.0; int g = color.g * str * 255.0; diff --git a/world/default/terra_world_default.cpp b/world/default/terra_world_default.cpp index ee8e21f..3258c04 100644 --- a/world/default/terra_world_default.cpp +++ b/world/default/terra_world_default.cpp @@ -83,35 +83,29 @@ PoolColorArray TerraWorldDefault::get_vertex_colors(const Transform &transform, //Note: floor is needed to handle negative numbers proiberly int x = static_cast(Math::floor(pos.x / get_chunk_size_x())); - int y = static_cast(Math::floor(pos.y / get_chunk_size_y())); int z = static_cast(Math::floor(pos.z / get_chunk_size_z())); int bx = static_cast(Math::floor(pos.x)) % get_chunk_size_x(); - int by = static_cast(Math::floor(pos.y)) % get_chunk_size_y(); int bz = static_cast(Math::floor(pos.z)) % get_chunk_size_z(); if (bx < 0) { bx += get_chunk_size_x(); } - if (by < 0) { - by += get_chunk_size_y(); - } - if (bz < 0) { bz += get_chunk_size_z(); } - Ref chunk = chunk_get(x, y, z); + Ref chunk = chunk_get(x, z); if (chunk.is_valid()) { Color light = Color( - chunk->get_voxel(bx, by, bz, TerraChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_R) / 255.0, - chunk->get_voxel(bx, by, bz, TerraChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_G) / 255.0, - chunk->get_voxel(bx, by, bz, TerraChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_B) / 255.0); + chunk->get_voxel(bx, bz, TerraChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_R) / 255.0, + chunk->get_voxel(bx, bz, TerraChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_G) / 255.0, + chunk->get_voxel(bx, bz, TerraChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_B) / 255.0); - float ao = (chunk->get_voxel(bx, by, bz, TerraChunkDefault::DEFAULT_CHANNEL_AO) / 255.0) * ao_strength; - float rao = chunk->get_voxel(bx, by, bz, TerraChunkDefault::DEFAULT_CHANNEL_RANDOM_AO) / 255.0; + float ao = (chunk->get_voxel(bx, bz, TerraChunkDefault::DEFAULT_CHANNEL_AO) / 255.0) * ao_strength; + float rao = chunk->get_voxel(bx, bz, TerraChunkDefault::DEFAULT_CHANNEL_RANDOM_AO) / 255.0; ao += rao; @@ -147,7 +141,6 @@ void TerraWorldDefault::_update_lods() { Vector3 ppos = get_player()->get_transform().origin; int ppx = int(ppos.x / get_chunk_size_x() / get_voxel_scale()); - int ppy = int(ppos.y / get_chunk_size_y() / get_voxel_scale()); int ppz = int(ppos.z / get_chunk_size_z() / get_voxel_scale()); for (int i = 0; i < chunk_get_count(); ++i) { @@ -157,10 +150,9 @@ void TerraWorldDefault::_update_lods() { continue; int dx = Math::abs(ppx - c->get_position_x()); - int dy = Math::abs(ppy - c->get_position_y()); int dz = Math::abs(ppz - c->get_position_z()); - int mr = MAX(MAX(dx, dy), dz); + int mr = MAX(dx, dz); mr -= _chunk_lod_falloff; @@ -172,7 +164,7 @@ void TerraWorldDefault::_update_lods() { } } -Ref TerraWorldDefault::_create_chunk(int x, int y, int z, Ref chunk) { +Ref TerraWorldDefault::_create_chunk(int x, int z, Ref chunk) { if (!chunk.is_valid()) { chunk = Ref(memnew(TerraChunkDefault)); } @@ -200,7 +192,7 @@ Ref TerraWorldDefault::_create_chunk(int x, int y, int z, Refset_lod_num(_num_lods); } - return TerraWorld::_create_chunk(x, y, z, chunk); + return TerraWorld::_create_chunk(x, z, chunk); } void TerraWorldDefault::_chunk_added(Ref chunk) { diff --git a/world/default/terra_world_default.h b/world/default/terra_world_default.h index 526f646..96fb68a 100644 --- a/world/default/terra_world_default.h +++ b/world/default/terra_world_default.h @@ -50,7 +50,7 @@ public: protected: void _update_lods(); - Ref _create_chunk(int x, int y, int z, Ref p_chunk); + Ref _create_chunk(int x, int z, Ref p_chunk); virtual void _chunk_added(Ref chunk); int _get_channel_index_info(const ChannelTypeInfo channel_type); diff --git a/world/jobs/terra_job.cpp b/world/jobs/terra_job.cpp index d679ff4..867d4fe 100644 --- a/world/jobs/terra_job.cpp +++ b/world/jobs/terra_job.cpp @@ -106,43 +106,36 @@ void TerraJob::generate_ao() { ERR_FAIL_COND(!_chunk.is_valid()); int data_size_x = _chunk->get_data_size_x(); - int data_size_y = _chunk->get_data_size_y(); int data_size_z = _chunk->get_data_size_z(); - ERR_FAIL_COND(data_size_x == 0 || data_size_y == 0 || data_size_z == 0); + ERR_FAIL_COND(data_size_x == 0 || data_size_z == 0); int margin_start = _chunk->get_margin_start(); int margin_end = _chunk->get_margin_end(); int ssize_x = _chunk->get_size_x(); - int ssize_y = _chunk->get_size_y(); int ssize_z = _chunk->get_size_z(); int size_x = ssize_x + margin_end; - int size_y = ssize_y + margin_end; int size_z = ssize_z + margin_end; - for (int y = margin_start - 1; y < size_y - 1; ++y) { - for (int z = margin_start - 1; z < size_z - 1; ++z) { - for (int x = margin_start - 1; x < size_x - 1; ++x) { - int current = _chunk->get_voxel(x, y, z, TerraChunkDefault::DEFAULT_CHANNEL_ISOLEVEL); + for (int z = margin_start - 1; z < size_z - 1; ++z) { + for (int x = margin_start - 1; x < size_x - 1; ++x) { + int current = _chunk->get_voxel(x, z, TerraChunkDefault::DEFAULT_CHANNEL_ISOLEVEL); - int sum = _chunk->get_voxel(x + 1, y, z, TerraChunkDefault::DEFAULT_CHANNEL_ISOLEVEL); - sum += _chunk->get_voxel(x - 1, y, z, TerraChunkDefault::DEFAULT_CHANNEL_ISOLEVEL); - sum += _chunk->get_voxel(x, y + 1, z, TerraChunkDefault::DEFAULT_CHANNEL_ISOLEVEL); - sum += _chunk->get_voxel(x, y - 1, z, TerraChunkDefault::DEFAULT_CHANNEL_ISOLEVEL); - sum += _chunk->get_voxel(x, y, z + 1, TerraChunkDefault::DEFAULT_CHANNEL_ISOLEVEL); - sum += _chunk->get_voxel(x, y, z - 1, TerraChunkDefault::DEFAULT_CHANNEL_ISOLEVEL); + int sum = _chunk->get_voxel(x + 1, z, TerraChunkDefault::DEFAULT_CHANNEL_ISOLEVEL); + sum += _chunk->get_voxel(x - 1, z, TerraChunkDefault::DEFAULT_CHANNEL_ISOLEVEL); + sum += _chunk->get_voxel(x, z + 1, TerraChunkDefault::DEFAULT_CHANNEL_ISOLEVEL); + sum += _chunk->get_voxel(x, z - 1, TerraChunkDefault::DEFAULT_CHANNEL_ISOLEVEL); - sum /= 6; + sum /= 6; - sum -= current; + sum -= current; - if (sum < 0) - sum = 0; + if (sum < 0) + sum = 0; - _chunk->set_voxel(sum, x, y, z, TerraChunkDefault::DEFAULT_CHANNEL_AO); - } + _chunk->set_voxel(sum, x, z, TerraChunkDefault::DEFAULT_CHANNEL_AO); } } } @@ -154,11 +147,9 @@ void TerraJob::generate_random_ao(int seed, int octaves, int period, float persi int margin_end = _chunk->get_margin_end(); int size_x = _chunk->get_size_x(); - int size_y = _chunk->get_size_y(); int size_z = _chunk->get_size_z(); int position_x = _chunk->get_position_x(); - int position_y = _chunk->get_position_y(); int position_z = _chunk->get_position_z(); Ref noise; @@ -171,19 +162,18 @@ void TerraJob::generate_random_ao(int seed, int octaves, int period, float persi for (int x = -margin_start; x < size_x + margin_end; ++x) { for (int z = -margin_start; z < size_z + margin_end; ++z) { - for (int y = -margin_start; y < size_y + margin_end; ++y) { - float val = noise->get_noise_3d(x + (position_x * size_x), y + (position_y * size_y), z + (position_z * size_z)); - val *= scale_factor; + float val = noise->get_noise_3d(x + (position_x * size_x), 0, z + (position_z * size_z)); - if (val > 1) - val = 1; + val *= scale_factor; - if (val < 0) - val = -val; + if (val > 1) + val = 1; - _chunk->set_voxel(int(val * 255.0), x, y, z, TerraChunkDefault::DEFAULT_CHANNEL_RANDOM_AO); - } + if (val < 0) + val = -val; + + _chunk->set_voxel(int(val * 255.0), x, z, TerraChunkDefault::DEFAULT_CHANNEL_RANDOM_AO); } } } diff --git a/world/terra_chunk.cpp b/world/terra_chunk.cpp index 80b3b18..e3f6467 100644 --- a/world/terra_chunk.cpp +++ b/world/terra_chunk.cpp @@ -96,12 +96,6 @@ _FORCE_INLINE_ int TerraChunk::get_position_x() const { void TerraChunk::set_position_x(const int value) { _position_x = value; } -_FORCE_INLINE_ int TerraChunk::get_position_y() const { - return _position_y; -} -void TerraChunk::set_position_y(const int value) { - _position_y = value; -} _FORCE_INLINE_ int TerraChunk::get_position_z() const { return _position_z; } @@ -109,27 +103,26 @@ void TerraChunk::set_position_z(const int value) { _position_z = value; } -_FORCE_INLINE_ Vector3 TerraChunk::get_position() const { - return Vector3(_position_x, _position_y, _position_z); +_FORCE_INLINE_ Vector2 TerraChunk::get_position() const { + return Vector2(_position_x, _position_z); } -_FORCE_INLINE_ Vector3 TerraChunk::get_world_position() const { - return Vector3(_position_x * _size_x * _voxel_scale, _position_y * _size_y * _voxel_scale, _position_z * _size_z * _voxel_scale); +_FORCE_INLINE_ Vector2 TerraChunk::get_world_position() const { + return Vector2(_position_x * _size_x * _voxel_scale, _position_z * _size_z * _voxel_scale); } _FORCE_INLINE_ Vector3 TerraChunk::get_world_size() const { - return Vector3(_size_x * _voxel_scale, _size_y * _voxel_scale, _size_z * _voxel_scale); + return Vector3(_size_x * _voxel_scale, _world_height * _voxel_scale, _size_z * _voxel_scale); } _FORCE_INLINE_ AABB TerraChunk::get_world_aabb() const { - return AABB(get_world_position(), get_world_size()); + Vector2 v = get_world_position(); + + return AABB(Vector3(v.x, 0, v.y), get_world_size()); } _FORCE_INLINE_ int TerraChunk::get_size_x() const { return _size_x; } -_FORCE_INLINE_ int TerraChunk::get_size_y() const { - return _size_y; -} _FORCE_INLINE_ int TerraChunk::get_size_z() const { return _size_z; } @@ -137,23 +130,17 @@ _FORCE_INLINE_ int TerraChunk::get_size_z() const { _FORCE_INLINE_ void TerraChunk::set_size_x(const int value) { _size_x = value; } -_FORCE_INLINE_ void TerraChunk::set_size_y(const int value) { - _size_y = value; -} _FORCE_INLINE_ void TerraChunk::set_size_z(const int value) { _size_z = value; } _FORCE_INLINE_ Vector3 TerraChunk::get_size() const { - return Vector3(_size_x, _size_y, _size_z); + return Vector3(_size_x, _world_height, _size_z); } _FORCE_INLINE_ int TerraChunk::get_data_size_x() const { return _data_size_x; } -_FORCE_INLINE_ int TerraChunk::get_data_size_y() const { - return _data_size_y; -} _FORCE_INLINE_ int TerraChunk::get_data_size_z() const { return _data_size_z; } @@ -161,16 +148,19 @@ _FORCE_INLINE_ int TerraChunk::get_data_size_z() const { _FORCE_INLINE_ void TerraChunk::set_data_size_x(const int value) { _data_size_x = value; } -_FORCE_INLINE_ void TerraChunk::set_data_size_y(const int value) { - _data_size_y = value; -} _FORCE_INLINE_ void TerraChunk::set_data_size_z(const int value) { _data_size_z = value; } -void TerraChunk::set_position(const int x, const int y, const int z) { +_FORCE_INLINE_ float TerraChunk::get_world_height() const { + return _world_height; +} +void TerraChunk::set_world_height(const float value) { + _world_height = value; +} + +void TerraChunk::set_position(const int x, const int z) { _position_x = x; - _position_y = y; _position_z = z; } @@ -289,8 +279,8 @@ void TerraChunk::channel_setup() { call("_channel_setup"); } -void TerraChunk::set_size(const int size_x, const int size_y, const int size_z, const int margin_start, const int margin_end) { - if (_size_x == size_x && _size_y == size_y && _size_z == size_z && _margin_start == margin_start && _margin_end == margin_end) { +void TerraChunk::set_size(const int size_x, const int size_z, const int margin_start, const int margin_end) { + if (_size_x == size_x && _size_z == size_z && _margin_start == margin_start && _margin_end == margin_end) { return; } @@ -305,47 +295,43 @@ void TerraChunk::set_size(const int size_x, const int size_y, const int size_z, channel_setup(); _size_x = size_x; - _size_y = size_y; _size_z = size_z; _data_size_x = size_x + margin_start + margin_end; - _data_size_y = size_y + margin_start + margin_end; _data_size_z = size_z + margin_start + margin_end; _margin_start = margin_start; _margin_end = margin_end; } -bool TerraChunk::validate_data_position(const int x, const int y, const int z) const { - return x < _data_size_x && y < _data_size_y && z < _data_size_z; +bool TerraChunk::validate_data_position(const int x, const int z) const { + return x < _data_size_x && z < _data_size_z; } -uint8_t TerraChunk::get_voxel(const int p_x, const int p_y, const int p_z, const int p_channel_index) const { +uint8_t TerraChunk::get_voxel(const int p_x, const int p_z, const int p_channel_index) const { int x = p_x + _margin_start; - int y = p_y + _margin_start; int z = p_z + _margin_start; ERR_FAIL_INDEX_V(p_channel_index, _channels.size(), 0); - ERR_FAIL_COND_V_MSG(!validate_data_position(x, y, z), 0, "Error, index out of range! " + String::num(x) + " " + String::num(y) + " " + String::num(z)); + ERR_FAIL_COND_V_MSG(!validate_data_position(x, z), 0, "Error, index out of range! " + String::num(x) + " " + String::num(z)); uint8_t *ch = _channels.get(p_channel_index); if (!ch) return 0; - return ch[get_data_index(x, y, z)]; + return ch[get_data_index(x, z)]; } -void TerraChunk::set_voxel(const uint8_t p_value, const int p_x, const int p_y, const int p_z, const int p_channel_index) { +void TerraChunk::set_voxel(const uint8_t p_value, const int p_x, const int p_z, const int p_channel_index) { int x = p_x + _margin_start; - int y = p_y + _margin_start; int z = p_z + _margin_start; ERR_FAIL_INDEX(p_channel_index, _channels.size()); - ERR_FAIL_COND_MSG(!validate_data_position(x, y, z), "Error, index out of range! " + String::num(x) + " " + String::num(y) + " " + String::num(z)); + ERR_FAIL_COND_MSG(!validate_data_position(x, z), "Error, index out of range! " + String::num(x) + " " + String::num(z)); uint8_t *ch = channel_get_valid(p_channel_index); - ch[get_data_index(x, y, z)] = p_value; + ch[get_data_index(x, z)] = p_value; } int TerraChunk::channel_get_count() const { @@ -393,7 +379,7 @@ void TerraChunk::channel_allocate(const int channel_index, const uint8_t default if (_channels[channel_index] != NULL) return; - uint32_t size = _data_size_x * _data_size_y * _data_size_z; + uint32_t size = _data_size_x * _data_size_z; uint8_t *ch = memnew_arr(uint8_t, size); memset(ch, default_value, size); @@ -450,7 +436,7 @@ uint8_t *TerraChunk::channel_get_valid(const int channel_index, const uint8_t de PoolByteArray TerraChunk::channel_get_array(const int channel_index) const { PoolByteArray arr; - uint32_t size = _data_size_x * _data_size_y * _data_size_z; + uint32_t size = _data_size_x * _data_size_z; if (channel_index >= _channels.size()) return arr; @@ -493,7 +479,7 @@ void TerraChunk::channel_set_array(const int channel_index, const PoolByteArray PoolByteArray TerraChunk::channel_get_compressed(const int channel_index) const { PoolByteArray arr; - int size = _data_size_x * _data_size_y * _data_size_z; + int size = _data_size_x * _data_size_z; if (channel_index >= _channels.size()) return arr; @@ -523,7 +509,7 @@ void TerraChunk::channel_set_compressed(const int channel_index, const PoolByteA if (data.size() == 0) return; - int size = _data_size_x * _data_size_y * _data_size_z; + int size = _data_size_x * _data_size_z; if (_channels.size() <= channel_index) channel_set_count(channel_index + 1); @@ -555,16 +541,16 @@ void TerraChunk::channel_set_compressed(const int channel_index, const PoolByteA #endif } -_FORCE_INLINE_ int TerraChunk::get_index(const int x, const int y, const int z) const { - return (y + _margin_start) + _data_size_y * ((x + _margin_start) + _data_size_x * (z + _margin_start)); +_FORCE_INLINE_ int TerraChunk::get_index(const int x, const int z) const { + return ((x + _margin_start) + _data_size_x * (z + _margin_start)); } -_FORCE_INLINE_ int TerraChunk::get_data_index(const int x, const int y, const int z) const { - return y + _data_size_y * (x + _data_size_x * z); +_FORCE_INLINE_ int TerraChunk::get_data_index(const int x, const int z) const { + return (x + _data_size_x * z); } _FORCE_INLINE_ int TerraChunk::get_data_size() const { - return _data_size_x * _data_size_y * _data_size_z; + return _data_size_x * _data_size_z; } //Terra Structures @@ -1015,15 +1001,12 @@ TerraChunk::TerraChunk() { _voxel_world = NULL; _position_x = 0; - _position_y = 0; _position_z = 0; _size_x = 0; - _size_y = 0; _size_z = 0; _data_size_x = 0; - _data_size_y = 0; _data_size_z = 0; _margin_start = 0; @@ -1031,6 +1014,8 @@ TerraChunk::TerraChunk() { _current_job = -1; + _world_height = 256; + _queued_generation = false; } @@ -1144,7 +1129,7 @@ void TerraChunk::_world_transform_changed() { wt = _voxel_world->get_transform(); } - set_transform(wt * Transform(Basis(), Vector3(_position_x * static_cast(_size_x) * _voxel_scale, _position_y * static_cast(_size_y) * _voxel_scale, _position_z * static_cast(_size_z) * _voxel_scale))); + set_transform(wt * Transform(Basis(), Vector3(_position_x * static_cast(_size_x) * _voxel_scale, 0, _position_z * static_cast(_size_z) * _voxel_scale))); } /* @@ -1271,10 +1256,6 @@ void TerraChunk::_bind_methods() { ClassDB::bind_method(D_METHOD("set_position_x", "value"), &TerraChunk::set_position_x); ADD_PROPERTY(PropertyInfo(Variant::INT, "position_x"), "set_position_x", "get_position_x"); - ClassDB::bind_method(D_METHOD("get_position_y"), &TerraChunk::get_position_y); - ClassDB::bind_method(D_METHOD("set_position_y", "value"), &TerraChunk::set_position_y); - ADD_PROPERTY(PropertyInfo(Variant::INT, "position_y"), "set_position_y", "get_position_y"); - ClassDB::bind_method(D_METHOD("get_position_z"), &TerraChunk::get_position_z); ClassDB::bind_method(D_METHOD("set_position_z", "value"), &TerraChunk::set_position_z); ADD_PROPERTY(PropertyInfo(Variant::INT, "position_z"), "set_position_z", "get_position_z"); @@ -1283,10 +1264,6 @@ void TerraChunk::_bind_methods() { ClassDB::bind_method(D_METHOD("set_size_x"), &TerraChunk::set_size_x); ADD_PROPERTY(PropertyInfo(Variant::INT, "size_x"), "set_size_x", "get_size_x"); - ClassDB::bind_method(D_METHOD("get_size_y"), &TerraChunk::get_size_y); - ClassDB::bind_method(D_METHOD("set_size_y"), &TerraChunk::set_size_y); - ADD_PROPERTY(PropertyInfo(Variant::INT, "size_y"), "set_size_y", "get_size_y"); - ClassDB::bind_method(D_METHOD("get_size_z"), &TerraChunk::get_size_z); ClassDB::bind_method(D_METHOD("set_size_z"), &TerraChunk::set_size_z); ADD_PROPERTY(PropertyInfo(Variant::INT, "size_z"), "set_size_z", "get_size_z"); @@ -1295,14 +1272,14 @@ void TerraChunk::_bind_methods() { ClassDB::bind_method(D_METHOD("set_data_size_x"), &TerraChunk::set_data_size_x); ADD_PROPERTY(PropertyInfo(Variant::INT, "data_size_x"), "set_data_size_x", "get_data_size_x"); - ClassDB::bind_method(D_METHOD("get_data_size_y"), &TerraChunk::get_data_size_y); - ClassDB::bind_method(D_METHOD("set_data_size_y"), &TerraChunk::set_data_size_y); - ADD_PROPERTY(PropertyInfo(Variant::INT, "data_size_y"), "set_data_size_y", "get_data_size_y"); - ClassDB::bind_method(D_METHOD("get_data_size_z"), &TerraChunk::get_data_size_z); ClassDB::bind_method(D_METHOD("set_data_size_z"), &TerraChunk::set_data_size_z); ADD_PROPERTY(PropertyInfo(Variant::INT, "data_size_z"), "set_data_size_z", "get_data_size_z"); + ClassDB::bind_method(D_METHOD("get_world_height"), &TerraChunk::get_world_height); + ClassDB::bind_method(D_METHOD("set_world_height", "height"), &TerraChunk::set_world_height); + ADD_PROPERTY(PropertyInfo(Variant::REAL, "world_height"), "set_world_height", "get_world_height"); + ClassDB::bind_method(D_METHOD("get_position"), &TerraChunk::get_position); ClassDB::bind_method(D_METHOD("set_position", "x", "y", "z"), &TerraChunk::set_position); diff --git a/world/terra_chunk.h b/world/terra_chunk.h index f58c448..624a29f 100644 --- a/world/terra_chunk.h +++ b/world/terra_chunk.h @@ -108,33 +108,30 @@ public: int get_position_x() const; void set_position_x(const int value); - int get_position_y() const; - void set_position_y(const int value); int get_position_z() const; void set_position_z(const int value); int get_size_x() const; - int get_size_y() const; int get_size_z() const; void set_size_x(const int value); - void set_size_y(const int value); void set_size_z(const int value); int get_data_size_x() const; - int get_data_size_y() const; int get_data_size_z() const; void set_data_size_x(const int value); - void set_data_size_y(const int value); void set_data_size_z(const int value); - Vector3 get_position() const; + float get_world_height() const; + void set_world_height(const float value); + + Vector2 get_position() const; Vector3 get_size() const; - Vector3 get_world_position() const; + Vector2 get_world_position() const; Vector3 get_world_size() const; AABB get_world_aabb() const; - void set_position(const int x, const int y, const int z); + void set_position(const int x, const int z); int get_margin_start() const; int get_margin_end() const; @@ -165,12 +162,12 @@ public: //Channels void channel_setup(); - void set_size(const int size_x, const int size_y, const int size_z, const int margin_start = 0, const int margin_end = 0); + void set_size(const int size_x, const int size_z, const int margin_start = 0, const int margin_end = 0); - bool validate_data_position(const int x, const int y, const int z) const; + bool validate_data_position(const int x, const int z) const; - uint8_t get_voxel(const int p_x, const int p_y, const int p_z, const int p_index) const; - void set_voxel(const uint8_t p_value, const int p_x, const int p_y, const int p_z, const int p_index); + uint8_t get_voxel(const int p_x, const int p_z, const int p_index) const; + void set_voxel(const uint8_t p_value, const int p_x, const int p_z, const int p_index); int channel_get_count() const; void channel_set_count(const int count); @@ -190,8 +187,8 @@ public: PoolByteArray channel_get_compressed(const int channel_index) const; void channel_set_compressed(const int channel_index, const PoolByteArray &data); - int get_index(const int x, const int y, const int z) const; - int get_data_index(const int x, const int y, const int z) const; + int get_index(const int x, const int z) const; + int get_data_index(const int x, const int z) const; int get_data_size() const; //Terra Structures @@ -353,20 +350,19 @@ protected: TerraWorld *_voxel_world; int _position_x; - int _position_y; int _position_z; int _size_x; - int _size_y; int _size_z; int _data_size_x; - int _data_size_y; int _data_size_z; int _margin_start; int _margin_end; + float _world_height; + Vector _channels; float _voxel_scale; diff --git a/world/terra_world.cpp b/world/terra_world.cpp index 13aa073..e595552 100644 --- a/world/terra_world.cpp +++ b/world/terra_world.cpp @@ -57,13 +57,6 @@ void TerraWorld::set_chunk_size_x(const int value) { _chunk_size_x = value; } -int TerraWorld::get_chunk_size_y() const { - return _chunk_size_y; -} -void TerraWorld::set_chunk_size_y(const int value) { - _chunk_size_y = value; -} - int TerraWorld::get_chunk_size_z() const { return _chunk_size_z; } @@ -108,6 +101,13 @@ void TerraWorld::set_use_threads(const bool value) { } } +_FORCE_INLINE_ float TerraWorld::get_world_height() const { + return _world_height; +} +void TerraWorld::set_world_height(const float value) { + _world_height = value; +} + int TerraWorld::get_max_concurrent_generations() const { return _max_concurrent_generations; } @@ -259,16 +259,16 @@ void TerraWorld::voxel_structures_set(const Vector &structures) { } } -void TerraWorld::chunk_add(Ref chunk, const int x, const int y, const int z) { +void TerraWorld::chunk_add(Ref chunk, const int x, const int z) { ERR_FAIL_COND(!chunk.is_valid()); ERR_FAIL_COND_MSG(chunk->get_voxel_world() != NULL && chunk->get_voxel_world() != this, "Chunk is already owned by an another world!"); - IntPos pos(x, y, z); + IntPos pos(x, z); //ERR_FAIL_COND(_chunks.has(pos)); chunk->set_voxel_world(this); - chunk->set_position(x, y, z); + chunk->set_position(x, z); chunk->world_transform_changed(); if (!_chunks.has(pos)) @@ -283,19 +283,19 @@ void TerraWorld::chunk_add(Ref chunk, const int x, const int y, cons if (has_method("_chunk_added")) call("_chunk_added", chunk); } -bool TerraWorld::chunk_has(const int x, const int y, const int z) const { - return _chunks.has(IntPos(x, y, z)); +bool TerraWorld::chunk_has(const int x, const int z) const { + return _chunks.has(IntPos(x, z)); } -Ref TerraWorld::chunk_get(const int x, const int y, const int z) { - IntPos pos(x, y, z); +Ref TerraWorld::chunk_get(const int x, const int z) { + IntPos pos(x, z); if (_chunks.has(pos)) return _chunks.get(pos); return Ref(); } -Ref TerraWorld::chunk_remove(const int x, const int y, const int z) { - IntPos pos(x, y, z); +Ref TerraWorld::chunk_remove(const int x, const int z) { + IntPos pos(x, z); if (!_chunks.has(pos)) return NULL; @@ -322,7 +322,7 @@ Ref TerraWorld::chunk_remove_index(const int index) { Ref chunk = _chunks_vector.get(index); _chunks_vector.remove(index); - _chunks.erase(IntPos(chunk->get_position_x(), chunk->get_position_y(), chunk->get_position_z())); + _chunks.erase(IntPos(chunk->get_position_x(), chunk->get_position_z())); chunk->exit_tree(); return chunk; @@ -350,18 +350,18 @@ void TerraWorld::chunks_clear() { _generating.clear(); } -Ref TerraWorld::chunk_get_or_create(int x, int y, int z) { - Ref chunk = chunk_get(x, y, z); +Ref TerraWorld::chunk_get_or_create(int x, int z) { + Ref chunk = chunk_get(x, z); if (!chunk.is_valid()) { - chunk = chunk_create(x, y, z); + chunk = chunk_create(x, z); } return chunk; } -Ref TerraWorld::chunk_create(const int x, const int y, const int z) { - Ref c = call("_create_chunk", x, y, z, Ref()); +Ref TerraWorld::chunk_create(const int x, const int z) { + Ref c = call("_create_chunk", x, z, Ref()); generation_queue_add_to(c); @@ -371,10 +371,10 @@ Ref TerraWorld::chunk_create(const int x, const int y, const int z) void TerraWorld::chunk_setup(Ref chunk) { ERR_FAIL_COND(!chunk.is_valid()); - call("_create_chunk", chunk->get_position_x(), chunk->get_position_y(), chunk->get_position_z(), chunk); + call("_create_chunk", chunk->get_position_x(), chunk->get_position_z(), chunk); } -Ref TerraWorld::_create_chunk(const int x, const int y, const int z, Ref chunk) { +Ref TerraWorld::_create_chunk(const int x, const int z, Ref chunk) { if (!chunk.is_valid()) { chunk.instance(); } @@ -383,7 +383,7 @@ Ref TerraWorld::_create_chunk(const int x, const int y, const int z, ERR_FAIL_COND_V(!chunk.is_valid(), NULL); - chunk->set_name("Chunk[" + String::num(x) + "," + String::num(y) + "," + String::num(z) + "]"); + chunk->set_name("Chunk[" + String::num(x) + "," + String::num(z) + "]"); chunk->set_voxel_world(this); @@ -391,13 +391,14 @@ Ref TerraWorld::_create_chunk(const int x, const int y, const int z, if (chunk->has_method("set_is_build_threaded")) chunk->call("set_is_build_threaded", _use_threads); - chunk->set_position(x, y, z); + chunk->set_position(x, z); + chunk->set_world_height(_world_height); chunk->set_library(_library); chunk->set_voxel_scale(_voxel_scale); - chunk->set_size(_chunk_size_x, _chunk_size_y, _chunk_size_z, _data_margin_start, _data_margin_end); + chunk->set_size(_chunk_size_x, _chunk_size_z, _data_margin_start, _data_margin_end); //chunk->set_translation(Vector3(x * _chunk_size_x * _voxel_scale, y * _chunk_size_y * _voxel_scale, z * _chunk_size_z * _voxel_scale)); - chunk_add(chunk, x, y, z); + chunk_add(chunk, x, z); return chunk; } @@ -440,7 +441,7 @@ void TerraWorld::chunks_set(const Vector &chunks) { if (_chunks_vector.find(chunk) != -1) continue; - chunk_add(chunk, chunk->get_position_x(), chunk->get_position_y(), chunk->get_position_z()); + chunk_add(chunk, chunk->get_position_x(), chunk->get_position_z()); } } else { _chunks_vector.clear(); @@ -487,10 +488,9 @@ bool TerraWorld::can_chunk_do_build_step() { bool TerraWorld::is_position_walkable(const Vector3 &p_pos) { int x = static_cast(Math::floor(p_pos.x / (_chunk_size_x * _voxel_scale))); - int y = static_cast(Math::floor(p_pos.y / (_chunk_size_y * _voxel_scale))); int z = static_cast(Math::floor(p_pos.z / (_chunk_size_z * _voxel_scale))); - Ref c = chunk_get(x, y, z); + Ref c = chunk_get(x, z); if (!c.is_valid()) return false; @@ -705,29 +705,23 @@ uint8_t TerraWorld::get_voxel_at_world_position(const Vector3 &world_position, c //Note: floor is needed to handle negative numbers properly int x = static_cast(Math::floor(pos.x / get_chunk_size_x())); - int y = static_cast(Math::floor(pos.y / get_chunk_size_y())); int z = static_cast(Math::floor(pos.z / get_chunk_size_z())); int bx = static_cast(Math::floor(pos.x)) % get_chunk_size_x(); - int by = static_cast(Math::floor(pos.y)) % get_chunk_size_y(); int bz = static_cast(Math::floor(pos.z)) % get_chunk_size_z(); if (bx < 0) { bx += get_chunk_size_x(); } - if (by < 0) { - by += get_chunk_size_y(); - } - if (bz < 0) { bz += get_chunk_size_z(); } - Ref chunk = chunk_get(x, y, z); + Ref chunk = chunk_get(x, z); if (chunk.is_valid()) - return chunk->get_voxel(bx, by, bz, channel_index); + return chunk->get_voxel(bx, bz, channel_index); return 0; } @@ -737,45 +731,31 @@ void TerraWorld::set_voxel_at_world_position(const Vector3 &world_position, cons //Note: floor is needed to handle negative numbers properly int x = static_cast(Math::floor(pos.x / get_chunk_size_x())); - int y = static_cast(Math::floor(pos.y / get_chunk_size_y())); int z = static_cast(Math::floor(pos.z / get_chunk_size_z())); int bx = static_cast(Math::floor(pos.x)) % get_chunk_size_x(); - int by = static_cast(Math::floor(pos.y)) % get_chunk_size_y(); int bz = static_cast(Math::floor(pos.z)) % get_chunk_size_z(); if (bx < 0) { bx += get_chunk_size_x(); } - if (by < 0) { - by += get_chunk_size_y(); - } - if (bz < 0) { bz += get_chunk_size_z(); } if (get_data_margin_end() > 0) { if (bx == 0) { - Ref chunk = chunk_get_or_create(x - 1, y, z); - chunk->set_voxel(data, get_chunk_size_x(), by, bz, channel_index); - - if (rebuild) - chunk->build(); - } - - if (by == 0) { - Ref chunk = chunk_get_or_create(x, y - 1, z); - chunk->set_voxel(data, bx, get_chunk_size_y(), bz, channel_index); + Ref chunk = chunk_get_or_create(x - 1, z); + chunk->set_voxel(data, get_chunk_size_x(), bz, channel_index); if (rebuild) chunk->build(); } if (bz == 0) { - Ref chunk = chunk_get_or_create(x, y, z - 1); - chunk->set_voxel(data, bx, by, get_chunk_size_z(), channel_index); + Ref chunk = chunk_get_or_create(x, z - 1); + chunk->set_voxel(data, bx, get_chunk_size_z(), channel_index); if (rebuild) chunk->build(); @@ -784,32 +764,24 @@ void TerraWorld::set_voxel_at_world_position(const Vector3 &world_position, cons if (get_data_margin_start() > 0) { if (bx == get_chunk_size_x() - 1) { - Ref chunk = chunk_get_or_create(x + 1, y, z); - chunk->set_voxel(data, -1, by, bz, channel_index); - - if (rebuild) - chunk->build(); - } - - if (by == get_chunk_size_y() - 1) { - Ref chunk = chunk_get_or_create(x, y + 1, z); - chunk->set_voxel(data, bx, -1, bz, channel_index); + Ref chunk = chunk_get_or_create(x + 1, z); + chunk->set_voxel(data, -1, bz, channel_index); if (rebuild) chunk->build(); } if (bz == get_chunk_size_z() - 1) { - Ref chunk = chunk_get_or_create(x, y, z + 1); - chunk->set_voxel(data, bx, by, -1, channel_index); + Ref chunk = chunk_get_or_create(x, z + 1); + chunk->set_voxel(data, bx, -1, channel_index); if (rebuild) chunk->build(); } } - Ref chunk = chunk_get_or_create(x, y, z); - chunk->set_voxel(data, bx, by, bz, channel_index); + Ref chunk = chunk_get_or_create(x, z); + chunk->set_voxel(data, bx, bz, channel_index); if (rebuild) chunk->build(); @@ -820,20 +792,18 @@ Ref TerraWorld::get_chunk_at_world_position(const Vector3 &world_pos //Note: floor is needed to handle negative numbers proiberly int x = static_cast(Math::floor(pos.x / get_chunk_size_x())); - int y = static_cast(Math::floor(pos.y / get_chunk_size_y())); int z = static_cast(Math::floor(pos.z / get_chunk_size_z())); - return chunk_get(x, y, z); + return chunk_get(x, z); } Ref TerraWorld::get_or_create_chunk_at_world_position(const Vector3 &world_position) { Vector3 pos = world_position / get_voxel_scale(); //Note: floor is needed to handle negative numbers proiberly int x = static_cast(Math::floor(pos.x / get_chunk_size_x())); - int y = static_cast(Math::floor(pos.y / get_chunk_size_y())); int z = static_cast(Math::floor(pos.z / get_chunk_size_z())); - return chunk_get_or_create(x, y, z); + return chunk_get_or_create(x, z); } void TerraWorld::set_voxel_with_tool(const bool mode_add, const Vector3 hit_position, const Vector3 hit_normal, const int selected_voxel, const int isolevel) { @@ -850,11 +820,11 @@ TerraWorld::TerraWorld() { _is_priority_generation = true; _chunk_size_x = 16; - _chunk_size_y = 16; _chunk_size_z = 16; _current_seed = 0; _data_margin_start = 0; _data_margin_end = 0; + _world_height = 256; set_use_threads(true); set_max_concurrent_generations(3); @@ -897,7 +867,7 @@ void TerraWorld::_generate_chunk(Ref chunk) { continue; if (structure->get_use_aabb()) { - if (structure->get_chunk_aabb().has_point(Vector3(chunk->get_position_x(), chunk->get_position_y(), chunk->get_position_z()))) + if (structure->get_chunk_aabb().has_point(Vector3(chunk->get_position_x(), 1, chunk->get_position_z()))) structure->write_to_chunk(chunk); } else { structure->write_to_chunk(chunk); @@ -1038,10 +1008,6 @@ void TerraWorld::_bind_methods() { ClassDB::bind_method(D_METHOD("set_chunk_size_x", "value"), &TerraWorld::set_chunk_size_x); ADD_PROPERTY(PropertyInfo(Variant::INT, "chunk_size_x"), "set_chunk_size_x", "get_chunk_size_x"); - ClassDB::bind_method(D_METHOD("get_chunk_size_y"), &TerraWorld::get_chunk_size_y); - ClassDB::bind_method(D_METHOD("set_chunk_size_y", "value"), &TerraWorld::set_chunk_size_y); - ADD_PROPERTY(PropertyInfo(Variant::INT, "chunk_size_y"), "set_chunk_size_y", "get_chunk_size_y"); - ClassDB::bind_method(D_METHOD("get_chunk_size_z"), &TerraWorld::get_chunk_size_z); ClassDB::bind_method(D_METHOD("set_chunk_size_z", "value"), &TerraWorld::set_chunk_size_z); ADD_PROPERTY(PropertyInfo(Variant::INT, "chunk_size_z"), "set_chunk_size_z", "get_chunk_size_z"); @@ -1054,6 +1020,10 @@ void TerraWorld::_bind_methods() { ClassDB::bind_method(D_METHOD("set_data_margin_end", "value"), &TerraWorld::set_data_margin_end); ADD_PROPERTY(PropertyInfo(Variant::INT, "data_margin_end"), "set_data_margin_end", "get_data_margin_end"); + ClassDB::bind_method(D_METHOD("get_world_height"), &TerraChunk::get_world_height); + ClassDB::bind_method(D_METHOD("set_world_height", "height"), &TerraChunk::set_world_height); + ADD_PROPERTY(PropertyInfo(Variant::REAL, "world_height"), "set_world_height", "get_world_height"); + ClassDB::bind_method(D_METHOD("get_current_seed"), &TerraWorld::get_current_seed); ClassDB::bind_method(D_METHOD("set_current_seed", "value"), &TerraWorld::set_current_seed); ADD_PROPERTY(PropertyInfo(Variant::INT, "current_seed"), "set_current_seed", "get_current_seed"); @@ -1114,10 +1084,10 @@ void TerraWorld::_bind_methods() { BIND_VMETHOD(MethodInfo("_chunk_added", PropertyInfo(Variant::OBJECT, "chunk", PROPERTY_HINT_RESOURCE_TYPE, "TerraChunk"))); - ClassDB::bind_method(D_METHOD("chunk_add", "chunk", "x", "y", "z"), &TerraWorld::chunk_add); - ClassDB::bind_method(D_METHOD("chunk_has", "x", "y", "z"), &TerraWorld::chunk_has); - ClassDB::bind_method(D_METHOD("chunk_get", "x", "y", "z"), &TerraWorld::chunk_get); - ClassDB::bind_method(D_METHOD("chunk_remove", "x", "y", "z"), &TerraWorld::chunk_remove); + ClassDB::bind_method(D_METHOD("chunk_add", "chunk", "x", "z"), &TerraWorld::chunk_add); + ClassDB::bind_method(D_METHOD("chunk_has", "x", "z"), &TerraWorld::chunk_has); + ClassDB::bind_method(D_METHOD("chunk_get", "x", "z"), &TerraWorld::chunk_get); + ClassDB::bind_method(D_METHOD("chunk_remove", "x", "z"), &TerraWorld::chunk_remove); ClassDB::bind_method(D_METHOD("chunk_remove_index", "index"), &TerraWorld::chunk_remove_index); ClassDB::bind_method(D_METHOD("chunk_get_index", "index"), &TerraWorld::chunk_get_index); @@ -1146,11 +1116,11 @@ void TerraWorld::_bind_methods() { BIND_VMETHOD(MethodInfo("_prepare_chunk_for_generation", PropertyInfo(Variant::OBJECT, "chunk", PROPERTY_HINT_RESOURCE_TYPE, "TerraChunk"))); BIND_VMETHOD(MethodInfo("_generate_chunk", PropertyInfo(Variant::OBJECT, "chunk", PROPERTY_HINT_RESOURCE_TYPE, "TerraChunk"))); - ClassDB::bind_method(D_METHOD("chunk_get_or_create", "x", "y", "z"), &TerraWorld::chunk_get_or_create); - ClassDB::bind_method(D_METHOD("chunk_create", "x", "y", "z"), &TerraWorld::chunk_create); + ClassDB::bind_method(D_METHOD("chunk_get_or_create", "x", "z"), &TerraWorld::chunk_get_or_create); + ClassDB::bind_method(D_METHOD("chunk_create", "x", "z"), &TerraWorld::chunk_create); ClassDB::bind_method(D_METHOD("chunk_setup", "chunk"), &TerraWorld::chunk_setup); - ClassDB::bind_method(D_METHOD("_create_chunk", "x", "y", "z", "chunk"), &TerraWorld::_create_chunk); + ClassDB::bind_method(D_METHOD("_create_chunk", "x", "z", "chunk"), &TerraWorld::_create_chunk); ClassDB::bind_method(D_METHOD("_generate_chunk", "chunk"), &TerraWorld::_generate_chunk); ClassDB::bind_method(D_METHOD("can_chunk_do_build_step"), &TerraWorld::can_chunk_do_build_step); diff --git a/world/terra_world.h b/world/terra_world.h index fa280b2..0e5a315 100644 --- a/world/terra_world.h +++ b/world/terra_world.h @@ -70,9 +70,6 @@ public: int get_chunk_size_x() const; void set_chunk_size_x(const int value); - int get_chunk_size_y() const; - void set_chunk_size_y(const int value); - int get_chunk_size_z() const; void set_chunk_size_z(const int value); @@ -88,6 +85,9 @@ public: bool get_use_threads() const; void set_use_threads(const bool value); + float get_world_height() const; + void set_world_height(const float value); + int get_max_concurrent_generations() const; void set_max_concurrent_generations(const int value); @@ -133,10 +133,10 @@ public: void voxel_structures_set(const Vector &structures); //Chunks - void chunk_add(Ref chunk, const int x, const int y, const int z); - bool chunk_has(const int x, const int y, const int z) const; - Ref chunk_get(const int x, const int y, const int z); - Ref chunk_remove(const int x, const int y, const int z); + void chunk_add(Ref chunk, const int x, const int z); + bool chunk_has(const int x, const int z) const; + Ref chunk_get(const int x, const int z); + Ref chunk_remove(const int x, const int z); Ref chunk_remove_index(const int index); Ref chunk_get_index(const int index); @@ -144,8 +144,8 @@ public: void chunks_clear(); - Ref chunk_get_or_create(const int x, const int y, const int z); - Ref chunk_create(const int x, const int y, const int z); + Ref chunk_get_or_create(const int x, const int z); + Ref chunk_create(const int x, const int z); void chunk_setup(Ref chunk); void chunk_generate(Ref chunk); @@ -196,7 +196,7 @@ public: protected: virtual void _generate_chunk(Ref chunk); - virtual Ref _create_chunk(int x, int y, int z, Ref p_chunk); + virtual Ref _create_chunk(int x, int z, Ref p_chunk); virtual int _get_channel_index_info(const ChannelTypeInfo channel_type); virtual void _set_voxel_with_tool(const bool mode_add, const Vector3 hit_position, const Vector3 hit_normal, const int selected_voxel, const int isolevel); @@ -211,20 +211,17 @@ public: IntPos() { x = 0; - y = 0; z = 0; } - IntPos(int p_x, int p_y, int p_z) { + IntPos(int p_x, int p_z) { x = p_x; - y = p_y; z = p_z; } - IntPos(const Vector3 &p) { + IntPos(const Vector2 &p) { x = p.x; - y = p.y; - z = p.z; + z = p.y; } }; @@ -242,11 +239,11 @@ private: bool _is_priority_generation; int _chunk_size_x; - int _chunk_size_y; int _chunk_size_z; int _current_seed; int _data_margin_start; int _data_margin_end; + float _world_height; Ref _library; Ref _level_generator; @@ -274,7 +271,7 @@ private: }; _FORCE_INLINE_ bool operator==(const TerraWorld::IntPos &a, const TerraWorld::IntPos &b) { - return a.x == b.x && a.y == b.y && a.z == b.z; + return a.x == b.x && a.z == b.z; } VARIANT_ENUM_CAST(TerraWorld::ChannelTypeInfo);