Lighting still looks horrible, but whatever for now

This commit is contained in:
Dynamitos
2024-05-04 09:25:13 +02:00
parent 247d6a54fb
commit f0fd9a7ae7
24 changed files with 563 additions and 386 deletions
+124 -7
View File
@@ -3,22 +3,48 @@ import Common;
interface IBRDF
{
float3 evaluate(float3x3 tbn, float3 viewDir_WS, float3 lightDir_WS, float3 lightColor);
float3 evaluateAmbient();
};
struct Phong : IBRDF
{
float3 baseColor;
float3 specular;
float3 normal;
float3 ambient;
float shininess;
__init()
{
normal = float3(0, 0, 1);
}
float3 evaluate(float3x3 tbn, float3 viewDir_TS, float3 lightDir_TS, float3 lightColor)
{
float3 n = normalize(normal);
float3 nDotL = dot(n, lightDir_TS);
float3 r = 2 * (nDotL) * n - lightDir_TS;
float rDotV = dot(r, viewDir_TS);
return baseColor * max(nDotL, 0.0) * lightColor + specular * pow(max(rDotV, 0.0), shininess) * lightColor;
}
float3 evaluateAmbient()
{
return ambient;
}
};
struct BlinnPhong : IBRDF
{
float3 baseColor;
float metallic;
float3 specularColor;
float3 normal;
float roughness;
float sheen;
float3 ambient;
__init()
{
metallic = 0;
normal = float3(0, 0, 1);
roughness = 0.5;
sheen = 1;
}
float3 evaluate(float3x3 tbn, float3 viewDir_TS, float3 lightDir_TS, float3 lightColor)
@@ -28,7 +54,12 @@ struct BlinnPhong : IBRDF
float3 h = normalize(lightDir_TS + viewDir_TS);
float specular = saturate(dot(normal_TS, h));
return baseColor * (diffuse + specular) * lightColor;
return baseColor;//((baseColor * diffuse) + (specularColor * specular)) * lightColor;
}
float3 evaluateAmbient()
{
return ambient;
}
};
@@ -58,4 +89,90 @@ struct CelShading : IBRDF
return darkenedBase * lightColor;
}
}
float3 evaluateAmbient()
{
return float3(0, 0, 0);
}
};
// https://learnopengl.com/PBR/Theory
struct CookTorrance : IBRDF
{
float3 baseColor;
float3 normal;
float roughness;
float metallic;
float ambientOcclusion;
__init()
{
normal = float3(0, 0, 1);
roughness = 0;
metallic = 0;
ambientOcclusion = 1;
}
float TrowbridgeReitzGGX(float3 normal, float3 halfway)
{
float a_sqr = roughness * roughness;
float nDotH = max(dot(normal, halfway), 0.0);
float nDotH_sqr = nDotH * nDotH;
float denom = (nDotH_sqr * (a_sqr - 1.0) + 1.0);
return a_sqr / (PI * denom * denom);
}
float SchlickGGX(float nDotV, float k)
{
return nDotV / (nDotV * (1.0 - k) + k);
}
float Smith(float3 normal, float3 view, float3 light)
{
float k = (roughness + 1);
k = (k * k) / 8;
float nDotV = max(dot(normal, view), 0.0);
float nDotL = max(dot(normal, light), 0.0);
float ggx1 = SchlickGGX(nDotV, k);
float ggx2 = SchlickGGX(nDotL, k);
return ggx1 * ggx2;
}
float3 FresnelSchlick(float cosTheta, float3 F0)
{
return F0 + (1.0 - F0) * pow(clamp(1.0 - cosTheta, 0.0, 1.0), 5.0);
}
float3 evaluate(float3x3 tbn, float3 viewDir_TS, float3 lightDir_TS, float3 lightColor)
{
float3 n = normalize(normal);
float3 h = normalize(lightDir_TS + viewDir_TS);
float3 F0 = float3(0.04);
F0 = lerp(F0, baseColor, metallic);
float3 F = FresnelSchlick(max(dot(h, viewDir_TS), 0.0), F0);
float NDF = TrowbridgeReitzGGX(n, h);
float G = Smith(n, viewDir_TS, lightDir_TS);
float3 num = NDF * G * F;
float denom = 4.0 * max(dot(n, viewDir_TS), 0.0) * max(dot(n, lightDir_TS), 0.0) + 0.000001;
float3 specular = num / denom;
float3 k_s = F;
float3 k_d = float3(1.0) - k_s;
k_d *= 1.0 - metallic;
float nDotL = max(dot(n, lightDir_TS), 0.0);
float3 result = (k_d * baseColor / PI + specular) * nDotL * lightColor;
return baseColor;//result * ambientOcclusion;
}
float3 evaluateAmbient()
{
return float3(0.03) * baseColor * ambientOcclusion;
}
};
+13 -16
View File
@@ -11,15 +11,12 @@ struct BoundingSphere
{
return centerRadius.w;
}
bool insideFrustum(float4x4 transform, Frustum frustum)
bool insideFrustum(Frustum frustum)
{
float3 transformed = mul(transform, float4(getCenter(), 1)).xyz;
float maxScale = max(max(transform[0][0], transform[1][1]), transform[2][2]);
float scaledRange = getRadius() * maxScale;
bool result = true;
for(int i = 0; i < 4 && result; ++i)
{
if(dot(frustum.sides[i].n, transformed) - frustum.sides[i].d < -scaledRange)
if(dot(frustum.sides[i].n, centerRadius.xyz) - frustum.sides[i].d < -getRadius())
{
result = false;
}
@@ -34,20 +31,20 @@ struct AABB
float pad0;
float3 max;
float pad1;
bool insideFrustum(float4x4 transform, Frustum frustum)
bool insideFrustum(Frustum frustum)
{
float4 corners[8];
corners[0] = mul(transform, float4(min.x, min.y, min.z, 1.0f));
corners[1] = mul(transform, float4(min.x, min.y, max.z, 1.0f));
corners[2] = mul(transform, float4(min.x, max.y, min.z, 1.0f));
corners[3] = mul(transform, float4(min.x, max.y, max.z, 1.0f));
corners[4] = mul(transform, float4(max.x, min.y, min.z, 1.0f));
corners[5] = mul(transform, float4(max.x, min.y, max.z, 1.0f));
corners[6] = mul(transform, float4(max.x, max.y, min.z, 1.0f));
corners[7] = mul(transform, float4(max.x, max.y, max.z, 1.0f));
float3 corners[8];
corners[0] = float3(min.x, min.y, min.z);
corners[1] = float3(min.x, min.y, max.z);
corners[2] = float3(min.x, max.y, min.z);
corners[3] = float3(min.x, max.y, max.z);
corners[4] = float3(max.x, min.y, min.z);
corners[5] = float3(max.x, min.y, max.z);
corners[6] = float3(max.x, max.y, min.z);
corners[7] = float3(max.x, max.y, max.z);
for(int i = 0; i < 8; ++i)
{
if(frustum.pointInside(corners[i].xyz))
if(frustum.pointInside(corners[i]))
{
return true;
}
+28 -1
View File
@@ -1,10 +1,10 @@
const static float PI = 3.1415926535897932f;
const static uint MAX_PARTICLES = 65536;
const static uint BLOCK_SIZE = 32;
struct ViewParameter
{
float4x4 viewMatrix;
float4x4 inverseViewMatrix;
float4x4 projectionMatrix;
float4x4 inverseProjection;
float4 cameraPos_WS;
@@ -13,6 +13,24 @@ struct ViewParameter
layout(set=0)
ParameterBlock<ViewParameter> pViewParams;
float4 worldToModel(float4x4 inverseTransform, float4 world)
{
float4 model = mul(inverseTransform, world);
model = model / model.w;
return model;
}
float4 viewToWorld(float4 view)
{
float4 world = mul(pViewParams.inverseViewMatrix, view);
world = world / world.w;
return world;
}
float4 clipToView(float4 clip)
{
float4 view = mul(pViewParams.inverseProjection, clip);
@@ -32,6 +50,15 @@ float4 screenToView(float4 screen)
return clipToView(clip);
}
float4 screenToModel(float4x4 inverseTransform, float4 screen)
{
float4 view = screenToView(screen);
float4 world = viewToWorld(view);
return worldToModel(inverseTransform, world);
}
struct Plane
{
float3 n;
+9 -8
View File
@@ -2,7 +2,7 @@ import Bounding;
struct MeshletDescription
{
BoundingSphere bounding;
AABB bounding;
uint32_t vertexCount;
uint32_t primitiveCount;
uint32_t vertexOffset;
@@ -13,13 +13,13 @@ struct MeshletDescription
struct MeshData
{
BoundingSphere bounding;
uint32_t numMeshlets;
uint32_t meshletOffset;
uint32_t firstIndex;
uint32_t numIndices;
uint32_t indicesOffset;
uint32_t pad0[3];
AABB bounding;
uint32_t numMeshlets;
uint32_t meshletOffset;
uint32_t firstIndex;
uint32_t numIndices;
uint32_t indicesOffset;
uint32_t pad0[3];
};
static const uint MAX_VERTICES = 256;
@@ -31,6 +31,7 @@ static const uint MAX_MESHLETS_PER_MESH = 512;
struct InstanceData
{
float4x4 transformMatrix;
float4x4 inverseTransformMatrix;
};
struct Scene