now using deferred rendering instead of forward rendering; some little problem: grid not render, shadow not impl
This commit is contained in:
@ -55,6 +55,12 @@ void main()
|
||||
#type fragment
|
||||
#version 430 core
|
||||
|
||||
layout(location = 0) out vec4 outAlbedoMetal;
|
||||
layout(location = 1) out vec4 outNormalRoughness;
|
||||
layout(location = 2) out vec4 outEmissiveAO;
|
||||
layout(location = 3) out vec4 outColor;
|
||||
layout(location = 4) out vec4 outBloomColor;
|
||||
|
||||
const float PI = 3.141592;
|
||||
const float Epsilon = 0.00001;
|
||||
|
||||
@ -100,8 +106,8 @@ in VertexOutput
|
||||
vec4 FragPosLightSpace;
|
||||
} vs_Input;
|
||||
|
||||
layout(location = 0) out vec4 color;
|
||||
layout(location = 1) out vec4 o_BloomColor;
|
||||
|
||||
uniform bool u_GBufferMode;
|
||||
|
||||
uniform DirectionalLight u_DirectionalLights;
|
||||
|
||||
@ -149,6 +155,12 @@ uniform float u_ShadowSoftness;
|
||||
uniform float u_ShadowIntensity;
|
||||
uniform int u_ShadowEnabled;
|
||||
|
||||
// Emissive
|
||||
uniform sampler2D u_EmissiveTexture;
|
||||
uniform float u_EmissiveTexToggle;
|
||||
uniform vec3 u_EmissiveColor;
|
||||
uniform float u_EmissiveIntensity;
|
||||
|
||||
struct PBRParameters
|
||||
{
|
||||
vec3 Albedo;
|
||||
@ -411,19 +423,44 @@ float ComputeShadow(vec4 fragPosLightSpace, float NdotL)
|
||||
|
||||
void main()
|
||||
{
|
||||
m_Params.Albedo = u_AlbedoTexToggle > 0.5 ? texture(u_AlbedoTexture, vs_Input.TexCoord).rgb : u_AlbedoColor;
|
||||
m_Params.Metalness = u_MetalnessTexToggle > 0.5 ? texture(u_MetalnessTexture, vs_Input.TexCoord).r : u_Metalness;
|
||||
m_Params.Roughness = u_RoughnessTexToggle > 0.5 ? texture(u_RoughnessTexture, vs_Input.TexCoord).r : u_Roughness;
|
||||
m_Params.Roughness = max(m_Params.Roughness, 0.05);
|
||||
// === 1. 采样基础属性(所有模式都需要) ===
|
||||
vec4 albedoWithAlpha = texture(u_AlbedoTexture, vs_Input.TexCoord);
|
||||
vec3 albedo = u_AlbedoTexToggle > 0.5 ? albedoWithAlpha.rgb : u_AlbedoColor;
|
||||
float alpha = u_AlbedoTexToggle > 0.5 ? albedoWithAlpha.a : 1.0;
|
||||
|
||||
// normal
|
||||
m_Params.Normal = normalize(vs_Input.Normal);
|
||||
float metallic = u_MetalnessTexToggle > 0.5 ? texture(u_MetalnessTexture, vs_Input.TexCoord).r : u_Metalness;
|
||||
float roughness = u_RoughnessTexToggle > 0.5 ? texture(u_RoughnessTexture, vs_Input.TexCoord).r : u_Roughness;
|
||||
roughness = max(roughness, 0.05);
|
||||
|
||||
// === 2. 法线计算(世界空间) ===
|
||||
vec3 normal = normalize(vs_Input.Normal);
|
||||
if (u_NormalTexToggle > 0.5)
|
||||
{
|
||||
m_Params.Normal = normalize(2.0 * texture(u_NormalTexture, vs_Input.TexCoord).rgb - 1.0);
|
||||
m_Params.Normal = normalize(vs_Input.WorldNormals * m_Params.Normal);
|
||||
vec3 tangentNormal = texture(u_NormalTexture, vs_Input.TexCoord).rgb * 2.0 - 1.0;
|
||||
normal = normalize(vs_Input.WorldNormals * tangentNormal);
|
||||
}
|
||||
|
||||
// === 3. 自发光计算 ===
|
||||
vec3 emissive = u_EmissiveColor;
|
||||
if (u_EmissiveTexToggle > 0.5)
|
||||
emissive = texture(u_EmissiveTexture, vs_Input.TexCoord).rgb;
|
||||
emissive *= u_EmissiveIntensity;
|
||||
|
||||
// === 4. GBuffer 模式:直接输出到多个目标 ===
|
||||
if (u_GBufferMode)
|
||||
{
|
||||
outAlbedoMetal = vec4(albedo, metallic);
|
||||
outNormalRoughness = vec4(normal * 0.5 + 0.5, roughness);
|
||||
outEmissiveAO = vec4(emissive, 1.0); // AO 暂设为 1.0
|
||||
return; // 提前结束
|
||||
}
|
||||
|
||||
// === 5. 非 GBuffer 模式:继续 PBR 光照计算 ===
|
||||
// 填充 PBRParameters
|
||||
m_Params.Albedo = albedo;
|
||||
m_Params.Metalness = metallic;
|
||||
m_Params.Roughness = roughness;
|
||||
m_Params.Normal = normal;
|
||||
m_Params.View = normalize(u_CameraPosition - vs_Input.WorldPosition);
|
||||
m_Params.NdotV = max(dot(m_Params.Normal, m_Params.View), 0.0);
|
||||
|
||||
@ -449,9 +486,12 @@ void main()
|
||||
|
||||
vec3 iblContribution = IBL(F0, Lr) * u_IBLContribution;
|
||||
|
||||
color = vec4(lightContribution + iblContribution, 1.0);
|
||||
vec3 finalRGB = lightContribution + iblContribution + emissive;
|
||||
vec4 finalColor = vec4(finalRGB, alpha);
|
||||
|
||||
outColor = finalColor;
|
||||
|
||||
// Bloom
|
||||
float brightness = dot(color.rgb, vec3(0.2126, 0.7152, 0.0722));
|
||||
o_BloomColor = brightness > u_BloomThreshold ? color : vec4(0.0, 0.0, 0.0, 1.0);
|
||||
float brightness = dot(finalColor.rgb, vec3(0.2126, 0.7152, 0.0722));
|
||||
outBloomColor = brightness > u_BloomThreshold ? finalColor : vec4(0.0, 0.0, 0.0, 1.0);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user