Skip to content

Commit

Permalink
Traktor: Refactored PBR lighting shaders.
Browse files Browse the repository at this point in the history
  • Loading branch information
apistol78 committed Jan 16, 2024
1 parent 94c58a5 commit 1913512
Show file tree
Hide file tree
Showing 7 changed files with 1,503 additions and 1,925 deletions.
7 changes: 5 additions & 2 deletions code/World/Editor/IrradianceGridAsset.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* TRAKTOR
* Copyright (c) 2022 Anders Pistol.
* Copyright (c) 2022-2024 Anders Pistol.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
Expand All @@ -14,7 +14,7 @@
namespace traktor::world
{

T_IMPLEMENT_RTTI_EDIT_CLASS(L"traktor.world.IrradianceGridAsset", 2, IrradianceGridAsset, editor::Asset)
T_IMPLEMENT_RTTI_EDIT_CLASS(L"traktor.world.IrradianceGridAsset", 3, IrradianceGridAsset, editor::Asset)

void IrradianceGridAsset::serialize(ISerializer& s)
{
Expand All @@ -23,6 +23,9 @@ void IrradianceGridAsset::serialize(ISerializer& s)
if (s.getVersion< IrradianceGridAsset >() >= 1)
s >> Member< float >(L"intensity", m_intensity, AttributeUnit(UnitType::Percent));

if (s.getVersion< IrradianceGridAsset >() >= 3)
s >> Member< float >(L"saturation", m_saturation, AttributeUnit(UnitType::Percent));

if (s.getVersion< IrradianceGridAsset >() >= 2)
s >> Member< bool >(L"cancelSun", m_cancelSun);
}
Expand Down
5 changes: 4 additions & 1 deletion code/World/Editor/IrradianceGridAsset.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* TRAKTOR
* Copyright (c) 2022 Anders Pistol.
* Copyright (c) 2022-2024 Anders Pistol.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
Expand Down Expand Up @@ -33,10 +33,13 @@ class T_DLLCLASS IrradianceGridAsset : public editor::Asset

float getIntensity() const { return m_intensity; }

float getSaturation() const { return m_saturation; }

bool shouldCancelSun() const { return m_cancelSun; }

private:
float m_intensity = 1.0f;
float m_saturation = 1.0f;
bool m_cancelSun = true;
};

Expand Down
10 changes: 9 additions & 1 deletion code/World/Editor/IrradianceGridPipeline.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* TRAKTOR
* Copyright (c) 2022-2023 Anders Pistol.
* Copyright (c) 2022-2024 Anders Pistol.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
Expand Down Expand Up @@ -193,9 +193,12 @@ bool IrradianceGridPipeline::buildOutput(
{
// No asset specified; use dynamic sky.
const Scalar intensity(asset->getIntensity());
const Scalar saturation(asset->getSaturation());

WrappedSHFunction shFunction([&] (const Vector4& unit) -> Vector4 {
Color4f cl(0.0f, 0.0f, 0.0f, 0.0f);

// Sample over hemisphere.
for (int32_t i = 0; i < 1000; ++i)
{
const Vector2 uv = Quasirandom::hammersley(i, 1000);
Expand All @@ -213,6 +216,11 @@ bool IrradianceGridPipeline::buildOutput(
col += clamp((0.1_simd - direction.y()) * 10.0_simd, 0.0_simd, 1.0_simd) * Vector4(0.0f, 0.1f, 0.2f, 0.0f);
cl += Color4f(col * w);
}

// Apply saturation.
const Scalar bw = dot3(cl, Vector4(1.0f, 1.0f, 1.0f)) / 3.0_simd;
cl = Color4f(lerp(Vector4(bw, bw, bw, 0.0f), cl, saturation));

return (cl * intensity * 2.0_simd) / 1000.0_simd;
});

Expand Down
97 changes: 97 additions & 0 deletions data/Source/System/Shaders/Modules/PBR.xdi
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?xml version="1.0" encoding="utf-8"?>
<object type="traktor.render.ShaderModule">
<text>
<![CDATA[
// https://learnopengl.com/PBR/Lighting
vec3 FresnelSchlick(float cosTheta, vec3 F0)
{
cosTheta = max(cosTheta, 0.0f);
return F0 + (1.0 - F0) * pow(clamp(1.0 - cosTheta, 0.0, 1.0), 5.0);
}
float DistributionGGX(vec3 N, vec3 H, float roughness)
{
const float PI = 3.14159265359f;
const float a = roughness * roughness;
const float a2 = a * a;
const float NdotH = max(dot(N, H), 0.0f);
const float NdotH2 = NdotH * NdotH;
const float num = a2;
float denom = (NdotH2 * (a2 - 1.0f) + 1.0f);
denom = PI * denom * denom;
return num / denom;
}
float GeometrySchlickGGX(float NdotV, float roughness)
{
const float r = (roughness + 1.0f);
const float k = (r * r) / 8.0f;
const float num = NdotV;
const float denom = NdotV * (1.0f - k) + k;
return num / denom;
}
float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness)
{
const float NdotV = max(dot(N, V), 0.0f);
const float NdotL = max(dot(N, L), 0.0f);
const float ggx2 = GeometrySchlickGGX(NdotV, roughness);
const float ggx1 = GeometrySchlickGGX(NdotL, roughness);
return ggx1 * ggx2;
}
vec3 CalculateLighting(vec3 N, vec3 V, vec3 L, vec3 albedo, float metallic, float roughness, vec3 radiance)
{
const float PI = 3.14159265359f;
const vec3 H = normalize(V + L);
const float NdotL = max(dot(N, L), 0.0f);
const float NdotV = max(dot(N, V), 0.0f);
const vec3 F0 = mix(vec3(0.04), albedo, metallic);
const vec3 F = FresnelSchlick(max(dot(H, V), 0.0), F0);
const float NDF = DistributionGGX(N, H, roughness);
const float G = GeometrySmith(N, V, L, roughness);
const vec3 numerator = NDF * G * F;
const float denominator = 4.0f * NdotV * NdotL + 0.0001f;
const vec3 specular = numerator / denominator;
const vec3 kS = F;
const vec3 kD = (vec3(1.0f) - kS) * (1.0f - metallic);
return (kD * albedo / PI + specular) * radiance * NdotL;
}
vec3 CalculateReflection(vec3 N, vec3 V, vec3 albedo, float metallic, float roughness)
{
// Use the view reflection vector as light direction.
const vec3 L = -reflect(V, N);
const vec3 H = normalize(V + L);
const float NdotL = max(dot(N, L), 0.0f);
const float NdotV = max(dot(N, V), 0.0f);
const vec3 F0 = mix(vec3(0.04), albedo, metallic);
const vec3 F = FresnelSchlick(max(dot(H, V), 0.0), F0);
const float NDF = DistributionGGX(N, H, roughness);
const float G = GeometrySmith(N, V, L, roughness);
const vec3 numerator = NDF * G * F;
const float denominator = 4.0f * NdotV * NdotL + 0.0001f;
const vec3 specular = numerator / denominator;
return clamp(specular, 0.0f, 1.0f);
}
]]>
</text>
</object>
6 changes: 6 additions & 0 deletions data/Source/System/Shaders/Modules/PBR.xdm
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<object type="traktor.db.LocalInstanceMeta" version="2">
<guid>{8541C8B0-4A05-AA42-BFE7-7DBF639AAF8B}</guid>
<primaryType>traktor.render.ShaderModule</primaryType>
<blobs/>
</object>
5 changes: 3 additions & 2 deletions data/Source/System/Weather/Sky/Sky.xdi
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<object type="traktor.world.IrradianceGridAsset" version="2">
<object type="traktor.world.IrradianceGridAsset" version="3">
<fileName></fileName>
<intensity>1</intensity>
<intensity>1.5</intensity>
<saturation>0.25</saturation>
<cancelSun>false</cancelSun>
</object>
Loading

0 comments on commit 1913512

Please sign in to comment.