Featured image of post 쉐이더 프로그래밍 기본공부 17 - 반사쉐이더

쉐이더 프로그래밍 기본공부 17 - 반사쉐이더


드디어 리얼 반사 쉐이더 완성. 얏호.
Cube Map을 이용한 반사 쉐이더이다. 배경 이미지는 모양으로 가져다 놓은 것. 없어도 반사된다. 레이트레이싱도 아니고 뭐.

아래는 Lighting 영향이랑 Specular Map까지 계산해서 완성한 버전. Texture와의 MixRate 까지 만들어져 있다.
동영상으로 보면 다음과 같다.

// 반사 쉐이더

float4x4 WorldViewProj : WORLDVIEWPROJ ;
float4x4 World   : WORLD ;
float4x4 ViewI   : VIEWI ;

float3 Lightdirection : DIRECTION
<
 string UIName = “DiffuseLight”;
 string Object = “Directionallight”;
 
 >;

float3 AmbientColor : AMBIENT
<
 string UIName = “AmbientColor”;

 > ;

float3 SpecularColor : SPECULAR
<
 string UIName = “SpecularColor”;

 > ; 
 
float SpecularPower : POWER
<
 string UIName = “SpecularPower”;
 //string UIWidget = “Numeric”;
   float UIMin = 5.00;
   float UIMax = 100.00;
   float UIStep = 1.0;

;

texture2D basetexture : DIFFUSE
<
 string UIName = “Basetexture”;
 int Texcoord = 0;
 int MapChannel = 1;

;

sampler2D BaseSampler = sampler_state
{
 texture = ;
 MinFilter = Linear;
 MagFilter = Linear;
 MipFilter = Linear;

};

texture2D ReflectTexture : REFLECT
<
 string UIName = “ReflectTexture”;
 int Texcoord = 0;
 int MapChannel = 1;

;

samplerCUBE ReflectSampler = sampler_state
{
 //AddressU = Clamp;
 //AddressV = Clamp;
 texture = ;
 MinFilter = Linear;
 MagFilter = Linear;
 MipFilter = Linear;

};

float Mixrate : MIX
<
 string UIName = “Mixrate”;
 //string UIWidget = “Numeric”;
   float UIMin = 0.00;
   float UIMax = 1.00;
   float UIStep = 0.01;

;

void VS
(
 in float4 iPos : POSITION,
 in float4 iNormal : NORMAL,
 in float2 itex : TEXCOORD0,
  
 out float4 oPos : POSITION,
 out float2 otex : TEXCOORD0,
 out float3 Diffuselight : TEXCOORD1,
 out float3 ReflectVector : TEXCOORD2,
 out float3 Specularlight : TEXCOORD3

)
{
 float4 WorldPos = mul(iPos , World) ;
 float3 oNormal = normalize(mul(iNormal,(float3x3)World));
 float3 ViewDirection = normalize ( ViewI[3].xyz - WorldPos.xyz );
 float3 HalfVecter = normalize ( Lightdirection + ViewDirection );
 
 
 
 Specularlight = pow(max(0,dot(HalfVecter, oNormal)),SpecularPower);
 ReflectVector = normalize (2 * dot(ViewDirection,oNormal) * ViewDirection - oNormal);
 Diffuselight = max(0, dot(Lightdirection, oNormal));
 oPos = mul(iPos, WorldViewProj);
 otex = itex;
}

void PS
(
 in float3 itex : TEXCOORD0,
 in float3 Diffuselight : TEXCOORD1,
 in float3 ReflectVector : TEXCOORD2,
 in float3 Specularlight : TEXCOORD3,

 out float4 oColor : COLOR
)
{

 float3 otex = tex2D(BaseSampler, itex);
 float3 Reflecttex = texCUBE(ReflectSampler,ReflectVector);

 oColor = float4 ( lerp(otex,Reflecttex,Mixrate)* (Diffuselight + (Specularlight*SpecularColor) + AmbientColor),1);
 
}

technique jp13
{
 pass p0
 {
  AlphaBlendEnable = true;
  //SrcBlend = SRCALPHA;
  //DestBlend = ONE;
  
  Vertexshader = compile vs_2_0 VS();
  Pixelshader = compile ps_2_0 PS();
 }
 
/*
 pass p1
 {
  AlphaBlendEnable = true;
  Vertexshader = compile vs_2_0 VS1();
  Pixelshader = compile ps_2_0 PS1();
 }*/
}

Hugo로 만듦
JimmyStack 테마 사용 중