romanilyin

EdgeDetectionOutlinesInclude

Jan 28th, 2025
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.47 KB | Gaming | 0 0
  1. // MIT License
  2.  
  3. #ifndef SOBELOUTLINES_INCLUDED
  4. #define SOBELOUTLINES_INCLUDED
  5.  
  6. // The sobel effect runs by sampling the texture around a point to see
  7. // if there are any large changes. Each sample is multiplied by a convolution
  8. // matrix weight for the x and y components seperately. Each value is then
  9. // added together, and the final sobel value is the length of the resulting float2.
  10. // Higher values mean the algorithm detected more of an edge
  11.  
  12. // These are points to sample relative to the starting point
  13. static float2 sobelSamplePoints[9] = {
  14.     float2(-1, 1), float2(0, 1), float2(1, 1),
  15.     float2(-1, 0), float2(0, 0), float2(1, 0),
  16.     float2(-1, -1), float2(0, -1), float2(1, -1),
  17. };
  18.  
  19. // Weights for the x component
  20. static float sobelXMatrix[9] = {
  21.     1, 0, -1,
  22.     2, 0, -2,
  23.     1, 0, -1
  24. };
  25.  
  26. // Weights for the y component
  27. static float sobelYMatrix[9] = {
  28.     1, 2, 1,
  29.     0, 0, 0,
  30.     -1, -2, -1
  31. };
  32.  
  33. // This function runs the sobel algorithm over the depth texture
  34. void DepthSobel_float(float2 UV, float Thickness, out float Out) {
  35.     float2 sobel = 0;
  36.     // We can unroll this loop to make it more efficient
  37.     // The compiler is also smart enough to remove the i=4 iteration, which is always zero
  38.     [unroll] for (int i = 0; i < 9; i++) {
  39.         float depth = SHADERGRAPH_SAMPLE_SCENE_DEPTH(UV + sobelSamplePoints[i] * Thickness);
  40.         sobel += depth * float2(sobelXMatrix[i], sobelYMatrix[i]);
  41.     }
  42.     // Get the final sobel value
  43.     Out = length(sobel);
  44. }
  45.  
  46. TEXTURE2D(_CameraOpaqueTexture);
  47. SAMPLER(sampler_CameraOpaqueTexture);
  48.  
  49. // This function runs the sobel algorithm over the opaque texture
  50. void ColorSobel_float(float2 UV, float Thickness, out float Out) {
  51.     // We have to run the sobel algorithm over the RGB channels separately
  52.     float2 sobelR = 0;
  53.     float2 sobelG = 0;
  54.     float2 sobelB = 0;
  55.     // We can unroll this loop to make it more efficient
  56.     // The compiler is also smart enough to remove the i=4 iteration, which is always zero
  57.     [unroll] for (int i = 0; i < 9; i++) {
  58.         // Sample the scene color texture
  59.         //float3 rgb = SHADERGRAPH_SAMPLE_SCENE_COLOR(UV + sobelSamplePoints[i] * Thickness);
  60.         float2 uv = UV + sobelSamplePoints[i] * Thickness;
  61.         float3 rgb = SAMPLE_TEXTURE2D(_CameraOpaqueTexture, sampler_CameraOpaqueTexture, uv).rgb;
  62.         // Create the kernel for this iteration
  63.         float2 kernel = float2(sobelXMatrix[i], sobelYMatrix[i]);
  64.         // Accumulate samples for each color
  65.         sobelR += rgb.r * kernel;
  66.         sobelG += rgb.g * kernel;
  67.         sobelB += rgb.b * kernel;
  68.         //sobelR = (rgb.r - rgb.b)*1000;
  69.     }
  70.     // Get the final sobel value
  71.     // Combine the RGB values by taking the one with the largest sobel value
  72.     Out = max(length(sobelR), max(length(sobelG), length(sobelB)));
  73.     // This is an alternate way to combine the three sobel values by taking the average
  74.     // See which one you like better
  75.     //Out = (length(sobelR) + length(sobelG) + length(sobelB)) / 3.0;
  76. }
  77.  
  78. //SAMPLER(sampler_MainTex);
  79.  
  80.  
  81. void ColorSobelMainTex_float(Texture2D<float4> tex, SamplerState ss, float2 UV, float Thickness, out float Out) {
  82.     // We have to run the sobel algorithm over the RGB channels separately
  83.     float2 sobelR = 0;
  84.     float2 sobelG = 0;
  85.     float2 sobelB = 0;
  86.     // We can unroll this loop to make it more efficient
  87.     // The compiler is also smart enough to remove the i=4 iteration, which is always zero
  88.     [unroll] for (int i = 0; i < 9; i++) {
  89.         // Sample the scene color texture
  90.         float2 uv = UV + sobelSamplePoints[i] * Thickness;
  91.         float3 rgb = SAMPLE_TEXTURE2D(tex, ss, uv).rgb;
  92.         // Create the kernel for this iteration
  93.         float2 kernel = float2(sobelXMatrix[i], sobelYMatrix[i]);
  94.         // Accumulate samples for each color
  95.         sobelR += rgb.r * kernel;
  96.         sobelG += rgb.g * kernel;
  97.         sobelB += rgb.b * kernel;
  98.     }
  99.     // Get the final sobel value
  100.     // Combine the RGB values by taking the one with the largest sobel value
  101.     Out = max(length(sobelR), max(length(sobelG), length(sobelB)));
  102.     Out = clamp(Out, 0, 1);
  103.     // This is an alternate way to combine the three sobel values by taking the average
  104.     // See which one you like better
  105.     //Out = (length(sobelR) + length(sobelG) + length(sobelB)) / 3.0;
  106.    
  107. }
  108.  
  109.  
  110. void ColorTest_float(float2 UV, float Thickness, out float Out) {
  111.    
  112.     float3 rgb = SHADERGRAPH_SAMPLE_SCENE_NORMAL(UV);
  113.     Out = max(rgb.r, max(rgb.g, rgb.b));
  114. }
  115.  
  116. #endif
Advertisement
Add Comment
Please, Sign In to add comment