Skip to content

Commit

Permalink
[rendering] fixed a couple of issues with transparent rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
PanosK92 committed Feb 27, 2025
1 parent 6bb59a3 commit 8cb91b4
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 22 deletions.
2 changes: 1 addition & 1 deletion data/shaders/common.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ static const float3 hemisphere_samples[64] =
float get_alpha_threshold(float3 position_world)
{
static const float ALPHA_THRESHOLD_DEFAULT = 0.6f;
static const float ALPHA_MAX_DISTANCE_SQ = 350.0f * 350.0f;
static const float ALPHA_MAX_DISTANCE_SQ = 200.0f * 200.0f;

// beyond max distance, no alpha testing (threshold = 0)
float3 offset = position_world - buffer_frame.camera_position;
Expand Down
2 changes: 0 additions & 2 deletions data/shaders/common_structs.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ struct Surface
bool is_grass_blade() { return flags & uint(1U << 10); }
bool vertex_animate_water() { return flags & uint(1U << 11); }
bool is_tessellated() { return flags & uint(1U << 12); }
bool is_water() { return ior == 1.33f; }
bool is_glass() { return ior == 1.52f; }
bool is_sky() { return alpha == 0.0f; }
bool is_opaque() { return alpha == 1.0f; }
bool is_transparent() { return alpha > 0.0f && alpha < 1.0f && ior != 1.0f; } // the ior is to avoid treating alpha tested objects as transparent
Expand Down
14 changes: 7 additions & 7 deletions data/shaders/g_buffer.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ struct gbuffer
float2 velocity : SV_Target3;
};

static float4 sample_texture(float3 position, float3 normal, float2 uv, uint texture_index, bool is_water, bool is_terrain)
static float4 sample_texture(float3 position, float3 normal, float2 uv, uint texture_index, Surface surface)
{
// parameters
const float sand_offset = 0.75f;
Expand All @@ -43,16 +43,16 @@ static float4 sample_texture(float3 position, float3 normal, float2 uv, uint tex
float4 base_color = GET_TEXTURE(texture_index).Sample(GET_SAMPLER(sampler_anisotropic_wrap), uv);

float4 color = 0.0f;
if (is_water)
if (surface.vertex_animate_water())
{
// sample with different UVs
// interleave normals
float2 uv_interleaved_1 = uv + (float)buffer_frame.time * speed_1 * direction_1;
float2 uv_interleaved_2 = uv + (float)buffer_frame.time * speed_2 * direction_2;
float3 sample_1 = GET_TEXTURE(texture_index).Sample(GET_SAMPLER(sampler_anisotropic_wrap), uv_interleaved_1).rgb;
float3 sample_2 = GET_TEXTURE(texture_index).Sample(GET_SAMPLER(sampler_anisotropic_wrap), uv_interleaved_2).rgb;
color = float4(normalize(sample_1 + sample_2), 0.0f);
}
else if (is_terrain)
else if (surface.texture_slope_based())
{
float4 tex_rock = GET_TEXTURE(texture_index + 1).Sample(GET_SAMPLER(sampler_anisotropic_wrap), uv);
float4 tex_sand = GET_TEXTURE(texture_index + 2).Sample(GET_SAMPLER(sampler_anisotropic_wrap), uv);
Expand Down Expand Up @@ -136,7 +136,7 @@ gbuffer main_ps(gbuffer_vertex vertex)
float4 albedo_sample = 1.0f;
if (surface.has_texture_albedo())
{
albedo_sample = sample_texture(vertex.position, vertex.normal, vertex.uv, material_texture_index_albedo, surface.is_water(), surface.texture_slope_based());
albedo_sample = sample_texture(vertex.position, vertex.normal, vertex.uv, material_texture_index_albedo, surface);
albedo_sample.rgb = srgb_to_linear(albedo_sample.rgb);
albedo *= albedo_sample;
}
Expand All @@ -157,7 +157,7 @@ gbuffer main_ps(gbuffer_vertex vertex)
if (surface.has_texture_normal())
{
// get tangent space normal and apply the user defined intensity, then transform it to world space
float3 normal_sample = sample_texture(vertex.position, vertex.normal, vertex.uv, material_texture_index_normal, surface.is_water(), surface.texture_slope_based()).xyz;
float3 normal_sample = sample_texture(vertex.position, vertex.normal, vertex.uv, material_texture_index_normal, surface).xyz;
float3 tangent_normal = normalize(unpack(normal_sample));

// reconstruct z-component as this can be a BC5 two channel normal map
Expand All @@ -171,7 +171,7 @@ gbuffer main_ps(gbuffer_vertex vertex)

// occlusion, roughness, metalness, height sample
{
float4 packed_sample = sample_texture(vertex.position, vertex.normal, vertex.uv, material_texture_index_packed, surface.is_water(), surface.texture_slope_based());
float4 packed_sample = sample_texture(vertex.position, vertex.normal, vertex.uv, material_texture_index_packed, surface);
occlusion = packed_sample.r;
roughness *= packed_sample.g;
metalness *= packed_sample.b;
Expand Down
3 changes: 3 additions & 0 deletions data/shaders/light_image_based.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -114,5 +114,8 @@ void main_cs(uint3 thread_id : SV_DispatchThreadID)
float3 ibl = (diffuse_ibl * diffuse_energy * surface.albedo.rgb) + (specular_ibl * specular_energy);
ibl *= surface.occlusion;

// modulate ibl by transparency
ibl *= surface.alpha;

tex_uav[thread_id.xy] += float4(ibl, 0.0f);
}
21 changes: 11 additions & 10 deletions runtime/Game/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -592,15 +592,16 @@ namespace spartan
// set material
shared_ptr<Material> material = make_shared<Material>();
material->SetObjectName("material_water");
material->SetColor(Color(0.0f, 60.0f / 255.0f, 120.0f / 255.0f, 250.0f / 255.0f));
material->SetTexture(MaterialTextureType::Normal, "project\\terrain\\water_normal.jpeg");
material->SetProperty(MaterialProperty::Ior, Material::EnumToIor(MaterialIor::Water));
material->SetProperty(MaterialProperty::Roughness, 0.0f);
material->SetProperty(MaterialProperty::Normal, 0.1f);
material->SetProperty(MaterialProperty::TextureTilingX, 800.0f);
material->SetProperty(MaterialProperty::TextureTilingY, 800.0f);
material->SetProperty(MaterialProperty::AnimationWaterFlow, 1.0f);
material->SetProperty(MaterialProperty::CullMode, static_cast<float>(RHI_CullMode::None));
material->SetColor(Color(0.0f, 90.0f / 255.0f, 110.0f / 255.0f, 230.0f / 255.0f));
material->SetTexture(MaterialTextureType::Normal, "project\\terrain\\water_normal.jpeg");
material->SetProperty(MaterialProperty::Ior, Material::EnumToIor(MaterialIor::Water));
material->SetProperty(MaterialProperty::Clearcoat, 1.0f);
material->SetProperty(MaterialProperty::Clearcoat_Roughness, 0.1f);
material->SetProperty(MaterialProperty::Normal, 0.1f);
material->SetProperty(MaterialProperty::TextureTilingX, 800.0f);
material->SetProperty(MaterialProperty::TextureTilingY, 800.0f);
material->SetProperty(MaterialProperty::AnimationWaterFlow, 1.0f);
material->SetProperty(MaterialProperty::CullMode, static_cast<float>(RHI_CullMode::None));

// create a file path for this material (required for the material to be able to be cached by the resource cache)
const string file_path = "project\\terrain\\water_material" + string(EXTENSION_MATERIAL);
Expand All @@ -609,7 +610,7 @@ namespace spartan
renderable->SetMaterial(material);
}

water->SetActive(false); // disable for now as it looks bad
water->SetActive(false);
}

// tree (it has a gazillion so bake everything together using MeshFlags::ImportCombineMeshes)
Expand Down
2 changes: 1 addition & 1 deletion runtime/Rendering/Renderer_Resources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ namespace spartan
}
else if (type == MeshType::Grid)
{
uint32_t resolution = 40;
uint32_t resolution = 512;
geometry_generation::generate_grid(&vertices, &indices, resolution);
mesh->SetResourceFilePath(project_directory + "standard_grid" + EXTENSION_MODEL);
}
Expand Down
2 changes: 1 addition & 1 deletion runtime/World/Components/Terrain.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ namespace spartan
void UpdateMesh(const uint32_t tile_index);
void Clear();

float m_min_y = -30.0f; // sea level is 0.0
float m_min_y = -35.0f; // sea level is 0.0
float m_max_y = 120.0f;
float m_area_km2 = 0.0f;
std::atomic<bool> m_is_generating = false;
Expand Down

0 comments on commit 8cb91b4

Please sign in to comment.