Skip to content

Instantly share code, notes, and snippets.

@HugoLnx
Created May 11, 2024 19:51
Show Gist options
  • Save HugoLnx/c6c4e9cba790520af9633f2370e37ae5 to your computer and use it in GitHub Desktop.
Save HugoLnx/c6c4e9cba790520af9633f2370e37ae5 to your computer and use it in GitHub Desktop.
// Created based on the shadergraph for full screen shaders
// Read more: https://docs.unity3d.com/Packages/com.unity.render-pipelines.universal@16.0/manual/post-processing/post-processing-custom-effect-low-code.html
// This shader can be used in the place of the FullScreen Shader Graph
// Add your logic to "TODO: Add custom logic here"
Shader "CustomTemplate/FullscreenShader"
{
Properties
{
// [HideInInspector][NoScaleOffset]unity_Lightmaps("unity_Lightmaps", 2DArray) = "" {}
// [HideInInspector][NoScaleOffset]unity_LightmapsInd("unity_LightmapsInd", 2DArray) = "" {}
// [HideInInspector][NoScaleOffset]unity_ShadowMasks("unity_ShadowMasks", 2DArray) = "" {}
}
SubShader
{
Tags
{
"RenderPipeline"="UniversalPipeline"
}
Pass
{
Name "FullscreenEffectPass"
// Render State
Cull Off
Blend Off
ZTest Off
ZWrite Off
// --------------------------------------------------
// Pass
HLSLPROGRAM
// Pragmas
#pragma target 3.0
#pragma vertex vert
#pragma fragment frag
// #pragma enable_d3d11_debug_symbols
// Defines
#define ATTRIBUTES_NEED_TEXCOORD0
#define ATTRIBUTES_NEED_TEXCOORD1
#define ATTRIBUTES_NEED_VERTEXID
#define VARYINGS_NEED_TEXCOORD0
#define VARYINGS_NEED_TEXCOORD1
// Includes
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
// --------------------------------------------------
// Structs and Packing
struct Attributes
{
#if UNITY_ANY_INSTANCING_ENABLED || defined(ATTRIBUTES_NEED_INSTANCEID)
uint instanceID : INSTANCEID_SEMANTIC;
#endif
uint vertexID : VERTEXID_SEMANTIC;
};
struct SurfaceDescriptionInputs
{
float2 NDCPosition;
};
struct Varyings
{
float4 positionCS : SV_POSITION;
float4 texCoord0;
float4 texCoord1;
#if UNITY_ANY_INSTANCING_ENABLED || defined(VARYINGS_NEED_INSTANCEID)
uint instanceID : CUSTOM_INSTANCE_ID;
#endif
#if (defined(UNITY_STEREO_MULTIVIEW_ENABLED)) || (defined(UNITY_STEREO_INSTANCING_ENABLED) && (defined(SHADER_API_GLES3) || defined(SHADER_API_GLCORE)))
uint stereoTargetEyeIndexAsBlendIdx0 : BLENDINDICES0;
#endif
#if (defined(UNITY_STEREO_INSTANCING_ENABLED))
uint stereoTargetEyeIndexAsRTArrayIdx : SV_RenderTargetArrayIndex;
#endif
};
struct VertexDescriptionInputs
{
};
struct PackedVaryings
{
float4 positionCS : SV_POSITION;
float4 texCoord0 : INTERP0;
float4 texCoord1 : INTERP1;
#if UNITY_ANY_INSTANCING_ENABLED || defined(VARYINGS_NEED_INSTANCEID)
uint instanceID : CUSTOM_INSTANCE_ID;
#endif
#if (defined(UNITY_STEREO_MULTIVIEW_ENABLED)) || (defined(UNITY_STEREO_INSTANCING_ENABLED) && (defined(SHADER_API_GLES3) || defined(SHADER_API_GLCORE)))
uint stereoTargetEyeIndexAsBlendIdx0 : BLENDINDICES0;
#endif
#if (defined(UNITY_STEREO_INSTANCING_ENABLED))
uint stereoTargetEyeIndexAsRTArrayIdx : SV_RenderTargetArrayIndex;
#endif
};
PackedVaryings PackVaryings (Varyings input)
{
PackedVaryings output;
ZERO_INITIALIZE(PackedVaryings, output);
output.positionCS = input.positionCS;
output.texCoord0.xyzw = input.texCoord0;
output.texCoord1.xyzw = input.texCoord1;
#if UNITY_ANY_INSTANCING_ENABLED || defined(VARYINGS_NEED_INSTANCEID)
output.instanceID = input.instanceID;
#endif
#if (defined(UNITY_STEREO_MULTIVIEW_ENABLED)) || (defined(UNITY_STEREO_INSTANCING_ENABLED) && (defined(SHADER_API_GLES3) || defined(SHADER_API_GLCORE)))
output.stereoTargetEyeIndexAsBlendIdx0 = input.stereoTargetEyeIndexAsBlendIdx0;
#endif
#if (defined(UNITY_STEREO_INSTANCING_ENABLED))
output.stereoTargetEyeIndexAsRTArrayIdx = input.stereoTargetEyeIndexAsRTArrayIdx;
#endif
return output;
}
Varyings UnpackVaryings (PackedVaryings input)
{
Varyings output;
output.positionCS = input.positionCS;
output.texCoord0 = input.texCoord0.xyzw;
output.texCoord1 = input.texCoord1.xyzw;
#if UNITY_ANY_INSTANCING_ENABLED || defined(VARYINGS_NEED_INSTANCEID)
output.instanceID = input.instanceID;
#endif
#if (defined(UNITY_STEREO_MULTIVIEW_ENABLED)) || (defined(UNITY_STEREO_INSTANCING_ENABLED) && (defined(SHADER_API_GLES3) || defined(SHADER_API_GLCORE)))
output.stereoTargetEyeIndexAsBlendIdx0 = input.stereoTargetEyeIndexAsBlendIdx0;
#endif
#if (defined(UNITY_STEREO_INSTANCING_ENABLED))
output.stereoTargetEyeIndexAsRTArrayIdx = input.stereoTargetEyeIndexAsRTArrayIdx;
#endif
return output;
}
// --------------------------------------------------
// Graph
// Graph Properties
CBUFFER_START(UnityPerMaterial)
// UNITY_TEXTURE_STREAMING_DEBUG_VARS;
CBUFFER_END
float _FlipY;
TEXTURE2D_X(_BlitTexture);
float4 URP_SampleBuffer_GetScreenTexture(float2 uv)
{
uint2 pixelCoords = uint2(uv * _ScreenSize.xy);
return LOAD_TEXTURE2D_X_LOD(_BlitTexture, pixelCoords, 0);
}
// Graph Pixel
struct SurfaceDescription
{
float3 BaseColor;
float Alpha;
};
SurfaceDescription SurfaceDescriptionFunction(SurfaceDescriptionInputs IN)
{
SurfaceDescription surface = (SurfaceDescription)0;
float2 uv = IN.NDCPosition.xy;
float4 col = URP_SampleBuffer_GetScreenTexture(uv);
// TODO: Add custom logic here
surface.BaseColor = col.rgb + 0.5;
surface.Alpha = float(1);
return surface;
}
SurfaceDescriptionInputs BuildSurfaceDescriptionInputs(Varyings input)
{
SurfaceDescriptionInputs output;
ZERO_INITIALIZE(SurfaceDescriptionInputs, output);
output.NDCPosition = input.texCoord0.xy;
return output;
}
// --------------------------------------------------
// Main
#include "Packages/com.unity.shadergraph/Editor/Generation/Targets/Fullscreen/Includes/FullscreenCommon.hlsl"
#include "Packages/com.unity.shadergraph/Editor/Generation/Targets/Fullscreen/Includes/FullscreenDrawProcedural.hlsl"
ENDHLSL
}
}
CustomEditor "UnityEditor.Rendering.Fullscreen.ShaderGraph.FullscreenShaderGUI"
FallBack "Hidden/Shader Graph/FallbackError"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment