Render Texture Black on Android Only

放肆的年华 提交于 2019-12-08 03:09:51

问题


I want to draw view of camera in texture and show texture in canvas, if camera not moving. I run it for android then I got black texuture, but for WebPlayer is Good!

public RawImage rawImage;

private void Start()
{
   texture = new RenderTexture(camera.pixelWidth, camera.pixelHeight, 32, RenderTextureFormat.ARGB32);
    texture.antiAliasing = 8;
    texture.Create();
}

public void ShowTexture()
{
   camera.targetTexture = texture;
   RenderTexture.active = texture2;
   if (!RenderTexture.active.IsCreated())
      RenderTexture.active.Create();

   camera.Render();

   var texture2d = new Texture2D(camera.targetTexture.width, camera.targetTexture.height, TextureFormat.RGB24, true, true);
   texture2d.ReadPixels(new Rect(0, 0, camera.pixelWidth, camera.pixelHeight), 0, 0);
   texture2d.Apply(false);

   RenderTexture.active = null;
   camera.targetTexture = null;

  rawImage.texture = texture2d;
}

回答1:


figure this might help someone...

Replicated same issue by accident... working both on iOS and Droid. Then disabled camera clear, and hey presto... works on iOS but black on Android. changed camera clear back to Z-depth only... and both iOS and Droid work again.

So try changing your camera clear settings.




回答2:


This is a known issue with Unity. You can find more details at:

https://forum.unity3d.com/threads/rendertexture-not-working-on-ios-and-android-unity-4-2-0f4.192561/

https://forum.unity3d.com/threads/render-texture-not-working-on-device-unity-5-2-1f1.358483/

https://forum.unity3d.com/threads/render-texture-works-in-editor-but-not-on-devices-after-upgrade-to-unity-5.362397/

and a few others where the moderators and staff claim it is fixed in a future release or (with a touch of unnecessary arrogance) that the issue is the user and a bug never existed at all.

BUT

This is going to sound silly, but add an ImageEffect to the main camera. I have made a dummy effect that is attached to my main camera and without any logical explanation, it fixes RenderTexture on mobile.

DummyEffect.cs:

using UnityEngine;

[ExecuteInEditMode]
[AddComponentMenu("Image Effects/Dummy Effect")]
public class DummyEffect : ImageEffectBase {

    // Called by camera to apply image effect
    void OnRenderImage (RenderTexture source, RenderTexture destination) {
        Graphics.Blit (source, destination, material);
    }
}

DummyEffect.shader:

Shader "Hidden/Dummy Effect" {
Properties {
    _MainTex ("Base (RGB)", RECT) = "white" {}
}

SubShader {
    Pass {
        ZTest Always Cull Off ZWrite Off
        Fog { Mode off }

CGPROGRAM
#pragma vertex vert_img
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"

uniform sampler2D _MainTex;

float4 frag (v2f_img i) : COLOR {
    return tex2D(_MainTex, i.uv);
}
ENDCG

    }
}

Fallback off

}



回答3:


I am currently working with unity ver 2019.2.0f1. For unknown reasons the initial camera I had imported from an earlier version was showing a black render texture and the new one was working perfectly. I seem to have found a solution, something on the camera itself seems to be the issue. Delete camera, re-create and Color Format on the render texture rgba8_unorm with depth buffer at 24 Now both show up properly on android. Hope this helps



来源:https://stackoverflow.com/questions/35196619/render-texture-black-on-android-only

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!