Skip to content

Instantly share code, notes, and snippets.

@NedMakesGames
Last active January 23, 2021 16:04
Show Gist options
  • Save NedMakesGames/e71304ae5c9400bea80d38933eff9bac to your computer and use it in GitHub Desktop.
Save NedMakesGames/e71304ae5c9400bea80d38933eff9bac 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.
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
public class DepthNormalsFeature : ScriptableRendererFeature {
class RenderPass : ScriptableRenderPass {
private Material material;
private RenderTargetHandle destinationHandle;
private List<ShaderTagId> shaderTags;
private FilteringSettings filteringSettings;
public RenderPass(Material material) : base() {
this.material = material;
// This contains a list of shader tags. The renderer will only render objects with
// materials containing a shader with at least one tag in this list
this.shaderTags = new List<ShaderTagId>() {
new ShaderTagId("DepthOnly"),
//new ShaderTagId("SRPDefaultUnlit"),
//new ShaderTagId("UniversalForward"),
//new ShaderTagId("LightweightForward"),
};
// Render opaque materials
this.filteringSettings = new FilteringSettings(RenderQueueRange.opaque);
destinationHandle.Init("_DepthNormalsTexture");
}
// Configure the pass by creating a temporary render texture and
// readying it for rendering
public override void Configure(CommandBuffer cmd, RenderTextureDescriptor cameraTextureDescriptor) {
cmd.GetTemporaryRT(destinationHandle.id, cameraTextureDescriptor, FilterMode.Point);
ConfigureTarget(destinationHandle.Identifier());
ConfigureClear(ClearFlag.All, Color.black);
}
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData) {
// Create the draw settings, which configures a new draw call to the GPU
var drawSettings = CreateDrawingSettings(shaderTags, ref renderingData, renderingData.cameraData.defaultOpaqueSortFlags);
// We cant to render all objects using our material
drawSettings.overrideMaterial = material;
context.DrawRenderers(renderingData.cullResults, ref drawSettings, ref filteringSettings);
}
public override void FrameCleanup(CommandBuffer cmd) {
cmd.ReleaseTemporaryRT(destinationHandle.id);
}
}
private RenderPass renderPass;
public override void Create() {
// We will use the built-in renderer's depth normals texture shader
Material material = CoreUtils.CreateEngineMaterial("Hidden/Internal-DepthNormalsTexture");
this.renderPass = new RenderPass(material);
// Render after shadow caster, depth, etc. passes
renderPass.renderPassEvent = RenderPassEvent.AfterRenderingPrePasses;
}
public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData) {
renderer.EnqueuePass(renderPass);
}
}
// 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
#include "DecodeDepthNormals.hlsl"
TEXTURE2D(_DepthNormalsTexture); SAMPLER(sampler_DepthNormalsTexture);
// 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);
}
// This function runs the sobel algorithm over the opaque texture
void ColorSobel_float(float2 UV, float Thickness, out float Out) {
// We have to run the sobel algorithm over the RGB channels separately
float2 sobelR = 0;
float2 sobelG = 0;
float2 sobelB = 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++) {
// Sample the scene color texture
float3 rgb = SHADERGRAPH_SAMPLE_SCENE_COLOR(UV + sobelSamplePoints[i] * Thickness);
// Create the kernel for this iteration
float2 kernel = float2(sobelXMatrix[i], sobelYMatrix[i]);
// Accumulate samples for each color
sobelR += rgb.r * kernel;
sobelG += rgb.g * kernel;
sobelB += rgb.b * kernel;
}
// Get the final sobel value
// Combine the RGB values by taking the one with the largest sobel value
Out = max(length(sobelR), max(length(sobelG), length(sobelB)));
// This is an alternate way to combine the three sobel values by taking the average
// See which one you like better
//Out = (length(sobelR) + length(sobelG) + length(sobelB)) / 3.0;
}
// Sample the depth normal map and decode depth and normal from the texture
void GetDepthAndNormal(float2 uv, out float depth, out float3 normal) {
float4 coded = SAMPLE_TEXTURE2D(_DepthNormalsTexture, sampler_DepthNormalsTexture, uv);
DecodeDepthNormal(coded, depth, normal);
}
// A wrapper around the above function for use in a custom function node
void CalculateDepthNormal_float(float2 UV, out float Depth, out float3 Normal) {
GetDepthAndNormal(UV, Depth, Normal);
// Normals are encoded from 0 to 1 in the texture. Remap them to -1 to 1 for easier use in the graph
Normal = Normal * 2 - 1;
}
// This function runs the sobel algorithm over the opaque texture
void NormalsSobel_float(float2 UV, float Thickness, out float Out) {
// We have to run the sobel algorithm over the XYZ channels separately, like color
float2 sobelX = 0;
float2 sobelY = 0;
float2 sobelZ = 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;
float3 normal;
GetDepthAndNormal(UV + sobelSamplePoints[i] * Thickness, depth, normal);
// Create the kernel for this iteration
float2 kernel = float2(sobelXMatrix[i], sobelYMatrix[i]);
// Accumulate samples for each coordinate
sobelX += normal.x * kernel;
sobelY += normal.y * kernel;
sobelZ += normal.z * kernel;
}
// Get the final sobel value
// Combine the XYZ values by taking the one with the largest sobel value
Out = max(length(sobelX), max(length(sobelY), length(sobelZ)));
}
void DepthAndNormalsSobel_float(float2 UV, float Thickness, out float OutDepth, out float OutNormal) {
// This function calculates the normal and depth sobels at the same time
// using the depth encoded into the depth normals texture
float2 sobelX = 0;
float2 sobelY = 0;
float2 sobelZ = 0;
float2 sobelDepth = 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;
float3 normal;
GetDepthAndNormal(UV + sobelSamplePoints[i] * Thickness, depth, normal);
// Create the kernel for this iteration
float2 kernel = float2(sobelXMatrix[i], sobelYMatrix[i]);
// Accumulate samples for each channel
sobelX += normal.x * kernel;
sobelY += normal.y * kernel;
sobelZ += normal.z * kernel;
sobelDepth += depth * kernel;
}
// Get the final sobel values by taking the maximum
OutDepth = length(sobelDepth);
OutNormal = max(length(sobelX), max(length(sobelY), length(sobelZ)));
}
void ViewDirectionFromScreenUV_float(float2 In, out float3 Out) {
// Code by Keijiro Takahashi @_kzr and Ben Golus @bgolus
// Get the perspective projection
float2 p11_22 = float2(unity_CameraProjection._11, unity_CameraProjection._22);
// Convert the uvs into view space by "undoing" projection
Out = -normalize(float3((In * 2 - 1) / p11_22, -1));
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment