Gary_Keen27

Sobel Filter

Nov 2nd, 2016
219
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 Sobel : MonoBehaviour
  9. {
  10.  
  11.     public float threshold;
  12.     private Material material;
  13.  
  14.     void Awake()
  15.     {
  16.         material = new Material(Shader.Find("My Shaders/Sobel")); //Use the shader.
  17.     }
  18.  
  19.     void OnRenderImage(RenderTexture source, RenderTexture destination) //Before the buffer is sent to the camera, modify it with above shader.
  20.     {
  21.         if (threshold >= 1.0)
  22.         {
  23.             Graphics.Blit(source, destination); //Intensity is 0. Blit the image straight to the screen.
  24.             return;
  25.         }
  26.         if (threshold < 0.0)
  27.             threshold = 0.0f;
  28.  
  29.         material.SetFloat("_ScreenWidth", Screen.width);
  30.         material.SetFloat("_ScreenHeight", Screen.height);
  31.         material.SetFloat("_Threshold", threshold); //Uniform the value to the shader.
  32.         Graphics.Blit(source, destination, material); //Blit the modified image to the screen.
  33.     }
  34. }
  35.  
  36. ////////////////////////////////////////////////
  37. ////////////////////GPU SIDE////////////////////
  38. ////////////////////////////////////////////////
  39. Shader "My Shaders/Sobel"
  40. {
  41.     Properties
  42.     {
  43.         _MainTex ("Buffer", 2D) = "white" {}
  44.         _Threshold ("Outline Threshold", Range (0, 1)) = 0
  45.         _ScreenWidth ("Screen Width", float) = 0.0
  46.         _ScreenHeight ("Screen Height", float) = 0.0
  47.     }
  48.     SubShader
  49.     {
  50.         Pass
  51.         {
  52.             CGPROGRAM
  53.             #pragma vertex vert_img
  54.             #pragma fragment frag
  55.  
  56.             #include "UnityCG.cginc"
  57.  
  58.             uniform sampler2D _MainTex;
  59.             uniform float _Threshold;
  60.             uniform float _ScreenWidth;
  61.             uniform float _ScreenHeight;
  62.  
  63.             float4 frag(v2f_img input) : COLOR
  64.             {
  65.                 float offsetX = 1.0 / _ScreenWidth;
  66.                 float offsetY = 1.0 / _ScreenHeight;
  67.  
  68.                 float4 result = float4(0.0, 0.0, 0.0, 0.0);
  69.                 float4 sumX = float4(0.0, 0.0, 0.0, 0.0);
  70.                 float4 sumY = float4(0.0, 0.0, 0.0, 0.0);
  71.                 float sum = 0.0;
  72.                 float4 temp = float4(0.0, 0.0, 0.0, 0.0);
  73.  
  74.                 temp = tex2D(_MainTex, input.uv + float2(-offsetX, -offsetY));
  75.                 sumX += temp * -3.0;
  76.  
  77.                 temp = tex2D(_MainTex, input.uv + float2(offsetX, -offsetY));
  78.                 sumX += temp * 3.0;
  79.  
  80.                 temp = tex2D(_MainTex, input.uv + float2(-offsetX, 0.0));
  81.                 sumX += temp * -10.0;
  82.  
  83.                 temp = tex2D(_MainTex, input.uv + float2(-offsetX, 0.0));
  84.                 sumX += temp * 10.0;
  85.  
  86.                 temp = tex2D(_MainTex, input.uv + float2(-offsetX, offsetY));
  87.                 sumX += temp * -3.0;
  88.  
  89.                 temp = tex2D(_MainTex, input.uv + float2(offsetX, offsetY));
  90.                 sumX += temp * 3.0;
  91.  
  92.  
  93.                 temp = tex2D(_MainTex, input.uv + float2(-offsetX, -offsetY));
  94.                 sumY += temp * 3.0;
  95.  
  96.                 temp = tex2D(_MainTex, input.uv + float2(0.0, -offsetY));
  97.                 sumY += temp * 10.0;
  98.  
  99.                 temp = tex2D(_MainTex, input.uv + float2(offsetX, -offsetY));
  100.                 sumY += temp * 3.0;
  101.  
  102.                 temp = tex2D(_MainTex, input.uv + float2(-offsetX, offsetY));
  103.                 sumY += temp * -3.0;
  104.  
  105.                 temp = tex2D(_MainTex, input.uv + float2(0.0, offsetY));
  106.                 sumY += temp * -10.0;
  107.  
  108.                 temp = tex2D(_MainTex, input.uv + float2(offsetX, offsetY));
  109.                 sumY += temp * -3.0;
  110.  
  111.                 sum = sqrt((sumX * sumX) + (sumY * sumY));
  112.  
  113.                 float4 textureMap = tex2D(_MainTex, input.uv);
  114.                
  115.                 //Result of the Sobel Filter.
  116. //              result = float4(sum, sum, sum, 1.0);
  117.  
  118.                 if(sum > _Threshold)
  119.                     result = float4(0.0, 0.0, 0.0, 1.0);
  120.                 else
  121.                     result = float4(textureMap.rgb, 1.0);
  122.  
  123.                 return result;
  124.             }
  125.             ENDCG
  126.         }
  127.     }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment