Skip to content

Commit

Permalink
[Forest] Water now uses interleaved sampling of the normal map, this …
Browse files Browse the repository at this point in the history
…make it look more realistic and less tiled
  • Loading branch information
PanosK92 committed Nov 21, 2023
1 parent a074d7b commit 1933dfd
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 15 deletions.
Binary file added assets/terrain/water_normal.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed assets/terrain/water_normal_2.jpeg
Binary file not shown.
5 changes: 2 additions & 3 deletions data/shaders/common_buffers.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,8 @@ bool has_texture_alpha_mask() { return buffer_material.propertie
bool has_texture_emissive() { return buffer_material.properties & uint(1U << 7); }
bool has_texture_occlusion() { return buffer_material.properties & uint(1U << 8); }
bool material_texture_slope_based() { return buffer_material.properties & uint(1U << 9); }
bool material_texture_animate() { return buffer_material.properties & uint(1U << 10); }
bool material_vertex_animate_wind() { return buffer_material.properties & uint(1U << 11); }
bool material_vertex_animate_water() { return buffer_material.properties & uint(1U << 12); }
bool material_vertex_animate_wind() { return buffer_material.properties & uint(1U << 10); }
bool material_vertex_animate_water() { return buffer_material.properties & uint(1U << 11); }

// lighting properties
bool light_is_directional() { return buffer_light.options & uint(1U << 0); }
Expand Down
30 changes: 29 additions & 1 deletion data/shaders/g_buffer.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,37 @@ float4 sample_albedo(float2 uv, float slope)
return albedo;
}

float3 sample_interleaved_normal(float2 uv)
{
// constants for scale and direction of the normal map movement
float2 direction1 = float2(1.0, 0.5);
float2 direction2 = float2(-0.5, 1.0);
float scale1 = 0.5;
float scale2 = 0.5;
float speed1 = 0.2;
float speed2 = 0.15;

// Calculate unique UV offsets for the two normal maps
float2 uv1 = uv + buffer_frame.time * speed1 * direction1;
float2 uv2 = uv + buffer_frame.time * speed2 * direction2;

// Sample the normal maps
float3 normal1 = tex_material_normal.Sample(samplers[sampler_anisotropic_wrap], uv1 * scale1).xyz;
float3 normal2 = tex_material_normal.Sample(samplers[sampler_anisotropic_wrap], uv2 * scale2).xyz;

// Blend the normals
float3 blendedNormal = normalize(normal1 + normal2);

return blendedNormal;
}

float3 smaple_normal(float2 uv, float slope)
{
if (material_vertex_animate_water())
{
return sample_interleaved_normal(uv);
}

float3 normal = tex_material_normal.Sample(samplers[sampler_anisotropic_wrap], uv).xyz;

if (material_texture_slope_based())
Expand Down Expand Up @@ -124,7 +153,6 @@ PixelOutputType mainPS(PixelInputType input)
// uv
float2 uv = input.uv;
uv = float2(uv.x * buffer_material.tiling.x + buffer_material.offset.x, uv.y * buffer_material.tiling.y + buffer_material.offset.y);
uv += float(buffer_frame.time * 0.1f) * material_texture_animate();

// alpha mask
float alpha_mask = 1.0f;
Expand Down
1 change: 0 additions & 1 deletion runtime/Rendering/Material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ namespace Spartan
case MaterialProperty::TextureOffsetX: return "texture_offset_x";
case MaterialProperty::TextureOffsetY: return "texture_offset_y";
case MaterialProperty::TextureSlopeBased: return "texture_slope_based";
case MaterialProperty::TextureAnimate: return "texture_animate";
case MaterialProperty::VertexAnimateWind: return "vertex_animate_wind";
case MaterialProperty::VertexAnimateWater: return "vertex_animate_water";
case MaterialProperty::Undefined: return "undefined";
Expand Down
1 change: 0 additions & 1 deletion runtime/Rendering/Material.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ namespace Spartan
TextureTilingY,
TextureOffsetX,
TextureOffsetY,
TextureAnimate,
TextureSlopeBased,
VertexAnimateWind,
VertexAnimateWater,
Expand Down
5 changes: 2 additions & 3 deletions runtime/Rendering/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -560,9 +560,8 @@ namespace Spartan
m_cb_material_cpu.properties |= material->HasTexture(MaterialTexture::Emission) ? (1U << 7) : 0;
m_cb_material_cpu.properties |= material->HasTexture(MaterialTexture::Occlusion) ? (1U << 8) : 0;
m_cb_material_cpu.properties |= material->GetProperty(MaterialProperty::TextureSlopeBased) ? (1U << 9) : 0;
m_cb_material_cpu.properties |= material->GetProperty(MaterialProperty::TextureAnimate) ? (1U << 10) : 0;
m_cb_material_cpu.properties |= material->GetProperty(MaterialProperty::VertexAnimateWind) ? (1U << 11) : 0;
m_cb_material_cpu.properties |= material->GetProperty(MaterialProperty::VertexAnimateWater) ? (1U << 12) : 0;
m_cb_material_cpu.properties |= material->GetProperty(MaterialProperty::VertexAnimateWind) ? (1U << 10) : 0;
m_cb_material_cpu.properties |= material->GetProperty(MaterialProperty::VertexAnimateWater) ? (1U << 11) : 0;

GetConstantBuffer(Renderer_ConstantBuffer::Material)->Update(&m_cb_material_cpu);
cmd_list->SetConstantBuffer(Renderer_BindingsCb::material, GetConstantBuffer(Renderer_ConstantBuffer::Material));
Expand Down
11 changes: 5 additions & 6 deletions runtime/World/World.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -857,13 +857,12 @@ namespace Spartan
// set material
shared_ptr<Material> material = make_shared<Material>();
material->SetObjectName("material_water");
material->SetColor(Color(0.0f, 48.0f / 255.0f, 75.0f / 255.0f, 50.0f / 255.0f));
material->SetTexture(MaterialTexture::Normal, "project\\terrain\\water_normal_2.jpeg");
material->SetColor(Color(0.0f, 60.0f / 255.0f, 75.0f / 255.0f, 60.0f / 255.0f));
material->SetTexture(MaterialTexture::Normal, "project\\terrain\\water_normal.jpeg");
material->SetProperty(MaterialProperty::MultiplierRoughness, 0.3f); // just a bit of roughness to diffuse the sun a little
material->SetProperty(MaterialProperty::MultiplierNormal, 0.3f);
material->SetProperty(MaterialProperty::TextureTilingX, 500.0f);
material->SetProperty(MaterialProperty::TextureTilingY, 500.0f);
material->SetProperty(MaterialProperty::TextureAnimate, 1.0f);
material->SetProperty(MaterialProperty::MultiplierNormal, 0.8f);
material->SetProperty(MaterialProperty::TextureTilingX, 1000.0f);
material->SetProperty(MaterialProperty::TextureTilingY, 1000.0f);
material->SetProperty(MaterialProperty::VertexAnimateWater, 1.0f);

// create a file path for this material (required for the material to be able to be cached by the resource cache)
Expand Down

0 comments on commit 1933dfd

Please sign in to comment.