Šviesos laužimas ir atspindėjimas per pixelį

Vertexų shaderis yra toks:
float4x4 view_proj_matrix;
float4 view_position;
struct VS_OUTPUT
{
float4 Pos: POSITION;
float2 TexCoord: TEXCOORD0;
float3 viewVec: TEXCOORD1;
float3 Normal: TEXCOORD2;
};
VS_OUTPUT vs_main(float4 inPos: POSITION, float3 inNormal: NORMAL,
float2 inTxr: TEXCOORD0)
{
VS_OUTPUT Out;
// Compute the projected position and send out the texture coordinates
Out.Pos = mul(view_proj_matrix, inPos);
Out.TexCoord = inTxr;
Out.viewVec = view_position - inPos;
Out.Normal = inNormal;
return Out;
}
Pixelių shaderis yra toks:
sampler Wood;
sampler EnvMap;
float4 ps_main(float2 inTxr: TEXCOORD0,float3 viewVec: TEXCOORD1,
float3 inNormal: TEXCOORD2) : COLOR
{
viewVec = normalize(viewVec);
// Compute reflection
float3 inReflect = reflect(-viewVec,inNormal);
// Compute the reflection vector using Snell’s law
// the refract HLSL function does not always work properly
// n_i * sin(theta_i) = n_r * sin(theta_r)
// sin(theta_i)
float cosine = dot(viewVec, inNormal);
float sine = sqrt(1 - cosine * cosine);
// sin(theta_r)
float sine2 = saturate(1.14 * sine);
float cosine2 = sqrt(1 - sine2 * sine2);
// Determine the refraction vector be using the normal and tangent
// vectors as basis to determine the refraction direction
float3 x = -inNormal;
float3 y = normalize(cross(cross(viewVec, inNormal), inNormal));
float3 inRefract = x * cosine2 + y * sine2;
// Output texture color with reflection map
return sine * texCUBE(EnvMap,inReflect) +
((1 - sine2) * texCUBE(EnvMap,inRefract) + 0.4)
* tex2D(Wood,inTxr);
}


High Level Shading Language