Noise3D.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #define LOAD_NOISE3D_ON_STARTUP // prevent loading spike from happening when the 1st beam is instantiated
  2. using UnityEngine;
  3. #pragma warning disable 0429, 0162 // Unreachable expression code detected (because of Noise3D.isSupported on mobile)
  4. namespace VLB
  5. {
  6. public static class Noise3D
  7. {
  8. /// <summary>
  9. /// Returns if the 3D Noise feature is supported on the current platform or not.
  10. /// 3D Noise feature requires a graphicsShaderLevel 35 or higher (which is basically Shader Model 3.5 / OpenGL ES 3.0 or above)
  11. /// If not supported, the beams will look like the 3D Noise has been disabled.
  12. /// </summary>
  13. public static bool isSupported {
  14. get {
  15. if (!ms_IsSupportedChecked)
  16. {
  17. ms_IsSupported = SystemInfo.graphicsShaderLevel >= kMinShaderLevel;
  18. if (!ms_IsSupported)
  19. Debug.LogWarning(isNotSupportedString);
  20. ms_IsSupportedChecked = true;
  21. }
  22. return ms_IsSupported;
  23. }
  24. }
  25. /// <summary>
  26. /// Returns if the 3D Noise Texture has been successfully loaded or not.
  27. /// If the feature is not supported (isSupported == false), isProperlyLoaded is also false.
  28. /// </summary>
  29. public static bool isProperlyLoaded { get { return ms_NoiseTexture != null; } }
  30. public static string isNotSupportedString { get {
  31. var str = string.Format("3D Noise requires higher shader capabilities (Shader Model 3.5 / OpenGL ES 3.0), which are not available on the current platform: graphicsShaderLevel (current/required) = {0} / {1}",
  32. SystemInfo.graphicsShaderLevel,
  33. kMinShaderLevel);
  34. #if UNITY_EDITOR
  35. str += "\nPlease change the editor's graphics emulation for a more capable one via \"Edit/Graphics Emulation\" and press Play to force the light beams to be recomputed.";
  36. #endif
  37. return str;
  38. }
  39. }
  40. static bool ms_IsSupportedChecked = false;
  41. static bool ms_IsSupported = false;
  42. static Texture3D ms_NoiseTexture = null;
  43. const HideFlags kHideFlags = HideFlags.HideAndDontSave; // hide the noise texture
  44. const int kMinShaderLevel = 35; // Shader Model 3.5 / OpenGL ES 3.0 to handle sampler3D -> https://docs.unity3d.com/ScriptReference/SystemInfo-graphicsShaderLevel.html
  45. #if LOAD_NOISE3D_ON_STARTUP
  46. [RuntimeInitializeOnLoadMethod]
  47. static void OnStartUp()
  48. {
  49. LoadIfNeeded();
  50. }
  51. #endif
  52. #if UNITY_EDITOR
  53. public static void _EditorForceReloadData()
  54. {
  55. if (ms_NoiseTexture)
  56. {
  57. Object.DestroyImmediate(ms_NoiseTexture);
  58. ms_NoiseTexture = null;
  59. }
  60. LoadIfNeeded();
  61. }
  62. #endif
  63. public static void LoadIfNeeded()
  64. {
  65. if (!isSupported) return;
  66. if (ms_NoiseTexture == null)
  67. {
  68. ms_NoiseTexture = LoadTexture3D(Config.Instance.noise3DData, Config.Instance.noise3DSize);
  69. if(ms_NoiseTexture)
  70. ms_NoiseTexture.hideFlags = kHideFlags;
  71. Shader.SetGlobalTexture(ShaderProperties.GlobalNoiseTex3D, ms_NoiseTexture);
  72. Shader.SetGlobalFloat(ShaderProperties.GlobalNoiseCustomTime, -1.0f);
  73. }
  74. }
  75. static Texture3D LoadTexture3D(TextAsset textData, int size)
  76. {
  77. if (textData == null)
  78. {
  79. // Debug.LogError("Fail to open Noise 3D Data");
  80. return null;
  81. }
  82. var bytes = textData.bytes;
  83. Debug.Assert(bytes != null);
  84. int dataLen = Mathf.Max(0, size * size * size);
  85. if (bytes.Length != dataLen)
  86. {
  87. Debug.LogErrorFormat("Noise 3D Data file has not the proper size {0}x{0}x{0}", size);
  88. return null;
  89. }
  90. var tex = new Texture3D(size, size, size, TextureFormat.Alpha8, false);
  91. var colors = new Color[dataLen];
  92. for (int i = 0; i < dataLen; ++i)
  93. colors[i] = new Color32(0, 0, 0, bytes[i]);
  94. tex.SetPixels(colors);
  95. tex.Apply();
  96. return tex;
  97. }
  98. }
  99. }