Cg Programming/Unity/Curved Glass
This tutorial covers refraction mapping and its implementation with cube maps.
It is a variation of Section “Reflecting Surfaces”, which should be read first.
Refraction Mapping
[edit | edit source]In Section “Reflecting Surfaces”, we reflected view rays and then performed texture lookups in a cube map in the reflected direction. Here, we refract view rays at a curved, transparent surface and then perform the lookups with the refracted direction. The effect will ignore the second refraction when the ray leaves the transparent object again; however, many people hardly notice the differences since such refractions are usually not part of our daily life.
Instead of the reflect
function, we are using the refract
function; thus, the fragment shader could be:
float4 frag(vertexOutput input) : COLOR
{
float refractiveIndex = 1.5;
float3 refractedDir = refract(normalize(input.viewDir),
normalize(input.normalDir), 1.0 / refractiveIndex);
return texCUBE(_Cube, refractedDir);
}
Note that refract
takes a third argument, which is the refractive index of the outside medium (e.g. 1.0 for air) divided by the refractive index of the object (e.g. 1.5 for some kinds of glass). Also note that the first argument has to be normalized, which isn't necessary for reflect
.
Complete Shader Code
[edit | edit source]With the adapted fragment shader, the complete shader code becomes:
Shader "Cg shader with refraction mapping" {
Properties {
_Cube("Reflection Map", Cube) = "" {}
}
SubShader {
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
// User-specified uniforms
uniform samplerCUBE _Cube;
struct vertexInput {
float4 vertex : POSITION;
float3 normal : NORMAL;
};
struct vertexOutput {
float4 pos : SV_POSITION;
float3 normalDir : TEXCOORD0;
float3 viewDir : TEXCOORD1;
};
vertexOutput vert(vertexInput input)
{
vertexOutput output;
float4x4 modelMatrix = unity_ObjectToWorld;
float4x4 modelMatrixInverse = unity_WorldToObject;
output.viewDir = mul(modelMatrix, input.vertex).xyz
- _WorldSpaceCameraPos;
output.normalDir = normalize(
mul(float4(input.normal, 0.0), modelMatrixInverse).xyz);
output.pos = UnityObjectToClipPos(input.vertex);
return output;
}
float4 frag(vertexOutput input) : COLOR
{
float refractiveIndex = 1.5;
float3 refractedDir = refract(normalize(input.viewDir),
normalize(input.normalDir), 1.0 / refractiveIndex);
return texCUBE(_Cube, refractedDir);
}
ENDCG
}
}
}
Summary
[edit | edit source]Congratulations. This is the end of another tutorial. We have seen:
- How to adapt reflection mapping to refraction mapping using the
refract
instruction.
Further reading
[edit | edit source]If you still want to know more
- about reflection mapping and cube maps, you should read Section “Reflecting Surfaces”.
- about the
refract
instruction, you could look it up in appendix E of Nivida's Cg Tutorial.