TimeOfDayController.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.Events;
  6. namespace Funly.SkyStudio
  7. {
  8. // This controller manages time and updating the skybox material with the proper configuration
  9. // values for the current time of day. This loads sky data from your sky profile.
  10. [ExecuteInEditMode]
  11. public class TimeOfDayController : MonoBehaviour
  12. {
  13. // Get access to the most recently created TimeOfDayController.
  14. public static TimeOfDayController instance { get; private set; }
  15. [Tooltip("Sky profile defines the skyColors configuration for times of day. " +
  16. "This script will animate between those skyColors values based on the time of day.")]
  17. [SerializeField]
  18. private SkyProfile m_SkyProfile;
  19. public SkyProfile skyProfile
  20. {
  21. get { return m_SkyProfile; }
  22. set
  23. {
  24. if (value != null && copySkyProfile)
  25. {
  26. m_SkyProfile = Instantiate(value);
  27. }
  28. else
  29. {
  30. m_SkyProfile = value;
  31. }
  32. m_SkyMaterialController = null;
  33. UpdateSkyForCurrentTime();
  34. SynchronizeAllShaderKeywords();
  35. }
  36. }
  37. [Tooltip("Time is expressed in a fractional number of days that have completed.")]
  38. [SerializeField]
  39. private float m_SkyTime = 0;
  40. public float skyTime
  41. {
  42. get { return m_SkyTime; }
  43. set
  44. {
  45. m_SkyTime = Mathf.Abs(value);
  46. UpdateSkyForCurrentTime();
  47. }
  48. }
  49. [Tooltip("Automatically advance time at fixed speed.")]
  50. public bool automaticTimeIncrement;
  51. [Tooltip("Create a copy of the sky profile at runtime, so modifications don't affect the original Sky Profile in your project.")]
  52. public bool copySkyProfile;
  53. // Use the Sky Material controller to directly manipulate the skybox values programatically.
  54. private SkyMaterialController m_SkyMaterialController;
  55. public SkyMaterialController SkyMaterial { get { return m_SkyMaterialController; } }
  56. [Tooltip("Speed at which to advance time by if in automatic increment is enabled.")]
  57. [Range(0, 1)]
  58. public float automaticIncrementSpeed = .01f;
  59. [Tooltip("Sun orbit.")]
  60. public OrbitingBody sunOrbit;
  61. [Tooltip("Moon orbit.")]
  62. public OrbitingBody moonOrbit;
  63. [Tooltip("Controller for managing weather effects")]
  64. public WeatherController weatherController;
  65. [Tooltip("If true we'll invoke DynamicGI.UpdateEnvironment() when skybox changes. This is an expensive operation.")]
  66. public bool updateGlobalIllumination = false;
  67. // Callback invoked whenever the time of day changes.
  68. public delegate void TimeOfDayDidChange(TimeOfDayController tc, float timeOfDay);
  69. public event TimeOfDayDidChange timeChangedCallback;
  70. private bool m_DidInitialUpdate;
  71. // Current progress value through a day cycle (value 0-1).
  72. public float timeOfDay
  73. {
  74. get { return m_SkyTime - ((int)m_SkyTime); }
  75. }
  76. public int daysElapsed
  77. {
  78. get { return (int)m_SkyTime; }
  79. }
  80. void Awake()
  81. {
  82. instance = this;
  83. }
  84. private void OnEnabled()
  85. {
  86. skyTime = m_SkyTime;
  87. }
  88. private void OnValidate()
  89. {
  90. if (gameObject.activeInHierarchy == false)
  91. {
  92. return;
  93. }
  94. skyTime = m_SkyTime;
  95. skyProfile = m_SkyProfile;
  96. }
  97. private void WarnInvalidSkySetup()
  98. {
  99. Debug.LogError("Your SkySystemController has an old or invalid prefab layout! Please run the upgrade tool in 'Windows -> Sky Studio -> Upgrade Sky System Controller'. Do not rename or modify any of the children in the SkySystemController hierarchy.");
  100. }
  101. void Update()
  102. {
  103. if (!skyProfile)
  104. {
  105. return;
  106. }
  107. if (automaticTimeIncrement && Application.isPlaying)
  108. {
  109. skyTime += automaticIncrementSpeed * Time.deltaTime;
  110. }
  111. // Catch older sky configurations or invalid setups early, and log an error message on how to fix it.
  112. if (sunOrbit == null || moonOrbit == null ||
  113. sunOrbit.rotateBody == null || moonOrbit.rotateBody == null ||
  114. sunOrbit.positionTransform == null || moonOrbit.positionTransform == null)
  115. {
  116. WarnInvalidSkySetup();
  117. return;
  118. }
  119. // We need to force a time update once, to intialize the state of everthing.
  120. if (m_DidInitialUpdate == false) {
  121. UpdateSkyForCurrentTime();
  122. m_DidInitialUpdate = true;
  123. }
  124. if (weatherController != null)
  125. {
  126. weatherController.UpdateForTimeOfDay(skyProfile, timeOfDay);
  127. }
  128. // Update Sun properties that need frame updates.
  129. if (skyProfile.IsFeatureEnabled(ProfileFeatureKeys.SunFeature))
  130. {
  131. if (sunOrbit.positionTransform)
  132. {
  133. // m_SkyMaterialController.SunWorldToLocalMatrix = sunOrbit.positionTransform.worldToLocalMatrix;
  134. }
  135. if (skyProfile.IsFeatureEnabled(ProfileFeatureKeys.SunCustomTextureFeature))
  136. {
  137. if (skyProfile.IsFeatureEnabled(ProfileFeatureKeys.SunRotationFeature))
  138. {
  139. sunOrbit.rotateBody.AllowSpinning = true;
  140. sunOrbit.rotateBody.SpinSpeed = skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.SunRotationSpeedKey, timeOfDay);
  141. }
  142. else
  143. {
  144. sunOrbit.rotateBody.AllowSpinning = false;
  145. }
  146. }
  147. }
  148. // Update Moon properties that need frame updates.
  149. if (skyProfile.IsFeatureEnabled(ProfileFeatureKeys.MoonFeature))
  150. {
  151. if (moonOrbit.positionTransform)
  152. {
  153. m_SkyMaterialController.MoonWorldToLocalMatrix = moonOrbit.positionTransform.worldToLocalMatrix;
  154. }
  155. if (skyProfile.IsFeatureEnabled(ProfileFeatureKeys.MoonCustomTextureFeature))
  156. {
  157. if (skyProfile.IsFeatureEnabled(ProfileFeatureKeys.MoonRotationFeature))
  158. {
  159. moonOrbit.rotateBody.AllowSpinning = true;
  160. moonOrbit.rotateBody.SpinSpeed = skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.MoonRotationSpeedKey, timeOfDay);
  161. }
  162. else
  163. {
  164. moonOrbit.rotateBody.AllowSpinning = false;
  165. }
  166. }
  167. }
  168. }
  169. public void UpdateGlobalIllumination()
  170. {
  171. DynamicGI.UpdateEnvironment();
  172. }
  173. private void SynchronizeAllShaderKeywords()
  174. {
  175. if (m_SkyProfile == null)
  176. {
  177. return;
  178. }
  179. foreach (ProfileFeatureSection section in m_SkyProfile.profileDefinition.features)
  180. {
  181. foreach (ProfileFeatureDefinition feature in section.featureDefinitions)
  182. {
  183. if (feature.featureType == ProfileFeatureDefinition.FeatureType.ShaderKeyword)
  184. {
  185. SynchronizedShaderKeyword(feature.featureKey, feature.shaderKeyword);
  186. }
  187. else if (feature.featureType == ProfileFeatureDefinition.FeatureType.ShaderKeywordDropdown)
  188. {
  189. for (int i = 0; i < feature.featureKeys.Length; i++)
  190. {
  191. SynchronizedShaderKeyword(feature.featureKeys[i], feature.shaderKeywords[i]);
  192. }
  193. }
  194. }
  195. }
  196. }
  197. private void SynchronizedShaderKeyword(string featureKey, string shaderKeyword)
  198. {
  199. if (skyProfile == null || skyProfile.skyboxMaterial == null)
  200. {
  201. return;
  202. }
  203. if (skyProfile.IsFeatureEnabled(featureKey))
  204. {
  205. if (!skyProfile.skyboxMaterial.IsKeywordEnabled(shaderKeyword))
  206. {
  207. skyProfile.skyboxMaterial.EnableKeyword(shaderKeyword);
  208. }
  209. }
  210. else
  211. {
  212. if (skyProfile.skyboxMaterial.IsKeywordEnabled(shaderKeyword))
  213. {
  214. skyProfile.skyboxMaterial.DisableKeyword(shaderKeyword);
  215. }
  216. }
  217. }
  218. private Vector3 GetPrimaryLightDirection()
  219. {
  220. Vector3 dir;
  221. if (skyProfile.IsFeatureEnabled(ProfileFeatureKeys.SunFeature) && sunOrbit)
  222. {
  223. dir = sunOrbit.BodyGlobalDirection;
  224. }
  225. else if (skyProfile.IsFeatureEnabled(ProfileFeatureKeys.MoonFeature) && moonOrbit)
  226. {
  227. dir = moonOrbit.BodyGlobalDirection;
  228. }
  229. else
  230. {
  231. dir = new Vector3(0, 1, 0);
  232. }
  233. return dir;
  234. }
  235. public void UpdateSkyForCurrentTime()
  236. {
  237. if (skyProfile == null)
  238. {
  239. return;
  240. }
  241. if (skyProfile.skyboxMaterial == null)
  242. {
  243. Debug.LogError("Your sky profile is missing a reference to the skybox material.");
  244. return;
  245. }
  246. if (m_SkyMaterialController == null)
  247. {
  248. m_SkyMaterialController = new SkyMaterialController();
  249. }
  250. m_SkyMaterialController.SkyboxMaterial = skyProfile.skyboxMaterial;
  251. if (RenderSettings.skybox == null ||
  252. RenderSettings.skybox.GetInstanceID() != skyProfile.skyboxMaterial.GetInstanceID())
  253. {
  254. RenderSettings.skybox = skyProfile.skyboxMaterial;
  255. }
  256. SynchronizeAllShaderKeywords();
  257. // Sky.
  258. m_SkyMaterialController.BackgroundCubemap = skyProfile.GetTexturePropertyValue(ProfilePropertyKeys.SkyCubemapKey, timeOfDay) as Cubemap;
  259. m_SkyMaterialController.SkyColor = skyProfile.GetColorPropertyValue(ProfilePropertyKeys.SkyUpperColorKey, timeOfDay);
  260. m_SkyMaterialController.SkyMiddleColor = skyProfile.GetColorPropertyValue(ProfilePropertyKeys.SkyMiddleColorKey, timeOfDay);
  261. m_SkyMaterialController.HorizonColor = skyProfile.GetColorPropertyValue(ProfilePropertyKeys.SkyLowerColorKey, timeOfDay);
  262. m_SkyMaterialController.GradientFadeBegin = skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.HorizonTrasitionStartKey, timeOfDay);
  263. m_SkyMaterialController.GradientFadeLength = skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.HorizonTransitionLengthKey, timeOfDay);
  264. m_SkyMaterialController.SkyMiddlePosition = skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.SkyMiddleColorPositionKey, timeOfDay);
  265. m_SkyMaterialController.StarFadeBegin = skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.StarTransitionStartKey, timeOfDay);
  266. m_SkyMaterialController.StarFadeLength = skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.StarTransitionLengthKey, timeOfDay);
  267. m_SkyMaterialController.HorizonDistanceScale = skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.HorizonStarScaleKey, timeOfDay);
  268. // Clouds.
  269. if (skyProfile.IsFeatureEnabled(ProfileFeatureKeys.CloudFeature))
  270. {
  271. if (skyProfile.IsFeatureEnabled(ProfileFeatureKeys.NoiseCloudFeature))
  272. {
  273. m_SkyMaterialController.CloudTexture = skyProfile.GetTexturePropertyValue(ProfilePropertyKeys.CloudNoiseTextureKey, timeOfDay);
  274. m_SkyMaterialController.CloudTextureTiling = skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.CloudTextureTiling, timeOfDay);
  275. m_SkyMaterialController.CloudDensity = skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.CloudDensityKey, timeOfDay);
  276. m_SkyMaterialController.CloudSpeed = skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.CloudSpeedKey, timeOfDay);
  277. m_SkyMaterialController.CloudDirection = skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.CloudDirectionKey, timeOfDay);
  278. m_SkyMaterialController.CloudHeight = skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.CloudHeightKey, timeOfDay);
  279. m_SkyMaterialController.CloudColor1 = skyProfile.GetColorPropertyValue(ProfilePropertyKeys.CloudColor1Key, timeOfDay);
  280. m_SkyMaterialController.CloudColor2 = skyProfile.GetColorPropertyValue(ProfilePropertyKeys.CloudColor2Key, timeOfDay);
  281. m_SkyMaterialController.CloudFadePosition = skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.CloudFadePositionKey, timeOfDay);
  282. m_SkyMaterialController.CloudFadeAmount = skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.CloudFadeAmountKey, timeOfDay);
  283. }
  284. else if (skyProfile.IsFeatureEnabled(ProfileFeatureKeys.CubemapCloudFeature))
  285. {
  286. m_SkyMaterialController.CloudCubemap = skyProfile.GetTexturePropertyValue(ProfilePropertyKeys.CloudCubemapTextureKey, timeOfDay);
  287. m_SkyMaterialController.CloudCubemapRotationSpeed = skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.CloudCubemapRotationSpeedKey, timeOfDay);
  288. m_SkyMaterialController.CloudCubemapTintColor = skyProfile.GetColorPropertyValue(ProfilePropertyKeys.CloudCubemapTintColorKey, timeOfDay);
  289. m_SkyMaterialController.CloudCubemapHeight = skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.CloudCubemapHeightKey, timeOfDay);
  290. if (skyProfile.IsFeatureEnabled(ProfileFeatureKeys.CubemapCloudDoubleLayerFeature))
  291. {
  292. m_SkyMaterialController.CloudCubemapDoubleLayerHeight = skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.CloudCubemapDoubleLayerHeightKey, timeOfDay);
  293. m_SkyMaterialController.CloudCubemapDoubleLayerRotationSpeed = skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.CloudCubemapDoubleLayerRotationSpeedKey, timeOfDay);
  294. m_SkyMaterialController.CloudCubemapDoubleLayerTintColor = skyProfile.GetColorPropertyValue(ProfilePropertyKeys.CloudCubemapDoubleLayerTintColorKey, timeOfDay);
  295. if (skyProfile.IsFeatureEnabled(ProfileFeatureKeys.CubemapCloudDoubleLayerCubemapFeature))
  296. {
  297. m_SkyMaterialController.CloudCubemapDoubleLayerCustomTexture = skyProfile.GetTexturePropertyValue(ProfilePropertyKeys.CloudCubemapDoubleLayerCustomTextureKey, timeOfDay);
  298. }
  299. }
  300. }
  301. else if (skyProfile.IsFeatureEnabled(ProfileFeatureKeys.CubemapNormalCloudFeature))
  302. {
  303. m_SkyMaterialController.CloudCubemapNormalLightDirection = GetPrimaryLightDirection();
  304. m_SkyMaterialController.CloudCubemapNormalTexture = skyProfile.GetTexturePropertyValue(ProfilePropertyKeys.CloudCubemapNormalTextureKey, timeOfDay);
  305. m_SkyMaterialController.CloudCubemapNormalLitColor = skyProfile.GetColorPropertyValue(ProfilePropertyKeys.CloudCubemapNormalLitColorKey, timeOfDay);
  306. m_SkyMaterialController.CloudCubemapNormalShadowColor = skyProfile.GetColorPropertyValue(ProfilePropertyKeys.CloudCubemapNormalShadowKey, timeOfDay);
  307. m_SkyMaterialController.CloudCubemapNormalAmbientIntensity = skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.CloudCubemapNormalAmbientIntensity, timeOfDay);
  308. m_SkyMaterialController.CloudCubemapNormalHeight = skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.CloudCubemapNormalHeightKey, timeOfDay);
  309. m_SkyMaterialController.CloudCubemapNormalRotationSpeed = skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.CloudCubemapNormalRotationSpeedKey, timeOfDay);
  310. if (skyProfile.IsFeatureEnabled(ProfileFeatureKeys.CubemapNormalCloudDoubleLayerFeature))
  311. {
  312. m_SkyMaterialController.CloudCubemapNormalDoubleLayerHeight = skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.CloudCubemapNormalDoubleLayerHeightKey, timeOfDay);
  313. m_SkyMaterialController.CloudCubemapNormalDoubleLayerRotationSpeed = skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.CloudCubemapNormalDoubleLayerRotationSpeedKey, timeOfDay);
  314. m_SkyMaterialController.CloudCubemapNormalDoubleLayerLitColor = skyProfile.GetColorPropertyValue(ProfilePropertyKeys.CloudCubemapNormalDoubleLayerLitColorKey, timeOfDay);
  315. m_SkyMaterialController.CloudCubemapNormalDoubleLayerShadowColor = skyProfile.GetColorPropertyValue(ProfilePropertyKeys.CloudCubemapNormalDoubleLayerShadowKey, timeOfDay);
  316. if (skyProfile.IsFeatureEnabled(ProfileFeatureKeys.CubemapNormalCloudDoubleLayerCubemapFeature))
  317. {
  318. m_SkyMaterialController.CloudCubemapNormalDoubleLayerCustomTexture = skyProfile.GetTexturePropertyValue(ProfilePropertyKeys.CloudCubemapNormalDoubleLayerCustomTextureKey, timeOfDay);
  319. }
  320. }
  321. }
  322. }
  323. // Fog.
  324. if (skyProfile.IsFeatureEnabled(ProfileFeatureKeys.FogFeature))
  325. {
  326. Color fogColor = skyProfile.GetColorPropertyValue(ProfilePropertyKeys.FogColorKey, timeOfDay);
  327. m_SkyMaterialController.FogColor = fogColor;
  328. m_SkyMaterialController.FogDensity = skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.FogDensityKey, timeOfDay);
  329. m_SkyMaterialController.FogHeight = skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.FogLengthKey, timeOfDay);
  330. // Synchronize with Unity's global fog color so the rest of the scene uses this color fog.
  331. if (skyProfile.GetBoolPropertyValue(ProfilePropertyKeys.FogSyncWithGlobal, timeOfDay))
  332. {
  333. RenderSettings.fogColor = fogColor;
  334. }
  335. }
  336. // Sun.
  337. if (skyProfile.IsFeatureEnabled(ProfileFeatureKeys.SunFeature) && sunOrbit)
  338. {
  339. sunOrbit.Point = skyProfile.GetSpherePointPropertyValue(ProfilePropertyKeys.SunPositionKey, timeOfDay);
  340. m_SkyMaterialController.SunDirection = sunOrbit.BodyGlobalDirection;
  341. m_SkyMaterialController.SunColor = skyProfile.GetColorPropertyValue(ProfilePropertyKeys.SunColorKey, timeOfDay);
  342. m_SkyMaterialController.SunSize = skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.SunSizeKey, timeOfDay);
  343. m_SkyMaterialController.SunEdgeFeathering = skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.SunEdgeFeatheringKey, timeOfDay);
  344. m_SkyMaterialController.SunBloomFilterBoost = skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.SunColorIntensityKey, timeOfDay);
  345. if (skyProfile.IsFeatureEnabled(ProfileFeatureKeys.SunCustomTextureFeature))
  346. {
  347. m_SkyMaterialController.SunWorldToLocalMatrix = sunOrbit.positionTransform.worldToLocalMatrix;
  348. m_SkyMaterialController.SunTexture = skyProfile.GetTexturePropertyValue(ProfilePropertyKeys.SunTextureKey, timeOfDay);
  349. if (skyProfile.IsFeatureEnabled(ProfileFeatureKeys.SunRotationFeature))
  350. {
  351. sunOrbit.rotateBody.SpinSpeed = skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.SunRotationSpeedKey, timeOfDay);
  352. }
  353. }
  354. if (skyProfile.IsFeatureEnabled(ProfileFeatureKeys.SunSpriteSheetFeature))
  355. {
  356. m_SkyMaterialController.SetSunSpriteDimensions(
  357. (int)skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.SunSpriteColumnCountKey, timeOfDay),
  358. (int)skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.SunSpriteRowCountKey, timeOfDay));
  359. m_SkyMaterialController.SunSpriteItemCount = (int)skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.SunSpriteItemCountKey, timeOfDay);
  360. m_SkyMaterialController.SunSpriteAnimationSpeed = skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.SunSpriteAnimationSpeedKey, timeOfDay);
  361. }
  362. if (sunOrbit.BodyLight)
  363. {
  364. if (!sunOrbit.BodyLight.enabled)
  365. {
  366. sunOrbit.BodyLight.enabled = true;
  367. }
  368. RenderSettings.sun = sunOrbit.BodyLight;
  369. sunOrbit.BodyLight.color = skyProfile.GetColorPropertyValue(ProfilePropertyKeys.SunLightColorKey, timeOfDay);
  370. sunOrbit.BodyLight.intensity = skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.SunLightIntensityKey, timeOfDay);
  371. }
  372. }
  373. else if (sunOrbit && sunOrbit.BodyLight)
  374. {
  375. sunOrbit.BodyLight.enabled = false;
  376. }
  377. // Moon.
  378. if (skyProfile.IsFeatureEnabled(ProfileFeatureKeys.MoonFeature) && moonOrbit)
  379. {
  380. moonOrbit.Point = skyProfile.GetSpherePointPropertyValue(ProfilePropertyKeys.MoonPositionKey, timeOfDay);
  381. m_SkyMaterialController.MoonDirection = moonOrbit.BodyGlobalDirection;
  382. m_SkyMaterialController.MoonColor = skyProfile.GetColorPropertyValue(ProfilePropertyKeys.MoonColorKey, timeOfDay);
  383. m_SkyMaterialController.MoonSize = skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.MoonSizeKey, timeOfDay);
  384. m_SkyMaterialController.MoonEdgeFeathering = skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.MoonEdgeFeatheringKey, timeOfDay);
  385. m_SkyMaterialController.MoonBloomFilterBoost = skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.MoonColorIntensityKey, timeOfDay);
  386. if (skyProfile.IsFeatureEnabled(ProfileFeatureKeys.MoonCustomTextureFeature))
  387. {
  388. m_SkyMaterialController.MoonTexture = skyProfile.GetTexturePropertyValue(ProfilePropertyKeys.MoonTextureKey, timeOfDay);
  389. m_SkyMaterialController.MoonWorldToLocalMatrix = moonOrbit.positionTransform.worldToLocalMatrix;
  390. if (skyProfile.IsFeatureEnabled(ProfileFeatureKeys.MoonRotationFeature))
  391. {
  392. moonOrbit.rotateBody.SpinSpeed = skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.MoonRotationSpeedKey, timeOfDay);
  393. }
  394. }
  395. if (skyProfile.IsFeatureEnabled(ProfileFeatureKeys.MoonSpriteSheetFeature))
  396. {
  397. m_SkyMaterialController.SetMoonSpriteDimensions(
  398. (int)skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.MoonSpriteColumnCountKey, timeOfDay),
  399. (int)skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.MoonSpriteRowCountKey, timeOfDay));
  400. m_SkyMaterialController.MoonSpriteItemCount = (int)skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.MoonSpriteItemCountKey, timeOfDay);
  401. m_SkyMaterialController.MoonSpriteAnimationSpeed = skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.MoonSpriteAnimationSpeedKey, timeOfDay);
  402. }
  403. if (moonOrbit.BodyLight)
  404. {
  405. if (!moonOrbit.BodyLight.enabled)
  406. {
  407. moonOrbit.BodyLight.enabled = true;
  408. }
  409. moonOrbit.BodyLight.color = skyProfile.GetColorPropertyValue(ProfilePropertyKeys.MoonLightColorKey, timeOfDay);
  410. moonOrbit.BodyLight.intensity = skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.MoonLightIntensityKey, timeOfDay);
  411. }
  412. }
  413. else if (moonOrbit && moonOrbit.BodyLight)
  414. {
  415. moonOrbit.BodyLight.enabled = false;
  416. }
  417. if (skyProfile.IsFeatureEnabled(ProfileFeatureKeys.StarBasicFeature))
  418. {
  419. m_SkyMaterialController.StarBasicCubemap = skyProfile.GetTexturePropertyValue(ProfilePropertyKeys.StarBasicCubemapKey, timeOfDay);
  420. m_SkyMaterialController.StarBasicTwinkleSpeed = skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.StarBasicTwinkleSpeedKey, timeOfDay);
  421. m_SkyMaterialController.StarBasicTwinkleAmount = skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.StarBasicTwinkleAmountKey, timeOfDay);
  422. m_SkyMaterialController.StarBasicOpacity = skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.StarBasicOpacityKey, timeOfDay);
  423. m_SkyMaterialController.StarBasicTintColor = skyProfile.GetColorPropertyValue(ProfilePropertyKeys.StarBasicTintColorKey, timeOfDay);
  424. m_SkyMaterialController.StarBasicExponent = skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.StarBasicExponentKey, timeOfDay);
  425. m_SkyMaterialController.StarBasicIntensity = skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.StarBasicIntensityKey, timeOfDay);
  426. }
  427. else
  428. {
  429. // Star Layer 1.
  430. if (skyProfile.IsFeatureEnabled(ProfileFeatureKeys.StarLayer1Feature))
  431. {
  432. m_SkyMaterialController.StarLayer1DataTexture = skyProfile.starLayer1DataTexture;
  433. m_SkyMaterialController.StarLayer1Color = skyProfile.GetColorPropertyValue(ProfilePropertyKeys.Star1ColorKey, timeOfDay);
  434. m_SkyMaterialController.StarLayer1MaxRadius = skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.Star1SizeKey, timeOfDay);
  435. m_SkyMaterialController.StarLayer1Texture = skyProfile.GetTexturePropertyValue(ProfilePropertyKeys.Star1TextureKey, timeOfDay);
  436. m_SkyMaterialController.StarLayer1TwinkleAmount = skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.Star1TwinkleAmountKey, timeOfDay);
  437. m_SkyMaterialController.StarLayer1TwinkleSpeed = skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.Star1TwinkleSpeedKey, timeOfDay);
  438. m_SkyMaterialController.StarLayer1RotationSpeed = skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.Star1RotationSpeedKey, timeOfDay);
  439. m_SkyMaterialController.StarLayer1EdgeFeathering = skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.Star1EdgeFeatheringKey, timeOfDay);
  440. m_SkyMaterialController.StarLayer1BloomFilterBoost = skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.Star1ColorIntensityKey, timeOfDay);
  441. if (skyProfile.IsFeatureEnabled(ProfileFeatureKeys.StarLayer1SpriteSheetFeature))
  442. {
  443. m_SkyMaterialController.StarLayer1SpriteItemCount = (int)skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.Star1SpriteItemCountKey, timeOfDay);
  444. m_SkyMaterialController.StarLayer1SpriteAnimationSpeed = (int)skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.Star1SpriteAnimationSpeedKey, timeOfDay);
  445. m_SkyMaterialController.SetStarLayer1SpriteDimensions(
  446. (int)skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.Star1SpriteColumnCountKey, timeOfDay),
  447. (int)skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.Star1SpriteRowCountKey, timeOfDay));
  448. }
  449. }
  450. // Star Layer 2.
  451. if (skyProfile.IsFeatureEnabled(ProfileFeatureKeys.StarLayer2Feature))
  452. {
  453. m_SkyMaterialController.StarLayer2DataTexture = skyProfile.starLayer2DataTexture;
  454. m_SkyMaterialController.StarLayer2Color = skyProfile.GetColorPropertyValue(ProfilePropertyKeys.Star2ColorKey, timeOfDay);
  455. m_SkyMaterialController.StarLayer2MaxRadius = skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.Star2SizeKey, timeOfDay); ;
  456. m_SkyMaterialController.StarLayer2Texture = skyProfile.GetTexturePropertyValue(ProfilePropertyKeys.Star2TextureKey, timeOfDay);
  457. m_SkyMaterialController.StarLayer2TwinkleAmount = skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.Star2TwinkleAmountKey, timeOfDay);
  458. m_SkyMaterialController.StarLayer2TwinkleSpeed = skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.Star2TwinkleSpeedKey, timeOfDay);
  459. m_SkyMaterialController.StarLayer2RotationSpeed = skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.Star2RotationSpeedKey, timeOfDay);
  460. m_SkyMaterialController.StarLayer2EdgeFeathering = skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.Star2EdgeFeatheringKey, timeOfDay);
  461. m_SkyMaterialController.StarLayer2BloomFilterBoost = skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.Star2ColorIntensityKey, timeOfDay);
  462. if (skyProfile.IsFeatureEnabled(ProfileFeatureKeys.StarLayer2SpriteSheetFeature))
  463. {
  464. m_SkyMaterialController.StarLayer2SpriteItemCount = (int)skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.Star2SpriteItemCountKey, timeOfDay);
  465. m_SkyMaterialController.StarLayer2SpriteAnimationSpeed = (int)skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.Star2SpriteAnimationSpeedKey, timeOfDay);
  466. m_SkyMaterialController.SetStarLayer2SpriteDimensions(
  467. (int)skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.Star2SpriteColumnCountKey, timeOfDay),
  468. (int)skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.Star2SpriteRowCountKey, timeOfDay));
  469. }
  470. }
  471. // Star Layer 3.
  472. if (skyProfile.IsFeatureEnabled(ProfileFeatureKeys.StarLayer3Feature))
  473. {
  474. m_SkyMaterialController.StarLayer3DataTexture = skyProfile.starLayer3DataTexture;
  475. m_SkyMaterialController.StarLayer3Color = skyProfile.GetColorPropertyValue(ProfilePropertyKeys.Star3ColorKey, timeOfDay);
  476. m_SkyMaterialController.StarLayer3MaxRadius = skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.Star3SizeKey, timeOfDay);
  477. m_SkyMaterialController.StarLayer3Texture = skyProfile.GetTexturePropertyValue(ProfilePropertyKeys.Star3TextureKey, timeOfDay);
  478. m_SkyMaterialController.StarLayer3TwinkleAmount = skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.Star3TwinkleAmountKey, timeOfDay);
  479. m_SkyMaterialController.StarLayer3TwinkleSpeed = skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.Star3TwinkleSpeedKey, timeOfDay);
  480. m_SkyMaterialController.StarLayer3RotationSpeed = skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.Star3RotationSpeedKey, timeOfDay);
  481. m_SkyMaterialController.StarLayer3EdgeFeathering = skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.Star3EdgeFeatheringKey, timeOfDay);
  482. m_SkyMaterialController.StarLayer3BloomFilterBoost = skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.Star3ColorIntensityKey, timeOfDay);
  483. if (skyProfile.IsFeatureEnabled(ProfileFeatureKeys.StarLayer3SpriteSheetFeature))
  484. {
  485. m_SkyMaterialController.StarLayer3SpriteItemCount = (int)skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.Star3SpriteItemCountKey, timeOfDay);
  486. m_SkyMaterialController.StarLayer3SpriteAnimationSpeed = (int)skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.Star3SpriteAnimationSpeedKey, timeOfDay);
  487. m_SkyMaterialController.SetStarLayer3SpriteDimensions(
  488. (int)skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.Star3SpriteColumnCountKey, timeOfDay),
  489. (int)skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.Star3SpriteRowCountKey, timeOfDay));
  490. }
  491. }
  492. }
  493. if (updateGlobalIllumination)
  494. {
  495. UpdateGlobalIllumination();
  496. }
  497. // Notify delegate after we've completed the sky modifications.
  498. if (timeChangedCallback != null)
  499. {
  500. timeChangedCallback(this, timeOfDay);
  501. }
  502. }
  503. }
  504. }