148 lines
4.6 KiB
HLSL
148 lines
4.6 KiB
HLSL
#ifndef LIGHTWEIGHT_FORWARD_LIT_PASS_INCLUDED
|
|
#define LIGHTWEIGHT_FORWARD_LIT_PASS_INCLUDED
|
|
|
|
#include "Lighting.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);
|
|
|
|
#ifdef _ADDITIONAL_LIGHTS
|
|
float3 positionWS : TEXCOORD2;
|
|
#endif
|
|
|
|
#ifdef _NORMALMAP
|
|
half4 normalWS : TEXCOORD3; // xyz: normal, w: viewDir.x
|
|
half4 tangentWS : TEXCOORD4; // xyz: tangent, w: viewDir.y
|
|
half4 bitangentWS : TEXCOORD5; // xyz: bitangent, w: viewDir.z
|
|
#else
|
|
half3 normalWS : TEXCOORD3;
|
|
half3 viewDirWS : TEXCOORD4;
|
|
#endif
|
|
|
|
half4 fogFactorAndVertexLight : TEXCOORD6; // x: fogFactor, yzw: vertex light
|
|
|
|
#ifdef _MAIN_LIGHT_SHADOWS
|
|
float4 shadowCoord : TEXCOORD7;
|
|
#endif
|
|
|
|
float4 positionCS : SV_POSITION;
|
|
UNITY_VERTEX_INPUT_INSTANCE_ID
|
|
UNITY_VERTEX_OUTPUT_STEREO
|
|
};
|
|
|
|
void InitializeInputData(Varyings input, SurfaceDataAdvanced surfaceData, out InputDataAdvanced inputData)
|
|
{
|
|
inputData = (InputDataAdvanced)0;
|
|
|
|
#ifdef _ADDITIONAL_LIGHTS
|
|
inputData.positionWS = input.positionWS;
|
|
#endif
|
|
|
|
#ifdef _NORMALMAP
|
|
half3 viewDirWS = half3(input.normalWS.w, input.tangentWS.w, input.bitangentWS.w);
|
|
inputData.normalWS = TransformTangentToWorld(surfaceData.normalTS,
|
|
half3x3(input.tangentWS.xyz, input.bitangentWS.xyz, input.normalWS.xyz));
|
|
#else
|
|
half3 viewDirWS = input.viewDirWS;
|
|
inputData.normalWS = input.normalWS;
|
|
#endif
|
|
inputData.normalWS = NormalizeNormalPerPixel(inputData.normalWS);
|
|
|
|
#if SHADER_HINT_NICE_QUALITY
|
|
viewDirWS = SafeNormalize(viewDirWS);
|
|
#endif
|
|
|
|
inputData.viewDirectionWS = viewDirWS;
|
|
#if defined(_MAIN_LIGHT_SHADOWS) && !defined(_RECEIVE_SHADOWS_OFF)
|
|
inputData.shadowCoord = input.shadowCoord;
|
|
#else
|
|
inputData.shadowCoord = float4(0, 0, 0, 0);
|
|
#endif
|
|
inputData.fogCoord = input.fogFactorAndVertexLight.x;
|
|
inputData.vertexLighting = input.fogFactorAndVertexLight.yzw;
|
|
inputData.bakedGI = SAMPLE_GI(input.lightmapUV, input.vertexSH, inputData.normalWS);
|
|
}
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
// Vertex and Fragment functions //
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Used in Standard (Physically Based) shader
|
|
Varyings LitPassVertex(Attributes input)
|
|
{
|
|
Varyings output = (Varyings)0;
|
|
|
|
UNITY_SETUP_INSTANCE_ID(input);
|
|
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
|
|
|
|
VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
|
|
VertexNormalInputs normalInput = GetVertexNormalInputs(input.normalOS, input.tangentOS);
|
|
half3 viewDirWS = GetCameraPositionWS() - vertexInput.positionWS;
|
|
|
|
#if !SHADER_HINT_NICE_QUALITY
|
|
viewDirWS = SafeNormalize(viewDirWS);
|
|
#endif
|
|
|
|
half3 vertexLight = VertexLighting(vertexInput.positionWS, normalInput.normalWS);
|
|
half fogFactor = ComputeFogFactor(vertexInput.positionCS.z);
|
|
|
|
output.uv = TRANSFORM_TEX(input.texcoord, _BaseMap);
|
|
|
|
#ifdef _NORMALMAP
|
|
output.normalWS = half4(normalInput.normalWS, viewDirWS.x);
|
|
output.tangentWS = half4(normalInput.tangentWS, viewDirWS.y);
|
|
output.bitangentWS = half4(normalInput.bitangentWS, viewDirWS.z);
|
|
#else
|
|
output.normalWS = normalInput.normalWS;
|
|
output.viewDirWS = viewDirWS;
|
|
#endif
|
|
|
|
OUTPUT_LIGHTMAP_UV(input.lightmapUV, unity_LightmapST, output.lightmapUV);
|
|
OUTPUT_SH(output.normalWS.xyz, output.vertexSH);
|
|
|
|
output.fogFactorAndVertexLight = half4(fogFactor, vertexLight);
|
|
|
|
#ifdef _ADDITIONAL_LIGHTS
|
|
output.positionWS = vertexInput.positionWS;
|
|
#endif
|
|
|
|
#if defined(_MAIN_LIGHT_SHADOWS) && !defined(_RECEIVE_SHADOWS_OFF)
|
|
output.shadowCoord = GetShadowCoord(vertexInput);
|
|
#endif
|
|
|
|
output.positionCS = vertexInput.positionCS;
|
|
|
|
return output;
|
|
}
|
|
|
|
// Used in Standard (Physically Based) shader
|
|
half4 LitPassFragment(Varyings input) : SV_Target
|
|
{
|
|
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
|
|
|
|
SurfaceDataAdvanced surfaceData;
|
|
InitializeStandardLitSurfaceData(input.uv, surfaceData);
|
|
|
|
InputDataAdvanced inputData;
|
|
InitializeInputData(input, surfaceData, inputData);
|
|
|
|
half4 color = LightweightFragmentAdvanced(inputData, surfaceData);
|
|
|
|
color.rgb = MixFog(color.rgb, inputData.fogCoord);
|
|
return color;
|
|
}
|
|
|
|
#endif
|