ShadowSettings m_ShadowSettings = ShadowSettings . Default ;
ShadowRenderPass m_ShadowPass ;
const int MAX_LIGHTS = 1 0 ;
const int MAX_SHADOWMAP_PER_LIGHTS = 6 ;
const int MAX_DIRECTIONAL_SPLIT = 4 ;
const int k_MaxLights = 1 0 ;
const int k_MaxShadowmapPerLights = 6 ;
const int k_MaxDirectionalSplit = 4 ;
const float DIRECTIONAL_LIGHT_PULLBACK_DISTANCE = 1 0 0 0 0.0f ;
const float k_DirectionalLightPullbackDistance = 1 0 0 0 0.0f ;
[NonSerialized] private int m_nWarnedTooManyLights = 0 ;
[NonSerialized] private int m_WarnedTooManyLights = 0 ;
void OnEnable ( )
//---------------------------------------------------------------------------------------------------------------------------------------------------
void UpdateLightConstants ( VisibleLight [ ] visibleLights , ref ShadowOutput shadow )
{
int nN umLightsIncludingTooMany = 0 ;
var n umLightsIncludingTooMany = 0 ;
int g_nN umLights = 0 ;
var n umLights = 0 ;
Vector4 [ ] g_vL ightColor = new Vector4 [ MAX_LIGHTS ] ;
Vector4 [ ] g_vLightPosition_flI nvRadius = new Vector4 [ MAX_LIGHTS ] ;
Vector4 [ ] g_vL ightDirection = new Vector4 [ MAX_LIGHTS ] ;
Vector4 [ ] g_vLightShadowIndex_vL ightParams = new Vector4 [ MAX_LIGHTS ] ;
Vector4 [ ] g_vL ightFalloffParams = new Vector4 [ MAX_LIGHTS ] ;
Vector4 [ ] g_vS potLightInnerOuterConeCosines = new Vector4 [ MAX_LIGHTS ] ;
Matrix4x4 [ ] g_ matWorldToShadow = new Matrix4x4 [ MAX_LIGHTS * MAX_SHADOWMAP_PER_LIGHTS ] ;
Vector4 [ ] g_vD irShadowSplitSpheres = new Vector4 [ MAX_DIRECTIONAL_SPLIT ] ;
var l ightColor = new Vector4 [ k_MaxLights ] ;
var lightPosition_i nvRadius = new Vector4 [ k_MaxLights ] ;
var l ightDirection = new Vector4 [ k_MaxLights ] ;
var lightShadowIndex_l ightParams = new Vector4 [ k_MaxLights ] ;
var l ightFalloffParams = new Vector4 [ k_MaxLights ] ;
var s potLightInnerOuterConeCosines = new Vector4 [ k_MaxLights ] ;
var matWorldToShadow = new Matrix4x4 [ k_MaxLights * k_MaxShadowmapPerLights ] ;
var d irShadowSplitSpheres = new Vector4 [ k_MaxDirectionalSplit ] ;
for ( int nLight = 0 ; nLight < visibleLights . Length ; nLight + + )
for ( var nLight = 0 ; nLight < visibleLights . Length ; nLight + + )
nN umLightsIncludingTooMany + + ;
if ( nN umLightsIncludingTooMany > MAX_LIGHTS )
numLightsIncludingTooMany + + ;
if ( numLightsIncludingTooMany > k_MaxLights )
VisibleLight light = visibleLights [ nLight ] ;
LightType lightType = light . lightType ;
Vector3 position = light . light . transform . position ;
Vector3 lightDir = light . light . transform . forward . normalized ;
AdditionalLightData additionalLightData = light . light . GetComponent < AdditionalLightData > ( ) ;
var light = visibleLights [ nLight ] ;
var lightType = light . lightType ;
var position = light . light . transform . position ;
var lightDir = light . light . transform . forward . normalized ;
var additionalLightData = light . light . GetComponent < AdditionalLightData > ( ) ;
bool hasShadows = shadow . GetShadowSliceCountLightIndex ( nLight ) ! = 0 ;
var hasShadows = shadow . GetShadowSliceCountLightIndex ( nLight ) ! = 0 ;
g_vL ightColor[ g_ nN umLights] = light . finalColor ;
g_vLightPosition_flI nvRadius[ g_ nN umLights] = new Vector4 (
position . x - ( lightDir . x * DIRECTIONAL_LIGHT_PULLBACK_DISTANCE ) ,
position . y - ( lightDir . y * DIRECTIONAL_LIGHT_PULLBACK_DISTANCE ) ,
position . z - ( lightDir . z * DIRECTIONAL_LIGHT_PULLBACK_DISTANCE ) ,
l ightColor[ numLights ] = light . finalColor ;
lightPosition_i nvRadius[ numLights ] = new Vector4 (
position . x - ( lightDir . x * k_DirectionalLightPullbackDistance ) ,
position . y - ( lightDir . y * k_DirectionalLightPullbackDistance ) ,
position . z - ( lightDir . z * k_DirectionalLightPullbackDistance ) ,
g_vL ightDirection[ g_ nN umLights] = new Vector4 ( lightDir . x , lightDir . y , lightDir . z ) ;
g_vLightShadowIndex_vL ightParams[ g_ nN umLights] = new Vector4 ( 0 , 0 , 1 , 1 ) ;
g_vL ightFalloffParams[ g_ nN umLights] = new Vector4 ( 0.0f , 0.0f , float . MaxValue , ( float ) lightType ) ;
g_vS potLightInnerOuterConeCosines[ g_ nN umLights] = new Vector4 ( 0.0f , - 1.0f , 1.0f ) ;
l ightDirection[ numLights ] = new Vector4 ( lightDir . x , lightDir . y , lightDir . z ) ;
lightShadowIndex_l ightParams[ numLights ] = new Vector4 ( 0 , 0 , 1 , 1 ) ;
l ightFalloffParams[ numLights ] = new Vector4 ( 0.0f , 0.0f , float . MaxValue , ( float ) lightType ) ;
s potLightInnerOuterConeCosines[ numLights ] = new Vector4 ( 0.0f , - 1.0f , 1.0f ) ;
for ( int s = 0 ; s < MAX_DIRECTIONAL_SPLIT ; + + s )
for ( var s = 0 ; s < k_MaxDirectionalSplit ; + + s )
g_vD irShadowSplitSpheres[ s ] = shadow . directionalShadowSplitSphereSqr [ s ] ;
d irShadowSplitSpheres[ s ] = shadow . directionalShadowSplitSphereSqr [ s ] ;
g_vL ightColor[ g_ nN umLights] = light . finalColor ;
l ightColor[ numLights ] = light . finalColor ;
g_vLightPosition_flI nvRadius[ g_ nN umLights] = new Vector4 ( position . x , position . y , position . z , 1.0f / light . range ) ;
g_vL ightDirection[ g_ nN umLights] = new Vector4 ( 0.0f , 0.0f , 0.0f ) ;
g_vLightShadowIndex_vL ightParams[ g_ nN umLights] = new Vector4 ( 0 , 0 , 1 , 1 ) ;
g_vL ightFalloffParams[ g_ nN umLights] = new Vector4 ( 1.0f , 0.0f , light . range * light . range , ( float ) lightType ) ;
g_vS potLightInnerOuterConeCosines[ g_ nN umLights] = new Vector4 ( 0.0f , - 1.0f , 1.0f ) ;
lightPosition_i nvRadius[ numLights ] = new Vector4 ( position . x , position . y , position . z , 1.0f / light . range ) ;
l ightDirection[ numLights ] = new Vector4 ( 0.0f , 0.0f , 0.0f ) ;
lightShadowIndex_l ightParams[ numLights ] = new Vector4 ( 0 , 0 , 1 , 1 ) ;
l ightFalloffParams[ numLights ] = new Vector4 ( 1.0f , 0.0f , light . range * light . range , ( float ) lightType ) ;
s potLightInnerOuterConeCosines[ numLights ] = new Vector4 ( 0.0f , - 1.0f , 1.0f ) ;
g_vL ightColor[ g_ nN umLights] = light . finalColor ;
g_vLightPosition_flI nvRadius[ g_ nN umLights] = new Vector4 ( position . x , position . y , position . z , 1.0f / light . range ) ;
g_vL ightDirection[ g_ nN umLights] = new Vector4 ( lightDir . x , lightDir . y , lightDir . z ) ;
g_vLightShadowIndex_vL ightParams[ g_ nN umLights] = new Vector4 ( 0 , 0 , 1 , 1 ) ;
g_vL ightFalloffParams[ g_ nN umLights] = new Vector4 ( 1.0f , 0.0f , light . range * light . range , ( float ) lightType ) ;
l ightColor[ numLights ] = light . finalColor ;
lightPosition_i nvRadius[ numLights ] = new Vector4 ( position . x , position . y , position . z , 1.0f / light . range ) ;
l ightDirection[ numLights ] = new Vector4 ( lightDir . x , lightDir . y , lightDir . z ) ;
lightShadowIndex_l ightParams[ numLights ] = new Vector4 ( 0 , 0 , 1 , 1 ) ;
l ightFalloffParams[ numLights ] = new Vector4 ( 1.0f , 0.0f , light . range * light . range , ( float ) lightType ) ;
float flInnerConePercent = AdditionalLightData . GetInnerSpotPercent01 ( additionalLightData ) ;
float spotAngle = light . light . spotAngle ;
float flPhiDot = Mathf . Clamp ( Mathf . Cos ( spotAngle * 0.5f * Mathf . Deg2Rad ) , 0.0f , 1.0f ) ; // outer cone
float flThetaDot = Mathf . Clamp ( Mathf . Cos ( spotAngle * 0.5f * flInnerConePercent * Mathf . Deg2Rad ) , 0.0f , 1.0f ) ; // inner cone
g_vS potLightInnerOuterConeCosines[ g_ nN umLights] = new Vector4 ( flThetaDot , flPhiDot , 1.0f / Mathf . Max ( 0.01f , flThetaDot - flPhiDot ) ) ;
var flInnerConePercent = AdditionalLightData . GetInnerSpotPercent01 ( additionalLightData ) ;
var spotAngle = light . light . spotAngle ;
var flPhiDot = Mathf . Clamp ( Mathf . Cos ( spotAngle * 0.5f * Mathf . Deg2Rad ) , 0.0f , 1.0f ) ; // outer cone
var flThetaDot = Mathf . Clamp ( Mathf . Cos ( spotAngle * 0.5f * flInnerConePercent * Mathf . Deg2Rad ) , 0.0f , 1.0f ) ; // inner cone
s potLightInnerOuterConeCosines[ numLights ] = new Vector4 ( flThetaDot , flPhiDot , 1.0f / Mathf . Max ( 0.01f , flThetaDot - flPhiDot ) ) ;
g_vLightShadowIndex_vL ightParams[ g_ nN umLights] . x = 1 ;
for ( int s = 0 ; s < shadow . GetShadowSliceCountLightIndex ( nLight ) ; + + s )
lightShadowIndex_l ightParams[ numLights ] . x = 1 ;
for ( var s = 0 ; s < shadow . GetShadowSliceCountLightIndex ( nLight ) ; + + s )
int shadowSliceIndex = shadow . GetShadowSliceIndex ( nLight , s ) ;
g_ matWorldToShadow[ g_ nN umLights * MAX_SHADOWMAP_PER_LIGHTS + s ] = shadow . shadowSlices [ shadowSliceIndex ] . shadowTransform . transpose ;
var shadowSliceIndex = shadow . GetShadowSliceIndex ( nLight , s ) ;
matWorldToShadow [ numLights * k_MaxShadowmapPerLights + s ] = shadow . shadowSlices [ shadowSliceIndex ] . shadowTransform . transpose ;
g_ nN umLights+ + ;
numLights + + ;
if ( nN umLightsIncludingTooMany > MAX_LIGHTS )
if ( numLightsIncludingTooMany > k_MaxLights )
if ( nN umLightsIncludingTooMany > m_n WarnedTooManyLights )
if ( numLightsIncludingTooMany > m_WarnedTooManyLights )
Debug . LogError ( "ERROR! Found " + nN umLightsIncludingTooMany + " runtime lights! Valve renderer supports up to " + MAX_LIGHTS +
" active runtime lights at a time!\nDisabling " + ( nN umLightsIncludingTooMany - MAX_LIGHTS ) + " runtime light" +
( ( nN umLightsIncludingTooMany - MAX_LIGHTS ) > 1 ? "s" : "" ) + "!\n" ) ;
Debug . LogError ( "ERROR! Found " + numLightsIncludingTooMany + " runtime lights! Valve renderer supports up to " + k_MaxLights +
" active runtime lights at a time!\nDisabling " + ( numLightsIncludingTooMany - k_MaxLights ) + " runtime light" +
( ( numLightsIncludingTooMany - k_MaxLights ) > 1 ? "s" : "" ) + "!\n" ) ;
m_n WarnedTooManyLights = nN umLightsIncludingTooMany ;
m_WarnedTooManyLights = numLightsIncludingTooMany ;
if ( m_n WarnedTooManyLights > 0 )
if ( m_WarnedTooManyLights > 0 )
m_n WarnedTooManyLights = 0 ;
Debug . Log ( "SUCCESS! Found " + nN umLightsIncludingTooMany + " runtime lights which is within the supported number of lights, " + MAX_LIGHTS + ".\n\n" ) ;
m_WarnedTooManyLights = 0 ;
Debug . Log ( "SUCCESS! Found " + numLightsIncludingTooMany + " runtime lights which is within the supported number of lights, " + k_MaxLights + ".\n\n" ) ;
Shader . SetGlobalInt ( "g_nNumLights" , g_ nN umLights) ;
Shader . SetGlobalInt ( "g_nNumLights" , numLights ) ;
Shader . SetGlobalVectorArray ( "g_vLightPosition_flInvRadius" , g_vLightPosition_flI nvRadius) ;
Shader . SetGlobalVectorArray ( "g_vLightColor" , g_vL ightColor) ;
Shader . SetGlobalVectorArray ( "g_vLightDirection" , g_vL ightDirection) ;
Shader . SetGlobalVectorArray ( "g_vLightShadowIndex_vLightParams" , g_vLightShadowIndex_vL ightParams) ;
Shader . SetGlobalVectorArray ( "g_vLightFalloffParams" , g_vL ightFalloffParams) ;
Shader . SetGlobalVectorArray ( "g_vSpotLightInnerOuterConeCosines" , g_vS potLightInnerOuterConeCosines) ;
Shader . SetGlobalMatrixArray ( "g_matWorldToShadow" , g_ matWorldToShadow) ;
Shader . SetGlobalVectorArray ( "g_vDirShadowSplitSpheres" , g_vD irShadowSplitSpheres) ;
Shader . SetGlobalVectorArray ( "g_vLightPosition_flInvRadius" , lightPosition_i nvRadius) ;
Shader . SetGlobalVectorArray ( "g_vLightColor" , l ightColor) ;
Shader . SetGlobalVectorArray ( "g_vLightDirection" , l ightDirection) ;
Shader . SetGlobalVectorArray ( "g_vLightShadowIndex_vLightParams" , lightShadowIndex_l ightParams) ;
Shader . SetGlobalVectorArray ( "g_vLightFalloffParams" , l ightFalloffParams) ;
Shader . SetGlobalVectorArray ( "g_vSpotLightInnerOuterConeCosines" , s potLightInnerOuterConeCosines) ;
Shader . SetGlobalMatrixArray ( "g_matWorldToShadow" , matWorldToShadow ) ;
Shader . SetGlobalVectorArray ( "g_vDirShadowSplitSpheres" , d irShadowSplitSpheres) ;
// Time
#if (UNITY_EDITOR)
#endif
// PCF 3x3 Shadows
float flT exelEpsilonX = 1.0f / m_ShadowSettings . shadowAtlasWidth ;
float flT exelEpsilonY = 1.0f / m_ShadowSettings . shadowAtlasHeight ;
Vector4 g_vS hadow3x3PCFTerms0 = new Vector4 ( 2 0.0f / 2 6 7.0f , 3 3.0f / 2 6 7.0f , 5 5.0f / 2 6 7.0f , 0.0f ) ;
Vector4 g_vS hadow3x3PCFTerms1 = new Vector4 ( flTexelEpsilonX , flTexelEpsilonY , - flTexelEpsilonX , - flT exelEpsilonY) ;
Vector4 g_vS hadow3x3PCFTerms2 = new Vector4 ( flTexelEpsilonX , flT exelEpsilonY, 0.0f , 0.0f ) ;
Vector4 g_vS hadow3x3PCFTerms3 = new Vector4 ( - flTexelEpsilonX , - flT exelEpsilonY, 0.0f , 0.0f ) ;
var t exelEpsilonX = 1.0f / m_ShadowSettings . shadowAtlasWidth ;
var t exelEpsilonY = 1.0f / m_ShadowSettings . shadowAtlasHeight ;
var s hadow3x3PCFTerms0 = new Vector4 ( 2 0.0f / 2 6 7.0f , 3 3.0f / 2 6 7.0f , 5 5.0f / 2 6 7.0f , 0.0f ) ;
var s hadow3x3PCFTerms1 = new Vector4 ( texelEpsilonX , texelEpsilonY , - texelEpsilonX , - t exelEpsilonY) ;
var s hadow3x3PCFTerms2 = new Vector4 ( texelEpsilonX , t exelEpsilonY, 0.0f , 0.0f ) ;
var s hadow3x3PCFTerms3 = new Vector4 ( - texelEpsilonX , - t exelEpsilonY, 0.0f , 0.0f ) ;
Shader . SetGlobalVector ( "g_vShadow3x3PCFTerms0" , g_vS hadow3x3PCFTerms0) ;
Shader . SetGlobalVector ( "g_vShadow3x3PCFTerms1" , g_vS hadow3x3PCFTerms1) ;
Shader . SetGlobalVector ( "g_vShadow3x3PCFTerms2" , g_vS hadow3x3PCFTerms2) ;
Shader . SetGlobalVector ( "g_vShadow3x3PCFTerms3" , g_vS hadow3x3PCFTerms3) ;
Shader . SetGlobalVector ( "g_vShadow3x3PCFTerms0" , s hadow3x3PCFTerms0) ;
Shader . SetGlobalVector ( "g_vShadow3x3PCFTerms1" , s hadow3x3PCFTerms1) ;
Shader . SetGlobalVector ( "g_vShadow3x3PCFTerms2" , s hadow3x3PCFTerms2) ;
Shader . SetGlobalVector ( "g_vShadow3x3PCFTerms3" , s hadow3x3PCFTerms3) ;
}
public override void Render ( Camera [ ] cameras , RenderLoop renderLoop )
UpdateLightConstants ( cullResults . visibleLights , ref shadows ) ;
DrawRendererSettings settings = new DrawRendererSettings ( cullResults , camera , new ShaderPassName ( "ForwardBase" ) ) ;
var settings = new DrawRendererSettings ( cullResults , camera , new ShaderPassName ( "ForwardBase" ) ) ;
settings . rendererConfiguration = RendererConfiguration . PerObjectLightProbe | RendererConfiguration . PerObjectReflectionProbes ;
settings . sorting . sortOptions = SortOptions . SortByMaterialThenMesh ;
#if UNITY_EDITOR
public override UnityEditor . SupportedRenderingFeatures GetSupportedRenderingFeatures ( )
{
var features = new UnityEditor . SupportedRenderingFeatures ( ) ;
features . reflectionProbe = UnityEditor . SupportedRenderingFeatures . ReflectionProbe . Rotation ;
var features = new UnityEditor . SupportedRenderingFeatures
{
reflectionProbe = UnityEditor . SupportedRenderingFeatures . ReflectionProbe . Rotation
} ;
return features ;
}