Skip to content

Commit

Permalink
[Material] Added index of refraction which can be tweaked via the edi…
Browse files Browse the repository at this point in the history
…tor as well
  • Loading branch information
PanosK92 committed Dec 1, 2023
1 parent 92def3d commit 3b9b371
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 23 deletions.
32 changes: 18 additions & 14 deletions data/shaders/common_structs.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ struct Surface
float anisotropic_rotation;
float sheen;
float3 sheen_tint;
float subsurface_scattering;
float ior;
float occlusion;
float3 gi;
float3 emissive;
Expand Down Expand Up @@ -82,20 +84,22 @@ struct Surface
depth = sample_depth;
normal = sample_normal.xyz;

albedo = replace_color_with_one ? 1.0f : sample_albedo.rgb;
alpha = sample_albedo.a;
roughness = sample_material.r;
metallic = sample_material.g;
emissive = sample_material.b;
F0 = lerp(0.04f, albedo, metallic);
anisotropic = mat_properties.anisotropic;
anisotropic_rotation = mat_properties.anisotropic_rotation;
clearcoat = mat_properties.clearcoat;
clearcoat_roughness = mat_properties.clearcoat_roughness;
sheen = mat_properties.sheen;
sheen_tint = mat_properties.sheen_tint;
specular_energy = 1.0f;
diffuse_energy = 1.0f;
albedo = replace_color_with_one ? 1.0f : sample_albedo.rgb;
alpha = sample_albedo.a;
roughness = sample_material.r;
metallic = sample_material.g;
emissive = sample_material.b;
F0 = lerp(0.04f, albedo, metallic);
anisotropic = mat_properties.anisotropic;
anisotropic_rotation = mat_properties.anisotropic_rotation;
clearcoat = mat_properties.clearcoat;
clearcoat_roughness = mat_properties.clearcoat_roughness;
sheen = mat_properties.sheen;
sheen_tint = mat_properties.sheen_tint;
subsurface_scattering = mat_properties.subsurface_scattering;
ior = mat_properties.ior;
specular_energy = 1.0f;
diffuse_energy = 1.0f;

// Roughness is authored as perceptual roughness; as is convention,
// convert to material roughness by squaring the perceptual roughness.
Expand Down
13 changes: 6 additions & 7 deletions data/shaders/light_composition.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ struct refraction
return eta * i + (eta * cosi - sqrt(abs(cost2))) * n;
}

static float3 get_color(Surface surface, float ior, float scale)
static float3 get_color(Surface surface, float scale)
{
// comute view space data
float3 view_pos = world_to_view(surface.position);
float3 view_normal = world_to_view(surface.normal, false);
float3 view_dir = normalize(view_pos);

// compute refracted uv
float3 refracted_dir = refract_vector(view_dir, view_normal, 1.0f / ior);
float3 refracted_dir = refract_vector(view_dir, view_normal, 1.0f / surface.ior);
float2 refraction_uv_offset = refracted_dir.xy * scale;
float2 refracted_uv = surface.uv + refraction_uv_offset;

Expand All @@ -60,12 +60,12 @@ struct refraction
// dont refract surfaces which are behind this surface
const float depth_bias = 0.02f;
const bool is_behind = step(get_linear_depth(surface.depth) - depth_bias, get_linear_depth(refracted_uv)) == 1.0f;
if (is_behind)
if (is_behind && surface.ior > 1.0f)
{
// simulate light breaking off into individual color bands via chromatic aberration
float3 color_refracted = float3(0, 0, 0);
{
float chromatic_aberration_strength = ior * 0.0005f;
float chromatic_aberration_strength = surface.ior * 0.0005f;
chromatic_aberration_strength *= (1.0f + surface.roughness_alpha);

float2 ca_offsets[3];
Expand Down Expand Up @@ -120,11 +120,10 @@ void mainCS(uint3 thread_id : SV_DispatchThreadID)

// refraction
float3 light_refraction = 0.0f;
if (surface.is_transparent())
if (surface.is_transparent() && surface.ior > 1.0f)
{
float ior = 1.33; // water
float scale = 0.05f;
light_refraction = refraction::get_color(surface, ior, scale);
light_refraction = refraction::get_color(surface, scale);
}

// compose
Expand Down
11 changes: 10 additions & 1 deletion editor/Widgets/Properties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,16 @@ void Properties::ShowMaterial(Material* material) const

if (mat_property != MaterialProperty::MultiplierMetalness)
{
ImGuiSp::draw_float_wrap("", &value, 0.004f, 0.0f, 1.0f);
float min = 0.0f;
float max = 1.0f;

if (mat_property == MaterialProperty::Ior)
{
min = 1.0f;
max = 2.4f; // diamond
}

ImGuiSp::draw_float_wrap("", &value, 0.004f, min, max);
}
else
{
Expand Down
9 changes: 8 additions & 1 deletion runtime/Rendering/Material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ namespace Spartan
SetProperty(MaterialProperty::TextureTilingX, 1.0f);
SetProperty(MaterialProperty::TextureTilingY, 1.0f);
SetProperty(MaterialProperty::WorldSpaceHeight, 1.0f);
SetProperty(MaterialProperty::Ior, 1.33f); // water
}

bool Material::LoadFromFile(const std::string& file_path)
Expand Down Expand Up @@ -283,7 +284,7 @@ namespace Spartan
return HasTexture(texture_type) ? m_textures[static_cast<uint32_t>(texture_type)] : texture_empty;
}

void Material::SetProperty(const MaterialProperty property_type, const float value)
void Material::SetProperty(const MaterialProperty property_type, float value)
{
if (m_properties[static_cast<uint32_t>(property_type)] == value)
return;
Expand All @@ -302,6 +303,12 @@ namespace Spartan
m_properties[static_cast<uint32_t>(MaterialProperty::MultiplierRoughness)] = value * 0.5f;
}

if (property_type == MaterialProperty::Ior)
{
// 2.4 - diamond
value = clamp(value, 1.0f, 2.4f);
}

if (property_type == MaterialProperty::Anisotropic ||
property_type == MaterialProperty::AnisotropicRotation ||
property_type == MaterialProperty::Clearcoat ||
Expand Down
9 changes: 9 additions & 0 deletions runtime/World/World.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,14 @@ namespace Spartan

// body
{
if (Entity* body = entity_car->GetDescendantByName("CarBody_Windows_0"))
{
if (Material* material = body->GetComponent<Renderable>()->GetMaterial())
{
material->SetProperty(MaterialProperty::Ior, 1.45f);
}
}

if (Entity* body = entity_car->GetDescendantByName("CarBody_Primary_0"))
{
if (Material* material = body->GetComponent<Renderable>()->GetMaterial())
Expand Down Expand Up @@ -851,6 +859,7 @@ namespace Spartan
material->SetObjectName("material_water");
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::Ior, 1.33f); // water
material->SetProperty(MaterialProperty::MultiplierRoughness, 0.3f); // just a bit of roughness to diffuse the sun a little
material->SetProperty(MaterialProperty::MultiplierNormal, 0.8f);
material->SetProperty(MaterialProperty::TextureTilingX, 250.0f);
Expand Down

0 comments on commit 3b9b371

Please sign in to comment.