Study/RenderMonkey - 2019년 백업

[RenderMonkey] Mapping

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

[RenderMonkey] Mapping


// Vertex Shader

float4x4 gWorldMatrix;
float4x4 gViewMatrix;
float4x4 gProjectionMatrix;

float4 gWorldLightPosition;
float4 gWorldCameraPosition;

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

struct VS_OUTPUT
{
    float4 mPosition : POSITION;
    float2 mUV : TEXCOORD0;
    float3 mDiffuse : TEXCOORD1;
    float3 mViewDir : TEXCOORD2;
    float3 mReflection : TEXCOORD3;
};

VS_OUTPUT vs_main(VS_INPUT Input)
{
    VS_OUTPUT Output;

    Output.mPosition = mul(Input.mPosition, gWorldMatrix);

    float3 lightDir = Output.mPosition.xyz - gWorldLightPosition.xyz;
    lightDir = normalize(lightDir);

    float3 viewDir = normalize(Output.mPosition.xyz - gWorldCameraPosition.xyz);
    Output.mViewDir = viewDir;

    Output.mPosition = mul(Output.mPosition, gViewMatrix);
    Output.mPosition = mul(Output.mPosition, gProjectionMatrix);

    float3 worldNormal = mul(Input.mNormal, (float3x3)gWorldMatrix);
    worldNormal = normalize(worldNormal);

    Output.mDiffuse = dot(-lightDir, worldNormal);
    Output.mReflection = reflect(lightDir, worldNormal);

    Output.mUV = Input.mUV;

    return Output;
}

// Pixel Shader

struct PS_INPUT
{
    float2 mUV : TEXCOORD0;
    float3 mDiffuse : TEXCOORD1;
    float3 mViewDir : TEXCOORD2;
    float3 mReflection : TEXCOORD3;
};

sampler2D DiffuseSampler;
sampler2D SpecularSampler;

float3 gLightColor;

float4 ps_main(PS_INPUT Input) : COLOR
{
   float4 albedo = tex2D(DiffuseSampler, Input.mUV);
   float3 diffuse = gLightColor * albedo.rgb * saturate(Input.mDiffuse);

   float3 reflection = normalize(Input.mReflection);
   float3 viewDir = normalize(Input.mViewDir);
   float3 specular = 0;

   if (diffuse.x > 0)
   {
      specular = saturate(dot(reflection, -viewDir));
      specular = pow(specular, 20.0f);

      float4 specularIntensity = tex2D(SpecularSampler, Input.mUV);
      specular *= specularIntensity.rgb * gLightColor;
   }

   float3 ambient = float3(0.1f, 0.1f, 0.1f) * albedo;

   return float4(ambient + diffuse + specular, 1);
}

// Stream Mapping
// 정점버퍼의 법선 시맨틱 NORMAL 추가로 Usage > NORMAL, FLOAT3 추가
// UV좌표 사용 하기 위해 TEXCOORD, FLOAT2 추가

// gWorldLightPosition

// gWorldCameraPosition

// gLightColor
// 라이트 칼라

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

[RenderMonkey] Normal Mapping  (1) 2023.12.06
[RenderMonkey] ToonShading  (1) 2023.12.06
[RenderMonkey] Specular, ambient  (1) 2023.12.06
[Render Monkey] Phong  (1) 2023.12.06
[RenderMonkey] Diffuse  (1) 2023.12.06