- Home /
How to write shaders for mobile?
I have a very basic monochroming shader as follows...
Shader "Custom/Monochrome"
{
Properties
{
_MainTex ("Base (RGB)", 2D) = "white" {}
_DimIntensity ("Monochrome Value", range(0.5, 5.0)) = 1.0
}
SubShader
{
Tags { "Queue" = "Transparent" }
Pass
{
LOD 100
Cull Back // now render the front faces
ZWrite Off // don't write to depth buffer
// in order not to occlude other objects
Blend SrcAlpha OneMinusSrcAlpha
// blend based on the fragment's alpha value
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
uniform sampler2D _MainTex;
uniform half _DimIntensity;
uniform float _StartTime = 0.0;
uniform float _isPlaying;
struct vertexInput
{
float4 vertex : POSITION;
float4 texcoord : TEXCOORD0;
};
struct vertexOutput
{
float4 pos : SV_POSITION;
float4 tex : TEXCOORD0;
};
vertexOutput vert(vertexInput input)
{
vertexOutput output;
output.tex = input.texcoord;
output.pos = mul(UNITY_MATRIX_MVP, input.vertex);
return output;
}
fixed4 frag(v2f_img input) : COLOR
{
fixed4 rgba = tex2D(_MainTex, input.uv.xy);
return fixed4(rgba.rgb/_DimIntensity, rgba.a);
}
ENDCG
}
}
//FallBack "Transparent/Cutout/VertixLit"
}
My guess why this isn't working is because I need to then use methods that are available on the GPU of the mobile device. I haven't seen anything clear from some google searches.
Anyone have any references?
What's the indication that it doesn't? How does it look? Do you get any warnings when compiling for platform or running? If you select the shader does it show any warnings in the inspector?
Generally there are no differences between writing shaders for Editor or $$anonymous$$obile - Unity takes care of cross compiling for you. Although there are situation where there are hardware limitations (which is not your case) or shaders work differently (like something doesn't get initialised or something like that)
The shader involved used on a mobile device comes out as pink. So, that means that the sub shader apparently didn't work correctly. I probably should use Unity Remote to see what errors pop up.
What "mobile device" in particular doesn't work? Android? Windows Phone? I don't see anything that wouldn't be supported (you've got some unused _StartTime and _isPlaying variables, but that shouldn't be it)
Android device. So far S5. I have another device which is not working at all, but I think it is a different issue.
Answer by tanoshimi · Dec 29, 2014 at 10:02 PM
Oh, hang on - I've just seen your error...
Your vertex shader is outputting a custom vertexOutput structure. (line43) However, your fragment shader is expecting to receive a v2f_img input structure. (line52)
You need to change your fragment shader to:
fixed4 frag(vertexOutput input) : COLOR
{
fixed4 rgba = tex2D(_MainTex, input.tex.xy);
return fixed4(rgba.rgb/_DimIntensity, rgba.a);
}
I don't see how this is related to Android, however - it should never have even worked in the editor!
Awesome thank you so much! That fixed the issue. Weird how it still worked in the editor.
Your answer
