YUV420PtoRGB.shader 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. Shader "Unlit/YUV420PtoRGB"
  2. {
  3. Properties
  4. {
  5. _MainTex("Texture", 2D) = "white" {}
  6. _UTex("U", 2D) = "white" {}
  7. _VTex("V", 2D) = "white" {}
  8. //_UVTex ("UV", 2D) = "white" {}
  9. }
  10. SubShader
  11. {
  12. Tags { "RenderType" = "Opaque" }
  13. LOD 100
  14. Pass
  15. {
  16. CGPROGRAM
  17. #pragma vertex vert
  18. #pragma fragment frag
  19. // make fog work
  20. #pragma multi_compile_fog
  21. #include "UnityCG.cginc"
  22. struct appdata
  23. {
  24. float4 vertex : POSITION;
  25. float2 uv : TEXCOORD0;
  26. };
  27. struct v2f
  28. {
  29. float2 uv : TEXCOORD0;
  30. UNITY_FOG_COORDS(1)
  31. float4 vertex : SV_POSITION;
  32. };
  33. sampler2D _MainTex;
  34. sampler2D _UTex;
  35. sampler2D _VTex;
  36. sampler2D _UVTex;
  37. float4 _MainTex_ST;
  38. v2f vert(appdata v)
  39. {
  40. v2f o;
  41. o.vertex = UnityObjectToClipPos(v.vertex);
  42. o.uv = TRANSFORM_TEX(v.uv, _MainTex);
  43. UNITY_TRANSFER_FOG(o,o.vertex);
  44. return o;
  45. }
  46. fixed4 frag(v2f i) : SV_Target
  47. {
  48. //不在C#侧做数组的反转,应该在这反转一下uv的y分量即可。
  49. fixed2 uv = fixed2(i.uv.x,1 - i.uv.y);
  50. fixed4 ycol = tex2D(_MainTex, uv);
  51. fixed4 ucol = tex2D(_UTex, uv);
  52. fixed4 vcol = tex2D(_VTex, uv);
  53. //fixed4 uvcol = tex2D(_UVTex,uv);
  54. //如果是使用 Alpha8 的纹理格式写入各分量的值,各分量的值就可以直接取a通道的值
  55. float r = ycol.a + 1.4022 * vcol.a - 0.7011;
  56. float g = ycol.a - 0.3456 * ucol.a - 0.7145 * vcol.a + 0.53005;
  57. float b = ycol.a + 1.771 * ucol.a - 0.8855;
  58. //如果是使用的RGBA4444的纹理格式写入UV分量,就需要多一道计算
  59. //才可以得到正确的U V分量的值
  60. //float yVal = ycol.a;
  61. //float uVal = (uvcol.r * 15 * 16 + uvcol.g * 15) / 255;
  62. //float vVal = (uvcol.b * 15 * 16 + uvcol.a * 15) / 255;
  63. //float r = yVal + 1.4022 * vVal - 0.7011;
  64. //float g = yVal - 0.3456 * uVal - 0.7145 * vVal + 0.53005;
  65. //float b = yVal + 1.771 * uVal - 0.8855;
  66. return fixed4(r,g,b,1);
  67. }
  68. ENDCG
  69. }
  70. }
  71. }