You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
3.4 KiB

Shader "Hidden/ScreenHomography"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
ZTest Always Cull Off ZWrite Off
Fog { Mode off }
CGINCLUDE
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"
float4x4 matrixHomo;
float4 textureSize;
sampler2D _MainTex;
float3 blend_color;
float amount;
float Triangular(float f)
{
f = f / 2.0;
if (f < 0.0)
{
return (f + 1.0);
}
else
{
return (1.0 - f);
}
return 0.0;
}
float4 getColor(sampler2D textureSampler, float2 TexCoord)
{
float4 homo = mul(matrixHomo, float4(TexCoord, 0.0f, 1.0f));
float2 tex_coord = homo.xy / homo.w;
if (tex_coord.x < 0.0f || tex_coord.x > 1.0f || tex_coord.y < 0.0f || tex_coord.y > 1.0f)
return float4(0, 0, 0, 1);
else
return tex2D(textureSampler, tex_coord);
}
float4 BiCubic(sampler2D textureSampler, float2 TexCoord)
{
float fWidth = textureSize.x;
float fHeight = textureSize.y;
float texelSizeX = 1.0 / fWidth; //size of one texel
float texelSizeY = 1.0 / fHeight; //size of one texel
float4 nSum = float4(0.0, 0.0, 0.0, 0.0);
float4 nDenom = float4(0.0, 0.0, 0.0, 0.0);
float a = frac(TexCoord.x * fWidth); // get the decimal part
float b = frac(TexCoord.y * fHeight); // get the decimal part
for (int m = -1; m <= 2; m++)
{
for (int n = -1; n <= 2; n++)
{
float4 vecData = getColor(textureSampler,
TexCoord + float2(texelSizeX * float(m),
texelSizeY * float(n)));
float f = Triangular(float(m) - a);
float4 vecCooef1 = float4(f, f, f, f);
float f1 = Triangular(-(float(n) - b));
float4 vecCoeef2 = float4(f1, f1, f1, f1);
nSum = nSum + (vecData * vecCoeef2 * vecCooef1);
nDenom = nDenom + ((vecCoeef2 * vecCooef1));
}
}
return nSum / nDenom;
}
float4 BiLinear(sampler2D textureSampler, float2 TexCoord)
{
float fWidth = textureSize.x;
float fHeight = textureSize.y;
float texelSizeX = 1.0 / fWidth; //size of one texel
float texelSizeY = 1.0 / fHeight; //size of one texel
float4 p0q0 = getColor(textureSampler, TexCoord);
float4 p1q0 = getColor(textureSampler, TexCoord + float2(texelSizeX, 0));
float4 p0q1 = getColor(textureSampler, TexCoord + float2(0, texelSizeY));
float4 p1q1 = getColor(textureSampler, TexCoord + float2(texelSizeX, texelSizeY));
float a = frac(TexCoord.x * fWidth); // Get Interpolation factor for X direction.
// Fraction near to valid data.
float4 pInterp_q0 = lerp(p0q0, p1q0, a); // Interpolates top row in X direction.
float4 pInterp_q1 = lerp(p0q1, p1q1, a); // Interpolates bottom row in X direction.
float b = frac(TexCoord.y * fHeight);// Get Interpolation factor for Y direction.
return lerp(pInterp_q0, pInterp_q1, b); // Interpolate in Y direction.
}
float4 frag(v2f_img i) : SV_Target
{
float4 scene_color = getColor(_MainTex, i.uv);
//scene_color = BiLinear(_MainTex, i.uv);
//scene_color = BiCubic(_MainTex, i.uv);
float3 col = scene_color * (1-amount) + blend_color * amount;
return float4(col, 1.0);
}
ENDCG
// (0) VRRenderCloud
Pass
{
CGPROGRAM
#pragma vertex vert_img
#pragma fragment frag
ENDCG
}
}
FallBack off
}