VXUtils.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright (c) 2022 Vuplex Inc. All rights reserved.
  2. //
  3. // Licensed under the Vuplex Commercial Software Library License, you may
  4. // not use this file except in compliance with the License. You may obtain
  5. // a copy of the License at
  6. //
  7. // https://vuplex.com/commercial-library-license
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. using System;
  15. using System.Collections.Generic;
  16. using System.Linq;
  17. using UnityEngine;
  18. using UnityEngine.Rendering;
  19. namespace Vuplex.WebView.Internal {
  20. /// <summary>
  21. /// Static utility methods used internally by 3D WebView.
  22. /// </summary>
  23. /// <remarks>
  24. /// This class used to be named Utils, but since Utils is a common class name,
  25. /// if the user's project contained a class named Utils in the global namespace,
  26. /// it would break the 3D WebView classes that use this class.
  27. /// Similarly, the XR-related methods used to be in a class named XRUtils, but Unity added
  28. /// an XRUtils class to the UnityEngine.Rendering namespace, which led to ambiguous references.
  29. /// </remarks>
  30. public static class VXUtils {
  31. public static Material CreateDefaultMaterial() {
  32. // Construct a new material, because Resources.Load<T>() returns a singleton.
  33. return new Material(Resources.Load<Material>("DefaultViewportMaterial"));
  34. }
  35. public static Texture2D CreateDefaultTexture(int width, int height) {
  36. VXUtils.ThrowExceptionIfAbnormallyLarge(width, height);
  37. var texture = new Texture2D(
  38. width,
  39. height,
  40. TextureFormat.RGBA32,
  41. false,
  42. false
  43. );
  44. #if UNITY_2020_2_OR_NEWER
  45. // In Unity 2020.2, Unity's internal TexturesD3D11.cpp class on Windows logs an error if
  46. // UpdateExternalTexture() is called on a Texture2D created from the constructor
  47. // rather than from Texture2D.CreateExternalTexture(). So, rather than returning
  48. // the original Texture2D created via the constructor, we return a copy created
  49. // via CreateExternalTexture(). This approach is only used for 2020.2 and newer because
  50. // it doesn't work in 2018.4 and instead causes a crash.
  51. texture = Texture2D.CreateExternalTexture(
  52. width,
  53. height,
  54. TextureFormat.RGBA32,
  55. false,
  56. false,
  57. texture.GetNativeTexturePtr()
  58. );
  59. #endif
  60. return texture;
  61. }
  62. public static string GetGraphicsApiErrorMessage(GraphicsDeviceType activeGraphicsApi, GraphicsDeviceType[] acceptableGraphicsApis) {
  63. var isValid = Array.IndexOf(acceptableGraphicsApis, activeGraphicsApi) != -1;
  64. if (isValid) {
  65. return null;
  66. }
  67. var acceptableApiStrings = acceptableGraphicsApis.ToList().Select(api => api.ToString());
  68. var acceptableApisList = String.Join(" or ", acceptableApiStrings.ToArray());
  69. return $"Unsupported graphics API: Vuplex 3D WebView requires {acceptableApisList} for this platform, but the selected graphics API is {activeGraphicsApi}. Please go to Player Settings and set \"Graphics APIs\" to {acceptableApisList}.";
  70. }
  71. public static void LogNative2DModeWarning(string methodName) {
  72. WebViewLogger.LogWarning(methodName + "() was called but will be ignored because it is not supported in Native 2D Mode.");
  73. }
  74. public static void ThrowExceptionIfAbnormallyLarge(int width, int height) {
  75. // Anything over 19.4 megapixels (6k) is almost certainly a mistake.
  76. // Cast to floats to avoid integer overflow.
  77. if ((float)width * (float)height > 19400000) {
  78. var message = $"The application specified an abnormally large webview size ({width}px x {height}px), and webviews of this size are normally only created by mistake. A WebViewPrefab's default resolution is 1300px per Unity unit, so it's likely that you specified a large physical size by mistake or need to adjust the resolution. For more information, please see WebViewPrefab.Resolution: https://developer.vuplex.com/webview/WebViewPrefab#Resolution .";
  79. #if VUPLEX_ALLOW_LARGE_WEBVIEWS
  80. WebViewLogger.LogWarning(message);
  81. #else
  82. throw new ArgumentException(message + " If this large webview size is intentional, you can disable this exception by adding the scripting symbol VUPLEX_ALLOW_LARGE_WEBVIEWS to player settings. However, please note that if the webview size is larger than the graphics system can handle, the app may crash.");
  83. #endif
  84. }
  85. }
  86. public static XRSettingsWrapper XRSettings { get { return XRSettingsWrapper.Instance; }}
  87. }
  88. }