- Home /
Custom Render Texture appears black on mobile
Hello,
I'm trying to set the texture of my Custom Render Texture (called crtDebug) to my RenderCamera.targetTexture, which is one of the cameras I have active on my project, and show it on a Raw Image.
It works fine on my computer when I run on the Unity Editor, but the moment I run it on my Android device the texture appears to be black. Any Ideas about why this is happening?
This is my code:
using UnityEngine;
using UnityEngine.UI;
public class RC_Get_Texture : MonoBehaviour
{
public Camera RenderCamera;
public GameObject debugObject;
CustomRenderTexture crtDebug;
void Start()
{
crtDebug = (CustomRenderTexture)debugObject.GetComponent<RawImage>().texture;
}
void Update()
{
UpdateVideoTextureDebug();
}
void UpdateVideoTextureDebug()
{
crtDebug.material.SetTexture("_Tex", RenderCamera.targetTexture);
crtDebug.Update();
}
}
Leaving this comment here because this is a really common issue. One I'm still debugging right now. We've dropped custom render textures on our (fairly big) project for exactly this reason. It was just simpler to bake into the actual shader ( and fewer worries about it breaking on devices we have not tested ).
Initialization on mobile ( both android and IOS ) just does not work reliably.
Answer by Tudor-Nita · Jul 22, 2020 at 08:07 PM
Better late than never.
Either Custom render textures have an undocumented initialization step you need to do on mobile or ( more likely ) it's just broken. I have rarely had them work on the first try.
This post describes a fairly inane workaround that works in 2019.4 as of writing this: https://forum.unity.com/threads/why-does-rendertexture-never-work-on-android-build-oculus-go.630559/
TLDR: set init and update to ondemand, build an initialization shader and call initialize from script on update.
This is an initialization shader for reference.
Shader "Custom/CRTShaderInit"
{
Properties{
[Gamma]_color("Color", Color) = (1.0,1.0,1.0)
}
SubShader
{
Tags { "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" }
Lighting Off
Blend One Zero
Pass
{
CGPROGRAM
#include "UnityCustomRenderTexture.cginc"
#pragma vertex InitCustomRenderTextureVertexShader
#pragma fragment frag
#pragma target 3.0
uniform float4 _color;
float4 frag(v2f_init_customrendertexture IN) : COLOR
{
return _color;
}
ENDCG
}
}
}
Your answer
Follow this Question
Related Questions
Problem with Texture Scaling for Mobile Resolutions 1 Answer
Render texture errors on OSX 0 Answers
App not installed android bug 0 Answers
Error to install game made in Unity on mobile. Android 1 Answer
Crashing after placing terrain 0 Answers