286 lines
10 KiB
HLSL
286 lines
10 KiB
HLSL
#ifndef UNIVERSAL_LIT_GBUFFER_PASS_INCLUDED
|
|
#define UNIVERSAL_LIT_GBUFFER_PASS_INCLUDED
|
|
|
|
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
|
|
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/UnityGBuffer.hlsl"
|
|
|
|
|
|
//Advanced Dissolve
|
|
#ifndef REQUIRES_WORLD_SPACE_POS_INTERPOLATOR //Need world space vertex position
|
|
#define REQUIRES_WORLD_SPACE_POS_INTERPOLATOR
|
|
#endif
|
|
|
|
#include "AdvancedDissolve_InitializeStandardLitSurfaceData.hlsl"
|
|
|
|
|
|
|
|
|
|
// TODO: Currently we support viewDirTS caclulated in vertex shader and in fragments shader.
|
|
// As both solutions have their advantages and disadvantages (etc. shader target 2.0 has only 8 interpolators).
|
|
// We need to find out if we can stick to one solution, which we needs testing.
|
|
// So keeping this until I get manaul QA pass.
|
|
#if defined(_PARALLAXMAP) && (SHADER_TARGET >= 30)
|
|
#define REQUIRES_TANGENT_SPACE_VIEW_DIR_INTERPOLATOR
|
|
#endif
|
|
|
|
#if (defined(_NORMALMAP) || (defined(_PARALLAXMAP) && !defined(REQUIRES_TANGENT_SPACE_VIEW_DIR_INTERPOLATOR))) || defined(_DETAIL)
|
|
#define REQUIRES_WORLD_SPACE_TANGENT_INTERPOLATOR
|
|
#endif
|
|
|
|
// keep this file in sync with LitForwardPass.hlsl
|
|
|
|
struct Attributes
|
|
{
|
|
float4 positionOS : POSITION;
|
|
float3 normalOS : NORMAL;
|
|
float4 tangentOS : TANGENT;
|
|
float2 texcoord : TEXCOORD0;
|
|
float2 staticLightmapUV : TEXCOORD1;
|
|
float2 dynamicLightmapUV : TEXCOORD2;
|
|
UNITY_VERTEX_INPUT_INSTANCE_ID
|
|
};
|
|
|
|
struct Varyings
|
|
{
|
|
float2 uv : TEXCOORD0;
|
|
|
|
#if defined(REQUIRES_WORLD_SPACE_POS_INTERPOLATOR)
|
|
float3 positionWS : TEXCOORD1;
|
|
#endif
|
|
|
|
half3 normalWS : TEXCOORD2;
|
|
#if defined(REQUIRES_WORLD_SPACE_TANGENT_INTERPOLATOR)
|
|
half4 tangentWS : TEXCOORD3; // xyz: tangent, w: sign
|
|
#endif
|
|
#ifdef _ADDITIONAL_LIGHTS_VERTEX
|
|
half3 vertexLighting : TEXCOORD4; // xyz: vertex lighting
|
|
#endif
|
|
|
|
#if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
|
|
float4 shadowCoord : TEXCOORD5;
|
|
#endif
|
|
|
|
#if defined(REQUIRES_TANGENT_SPACE_VIEW_DIR_INTERPOLATOR)
|
|
half3 viewDirTS : TEXCOORD6;
|
|
#endif
|
|
|
|
DECLARE_LIGHTMAP_OR_SH(staticLightmapUV, vertexSH, 7);
|
|
#ifdef DYNAMICLIGHTMAP_ON
|
|
float2 dynamicLightmapUV : TEXCOORD8; // Dynamic lightmap UVs
|
|
#endif
|
|
|
|
float4 positionCS : SV_POSITION;
|
|
UNITY_VERTEX_INPUT_INSTANCE_ID
|
|
UNITY_VERTEX_OUTPUT_STEREO
|
|
|
|
|
|
//Advanced Dissolve
|
|
ADVANCED_DISSOLVE_UV(9)
|
|
|
|
};
|
|
|
|
void InitializeInputData(Varyings input, half3 normalTS, out InputData inputData)
|
|
{
|
|
inputData = (InputData)0;
|
|
|
|
#if defined(REQUIRES_WORLD_SPACE_POS_INTERPOLATOR)
|
|
inputData.positionWS = input.positionWS;
|
|
#endif
|
|
|
|
inputData.positionCS = input.positionCS;
|
|
half3 viewDirWS = GetWorldSpaceNormalizeViewDir(input.positionWS);
|
|
#if defined(_NORMALMAP) || defined(_DETAIL)
|
|
float sgn = input.tangentWS.w; // should be either +1 or -1
|
|
float3 bitangent = sgn * cross(input.normalWS.xyz, input.tangentWS.xyz);
|
|
inputData.normalWS = TransformTangentToWorld(normalTS, half3x3(input.tangentWS.xyz, bitangent.xyz, input.normalWS.xyz));
|
|
#else
|
|
inputData.normalWS = input.normalWS;
|
|
#endif
|
|
|
|
inputData.normalWS = NormalizeNormalPerPixel(inputData.normalWS);
|
|
inputData.viewDirectionWS = viewDirWS;
|
|
|
|
#if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
|
|
inputData.shadowCoord = input.shadowCoord;
|
|
#elif defined(MAIN_LIGHT_CALCULATE_SHADOWS)
|
|
inputData.shadowCoord = TransformWorldToShadowCoord(inputData.positionWS);
|
|
#else
|
|
inputData.shadowCoord = float4(0, 0, 0, 0);
|
|
#endif
|
|
|
|
inputData.fogCoord = 0.0; // we don't apply fog in the guffer pass
|
|
|
|
#ifdef _ADDITIONAL_LIGHTS_VERTEX
|
|
inputData.vertexLighting = input.vertexLighting.xyz;
|
|
#else
|
|
inputData.vertexLighting = half3(0, 0, 0);
|
|
#endif
|
|
|
|
#if defined(DYNAMICLIGHTMAP_ON)
|
|
inputData.bakedGI = SAMPLE_GI(input.staticLightmapUV, input.dynamicLightmapUV, input.vertexSH, inputData.normalWS);
|
|
#else
|
|
inputData.bakedGI = SAMPLE_GI(input.staticLightmapUV, input.vertexSH, inputData.normalWS);
|
|
#endif
|
|
|
|
inputData.normalizedScreenSpaceUV = GetNormalizedScreenSpaceUV(input.positionCS);
|
|
inputData.shadowMask = SAMPLE_SHADOWMASK(input.staticLightmapUV);
|
|
}
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
// Vertex and Fragment functions //
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Used in Standard (Physically Based) shader
|
|
Varyings LitGBufferPassVertex(Attributes input)
|
|
{
|
|
Varyings output = (Varyings)0;
|
|
|
|
UNITY_SETUP_INSTANCE_ID(input);
|
|
UNITY_TRANSFER_INSTANCE_ID(input, output);
|
|
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
|
|
|
|
|
|
#if defined(CURVEDWORLD_IS_INSTALLED) && !defined(CURVEDWORLD_DISABLED_ON)
|
|
#ifdef CURVEDWORLD_NORMAL_TRANSFORMATION_ON
|
|
CURVEDWORLD_TRANSFORM_VERTEX_AND_NORMAL(input.positionOS, input.normalOS, input.tangentOS)
|
|
#else
|
|
CURVEDWORLD_TRANSFORM_VERTEX(input.positionOS)
|
|
#endif
|
|
#endif
|
|
|
|
|
|
VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
|
|
|
|
// normalWS and tangentWS already normalize.
|
|
// this is required to avoid skewing the direction during interpolation
|
|
// also required for per-vertex lighting and SH evaluation
|
|
VertexNormalInputs normalInput = GetVertexNormalInputs(input.normalOS, input.tangentOS);
|
|
|
|
output.uv = TRANSFORM_TEX(input.texcoord, _BaseMap);
|
|
|
|
// already normalized from normal transform to WS.
|
|
output.normalWS = normalInput.normalWS;
|
|
|
|
#if defined(REQUIRES_WORLD_SPACE_TANGENT_INTERPOLATOR) || defined(REQUIRES_TANGENT_SPACE_VIEW_DIR_INTERPOLATOR)
|
|
real sign = input.tangentOS.w * GetOddNegativeScale();
|
|
half4 tangentWS = half4(normalInput.tangentWS.xyz, sign);
|
|
#endif
|
|
|
|
#if defined(REQUIRES_WORLD_SPACE_TANGENT_INTERPOLATOR)
|
|
output.tangentWS = tangentWS;
|
|
#endif
|
|
|
|
#if defined(REQUIRES_TANGENT_SPACE_VIEW_DIR_INTERPOLATOR)
|
|
half3 viewDirWS = GetWorldSpaceNormalizeViewDir(vertexInput.positionWS);
|
|
half3 viewDirTS = GetViewDirectionTangentSpace(tangentWS, output.normalWS, viewDirWS);
|
|
output.viewDirTS = viewDirTS;
|
|
#endif
|
|
|
|
OUTPUT_LIGHTMAP_UV(input.staticLightmapUV, unity_LightmapST, output.staticLightmapUV);
|
|
#ifdef DYNAMICLIGHTMAP_ON
|
|
output.dynamicLightmapUV = input.dynamicLightmapUV.xy * unity_DynamicLightmapST.xy + unity_DynamicLightmapST.zw;
|
|
#endif
|
|
OUTPUT_SH(output.normalWS.xyz, output.vertexSH);
|
|
|
|
#ifdef _ADDITIONAL_LIGHTS_VERTEX
|
|
half3 vertexLight = VertexLighting(vertexInput.positionWS, normalInput.normalWS);
|
|
output.vertexLighting = vertexLight;
|
|
#endif
|
|
|
|
#if defined(REQUIRES_WORLD_SPACE_POS_INTERPOLATOR)
|
|
output.positionWS = vertexInput.positionWS;
|
|
#endif
|
|
|
|
#if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
|
|
output.shadowCoord = GetShadowCoord(vertexInput);
|
|
#endif
|
|
|
|
output.positionCS = vertexInput.positionCS;
|
|
|
|
|
|
//Advanced Dissolve
|
|
ADVANCED_DISSOLVE_INIT_UV(output, input.texcoord, output.positionCS)
|
|
|
|
|
|
return output;
|
|
}
|
|
|
|
// Used in Standard (Physically Based) shader
|
|
FragmentOutput LitGBufferPassFragment(Varyings input)
|
|
{
|
|
UNITY_SETUP_INSTANCE_ID(input);
|
|
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
|
|
|
|
|
|
//Advanced Dissolve////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
#if defined(_AD_STATE_ENABLED)
|
|
|
|
float4 dissolveBase = 0;
|
|
#if defined(_AD_CUTOUT_STANDARD_SOURCE_BASE_ALPHA) || defined(_AD_EDGE_ADDITIONAL_COLOR_BASE_COLOR)
|
|
dissolveBase = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, input.uv);
|
|
dissolveBase.rgb *= _BaseColor.rgb;
|
|
#endif
|
|
|
|
ADVANCED_DISSOLVE_SETUP_CUTOUT_SOURCE_USING_WS(input, dissolveBase, input.positionWS.xyz, input.normalWS.xyz)
|
|
|
|
#if !defined(_ALPHATEST_ON)
|
|
AdvancedDissolveClip(cutoutSource);
|
|
#endif
|
|
|
|
float3 dissolveAlbedo = 0;
|
|
float3 dissolveEmission = 0;
|
|
float dissolveBlend = AdvancedDissolveAlbedoEmission(cutoutSource, dissolveBase, dissolveAlbedo, dissolveEmission, input.uv);
|
|
|
|
#endif
|
|
//Advanced Dissolve/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#if defined(_PARALLAXMAP)
|
|
#if defined(REQUIRES_TANGENT_SPACE_VIEW_DIR_INTERPOLATOR)
|
|
half3 viewDirTS = input.viewDirTS;
|
|
#else
|
|
half3 viewDirTS = GetViewDirectionTangentSpace(input.tangentWS, input.normalWS, input.viewDirWS);
|
|
#endif
|
|
ApplyPerPixelDisplacement(viewDirTS, input.uv);
|
|
#endif
|
|
|
|
SurfaceData surfaceData;
|
|
#if defined(_AD_STATE_ENABLED) && defined(_ALPHATEST_ON)
|
|
AdvancedDissolve_InitializeStandardLitSurfaceData(input.uv, surfaceData, cutoutSource);
|
|
#else
|
|
InitializeStandardLitSurfaceData(input.uv, surfaceData);
|
|
#endif
|
|
|
|
InputData inputData;
|
|
InitializeInputData(input, surfaceData.normalTS, inputData);
|
|
SETUP_DEBUG_TEXTURE_DATA(inputData, input.uv, _BaseMap);
|
|
|
|
#ifdef _DBUFFER
|
|
ApplyDecalToSurfaceData(input.positionCS, surfaceData, inputData);
|
|
#endif
|
|
|
|
// Stripped down version of UniversalFragmentPBR().
|
|
|
|
// in LitForwardPass GlobalIllumination (and temporarily LightingPhysicallyBased) are called inside UniversalFragmentPBR
|
|
// in Deferred rendering we store the sum of these values (and of emission as well) in the GBuffer
|
|
BRDFData brdfData;
|
|
InitializeBRDFData(surfaceData.albedo, surfaceData.metallic, surfaceData.specular, surfaceData.smoothness, surfaceData.alpha, brdfData);
|
|
|
|
|
|
//Advanced Dissolve/////////////////////////////////////////
|
|
#if defined(_AD_STATE_ENABLED)
|
|
surfaceData.albedo = lerp(surfaceData.albedo, dissolveAlbedo, dissolveBlend);
|
|
surfaceData.emission = lerp(surfaceData.emission, dissolveEmission, dissolveBlend);
|
|
#endif
|
|
|
|
|
|
Light mainLight = GetMainLight(inputData.shadowCoord, inputData.positionWS, inputData.shadowMask);
|
|
MixRealtimeAndBakedGI(mainLight, inputData.normalWS, inputData.bakedGI, inputData.shadowMask);
|
|
half3 color = GlobalIllumination(brdfData, inputData.bakedGI, surfaceData.occlusion, inputData.positionWS, inputData.normalWS, inputData.viewDirectionWS);
|
|
|
|
return BRDFDataToGbuffer(brdfData, inputData, surfaceData.smoothness, surfaceData.emission + color, surfaceData.occlusion);
|
|
}
|
|
|
|
#endif
|