FogVolume.lua 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. FogVolume =
  12. {
  13. type = "FogVolume",
  14. Properties =
  15. {
  16. bActive = 1, --[0,1,1,"If true, fog volume will be enabled."]
  17. eiVolumeType = 0, --[0,1,1,"Specifies the volume type. The following types are currently supported: 0 - Ellipsoid, 1 - Cube."
  18. Size = { x = 1, y = 1, z = 1 },
  19. color_Color = { x = 1, y = 1, z = 1 },
  20. fHDRDynamic = 0, --[-10,20,0.01,"Specifies how much brighter than the default 255,255,255 white the fog is."]
  21. bUseGlobalFogColor = 0, --[0,1,1,"If true, the Color property is ignored. Instead, the current global fog color is used."]
  22. GlobalDensity = 1.0, --[0,1000,1,"Controls the density of the fog. The higher the value the more dense the fog and the less you'll be able to see objects behind or inside the fog volume."]
  23. DensityOffset = 0.0, --[-1000,1000,1,"Offset fog density, used in conjunction with the GlobalDensity parameter."]
  24. NearCutoff = 0.0, --[0,2,0.1,"Stop rendering the object, depending on camera distance to object."]
  25. FallOffDirLong = 0.0, --[0,360,1,"Controls the longitude of the world space fall off direction of the fog. 0 represents East, rotation is counter-clockwise."]
  26. FallOffDirLati = 90.0, --[0,360,1,"Controls the latitude of the world space fall off direction of the fog. 90 lets the fall off direction point upwards in world space."]
  27. FallOffShift = 0.0, --[-100,100,0.1,"Controls how much to shift the fog density distribution along the fall off direction in world units (m)."]
  28. FallOffScale = 1.0, --[-100,100,0.01,"Scales the density distribution along the fall off direction. Higher values will make the fog fall off more rapidly and generate thicker fog layers along the negative fall off direction."]
  29. SoftEdges = 1.0, --[0,1,0.01,"Specifies a factor that is used to soften the edges of the fog volume when viewed from outside."]
  30. RampStart = 0.0, --[0,30000,1.0,"Specifies the start distance of fog density ramp in world units (m)."]
  31. RampEnd = 50.0, --[0,30000,1.0,"Specifies the end distance of fog density ramp in world units (m)."]
  32. RampInfluence = 0.0, --[0,1,0.0,"Controls the influence of fog density ramp."]
  33. WindInfluence = 1.0, --[0,20,0.0,"Controls the influence of the wind."]
  34. DensityNoiseScale = 1.0, --[0.0,10.0,0.0,"Scales the noise for the density."]
  35. DensityNoiseOffset = 1.0, --[-2,2,0.0,"Offsets the noise for the density."]
  36. DensityNoiseTimeFrequency = 0.0, --[0,1,0.0,"Controls the time frequency of the noise for the density."]
  37. DensityNoiseFrequency = { x = 10, y = 10, z = 10 }, --[1,1000,0.1,"Controls the spatial frequency of the noise for the density."]
  38. bIgnoresVisAreas = 0, --[0,1,0,"Controls whether this entity should respond to visareas."]
  39. bAffectsThisAreaOnly = 0, --[0,1,0,"Set this parameter to false to make this entity affect in multiple visareas."]
  40. },
  41. Fader =
  42. {
  43. fadeTime = 0.0,
  44. fadeToValue = 0.0,
  45. },
  46. Editor =
  47. {
  48. Model = "Editor/Objects/invisiblebox.cgf",
  49. Icon = "FogVolume.bmp",
  50. ShowBounds = 1,
  51. },
  52. }
  53. function FogVolume:OnSpawn()
  54. self:SetFlags(ENTITY_FLAG_CLIENT_ONLY, 0);
  55. self:SetFlags(ENTITY_FLAG_NO_PROXIMITY, 0);
  56. end
  57. -------------------------------------------------------
  58. function FogVolume:InitFogVolumeProperties()
  59. --System.Log( "FogVolume:InitFogVolumeProperties" )
  60. local props = self.Properties;
  61. self:LoadFogVolume( 0, self.Properties );
  62. end;
  63. -------------------------------------------------------
  64. function FogVolume:CreateFogVolume()
  65. --System.Log( "FogVolume:CreateFogVolume" )
  66. self:InitFogVolumeProperties()
  67. self.active = true;
  68. end
  69. -------------------------------------------------------
  70. function FogVolume:DeleteFogVolume()
  71. --System.Log( "FogVolume:DeleteFogVolume" )
  72. self:FreeSlot( 0 );
  73. self.active = false;
  74. end
  75. -------------------------------------------------------
  76. function FogVolume:OnInit()
  77. self.active = false;
  78. if( self.Properties.bActive == 1 ) then
  79. self:CreateFogVolume();
  80. end
  81. end
  82. -------------------------------------------------------
  83. function FogVolume:CheckMove()
  84. end
  85. -------------------------------------------------------
  86. function FogVolume:OnShutDown()
  87. end
  88. -------------------------------------------------------
  89. function FogVolume:OnPropertyChange()
  90. --System.Log( "FogVolume:OnPropertyChange" )
  91. if( self.Properties.bActive == 1 ) then
  92. self:CreateFogVolume();
  93. else
  94. self:DeleteFogVolume();
  95. end
  96. end
  97. -------------------------------------------------------
  98. -- optimization for common animated trackview properties, to avoid fully recreating everything on every animated frame
  99. function FogVolume:OnPropertyAnimated( name )
  100. local changeTakenCareOf = false;
  101. if (name=="GlobalDensity") then
  102. self:FadeGlobalDensity(0, 0, self.Properties.GlobalDensity); -- using fade with 0 time as there is not a 'set' function.
  103. changeTakenCareOf = true;
  104. end
  105. return changeTakenCareOf;
  106. end
  107. -------------------------------------------------------
  108. function FogVolume:OnReset()
  109. self.active = false;
  110. if( self.Properties.bActive == 1 ) then
  111. self:CreateFogVolume();
  112. end
  113. end
  114. -------------------------------------------------------
  115. -- Hide Event
  116. -------------------------------------------------------
  117. function FogVolume:Event_Hide()
  118. self:DeleteFogVolume();
  119. BroadcastEvent(self, "Hide");
  120. end
  121. -------------------------------------------------------
  122. -- Show Event
  123. -------------------------------------------------------
  124. function FogVolume:Event_Show()
  125. self:CreateFogVolume();
  126. BroadcastEvent(self, "Show");
  127. end
  128. -------------------------------------------------------
  129. -- Fade Event
  130. -------------------------------------------------------
  131. function FogVolume:Event_Fade()
  132. --System.Log("Do Fading");
  133. self:FadeGlobalDensity(0, self.Fader.fadeTime, self.Fader.fadeToValue);
  134. end
  135. -------------------------------------------------------
  136. -- Fade Time Event
  137. -------------------------------------------------------
  138. function FogVolume:Event_FadeTime(i, time)
  139. --System.Log("Fade time "..tostring(time));
  140. self.Fader.fadeTime = time;
  141. end
  142. -------------------------------------------------------
  143. -- Fade Value Event
  144. -------------------------------------------------------
  145. function FogVolume:Event_FadeValue(i, val)
  146. --System.Log("Fade val "..tostring(val));
  147. self.Fader.fadeToValue = val;
  148. end
  149. -------------------------------------------------------
  150. -- Set Enabled Event
  151. -------------------------------------------------------
  152. function FogVolume:Event_Enabled(i, enable)
  153. if (enable) then
  154. self:CreateFogVolume();
  155. self:ActivateOutput( "Enabled", true );
  156. else
  157. self:DeleteFogVolume();
  158. self:ActivateOutput( "Enabled", false );
  159. end
  160. end
  161. -------------------------------------------------------
  162. -- Set GlobalDensity Event
  163. -------------------------------------------------------
  164. function FogVolume:Event_SetGlobalDensity(i, val)
  165. self.Properties.GlobalDensity = val;
  166. self:FadeGlobalDensity(0, 0, self.Properties.GlobalDensity);
  167. end
  168. -------------------------------------------------------
  169. -- Set DensityNoiseOffset Event
  170. -------------------------------------------------------
  171. function FogVolume:Event_SetDensityNoiseOffset(i, val)
  172. self.Properties.DensityNoiseOffset = val;
  173. self:CreateFogVolume();
  174. end
  175. -------------------------------------------------------
  176. -- Set DensityNoiseScale Event
  177. -------------------------------------------------------
  178. function FogVolume:Event_SetDensityNoiseScale(i, val)
  179. self.Properties.DensityNoiseScale = val;
  180. self:CreateFogVolume();
  181. end
  182. -------------------------------------------------------
  183. -- Set WindInfluence Event
  184. -------------------------------------------------------
  185. function FogVolume:Event_SetWindInfluence(i, val)
  186. self.Properties.WindInfluence = val;
  187. self:CreateFogVolume();
  188. end
  189. -------------------------------------------------------
  190. -- Serialization
  191. -------------------------------------------------------
  192. function FogVolume:OnLoad(table)
  193. if(self.active and not table.active) then
  194. self:DeleteFogVolume();
  195. elseif(not self.active and table.active) then
  196. self:CreateFogVolume();
  197. end
  198. self.active = table.active;
  199. end
  200. -------------------------------------------------------
  201. function FogVolume:OnSave(table)
  202. table.active = self.active;
  203. end
  204. -------------------------------------------------------
  205. FogVolume.FlowEvents =
  206. {
  207. Inputs =
  208. {
  209. AO_Enabled = { FogVolume.Event_Enabled, "bool" },
  210. EV_Density = { FogVolume.Event_SetGlobalDensity, "float" },
  211. EV_DensityNoiseOffset = { FogVolume.Event_SetDensityNoiseOffset, "float" },
  212. EV_DensityNoiseScale = { FogVolume.Event_SetDensityNoiseScale, "float" },
  213. EV_WindInfluence = { FogVolume.Event_SetWindInfluence, "float" },
  214. },
  215. Outputs =
  216. {
  217. Enabled = "bool"
  218. },
  219. }