Updated the color baking algorithm, and restructured build phases.

This commit is contained in:
Relintai 2019-11-06 17:43:51 +01:00
parent f23f3122a4
commit 033fe750c0
3 changed files with 45 additions and 24 deletions

View File

@ -279,17 +279,22 @@ void VoxelMesher::_bake_colors(Ref<VoxelBuffer> buffer) {
unsigned int z = (unsigned int)(vert.z / _voxel_scale); unsigned int z = (unsigned int)(vert.z / _voxel_scale);
if (buffer->validate_pos(x, y, z)) { if (buffer->validate_pos(x, y, z)) {
int ao = buffer->get_voxel(x, y, z, VoxelBuffer::CHANNEL_AO); Color light = Color(
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); buffer->get_voxel(x, y, z, VoxelBuffer::CHANNEL_LIGHT_COLOR_R) / 255.0,
Color ao_color(ao, ao, ao); 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.r -= ao;
light.g -= ao;
light -= ao_color * _ao_strength; light.b -= ao;
light.r = CLAMP(light.r, 0, 1.0); light.r = CLAMP(light.r, 0, 1.0);
light.g = CLAMP(light.g, 0, 1.0); light.g = CLAMP(light.g, 0, 1.0);

View File

@ -216,22 +216,20 @@ void VoxelChunk::_build_phase(int phase) {
return; return;
} }
case BUILD_PHASE_TERRARIN_MESH: { case BUILD_PHASE_TERRARIN_MESH_SETUP: {
if (has_method("_create_mesh")) { if (has_method("_create_mesh")) {
call("_create_mesh"); call("_create_mesh");
} else { } else {
_mesher->add_buffer(_buffer); _mesher->add_buffer(_buffer);
} }
finalize_mesh(); //finalize_mesh();
next_phase(); next_phase();
return; return;
} }
case BUILD_PHASE_TERRARIN_MESH_COLLIDER: { case BUILD_PHASE_TERRARIN_MESH_COLLIDER: {
if (get_create_collider()) { if (get_create_collider()) {
build_collider(); build_collider();
} }
@ -240,6 +238,15 @@ void VoxelChunk::_build_phase(int phase) {
return; return;
} }
case BUILD_PHASE_TERRARIN_MESH: {
_mesher->bake_colors(_buffer);
finalize_mesh();
next_phase();
return;
}
case BUILD_PHASE_PROP_MESH: { case BUILD_PHASE_PROP_MESH: {
_mesher->reset(); _mesher->reset();
@ -578,14 +585,17 @@ void VoxelChunk::create_debug_immediate_geometry() {
_debug_drawer = memnew(ImmediateGeometry()); _debug_drawer = memnew(ImmediateGeometry());
get_voxel_world()->add_child(_debug_drawer); add_child(_debug_drawer);
_debug_drawer->set_owner(get_voxel_world());
_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() { void VoxelChunk::free_debug_immediate_geometry() {
if (_debug_drawer != NULL) { if (ObjectDB::instance_validate(_debug_drawer)) {
_debug_drawer->queue_delete(); _debug_drawer->queue_delete();
_debug_drawer = NULL; _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); 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(); _debug_drawer->end();
} }
void VoxelChunk::free_chunk() { void VoxelChunk::free_chunk() {
free_debug_immediate_geometry();
free_main_mesh(); free_main_mesh();
remove_colliders(); remove_colliders();
free_prop_mesh(); free_prop_mesh();
@ -871,6 +883,8 @@ void VoxelChunk::_bind_methods() {
ClassDB::bind_method(D_METHOD("free_chunk"), &VoxelChunk::free_chunk); 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", "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_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))); 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_DONE);
BIND_CONSTANT(BUILD_PHASE_SETUP); 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_LIGHTS);
BIND_CONSTANT(BUILD_PHASE_TERRARIN_MESH); BIND_CONSTANT(BUILD_PHASE_TERRARIN_MESH);
BIND_CONSTANT(BUILD_PHASE_TERRARIN_MESH_COLLIDER);
BIND_CONSTANT(BUILD_PHASE_PROP_MESH); BIND_CONSTANT(BUILD_PHASE_PROP_MESH);
BIND_CONSTANT(BUILD_PHASE_PROP_COLLIDER); BIND_CONSTANT(BUILD_PHASE_PROP_COLLIDER);
BIND_CONSTANT(BUILD_PHASE_MAX); BIND_CONSTANT(BUILD_PHASE_MAX);

View File

@ -172,12 +172,13 @@ public:
enum { enum {
BUILD_PHASE_DONE = 0, BUILD_PHASE_DONE = 0,
BUILD_PHASE_SETUP = 1, BUILD_PHASE_SETUP = 1,
BUILD_PHASE_LIGHTS = 2, BUILD_PHASE_TERRARIN_MESH_SETUP = 2,
BUILD_PHASE_TERRARIN_MESH = 3, BUILD_PHASE_TERRARIN_MESH_COLLIDER = 3,
BUILD_PHASE_TERRARIN_MESH_COLLIDER = 4, BUILD_PHASE_LIGHTS = 4,
BUILD_PHASE_PROP_MESH = 5, BUILD_PHASE_TERRARIN_MESH = 5,
BUILD_PHASE_PROP_COLLIDER = 6, BUILD_PHASE_PROP_MESH = 6,
BUILD_PHASE_MAX = 7 BUILD_PHASE_PROP_COLLIDER = 7,
BUILD_PHASE_MAX = 8
}; };
protected: protected: