123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- #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"
- // 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 lightmapUV : TEXCOORD1;
- UNITY_VERTEX_INPUT_INSTANCE_ID
- };
- struct Varyings
- {
- float2 uv : TEXCOORD0;
- DECLARE_LIGHTMAP_OR_SH(lightmapUV, vertexSH, 1);
- #if defined(REQUIRES_WORLD_SPACE_POS_INTERPOLATOR)
- float3 positionWS : TEXCOORD2;
- #endif
- float3 normalWS : TEXCOORD3;
- #if defined(REQUIRES_WORLD_SPACE_TANGENT_INTERPOLATOR)
- float4 tangentWS : TEXCOORD4; // xyz: tangent, w: sign
- #endif
- float3 viewDirWS : TEXCOORD5;
- half3 vertexLighting : TEXCOORD6; // xyz: vertex lighting
- #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
- float4 shadowCoord : TEXCOORD7;
- #endif
- #if defined(REQUIRES_TANGENT_SPACE_VIEW_DIR_INTERPOLATOR)
- float3 viewDirTS : TEXCOORD8;
- #endif
- float4 positionCS : SV_POSITION;
- UNITY_VERTEX_INPUT_INSTANCE_ID
- UNITY_VERTEX_OUTPUT_STEREO
- };
- void InitializeInputData(Varyings input, half3 normalTS, out InputData inputData)
- {
- inputData = (InputData)0;
- #if defined(REQUIRES_WORLD_SPACE_POS_INTERPOLATOR)
- inputData.positionWS = input.positionWS;
- #endif
- half3 viewDirWS = SafeNormalize(input.viewDirWS);
- #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
- inputData.vertexLighting = input.vertexLighting.xyz;
- inputData.bakedGI = SAMPLE_GI(input.lightmapUV, input.vertexSH, inputData.normalWS);
- inputData.normalizedScreenSpaceUV = GetNormalizedScreenSpaceUV(input.positionCS);
- inputData.shadowMask = SAMPLE_SHADOWMASK(input.lightmapUV);
- }
- ///////////////////////////////////////////////////////////////////////////////
- // 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);
- 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);
- half3 viewDirWS = GetWorldSpaceViewDir(vertexInput.positionWS);
- half3 vertexLight = VertexLighting(vertexInput.positionWS, normalInput.normalWS);
- output.uv = TRANSFORM_TEX(input.texcoord, _BaseMap);
- // already normalized from normal transform to WS.
- output.normalWS = normalInput.normalWS;
- output.viewDirWS = viewDirWS;
- #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 viewDirTS = GetViewDirectionTangentSpace(tangentWS, output.normalWS, viewDirWS);
- output.viewDirTS = viewDirTS;
- #endif
- OUTPUT_LIGHTMAP_UV(input.lightmapUV, unity_LightmapST, output.lightmapUV);
- OUTPUT_SH(output.normalWS.xyz, output.vertexSH);
- output.vertexLighting = vertexLight;
- #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;
- 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);
- #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;
- InitializeStandardLitSurfaceData(input.uv, surfaceData);
- InputData inputData;
- InitializeInputData(input, surfaceData.normalTS, inputData);
- // Stripped down version of UniversalFragmentPBR().
- #ifdef _SPECULARHIGHLIGHTS_OFF
- bool specularHighlightsOff = true;
- #else
- bool specularHighlightsOff = false;
- #endif
- // 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);
- half3 color = GlobalIllumination(brdfData, inputData.bakedGI, surfaceData.occlusion, inputData.normalWS, inputData.viewDirectionWS);
- return BRDFDataToGbuffer(brdfData, inputData, surfaceData.smoothness, surfaceData.emission + color);
- }
- #endif
|