Any Unity screen effects really slow on mobile.

Started by
3 comments, last by Seabolt 6 years, 3 months ago

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:

Spoiler

 



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?

Advertisement

What mobile device are you testing on? I've made live games on mobile with post processing just fine. I didn't use OnRenderImage though, I manually did Graphics.SetRenderTarget, but you should be fine if targeting anything somewhat recent, (I was doing an approximation of shadow mapping on Samsung Galaxy S4). Maybe it's something to do with OnRenderImage?

Perception is when one imagination clashes with another
5 minutes ago, Seabolt said:

OnRenderImage though, I manually did Graphics.SetRenderTarget,

This looks like my problem. It's running above 60fps now. Strange that this one thing had such a huge difference between desktop and mobile. Even works with my shader on old mobiles.

Maybe it was allocating a render texture for you under the hood each frame or something silly like that. I'm glad it helped!

Perception is when one imagination clashes with another

This topic is closed to new replies.

Advertisement