Study/RenderMonkey - 2019년 백업

[RenderMonkey] Shadow 2/2

김성인 2023. 12. 6. 23:34

[RenderMonkey] Shadow 2/2


// Vertex Shader

struct VS_INPUT
{
    float4 mPosition : POSITION;
    float3 mNormal : NORMAL;
};

struct VS_OUTPUT
{
    float4 mPosition : POSITION;
    float4 mClipPosition : TEXCOORD1;
    float mDiffuse : TEXCOORD2;
};

float4x4 gWorldMatrix;
float4x4 gLightViewMatrix;
float4x4 gLightProjectionMatrix;

float4 gWorldLightPosition;

float4x4 gViewProjectionMatrix;

VS_OUTPUT vs_main(VS_INPUT Input)
{
    VS_OUTPUT Output;

    float4x4 lightViewMatrix = gLightViewMatrix;

    float3 dirZ = -normalize(gWorldLightPosition.xyz);
    float3 up = float3(0, 1, 0);
    float3 dirX = cross(up, dirZ);
    float3 dirY = cross(dirZ, dirX);;

    lightViewMatrix = float4x4(
        float4(dirX, -dot(gWorldLightPosition.xyz, dirX)),
        float4(dirY, -dot(gWorldLightPosition.xyz, dirY)),
        float4(dirZ, -dot(gWorldLightPosition.xyz, dirZ)),
        float4(0, 0, 0, 1));
    lightViewMatrix = transpose(lightViewMatrix);

    float4 worldPosition = mul(Input.mPosition, gWorldMatrix);
    Output.mPosition = mul(worldPosition, gViewProjectionMatrix);

    Output.mClipPosition = mul(worldPosition, lightViewMatrix);
    Output.mClipPosition = mul(Output.mClipPosition, gLightProjectionMatrix);

    float3 lightDir = normalize(worldPosition.xyz - gWorldLightPosition.xyz);
    float3 worldNormal = normalize(mul(Input.mNormal, (float3x3)gWorldMatrix));
    Output.mDiffuse = dot(-lightDir, worldNormal);

    return Output;
}

// Pixel Shader

sampler2D ShadowSampler;
float4 gObjectColor;

struct PS_INPUT
{
    float4 mClipPosition: TEXCOORD1;
    float mDiffuse : TEXCOORD2;
};

float4 ps_main(PS_INPUT Input) : COLOR
{
   float3 rgb = saturate(Input.mDiffuse) * gObjectColor;

   float currentDepth = Input.mClipPosition.z / Input.mClipPosition.w;

   float2 uv = Input.mClipPosition.xy / Input.mClipPosition.w;
   uv.y = -uv.y;
   uv = uv * 0.5 + 0.5;

   float shadowDepth = tex2D(ShadowSampler, uv).r;

   if (currentDepth > shadowDepth + 0.0000125f)
   {
      rgb *= 0.5f;
   }

   return(float4(rgb, 1.0f));
}

'Study > RenderMonkey - 2019년 백업' 카테고리의 다른 글

[RenderMonkey] Edge Detection  (0) 2023.12.06
[RenderMonkey] Color Conversion  (1) 2023.12.06
[RenderMonkey] Shadow 1/2  (0) 2023.12.06
[RenderMonkey] Vertex Animation  (1) 2023.12.06
[RenderMonkey] UV Animation  (1) 2023.12.06