Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ////////////////////////////////////////////////
- ////////////////////CPU SIDE////////////////////
- ////////////////////////////////////////////////
- using UnityEngine;
- using System.Collections;
- [ExecuteInEditMode]
- public class Noise : MonoBehaviour
- {
- public float intensity;
- private Material material;
- void Awake()
- {
- material = new Material(Shader.Find("My Shaders/Noise")); //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/Noise"
- {
- Properties
- {
- _MainTex ("Buffer", 2D) = "white" {}
- _Scale ("Intensity", Range (0, 1)) = 0
- }
- SubShader
- {
- Pass
- {
- CGPROGRAM
- #pragma vertex vert_img
- #pragma fragment frag
- #include "UnityCG.cginc"
- uniform sampler2D _MainTex;
- uniform float _Scale;
- float rand(float2 co) //The infamous "One-Liner"
- {
- const float2 random = float2(12.9898,78.233);
- const float multiplier = 43758.5453;
- return frac(sin(dot(co.xy, random)) * multiplier);
- }
- float4 frag(v2f_img input) : COLOR
- {
- float4 textureMap = tex2D(_MainTex, input.uv);
- float value = rand((input.uv * (unity_DeltaTime.x + input.uv))); //Use the current texCoord as the random input. Using deltaTime ensures a different result every frame.
- float3 randNoise = float3(value, value, value); //A float3 of the above result. All channels same colour to get a greyscale result (White noise)
- float4 result = textureMap;
- result.rgb = lerp(textureMap.rgb, randNoise, _Scale); //Mix the two results based on the weight provided by _Scale.
- return result;
- }
- ENDCG
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment