Search Unity

shader help! mobile movie transparent shader

Discussion in 'Shaders' started by kuailaiwanli, Sep 4, 2013.

  1. kuailaiwanli

    kuailaiwanli

    Joined:
    Apr 22, 2013
    Posts:
    47
    Code (csharp):
    1. Shader "C/C"
    2. {
    3.     Properties
    4.     {
    5.         _YTex ("Y (RGB)", 2D) = "white" {}
    6.         _CrTex ("Cr (RGB)", 2D) = "white" {}
    7.         _CbTex ("Cb (RGB)", 2D) = "white" {}
    8.        
    9.                
    10.         //_TintColor ("TintColor", Vector) = (1.0, 1.0, 1.0, 1.0)
    11.         //_CutAlphaColor ("CutColor", Vector) = (1.0, 1.0, 1.0, 1.0)
    12.         //_TintColor ("TintColor", Color) = (1.0, 1.0, 1.0, 1.0)
    13.         //_CutAlphaColor ("CutColor", Color) = (1.0, 1.0, 1.0, 1.0)
    14.     //  _Range("Range",float)=0.1
    15.    
    16.        
    17.     }
    18.     SubShader
    19.     {        Tags { "Queue"="Transparent" "RenderType"="Transparent" }
    20.         //Offset -1, -1 // This line could be here, or anywhere else before "SubShader".\
    21.         Cull Off
    22.         Lighting Off
    23.         ZWrite Off
    24.         Blend SrcAlpha OneMinusSrcAlpha
    25.         Pass
    26.         {
    27.             ColorMask RGB
    28.             Lighting Off Fog { Color (0,0,0,0) }
    29.  
    30.             Program "vp" {
    31. // Vertex combos: 1
    32. //   opengl - ALU: 5 to 5
    33. //   d3d9 - ALU: 5 to 5
    34. SubProgram "opengl " {
    35. Keywords { }
    36. Bind "vertex" Vertex
    37. Bind "texcoord" TexCoord0
    38. Vector 5 [_YTex_ST]
    39. "!!ARBvp1.0
    40. # 5 ALU
    41. PARAM c[6] = { program.local[0],
    42.         state.matrix.mvp,
    43.         program.local[5] };
    44. MAD result.texcoord[0].xy, vertex.texcoord[0], c[5], c[5].zwzw;
    45. DP4 result.position.w, vertex.position, c[4];
    46. DP4 result.position.z, vertex.position, c[3];
    47. DP4 result.position.y, vertex.position, c[2];
    48. DP4 result.position.x, vertex.position, c[1];
    49. END
    50. # 5 instructions, 0 R-regs
    51. "
    52. }
    53.  
    54. SubProgram "d3d9 " {
    55. Keywords { }
    56. Bind "vertex" Vertex
    57. Bind "texcoord" TexCoord0
    58. Matrix 0 [glstate_matrix_mvp]
    59. Vector 4 [_YTex_ST]
    60. "vs_2_0
    61. ; 5 ALU
    62. dcl_position0 v0
    63. dcl_texcoord0 v1
    64. mad oT0.xy, v1, c4, c4.zwzw
    65. dp4 oPos.w, v0, c3
    66. dp4 oPos.z, v0, c2
    67. dp4 oPos.y, v0, c1
    68. dp4 oPos.x, v0, c0
    69. "
    70. }
    71.  
    72. SubProgram "gles " {
    73. Keywords { }
    74. "!!GLES
    75. #define SHADER_API_GLES 1
    76. #define tex2D texture2D
    77.  
    78.  
    79. #ifdef VERTEX
    80. #define gl_ModelViewProjectionMatrix glstate_matrix_mvp
    81. uniform mat4 glstate_matrix_mvp;
    82.  
    83. varying highp vec2 xlv_TEXCOORD0;
    84.  
    85. uniform highp vec4 _YTex_ST;
    86. attribute vec4 _glesMultiTexCoord0;
    87. attribute vec4 _glesVertex;
    88. void main ()
    89. {
    90.  gl_Position = (gl_ModelViewProjectionMatrix * _glesVertex);
    91.  xlv_TEXCOORD0 = ((_glesMultiTexCoord0.xy * _YTex_ST.xy) + _YTex_ST.zw);
    92. }
    93.  
    94.  
    95.  
    96. #endif
    97. #ifdef FRAGMENT
    98.  
    99. varying highp vec2 xlv_TEXCOORD0;
    100. uniform sampler2D _YTex;
    101. uniform sampler2D _CrTex;
    102. uniform sampler2D _CbTex;
    103. //vec4 _TintColor;
    104. //vec4 _CutAlphaColor;
    105. //uniform vec _Range;
    106. //uniform vec4 _TintColor;
    107. //uniform vec4 _CutAlphaColor;
    108.  
    109. void main ()
    110. {
    111.  mediump vec4 rgbVec;
    112.  mediump vec4 yuvVec;
    113.  lowp vec4 tmpvar_1;
    114.  tmpvar_1.w = 1.0;
    115.  tmpvar_1.x = texture2D (_YTex, xlv_TEXCOORD0).x;
    116.  tmpvar_1.y = texture2D (_CrTex, xlv_TEXCOORD0).y;
    117.  tmpvar_1.z = texture2D (_CbTex, xlv_TEXCOORD0).z;
    118.  yuvVec = tmpvar_1;
    119.  rgbVec.x = dot (vec4(1.16438, 0.0, 1.59603, -0.870785), yuvVec);
    120.  rgbVec.y = dot (vec4(1.16438, -0.391762, -0.812969, 0.529594), yuvVec);
    121.  rgbVec.z = dot (vec4(1.16438, 2.01723, 0.0, -1.08139), yuvVec);
    122.  rgbVec.w = 1.0;
    123.  
    124.  
    125. //if(rgbVec.x == _CutAlphaColor.x rgbVec.y == _CutAlphaColor.y&rgbVec.z == _CutAlphaColor.z) rgbVec=_TintColor;
    126.  
    127.  //mediump vec4 abc = texture2D (_YTex, xlv_TEXCOORD0);
    128.  //rgbVec.w = rgbVec.x*0.299 + rgbVec.y*0.587 + rgbVec.z*0.114;
    129.  
    130. // rgbVec.w =(1- rgbVec.x)*(1-rgbVec.y)*rgbVec.z;
    131.  lowp vec4 tmpvar_2 = vec4(0.2466096, 0.8694299, 0.3780449, -0.2442727);
    132.  lowp vec4 tmpvar_3 = vec4(0.9210526, 1, 1, 6.909088);
    133.  rgbVec.w =((length (((yuvVec.xyz - tmpvar_2.xyz) * tmpvar_3.xyz))+ tmpvar_2.w) * tmpvar_3.w);
    134.  gl_FragData[0] = rgbVec;
    135. }
    136.  
    137.  
    138.  
    139. #endif"
    140. }
    141.  
    142. SubProgram "glesdesktop " {
    143. Keywords { }
    144. "!!GLES
    145. #define SHADER_API_GLES 1
    146. #define tex2D texture2D
    147.  
    148.  
    149. #ifdef VERTEX
    150. #define gl_ModelViewProjectionMatrix glstate_matrix_mvp
    151. uniform mat4 glstate_matrix_mvp;
    152.  
    153. varying highp vec2 xlv_TEXCOORD0;
    154.  
    155. uniform highp vec4 _YTex_ST;
    156. attribute vec4 _glesMultiTexCoord0;
    157. attribute vec4 _glesVertex;
    158. void main ()
    159. {
    160.  gl_Position = (gl_ModelViewProjectionMatrix * _glesVertex);
    161.  xlv_TEXCOORD0 = ((_glesMultiTexCoord0.xy * _YTex_ST.xy) + _YTex_ST.zw);
    162. }
    163.  
    164.  
    165.  
    166. #endif
    167. #ifdef FRAGMENT
    168.  
    169. varying highp vec2 xlv_TEXCOORD0;
    170. uniform sampler2D _YTex;
    171. uniform sampler2D _CrTex;
    172. uniform sampler2D _CbTex;
    173. //fixed4 _TintColor;
    174. //fixed4 _CutAlphaColor;
    175. //uniform vec _Range;
    176. //uniform vec4 _TintColor;
    177. //uniform vec4 _CutAlphaColor;
    178. void main ()
    179. {
    180.  mediump vec4 rgbVec;
    181.  mediump vec4 yuvVec;
    182.  lowp vec4 tmpvar_1;
    183.  tmpvar_1.w = 1.0;
    184.  tmpvar_1.x = texture2D (_YTex, xlv_TEXCOORD0).x;
    185.  tmpvar_1.y = texture2D (_CrTex, xlv_TEXCOORD0).y;
    186.  tmpvar_1.z = texture2D (_CbTex, xlv_TEXCOORD0).z;
    187.  yuvVec = tmpvar_1;
    188.  rgbVec.x = dot (vec4(1.16438, 0.0, 1.59603, -0.870785), yuvVec);
    189.  rgbVec.y = dot (vec4(1.16438, -0.391762, -0.812969, 0.529594), yuvVec);
    190.  rgbVec.z = dot (vec4(1.16438, 2.01723, 0.0, -1.08139), yuvVec);
    191.  rgbVec.w = 1.0;
    192.  
    193.  //mediump vec4 abc = texture2D (_YTex, xlv_TEXCOORD0);
    194.  //rgbVec.w = rgbVec.x*0.299 + rgbVec.y*0.587 + rgbVec.z*0.114;
    195.  lowp vec4 tmpvar_2 = vec4(0.2466096, 0.8694299, 0.3780449, -0.2442727);
    196.  lowp vec4 tmpvar_3 = vec4(0.9210526, 1, 1, 6.909088);
    197.  rgbVec.w =((length (((yuvVec.xyz - tmpvar_2.xyz) * tmpvar_3.xyz))+ tmpvar_2.w) * tmpvar_3.w);
    198.  gl_FragData[0] = rgbVec;
    199. }
    200.  
    201.  
    202.  
    203. #endif"
    204. }
    205.  
    206. }
    207. Program "fp" {
    208. // Fragment combos: 1
    209. //   opengl - ALU: 8 to 8, TEX: 3 to 3
    210. //   d3d9 - ALU: 6 to 6, TEX: 3 to 3
    211. SubProgram "opengl " {
    212. Keywords { }
    213. SetTexture 0 [_YTex] 2D
    214. SetTexture 1 [_CrTex] 2D
    215. SetTexture 2 [_CbTex] 2D
    216. "!!ARBfp1.0
    217. # 8 ALU, 3 TEX
    218. PARAM c[5] = { { 1 },
    219.         { 1.1640625, 2.0175781, 0, -1.0810547 },
    220.         { 1.1640625, -0.3918457, -0.81298828, 0.52978516 },
    221.         { 1.1640625, 0, 1.5957031, -0.87060547 },
    222.         { 0.299, 0.587, 0.114, 0 }
    223.          };
    224. TEMP R0,R1;
    225. TEX R0.x, fragment.texcoord[0], texture[0], 2D;
    226. TEX R0.y, fragment.texcoord[0], texture[1], 2D;
    227. TEX R0.z, fragment.texcoord[0], texture[2], 2D;
    228. MOV R0.w, c[0].x;
    229.  
    230. DP4 R1.z, R0, c[1];
    231. DP4 R1.y, R0, c[2];
    232. DP4 R1.x, R0, c[3];
    233. MOV R1.w, c[0].x;
    234.  
    235.  
    236. MOV result.color.z, R1.z;
    237. MOV result.color.y, R1.y;
    238. MOV result.color.x, R1.x;
    239. DP4 result.color.w, R1, c[4];
    240. END
    241. # 8 instructions, 1 R-regs
    242. "
    243. }
    244.  
    245. SubProgram "d3d9 " {
    246. Keywords { }
    247. SetTexture 0 [_YTex] 2D
    248. SetTexture 1 [_CrTex] 2D
    249. SetTexture 2 [_CbTex] 2D
    250. "ps_2_0
    251. ; 8 ALU, 3 TEX
    252. dcl_2d s0
    253. dcl_2d s1
    254. dcl_2d s2
    255. def c0, 1.00000000, 0, 0, 0
    256. def c1, 1.16406250, 2.01757813, 0.00000000, -1.08105469
    257. def c2, 1.16406250, -0.39184570, -0.81298828, 0.52978516
    258. def c3, 1.16406250, 0.00000000, 1.59570313, -0.87060547
    259. dcl t0.xy
    260. texld r1, t0, s2
    261. texld r0, t0, s0
    262. mov_pp r1.x, r0.x
    263. texld r0, t0, s1
    264. mov_pp r1.y, r0.y
    265. mov_pp r1.w, c0.x
    266. dp4_pp r0.z, r1, c1
    267. dp4_pp r0.y, r1, c2
    268. dp4_pp r0.x, r1, c3
    269. mov_pp r0.w, c0.x
    270. mov_pp oC0, r0
    271. "
    272. }
    273.  
    274. SubProgram "gles " {
    275. Keywords { }
    276. "!!GLES"
    277. }
    278.  
    279. SubProgram "glesdesktop " {
    280. Keywords { }
    281. "!!GLES"
    282. }
    283.  
    284. }
    285.  
    286. #LINE 60
    287.  
    288.         }
    289.  
    290.    
    291.  
    292.        
    293.        
    294.     }
    295. }
    296.  
    i have a shader like this. it will cut the blue color.
    but the color can't be cleared thoroughly.
    there are still some blue edges on the image.
    could any one modify this shader for me? i want to add a value to control border,and set a color to cut
    note:this shader is used on mobile