Jump to content
  • Advertisement
Sign in to follow this  
Scouting Ninja

Any Unity screen effects really slow on mobile.

Recommended Posts

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?

Edited by Scouting Ninja

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!