Hello, I’ve got this up and running but I think it could still do with some optimization.
Any takers (testers)?
Hayd
//@author: vux
//@help: template for texture fx
//@tags: texture
//@credits:
Texture2D texture2d : PREVIOUS;
float2 R:TARGETSIZE;
SamplerState linearSampler : IMMUTABLE
{
Filter = MIN_MAG_MIP_LINEAR;
AddressU = Wrap;
AddressV = Wrap;
};
cbuffer cbPerObj : register( b1 )
{
float2 _Center = float2(0,0);
float2 _ScaleIn = float2(0,0);
float2 _Scale = float2(0,0);
float4 _HmdWarpParam = float4(0,0,0,0);
float4 _ChromaticAberration = float4(0,0,0,0);
};
float4 PDistort(float4 PosWVP:SV_POSITION,float2 uv:TEXCOORD0) : SV_Target
{
/*float4 c = texture2d.Sample(linearSampler,input.uv);
return c;
*/
// calculate normal distortion values
float4 i = texture2d.Sample(linearSampler,uv);
float2 theta = (uv - _Center) * _ScaleIn;
float rSq = theta.x * theta.x + theta.y * theta.y;
float2 theta1 = theta * (_HmdWarpParam.x +
_HmdWarpParam.y * rSq +
_HmdWarpParam.z * rSq * rSq +
_HmdWarpParam.w * rSq * rSq * rSq);
// calculate the texture co-ordinates for each color channel
float2 thetaRed = (theta1 * _ChromaticAberration.x) +
(theta1 * rSq * _ChromaticAberration.y);
float2 tcRed = _Center + _Scale * thetaRed;
float2 tcGreen = _Center + _Scale * theta1;
float2 thetaBlue = (theta1 * _ChromaticAberration.z) +
(theta1 * rSq * _ChromaticAberration.w);
float2 tcBlue = _Center + _Scale * thetaBlue;
// Check to see if we are out of range on the texture co-ordinates
// (green channel is the same as normal distortion)
// Using 3 different texture co-ordinates, sample each channel
// to correct for color aberration
float red = texture2d.Sample(linearSampler, tcRed).x;
float green = texture2d.Sample(linearSampler, tcGreen).y;
float blue = texture2d.Sample(linearSampler, tcBlue).z;
float alpha = 1;
/*float red = tex2D (_MainTex, i.uv + float2(0.1, 0.0)).x;;
float green = tex2D (_MainTex, i.uv).y;
float blue = tex2D (_MainTex, i.uv + float2(0.0, 0.1)).z;;
float alpha = 1;*/
if (any(clamp(tcGreen, float2(0, 0), float2(1, 1)) - tcGreen))
{
red = 0;
green = 0;
blue = 0;
alpha = 0;
}
return float4(red, green, blue, alpha);
}
technique10 Distort
{
pass P0
{
SetPixelShader(CompileShader(ps_4_0,PDistort()));
}
}