SkyBuilder.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. using System;
  2. using System.IO;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using UnityEditor;
  7. using UnityEngine.SceneManagement;
  8. namespace Funly.SkyStudio
  9. {
  10. public class SkyBuilder : System.Object
  11. {
  12. public SkyProfile profile;
  13. public Material skyboxMaterial;
  14. public bool starLayer1Enabled;
  15. public bool starLayer2Enabled;
  16. public bool starLayer3Enabled;
  17. public float starLayer1Density;
  18. public float starLayer2Density;
  19. public float starLayer3Density;
  20. public float starLayer1MaxRadius;
  21. public float starLayer2MaxRadius;
  22. public float starLayer3MaxRadius;
  23. private int m_ImageSize = 256;
  24. private int m_BusyRenderingCount;
  25. private bool m_IsCancelled;
  26. public bool IsComplete
  27. {
  28. get { return m_BusyRenderingCount == 0; }
  29. }
  30. public delegate void SkyBuilderProgressCallback(SkyBuilder builder, float progress);
  31. public event SkyBuilderProgressCallback progressCallback;
  32. public delegate void SkyBuilderCompletionCallback(SkyBuilder builder, bool success);
  33. public event SkyBuilderCompletionCallback completionCallback;
  34. private List<BaseStarDataRenderer> m_Renderers = new List<BaseStarDataRenderer>();
  35. // Stars and async task to rebuild the skyColors system data.
  36. public void BuildSkySystem()
  37. {
  38. m_Renderers.Clear();
  39. if (skyboxMaterial == null) {
  40. Debug.LogError("Can't build skyColors system without skybox material.");
  41. return;
  42. }
  43. m_BusyRenderingCount = CountToRender();
  44. if (starLayer1Enabled) {
  45. RebuildStarLayer("1",
  46. starLayer1Density,
  47. starLayer1MaxRadius);
  48. }
  49. if (starLayer2Enabled) {
  50. RebuildStarLayer("2",
  51. starLayer2Density,
  52. starLayer2MaxRadius);
  53. }
  54. if (starLayer3Enabled) {
  55. RebuildStarLayer("3",
  56. starLayer3Density,
  57. starLayer3MaxRadius);
  58. }
  59. }
  60. public string TextureNameForStarLayer(int layerId)
  61. {
  62. return TextureNameForStarLayer(layerId.ToString());
  63. }
  64. public string TextureNameForStarLayer(string layerId)
  65. {
  66. return "StarData" + layerId.ToString();
  67. }
  68. public void CancelBuild()
  69. {
  70. m_IsCancelled = true;
  71. completionCallback = null;
  72. progressCallback = null;
  73. foreach (BaseStarDataRenderer r in m_Renderers)
  74. {
  75. r.Cancel();
  76. }
  77. m_Renderers.Clear();
  78. }
  79. int CountToRender()
  80. {
  81. int count = 0;
  82. if (starLayer1Enabled) {
  83. count += 1;
  84. }
  85. if (starLayer2Enabled) {
  86. count += 1;
  87. }
  88. if (starLayer3Enabled) {
  89. count += 1;
  90. }
  91. return count;
  92. }
  93. void RebuildStarLayer(string layerId, float density, float radius)
  94. {
  95. BaseStarDataRenderer renderer = GetBestRendererForPlatform();
  96. renderer.layerId = layerId;
  97. renderer.imageSize = m_ImageSize;
  98. renderer.density = density;
  99. renderer.maxRadius = radius;
  100. renderer.completionCallback += OnStarRenderingComplete;
  101. renderer.progressCallback += OnStarRenderingProgress;
  102. m_Renderers.Add(renderer);
  103. EditorCoroutine.start(renderer.ComputeStarData());
  104. }
  105. private BaseStarDataRenderer GetBestRendererForPlatform()
  106. {
  107. return new NearbyStarRenderer();
  108. }
  109. // Remove an texture associated with a material.
  110. private void RemoveAllObjectsWithName(string textureName, Material mat)
  111. {
  112. string assetPath = AssetDatabase.GetAssetPath(skyboxMaterial);
  113. UnityEngine.Object[] objs = AssetDatabase.LoadAllAssetsAtPath(assetPath);
  114. foreach (UnityEngine.Object obj in objs) {
  115. if (obj == null) {
  116. continue;
  117. }
  118. if (obj.name == textureName) {
  119. UnityEngine.Object.DestroyImmediate(obj, true);
  120. }
  121. }
  122. AssetDatabase.ImportAsset(assetPath);
  123. }
  124. private void OnStarRenderingProgress(BaseStarDataRenderer renderer, float progress)
  125. {
  126. if (progressCallback != null) {
  127. progressCallback(this, progress);
  128. }
  129. }
  130. private void OnStarRenderingComplete(BaseStarDataRenderer renderer, Texture2D texture, bool success)
  131. {
  132. if (m_IsCancelled)
  133. {
  134. m_BusyRenderingCount = 0;
  135. return;
  136. }
  137. texture.name = TextureNameForStarLayer(renderer.layerId);
  138. SkyEditorUtility.AddSkyResource(profile, texture);
  139. AssetDatabase.Refresh();
  140. if (renderer.layerId == "1") {
  141. profile.starLayer1DataTexture = texture;
  142. } else if (renderer.layerId == "2") {
  143. profile.starLayer2DataTexture = texture;
  144. } else if (renderer.layerId == "3") {
  145. profile.starLayer3DataTexture = texture;
  146. }
  147. m_BusyRenderingCount -= 1;
  148. if (completionCallback != null) {
  149. completionCallback(this, true);
  150. }
  151. }
  152. }
  153. }