기본적인 조명 셰이더

 

 

정점셰이더

 

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

 

 

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

 

 

float4x4 gWorldMatrix;
float4x4 gViewMatrix;
float4x4 gProjectionMatrix;

float4 gWorldLightingPosition;
float4 gWorldCameraPosition;

 


VS_OUTPUT vs_main (VS_INPUT Input){
   VS_OUTPUT Output;
  
   Output.mPosition = mul(Input.mPosition, gWorldMatrix);
  
   float3 lightDir = Output.mPosition.xyz - gWorldLightingPosition;
   //월드노멀에 라이팅 구하기
   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);
  
   return Output;
}

 

 

픽셀 셰이더

 

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

 

float4 ps_main(PS_INPUT Input):COLOR{
   float3 diffuse = saturate(Input.mDiffuse);
  
   float3 reflection = normalize(Input.mReflection);
   float3 viewDir = normalize(Input.mViewDir);
   float3 spacular = 0;
  
   if( diffuse.x > 0){
      spacular = saturate(dot(reflection,-viewDir));
      spacular = pow(spacular, 20.0f);
   }
   //diffuse.x값이 0보다 크면  스펙큘러 구현.
  
   float3 ambient = float3(0.1f, 0.1f, 0.1f);

//엠비언트추가

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

 

 

 

 

**HLSL함수**

normalize() :정규화 함수

saturate() : 0~1을 넘는 값을 잘라 냄

reflect() : 벡터반사 함수

pow() : 거듭제곱 함수

dot() : 내적 함수

 

 

 

 

 

 

 

 

 

 

 

 

 

Posted by 프리랜서 디자이너