So I am trying to learn how to do screen effects in Unity. The problem is that ANY effect is slow on mobile. At first I thought it was the complexity of my shader, so I tried it with a much simpler shader:
Shader "Hidden/NewImageEffectShader"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_Amount ("Effect Amount", Range (0, 1)) = 0
}
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 = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
sampler2D _MainTex;
uniform float _Amount;
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv);
//Make it white
col = col + _Amount;
return col;
}
ENDCG
}
}
}
It's a very basic adaption of the shader Unity provides as default. All it does is add 1 to the color
The script to run the shader looks like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TurnThingsWhite : MonoBehaviour {
public float _Amount;
public Material material;
float Timer = 2f;
void Update(){
if (Timer > 0.1f) {
Timer -= 1f * Time.deltaTime;
} else {
Timer = 2f;
_Amount = ((_Amount * -1f) + 1f);//toggle binary
}
}
// Postprocess the image
void OnRenderImage (RenderTexture source, RenderTexture destination){
material.SetFloat("_Amount", _Amount);
Graphics.Blit (source, destination, material);
}
}
On Desktop this has almost no impact on performance, on mobile this is dropping even new mobiles down to 30fps-40fps. With nothing else in the scene except a 3D text object to see the fps.
Is this a problem with my script? Is there some other way mobile screen shaders should be made?