From f07ddb37e32630eb2f46c001e0eab990bbce3c04 Mon Sep 17 00:00:00 2001 From: Marc Gilleron Date: Sun, 9 Apr 2017 20:52:39 +0200 Subject: [PATCH] Voxel raycast now considers smooth voxels too --- voxel_mesher.cpp | 1 - voxel_terrain.cpp | 36 +++++++++++++++++++++++++----------- voxel_terrain.h | 1 + 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/voxel_mesher.cpp b/voxel_mesher.cpp index 90a03b2..86af204 100644 --- a/voxel_mesher.cpp +++ b/voxel_mesher.cpp @@ -1,6 +1,5 @@ #include "voxel_mesher.h" #include "voxel_library.h" -#include "voxel_mesher_smooth.h" // The following tables respect the following conventions // diff --git a/voxel_terrain.cpp b/voxel_terrain.cpp index 2008be6..bae19c3 100644 --- a/voxel_terrain.cpp +++ b/voxel_terrain.cpp @@ -423,24 +423,36 @@ void VoxelTerrain::update_block_mesh(Vector3i block_pos) { // } //} -static bool _raycast_binding_predicate(Vector3i pos, void *context) { +struct _VoxelTerrainRaycastContext { + VoxelTerrain & terrain; + //unsigned int channel_mask; +}; - ERR_FAIL_COND_V(context == NULL, false); - VoxelTerrain * terrain = (VoxelTerrain*)context; +static bool _raycast_binding_predicate(Vector3i pos, void *context_ptr) { - Ref lib_ref = terrain->get_voxel_library(); + ERR_FAIL_COND_V(context_ptr == NULL, false); + _VoxelTerrainRaycastContext * context = (_VoxelTerrainRaycastContext*)context_ptr; + VoxelTerrain & terrain = context->terrain; + + //unsigned int channel = context->channel; + + Ref map = terrain.get_map(); + int v0 = map->get_voxel(pos, Voxel::CHANNEL_TYPE); + + Ref lib_ref = terrain.get_voxel_library(); if(lib_ref.is_null()) return false; const VoxelLibrary & lib = **lib_ref; - Ref map = terrain->get_map(); - // TODO In the future we may want to query more channels - int v = map->get_voxel(pos, 0); - if(lib.has_voxel(v) == false) + if(lib.has_voxel(v0) == false) return false; - const Voxel & voxel = lib.get_voxel_const(v); - return !voxel.is_transparent(); + const Voxel & voxel = lib.get_voxel_const(v0); + if(voxel.is_transparent() == false) + return true; + + int v1 = map->get_voxel(pos, Voxel::CHANNEL_ISOLEVEL); + return v1 - 128 >= 0; } Variant VoxelTerrain::_raycast_binding(Vector3 origin, Vector3 direction, real_t max_distance) { @@ -450,7 +462,9 @@ Variant VoxelTerrain::_raycast_binding(Vector3 origin, Vector3 direction, real_t Vector3i hit_pos; Vector3i prev_pos; - if(voxel_raycast(origin, direction, _raycast_binding_predicate, this, max_distance, hit_pos, prev_pos)) { + _VoxelTerrainRaycastContext context = { *this }; + + if(voxel_raycast(origin, direction, _raycast_binding_predicate, &context, max_distance, hit_pos, prev_pos)) { Dictionary hit = Dictionary(); hit["position"] = hit_pos.to_vec3(); diff --git a/voxel_terrain.h b/voxel_terrain.h index 28743bf..491d72f 100644 --- a/voxel_terrain.h +++ b/voxel_terrain.h @@ -4,6 +4,7 @@ #include #include "voxel_map.h" #include "voxel_mesher.h" +#include "voxel_mesher_smooth.h" #include "voxel_provider.h" #include "zprofiling.h"