RGB to YUV using shader

I want to use fragment shader to convert RGB to YUV420P using shader.

1,I figured out the coordinates of the current texture color.

2,I calculated the 4 Y and put it on the current color.

My code:

    precision mediump float;
    varying vec2 vTextureCoord;
    uniform sampler2D tex;
    uniform float width;
    uniform float height;
    uniform float offset;
    uniform vec3 scal;

    void main(void) {
        vec2 nowTxtPos = vTextureCoord;
        vec2 size = vec2(width, height);
    // y
        if(nowTxtPos.y<0.25){
    // y1 postion
            vec2 now_pos = nowTxtPos * size;
            vec2 basePos = now_pos * vec2(4.0,4.0);
            float addY = float(int(basePos.x / width));
            basePos.x = basePos.x - addY * width;
            basePos.y += addY;

            float y1,y2,y3,y4;
            vec2 samplingPos = basePos / size;
            vec4 texel = texture2D(tex, samplingPos);
            y1 = dot(texel.rgb, scal);
            y1 += offset;

            basePos.x+=1.0;
            samplingPos = basePos/size;
            texel = texture2D(tex, samplingPos);
            y2 = dot(texel.rgb, scal);
            y2 += offset;

            basePos.x+=1.0;
            samplingPos = basePos/size;
            texel = texture2D(tex, samplingPos);
            y3 = dot(texel.rgb, scal);
            y3 += offset;

            basePos.x+=1.0;
            samplingPos = basePos/size;
            texel = texture2D(tex, samplingPos);
            y4 = dot(texel.rgb, scal);
            y4 += offset;

            gl_FragColor = vec4(y1, y2, y3, y4);
        }
    }

The picture(YUV420P):

picture

Enlarged post picture

It has aliasing. I don't know why.

share improve this question
 

1 Answer

up vote 1 down vote accepted

I have solved it. Because the calculation accuracy is not enough.

Edit here:

"precision mediump float;" -> "precision highp float;"

share improve this answer

https://stackoverflow.com/questions/33339874/rgb-to-yuv-using-shader
### 处理Unity中YUV420 NV12格式的数据 在Unity环境中处理YUV420 NV12格式的数据主要涉及两个方面:一是理解这种数据格式本身的特点;二是掌握如何利用Shader或其他方法将其转换成可以在屏幕上正常显示的颜色模式。 #### YUV420 NV12格式解析 NV12是一种特定类型的YUV采样格式,在该格式下,亮度(Y)和平滑度(CbCr或UV)被分开存储。具体来说,对于每一个像素点都有一个对应的Y值表示其亮度信息,而每四个相邻的像素共享一组UV值用于描述色彩差异[^2]。这意味着如果原始图像是宽度W高度H,则Y平面大小为W×H字节,UV平面上则只有(W/2)×(H/2)*2字节因为它们是以半分辨率保存的。 #### 使用Shader实现NV12转RGB 为了能够在Unity内正确展示这些来自外部源或是自定义生成的NV12编码图片,通常会编写专门针对此目的设计的着色器程序——即所谓的“Shader”。下面是一个简单的例子展示了怎样创建这样一个Shader: ```csharp // C#脚本部分, 用来传递纹理给shader using UnityEngine; public class Nv12Texture : MonoBehaviour { public RenderTexture yPlane; public RenderTexture uvPlane; void Start() { Material material = new Material(Shader.Find("Custom/NV12toRGB")); Graphics.Blit(null, null, material); material.SetTexture("_YTex", yPlane); material.SetTexture("_UTex", uvPlane); } } ``` ```glsl // ShaderLab代码片段 Shader "Custom/NV12toRGB" { Properties { _YTex ("Luminance (Y)", 2D) = "" {} _UTex ("Chrominance (U/V interleaved)", 2D) = "" {} } SubShader { Pass { CGPROGRAM #pragma vertex vert_img #pragma fragment frag sampler2D _YTex; // Luma texture. sampler2D _UTex; // Chroma UV packed together. fixed4 frag(v2f_img i): COLOR { float y = tex2D(_YTex,i.uv).r; half2 chroma_uv = UnpackHalf2x16(tex2D(_UTex,floor(i.uv * _ScreenParams.xy)/_ScreenParams.xy)); // Convert from YUV to RGB color space using standard conversion formulae float r = y + 1.402 * (chroma_uv.g-0.5); float g = y - 0.344136 * (chroma_uv.r-0.5) - 0.714136*(chroma_uv.g-0.5); float b = y + 1.772 * (chroma_uv.r-0.5); return fixed4(r,g,b,1); } ENDCG } } } ``` 上述C#脚本负责准备并设置材质属性,而GLSL风格的ShaderLab代码实现了具体的颜色空间变换逻辑。这里的关键在于`frag()`函数内部的操作,它接收输入坐标计算出相应的Y、U、V分量之后再依据标准公式转化为最终呈现出来的红绿蓝三原色强度[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值