Skip to content

Commit

Permalink
fix(Runtime): Update MovementLitMetaPass and MovementShadowCasterPass…
Browse files Browse the repository at this point in the history
… to be compatible with multiple URP versions

Summary: Update MovementLitMetaPass and MovementShadowCasterPass to be compatible with URP version 10.x.x and 12.x.x.

Reviewed By: sohailshafiiWk

Differential Revision: D44472146

fbshipit-source-id: 48d5adba6f5553d6e8d00ed3415302183350fb79
  • Loading branch information
Andrew Kim authored and facebook-github-bot committed Mar 29, 2023
1 parent 96cd2d9 commit 35bfbc1
Show file tree
Hide file tree
Showing 16 changed files with 369 additions and 101 deletions.
21 changes: 21 additions & 0 deletions Editor/MovementPBRShaderGUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using System;
using UnityEngine;
using UnityEngine.Rendering;

namespace UnityEditor
{
Expand Down Expand Up @@ -519,6 +520,8 @@ public static void SetupMaterialWithBlendMode(Material material, BlendMode blend
material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
material.SetInt("_ZWrite", 1);
material.SetShaderPassEnabled("ShadowCaster", true);
material.DisableKeyword("_SURFACE_TYPE_TRANSPARENT");
material.DisableKeyword("_ALPHATEST_ON");
material.DisableKeyword("_ALPHABLEND_ON");
material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
Expand All @@ -528,6 +531,12 @@ public static void SetupMaterialWithBlendMode(Material material, BlendMode blend
material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
material.SetInt("_ZWrite", 1);
// Transparent objects in URP should not cast shadows.
if (GraphicsSettings.renderPipelineAsset != null)
{
material.SetShaderPassEnabled("ShadowCaster", false);
}
material.EnableKeyword("_SURFACE_TYPE_TRANSPARENT");
material.EnableKeyword("_ALPHATEST_ON");
material.DisableKeyword("_ALPHABLEND_ON");
material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
Expand All @@ -537,6 +546,12 @@ public static void SetupMaterialWithBlendMode(Material material, BlendMode blend
material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
material.SetInt("_ZWrite", 0);
// Transparent objects in URP should not cast shadows.
if (GraphicsSettings.renderPipelineAsset != null)
{
material.SetShaderPassEnabled("ShadowCaster", false);
}
material.EnableKeyword("_SURFACE_TYPE_TRANSPARENT");
material.DisableKeyword("_ALPHATEST_ON");
material.EnableKeyword("_ALPHABLEND_ON");
material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
Expand All @@ -546,6 +561,12 @@ public static void SetupMaterialWithBlendMode(Material material, BlendMode blend
material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
material.SetInt("_ZWrite", 0);
// Transparent objects in URP should not cast shadows.
if (GraphicsSettings.renderPipelineAsset != null)
{
material.SetShaderPassEnabled("ShadowCaster", false);
}
material.EnableKeyword("_SURFACE_TYPE_TRANSPARENT");
material.DisableKeyword("_ALPHATEST_ON");
material.DisableKeyword("_ALPHABLEND_ON");
material.EnableKeyword("_ALPHAPREMULTIPLY_ON");
Expand Down
28 changes: 28 additions & 0 deletions Runtime/Shaders/MovementCommon.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,34 @@ TEXTURE2D(_MetallicGlossMap); SAMPLER(sampler_MetallicGlossMap);
TEXTURE2D(_SpecGlossMap); SAMPLER(sampler_SpecGlossMap);
TEXTURE2D(_MainTex); SAMPLER(sampler_MainTex);

half4 SampleMetallicSpecGloss(float2 uv, half albedoAlpha)
{
half4 specGloss;

#ifdef _METALLICGLOSSMAP
specGloss = half4(SAMPLE_METALLICSPECULAR(uv));
#ifdef _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
specGloss.a = albedoAlpha * _Smoothness;
#else
specGloss.a *= _Smoothness;
#endif
#else
#if _SPECGLOSSMAP
specGloss.rgb = _SpecColor.rgb;
#else
specGloss.rgb = _Metallic.rrr;
#endif

#ifdef _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
specGloss.a = albedoAlpha * _Glossiness;
#else
specGloss.a = _Glossiness;
#endif
#endif

return specGloss;
}

half4 ComputeSpecularGloss(float2 uv)
{
half4 sg;
Expand Down
12 changes: 12 additions & 0 deletions Runtime/Shaders/MovementPBRMetallic.shader
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ Shader "Movement/PBR (Metallic)"
#pragma shader_feature _EMISSION
#pragma shader_feature_local _NORMALMAP

#pragma shader_feature_local_fragment _SURFACE_TYPE_TRANSPARENT
#pragma shader_feature_local_fragment _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
#pragma shader_feature_local_fragment _METALLICGLOSSMAP
#pragma shader_feature_local_fragment _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
Expand Down Expand Up @@ -182,12 +183,19 @@ Shader "Movement/PBR (Metallic)"
#pragma shader_feature_local _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
#pragma shader_feature_local _METALLICGLOSSMAP

// This is used during shadow map generation to differentiate between directional and punctual light shadows, as they use different formulas to apply Normal Bias
#pragma multi_compile_vertex _ _CASTING_PUNCTUAL_LIGHT_SHADOW

#pragma multi_compile_instancing

#pragma vertex ShadowPassVertex
#pragma fragment ShadowPassFragment

#if UNITY_VERSION >= 202120
#include "Packages/com.meta.movement/Runtime/ThirdParty/Unity/MovementShadowCasterPass.hlsl"
#else
#include "Packages/com.meta.movement/Runtime/ThirdParty/Unity/MovementShadowCasterPass2020.hlsl"
#endif
ENDHLSL
}

Expand Down Expand Up @@ -222,7 +230,11 @@ Shader "Movement/PBR (Metallic)"
#pragma fragment UniversalFragmentMeta
#endif

#if UNITY_VERSION >= 202120
#include "Packages/com.meta.movement/Runtime/ThirdParty/Unity/MovementLitMetaPass.hlsl"
#else
#include "Packages/com.meta.movement/Runtime/ThirdParty/Unity/MovementLitMetaPass2020.hlsl"
#endif
ENDHLSL
}
}
Expand Down
16 changes: 15 additions & 1 deletion Runtime/Shaders/MovementPBRSpecular.shader
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ Shader "Movement/PBR (Specular)"
_SpecColor("Specular", Color) = (0.2,0.2,0.2)
_SpecGlossMap("Specular", 2D) = "white" {}

[HideInInspector] _Metallic("Metallic", Range(0.0, 1.0)) = 0.0

[ToggleOff] _SpecularHighlights("Specular Highlights", Float) = 1.0
[ToggleOff] _GlossyReflections("Glossy Reflections", Float) = 1.0

Expand Down Expand Up @@ -118,6 +120,7 @@ Shader "Movement/PBR (Specular)"
#pragma shader_feature _EMISSION
#pragma shader_feature_local _NORMALMAP

#pragma shader_feature_local_fragment _SURFACE_TYPE_TRANSPARENT
#pragma shader_feature_local_fragment _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
#pragma shader_feature_local_fragment _SPECGLOSSMAP
#pragma shader_feature_local_fragment _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
Expand Down Expand Up @@ -177,16 +180,23 @@ Shader "Movement/PBR (Specular)"
#pragma shader_feature_local _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
#pragma shader_feature_local _SPECGLOSSMAP

// This is used during shadow map generation to differentiate between directional and punctual light shadows, as they use different formulas to apply Normal Bias
#pragma multi_compile_vertex _ _CASTING_PUNCTUAL_LIGHT_SHADOW

#pragma multi_compile_instancing

#pragma vertex ShadowPassVertex
#pragma fragment ShadowPassFragment

#if UNITY_VERSION >= 202120
#include "Packages/com.meta.movement/Runtime/ThirdParty/Unity/MovementShadowCasterPass.hlsl"
#else
#include "Packages/com.meta.movement/Runtime/ThirdParty/Unity/MovementShadowCasterPass2020.hlsl"
#endif
ENDHLSL
}

// ------------------------------------------------------------------
// ------------------------------------------------------------------
// Extracts information for lightmapping, GI (emission, albedo, ...)
// This pass is not used during regular rendering.
Pass
Expand Down Expand Up @@ -217,7 +227,11 @@ Shader "Movement/PBR (Specular)"
#pragma fragment UniversalFragmentMeta
#endif

#if UNITY_VERSION >= 202120
#include "Packages/com.meta.movement/Runtime/ThirdParty/Unity/MovementLitMetaPass.hlsl"
#else
#include "Packages/com.meta.movement/Runtime/ThirdParty/Unity/MovementLitMetaPass2020.hlsl"
#endif
ENDHLSL
}
}
Expand Down
98 changes: 35 additions & 63 deletions Runtime/ThirdParty/Unity/MovementLitMetaPass.hlsl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Based on com.unity.render-pipelines.universal/Shaders/LitMetaPass.hlsl
// Based on com.unity.render-pipelines.universal/Shaders/LitMetaPass.hlsl and
// com.unity.render-pipelines.universal/Shaders/LitInput.hlsl
#ifndef UNIVERSAL_LIT_META_PASS_INCLUDED
#define UNIVERSAL_LIT_META_PASS_INCLUDED

Expand All @@ -12,89 +13,60 @@ struct Attributes
float2 uv0 : TEXCOORD0;
float2 uv1 : TEXCOORD1;
float2 uv2 : TEXCOORD2;
#ifdef _TANGENT_TO_WORLD
float4 tangentOS : TANGENT;
#endif
UNITY_VERTEX_INPUT_INSTANCE_ID
};

struct Varyings
{
float4 positionCS : SV_POSITION;
float2 uv : TEXCOORD0;
#ifdef EDITOR_VISUALIZATION
float2 VizUV : TEXCOORD1;
float4 LightCoord : TEXCOORD2;
#endif
};

Varyings UniversalVertexMeta(Attributes input)
{
Varyings output;
output.positionCS = MetaVertexPosition(input.positionOS, input.uv1, input.uv2, unity_LightmapST, unity_DynamicLightmapST);
Varyings output = (Varyings)0;
output.positionCS = UnityMetaVertexPosition(input.positionOS.xyz, input.uv1, input.uv2);
output.uv = TRANSFORM_TEX(input.uv0, _MainTex);
#ifdef EDITOR_VISUALIZATION
UnityEditorVizData(input.positionOS.xyz, input.uv0, input.uv1, input.uv2, output.VizUV, output.LightCoord);
#endif
return output;
}

void InitializeStandardLitSurfaceData(float2 uv, out SurfaceData outSurfaceData)
half4 UniversalFragmentMeta(Varyings fragIn, MetaInput metaInput)
{
half4 albedoAlpha = SampleAlbedoAlpha(uv, TEXTURE2D_ARGS(_BaseMap, sampler_BaseMap));
outSurfaceData.alpha = Alpha(albedoAlpha.a, _Color, _Cutoff);

half4 specGloss = ComputeSpecularGloss(uv);
outSurfaceData.albedo = albedoAlpha.rgb * _Color.rgb;

#if _SPECULAR_SETUP
outSurfaceData.metallic = 1.0h;
outSurfaceData.specular = specGloss.rgb;
#else
outSurfaceData.metallic = specGloss.r;
outSurfaceData.specular = half3(0.0h, 0.0h, 0.0h);
#ifdef EDITOR_VISUALIZATION
metaInput.VizUV = fragIn.VizUV;
metaInput.LightCoord = fragIn.LightCoord;
#endif

outSurfaceData.smoothness = specGloss.a;
outSurfaceData.normalTS = SampleNormal(uv, TEXTURE2D_ARGS(_BumpMap, sampler_BumpMap), _BumpScale);
outSurfaceData.occlusion = GetAmbientOcclusion(uv);
outSurfaceData.emission = SampleEmission(uv, _EmissionColor.rgb, TEXTURE2D_ARGS(_EmissionMap, sampler_EmissionMap));

#if defined(_CLEARCOAT) || defined(_CLEARCOATMAP)
half2 clearCoat = SampleClearCoat(uv);
outSurfaceData.clearCoatMask = clearCoat.r;
outSurfaceData.clearCoatSmoothness = clearCoat.g;
#else
outSurfaceData.clearCoatMask = 0.0h;
outSurfaceData.clearCoatSmoothness = 0.0h;
#endif

#if defined(_DETAIL)
half detailMask = SAMPLE_TEXTURE2D(_DetailMask, sampler_DetailMask, uv).a;
float2 detailUv = uv * _DetailAlbedoMap_ST.xy + _DetailAlbedoMap_ST.zw;
outSurfaceData.albedo = ApplyDetailAlbedo(detailUv, outSurfaceData.albedo, detailMask);
outSurfaceData.normalTS = ApplyDetailNormal(detailUv, outSurfaceData.normalTS, detailMask);

#endif
return UnityMetaFragment(metaInput);
}

half4 UniversalFragmentMeta(Varyings input) : SV_Target
half4 UniversalFragmentMetaLit(Varyings input) : SV_Target
{
SurfaceData surfaceData;
InitializeStandardLitSurfaceData(input.uv, surfaceData);

BRDFData brdfData;
InitializeBRDFData(surfaceData.albedo, surfaceData.metallic, surfaceData.specular, surfaceData.smoothness, surfaceData.alpha, brdfData);
half4 albedoAlpha = half4(SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, input.uv));
half4 specGloss = SampleMetallicSpecGloss(input.uv, albedoAlpha.a);
half3 diffuse;
half3 specular;
const half perceptualRoughness = PerceptualSmoothnessToPerceptualRoughness(specGloss.a);
const half roughness = PerceptualRoughnessToRoughness(perceptualRoughness);
#if _SPECULAR_SETUP
diffuse = half(1.0);
specular = specGloss.rgb;
#else
diffuse = specGloss.r;
specular = half3(0.0, 0.0, 0.0);
#endif

MetaInput metaInput;
metaInput.Albedo = brdfData.diffuse + brdfData.specular * brdfData.roughness * 0.5;
metaInput.SpecularColor = surfaceData.specular;
metaInput.Emission = surfaceData.emission;

return MetaFragment(metaInput);
}

//LWRP -> Universal Backwards Compatibility
Varyings LightweightVertexMeta(Attributes input)
{
return UniversalVertexMeta(input);
}

half4 LightweightFragmentMeta(Varyings input) : SV_Target
{
return UniversalFragmentMeta(input);
metaInput.Albedo = diffuse + specular * roughness * 0.5;
metaInput.Emission = _EmissionStrength *
SAMPLE_TEXTURE2D(_EmissionMap, sampler_EmissionMap, input.uv).rgb * _EmissionColor.rgb;
return UniversalFragmentMeta(input, metaInput);
}

#endif
2 changes: 1 addition & 1 deletion Runtime/ThirdParty/Unity/MovementLitMetaPass.hlsl.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 35bfbc1

Please sign in to comment.