- Home /
Post Processing Shader does not work on android device
I have a post-processing shader that works perfectly fine in the editor but when I build to android and try running it on my android phone, the shader doesn't work. Why is this? I am new to writing shaders but it's a really simple shader that doesn't utilize any advanced features (to my knowledge) so why can't it be run on my android device?
Shader:
Shader "Post-Processing/BarrelDistortionAndBlurredEdges"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_Distortion("Distortion", float) = 0.0
_Inverse("Inverse", float) = 1.0
_ScreenWidth("ScreenWidth", float) = 0.0
_ScreenHeight("ScreenHeihgt", float) = 0.0
_BlurStart("BlurStart", float) = 0.5
_BlurEnd("BlurEnd", float) = 0.9
_BlurColor("BlurColor", Color) = (1, 1, 1, 1)
}
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 = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv = v.uv;
return o;
}
sampler2D _MainTex;
float _Distortion;
float _Inverse;
float _BlurStart;
float _BlurEnd;
fixed4 _BlurColor;
float _ScreenWidth;
float _ScreenHeight;
float2 cartesianToPolar(float2 cartesian)
{
return float2(sqrt(pow(cartesian.y, 2.0f) + pow(cartesian.x, 2.0f)), atan2(cartesian.y, cartesian.x));
}
float2 polarToCartesian(float2 polar)
{
return float2(polar.x * cos(polar.y), polar.x * sin(polar.y));
}
float difference(float r)
{
return float((-1.0f + sqrt(-4.0f * (pow(r, 2.0f) - r) + 1.0f)) / 2.0f);
}
float4 frag (v2f i) : SV_Target
{
float2 src = i.uv;
if(_Distortion > 0.0f) {
float2 res = i.uv;
float2 resNormalized = float2(res.x * 2.0f - 1.0f, res.y * 2.0f - 1.0);
float2 resPolar = cartesianToPolar(resNormalized);
float2 srcPolar = float2(resPolar.x - _Inverse * cos(radians(45.0f)) * difference(resPolar.x) * _Distortion, resPolar.y);
float2 srcNormalized = polarToCartesian(srcPolar);
src = (srcNormalized) / 2.0f + 0.5f;
}
float blur = 0.0f;
if(_BlurEnd > _BlurStart) {
float d = length(i.uv * 2.0f - 1.0f) - _BlurStart;
float blurZone = _BlurEnd - _BlurStart;
blur = d / blurZone;
}
if(blur < 0.0f) {
blur = 0.0f;
} else if(blur > 1.0f) {
blur = 1.0f;
}
return lerp(tex2D(_MainTex, src), _BlurColor, blur);
}
ENDCG
}
}
}
Script:
using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
public class BarrelDistortionAndBlurredEdges : MonoBehaviour {
public float distortion = 0.0f;
public bool inverse = false;
public float blurStart = 0.5f;
public float blurEnd = 0.9f;
public Color blurColor = Color.white;
private Material material;
void Awake() {
material = new Material (Shader.Find("Post-Processing/BarrelDistortionAndBlurredEdges"));
}
void OnRenderImage(RenderTexture src, RenderTexture dest) {
if (material != null) {
material.SetFloat ("_Distortion", distortion);
if(inverse) {
material.SetFloat ("_Inverse", -1.0f);
} else {
material.SetFloat ("_Inverse", 1.0f);
}
material.SetFloat ("_BlurStart", blurStart);
material.SetFloat ("_BlurEnd", blurEnd);
material.SetColor ("_BlurColor", blurColor);
material.SetFloat ("_ScreenWidth", Screen.width);
material.SetFloat ("_ScreenHeight", Screen.height);
Graphics.Blit (src, dest, material);
} else {
Graphics.Blit (src, dest);
}
}
}
Answer by 01000101 · Sep 21, 2016 at 01:35 PM
So, apparently this wasn't an error with android. When I tried to export the same shader to a standalone PC build the shader wouldn't work either. The problem was that the shader couldn't be found using the Shader.Find function because the shader wasn't in the Assets/Resources folder.
If anyone else has this problem: Try moving your shaders into the Assets/Resources folder or referencing your shader using another method than: Shader.Find()
Thanks for co$$anonymous$$g back and documenting your findings :) This helps others like me who ran into the same issue.
I found out that you can also include your shader in the "Always Included Shaders" list under the "Edit > Project Settings > Graphics" menu. This will force your shaders to be included in the build process, and this way you don't have to put them under the Resources folder.