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();
}
}
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
}
}
}