仅在Android上渲染纹理黑色

时间:2016-02-04 09:02:31

标签: unity3d camera rendering

如果相机不移动,我想在画布中绘制相机视图并在画布中显示纹理。我运行它为Android然后我得到黑色texuture,但对于WebPlayer是好!

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

3 个答案:

答案 0 :(得分:1)

这可能对某人有帮助......

偶然复制同样的问题......在iOS和Droid上都有效。 然后禁用相机清除,嘿presto ...在iOS上工作但在Android上黑色。 将相机清除回Z深度...... iOS和Droid都再次工作。

因此,请尝试更改相机清除设置。

答案 1 :(得分:0)

这是Unity的一个已知问题。您可以在以下网址找到更多详细信息:

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/

以及其他一些主持人和工作人员声称在将来的版本中修复它或者(带有一些不必要的傲慢),问题是用户和一个根本就不存在的bug。

<强> BUT

这听起来很傻,但是将ImageEffect添加到主摄像头。我已经制作了一个附加到我的主摄像头的虚拟效果,没有任何合理的解释,它在移动设备上修复了RenderTexture。

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

}

答案 2 :(得分:0)

我目前正在使用unity ver 2019.2.0f1。 出于未知原因,我从较早版本导入的初始相机显示黑色渲染纹理,而新相机运行良好。 我似乎已经找到了解决方案,相机本身似乎有问题。删除相机,在渲染纹理rgba8_unorm上重新创建颜色格式,并在深度缓冲区设置为24 现在两者都可以正确显示在android上。 希望这会有所帮助

相关问题