Hi, I'm new to shaders and i have some problems with appling shader i found to a camera. I wrote a script that creates a material that uses my shader and through Graphics.Blit it should supposedly work, but i doesn't
Here is shader itself:
Shader "Custom/FishEyeFilter"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_BarrelPower ("BarrelPower", float) = 1.5
}
SubShader
{
// No culling or depth
Cull Off ZWrite Off ZTest Always
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
sampler2D _MainTex;
float _BarrelPower;
float2 Distort(float2 uv, float radius)
{
float theta = atan2(uv.y, uv.x);
radius = pow(radius, _BarrelPower);
uv.x = radius * cos(theta);
uv.y = radius * sin(theta);
return (0.5 * uv + 1);
}
fixed4 frag (v2f i) : SV_Target
{
float2 uv = (i.uv * 2.0) - 1.0;
float radius = length(uv);
fixed4 col = tex2D(_MainTex, i.uv);
if(radius >= 1)
return col;
uv = Distort(uv, radius);
// just invert the colors
col = tex2D(_MainTex, uv);
return col;
}
ENDCG
}
}
}
Here is script that creates material:
public class SimpleFilter : MonoBehaviour
{
[SerializeField] private Shader shader;
[SerializeField] private float _pow;
protected Material _mat;
private void Awake()
{
_mat = new Material(shader);
}
void Update()
{
OnUpdate();
}
protected void OnUpdate()
{
_mat.SetFloat(name: "_BarrelPower", _pow);
}
private void OnRenderImage(RenderTexture src, RenderTexture dst)
{
Graphics.Blit(src, dst, _mat);
}
}
↧