- Home /
Magnification shader creating very pixelated image
So I am trying to make a scope for my vr shooter. I have a setup where there is a render texture and a crosshair on the inside of the scope with a plane with a magnification shader on the eye side lense. This works great until I up the magnification on the scope to more than 2 times, then it becomes a pixelated mess. Basically, is there a way to remake this shader so that it doesn't end up like this?
Shader "Custom/Magnification"
{
Properties
{
_Magnification("Magnification", Float) = 1
}
SubShader
{
Tags{ "Queue" = "Transparent" "PreviewType" = "Plane" }
LOD 100
GrabPass{ "_GrabTexture" }
Pass
{
ZTest On
ZWrite Off
Blend One Zero
Lighting Off
Fog{ Mode Off }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
};
struct v2f
{
//our UV coordinate on the GrabTexture
float4 uv : TEXCOORD0;
//our vertex position after projection
float4 vertex : SV_POSITION;
};
sampler2D _GrabTexture;
half _Magnification;
v2f vert(appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
//the UV coordinate of our object's center on the GrabTexture
float4 uv_center = ComputeGrabScreenPos(UnityObjectToClipPos(float4(0, 0, 0, 1)));
//the vector from uv_center to our UV coordinate on the GrabTexture
float4 uv_diff = ComputeGrabScreenPos(o.vertex) - uv_center;
//apply magnification
uv_diff /= _Magnification;
//save result
o.uv = uv_center + uv_diff;
return o;
}
fixed4 frag(v2f i) : COLOR
{
return tex2Dproj(_GrabTexture, UNITY_PROJ_COORD(i.uv));
}
ENDCG
}
}
}
Thanks for the update and quick reply. I'll be sure to keep an eye on this thread. Looking for the same issue. Bumped into your thread. Thanks for creating it. Looking forward for solution. AESsuccess
$$anonymous$$e too, its such a good shader for lense effects, I really hope somebody can recreate it with a different method,
Answer by Bunny83 · Nov 20, 2020 at 04:47 AM
That's because all you do is scaling up the rendertexture that still contains the same content. So of coruse zooming in on a finite pixel image won't be great for the quality of the image. You should use a seperate camera for your rendertexture and zoom in by reducing the fov of that seperate camera. There's not really a need for a magnification shader. You just want to make sure you cut out / mask your rendertexture to your scope shape properly.
ps: currently you don't use an actual rendertexture since you just use a grabpass. A grabpass has a similar overhead
That's because all you do is scaling up the rendertexture that still contains the same content. So of coruse zoo$$anonymous$$g in on a finite pixel image won't be great for the quality of the image.
Well yes, I am aware of this. This is why the question, in the tile, asks if there is a way to make the shader without this issue.
You should use a seperate camera for your rendertexture and zoom in by reducing the fov of that seperate camera. There's not really a need for a magnification shader.
This is unfortunately not true, I have a render texture with a small fov camera, as I said in the question. The magnification shader is to create a lense effect. So, yes there is a need for the shader, and that is why the question was asking is there a way to recreate the effect this shader has using a different method.
I do appreciate your time though, unfortunately im looking for a different solution.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Set list in shader (uniform) 1 Answer