Skip to content

Instantly share code, notes, and snippets.

@NedMakesGames
Last active February 23, 2025 08:08
Show Gist options
  • Select an option

  • Save NedMakesGames/ce127e6bcad7e827ff9f4c22f04dc48f to your computer and use it in GitHub Desktop.

Select an option

Save NedMakesGames/ce127e6bcad7e827ff9f4c22f04dc48f to your computer and use it in GitHub Desktop.
// MIT License
// Copyright (c) 2020 NedMakesGames
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files(the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions :
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#ifndef SOBELOUTLINES_INCLUDED
#define SOBELOUTLINES_INCLUDED
// The sobel effect runs by sampling the texture around a point to see
// if there are any large changes. Each sample is multiplied by a convolution
// matrix weight for the x and y components seperately. Each value is then
// added together, and the final sobel value is the length of the resulting float2.
// Higher values mean the algorithm detected more of an edge
// These are points to sample relative to the starting point
static float2 sobelSamplePoints[9] = {
float2(-1, 1), float2(0, 1), float2(1, 1),
float2(-1, 0), float2(0, 0), float2(1, 0),
float2(-1, -1), float2(0, -1), float2(1, -1),
};
// Weights for the x component
static float sobelXMatrix[9] = {
1, 0, -1,
2, 0, -2,
1, 0, -1
};
// Weights for the y component
static float sobelYMatrix[9] = {
1, 2, 1,
0, 0, 0,
-1, -2, -1
};
// This function runs the sobel algorithm over the depth texture
void DepthSobel_float(float2 UV, float Thickness, out float Out) {
float2 sobel = 0;
// We can unroll this loop to make it more efficient
// The compiler is also smart enough to remove the i=4 iteration, which is always zero
[unroll] for (int i = 0; i < 9; i++) {
float depth = SHADERGRAPH_SAMPLE_SCENE_DEPTH(UV + sobelSamplePoints[i] * Thickness);
sobel += depth * float2(sobelXMatrix[i], sobelYMatrix[i]);
}
// Get the final sobel value
Out = length(sobel);
}
#endif
@Luk3Schro3d3r
Copy link

In newer versions the 'SHADERGRAPH_SAMPLE_SCENE_DEPTH' makes an error, but I dont know how to fix it, do you have a suggestion ?

@jakeb99
Copy link

jakeb99 commented Oct 10, 2024

@Luk3Schro3d3r What version are you using? I am using 2022.43f1 and am not getting any errors.

@MacSeem93
Copy link

MacSeem93 commented Feb 4, 2025

Hey, the error I'm getting on my end is the shadergraph's function being 'undeclared' as if I needed to declare it, which shouldn't be !

@aletoivonen
Copy link

aletoivonen commented Feb 22, 2025

Hey, the error I'm getting on my end is the shadergraph's function being 'undeclared' as if I needed to declare it, which shouldn't be !

#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/ShaderGraphFunctions.hlsl"

EDIT: Actually don't add that. Yes it fixes the error inside the IDE, but it breaks the node. For me the node works even if this file has an error in it. I'm on Unity 6.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment