Guest User

pixellate-fixed.cg

a guest
Aug 12th, 2013
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. void main_vertex
  2. (
  3. float4 position : POSITION,
  4. out float4 oPosition : POSITION,
  5. uniform float4x4 modelViewProj,
  6.  
  7. float2 tex : TEXCOORD,
  8. out float2 oTex : TEXCOORD
  9. )
  10. {
  11. oPosition = mul(modelViewProj, position);
  12. oTex = tex;
  13. }
  14.  
  15. struct input
  16. {
  17. float2 video_size;
  18. float2 texture_size;
  19. float2 output_size;
  20. float frame_count;
  21. float frame_direction;
  22. float frame_rotation;
  23. };
  24.  
  25. float4 main_fragment(uniform sampler2D tex : TEXUNIT0, float2 coords : TEXCOORD0, uniform input IN) : COLOR
  26. {
  27. float2 texelSize = 1.0 / IN.texture_size;
  28.  
  29. float2 range = float2(abs(dFdx(coords.x)), abs(dFdy(coords.y)));
  30. range = range / 2.0 * 0.999;
  31.  
  32. float left = coords.x - range.x;
  33. float top = coords.y + range.y;
  34. float right = coords.x + range.x;
  35. float bottom = coords.y - range.y;
  36.  
  37. float3 topLeftColor = tex2D(tex, (floor(float2(left, top) / texelSize) + 0.5) * texelSize).rgb;
  38. float3 bottomRightColor = tex2D(tex, (floor(float2(right, bottom) / texelSize) + 0.5) * texelSize).rgb;
  39. float3 bottomLeftColor = tex2D(tex, (floor(float2(left, bottom) / texelSize) + 0.5) * texelSize).rgb;
  40. float3 topRightColor = tex2D(tex, (floor(float2(right, top) / texelSize) + 0.5) * texelSize).rgb;
  41.  
  42. float2 border = clamp(round(coords / texelSize) * texelSize, float2(left, bottom), float2(right, top));
  43.  
  44. float totalArea = 4.0 * range.x * range.y;
  45.  
  46. float3 averageColor;
  47. averageColor = ((border.x - left) * (top - border.y) / totalArea) * topLeftColor;
  48. averageColor += ((right - border.x) * (border.y - bottom) / totalArea) * bottomRightColor;
  49. averageColor += ((border.x - left) * (border.y - bottom) / totalArea) * bottomLeftColor;
  50. averageColor += ((right - border.x) * (top - border.y) / totalArea) * topRightColor;
  51.  
  52. return float4(averageColor, 1.0);
  53. }
Advertisement
Add Comment
Please, Sign In to add comment