Featured image of post 쉐이더 프로그래밍 기본공부 18 - 굴절쉐이더

쉐이더 프로그래밍 기본공부 18 - 굴절쉐이더

굴절쉐이더는… 반사쉐이더에서 숫자만 바꿔주면 되는 대단히 간단한 쉐이더 방식이다.


핵심은 이게 전부.
볼록렌즈나 오목렌즈 같은 효과를 만들어 준다.
굴절율은 NASA에서 발표한 매질의 굴절율에 따르면 될 듯.
어쨌건 재질 자체를 무시하고 완전히 굴절만 만들면,
공각기동대의 광학미체 같은 효과를 낼 수 있을 듯.
여기다가 외각면 인식한 텍스쳐를 추가하면 외각부분의 투명도 제어 느낌도 가능할듯 하다.
각종 응용이 생각나는구나 에헤야디야.
일단 굴절율 자체는 0.04 단계쯤 해줘야 알아보기 쉽다. 그 이상 크면 반사와 구별하기 매우 어렵다.


동영상은 이렇게.
배경과의 싱크는 사실 맞지 않다. 배경은 그냥 이해를 쉽게 할 수 있도록 넣은것일 뿐.

// 반사 쉐이더/굴절 쉐이더

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 (0.03 * dot(oNormal, ViewDirection) * oNormal - ViewDirection );
 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 테마 사용 중