Skip to content

Instantly share code, notes, and snippets.

@StigOlavsen
Last active November 10, 2021 08:01
Show Gist options
  • Save StigOlavsen/c75c78df4e704aeb5f904b6c851748ff to your computer and use it in GitHub Desktop.
Save StigOlavsen/c75c78df4e704aeb5f904b6c851748ff to your computer and use it in GitHub Desktop.
Unity shader for LWRP and AR Foundation, renders as transparent with occlusion and shadows. Put this on AR planes to get shadows and occlusion for 3D objects.
Shader "AR Proxy"
{
Properties
{
}
SubShader
{
Tags
{
"RenderType" = "Transparent"
"RenderPipeline" = "LightweightPipeline"
"IgnoreProjector" = "True"
}
LOD 300
Pass
{
Name "AR Proxy"
Tags
{
"LightMode" = "LightweightForward"
}
Blend SrcAlpha OneMinusSrcAlpha
ZWrite On
Cull Off
HLSLPROGRAM
#pragma prefer_hlslcc gles
#pragma exclude_renderers d3d11_9x
#pragma target 2.0
// -------------------------------------
// Material Keywords
#pragma shader_feature _ALPHATEST_ON
#pragma shader_feature _ALPHAPREMULTIPLY_ON
// -------------------------------------
// Lightweight Pipeline keywords
#pragma multi_compile _ _MAIN_LIGHT_SHADOWS
#pragma multi_compile _ _MAIN_LIGHT_SHADOWS_CASCADE
#pragma multi_compile _ _ADDITIONAL_LIGHTS_VERTEX _ADDITIONAL_LIGHTS
#pragma multi_compile _ _ADDITIONAL_LIGHT_SHADOWS
#pragma multi_compile _ _SHADOWS_SOFT
// -------------------------------------
// Unity defined keywords
#pragma multi_compile _ DIRLIGHTMAP_COMBINED
#pragma multi_compile _ LIGHTMAP_ON
//--------------------------------------
// GPU Instancing
#pragma multi_compile_instancing
#pragma vertex HiddenVertex
#pragma fragment HiddenFragment
#include "Packages/com.unity.render-pipelines.lightweight/ShaderLibrary/Core.hlsl"
#include "Packages/com.unity.render-pipelines.lightweight/ShaderLibrary/Lighting.hlsl"
struct Attributes
{
UNITY_VERTEX_INPUT_INSTANCE_ID
float4 positionOS : POSITION;
};
struct Varyings
{
float4 positionCS : SV_POSITION;
float4 shadowCoord : TEXCOORD0;
};
Varyings HiddenVertex(Attributes input)
{
Varyings output = (Varyings)0;
UNITY_SETUP_INSTANCE_ID(input);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
output.shadowCoord = GetShadowCoord(vertexInput);
output.positionCS = vertexInput.positionCS;
return output;
}
half4 HiddenFragment(Varyings input) : SV_Target
{
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
half s = MainLightRealtimeShadow(input.shadowCoord);
return half4(0, 0, 0, 1 - s);
}
ENDHLSL
}
UsePass "Lightweight Render Pipeline/Lit/DepthOnly"
}
FallBack "Hidden/InternalErrorShader"
}
@leeprobert
Copy link

I have added this to a large cube to mask content that I want to stay below the ground until it moves up above the surface. Is this going to work? In my game view the cube is bright pink.
image

@StigOlavsen
Copy link
Author

In my game view the cube is bright pink.

This means the shader is not compiling correctly. It's been a long time since I made this, and I haven't updated it, so it will likely require some modifications to work in newer versions of Unity / URP. You can try to take a look at this fork that is supposedly updated for URP 7.2.1.

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