Skip to content

Instantly share code, notes, and snippets.

@antonsem
Forked from StigOlavsen/arproxy.shader
Last active April 3, 2024 20:54
Show Gist options
  • Save antonsem/b294fa7b7443e017599ee7a87a22b8b4 to your computer and use it in GitHub Desktop.
Save antonsem/b294fa7b7443e017599ee7a87a22b8b4 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" = "UniversalPipeline"
"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.universal/ShaderLibrary/Core.hlsl"
#include "Packages/com.unity.render-pipelines.universal/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 "Universal Render Pipeline/Lit/DepthOnly"
}
FallBack "Hidden/InternalErrorShader"
}
@antonsem
Copy link
Author

Updated for Universal Render Pipeline 7.2.1

@kirkokuev
Copy link

Does not work with URP 7.2.1 Gives pink color.

@dilmerv
Copy link

dilmerv commented Jul 17, 2020

It works for me but it has a gray color instead of completely transparent.

@kirkokuev
Copy link

kirkokuev commented Jul 17, 2020 via email

@ACWesterberg
Copy link

Render Queue Transparency at 3000 does make the material transparent, yes. But it also nullifies the Occlusion for me, I can see the object completely.
Anyone found a solution for this?

I figured out that it works only with URP installed and enabled. For transparency set Render Queue 3000

On Fri, Jul 17, 2020, 20:24 Dilmer Valecillos @.> wrote: @.* commented on this gist. ------------------------------ It works for me but it has a gray color instead of completely transparent. — You are receiving this because you commented. Reply to this email directly, view it on GitHub https://gist.github.com/b294fa7b7443e017599ee7a87a22b8b4#gistcomment-3381495, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAVQ46ZKGB4BFSZLKV2GLN3R4CCNLANCNFSM4KXCMCJQ .

@soorya-ha
Copy link

soorya-ha commented Mar 13, 2021

shader not working properly. Does anyone have the same issue?
I'm using unity 2020.1.7f,
and tried with URP - 8.x, 9.x & 10.x
ezgif com-gif-maker (1)

Any help would be much appreciated.

@Hoppenbrouwers
Copy link

Thank you for this! Works like a charm here. 2020.3.1f1 (LTS) with URP 10.3.2. using it together with AR Foundation PlaneOcclusionShader to render shadows on the ground. Really awesome work dude!

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