【问题标题】:YUV shader for Unity?Unity 的 YUV 着色器?
【发布时间】:2021-04-19 18:24:14
【问题描述】:

我在 Unity 中有一些(适当的、平面的)YUV 纹理,我只是想看看它们以供开发人员使用。

是否真的有一个适用于 Unity 的有效 YUV 着色器(他们的着色器语言变体)?

从概念上来说,从 YUV 转换为 RGB 很简单……但有点繁琐。

【问题讨论】:

  • 据我了解,您正在寻找 YUV -> RGB 着色器,对吧?
  • 谢谢 - 嗯,基本上是的。所以它是一个着色器,它将采用 YUV 格式的纹理.. 并绘制它。

标签: unity3d yuv unity3d-shaders


【解决方案1】:

YUV 到 RGB 转换着色器:

 Shader "Hidden/YUVtoRGB"
    {
        Properties
        {
            _MainTex ("Texture", 2D) = "white" {}
        }
        SubShader
        {
            // No culling or depth
            Cull Off ZWrite Off ZTest Always

            Pass
            {
                CGPROGRAM
                #pragma vertex vert
                #pragma fragment frag

                #include "UnityCG.cginc"

                struct appdata
                {
                    float4 vertex : POSITION;
                    float2 uv : TEXCOORD0;
                };

                struct v2f
                {
                    float2 uv : TEXCOORD0;
                    float4 vertex : SV_POSITION;
                };

                v2f vert (appdata v)
                {
                    v2f o;
                    o.vertex = UnityObjectToClipPos(v.vertex);
                    o.uv = v.uv;
                    return o;
                }

                sampler2D _MainTex;


                fixed3 YUVtoRGB(fixed3 c)
                {
                    fixed3 rgb;
                    rgb.r = c.x + c.z * 1.13983;
                    rgb.g = c.x + dot(fixed2(-0.39465, -0.58060), c.yz);
                    rgb.b = c.x + c.y * 2.03211;
                    return rgb;
                }

                fixed4 frag (v2f i) : SV_Target
                {
                    fixed4 yuv = tex2D(_MainTex, i.uv);
                    fixed4 rgb = fixed4(YUVtoRGB(yuv.rgb), yuv.a);
                    return rgb;
                }
                ENDCG
            }
        }
    }

【讨论】:

  • 啊..我明白了..所以你只是在片段中这样做............太棒了!太棒了!
  • 很高兴能帮上忙。
  • 谢谢@Fattie!
  • 这个着色器是如何使用的?我有每像素 12 位的 YUV420p 数据。此着色器是否适用于不同的 YUV 格式?
【解决方案2】:

只是为了回答,

你可以非常简单地只使用 YUV 的 Y 通道,

您将获得准确的单色版本的图像。

所以,用抽象的伪代码术语来说,只需做这样的事情..

uint32_t grayscale_from_y(uint8_t y)
    {
        // when we want to show a YUV texture on screen
        // for checking during development,
        // you can be super lazy and just feed
        // the "Y" to RGB upstream,
        // set R G and B to the Y value, something like this:

        uint32_t r = y;
        uint32_t g = y;
        uint32_t b = y;
        return r + (g << 8) + (b << 16) + 0xff000000;
        // (alpha is one there)
    }

它可能对某人有帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多