【问题标题】:Render Texture Black on Android Only仅在 Android 上渲染黑色纹理
【发布时间】:2016-02-04 09:02:30
【问题描述】:

如果相机不移动,我想在纹理中绘制相机视图并在画布中显示纹理。我为 android 运行它,然后我得到黑色纹理,但对于 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;
}

【问题讨论】:

  • 我确定这是因为您从 Internet 上的某个地方复制粘贴了此代码。 Update()(和所有其他单一行为)函数在应用程序编译时随机排序,其他平台上的顺序可能不同。您可能需要使用 Unity 执行顺序 明确地对它们进行排序。总之 - 确保在主相机渲染时 camera.targetTexture 不为空。 其他提示:你不能有 32 位纹理Source。另外,试着把抗锯齿调低一点,8 对任何手机来说都太多了,要求很高。
  • @NikaKasradze 虽然在 SO 上人们通常会立即假设代码是在不知情的情况下粘贴的,但这是 Unity 中的错误的结果。 Unity 中一个非常知名的错误。自 4.X 以来,RenderTexture 一直存在问题,并持续到今天的当前版本中。
  • @LoungeKatt 我的错,我不知道是什么击中了我,编辑我的评论,谢谢,编辑:我无法编辑我之前的评论,哈哈,它太旧了¯_(ツ)_/¯我承认我有点粗鲁,对此我深表歉意

标签: unity3d camera rendering


【解决方案1】:

认为这可能会对某人有所帮助...

无意中复制了相同的问题...在 iOS 和 Droid 上都可以使用。 然后禁用相机清除,嘿presto ......适用于iOS但在Android上是黑色的。 将相机清晰更改回仅 Z 深度...iOS 和 Droid 再次工作。

所以请尝试更改您的相机清除设置。

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      这是 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/

      还有一些其他的,版主和工作人员声称它已在未来的版本中得到修复,或者(带有一点不必要的傲慢)问题出在用户身上,并且根本不存在错误。

      但是

      这听起来很傻,但是在主摄像头上添加一个 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
      
      }
      

      【讨论】:

        猜你喜欢
        • 2021-11-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-09-17
        • 1970-01-01
        • 2013-11-25
        • 1970-01-01
        • 2014-08-07
        相关资源
        最近更新 更多