@@ -144,6 +144,8 @@ static internal VertexBuffer GetDummyVertexBuffer(GraphicsDevice graphicsDevice)
144144 }
145145 return DummyVertexBuffer ;
146146 }
147+
148+ public StaticLight Light { get ; protected set ; }
147149 }
148150
149151 [ DebuggerDisplay ( "{Material} {RenderPrimitive} {Flags}" ) ]
@@ -391,7 +393,7 @@ public class RenderFrame
391393 const float SunIntensity = 1 ;
392394 const float MoonIntensity = SunIntensity / 380000 ;
393395 public const float HeadLightIntensity = 25000 ; // See some sample values: https://docs.unity3d.com/Packages/com.unity.cloud.gltfast@5.2/manual/LightUnits.html
394- float LightClampTo = 1 ;
396+ float LightDayNightClampTo = 1 ;
395397 float LightDayNightMultiplier = 1 ;
396398
397399 // Local shadow map data.
@@ -566,17 +568,17 @@ public void PrepareFrame(Viewer viewer)
566568
567569 if ( SolarDirection . Y <= - 0.05 )
568570 {
569- LightClampTo = 1 ; // at nighttime max light
571+ LightDayNightClampTo = 1 ; // at nighttime max light
570572 LightDayNightMultiplier = 1 ;
571573 }
572574 else if ( SolarDirection . Y >= 0.15 )
573575 {
574- LightClampTo = 0.5f ; // at daytime min light
576+ LightDayNightClampTo = 0.5f ; // at daytime min light
575577 LightDayNightMultiplier = 0.1f ;
576578 }
577579 else
578580 {
579- LightClampTo = 1 - 2.5f * ( SolarDirection . Y + 0.05f ) ; // in the meantime interpolate
581+ LightDayNightClampTo = 1 - 2.5f * ( SolarDirection . Y + 0.05f ) ; // in the meantime interpolate
580582 LightDayNightMultiplier = 1 - 4.5f * ( SolarDirection . Y + 0.05f ) ;
581583 }
582584 }
@@ -663,7 +665,12 @@ public void AddAutoPrimitive(Vector3 mstsLocation, float objectRadius, float obj
663665 if ( float . IsPositiveInfinity ( objectViewingDistance ) || ( Camera != null && Camera . InRange ( mstsLocation , objectRadius , objectViewingDistance ) ) )
664666 {
665667 if ( Camera != null && Camera . InFov ( mstsLocation , objectRadius ) )
668+ {
666669 AddPrimitive ( material , primitive , group , ref xnaMatrix , flags , itemData ) ;
670+
671+ if ( primitive ? . Light != null && ! primitive . Light . IsManaged && primitive . Light . Range > 0 && Camera . InRange ( mstsLocation , objectRadius , primitive . Light . Range ) )
672+ AddLight ( primitive . Light , ref xnaMatrix ) ;
673+ }
667674 }
668675
669676 if ( Game . Settings . DynamicShadows && ( RenderProcess . ShadowMapCount > 0 ) && ( ( flags & ShapeFlags . ShadowCaster ) != 0 ) )
@@ -1049,17 +1056,35 @@ void DrawSequencesDistantMountains(GraphicsDevice graphicsDevice, bool logging)
10491056 }
10501057 }
10511058
1052- public void AddLight ( ShapeLight light , ref Matrix worldMatrix , float lodBias , float fadingIntensity )
1059+ /// <summary>
1060+ /// Add unmanaged static lights
1061+ /// </summary>
1062+ public void AddLight ( StaticLight light , ref Matrix worldMatrix )
10531063 {
10541064 // Do not allow directional light injection. That is reserved to the sun and the moon.
1055- if ( light != null && light . Type != LightMode . Directional )
1056- AddLight ( light . Type , worldMatrix . Translation , Vector3 . TransformNormal ( - Vector3 . UnitZ , worldMatrix ) , light . Color , light . Intensity , light . Range , light . InnerConeCos , light . OuterConeCos , fadingIntensity , false ) ;
1065+ if ( light != null && ! light . IsManaged && light . Type != LightMode . Directional )
1066+ AddLight ( light . Type , worldMatrix . Translation , Vector3 . TransformNormal ( - Vector3 . UnitZ , worldMatrix ) , light . Color , light . Intensity , light . Range , light . InnerConeCos , light . OuterConeCos , 1 , false ) ;
10571067 }
1068+
1069+ /// <summary>
1070+ /// Used for the Sun / Moon only, intensity is in lm/m2
1071+ /// </summary>
1072+ void AddLight ( Vector3 direction , Vector3 color , float intensity , float fade ) => AddLight ( LightMode . Directional , Vector3 . Zero , direction , color , intensity , - 1 , 0 , 0 , fade , true ) ;
1073+
10581074 /// <summary>
1059- /// Used for Sun / Moon only
1075+ /// Add an active light to the actually compiled frame.
10601076 /// </summary>
1061- public void AddLight ( Vector3 direction , Vector3 color , float intensity , float fadingIntensity ) => AddLight ( LightMode . Directional , Vector3 . Zero , direction , color , intensity , - 1 , 0 , 0 , fadingIntensity , true ) ;
1062- public void AddLight ( LightMode type , Vector3 position , Vector3 direction , Vector3 color , float intensity , float range , float innerConeCos , float outerConeCos , float fadingIntensity , bool ignoreDayNight )
1077+ /// <param name="type">Can be Point or Spot only. Use the Directional type only for the Sun and the Moon.</param>
1078+ /// <param name="position">The worldMatrix.Translation</param>
1079+ /// <param name="direction">The light direction</param>
1080+ /// <param name="color">The light color</param>
1081+ /// <param name="intensity">Luminous intensity in candela (lm/sr)</param>
1082+ /// <param name="range">Cutoff distance</param>
1083+ /// <param name="innerConeCos"></param>
1084+ /// <param name="outerConeCos"></param>
1085+ /// <param name="fade">Fading, 0 no light, 1 full light</param>
1086+ /// <param name="ignoreDayNight">At daytime the intensity is automatically reduced to match the sunlight. Disable this by this parameter.</param>
1087+ public void AddLight ( LightMode type , Vector3 position , Vector3 direction , Vector3 color , float intensity , float range , float innerConeCos , float outerConeCos , float fade , bool ignoreDayNight )
10631088 {
10641089 if ( NumLights >= RenderProcess . MAX_LIGHTS )
10651090 return ;
@@ -1068,8 +1093,9 @@ public void AddLight(LightMode type, Vector3 position, Vector3 direction, Vector
10681093 LightPositions [ NumLights ] = position ;
10691094 LightDirections [ NumLights ] = direction ;
10701095 LightColors [ NumLights ] = color ;
1071- LightIntensities [ NumLights ] = intensity
1072- * ( ignoreDayNight ? MathHelper . Clamp ( fadingIntensity , 0 , 1 ) : MathHelper . Clamp ( fadingIntensity * LightDayNightMultiplier * LightClampTo , 0 , LightClampTo ) ) ;
1096+ LightIntensities [ NumLights ] = intensity * ( ignoreDayNight
1097+ ? MathHelper . Clamp ( fade , 0 , 1 )
1098+ : MathHelper . Clamp ( fade * LightDayNightMultiplier * LightDayNightClampTo , 0 , LightDayNightClampTo ) ) ;
10731099 LightRanges [ NumLights ] = range * ( ignoreDayNight ? 1 : LightDayNightMultiplier ) ;
10741100 LightInnerConeCos [ NumLights ] = innerConeCos ;
10751101 LightOuterConeCos [ NumLights ] = outerConeCos ;
0 commit comments