Created
July 24, 2022 10:17
-
-
Save EricHu33/3e7fa8e4e872519839598b6c6bae9110 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Shader "URP/CustomLighting" | |
{ | |
Properties | |
{ | |
_BaseColor ("Base Color", Color) = (1, 1, 1, 1) | |
} | |
SubShader | |
{ | |
Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" "UniversalMaterialType" = "SimpleLit" "IgnoreProjector" = "True" "ShaderModel" = "4.5" } | |
LOD 300 | |
Pass | |
{ | |
Tags { "LightMode" = "UniversalForward" } | |
HLSLPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
#pragma prefer_hlslcc gles | |
#pragma exclude_renderers d3d11_9x | |
#pragma target 2.0 | |
// ------------------------------------- | |
// Universal Pipeline keywords | |
#pragma multi_compile _ _MAIN_LIGHT_SHADOWS | |
#pragma multi_compile _ _MAIN_LIGHT_SHADOWS_CASCADE | |
#pragma multi_compile _ _ADDITIONAL_LIGHTS | |
// ------------------------------------- | |
// Unity defined keywords | |
#pragma multi_compile _ DIRLIGHTMAP_COMBINED | |
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl" | |
CBUFFER_START(UnityPerMaterial) | |
float4 _BaseMap_ST; | |
float4 _BaseColor; | |
CBUFFER_END | |
struct Attributes | |
{ | |
float4 positionOS : POSITION; | |
float2 uv : TEXCOORD0; | |
float3 normalOS : NORMAL; | |
}; | |
struct Varyings | |
{ | |
float2 uv : TEXCOORD0; | |
float3 positionWS : TEXCOORD1; | |
float3 normalWS : TEXCOORD2; | |
float4 positionHCS : SV_POSITION; | |
}; | |
TEXTURE2D(_BaseMap); | |
SAMPLER(sampler_BaseMap); | |
Varyings vert(Attributes IN) | |
{ | |
Varyings OUT; | |
VertexPositionInputs positionInputs = GetVertexPositionInputs(IN.positionOS.xyz); | |
OUT.positionHCS = positionInputs.positionCS; | |
OUT.positionWS = positionInputs.positionWS; | |
VertexNormalInputs normalInputs = GetVertexNormalInputs(IN.normalOS); | |
OUT.normalWS = normalInputs.normalWS; | |
OUT.uv = TRANSFORM_TEX(IN.uv, _BaseMap); | |
return OUT; | |
} | |
float4 frag(Varyings IN) : SV_Target | |
{ | |
half3 normalWS = normalize(IN.normalWS); | |
half4 albedo = _BaseColor; | |
float4 shadowCoord = TransformWorldToShadowCoord(IN.positionWS); | |
Light mainLight = GetMainLight(shadowCoord); | |
float ndotl = saturate(dot(normalWS, mainLight.direction)); | |
float3 bakedGI = SampleSH(normalWS); | |
float3 lambert = ndotl * mainLight.color * mainLight.shadowAttenuation * mainLight.distanceAttenuation; | |
float3 diffuseColor = bakedGI + lambert; | |
#ifdef _ADDITIONAL_LIGHTS | |
uint pixelLightCount = GetAdditionalLightsCount(); | |
for (uint lightIndex = 0u; lightIndex < pixelLightCount; ++lightIndex) | |
{ | |
Light light = GetAdditionalLight(lightIndex, IN.positionWS); | |
half3 attenuatedLightColor = light.color * (light.distanceAttenuation * light.shadowAttenuation); | |
diffuseColor += saturate(dot(normalWS, light.direction)) * attenuatedLightColor; | |
} | |
#endif | |
float3 finalColor = diffuseColor * albedo; | |
return float4(finalColor, 1); | |
} | |
ENDHLSL | |
} | |
// Here I Only use `UsePass` for showcase the effect and redeuce duplicate code, this will break SRP batcher. you should always implement your own pass. | |
UsePass "Universal Render Pipeline/Simple Lit/ShadowCaster" | |
UsePass "Universal Render Pipeline/Simple Lit/DepthOnly" | |
UsePass "Universal Render Pipeline/Simple Lit/Meta" | |
UsePass "Universal Render Pipeline/Simple Lit/Universal2D" | |
} | |
Fallback "Hidden/Universal Render Pipeline/FallbackError" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment