Skip to content

Commit 572b69d

Browse files
committed
Some solvings in the position reconstruction by zbuffer
1 parent 171c18b commit 572b69d

File tree

3 files changed

+38
-52
lines changed

3 files changed

+38
-52
lines changed

src/Shader/LightPass.hlsl

Lines changed: 36 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -69,22 +69,15 @@ float GeometrySmith(float3 N, float3 V, float3 L, float roughness)
6969
return ggx1 * ggx2;
7070
}
7171

72-
float LinearizeDepth(float depth)
72+
float3 ReconstructPosition(int2 pixelCoord, uint index)
7373
{
74-
float z = depth * 2.0 - 1.0;
75-
float near = 0.1f;
76-
float far = 100.0f;
77-
return (2.0 * near * far) / (far + near - z * (far - near));
78-
}
74+
float depth = depthTex.Load(pixelCoord, index).r;
7975

80-
float3 ReconstructPosition(float2 pixelCoord, uint index)
81-
{
82-
float depth = LinearizeDepth(depthTex.Load(pixelCoord, index).r);
83-
84-
float2 ndc = (pixelCoord / screenSize) * 2.0f - 1.0f;
76+
float2 ndc = (float2(pixelCoord) / screenSize) * 2.0f - 1.0f;
8577
ndc.y *= -1.0f;
8678

87-
float4 clipPos = float4(ndc.xy, depth, 1.0f);
79+
float z = depth * 2.0f - 1.0f;
80+
float4 clipPos = float4(ndc.xy, z, 1.0f);
8881

8982
float4 viewPos = mul(InverseProjection, clipPos);
9083
viewPos /= viewPos.w;
@@ -94,64 +87,55 @@ float3 ReconstructPosition(float2 pixelCoord, uint index)
9487
return worldPos.xyz;
9588
}
9689

97-
9890
PixelOutput PixelMain(VertexOutput input, uint index : SV_SampleIndex)
9991
{
100-
float3 lightPos = float3(0.f, 5.f, 5.f);
101-
float3 lightColor = float3(100.f, 100.f, 100.f);
92+
float3 lightPos = float3(0.f, 20.f, 20.f);
93+
float3 lightColor = float3(500.f, 500.f, 500.f);
10294

103-
float4 albedoColor = albedo.Load(input.position.xy, index);
104-
float4 nml = normalize(normal.Load(input.position.xy, index) * 2 - 1);
105-
float3 pos = ReconstructPosition(input.position.xy, index);
106-
float4 vertexPos = float4(pos.x, pos.y, pos.z, 1.f);
95+
int2 pixelCoord = int2(input.position.xy);
96+
97+
float3 albedoColor = albedo.Load(pixelCoord, index).rgb;
98+
float3 N = normalize(normal.Load(pixelCoord, index).rgb * 2 - 1);
99+
float3 pos = ReconstructPosition(pixelCoord, index);
107100

108-
/* ORM G-Buffer */
109-
float3 ormData = orm.Load(input.position.xy, index).rgb;
101+
float3 ormData = orm.Load(pixelCoord, index).rgb;
110102
float ao = ormData.r;
111103
float roughness = ormData.g;
112104
float metallic = ormData.b;
113-
114-
115-
float3 N = nml.xyz;
116-
float3 V = normalize(cameraPos - vertexPos.xyz);
117-
float3 F0 = float3(.04f, .04f, .04f);
118-
F0 = lerp(F0, albedoColor.xyz, metallic);
119-
120-
/* Reflectance equation */
121-
float3 Lo = float3(0.f, 0.f, 0.f);
122-
123-
/* Calculate light radiance */
124-
float3 L = normalize(lightPos - vertexPos.xyz);
105+
106+
float3 V = normalize(cameraPos - pos);
107+
float3 F0 = lerp(float3(0.04f, 0.04f, 0.04f), albedoColor, metallic);
108+
109+
float3 L = normalize(lightPos - pos);
125110
float3 H = normalize(V + L);
126-
float distance = length(lightPos - vertexPos.xyz);
111+
float distance = length(lightPos - pos);
127112
float attenuation = 1.0f / (distance * distance + 1.f);
128113
float3 radiance = lightColor * attenuation;
129114
radiance *= (1.0 + metallic * 0.2);
130-
131-
/* Cook torrance BRDF */
115+
132116
float NDF = DistributionGGX(N, H, roughness);
133117
float G = GeometrySmith(N, V, L, roughness);
134118
float3 F = FresnelShlick(saturate(dot(H, V)), F0);
135-
136-
float3 kS = F;
137-
float3 kD = (1.0 - kS) * (1.0 - metallic);;
138-
kD *= 1.f - metallic;
139-
119+
120+
float3 kS = F;
121+
float3 kD = (1.0 - kS) * (1.0 - metallic);
122+
140123
float3 numerator = NDF * G * F;
141124
float denominator = 4.f * max(dot(N, V), 0.f) * max(dot(N, L), 0.f) + 0.0001f;
142125
float3 specular = numerator / denominator;
143-
126+
144127
float NdotL = max(dot(N, L), 0.f);
145-
146-
Lo += (kD * albedoColor.xyz / PI + specular) * radiance * NdotL;
147-
148-
float3 ambient = 0.1f * albedoColor.xyz * ao;
149-
128+
float3 Lo = (kD * albedoColor / PI + specular) * radiance * NdotL;
129+
130+
float3 ambient = 0.1f * albedoColor * ao;
131+
150132
float3 color = ambient + Lo;
151-
//float3 color = vertexPos.xyz;
152-
133+
134+
/* Tonemap */
135+
color = color / (color + 1.0);
136+
//color = pow(saturate(color), 1.0 / 2.2);
137+
153138
PixelOutput output;
154-
155-
output.screen = float4(color.x, color.y, color.z, 1.f);
139+
output.screen = float4(color, 1.f);
156140
return output;
157-
}
141+
}

src/private/Core/GameObject/Component/Mesh.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ void Mesh::InitConstantBuffer() {
122122
void Mesh::UpdateConstantBuffer() {
123123
UINT nWVPSize = (sizeof(this->m_wvp) + 255) & ~255;
124124
this->m_wvp.World = XMMatrixTranspose(XMMatrixIdentity());
125+
125126
this->m_wvp.World *= XMMatrixTranspose(XMMatrixRotationX(XMConvertToRadians(this->m_transform->rotation.x)));
126127
this->m_wvp.World *= XMMatrixTranspose(XMMatrixRotationY(XMConvertToRadians(this->m_transform->rotation.y)));
127128
this->m_wvp.World *= XMMatrixTranspose(XMMatrixRotationZ(XMConvertToRadians(this->m_transform->rotation.z)));

src/private/Core/Scene/Scene.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ void Scene::Init() {
1919
for (std::pair<std::string, GameObject*> object : this->m_gameObjects) {
2020
object.second->Init();
2121
}
22+
//m_go->transform.Rotate(Vector3(90.f, 0.f, 0.f));
2223
}
2324

2425
void Scene::Update() {

0 commit comments

Comments
 (0)