HI,
I have a problem with custom made shader that worked perfectly in Unity4 but give strange result in Unity5.
Shader will normally produce an Xray kind of effect with circular shape when used with texture on a plane.
In Unity5 i will instead get elliptical shape, with dimensions being connected to the plane scale.
I havent wrote this shader myself, and my shader writting knowledge is basic, but i managed to find possible problem in unity documentation:
Non-uniform mesh scale has to be taken into account in shaders
In Unity 5.0, non-uniform meshes are not “prescaled” on the CPU anymore. This means that normal & tangent vectors can be non-normalized in the vertex shader. If you’re doing manual lighting calculations there, you’d have to normalize them. If you’re using Unity’s surface shaders, then all necessary code will be generated for you.
Im assuming that since my planes are scaled trough transform component, that this is making problems in Unity, heres the code to help with possible solution.
Shader "Custom/VisionShader" {
Properties {
_MainTex ("Texture", 2D) = "white" {}
_EffectX("X Coord", float) = 0.5
_EffectY("Y Coord", float) = 0.5
_EffectRadius("Effect Radius", float) = 4.0
_ScaleX("X Scale", float) = 1.0
_ScaleY("Y Scale", float) = 1.0
}
SubShader {
Tags { "Queue" = "Transparent" }
Pass {
ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha
Cull Off
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
uniform sampler2D _MainTex;
float _EffectX, _EffectY, dist, _EffectRadius, _ScaleX, _ScaleY;
float4 designColor;
struct vertexInput {
float4 vertex : POSITION;
float4 texcoord : TEXCOORD1;
};
struct vertexOutput {
float4 pos : SV_POSITION;
float4 posInObjectCoords : TEXCOORD0;
float4 tex : TEXCOORD1;
};
vertexOutput vert(vertexInput input)
{
vertexOutput output;
output.pos = mul(UNITY_MATRIX_MVP, input.vertex);
output.posInObjectCoords = input.vertex;
output.tex = input.texcoord;
return output;
}
float4 frag(vertexOutput input) : COLOR
{
designColor = tex2D(_MainTex, float2(input.tex));
dist = (input.posInObjectCoords.z - _EffectY) * (input.posInObjectCoords.z - _EffectY) + (input.posInObjectCoords.x - _EffectX)*(input.posInObjectCoords.x - _EffectX) ;
if (dist < _EffectRadius && _EffectRadius > 0)
{
if (designColor.a > (dist/_EffectRadius))
{
designColor = float4(designColor.r, designColor.g , designColor.b, ((dist*dist/_EffectRadius)/_EffectRadius));
}
}
return designColor;
}
ENDCG
}
}
Fallback "Diffuse"
}
I looked for solution on internet and found that similar problem was solved using suggestion above to normalize vectors, changing
output.pos = mul(UNITY_MATRIX_MVP, input.vertex);
to
output.pos = normalize(mul(UNITY_MATRIX_MVP, input.vertex));
but this didnt worked for me.
I have also put variables _ScaleX and _ScaleY that will hold scale of the transform component so that they can be used inside of shader.

