Advertisement
Guest User

unblend.cg

a guest
May 18th, 2014
450
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. /* COMPATIBILITY
  2. - HLSL compilers
  3. - Cg compilers
  4. */
  5.  
  6. /*
  7. Hyllian's Unblend Shader
  8.  
  9. Copyright (C) 2011-2014 Hyllian/Jararaca - sergiogdb@gmail.com
  10.  
  11. This program is free software; you can redistribute it and/or
  12. modify it under the terms of the GNU General Public License
  13. as published by the Free Software Foundation; either version 2
  14. of the License, or (at your option) any later version.
  15.  
  16. This program is distributed in the hope that it will be useful,
  17. but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. GNU General Public License for more details.
  20.  
  21. You should have received a copy of the GNU General Public License
  22. along with this program; if not, write to the Free Software
  23. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  24.  
  25. */
  26.  
  27.  
  28. float c_df(float3 c1, float3 c2)
  29. {
  30. float3 df = abs(c1 - c2);
  31. return df.r + df.g + df.b;
  32. // return 2*df.r + 3*df.g + df.b;
  33. }
  34.  
  35.  
  36. float3 bestfit(float3 ca, float3 cb, float3 cref)
  37. {
  38. // return lerp(ca, cb, step(c_df(cref, cb), c_df(cref, ca)));
  39. return lerp(min(ca, cb), max(ca, cb), step((ca+cb)*0.5, cref));
  40. }
  41.  
  42.  
  43.  
  44. struct orig
  45. {
  46. float2 tex_coord;
  47. uniform float2 texture_size;
  48. uniform sampler2D texture;
  49. };
  50.  
  51.  
  52. struct input
  53. {
  54. float2 video_size;
  55. float2 texture_size;
  56. float2 output_size;
  57. float frame_count;
  58. float frame_direction;
  59. float frame_rotation;
  60. };
  61.  
  62. struct out_vertex {
  63. float4 position : POSITION;
  64. float4 color : COLOR;
  65. float2 texCoord : TEXCOORD0;
  66. float2 orig_tex;
  67. };
  68.  
  69. /* VERTEX_SHADER */
  70. void main_vertex
  71. (
  72. float4 position : POSITION,
  73. float4 color : COLOR,
  74. float2 texCoord : TEXCOORD0,
  75.  
  76. uniform float4x4 modelViewProj,
  77. orig ORIG,
  78. out out_vertex co
  79. )
  80. {
  81. co.position = mul(modelViewProj, position);
  82. co.color = color;
  83.  
  84. co.texCoord = texCoord;
  85. co.orig_tex = ORIG.tex_coord;
  86. }
  87.  
  88.  
  89. /* FRAGMENT SHADER */
  90. float4 main_fragment(in out_vertex co, uniform sampler2D decal : TEXUNIT0, orig ORIG, uniform input IN) : COLOR
  91. {
  92. float2 dx = float2(1.0, 0.0)/IN.texture_size;
  93. float2 dy = float2(0.0, 1.0)/IN.texture_size;
  94.  
  95. float2 tc = (floor(co.texCoord*IN.texture_size-float2(0.5,0.5))+float2(0.5,0.5))/IN.texture_size;
  96.  
  97. float3 A = tex2D(ORIG.texture, tc ).xyz;
  98. float3 B = tex2D(ORIG.texture, tc +dx ).xyz;
  99. float3 C = tex2D(ORIG.texture, tc +dy).xyz;
  100. float3 D = tex2D(ORIG.texture, tc +dx+dy).xyz;
  101.  
  102. float3 color = tex2D(decal, co.texCoord).xyz;
  103.  
  104. color = bestfit(bestfit(A, B, color), bestfit(C, D, color), color);
  105.  
  106. return float4(color, 1.0);
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement