- Home /
My shader works on desktop, but black screen on Android.
You can find the shader code below. I did this as an "image effect shader" in Unity. Then I apply it to my camera along with the C# script (below the shader). Looks great on desktop but on Android it just makes the screen black. I'm probably using something that doesn't work on Android, maybe someone could point me in the right direction for fixing it?
//#<!--
//# CRT-simple shader
//#
//# Copyright (C) 2011 DOLLS. Based on cgwg's CRT shader.
//#
//# Modified by fontmas: 2015-03-06
//#
//# This program is free software; you can redistribute it and/or modify it
//# under the terms of the GNU General Public License as published by the Free
//# Software Foundation; either version 2 of the License, or (at your option)
//# any later version.
//# -->
Shader "Custom/CRT"
{
Properties
{
_MainTex ("Base (RGB)", 2D) = "white" {}
}
SubShader
{
Pass
{
CGPROGRAM
#pragma vertex vert_img
#pragma fragment frag
#include "UnityCG.cginc"
#define CURVATURE
#pragma target 3.0
#define PI 3.141592653589
uniform sampler2D _MainTex;
uniform float2 _InputSize;
uniform float2 _OutputSize;
uniform float2 _TextureSize;
uniform float2 _One;
uniform float2 _Texcoord;
uniform float _Factor;
uniform float _Distortion = 0.1f; // 0.1f
uniform float _Gamma = 1.0f; // 1.0f
uniform float _curvatureSet1 = 0.5f; // 0.5f
uniform float _curvatureSet2 = 0.5f; // 0.5f
uniform float _YExtra = 0.5f; // 0.5f;
uniform float _rgb1R = 1.0f; // 1.0f
uniform float _rgb1G = 1.0f; // 1.0f
uniform float _rgb1B = 1.0f; // 1.0f
uniform float _rgb2R = 1.0f; // 1.0f
uniform float _rgb2G = 1.0f; // 1.0f
uniform float _rgb2B = 1.0f; // 1.0f
uniform float _dotWeight = 2.0f; // 2.0f
float2 RadialDistortion(float2 coord)
{
coord *= _TextureSize / _InputSize;
float2 cc = coord - _curvatureSet1;
float dist = dot(cc, cc) * _Distortion;
return (coord + cc * (_curvatureSet2 + dist) * dist) * _InputSize / _TextureSize;
}
float4 ScanlineWeights(float distance, float4 color)
{
float4 width = 2.0f + 2.0f * pow(color, float4(4.0f, 4.0f, 4.0f, 4.0f));
float4 weights = float4(distance / 0.5f, distance / 0.5f, distance / 0.5f, distance / 0.5f);
return 1.4f * exp(-pow(weights * rsqrt(0.5f * width), width)) / (0.3f + 0.2f * width);
}
float4 frag(v2f_img i) : COLOR
{
_Texcoord = i.uv;
_One = 1.0f / _TextureSize;
_OutputSize = _TextureSize;
_InputSize = _TextureSize;
_Factor = _Texcoord.x * _TextureSize.x * _OutputSize.x / _InputSize.x;
//float4 ScreenGamma = pow(tex2D(_MainTex, _Texcoord), _Gamma);
#ifdef CURVATURE
float2 xy = RadialDistortion(_Texcoord);
#else
float2 xy = _Texcoord;
#endif
float2 ratio = xy * _TextureSize - float2(0.5f, 0.5f);
float2 uvratio = frac(ratio);
xy.y = (floor(ratio.y) + _YExtra) / _TextureSize;
float4 col = tex2D(_MainTex, xy);
float4 col2 = tex2D(_MainTex, xy + float2(0.0f, _One.y));
float4 weights = ScanlineWeights(uvratio.y, col);
float4 weights2 = ScanlineWeights(1.0f - uvratio.y, col2);
float3 res = (col * weights + col2 * weights2).rgb;
float3 rgb1 = float3(_rgb1R, _rgb1G, _rgb1B);
float3 rgb2 = float3(_rgb2R, _rgb2G, _rgb2B);
float3 dotMaskWeights = lerp(rgb1, rgb2, floor(fmod(_Factor, _dotWeight)));
res *= dotMaskWeights;
return float4(pow(res, float3(1.0f / _Gamma, 1.0f / _Gamma, 1.0f / _Gamma)), 1.0f);
//return float4(pow(res, float3(1.0f / ScreenGamma.x, 1.0f / ScreenGamma.y, 1.0f / ScreenGamma.z)), 1.0f);
}
ENDCG
}
}
}
//C# below using UnityEngine; using System.Collections;
public enum CRTScanLinesSizes { S32 = 32, S64 = 64, S128 = 128, S256 = 256, S512 = 512, S1024 = 1024 };
[ExecuteInEditMode]
public class CRT : MonoBehaviour
{
#region Variables
public Shader curShader;
public float Distortion = 0.1f;
public float Gamma = 1.0f;
public float YExtra = 0.5f;
public float CurvatureSet1 = 0.5f;
public float CurvatureSet2 = 1.0f;
public float DotWeight = 1.0f;
public CRTScanLinesSizes scanSize = CRTScanLinesSizes.S512;
public Color rgb1 = Color.white;
public Color rgb2 = Color.white;
private Material curMaterial;
#endregion
#region Properties
Material material
{
get
{
if (curMaterial == null)
{
curMaterial = new Material(curShader);
curMaterial.hideFlags = HideFlags.HideAndDontSave;
}
return curMaterial;
}
}
#endregion
// Use this for initialization
void Start()
{
if (!SystemInfo.supportsImageEffects)
{
enabled = false;
return;
}
}
void OnRenderImage(RenderTexture sourceTexture, RenderTexture destTexture)
{
if (curShader != null)
{
material.SetFloat("_Distortion", Distortion);
material.SetFloat("_Gamma", Gamma);
material.SetFloat("_curvatureSet1", CurvatureSet1);
material.SetFloat("_curvatureSet2", CurvatureSet2);
material.SetFloat("_YExtra", YExtra);
material.SetFloat("_rgb1R", rgb1.r);
material.SetFloat("_rgb1G", rgb1.g);
material.SetFloat("_rgb1B", rgb1.b);
material.SetFloat("_rgb2R", rgb2.r);
material.SetFloat("_rgb2G", rgb2.g);
material.SetFloat("_rgb2B", rgb2.b);
material.SetFloat("_dotWeight", DotWeight);
material.SetVector("_TextureSize", new Vector2((float)scanSize, (float)scanSize));
Graphics.Blit(sourceTexture, destTexture, material);
}
else
{
Graphics.Blit(sourceTexture, destTexture);
}
}
// Update is called once per frame
void Update()
{
}
void OnDisable()
{
if (curMaterial)
{
DestroyImmediate(curMaterial);
}
}
}
,You can find the shader code below. I did this as an "image effect shader" in Unity. Then I apply it to my camera along with the C# script (below the shader). Looks great on desktop but on Android it just makes the screen black. I'm probably using something that doesn't work on Android, maybe someone could point me in the right direction for fixing it?
//#<!--
//# CRT-simple shader
//#
//# Copyright (C) 2011 DOLLS. Based on cgwg's CRT shader.
//#
//# Modified by fontmas: 2015-03-06
//#
//# This program is free software; you can redistribute it and/or modify it
//# under the terms of the GNU General Public License as published by the Free
//# Software Foundation; either version 2 of the License, or (at your option)
//# any later version.
//# -->
Shader "Custom/CRT"
{
Properties
{
_MainTex ("Base (RGB)", 2D) = "white" {}
}
SubShader
{
Pass
{
CGPROGRAM
#pragma vertex vert_img
#pragma fragment frag
#include "UnityCG.cginc"
#define CURVATURE
#pragma target 3.0
#define PI 3.141592653589
uniform sampler2D _MainTex;
uniform float2 _InputSize;
uniform float2 _OutputSize;
uniform float2 _TextureSize;
uniform float2 _One;
uniform float2 _Texcoord;
uniform float _Factor;
uniform float _Distortion = 0.1f; // 0.1f
uniform float _Gamma = 1.0f; // 1.0f
uniform float _curvatureSet1 = 0.5f; // 0.5f
uniform float _curvatureSet2 = 0.5f; // 0.5f
uniform float _YExtra = 0.5f; // 0.5f;
uniform float _rgb1R = 1.0f; // 1.0f
uniform float _rgb1G = 1.0f; // 1.0f
uniform float _rgb1B = 1.0f; // 1.0f
uniform float _rgb2R = 1.0f; // 1.0f
uniform float _rgb2G = 1.0f; // 1.0f
uniform float _rgb2B = 1.0f; // 1.0f
uniform float _dotWeight = 2.0f; // 2.0f
float2 RadialDistortion(float2 coord)
{
coord *= _TextureSize / _InputSize;
float2 cc = coord - _curvatureSet1;
float dist = dot(cc, cc) * _Distortion;
return (coord + cc * (_curvatureSet2 + dist) * dist) * _InputSize / _TextureSize;
}
float4 ScanlineWeights(float distance, float4 color)
{
float4 width = 2.0f + 2.0f * pow(color, float4(4.0f, 4.0f, 4.0f, 4.0f));
float4 weights = float4(distance / 0.5f, distance / 0.5f, distance / 0.5f, distance / 0.5f);
return 1.4f * exp(-pow(weights * rsqrt(0.5f * width), width)) / (0.3f + 0.2f * width);
}
float4 frag(v2f_img i) : COLOR
{
_Texcoord = i.uv;
_One = 1.0f / _TextureSize;
_OutputSize = _TextureSize;
_InputSize = _TextureSize;
_Factor = _Texcoord.x * _TextureSize.x * _OutputSize.x / _InputSize.x;
//float4 ScreenGamma = pow(tex2D(_MainTex, _Texcoord), _Gamma);
#ifdef CURVATURE
float2 xy = RadialDistortion(_Texcoord);
#else
float2 xy = _Texcoord;
#endif
float2 ratio = xy * _TextureSize - float2(0.5f, 0.5f);
float2 uvratio = frac(ratio);
xy.y = (floor(ratio.y) + _YExtra) / _TextureSize;
float4 col = tex2D(_MainTex, xy);
float4 col2 = tex2D(_MainTex, xy + float2(0.0f, _One.y));
float4 weights = ScanlineWeights(uvratio.y, col);
float4 weights2 = ScanlineWeights(1.0f - uvratio.y, col2);
float3 res = (col * weights + col2 * weights2).rgb;
float3 rgb1 = float3(_rgb1R, _rgb1G, _rgb1B);
float3 rgb2 = float3(_rgb2R, _rgb2G, _rgb2B);
float3 dotMaskWeights = lerp(rgb1, rgb2, floor(fmod(_Factor, _dotWeight)));
res *= dotMaskWeights;
return float4(pow(res, float3(1.0f / _Gamma, 1.0f / _Gamma, 1.0f / _Gamma)), 1.0f);
//return float4(pow(res, float3(1.0f / ScreenGamma.x, 1.0f / ScreenGamma.y, 1.0f / ScreenGamma.z)), 1.0f);
}
ENDCG
}
}
}
//C# below using UnityEngine; using System.Collections;
public enum CRTScanLinesSizes { S32 = 32, S64 = 64, S128 = 128, S256 = 256, S512 = 512, S1024 = 1024 };
[ExecuteInEditMode]
public class CRT : MonoBehaviour
{
#region Variables
public Shader curShader;
public float Distortion = 0.1f;
public float Gamma = 1.0f;
public float YExtra = 0.5f;
public float CurvatureSet1 = 0.5f;
public float CurvatureSet2 = 1.0f;
public float DotWeight = 1.0f;
public CRTScanLinesSizes scanSize = CRTScanLinesSizes.S512;
public Color rgb1 = Color.white;
public Color rgb2 = Color.white;
private Material curMaterial;
#endregion
#region Properties
Material material
{
get
{
if (curMaterial == null)
{
curMaterial = new Material(curShader);
curMaterial.hideFlags = HideFlags.HideAndDontSave;
}
return curMaterial;
}
}
#endregion
// Use this for initialization
void Start()
{
if (!SystemInfo.supportsImageEffects)
{
enabled = false;
return;
}
}
void OnRenderImage(RenderTexture sourceTexture, RenderTexture destTexture)
{
if (curShader != null)
{
material.SetFloat("_Distortion", Distortion);
material.SetFloat("_Gamma", Gamma);
material.SetFloat("_curvatureSet1", CurvatureSet1);
material.SetFloat("_curvatureSet2", CurvatureSet2);
material.SetFloat("_YExtra", YExtra);
material.SetFloat("_rgb1R", rgb1.r);
material.SetFloat("_rgb1G", rgb1.g);
material.SetFloat("_rgb1B", rgb1.b);
material.SetFloat("_rgb2R", rgb2.r);
material.SetFloat("_rgb2G", rgb2.g);
material.SetFloat("_rgb2B", rgb2.b);
material.SetFloat("_dotWeight", DotWeight);
material.SetVector("_TextureSize", new Vector2((float)scanSize, (float)scanSize));
Graphics.Blit(sourceTexture, destTexture, material);
}
else
{
Graphics.Blit(sourceTexture, destTexture);
}
}
// Update is called once per frame
void Update()
{
}
void OnDisable()
{
if (curMaterial)
{
DestroyImmediate(curMaterial);
}
}
}
Did you take a look at this Answer:
https://answers.unity.com/questions/1197905/shader-works-in-editorwindows-but-not-on-android.html
I think they figured out the solution by changing RenderTextureFormat.R8 to RenderTextureFormat.RGBA32
Your answer
Follow this Question
Related Questions
RWTexture2D in Compute Shader on Android? 2 Answers
Foliage shader makes objects invisible on Android Build 0 Answers
Why is shader behavior different between Android and iOS? 1 Answer
Mobile Shader with Albedo Color and Normal Map 0 Answers
How to create a mirror reflection for mobile devices 1 Answer