You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

162 lines
4.7 KiB

#ifndef UNIVERSAL_SIMPLE_LIT_PASS_INCLUDED
#define UNIVERSAL_SIMPLE_LIT_PASS_INCLUDED
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
struct Attributes
{
float4 positionOS : POSITION;
float3 normalOS : NORMAL;
float4 tangentOS : TANGENT;
float2 texcoord : TEXCOORD0;
#ifdef _QLIGHTMAP_ON
float2 lightmapUV : TEXCOORD1;
#endif
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct Varyings
{
#ifdef _AFFINE_ON
noperspective
#endif
float2 uv : TEXCOORD0;
#ifdef _QLIGHTMAP_ON
float2 lightmapUV : TEXCOORD1;
#else
half3 vertexSH : TEXCOORD1;
#endif
float3 posWS : TEXCOORD2; // xyz: posWS
float3 normal : TEXCOORD3;
float3 viewDir : TEXCOORD4;
half4 fogFactorAndVertexLight : TEXCOORD6; // x: fogFactor, yzw: vertex light
float4 positionCS : SV_POSITION;
UNITY_VERTEX_INPUT_INSTANCE_ID
UNITY_VERTEX_OUTPUT_STEREO
};
void InitializeInputData(Varyings input, out InputData inputData)
{
inputData = (InputData)0;
inputData.positionWS = input.posWS;
half3 viewDirWS = input.viewDir;
inputData.normalWS = input.normal;
inputData.normalWS = NormalizeNormalPerPixel(inputData.normalWS);
viewDirWS = SafeNormalize(viewDirWS);
inputData.viewDirectionWS = viewDirWS;
inputData.shadowCoord = float4(0, 0, 0, 0);
inputData.fogCoord = input.fogFactorAndVertexLight.x;
inputData.vertexLighting = input.fogFactorAndVertexLight.yzw;
#ifdef _QLIGHTMAP_ON
inputData.bakedGI = 0.0;
#else
inputData.bakedGI = SampleSHPixel(input.vertexSH, inputData.normalWS);
#endif
inputData.normalizedScreenSpaceUV = GetNormalizedScreenSpaceUV(input.positionCS);
inputData.shadowMask = 0.0;
}
///////////////////////////////////////////////////////////////////////////////
// Vertex and Fragment functions //
///////////////////////////////////////////////////////////////////////////////
// Used in Standard (Simple Lighting) shader
Varyings LitPassVertexSimple(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);
VertexNormalInputs normalInput = GetVertexNormalInputs(input.normalOS, input.tangentOS);
half3 viewDirWS = GetWorldSpaceViewDir(vertexInput.positionWS);
half3 vertexLight = VertexLighting(vertexInput.positionWS, normalInput.normalWS);
half fogFactor = ComputeFogFactor(vertexInput.positionCS.z);
output.uv = TRANSFORM_TEX(input.texcoord, _BaseMap);
output.posWS.xyz = vertexInput.positionWS;
output.positionCS = vertexInput.positionCS;
output.normal = NormalizeNormalPerVertex(normalInput.normalWS);
output.viewDir = viewDirWS;
#ifdef _QLIGHTMAP_ON
output.lightmapUV.xy = input.lightmapUV.xy;
#else
output.vertexSH.xyz = SampleSHVertex(output.normal.xyz);
#endif
output.fogFactorAndVertexLight = half4(fogFactor, vertexLight);
return output;
}
// Used for StandardSimpleLighting shader
half4 LitPassFragmentSimple(Varyings input) : SV_Target
{
UNITY_SETUP_INSTANCE_ID(input);
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
float2 uv = input.uv.xy;
half4 diffuseAlpha = SampleAlbedoAlpha(uv,
#if _POINT_SAMPLING
TEXTURE2D_ARGS(_BaseMap, point_repeat_sampler)
#else
TEXTURE2D_ARGS(_BaseMap, sampler_BaseMap)
#endif
);
half3 diffuse = diffuseAlpha.rgb * _BaseColor.rgb;
half alpha = diffuseAlpha.a * _BaseColor.a;
AlphaDiscard(alpha, _Cutoff);
InputData inputData;
InitializeInputData(input, inputData);
#ifdef _QLIGHTMAP_ON
half4 lightmapColor = SAMPLE_TEXTURE2D(_LightMap, sampler_LightMap, input.lightmapUV);
half3 finalColor = diffuse * lightmapColor.rgb * 2.0f;
#else
// This reproduces anorm_dots within a reasonable degree of tolerance
half shade = dot(_ShadeVector.xyz, inputData.normalWS);
if (shade < 0.0)
shade = 1.0 + shade * (13.0 / 44.0);
else
shade = 1.0 + shade;
half3 finalColor = diffuse * shade * _LightColor.rgb;
#endif
#ifdef _EMISSION
finalColor += SampleEmission(uv, _EmissionColor.rgb,
#if _POINT_SAMPLING
TEXTURE2D_ARGS(_EmissionMap, point_repeat_sampler)
#else
TEXTURE2D_ARGS(_EmissionMap, sampler_EmissionMap)
#endif
);
#endif
half4 color = half4(finalColor, alpha);
color.rgb = MixFog(color.rgb, inputData.fogCoord);
color.a = OutputAlpha(color.a, _Surface);
return color;
}
#endif