EnvironmentLight.lua 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. ----------------------------------------------------------------------------------------------------
  2. --
  3. -- Copyright (c) Contributors to the Open 3D Engine Project.
  4. -- For complete copyright and license terms please see the LICENSE at the root of this distribution.
  5. --
  6. -- SPDX-License-Identifier: Apache-2.0 OR MIT
  7. --
  8. --
  9. --
  10. ----------------------------------------------------------------------------------------------------
  11. Script.ReloadScript("Scripts/Entities/Lights/Light.lua")
  12. EnvironmentLight =
  13. {
  14. Properties =
  15. {
  16. _nVersion = -1,
  17. bActive = 0,
  18. BoxSizeX = 10,
  19. BoxSizeY = 10,
  20. BoxSizeZ = 10,
  21. Color =
  22. {
  23. clrDiffuse = { x=1,y=1,z=1 },
  24. fDiffuseMultiplier = 1,
  25. fSpecularMultiplier = 1,
  26. },
  27. Projection =
  28. {
  29. bBoxProject = 0,
  30. fBoxWidth = 10,
  31. fBoxHeight = 10,
  32. fBoxLength = 10,
  33. },
  34. Options =
  35. {
  36. bAffectsThisAreaOnly = 1,
  37. bIgnoresVisAreas = 0,
  38. bDeferredClipBounds = 0,
  39. _texture_deferred_cubemap = "",
  40. SortPriority = 0,
  41. fAttenuationFalloffMax = 0.3,
  42. bVolumetricFog = 1, --[0,1,1,"Enables the light to affect volumetric fog."]
  43. bAffectsVolumetricFogOnly = 0, --[0,1,0,"Enables the light to affect only volumetric fog."]
  44. },
  45. OptionsAdvanced =
  46. {
  47. texture_deferred_cubemap = "",
  48. },
  49. },
  50. Editor =
  51. {
  52. ShowBounds = 0,
  53. AbsoluteRadius = 1,
  54. },
  55. _LightTable = {},
  56. }
  57. LightSlot = 1
  58. function EnvironmentLight:OnInit()
  59. self:SetFlags(ENTITY_FLAG_CLIENT_ONLY, 0);
  60. self:OnReset();
  61. self:Activate(1); -- Force OnUpdate to get called
  62. self:CacheResources("EnvironmentLight.lua");
  63. end
  64. function EnvironmentLight:CacheResources(requesterName)
  65. if ( self.Properties.OptionsAdvanced.texture_deferred_cubemap == "" ) then
  66. self.Properties.OptionsAdvanced.texture_deferred_cubemap = self.Properties.Options._texture_deferred_cubemap;
  67. end
  68. end
  69. function EnvironmentLight:OnShutDown()
  70. self:FreeSlot(LightSlot);
  71. end
  72. function EnvironmentLight:OnLoad(props)
  73. self:OnReset()
  74. self:ActivateLight(props.bActive)
  75. end
  76. function EnvironmentLight:OnSave(props)
  77. props.bActive = self.bActive
  78. end
  79. function EnvironmentLight:OnLevelLoaded()
  80. if (self.Properties.Options.bDeferredClipBounds) then
  81. self:UpdateLightClipBounds(LightSlot);
  82. end
  83. end
  84. function EnvironmentLight:OnPropertyChange()
  85. self:OnReset();
  86. self:ActivateLight( self.bActive );
  87. if (self.Properties.Options.bAffectsThisAreaOnly == 1) then
  88. self:UpdateLightClipBounds(LightSlot);
  89. end
  90. end
  91. -- optimization for common animated trackview properties, to avoid fully recreating everything on every animated frame
  92. function EnvironmentLight:OnPropertyAnimated( name )
  93. local changeTakenCareOf = false;
  94. if (name=="fDiffuseMultiplier" or name=="fSpecularMultiplier") then
  95. changeTakenCareOf = true;
  96. local Color = self.Properties.Color;
  97. local diffuse_mul = Color.fDiffuseMultiplier;
  98. local specular_multiplier = Color.fSpecularMultiplier;
  99. local diffuse_color = { x=Color.clrDiffuse.x*diffuse_mul, y=Color.clrDiffuse.y*diffuse_mul, z=Color.clrDiffuse.z*diffuse_mul };
  100. self:SetLightColorParams( LightSlot, diffuse_color, specular_multiplier);
  101. end
  102. return changeTakenCareOf;
  103. end
  104. function EnvironmentLight:OnUpdate(dt)
  105. if (self.bActive == 1 and self.Properties.Options.bAffectsThisAreaOnly == 1) then
  106. self:UpdateLightClipBounds(LightSlot);
  107. end
  108. if (not System.IsEditor()) then
  109. self:Activate(0);
  110. end
  111. end
  112. function EnvironmentLight:OnReset()
  113. if (self.bActive ~= self.Properties.bActive) then
  114. self:ActivateLight( self.Properties.bActive );
  115. end
  116. end
  117. function EnvironmentLight:ActivateLight( enable )
  118. if (enable and enable ~= 0) then
  119. self.bActive = 1;
  120. self:LoadLightToSlot(LightSlot);
  121. self:ActivateOutput( "Active",true );
  122. else
  123. self.bActive = 0;
  124. self:FreeSlot(LightSlot);
  125. self:ActivateOutput( "Active",false );
  126. end
  127. end
  128. function EnvironmentLight:LoadLightToSlot( nSlot )
  129. local props = self.Properties;
  130. local Color = props.Color;
  131. local Options = props.Options;
  132. local OptionsAdvanced = props.OptionsAdvanced;
  133. local Projection = props.Projection;
  134. local diffuse_mul = Color.fDiffuseMultiplier;
  135. local specular_mul = Color.fSpecularMultiplier;
  136. local lt = self._LightTable;
  137. lt.radius = 0.5 * (props.BoxSizeX*props.BoxSizeX + props.BoxSizeY*props.BoxSizeY + props.BoxSizeZ*props.BoxSizeZ) ^ 0.5;
  138. lt.box_size_x = props.BoxSizeX;
  139. lt.box_size_y = props.BoxSizeY;
  140. lt.box_size_z = props.BoxSizeZ;
  141. lt.diffuse_color = { x=Color.clrDiffuse.x*diffuse_mul, y=Color.clrDiffuse.y*diffuse_mul, z=Color.clrDiffuse.z*diffuse_mul };
  142. lt.specular_multiplier = specular_mul;
  143. if ( OptionsAdvanced.texture_deferred_cubemap == "" ) then
  144. OptionsAdvanced.texture_deferred_cubemap = Options._texture_deferred_cubemap;
  145. end
  146. lt.deferred_cubemap = OptionsAdvanced.texture_deferred_cubemap;
  147. lt.this_area_only = Options.bAffectsThisAreaOnly;
  148. lt.ignore_visareas = Options.bIgnoresVisAreas;
  149. lt.volumetric_fog = Options.bVolumetricFog;
  150. lt.volumetric_fog_only = Options.bAffectsVolumetricFogOnly;
  151. lt.box_projection = Projection.bBoxProject; -- settings for box projection
  152. lt.box_width = Projection.fBoxWidth;
  153. lt.box_height = Projection.fBoxHeight;
  154. lt.box_length = Projection.fBoxLength;
  155. lt.sort_priority = Options.SortPriority;
  156. lt.attenuation_falloff_max = Options.fAttenuationFalloffMax;
  157. lt.lightmap_linear_attenuation = 1;
  158. lt.is_rectangle_light = 0;
  159. lt.is_sphere_light = 0;
  160. lt.area_sample_number = 1;
  161. lt.RAE_AmbientColor = { x = 0, y = 0, z = 0 };
  162. lt.RAE_MaxShadow = 1;
  163. lt.RAE_DistMul = 1;
  164. lt.RAE_DivShadow = 1;
  165. lt.RAE_ShadowHeight = 1;
  166. lt.RAE_FallOff = 2;
  167. lt.RAE_VisareaNumber = 0;
  168. self:LoadLight( nSlot,lt );
  169. end
  170. function EnvironmentLight:Event_Enable()
  171. if (self.bActive == 0) then
  172. self:ActivateLight( 1 );
  173. end
  174. end
  175. function EnvironmentLight:Event_Disable()
  176. if (self.bActive == 1) then
  177. self:ActivateLight( 0 );
  178. end
  179. end
  180. function Light:NotifySwitchOnOffFromParent(wantOn)
  181. local wantOff = wantOn~=true;
  182. if (self.bActive == 1 and wantOff) then
  183. self:ActivateLight( 0 );
  184. elseif (self.bActive == 0 and wantOn) then
  185. self:ActivateLight( 1 );
  186. end
  187. end
  188. ------------------------------------------------------------------------------------------------------
  189. -- Event Handlers
  190. ------------------------------------------------------------------------------------------------------
  191. function EnvironmentLight:Event_Active( bActive )
  192. if (self.bActive == 0 and bActive == true) then
  193. self:ActivateLight( 1 );
  194. else
  195. if (self.bActive == 1 and bActive == false) then
  196. self:ActivateLight( 0 );
  197. end
  198. end
  199. end
  200. ------------------------------------------------------------------------------------------------------
  201. -- Event descriptions.
  202. ------------------------------------------------------------------------------------------------------
  203. EnvironmentLight.FlowEvents =
  204. {
  205. Inputs =
  206. {
  207. Active = { EnvironmentLight.Event_Active,"bool" },
  208. Enable = { EnvironmentLight.Event_Enable,"bool" },
  209. Disable = { EnvironmentLight.Event_Disable,"bool" },
  210. },
  211. Outputs =
  212. {
  213. Active = "bool",
  214. },
  215. }