From 033fe750c078c91aaf15d3b37aa317a6e57ee1a6 Mon Sep 17 00:00:00 2001 From: Relintai Date: Wed, 6 Nov 2019 17:43:51 +0100 Subject: [PATCH] Updated the color baking algorithm, and restructured build phases. --- meshers/voxel_mesher.cpp | 21 +++++++++++++-------- world/voxel_chunk.cpp | 35 +++++++++++++++++++++++++---------- world/voxel_chunk.h | 13 +++++++------ 3 files changed, 45 insertions(+), 24 deletions(-) diff --git a/meshers/voxel_mesher.cpp b/meshers/voxel_mesher.cpp index b69ac3f..e48a0fe 100644 --- a/meshers/voxel_mesher.cpp +++ b/meshers/voxel_mesher.cpp @@ -279,17 +279,22 @@ void VoxelMesher::_bake_colors(Ref buffer) { unsigned int z = (unsigned int)(vert.z / _voxel_scale); if (buffer->validate_pos(x, y, z)) { - int ao = buffer->get_voxel(x, y, z, VoxelBuffer::CHANNEL_AO); - Color light = Color(buffer->get_voxel(x, y, z, VoxelBuffer::CHANNEL_LIGHT_COLOR_R) / 255.0, buffer->get_voxel(x, y, z, VoxelBuffer::CHANNEL_LIGHT_COLOR_G) / 255.0, buffer->get_voxel(x, y, z, VoxelBuffer::CHANNEL_LIGHT_COLOR_B) / 255.0); - Color ao_color(ao, ao, ao); + Color light = Color( + buffer->get_voxel(x, y, z, VoxelBuffer::CHANNEL_LIGHT_COLOR_R) / 255.0, + buffer->get_voxel(x, y, z, VoxelBuffer::CHANNEL_LIGHT_COLOR_G) / 255.0, + buffer->get_voxel(x, y, z, VoxelBuffer::CHANNEL_LIGHT_COLOR_B) / 255.0); - light += base_light; + int ao = (buffer->get_voxel(x, y, z, VoxelBuffer::CHANNEL_AO) / 255.0) * _ao_strength; + int rao = buffer->get_voxel(x, y, z, VoxelBuffer::CHANNEL_RANDOM_AO) / 255.0; + ao += rao; - float NdotL = CLAMP(_normals[i].dot(vert - Vector3(x, y, z)), 0, 1.0); + light.r += _base_light_value; + light.g += _base_light_value; + light.b += _base_light_value; - light *= NdotL; - - light -= ao_color * _ao_strength; + light.r -= ao; + light.g -= ao; + light.b -= ao; light.r = CLAMP(light.r, 0, 1.0); light.g = CLAMP(light.g, 0, 1.0); diff --git a/world/voxel_chunk.cpp b/world/voxel_chunk.cpp index adc9513..afb7ea3 100644 --- a/world/voxel_chunk.cpp +++ b/world/voxel_chunk.cpp @@ -216,22 +216,20 @@ void VoxelChunk::_build_phase(int phase) { return; } - case BUILD_PHASE_TERRARIN_MESH: { - + case BUILD_PHASE_TERRARIN_MESH_SETUP: { if (has_method("_create_mesh")) { call("_create_mesh"); } else { _mesher->add_buffer(_buffer); } - finalize_mesh(); + //finalize_mesh(); next_phase(); return; } case BUILD_PHASE_TERRARIN_MESH_COLLIDER: { - if (get_create_collider()) { build_collider(); } @@ -240,6 +238,15 @@ void VoxelChunk::_build_phase(int phase) { return; } + case BUILD_PHASE_TERRARIN_MESH: { + _mesher->bake_colors(_buffer); + + finalize_mesh(); + + next_phase(); + + return; + } case BUILD_PHASE_PROP_MESH: { _mesher->reset(); @@ -578,14 +585,17 @@ void VoxelChunk::create_debug_immediate_geometry() { _debug_drawer = memnew(ImmediateGeometry()); - get_voxel_world()->add_child(_debug_drawer); - _debug_drawer->set_owner(get_voxel_world()); + add_child(_debug_drawer); - _debug_drawer->set_transform(Transform(Basis(), Vector3(_chunk_position.x * _chunk_size.x * _voxel_scale, _chunk_position.y * _chunk_size.y * _voxel_scale, _chunk_position.z * _chunk_size.z * _voxel_scale))); + if (Engine::get_singleton()->is_editor_hint()) + _debug_drawer->set_owner(get_tree()->get_edited_scene_root()); + + //_debug_drawer->set_transform(Transform(Basis(), Vector3(_chunk_position.x * _chunk_size.x * _voxel_scale, _chunk_position.y * _chunk_size.y * _voxel_scale, _chunk_position.z * _chunk_size.z * _voxel_scale))); + //_debug_drawer->set_transform(Transform(Basis(), Vector3(_chunk_position.x * _chunk_size.x * _voxel_scale, _chunk_position.y * _chunk_size.y * _voxel_scale, _chunk_position.z * _chunk_size.z * _voxel_scale))); } void VoxelChunk::free_debug_immediate_geometry() { - if (_debug_drawer != NULL) { + if (ObjectDB::instance_validate(_debug_drawer)) { _debug_drawer->queue_delete(); _debug_drawer = NULL; @@ -679,11 +689,13 @@ void VoxelChunk::draw_debug_voxel_lights() { draw_cross_voxels_fill(Vector3(pos_x, pos_y, pos_z), 1.0); } + if (has_method("_draw_debug_voxel_lights")) + call("_draw_debug_voxel_lights", _debug_drawer); + _debug_drawer->end(); } void VoxelChunk::free_chunk() { - free_debug_immediate_geometry(); free_main_mesh(); remove_colliders(); free_prop_mesh(); @@ -871,6 +883,8 @@ void VoxelChunk::_bind_methods() { ClassDB::bind_method(D_METHOD("free_chunk"), &VoxelChunk::free_chunk); + BIND_VMETHOD(MethodInfo("_draw_debug_voxel_lights", PropertyInfo(Variant::OBJECT, "debug_drawer", PROPERTY_HINT_RESOURCE_TYPE, "ImmediateGeometry"))); + ClassDB::bind_method(D_METHOD("draw_cross_voxels", "pos"), &VoxelChunk::draw_cross_voxels); ClassDB::bind_method(D_METHOD("draw_cross_voxels_fill", "pos", "fill"), &VoxelChunk::draw_cross_voxels_fill); ClassDB::bind_method(D_METHOD("draw_debug_voxels", "pos", "color"), &VoxelChunk::draw_debug_voxels, DEFVAL(Color(1, 1, 1))); @@ -879,9 +893,10 @@ void VoxelChunk::_bind_methods() { BIND_CONSTANT(BUILD_PHASE_DONE); BIND_CONSTANT(BUILD_PHASE_SETUP); + BIND_CONSTANT(BUILD_PHASE_TERRARIN_MESH_SETUP); + BIND_CONSTANT(BUILD_PHASE_TERRARIN_MESH_COLLIDER); BIND_CONSTANT(BUILD_PHASE_LIGHTS); BIND_CONSTANT(BUILD_PHASE_TERRARIN_MESH); - BIND_CONSTANT(BUILD_PHASE_TERRARIN_MESH_COLLIDER); BIND_CONSTANT(BUILD_PHASE_PROP_MESH); BIND_CONSTANT(BUILD_PHASE_PROP_COLLIDER); BIND_CONSTANT(BUILD_PHASE_MAX); diff --git a/world/voxel_chunk.h b/world/voxel_chunk.h index 9edcafc..d91ac57 100644 --- a/world/voxel_chunk.h +++ b/world/voxel_chunk.h @@ -172,12 +172,13 @@ public: enum { BUILD_PHASE_DONE = 0, BUILD_PHASE_SETUP = 1, - BUILD_PHASE_LIGHTS = 2, - BUILD_PHASE_TERRARIN_MESH = 3, - BUILD_PHASE_TERRARIN_MESH_COLLIDER = 4, - BUILD_PHASE_PROP_MESH = 5, - BUILD_PHASE_PROP_COLLIDER = 6, - BUILD_PHASE_MAX = 7 + BUILD_PHASE_TERRARIN_MESH_SETUP = 2, + BUILD_PHASE_TERRARIN_MESH_COLLIDER = 3, + BUILD_PHASE_LIGHTS = 4, + BUILD_PHASE_TERRARIN_MESH = 5, + BUILD_PHASE_PROP_MESH = 6, + BUILD_PHASE_PROP_COLLIDER = 7, + BUILD_PHASE_MAX = 8 }; protected: