【问题标题】:Physically based shader not producing desired results基于物理的着色器没有产生预期的结果
【发布时间】:2014-05-18 20:24:55
【问题描述】:

在过去的大约 2-3 周内,我一直在学习基于物理的着色,但我无法解决我遇到的一些问题。

片段着色器

#version 430
#define PI 3.14159265358979323846

// Inputs
in vec3 inputNormal;
vec3 fNormal;

// Material
float reflectance = 1.0; // 0 to 1
float roughness = 0.5;
vec3 specularColor = vec3(1.0, 1.0, 1.0); // f0

// Values
vec3 lightVector = vec3(1, 1, 1); // Light (l)
vec3 eyeVector = vec3(2.75, 1.25, 1.25); // Camera (v)
vec3 halfVector = normalize(lightVector + eyeVector); // L + V / |L + V|

out vec4 fColor; // Output Color

// Specular Functions
vec3 D(vec3 h) // Normal Distribution Function - GGX/Trowbridge-Reitz
{
    float alpha = roughness * roughness;
    float alpha2 = alpha * alpha;
    float NoH = dot(fNormal, h);
    float finalTerm = ((NoH * NoH) * (alpha2 - 1.0) + 1.0);
    return vec3(alpha2 / (PI * (finalTerm * finalTerm)));
}
vec3 Gsub(vec3 v) // Sub Function of G
{
    float k = ((roughness + 1.0) * (roughness + 1.0)) / 8;
    return vec3(dot(fNormal, v) / ((dot(fNormal, v)) * (1.0 - k) + k));
}
vec3 G(vec3 l, vec3 v, vec3 h) // Geometric Attenuation Term - Schlick Modified (k = a/2)
{
    return Gsub(l) * Gsub(v);
}
vec3 F(vec3 v, vec3 h) // Fresnel - Schlick Modified (Spherical Gaussian Approximation)
{
    vec3 f0 = specularColor; // right?
    return f0 + (1.0 - f0) * pow(2, (-5.55473 * (dot(v, h)) - 6.98316) * (dot(v, h)));
}

vec3 specular()
{
    return (D(halfVector) * F(eyeVector, halfVector) * G(lightVector, eyeVector, halfVector)) / 4 * ((dot(fNormal, lightVector)) * (dot(fNormal, eyeVector)));
}
vec3 diffuse()
{
    float NoL = dot(fNormal, lightVector);
    vec3 result = vec3(reflectance / PI);
    return result * NoL;
}
void main()
{
    fNormal = normalize(inputNormal);
    fColor = vec4(diffuse() + specular(), 1.0);
    //fColor = vec4(D(halfVector), 1.0);
}

到目前为止,我已经能够解决一些问题,现在我得到了更好的结果。

但现在看来,亮点太大了;这源于正态分布函数 (Specular D)。

【问题讨论】:

  • 您是否尝试更改 alpha 值?它旨在更改 GGX 分布中的高光大小。试试 0.99 或 0.01 之类的。
  • 感谢 UE 4 着色实现!

标签: opengl glsl shader fragment-shader


【解决方案1】:

您的 GGX/Trowbridge-Reitz 编码错误:

vec3 NxH = fNormal * h;

星 * 表示您需要点积的逐项积

还有

float alphaTerm = (alpha * alpha - 1.0) + 1.0;

不正确,因为公式在添加 1.0 之前将 n.m 乘以 (alpha * alpha - 1.0)。你的公式等于 alpha*alpha!

试试:

// Specular
vec3 D(vec3 h) // Normal Distribution Function - GGX/Trowbridge-Reitz
{
    float alpha = roughness * roughness;
    float NxH = dot(fNormal,h);
    float alpha2 = alpha*alpha;
    float t = ((NxH * NxH) * (alpha2 - 1.0) + 1.0);
    return alpha2 / (PI * t * t);
}

在许多其他地方,您使用* 而不是dot。你需要纠正所有这些。另外,请检查您的公式,许多似乎不正确。

【讨论】:

  • 哇,我没有意识到那些意味着点积。谢谢!
  • 但是你不会忘记等式末尾的正方形吗?无论如何,我已经更新了代码以显示我到目前为止所做的更改。
  • 是的,方块不见了。现已修复。
  • 这最终帮了大忙,谢谢!为了在这篇文章中没有得到想要的结果,我必须进行一些更改,但我不确定该放在哪里。感谢您的帮助!
猜你喜欢
  • 2015-04-20
  • 2017-01-22
  • 2019-12-13
  • 2015-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-26
  • 2017-05-22
相关资源
最近更新 更多