Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ////////////////////////////////////////////////
- ////////////////////CPU SIDE////////////////////
- ////////////////////////////////////////////////
- using UnityEngine;
- using System.Collections;
- [ExecuteInEditMode]
- public class Pixelation : MonoBehaviour
- {
- public float pixelX, pixelY;
- private Material material;
- void Awake()
- {
- material = new Material(Shader.Find("My Shaders/Pixelation")); //Use the shader.
- }
- void OnRenderImage(RenderTexture source, RenderTexture destination) //Before the buffer is sent to the camera, modify it with above shader.
- {
- material.SetFloat("_PixelCountX", pixelX); //Uniform the value to the shader.
- material.SetFloat("_PixelCountY", pixelY); //Uniform the value to the shader.
- Graphics.Blit(source, destination, material); //Blit the modified image to the screen.
- }
- }
- ////////////////////////////////////////////////
- ////////////////////GPU SIDE////////////////////
- ////////////////////////////////////////////////
- Shader "My Shaders/Pixelation"
- {
- Properties
- {
- _MainTex ("Buffer", 2D) = "white" {}
- _PixelCountX ("Number of X", float) = 128 //Xres default value.
- _PixelCountY ("Number of Y", float) = 72 //Yres default value.
- }
- SubShader
- {
- Pass
- {
- CGPROGRAM
- #pragma vertex vert
- #pragma fragment frag
- #include "UnityCG.cginc"
- sampler2D _MainTex;
- float _PixelCountX;
- float _PixelCountY;
- struct vertexOutput
- {
- float4 position : SV_POSITION;
- float2 texCoord : TEXCOORD0;
- };
- vertexOutput vert(appdata_base vIn) //Passthrough
- {
- vertexOutput output;
- output.texCoord = vIn.texcoord.xy;
- output.position = mul(UNITY_MATRIX_MVP, vIn.vertex);
- return output;
- }
- float4 frag(vertexOutput input) : COLOR
- {
- //Get the percentages of the screen per pixel.
- float pixelWidth = 1.0f / _PixelCountX;
- float pixelHeight = 1.0f / _PixelCountY;
- float2 texCoord = float2((int)(input.texCoord.x / pixelWidth) * pixelWidth, (int)(input.texCoord.y / pixelHeight) * pixelHeight);
- float4 colour = tex2D(_MainTex, texCoord); //Redraw the buffer using our new, low resolution texture coordinates.
- return colour;
- }
- ENDCG
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment