- Home /
2D and distortion shader
So im trying to make some sort of distortion. Everything looks fine with perspective camera,but doesnt work with 2D/ortographic camera.
Perspective camera
2D/Ortographic camera
Here`s my shader.
Shader "Effects/Distortion/CullBack+1" {
Properties {
_Color ("Main Color", Color) = (1,1,1,1)
_MainTex ("Base (RGB) Gloss (A)", 2D) = "black" {}
_BumpMap ("Normalmap", 2D) = "bump" {}
_ColorStrength ("Color Strength", Float) = 1
_BumpAmt ("Distortion [0-100]", range (0, 100)) = 10
}
SubShader {
Tags { "Queue"="Transparent+1" "RenderType"="Transperent" }
GrabPass {}
LOD 200
ZWrite On
Cull Back
CGPROGRAM
#pragma surface surf Lambert alpha vertex:vert
sampler2D _MainTex;
sampler2D _BumpMap;
float _BumpAmt;
float _ColorStrength;
sampler2D _GrabTexture;
float4 _GrabTexture_TexelSize;
float4 _Color;
struct Input {
float2 uv_MainTex;
float2 uv_BumpMap;
float4 proj : TEXCOORD0;
};
void vert (inout appdata_full v, out Input o) {
UNITY_INITIALIZE_OUTPUT(Input,o);
float4 oPos = mul(UNITY_MATRIX_MVP, v.vertex);
#if UNITY_UV_STARTS_AT_TOP
float scale = -1.0;
#else
float scale = 1.0;
#endif
o.proj.xy = (float2(oPos.x, oPos.y*scale) + oPos.w) * 0.5;
o.proj.zw = oPos.zw;
}
void surf (Input IN, inout SurfaceOutput o) {
o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
half2 offset = o.Normal.rg * _BumpAmt * _GrabTexture_TexelSize.xy;
IN.proj.xy = offset * IN.proj.z + IN.proj.xy;
half4 col = tex2Dproj(_GrabTexture, UNITY_PROJ_COORD(IN.proj));
fixed4 tex = tex2D(_MainTex, IN.uv_MainTex) * _Color;
o.Emission = col.xyz + tex*_ColorStrength;
o.Alpha = _Color.a;
}
ENDCG
}
FallBack "Effects/Distortion/Free/CullBack"
}
Answer by daniel-eherbert · Aug 18, 2015 at 06:37 AM
I haven't tried your shader, but perhaps IN.proj.z
(from mul(UNITY_MATRIX_MVP, v.vertex).z
) is different/zero with an orthographic setup. I imagine the concept of "depth" is different to an orthographic camera...
Maybe try some changes like this to help you find out what's going on:
Shader "Effects/Distortion/CullBack+1" {
Properties {
_Color ("Main Color", Color) = (1,1,1,1)
_MainTex ("Base (RGB) Gloss (A)", 2D) = "black" {}
_BumpMap ("Normalmap", 2D) = "bump" {}
_ColorStrength ("Color Strength", Float) = 1
_BumpAmt ("Distortion [0-100]", range (0, 100)) = 10
_Distance ("Distance", Float) = 1
}
[... snip ...]
float _BumpAmt;
float _ColorStrength;
sampler2D _GrabTexture;
float4 _GrabTexture_TexelSize;
float _Distance;
[... snip ...]
//IN.proj.xy = offset * IN.proj.z + IN.proj.xy;
IN.proj.xy = offset * _Distance + IN.proj.xy;
[... snip ...]
FallBack "Effects/Distortion/Free/CullBack"
}
(Note addition of _Distance
)
Your answer
Follow this Question
Related Questions
Weird sprite distortion during slow movement 0 Answers
How to simulate 2D layers in Unity 2 Answers
Camera Distortion effect needed improve (SOLVED) 2 Answers
2d Shader: How to show camera view on a sprite? 1 Answer
one shader for multiple sprites 0 Answers