Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ////////////////////////////////////////////////
- ////////////////////CPU SIDE////////////////////
- ////////////////////////////////////////////////
- using UnityEngine;
- using System.Collections;
- [ExecuteInEditMode]
- public class Sobel : MonoBehaviour
- {
- public float threshold;
- private Material material;
- void Awake()
- {
- material = new Material(Shader.Find("My Shaders/Sobel")); //Use the shader.
- }
- void OnRenderImage(RenderTexture source, RenderTexture destination) //Before the buffer is sent to the camera, modify it with above shader.
- {
- if (threshold >= 1.0)
- {
- Graphics.Blit(source, destination); //Intensity is 0. Blit the image straight to the screen.
- return;
- }
- if (threshold < 0.0)
- threshold = 0.0f;
- material.SetFloat("_ScreenWidth", Screen.width);
- material.SetFloat("_ScreenHeight", Screen.height);
- material.SetFloat("_Threshold", threshold); //Uniform the value to the shader.
- Graphics.Blit(source, destination, material); //Blit the modified image to the screen.
- }
- }
- ////////////////////////////////////////////////
- ////////////////////GPU SIDE////////////////////
- ////////////////////////////////////////////////
- Shader "My Shaders/Sobel"
- {
- Properties
- {
- _MainTex ("Buffer", 2D) = "white" {}
- _Threshold ("Outline Threshold", Range (0, 1)) = 0
- _ScreenWidth ("Screen Width", float) = 0.0
- _ScreenHeight ("Screen Height", float) = 0.0
- }
- SubShader
- {
- Pass
- {
- CGPROGRAM
- #pragma vertex vert_img
- #pragma fragment frag
- #include "UnityCG.cginc"
- uniform sampler2D _MainTex;
- uniform float _Threshold;
- uniform float _ScreenWidth;
- uniform float _ScreenHeight;
- float4 frag(v2f_img input) : COLOR
- {
- float offsetX = 1.0 / _ScreenWidth;
- float offsetY = 1.0 / _ScreenHeight;
- float4 result = float4(0.0, 0.0, 0.0, 0.0);
- float4 sumX = float4(0.0, 0.0, 0.0, 0.0);
- float4 sumY = float4(0.0, 0.0, 0.0, 0.0);
- float sum = 0.0;
- float4 temp = float4(0.0, 0.0, 0.0, 0.0);
- temp = tex2D(_MainTex, input.uv + float2(-offsetX, -offsetY));
- sumX += temp * -3.0;
- temp = tex2D(_MainTex, input.uv + float2(offsetX, -offsetY));
- sumX += temp * 3.0;
- temp = tex2D(_MainTex, input.uv + float2(-offsetX, 0.0));
- sumX += temp * -10.0;
- temp = tex2D(_MainTex, input.uv + float2(-offsetX, 0.0));
- sumX += temp * 10.0;
- temp = tex2D(_MainTex, input.uv + float2(-offsetX, offsetY));
- sumX += temp * -3.0;
- temp = tex2D(_MainTex, input.uv + float2(offsetX, offsetY));
- sumX += temp * 3.0;
- temp = tex2D(_MainTex, input.uv + float2(-offsetX, -offsetY));
- sumY += temp * 3.0;
- temp = tex2D(_MainTex, input.uv + float2(0.0, -offsetY));
- sumY += temp * 10.0;
- temp = tex2D(_MainTex, input.uv + float2(offsetX, -offsetY));
- sumY += temp * 3.0;
- temp = tex2D(_MainTex, input.uv + float2(-offsetX, offsetY));
- sumY += temp * -3.0;
- temp = tex2D(_MainTex, input.uv + float2(0.0, offsetY));
- sumY += temp * -10.0;
- temp = tex2D(_MainTex, input.uv + float2(offsetX, offsetY));
- sumY += temp * -3.0;
- sum = sqrt((sumX * sumX) + (sumY * sumY));
- float4 textureMap = tex2D(_MainTex, input.uv);
- //Result of the Sobel Filter.
- // result = float4(sum, sum, sum, 1.0);
- if(sum > _Threshold)
- result = float4(0.0, 0.0, 0.0, 1.0);
- else
- result = float4(textureMap.rgb, 1.0);
- return result;
- }
- ENDCG
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment