Gary_Keen27

Pixelation Filter

Nov 2nd, 2016
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ////////////////////////////////////////////////
  2. ////////////////////CPU SIDE////////////////////
  3. ////////////////////////////////////////////////
  4. using UnityEngine;
  5. using System.Collections;
  6.  
  7. [ExecuteInEditMode]
  8. public class Pixelation : MonoBehaviour
  9. {
  10.     public float pixelX, pixelY;
  11.     private Material material;
  12.  
  13.     void Awake()
  14.     {
  15.         material = new Material(Shader.Find("My Shaders/Pixelation")); //Use the shader.
  16.     }
  17.  
  18.     void OnRenderImage(RenderTexture source, RenderTexture destination) //Before the buffer is sent to the camera, modify it with above shader.
  19.     {
  20.         material.SetFloat("_PixelCountX", pixelX); //Uniform the value to the shader.
  21.         material.SetFloat("_PixelCountY", pixelY); //Uniform the value to the shader.
  22.         Graphics.Blit(source, destination, material); //Blit the modified image to the screen.
  23.     }
  24. }
  25.  
  26. ////////////////////////////////////////////////
  27. ////////////////////GPU SIDE////////////////////
  28. ////////////////////////////////////////////////
  29. Shader "My Shaders/Pixelation"
  30. {
  31.     Properties
  32.     {
  33.         _MainTex ("Buffer", 2D) = "white" {}
  34.         _PixelCountX ("Number of X", float) = 128 //Xres default value.
  35.         _PixelCountY ("Number of Y", float) = 72 //Yres default value.
  36.     }
  37.     SubShader
  38.     {      
  39.         Pass
  40.         {            
  41.             CGPROGRAM
  42.  
  43.             #pragma vertex vert
  44.             #pragma fragment frag
  45.                            
  46.             #include "UnityCG.cginc"
  47.            
  48.             sampler2D _MainTex;
  49.             float _PixelCountX;
  50.             float _PixelCountY;
  51.                    
  52.             struct vertexOutput
  53.             {
  54.                 float4 position : SV_POSITION;
  55.                 float2 texCoord : TEXCOORD0;
  56.             };
  57.            
  58.             vertexOutput vert(appdata_base vIn) //Passthrough
  59.             {
  60.                 vertexOutput output;               
  61.                 output.texCoord = vIn.texcoord.xy;
  62.                 output.position = mul(UNITY_MATRIX_MVP, vIn.vertex);
  63.                
  64.                 return output;
  65.             }
  66.            
  67.             float4 frag(vertexOutput input) : COLOR
  68.             {
  69.                 //Get the percentages of the screen per pixel.
  70.                 float pixelWidth = 1.0f / _PixelCountX;
  71.                 float pixelHeight = 1.0f / _PixelCountY;
  72.  
  73.  
  74.                 float2 texCoord = float2((int)(input.texCoord.x / pixelWidth) * pixelWidth, (int)(input.texCoord.y / pixelHeight) * pixelHeight);
  75.                 float4 colour = tex2D(_MainTex, texCoord); //Redraw the buffer using our new, low resolution texture coordinates.
  76.            
  77.                 return colour;
  78.             }
  79.             ENDCG
  80.         }
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment