[add] PBR Shader

This commit is contained in:
kimnuri
2025-11-18 19:03:18 +09:00
parent 86bd315609
commit cb19a12eab
3 changed files with 1316 additions and 0 deletions
@@ -0,0 +1,475 @@
#include "Engine_Shader_Defines.hlsli"
matrix g_WorldMatrix, g_ViewMatrix, g_ProjMatrix;
matrix g_invMatView, g_invmatProj;
matrix g_LightViewMatrix, g_LightProjMatrix;
matrix g_PreShadowLightViewMatrix, g_PreShadowLightProjMatrix;
float g_fFar;
float g_fPreShadowFar;
uint g_iMaxShadowWidth;
uint g_iMaxShadowHeight;
float2 g_vResolution;
Texture2D g_Texture;
float g_fLightRange;
float g_fSpotInnerAngle;
float g_fSpotOuterAngle;
vector g_vLightDir;
vector g_vLightPos;
vector g_vLightPosOffset;
vector g_vCamPosition;
Texture2D g_NormalTexture;
Texture2D g_DiffuseTexture;
Texture2D g_ShadeTexture;
Texture2D g_DepthTexture;
Texture2D g_SpecularTexture;
Texture2D g_ShadowTexture;
Texture2D g_PreShadowTexture;
Texture2D g_BlurTexture;
Texture2D g_BlurXTexture;
Texture2D g_BlurWeightTexture;
Texture2D g_MROTexture;
vector g_vLightDiffuse;
vector g_vLightAmbient;
vector g_vLightSpecular;
struct VS_IN
{
float3 vPosition : POSITION;
float2 vTexcoord : TEXCOORD0;
};
struct VS_OUT
{
float4 vPosition : SV_POSITION;
float2 vTexcoord : TEXCOORD0;
};
VS_OUT VS_MAIN(VS_IN In)
{
VS_OUT Out;
matrix matWV, matWVP;
matWV = mul(g_WorldMatrix, g_ViewMatrix);
matWVP = mul(matWV, g_ProjMatrix);
Out.vPosition = mul(vector(In.vPosition, 1.f), matWVP);
Out.vTexcoord = In.vTexcoord;
return Out;
}
struct PS_IN
{
float4 vPosition : SV_POSITION;
float2 vTexcoord : TEXCOORD0;
};
struct PS_OUT_BACKBUFFER
{
float4 vBackBuffer : SV_TARGET0;
};
PS_OUT_BACKBUFFER PS_MAIN_DEBUG(PS_IN In)
{
PS_OUT_BACKBUFFER Out;
Out.vBackBuffer = g_Texture.Sample(DefaultSampler, In.vTexcoord);
return Out;
}
struct PS_OUT_LIGHT
{
vector vShade : SV_TARGET0;
vector vSpecular : SV_TARGET1;
};
PS_OUT_LIGHT PS_MAIN_DIRECTIONAL(PS_IN In)
{
PS_OUT_LIGHT Out;
float2 uv = In.vTexcoord;
float3 vNormal = normalize(g_NormalTexture.Sample(DefaultSampler, uv).xyz * 2.f - 1);
float4 vDepth = g_DepthTexture.Sample(DefaultSampler, uv);
float fViewZ = vDepth.y * g_fFar;
float4 vWorldPosition;
vWorldPosition.x = uv.x * 2 - 1;
vWorldPosition.y = uv.y * -2 + 1;
vWorldPosition.z = vDepth.x;
vWorldPosition.w = 1.f;
vWorldPosition *= fViewZ;
vWorldPosition = mul(vWorldPosition, g_invmatProj);
vWorldPosition = mul(vWorldPosition, g_invMatView);
float fMetallic;
float fRoughness;
float fOcclusion;
float fAttenuation;
float3 vWPos = vWorldPosition.xyz;
float3 vToView = normalize(vWPos - g_vCamPosition.xyz); // 카메라에서 픽셀로 가는 방향
float3 vToLight = normalize(g_vLightDir.xyz); // 라이트에서 오는 라이트 벡터
float3 vAlbedo = g_DiffuseTexture.Sample(DefaultSampler, uv).rgb;
float3 vMRO = g_MROTexture.Sample(DefaultSampler, uv).rgb;
fMetallic = vMRO.r;
fRoughness = vMRO.g;
fOcclusion = vMRO.b;
fAttenuation = 1.f;
PBR_LIGHT_OUT PBR_Out = PBR_Lighting(vNormal, vToView, vToLight, vAlbedo, fMetallic, fRoughness, g_vLightDiffuse.rgb, fAttenuation);
Out.vShade = float4(PBR_Out.vShade, 1.f);
Out.vSpecular = float4(PBR_Out.vSpecular, 1.f);
return Out;
}
PS_OUT_LIGHT PS_MAIN_POINT(PS_IN In)
{
PS_OUT_LIGHT Out;
float2 uv = In.vTexcoord;
float3 vNormal = normalize(g_NormalTexture.Sample(DefaultSampler, uv).xyz * 2.f - 1);
float4 vDepth = g_DepthTexture.Sample(DefaultSampler, uv);
float fViewZ = vDepth.y * g_fFar;
float4 vWorldPosition;
vWorldPosition.x = uv.x * 2 - 1;
vWorldPosition.y = uv.y * -2 + 1;
vWorldPosition.z = vDepth.x;
vWorldPosition.w = 1.f;
vWorldPosition *= fViewZ;
vWorldPosition = mul(vWorldPosition, g_invmatProj);
vWorldPosition = mul(vWorldPosition, g_invMatView);
float3 vWPos = vWorldPosition.xyz;
float3 vToView = normalize(vWPos - g_vCamPosition.xyz); // 카메라에서 픽셀로 가는 방향
float3 vToLight = normalize(g_vLightDir.xyz); // 라이트에서 오는 라이트 벡터
float fMetallic;
float fRoughness;
float fOcclusion;
float fAttenuation;
float fDistance = length(vToLight);
fAttenuation = saturate((g_fLightRange - fDistance) / g_fLightRange);
if (fAttenuation <= 0.f)
{
discard;
}
float3 vAlbedo = g_DiffuseTexture.Sample(DefaultSampler, uv).rgb;
float3 vMRO = g_MROTexture.Sample(DefaultSampler, uv).rgb;
fMetallic = vMRO.r;
fRoughness = vMRO.g;
fOcclusion = vMRO.b;
PBR_LIGHT_OUT PBR_Out = PBR_Lighting(vNormal, vToView, vToLight, vAlbedo, fMetallic, fRoughness, g_vLightDiffuse.rgb, fAttenuation);
Out.vShade = float4(PBR_Out.vShade, 1.f);
Out.vSpecular = float4(PBR_Out.vSpecular, 1.f);
return Out;
}
PS_OUT_LIGHT PS_MAIN_SPOT(PS_IN In)
{
PS_OUT_LIGHT Out;
float2 uv = In.vTexcoord;
float3 vNormal = normalize(g_NormalTexture.Sample(DefaultSampler, uv).xyz * 2.f - 1);
float4 vDepth = g_DepthTexture.Sample(DefaultSampler, uv);
float fViewZ = vDepth.y * g_fFar;
float4 vWorldPosition;
vWorldPosition.x = uv.x * 2 - 1;
vWorldPosition.y = uv.y * -2 + 1;
vWorldPosition.z = vDepth.x;
vWorldPosition.w = 1.f;
vWorldPosition *= fViewZ;
vWorldPosition = mul(vWorldPosition, g_invmatProj);
vWorldPosition = mul(vWorldPosition, g_invMatView);
float3 vWPos = vWorldPosition.xyz;
float3 vToView = normalize(vWPos - g_vCamPosition.xyz); // 카메라에서 픽셀로 가는 방향
float3 vToLight = normalize(g_vLightDir.xyz); // 라이트에서 오는 라이트 벡터
float fMetallic;
float fRoughness;
float fOcclusion;
float fAttenuation;
{
float fDistance = length(vToLight);
fAttenuation = saturate((g_fLightRange - fDistance) / g_fLightRange);
if (fAttenuation <= 0.f)
{
discard;
}
}
float fCosAngle = dot(vToLight, g_vLightDir.xyz);
if (fCosAngle < g_fSpotOuterAngle)
{
discard;
}
float fSpotAttenenuation = saturate((fCosAngle - g_fSpotInnerAngle) / (g_fSpotInnerAngle - g_fSpotOuterAngle));
fAttenuation *= fSpotAttenenuation;
float3 vAlbedo = g_DiffuseTexture.Sample(DefaultSampler, uv).rgb;
float3 vMRO = g_MROTexture.Sample(DefaultSampler, uv).rgb;
fMetallic = vMRO.r;
fRoughness = vMRO.g;
fOcclusion = vMRO.b;
PBR_LIGHT_OUT PBR_Out = PBR_Lighting(vNormal, vToView, vToLight, vAlbedo, fMetallic, fRoughness, g_vLightDiffuse.rgb, fAttenuation);
Out.vShade = float4(PBR_Out.vShade, 1.f);
Out.vSpecular = float4(PBR_Out.vSpecular, 1.f);
return Out;
}
float g_fWeights[32] =
{
0.000400, 0.000700, 0.001200, 0.002000, 0.003200, 0.005000, 0.007500, 0.011000,
0.015700, 0.021700, 0.029000, 0.037300, 0.046200, 0.055200, 0.063600, 0.070800,
0.075900, 0.078500, 0.078500, 0.075900, 0.070800, 0.063600, 0.055200, 0.046200,
0.037300, 0.029000, 0.021700, 0.015700, 0.011000, 0.007500, 0.005000, 0.003200
};
PS_OUT_BACKBUFFER PS_MAIN_COMBINED(PS_IN In)
{
PS_OUT_BACKBUFFER Out;
vector vDiffuse = g_DiffuseTexture.Sample(DefaultSampler, In.vTexcoord);
if (0.f == vDiffuse.a)
{
discard;
}
vector vShade = g_ShadeTexture.Sample(DefaultSampler, In.vTexcoord);
vector vSpecular = g_SpecularTexture.Sample(DefaultSampler, In.vTexcoord);
Out.vBackBuffer = vDiffuse * vShade + vSpecular;
vector vDepthDesc = g_DepthTexture.Sample(DefaultSampler, In.vTexcoord);
float fViewZ = vDepthDesc.y * g_fFar;
vector vPosition, vPreShadowPosition;
/* (로컬위치 * 월드 * 뷰 * 투영 / w) -> (로컬위치 * 월드) */
vPosition.x = In.vTexcoord.x * 2.f - 1.f;
vPosition.y = In.vTexcoord.y * -2.f + 1.f;
vPosition.z = vDepthDesc.x;
vPosition.w = 1.f;
vPosition = vPosition * fViewZ;
vPosition = mul(vPosition, g_invmatProj);
vPosition = mul(vPosition, g_invMatView);
vPreShadowPosition = vPosition;
/* (로컬위치 * 월드) -> (로컬위치 * 월드 * 광원의 뷰 * 광원의 투영 ) */
vPosition = mul(vPosition, g_LightViewMatrix);
vPosition = mul(vPosition, g_LightProjMatrix);
vPreShadowPosition = mul(vPreShadowPosition, g_PreShadowLightViewMatrix);
vPreShadowPosition = mul(vPreShadowPosition, g_PreShadowLightProjMatrix);
/* (로컬위치 * 월드 * 광원의 뷰 * 광원의 투영 ) -> (로컬위치 * 월드 * 광원의 뷰 * 광원의 투영 * (/w) */
float2 vTexcoord, vPreShadowTexcoord;
vTexcoord.x = (vPosition.x / vPosition.w) * 0.5f + 0.5f;
vTexcoord.y = (vPosition.y / vPosition.w) * -0.5f + 0.5f;
vPreShadowTexcoord.x = (vPreShadowPosition.x / vPreShadowPosition.w) * 0.5f + 0.5f;
vPreShadowTexcoord.y = (vPreShadowPosition.y / vPreShadowPosition.w) * -0.5f + 0.5f;
/* 광원의 NDC에서 샘플링 */
float fVisibility_Dynamic = ShadowVisibility_hwPCF(g_ShadowTexture, vPosition, float2(g_iMaxShadowWidth, g_iMaxShadowHeight), 0.0005f);
float fVisibility_Static = ShadowVisibility_hwPCF(g_PreShadowTexture, vPreShadowPosition, float2(g_iMaxShadowWidth, g_iMaxShadowHeight), 0.0005f);
Out.vBackBuffer.rgb *= lerp(0.5f, 1.f, min(fVisibility_Dynamic, fVisibility_Static));
//float fShadowDepth = g_ShadowTexture.Sample(DefaultSampler, vTexcoord).x;
//float fPreShadowDepth = g_PreShadowTexture.Sample(DefaultSampler, vPreShadowTexcoord).x;
//bool IsShadow = { false };
//if (vPosition.z - 0.0005f > fShadowDepth)
//{
// IsShadow = true;
//}
//if (vPreShadowPosition.z - 0.0005f > fPreShadowDepth)
//{
// IsShadow = true;
//}
//if (true == IsShadow)
//{
// Out.vBackBuffer *= 0.5f;
//}
float4 vColor = 0.f;
//int iBlurWeight = g_BlurWeightTexture.Sample(ClampSampler, In.vTexcoord).r * 32.f;
//int iBlurMin;
//int iBlurMax;
//if (iBlurWeight % 2 == 0)
//{
// iBlurMin = -1 * iBlurWeight / 2;
// iBlurMax = iBlurWeight / 2;
//}
//else
//{
// iBlurMin = -iBlurWeight / 2;
// iBlurMax = iBlurWeight / 2 + 1;
//}
//[loop] // 변수로 루프돌리려면 반드시 필요함
for (int i = -15; i < 16; ++i)
{
vTexcoord.x = In.vTexcoord.x;
vTexcoord.y = In.vTexcoord.y + (float) i / g_vResolution.y;
vColor += g_fWeights[i + 15] * g_BlurXTexture.Sample(ClampSampler, vTexcoord);
}
Out.vBackBuffer += vColor;
return Out;
}
struct PS_OUT_BLUR_X
{
float4 vBlurX : SV_TARGET0;
};
PS_OUT_BLUR_X PS_MAIN_BLUR_X(PS_IN In)
{
PS_OUT_BLUR_X Out;
float2 vTexcoord;
float4 vColor = 0.f;
//TODO 블러 조절 해보려햇는데 실패함..
//int iBlurWeight = g_BlurWeightTexture.Sample(ClampSampler, In.vTexcoord).r * 32.f;
//int iBlurMin;
//int iBlurMax;
//if (iBlurWeight % 2 == 0)
//{
// iBlurMin = -1 * iBlurWeight / 2;
// iBlurMax = iBlurWeight / 2;
//}
//else
//{
// iBlurMin = -iBlurWeight / 2;
// iBlurMax = iBlurWeight / 2 + 1;
//}
//[loop] // 변수로 루프돌리려면 반드시 필요함
for (int i = -15; i < 16; ++i)
{
vTexcoord.x = In.vTexcoord.x + (float) i / g_vResolution.x;
vTexcoord.y = In.vTexcoord.y;
vColor += g_fWeights[i + 15] * g_BlurTexture.Sample(ClampSampler, vTexcoord);
}
Out.vBlurX = vColor;
return Out;
}
technique11 DefaultTechnique
{
pass DebugPass // 0
{
SetRasterizerState(RS_Default);
SetDepthStencilState(DSS_Default, 0);
SetBlendState(BS_None, float4(0.f, 0.f, 0.f, 0.f), 0xffffffff);
VertexShader = compile vs_5_0 VS_MAIN();
GeometryShader = NULL;
PixelShader = compile ps_5_0 PS_MAIN_DEBUG();
}
pass DirectionalPass // 1
{
SetRasterizerState(RS_Default);
SetDepthStencilState(DSS_None, 0);
SetBlendState(BS_Blend, float4(0.f, 0.f, 0.f, 0.f), 0xffffffff);
VertexShader = compile vs_5_0 VS_MAIN();
GeometryShader = NULL;
PixelShader = compile ps_5_0 PS_MAIN_DIRECTIONAL();
}
pass PointPass // 2
{
SetRasterizerState(RS_Default);
SetDepthStencilState(DSS_None, 0);
SetBlendState(BS_Blend, float4(0.f, 0.f, 0.f, 0.f), 0xffffffff);
VertexShader = compile vs_5_0 VS_MAIN();
GeometryShader = NULL;
PixelShader = compile ps_5_0 PS_MAIN_POINT();
}
pass CombinedPass // 3
{
SetRasterizerState(RS_Default);
SetDepthStencilState(DSS_None, 0);
SetBlendState(BS_None, float4(0.f, 0.f, 0.f, 0.f), 0xffffffff);
VertexShader = compile vs_5_0 VS_MAIN();
GeometryShader = NULL;
PixelShader = compile ps_5_0 PS_MAIN_COMBINED();
}
pass BlurPass // 4
{
SetRasterizerState(RS_Default);
SetDepthStencilState(DSS_None, 0);
SetBlendState(BS_Blend, float4(0.f, 0.f, 0.f, 0.f), 0xffffffff);
VertexShader = compile vs_5_0 VS_MAIN();
GeometryShader = NULL;
PixelShader = compile ps_5_0 PS_MAIN_BLUR_X();
}
pass SpotPass // 5
{
SetRasterizerState(RS_Default);
SetDepthStencilState(DSS_None, 0);
SetBlendState(BS_Blend, float4(0.f, 0.f, 0.f, 0.f), 0xffffffff);
VertexShader = compile vs_5_0 VS_MAIN();
GeometryShader = NULL;
PixelShader = compile ps_5_0 PS_MAIN_SPOT();
}
}
@@ -0,0 +1,475 @@
#include "Engine_Shader_Defines.hlsli"
matrix g_WorldMatrix, g_ViewMatrix, g_ProjMatrix;
matrix g_invMatView, g_invmatProj;
matrix g_LightViewMatrix, g_LightProjMatrix;
matrix g_PreShadowLightViewMatrix, g_PreShadowLightProjMatrix;
float g_fFar;
float g_fPreShadowFar;
uint g_iMaxShadowWidth;
uint g_iMaxShadowHeight;
float2 g_vResolution;
Texture2D g_Texture;
float g_fLightRange;
float g_fSpotInnerAngle;
float g_fSpotOuterAngle;
vector g_vLightDir;
vector g_vLightPos;
vector g_vLightPosOffset;
vector g_vCamPosition;
Texture2D g_NormalTexture;
Texture2D g_DiffuseTexture;
Texture2D g_ShadeTexture;
Texture2D g_DepthTexture;
Texture2D g_SpecularTexture;
Texture2D g_ShadowTexture;
Texture2D g_PreShadowTexture;
Texture2D g_BlurTexture;
Texture2D g_BlurXTexture;
Texture2D g_BlurWeightTexture;
Texture2D g_MROTexture;
vector g_vLightDiffuse;
vector g_vLightAmbient;
vector g_vLightSpecular;
struct VS_IN
{
float3 vPosition : POSITION;
float2 vTexcoord : TEXCOORD0;
};
struct VS_OUT
{
float4 vPosition : SV_POSITION;
float2 vTexcoord : TEXCOORD0;
};
VS_OUT VS_MAIN(VS_IN In)
{
VS_OUT Out;
matrix matWV, matWVP;
matWV = mul(g_WorldMatrix, g_ViewMatrix);
matWVP = mul(matWV, g_ProjMatrix);
Out.vPosition = mul(vector(In.vPosition, 1.f), matWVP);
Out.vTexcoord = In.vTexcoord;
return Out;
}
struct PS_IN
{
float4 vPosition : SV_POSITION;
float2 vTexcoord : TEXCOORD0;
};
struct PS_OUT_BACKBUFFER
{
float4 vBackBuffer : SV_TARGET0;
};
PS_OUT_BACKBUFFER PS_MAIN_DEBUG(PS_IN In)
{
PS_OUT_BACKBUFFER Out;
Out.vBackBuffer = g_Texture.Sample(DefaultSampler, In.vTexcoord);
return Out;
}
struct PS_OUT_LIGHT
{
vector vShade : SV_TARGET0;
vector vSpecular : SV_TARGET1;
};
PS_OUT_LIGHT PS_MAIN_DIRECTIONAL(PS_IN In)
{
PS_OUT_LIGHT Out;
float2 uv = In.vTexcoord;
float3 vNormal = normalize(g_NormalTexture.Sample(DefaultSampler, uv).xyz * 2.f - 1);
float4 vDepth = g_DepthTexture.Sample(DefaultSampler, uv);
float fViewZ = vDepth.y * g_fFar;
float4 vWorldPosition;
vWorldPosition.x = uv.x * 2 - 1;
vWorldPosition.y = uv.y * -2 + 1;
vWorldPosition.z = vDepth.x;
vWorldPosition.w = 1.f;
vWorldPosition *= fViewZ;
vWorldPosition = mul(vWorldPosition, g_invmatProj);
vWorldPosition = mul(vWorldPosition, g_invMatView);
float fMetallic;
float fRoughness;
float fOcclusion;
float fAttenuation;
float3 vWPos = vWorldPosition.xyz;
float3 vToView = normalize(vWPos - g_vCamPosition.xyz); // 카메라에서 픽셀로 가는 방향
float3 vToLight = normalize(g_vLightDir.xyz); // 라이트에서 오는 라이트 벡터
float3 vAlbedo = g_DiffuseTexture.Sample(DefaultSampler, uv).rgb;
float3 vMRO = g_MROTexture.Sample(DefaultSampler, uv).rgb;
fMetallic = vMRO.r;
fRoughness = vMRO.g;
fOcclusion = vMRO.b;
fAttenuation = 1.f;
PBR_LIGHT_OUT PBR_Out = PBR_Lighting(vNormal, vToView, vToLight, vAlbedo, fMetallic, fRoughness, g_vLightDiffuse.rgb, fAttenuation);
Out.vShade = float4(PBR_Out.vShade, 1.f);
Out.vSpecular = float4(PBR_Out.vSpecular, 1.f);
return Out;
}
PS_OUT_LIGHT PS_MAIN_POINT(PS_IN In)
{
PS_OUT_LIGHT Out;
float2 uv = In.vTexcoord;
float3 vNormal = normalize(g_NormalTexture.Sample(DefaultSampler, uv).xyz * 2.f - 1);
float4 vDepth = g_DepthTexture.Sample(DefaultSampler, uv);
float fViewZ = vDepth.y * g_fFar;
float4 vWorldPosition;
vWorldPosition.x = uv.x * 2 - 1;
vWorldPosition.y = uv.y * -2 + 1;
vWorldPosition.z = vDepth.x;
vWorldPosition.w = 1.f;
vWorldPosition *= fViewZ;
vWorldPosition = mul(vWorldPosition, g_invmatProj);
vWorldPosition = mul(vWorldPosition, g_invMatView);
float3 vWPos = vWorldPosition.xyz;
float3 vToView = normalize(vWPos - g_vCamPosition.xyz); // 카메라에서 픽셀로 가는 방향
float3 vToLight = normalize(g_vLightDir.xyz); // 라이트에서 오는 라이트 벡터
float fMetallic;
float fRoughness;
float fOcclusion;
float fAttenuation;
float fDistance = length(vToLight);
fAttenuation = saturate((g_fLightRange - fDistance) / g_fLightRange);
if (fAttenuation <= 0.f)
{
discard;
}
float3 vAlbedo = g_DiffuseTexture.Sample(DefaultSampler, uv).rgb;
float3 vMRO = g_MROTexture.Sample(DefaultSampler, uv).rgb;
fMetallic = vMRO.r;
fRoughness = vMRO.g;
fOcclusion = vMRO.b;
PBR_LIGHT_OUT PBR_Out = PBR_Lighting(vNormal, vToView, vToLight, vAlbedo, fMetallic, fRoughness, g_vLightDiffuse.rgb, fAttenuation);
Out.vShade = float4(PBR_Out.vShade, 1.f);
Out.vSpecular = float4(PBR_Out.vSpecular, 1.f);
return Out;
}
PS_OUT_LIGHT PS_MAIN_SPOT(PS_IN In)
{
PS_OUT_LIGHT Out;
float2 uv = In.vTexcoord;
float3 vNormal = normalize(g_NormalTexture.Sample(DefaultSampler, uv).xyz * 2.f - 1);
float4 vDepth = g_DepthTexture.Sample(DefaultSampler, uv);
float fViewZ = vDepth.y * g_fFar;
float4 vWorldPosition;
vWorldPosition.x = uv.x * 2 - 1;
vWorldPosition.y = uv.y * -2 + 1;
vWorldPosition.z = vDepth.x;
vWorldPosition.w = 1.f;
vWorldPosition *= fViewZ;
vWorldPosition = mul(vWorldPosition, g_invmatProj);
vWorldPosition = mul(vWorldPosition, g_invMatView);
float3 vWPos = vWorldPosition.xyz;
float3 vToView = normalize(vWPos - g_vCamPosition.xyz); // 카메라에서 픽셀로 가는 방향
float3 vToLight = normalize(g_vLightDir.xyz); // 라이트에서 오는 라이트 벡터
float fMetallic;
float fRoughness;
float fOcclusion;
float fAttenuation;
{
float fDistance = length(vToLight);
fAttenuation = saturate((g_fLightRange - fDistance) / g_fLightRange);
if (fAttenuation <= 0.f)
{
discard;
}
}
float fCosAngle = dot(vToLight, g_vLightDir.xyz);
if (fCosAngle < g_fSpotOuterAngle)
{
discard;
}
float fSpotAttenenuation = saturate((fCosAngle - g_fSpotInnerAngle) / (g_fSpotInnerAngle - g_fSpotOuterAngle));
fAttenuation *= fSpotAttenenuation;
float3 vAlbedo = g_DiffuseTexture.Sample(DefaultSampler, uv).rgb;
float3 vMRO = g_MROTexture.Sample(DefaultSampler, uv).rgb;
fMetallic = vMRO.r;
fRoughness = vMRO.g;
fOcclusion = vMRO.b;
PBR_LIGHT_OUT PBR_Out = PBR_Lighting(vNormal, vToView, vToLight, vAlbedo, fMetallic, fRoughness, g_vLightDiffuse.rgb, fAttenuation);
Out.vShade = float4(PBR_Out.vShade, 1.f);
Out.vSpecular = float4(PBR_Out.vSpecular, 1.f);
return Out;
}
float g_fWeights[32] =
{
0.000400, 0.000700, 0.001200, 0.002000, 0.003200, 0.005000, 0.007500, 0.011000,
0.015700, 0.021700, 0.029000, 0.037300, 0.046200, 0.055200, 0.063600, 0.070800,
0.075900, 0.078500, 0.078500, 0.075900, 0.070800, 0.063600, 0.055200, 0.046200,
0.037300, 0.029000, 0.021700, 0.015700, 0.011000, 0.007500, 0.005000, 0.003200
};
PS_OUT_BACKBUFFER PS_MAIN_COMBINED(PS_IN In)
{
PS_OUT_BACKBUFFER Out;
vector vDiffuse = g_DiffuseTexture.Sample(DefaultSampler, In.vTexcoord);
if (0.f == vDiffuse.a)
{
discard;
}
vector vShade = g_ShadeTexture.Sample(DefaultSampler, In.vTexcoord);
vector vSpecular = g_SpecularTexture.Sample(DefaultSampler, In.vTexcoord);
Out.vBackBuffer = vDiffuse * vShade + vSpecular;
vector vDepthDesc = g_DepthTexture.Sample(DefaultSampler, In.vTexcoord);
float fViewZ = vDepthDesc.y * g_fFar;
vector vPosition, vPreShadowPosition;
/* (로컬위치 * 월드 * 뷰 * 투영 / w) -> (로컬위치 * 월드) */
vPosition.x = In.vTexcoord.x * 2.f - 1.f;
vPosition.y = In.vTexcoord.y * -2.f + 1.f;
vPosition.z = vDepthDesc.x;
vPosition.w = 1.f;
vPosition = vPosition * fViewZ;
vPosition = mul(vPosition, g_invmatProj);
vPosition = mul(vPosition, g_invMatView);
vPreShadowPosition = vPosition;
/* (로컬위치 * 월드) -> (로컬위치 * 월드 * 광원의 뷰 * 광원의 투영 ) */
vPosition = mul(vPosition, g_LightViewMatrix);
vPosition = mul(vPosition, g_LightProjMatrix);
vPreShadowPosition = mul(vPreShadowPosition, g_PreShadowLightViewMatrix);
vPreShadowPosition = mul(vPreShadowPosition, g_PreShadowLightProjMatrix);
/* (로컬위치 * 월드 * 광원의 뷰 * 광원의 투영 ) -> (로컬위치 * 월드 * 광원의 뷰 * 광원의 투영 * (/w) */
float2 vTexcoord, vPreShadowTexcoord;
vTexcoord.x = (vPosition.x / vPosition.w) * 0.5f + 0.5f;
vTexcoord.y = (vPosition.y / vPosition.w) * -0.5f + 0.5f;
vPreShadowTexcoord.x = (vPreShadowPosition.x / vPreShadowPosition.w) * 0.5f + 0.5f;
vPreShadowTexcoord.y = (vPreShadowPosition.y / vPreShadowPosition.w) * -0.5f + 0.5f;
/* 광원의 NDC에서 샘플링 */
float fVisibility_Dynamic = ShadowVisibility_hwPCF(g_ShadowTexture, vPosition, float2(g_iMaxShadowWidth, g_iMaxShadowHeight), 0.0005f);
float fVisibility_Static = ShadowVisibility_hwPCF(g_PreShadowTexture, vPreShadowPosition, float2(g_iMaxShadowWidth, g_iMaxShadowHeight), 0.0005f);
Out.vBackBuffer.rgb *= lerp(0.5f, 1.f, min(fVisibility_Dynamic, fVisibility_Static));
//float fShadowDepth = g_ShadowTexture.Sample(DefaultSampler, vTexcoord).x;
//float fPreShadowDepth = g_PreShadowTexture.Sample(DefaultSampler, vPreShadowTexcoord).x;
//bool IsShadow = { false };
//if (vPosition.z - 0.0005f > fShadowDepth)
//{
// IsShadow = true;
//}
//if (vPreShadowPosition.z - 0.0005f > fPreShadowDepth)
//{
// IsShadow = true;
//}
//if (true == IsShadow)
//{
// Out.vBackBuffer *= 0.5f;
//}
float4 vColor = 0.f;
//int iBlurWeight = g_BlurWeightTexture.Sample(ClampSampler, In.vTexcoord).r * 32.f;
//int iBlurMin;
//int iBlurMax;
//if (iBlurWeight % 2 == 0)
//{
// iBlurMin = -1 * iBlurWeight / 2;
// iBlurMax = iBlurWeight / 2;
//}
//else
//{
// iBlurMin = -iBlurWeight / 2;
// iBlurMax = iBlurWeight / 2 + 1;
//}
//[loop] // 변수로 루프돌리려면 반드시 필요함
for (int i = -15; i < 16; ++i)
{
vTexcoord.x = In.vTexcoord.x;
vTexcoord.y = In.vTexcoord.y + (float) i / g_vResolution.y;
vColor += g_fWeights[i + 15] * g_BlurXTexture.Sample(ClampSampler, vTexcoord);
}
Out.vBackBuffer += vColor;
return Out;
}
struct PS_OUT_BLUR_X
{
float4 vBlurX : SV_TARGET0;
};
PS_OUT_BLUR_X PS_MAIN_BLUR_X(PS_IN In)
{
PS_OUT_BLUR_X Out;
float2 vTexcoord;
float4 vColor = 0.f;
//TODO 블러 조절 해보려햇는데 실패함..
//int iBlurWeight = g_BlurWeightTexture.Sample(ClampSampler, In.vTexcoord).r * 32.f;
//int iBlurMin;
//int iBlurMax;
//if (iBlurWeight % 2 == 0)
//{
// iBlurMin = -1 * iBlurWeight / 2;
// iBlurMax = iBlurWeight / 2;
//}
//else
//{
// iBlurMin = -iBlurWeight / 2;
// iBlurMax = iBlurWeight / 2 + 1;
//}
//[loop] // 변수로 루프돌리려면 반드시 필요함
for (int i = -15; i < 16; ++i)
{
vTexcoord.x = In.vTexcoord.x + (float) i / g_vResolution.x;
vTexcoord.y = In.vTexcoord.y;
vColor += g_fWeights[i + 15] * g_BlurTexture.Sample(ClampSampler, vTexcoord);
}
Out.vBlurX = vColor;
return Out;
}
technique11 DefaultTechnique
{
pass DebugPass // 0
{
SetRasterizerState(RS_Default);
SetDepthStencilState(DSS_Default, 0);
SetBlendState(BS_None, float4(0.f, 0.f, 0.f, 0.f), 0xffffffff);
VertexShader = compile vs_5_0 VS_MAIN();
GeometryShader = NULL;
PixelShader = compile ps_5_0 PS_MAIN_DEBUG();
}
pass DirectionalPass // 1
{
SetRasterizerState(RS_Default);
SetDepthStencilState(DSS_None, 0);
SetBlendState(BS_Blend, float4(0.f, 0.f, 0.f, 0.f), 0xffffffff);
VertexShader = compile vs_5_0 VS_MAIN();
GeometryShader = NULL;
PixelShader = compile ps_5_0 PS_MAIN_DIRECTIONAL();
}
pass PointPass // 2
{
SetRasterizerState(RS_Default);
SetDepthStencilState(DSS_None, 0);
SetBlendState(BS_Blend, float4(0.f, 0.f, 0.f, 0.f), 0xffffffff);
VertexShader = compile vs_5_0 VS_MAIN();
GeometryShader = NULL;
PixelShader = compile ps_5_0 PS_MAIN_POINT();
}
pass CombinedPass // 3
{
SetRasterizerState(RS_Default);
SetDepthStencilState(DSS_None, 0);
SetBlendState(BS_None, float4(0.f, 0.f, 0.f, 0.f), 0xffffffff);
VertexShader = compile vs_5_0 VS_MAIN();
GeometryShader = NULL;
PixelShader = compile ps_5_0 PS_MAIN_COMBINED();
}
pass BlurPass // 4
{
SetRasterizerState(RS_Default);
SetDepthStencilState(DSS_None, 0);
SetBlendState(BS_Blend, float4(0.f, 0.f, 0.f, 0.f), 0xffffffff);
VertexShader = compile vs_5_0 VS_MAIN();
GeometryShader = NULL;
PixelShader = compile ps_5_0 PS_MAIN_BLUR_X();
}
pass SpotPass // 5
{
SetRasterizerState(RS_Default);
SetDepthStencilState(DSS_None, 0);
SetBlendState(BS_Blend, float4(0.f, 0.f, 0.f, 0.f), 0xffffffff);
VertexShader = compile vs_5_0 VS_MAIN();
GeometryShader = NULL;
PixelShader = compile ps_5_0 PS_MAIN_SPOT();
}
}
@@ -0,0 +1,366 @@
vector g_vMtrlDiffuse = 1.f;
vector g_vMtrlAmbient = 1.f;
vector g_vMtrlSpecular = 1.f;
float PI = 3.14592;
sampler DefaultSampler = sampler_state
{
Filter = MIN_MAG_MIP_LINEAR;
AddressU = wrap;
AddressV = wrap;
};
sampler ClampSampler = sampler_state
{
Filter = MIN_MAG_MIP_POINT;
AddressU = clamp;
AddressV = clamp;
};
sampler MirrorSampler = sampler_state
{
Filter = MIN_MAG_MIP_LINEAR;
AddressU = mirror;
AddressV = mirror;
};
sampler BorderZeroSampler = sampler_state
{
Filter = MIN_MAG_MIP_POINT;
AddressU = Border;
AddressV = Border;
BorderColor = float4(0, 0, 0, 0);
};
sampler PointSampler = sampler_state
{
Filter = MIN_MAG_MIP_POINT;
AddressU = wrap;
AddressV = wrap;
};
RasterizerState RS_Default
{
FillMode = Solid;
CullMode = Back;
};
RasterizerState RS_Nocull
{
FillMode = Solid;
CullMode = None;
FrontCounterClockwise = false;
DepthClipEnable = false;
};
RasterizerState RS_Nocull_Wireframe
{
FillMode = Wireframe;
CullMode = None;
FrontCounterClockwise = false;
DepthBias = 0;
SlopeScaledDepthBias = 0.f;
DepthBiasClamp = 0.f;
DepthClipEnable = false;
ScissorEnable = false;
MultisampleEnable = false;
AntialiasedLineEnable = false;
};
RasterizerState RS_Scissor
{
FillMode = Solid;
CullMode = Back;
FrontCounterClockwise = false;
ScissorEnable = true;
};
DepthStencilState DSS_Default
{
DepthEnable = true;
DepthWriteMask = all;
DepthFunc = less_equal;
};
DepthStencilState DSS_None
{
DepthEnable = false;
DepthWriteMask = zero;
};
DepthStencilState DSS_Effect
{
DepthEnable = true;
DepthWriteMask = zero;
DepthFunc = less_equal;
StencilEnable = false;
};
BlendState BS_None
{
BlendEnable[0] = false;
};
BlendState BS_AlphaBlend // ALPHA_BLEND
{
BlendEnable[0] = true;
BlendEnable[1] = true;
BlendEnable[2] = true;
BlendEnable[3] = true;
BlendEnable[4] = true;
BlendEnable[5] = true;
BlendEnable[6] = true;
BlendEnable[7] = true;
// Ac * Aa + Bc * (1 - Aa);
SrcBlend = Src_Alpha;
DestBlend = Inv_Src_Alpha;
BlendOp = Add;
};
BlendState BS_Blend // ADDITIVE_BLEND
{
BlendEnable[0] = true;
BlendEnable[1] = true;
BlendEnable[2] = true;
BlendEnable[3] = true;
BlendEnable[4] = true;
BlendEnable[5] = true;
BlendEnable[6] = true;
BlendEnable[7] = true;
// Ac * (1) + Bc * (1);
SrcBlend = one;
DestBlend = one;
BlendOp = Add;
};
BlendState BS_UIBlend
{
BlendEnable[0] = true;
BlendEnable[1] = true;
BlendEnable[2] = true;
BlendEnable[3] = true;
BlendEnable[4] = true;
BlendEnable[5] = true;
BlendEnable[6] = true;
BlendEnable[7] = true;
SrcBlend = Src_Alpha;
DestBlend = Inv_Src_Alpha;
BlendOp = Add;
SrcBlendAlpha = One;
DestBlendAlpha = Zero;
BlendOpAlpha = Add;
RenderTargetWriteMask[0] = 0x0F; // RGBA 모두 출력
};
BlendState BS_WB_Acc
{
BlendEnable[0] = true;
BlendEnable[1] = true;
BlendEnable[2] = true;
BlendEnable[3] = true;
BlendEnable[4] = true;
BlendEnable[5] = true;
BlendEnable[6] = true;
BlendEnable[7] = true;
SrcBlend[0] = one;
DestBlend[0] = one;
BlendOp[0] = Add;
SrcBlend[1] = Zero;
DestBlend[1] = Inv_Src_Alpha;
BlendOp[1] = Add;
SrcBlend[2] = one;
DestBlend[2] = one;
BlendOp[2] = Add;
};
BlendState BS_WeightBlend
{
BlendEnable[0] = true;
BlendEnable[1] = true;
BlendEnable[2] = true;
BlendEnable[3] = true;
BlendEnable[4] = true;
BlendEnable[5] = true;
BlendEnable[6] = true;
BlendEnable[7] = true;
SrcBlend[0] = Src_Alpha;
DestBlend[0] = Inv_Src_Alpha;
BlendOp[0] = Add;
};
float ShadowVisibility_hwPCF(Texture2D ShadowMap, float4 vLightClip, float2 vShadowMapSize, float bias)
{
float2 uv = float2(vLightClip.x / vLightClip.w, -vLightClip.y / vLightClip.w) * 0.5f + 0.5f;
float fDepthCenter = saturate(vLightClip.z / vLightClip.w);
float2 vTexel = 1.f / vShadowMapSize;
float fVisibility = 0.f;
for (int v = -1; v <= 1; ++v)
{
for (int u = -1; u <= 1; ++u)
{
float2 vOffset = float2(u, v) * vTexel;
float fDepth = ShadowMap.Sample(DefaultSampler, uv + vOffset).x;
fVisibility += (fDepthCenter - bias > fDepth) ? 0.f : 1.f;
}
}
return fVisibility / 9.f;
}
float GetRimLight(float3 vCamPosition, float3 vPosition, float3 vNormal, float fRimPower, float fRimStrength)
{
float fRimLight = (1 - dot(normalize(vCamPosition - vPosition), vNormal));
fRimLight = pow(fRimLight, fRimPower);
fRimLight = fRimLight * fRimStrength;
return fRimLight;
}
float2 Get_MovedUV(float2 vOriginalUV, float fDeltaU, float fDeltaV, uint iIndexU, uint iIndexV)
{
float2 vDelta = float2(fDeltaU, fDeltaV);
float2 vOffset = float2(iIndexU * fDeltaU, iIndexV * fDeltaV);
return vOriginalUV * vDelta + vOffset;
}
float2 UV_Cutting(float2 vUV, float2 vUVCutting, int iCurrentFrame)
{
float2 UV = vUV; // 이미지의 UV값
int iTotalFrame = vUVCutting.x * vUVCutting.y; // 이미지의 최대 프레임 (몇 곱하기 몇인지)
int iFrameX = iCurrentFrame % (int) vUVCutting.x; // 현재 x축의 위치(현재 이미지의 몇번째 칸을 보여줄 지)
int iFrameY = iCurrentFrame / (int) vUVCutting.x; // 현재 y축의 위치(현재 이미지의 몇번째 줄을 보여줄 지)
float fFreamWidth = 1.0 / vUVCutting.x; // 1.0 나누기 이미지 갯수를 해서 한칸에 얼마나 갈지 정해준다.
float fFreamHeight = 1.0 / vUVCutting.y; // 1.0 나누기 이미지 갯수를 해서 한줄에 얼마나 갈지 정해준다.
UV.x = UV.x * fFreamWidth + iFrameX * fFreamWidth; // 먼저 uv를 0~1이 아닌 0~fFrameWidth로 만든 다음에 한칸씩 옆으로 밀어준다.
UV.y = UV.y * fFreamHeight + iFrameY * fFreamHeight; // 먼저 uv를 0~1이 아닌 0~fFrameHeight로 만든 다음에 한줄씩 밑으로 내려준다.
return UV;
}
struct PBR_LIGHT_OUT
{
float3 vShade;
float3 vSpecular;
};
// (Kd F(Lambert)) + (Ks F(cook-torrance))
//float LambertDiffuse(float c)
//{
// return c / PI;
//}
// F(cook-torrance)
// DFG / (4 (Wo dot N) (Wi dot N))
// D(distribution, NormalDistribution function), 표면의 거칠기에 영향을 받는 halfWay vector
// G(Geometry, GeometryFunc), 에너지 보존법칙 적용
// F(Fresnel-Fresnel equation), 표면 각도에 따른 반사율
// F->D
float NDF_ggxtr(float3 n, float3 h, float a) // NormalDistributionGGXTR, (H, halfWay vector), (A, Roughness), (N, Normal?)
{
float a2 = a * a;
float NdotH = saturate(dot(n, h));
float NdotH2 = NdotH * NdotH;
float nom = a2;
float denom = (NdotH2 * (a2 - 1.f) + 1.f);
denom = PI * denom * denom;
return (nom / denom);
}
// F->G
// GschlickGGX(n, v, k)
// k는 이하 두개 조명 조건에 따라 가변적임
// Direct // Kdir -> ((a + 1) * (a + 1)) / 8 // direct lighting 추천
// IBL Lighting // Kibl -> a * a / 2 // 이미지 기반 조명기법
float Geometry_SchlickGGX(float NdotV, float fK)
{
return NdotV / (NdotV * (1.0f - fK) + fK);
}
float Geometry_Smith(float3 n, float3 v, float3 l, float k)
{
// Geometry Obstruction
// 철이 있는, 주로 튀어나온 장애물
float NdotV = saturate(dot(n, v));
// Geometry Shadowing
// 요가 있는, 주로 음푹 들어간 장애물 (그래서 셰도잉)
float NdotL = saturate(dot(n, l));
// 요철이 골고루 있다고 가정하고 적당히 섞음
return Geometry_SchlickGGX(NdotV, k) * Geometry_SchlickGGX(NdotL, k);
}
// F -> F
float3 Fresnel_Schlick(float cosTheta, float3 F0)
{
// 모서리 부분의 반사
return F0 + (1.0f - F0) * pow(1.0f - cosTheta, 5.0f);
}
PBR_LIGHT_OUT PBR_Lighting(
float3 vNormal, float3 vToView, float3 vToLight,
float3 vAlbedo, float fMetallic, float fRoughness,
float3 vLightColor, float fAttenuation
) {
PBR_LIGHT_OUT Out;
Out.vShade = 0;
Out.vSpecular = 0;
float3 vHalfVector = normalize(vToView + vToLight);
float NdotL = saturate(dot(vNormal, vToLight));
float NdotV = saturate(dot(vNormal, vToView));
if (NdotL <= 0 || NdotV <= 0)
{
return Out;
}
float fAlpha = max(fRoughness * fRoughness, 0.04f);
float k = ((fRoughness + 1.f) * (fRoughness + 1.f)) / 8.f;
float3 F0 = lerp(float3(0.04f, 0.04f, 0.04f), vAlbedo, fMetallic);
float D = NDF_ggxtr(vNormal, vHalfVector, fAlpha);
float G = Geometry_Smith(vNormal, vToView, vToLight, k);
float3 F = Fresnel_Schlick(saturate(dot(vHalfVector, vToView)), F0);
float3 vNumerator = D * G * F;
float fDenom = max(4.f * NdotL * NdotV, 1e-7);
float3 specularBRDF = vNumerator / fDenom;
float3 kS = F;
float3 kD = (1.f - kS) * (1.f -fMetallic);
Out.vShade = (kD / PI) * (NdotL * fAttenuation) * vLightColor;
Out.vSpecular = specularBRDF * vLightColor * fAttenuation * NdotL;
return Out;
}