@@ -137,6 +137,7 @@ public void BindBuffers(Material mat)
137137 public const int k_MaxAreaLightsOnSCreen = 128 ;
138138 public const int k_MaxEnvLightsOnSCreen = 64 ;
139139 public const int k_MaxShadowOnScreen = 16 ;
140+ public const int k_MaxCascadeCount = 4 ; //Should be not less than m_Settings.directionalLightCascadeCount;
140141
141142 [ SerializeField ]
142143 TextureSettings m_TextureSettings = TextureSettings . Default ;
@@ -157,12 +158,26 @@ public void BindBuffers(Material mat)
157158 public class LightList
158159 {
159160 public List < DirectionalLightData > directionalLights ;
161+ public List < DirectionalShadowData > directionalShadows ;
160162 public List < LightData > punctualLights ;
163+ public List < PunctualShadowData > punctualShadows ;
161164 public List < LightData > areaLights ;
162165 public List < EnvLightData > envLights ;
163- public List < PunctualShadowData > punctualShadows ;
166+ public Vector4 [ ] directionalShadowSplitSphereSqr ;
167+
168+ public void Clear ( )
169+ {
170+ directionalLights . Clear ( ) ;
171+ directionalShadows . Clear ( ) ;
172+ punctualLights . Clear ( ) ;
173+ punctualShadows . Clear ( ) ;
174+ areaLights . Clear ( ) ;
175+ envLights . Clear ( ) ;
176+ }
164177 }
165178
179+ LightList m_lightList ;
180+
166181 // Detect when windows size is changing
167182 int m_WidthOnRecord ;
168183 int m_HeightOnRecord ;
@@ -254,6 +269,15 @@ public override void Rebuild()
254269 m_SinglePassLightLoop . Rebuild ( ) ;
255270 // m_TilePassLightLoop = new TilePass.LightLoop();
256271 // m_TilePassLightLoop.Rebuild();
272+
273+ m_lightList = new LightList ( ) ;
274+ m_lightList . directionalLights = new List < DirectionalLightData > ( ) ;
275+ m_lightList . punctualLights = new List < LightData > ( ) ;
276+ m_lightList . areaLights = new List < LightData > ( ) ;
277+ m_lightList . envLights = new List < EnvLightData > ( ) ;
278+ m_lightList . punctualShadows = new List < PunctualShadowData > ( ) ;
279+ m_lightList . directionalShadows = new List < DirectionalShadowData > ( ) ;
280+ m_lightList . directionalShadowSplitSphereSqr = new Vector4 [ k_MaxCascadeCount ] ;
257281 }
258282
259283 void OnDisable ( )
@@ -680,15 +704,9 @@ void FinalPass(RenderLoop renderLoop)
680704 }
681705
682706 // Function to prepare light structure for GPU lighting
683- void ConvertLightForGPU ( CullResults cullResults , ref ShadowOutput shadowOutput , out LightList lightList )
707+ void ConvertLightForGPU ( CullResults cullResults , ref ShadowOutput shadowOutput , ref LightList lightList )
684708 {
685- // Init light list
686- lightList = new LightList ( ) ;
687- lightList . directionalLights = new List < DirectionalLightData > ( ) ;
688- lightList . punctualLights = new List < LightData > ( ) ;
689- lightList . areaLights = new List < LightData > ( ) ;
690- lightList . envLights = new List < EnvLightData > ( ) ;
691- lightList . punctualShadows = new List < PunctualShadowData > ( ) ;
709+ lightList . Clear ( ) ;
692710
693711 for ( int lightIndex = 0 , numLights = cullResults . visibleLights . Length ; lightIndex < numLights ; ++ lightIndex )
694712 {
@@ -710,7 +728,7 @@ void ConvertLightForGPU(CullResults cullResults, ref ShadowOutput shadowOutput,
710728
711729 if ( light . lightType == LightType . Directional )
712730 {
713- if ( lightList . areaLights . Count >= k_MaxDirectionalLightsOnSCreen )
731+ if ( lightList . directionalLights . Count >= k_MaxDirectionalLightsOnSCreen )
714732 continue ;
715733
716734 var directionalLightData = new DirectionalLightData ( ) ;
@@ -723,7 +741,32 @@ void ConvertLightForGPU(CullResults cullResults, ref ShadowOutput shadowOutput,
723741 directionalLightData . sinAngle = 0.0f ;
724742 directionalLightData . shadowIndex = - 1 ;
725743
726- // TODO: shadow
744+ bool hasDirectionalShadows = light . light . shadows != LightShadows . None && shadowOutput . GetShadowSliceCountLightIndex ( lightIndex ) != 0 ;
745+ bool hasDirectionalNotReachMaxLimit = lightList . directionalShadows . Count == 0 ; // Only one cascade shadow allowed
746+
747+ if ( hasDirectionalShadows && hasDirectionalNotReachMaxLimit ) // Note < MaxShadows should be check at shadowOutput creation
748+ {
749+ // When we have a point light, we assumed that there is 6 consecutive PunctualShadowData
750+ directionalLightData . shadowIndex = 0 ;
751+
752+ for ( int sliceIndex = 0 ; sliceIndex < shadowOutput . GetShadowSliceCountLightIndex ( lightIndex ) ; ++ sliceIndex )
753+ {
754+ DirectionalShadowData directionalShadowData = new DirectionalShadowData ( ) ;
755+
756+ int shadowSliceIndex = shadowOutput . GetShadowSliceIndex ( lightIndex , sliceIndex ) ;
757+ directionalShadowData . worldToShadow = shadowOutput . shadowSlices [ shadowSliceIndex ] . shadowTransform . transpose ; // Transpose for hlsl reading ?
758+
759+ directionalShadowData . bias = light . light . shadowBias ;
760+
761+ lightList . directionalShadows . Add ( directionalShadowData ) ;
762+ }
763+
764+ // Fill split information for shaders
765+ for ( int s = 0 ; s < k_MaxCascadeCount ; ++ s )
766+ {
767+ lightList . directionalShadowSplitSphereSqr [ s ] = shadowOutput . directionalShadowSplitSphereSqr [ s ] ;
768+ }
769+ }
727770
728771 lightList . directionalLights . Add ( directionalLightData ) ;
729772
@@ -733,15 +776,22 @@ void ConvertLightForGPU(CullResults cullResults, ref ShadowOutput shadowOutput,
733776 // Note: LightType.Area is offline only, use for baking, no need to test it
734777 var lightData = new LightData ( ) ;
735778
736- // Early out if we reach the maximum
737779 // Test whether we should treat this punctual light as an area light.
738780 // It's a temporary hack until the proper UI support is added.
739- if ( additionalData . treatAsAreaLight )
781+ if ( additionalData . archetype != LightArchetype . Punctual )
740782 {
783+ // Early out if we reach the maximum
741784 if ( lightList . areaLights . Count >= k_MaxAreaLightsOnSCreen )
742785 continue ;
743786
744- lightData . lightType = GPULightType . Rectangle ;
787+ if ( additionalData . archetype == LightArchetype . Rectangle )
788+ {
789+ lightData . lightType = GPULightType . Rectangle ;
790+ }
791+ else
792+ {
793+ lightData . lightType = GPULightType . Line ;
794+ }
745795 }
746796 else
747797 {
@@ -831,13 +881,14 @@ void ConvertLightForGPU(CullResults cullResults, ref ShadowOutput shadowOutput,
831881 lightData . size = new Vector2 ( additionalData . areaLightLength , additionalData . areaLightWidth ) ;
832882 lightData . twoSided = additionalData . isDoubleSided ;
833883
834- if ( additionalData . treatAsAreaLight )
884+ if ( additionalData . archetype == LightArchetype . Punctual )
835885 {
836- lightList . areaLights . Add ( lightData ) ;
886+ lightList . punctualLights . Add ( lightData ) ;
837887 }
838888 else
839889 {
840- lightList . punctualLights . Add ( lightData ) ;
890+ // Area and line lights are both currently stored as area lights on the GPU.
891+ lightList . areaLights . Add ( lightData ) ;
841892 }
842893 }
843894
@@ -967,9 +1018,8 @@ public override void Render(Camera[] cameras, RenderLoop renderLoop)
9671018
9681019 renderLoop . SetupCameraProperties ( camera ) ; // Need to recall SetupCameraProperties after m_ShadowPass.Render
9691020
970- LightList lightList ;
971- ConvertLightForGPU ( cullResults , ref shadows , out lightList ) ;
972- PushGlobalParams ( camera , renderLoop , lightList ) ;
1021+ ConvertLightForGPU ( cullResults , ref shadows , ref m_lightList ) ;
1022+ PushGlobalParams ( camera , renderLoop , m_lightList ) ;
9731023
9741024 // build per tile light lists
9751025 //var numLights = 0; // GenerateSourceLightBuffers(camera, cullResults);
0 commit comments