2016-09-19 03:26:44 +02:00
''' <summary>
2016-09-07 18:50:38 +02:00
''' A class to handle entity lighting.
''' </summary>
Public Class Lighting
2022-04-15 16:28:17 +02:00
Shared LightingColorTexture As Texture2D = TextureManager . GetTexture ( " SkyDomeResource\LightingColors " )
Shared FogColorTexture As Texture2D = TextureManager . GetTexture ( " SkyDomeResource\FogColors " )
2016-09-07 18:50:38 +02:00
2022-04-15 16:28:17 +02:00
#Region "Methods"
Public Shared Function GetEnvironmentColor ( TextureType As Integer ) As Vector3 '0 = Directional light, 1 = Ambient light, 2 = Fog color
Dim ColorTexture As Texture2D
Dim EnvironmentColor As Vector3 = Nothing
2016-09-07 18:50:38 +02:00
2022-04-15 16:28:17 +02:00
Dim x As Integer = 0
Dim y As Integer = 0
2016-09-07 18:50:38 +02:00
2022-04-15 16:28:17 +02:00
Select Case TextureType
Case 0
x = Screen . Level . DayTime - 1
If x >= 0 AndAlso x <= 3 Then
ColorTexture = LightingColorTexture
Dim ColorData ( 0 ) As Color
ColorTexture . GetData ( 0 , New Rectangle ( x , 0 , 1 , 1 ) , ColorData , 0 , 1 )
Dim DarkOrBrightData ( 0 ) As Color
ColorTexture . GetData ( 0 , New Rectangle ( x , 1 , 1 , 1 ) , DarkOrBrightData , 0 , 1 )
EnvironmentColor = ColorData ( 0 ) . ToVector3
If DarkOrBrightData ( 0 ) = Color . Black Then
EnvironmentColor = New Vector3 ( 0 . 0F ) - EnvironmentColor
End If
Else
EnvironmentColor = New Vector3 ( 0 . 0F )
End If
Case 1
x = Screen . Level . DayTime - 1
If x >= 0 AndAlso x <= 3 Then
ColorTexture = LightingColorTexture
2016-09-07 18:50:38 +02:00
2022-04-15 16:28:17 +02:00
Dim ColorData ( 0 ) As Color
ColorTexture . GetData ( 0 , New Rectangle ( x , 2 , 1 , 1 ) , ColorData , 0 , 1 )
2016-09-07 18:50:38 +02:00
2022-04-15 16:28:17 +02:00
Dim DarkOrBrightData ( 0 ) As Color
ColorTexture . GetData ( 0 , New Rectangle ( x , 3 , 1 , 1 ) , DarkOrBrightData , 0 , 1 )
2016-09-07 18:50:38 +02:00
2022-04-15 16:28:17 +02:00
EnvironmentColor = ColorData ( 0 ) . ToVector3
If DarkOrBrightData ( 0 ) = Color . Black Then
EnvironmentColor = New Vector3 ( 0 . 0F ) - EnvironmentColor
End If
2016-09-07 18:50:38 +02:00
2022-04-15 16:28:17 +02:00
Else
EnvironmentColor = New Vector3 ( 0 . 0F )
End If
Case 2
ColorTexture = FogColorTexture
Select Case Screen . Level . EnvironmentType
Case 0
2022-10-16 20:31:07 +02:00
x = Screen . Level . DayTime - 1
2022-04-15 16:28:17 +02:00
If x > 2 Then
x = 0
y += 1
End If
Case 1
x = 1
y = 1
Case 2
x = 2
y = 1
Case 3
x = 0
y = 2
Case 4
x = 1
y = 2
Case 5
x = 2
y = 2
End Select
Dim ColorData ( 0 ) As Color
ColorTexture . GetData ( 0 , New Rectangle ( x , y , 1 , 1 ) , ColorData , 0 , 1 )
EnvironmentColor = ColorData ( 0 ) . ToVector3
End Select
Return EnvironmentColor
End Function
2016-09-07 18:50:38 +02:00
''' <summary>
''' Updates the lighting values of a BasicEffect instance.
''' </summary>
''' <param name="refEffect">A reference to the BasicEffect that should receive the lighting update.</param>
''' <param name="ForceLighting">Checks, if the lighting update on the effect should be forced.</param>
Public Shared Sub UpdateLighting ( ByRef refEffect As BasicEffect , Optional ByVal ForceLighting As Boolean = False )
2023-07-30 12:05:08 +02:00
If Core . GameOptions . LightingEnabled = True OrElse ForceLighting = True Then ' Only update the lighting if either the currently loaded level instance allows this, or it's getting forced.
' Set default parameters:
refEffect . LightingEnabled = True ' Enable lighting (gets disabled later, if not used)
refEffect . PreferPerPixelLighting = True ' Yes. Please.
refEffect . SpecularPower = 2000 . 0F
' LightType results:
' 0 = Night
' 1 = Morning
' 2 = Day
' 3 = Evening
' Anything higher than 3 = No Lighting
Select Case GetLightingType ( )
Case 0 ' Night
refEffect . AmbientLightColor = GetEnvironmentColor ( 1 )
refEffect . DirectionalLight0 . DiffuseColor = GetEnvironmentColor ( 0 )
refEffect . DirectionalLight0 . Direction = Vector3 . Normalize ( New Vector3 ( 1 . 0F , 1 . 0F , - 1 . 0F ) )
refEffect . DirectionalLight0 . SpecularColor = New Vector3 ( 0 . 0F )
refEffect . DirectionalLight0 . Enabled = True
Case 1 ' Morning
refEffect . AmbientLightColor = GetEnvironmentColor ( 1 )
refEffect . DirectionalLight0 . DiffuseColor = GetEnvironmentColor ( 0 )
refEffect . DirectionalLight0 . Direction = Vector3 . Normalize ( New Vector3 ( - 1 . 0F , 0 . 0F , 1 . 0F ) )
refEffect . DirectionalLight0 . SpecularColor = New Vector3 ( 0 . 0F )
refEffect . DirectionalLight0 . Enabled = True
Case 2 ' Day
refEffect . AmbientLightColor = GetEnvironmentColor ( 1 )
refEffect . DirectionalLight0 . DiffuseColor = GetEnvironmentColor ( 0 )
refEffect . DirectionalLight0 . Direction = Vector3 . Normalize ( New Vector3 ( - 1 . 0F , 0 . 0F , 1 . 0F ) )
refEffect . DirectionalLight0 . SpecularColor = New Vector3 ( 0 . 0F )
refEffect . DirectionalLight0 . Enabled = True
Case 3 ' Evening
refEffect . AmbientLightColor = GetEnvironmentColor ( 1 )
refEffect . DirectionalLight0 . DiffuseColor = GetEnvironmentColor ( 0 )
refEffect . DirectionalLight0 . Direction = Vector3 . Normalize ( New Vector3 ( 1 . 0F , 1 . 0F , - 1 . 0F ) )
refEffect . DirectionalLight0 . SpecularColor = New Vector3 ( 0 . 0F )
refEffect . DirectionalLight0 . Enabled = True
Case Else 'Disable lighting on the effect
refEffect . LightingEnabled = False
End Select
Else
' Disable lighting if the effect isn't supposed to have light.
refEffect . LightingEnabled = False
End If
End Sub
''' <summary>
''' Updates the lighting values of a BasicEffect instance.
''' </summary>
''' <param name="refEffect">A reference to the BasicEffect that should receive the lighting update.</param>
''' <param name="ForceLighting">Checks, if the lighting update on the effect should be forced.</param>
Public Shared Sub UpdateLighting ( ByRef refEffect As BasicEffectWithAlphaTest , Optional ByVal ForceLighting As Boolean = False )
If Core . GameOptions . LightingEnabled = True OrElse ForceLighting = True Then ' Only update the lighting if either the currently loaded level instance allows this, or it's getting forced.
2017-01-18 21:07:45 +01:00
' Set default parameters:
refEffect . LightingEnabled = True ' Enable lighting (gets disabled later, if not used)
refEffect . PreferPerPixelLighting = True ' Yes. Please.
2016-09-07 18:50:38 +02:00
refEffect . SpecularPower = 2000 . 0F
2017-01-18 21:07:45 +01:00
' LightType results:
2022-09-15 21:12:12 +02:00
' 0 = Night
' 1 = Morning
' 2 = Day
2017-01-18 21:07:45 +01:00
' 3 = Evening
' Anything higher than 3 = No Lighting
2016-09-07 18:50:38 +02:00
Select Case GetLightingType ( )
2022-06-22 21:04:03 +02:00
Case 0 ' Night
2022-04-15 16:28:17 +02:00
refEffect . AmbientLightColor = GetEnvironmentColor ( 1 )
2016-09-07 18:50:38 +02:00
2022-04-15 16:28:17 +02:00
refEffect . DirectionalLight0 . DiffuseColor = GetEnvironmentColor ( 0 )
2022-06-22 21:04:03 +02:00
refEffect . DirectionalLight0 . Direction = Vector3 . Normalize ( New Vector3 ( 1 . 0F , 1 . 0F , - 1 . 0F ) )
2016-09-07 18:50:38 +02:00
refEffect . DirectionalLight0 . SpecularColor = New Vector3 ( 0 . 0F )
refEffect . DirectionalLight0 . Enabled = True
2022-06-22 21:04:03 +02:00
Case 1 ' Morning
2022-04-15 16:28:17 +02:00
refEffect . AmbientLightColor = GetEnvironmentColor ( 1 )
2016-09-07 18:50:38 +02:00
2022-04-15 16:28:17 +02:00
refEffect . DirectionalLight0 . DiffuseColor = GetEnvironmentColor ( 0 )
2022-06-22 21:04:03 +02:00
refEffect . DirectionalLight0 . Direction = Vector3 . Normalize ( New Vector3 ( - 1 . 0F , 0 . 0F , 1 . 0F ) )
2016-09-07 18:50:38 +02:00
refEffect . DirectionalLight0 . SpecularColor = New Vector3 ( 0 . 0F )
refEffect . DirectionalLight0 . Enabled = True
2022-06-22 21:04:03 +02:00
Case 2 ' Day
2022-04-15 16:28:17 +02:00
refEffect . AmbientLightColor = GetEnvironmentColor ( 1 )
2016-09-07 18:50:38 +02:00
2022-04-15 16:28:17 +02:00
refEffect . DirectionalLight0 . DiffuseColor = GetEnvironmentColor ( 0 )
refEffect . DirectionalLight0 . Direction = Vector3 . Normalize ( New Vector3 ( - 1 . 0F , 0 . 0F , 1 . 0F ) )
2016-09-07 18:50:38 +02:00
refEffect . DirectionalLight0 . SpecularColor = New Vector3 ( 0 . 0F )
refEffect . DirectionalLight0 . Enabled = True
2017-01-18 21:07:45 +01:00
Case 3 ' Evening
2022-04-15 16:28:17 +02:00
refEffect . AmbientLightColor = GetEnvironmentColor ( 1 )
2016-09-07 18:50:38 +02:00
2022-04-15 16:28:17 +02:00
refEffect . DirectionalLight0 . DiffuseColor = GetEnvironmentColor ( 0 )
refEffect . DirectionalLight0 . Direction = Vector3 . Normalize ( New Vector3 ( 1 . 0F , 1 . 0F , - 1 . 0F ) )
2016-09-07 18:50:38 +02:00
refEffect . DirectionalLight0 . SpecularColor = New Vector3 ( 0 . 0F )
refEffect . DirectionalLight0 . Enabled = True
2017-01-18 21:07:45 +01:00
Case Else 'Disable lighting on the effect
2016-09-07 18:50:38 +02:00
refEffect . LightingEnabled = False
End Select
Else
2017-01-18 21:07:45 +01:00
' Disable lighting if the effect isn't supposed to have light.
2016-09-07 18:50:38 +02:00
refEffect . LightingEnabled = False
End If
End Sub
Public Shared Function GetLightingType ( ) As Integer
2017-01-18 21:07:45 +01:00
Dim LightType As Integer = CInt ( World . GetTime ( ) ) ' Determine default lighting type by the world time.
2016-09-07 18:50:38 +02:00
2017-01-18 21:07:45 +01:00
' Level's lighttype values:
' 0 = Get lighting from the current time of day.
' 1 = Disable lighting
2022-06-22 21:04:03 +02:00
' 2 = Always Night
' 3 = Always Morning
' 4 = Always Day
2017-01-18 21:07:45 +01:00
' 5 = Always Evening
2016-09-07 18:50:38 +02:00
2021-08-20 17:53:40 +02:00
If Screen . Level . EnvironmentType = World . EnvironmentTypes . Outside Then
Select Case Screen . Level . DayTime
Case 1
LightType = 0
2022-04-15 16:28:17 +02:00
Case 2
2021-08-20 17:53:40 +02:00
LightType = 1
2022-04-15 16:28:17 +02:00
Case 3
LightType = 2
2021-08-20 17:53:40 +02:00
Case 4
LightType = 3
End Select
End If
2022-10-08 20:36:59 +02:00
If Screen . Level . LightingType = 1 Then ' If the level lighting type is 1, disable lighting (set index to 99).
LightType = 99
End If
2022-02-28 15:56:00 +01:00
If Screen . Level . LightingType > 1 And Screen . Level . LightingType < 6 Then ' If the level's lighting type is 2, 3, 4 or 5, set to the respective LightType (set time of day).
LightType = Screen . Level . LightingType - 2
End If
2022-11-26 14:41:30 +01:00
If Screen . Level . LightingType = 6 AndAlso Screen . Level . EnvironmentType = 1 OrElse Screen . Level . LightingType > 6 Then
LightType = 99
End If
2016-09-07 18:50:38 +02:00
Return LightType
End Function
#End Region
2017-01-18 21:07:45 +01:00
End Class