Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ////////////////////////////////////////////////
- ////////////////////CPU SIDE////////////////////
- ////////////////////////////////////////////////
- using UnityEngine;
- using System.Collections;
- [ExecuteInEditMode]
- public class Greyscale : MonoBehaviour
- {
- public float intensity;
- private Material material;
- void Awake()
- {
- material = new Material(Shader.Find("My Shaders/Greyscale")); //Use the shader.
- }
- void OnRenderImage(RenderTexture source, RenderTexture destination) //Before the buffer is sent to the camera, modify it with above shader.
- {
- if (intensity == 0)
- {
- Graphics.Blit(source, destination); //Intensity is 0. Blit the image straight to the screen.
- return;
- }
- material.SetFloat("_Scale", intensity); //Uniform the value to the shader.
- Graphics.Blit(source, destination, material); //Blit the modified image to the screen.
- }
- }
- ////////////////////////////////////////////////
- ////////////////////GPU SIDE////////////////////
- ////////////////////////////////////////////////
- Shader "My Shaders/Greyscale"
- {
- Properties
- {
- _MainTex ("Buffer", 2D) = "white" {}
- _Scale ("Intensity", Range (0, 1)) = 0
- }
- SubShader
- {
- Pass
- {
- CGPROGRAM
- #pragma vertex vert_img //Standard vertex shader as we don't need to make a custom one.
- #pragma fragment frag
- #include "UnityCG.cginc"
- uniform sampler2D _MainTex;
- uniform float _Scale;
- float4 frag(v2f_img input) : COLOR
- {
- float4 textureMap = tex2D(_MainTex, input.uv);
- float lum = textureMap.r * 0.2126 + textureMap.g * 0.7152 + textureMap.b * 0.0722; //Standard values for HDTV greyscale calibration. It is important that each channel is modified by a seperate amount due to luminosity.
- float3 greyscale = float3(lum, lum, lum); //A vec3 result of the above.
- float4 result = textureMap;
- result.rgb = lerp(textureMap.rgb, greyscale, _Scale); //Mix between the original buffer and the greyscale modification based off the weight of _Scale.
- return result;
- }
- ENDCG
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement