SW 중심대학 OSS GIT 서버 박건태, 이승준, 고기완, 이준호 새로운 배포
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
5.2 KiB

4 years ago
  1. using System;
  2. using System.IO;
  3. using UnityEngine;
  4. using UnityEngine.XR.ARSubsystems;
  5. namespace UnityEditor.XR.ARKit
  6. {
  7. /// <summary>
  8. /// Represents an AR Reference Image in an Xcode asset catalog. This is the
  9. /// Xcode representation of a UnityEngine.XR.ARSubsystems.XRReferenceImage.
  10. /// </summary>
  11. internal class ARReferenceImage : ARResource
  12. {
  13. public class InvalidWidthException : Exception {}
  14. public class BadTexturePathException : Exception {}
  15. public class MissingTextureException : Exception {}
  16. public class TextureNotExportableException : Exception {}
  17. public class LoadTextureException : Exception
  18. {
  19. public string path { get; private set; }
  20. public LoadTextureException(string path)
  21. {
  22. this.path = path;
  23. }
  24. }
  25. public override string extension
  26. {
  27. get { return "arreferenceimage"; }
  28. }
  29. public override void Write(string pathToResourceGroup)
  30. {
  31. // Create the ARReferenceImage in Xcode
  32. var pathToReferenceImage = Path.Combine(pathToResourceGroup, filename);
  33. Directory.CreateDirectory(pathToReferenceImage);
  34. // Copy or create the texture as a file in Xcode
  35. var textureFilename = Path.GetFileName(m_TexturePath);
  36. string destinationPath = Path.Combine(pathToReferenceImage, textureFilename);
  37. if (m_ImageBytes == null)
  38. {
  39. File.Copy(m_TexturePath, destinationPath, true);
  40. }
  41. else
  42. {
  43. var textureImporter = AssetImporter.GetAtPath(m_TexturePath) as TextureImporter;
  44. if (textureImporter != null)
  45. {
  46. if (textureImporter.npotScale != TextureImporterNPOTScale.None)
  47. {
  48. Debug.LogWarningFormat(
  49. "The import settings for texture at {0} cause its dimensions to be rounded to a power of two. " +
  50. "To use this texture with ARKit, it will be exported to a PNG with the rounded dimensions. " +
  51. "This may result in unexpected behavior. " +
  52. "You can change this on the texture's import settings under Advanced > Non Power of 2, or " +
  53. "use a JPG or PNG, since those formats can be used directly rather than exported with the texture importer settings.",
  54. m_TexturePath);
  55. }
  56. }
  57. // If the image is some other format, then attempt to convert it to a PNG
  58. textureFilename = Path.ChangeExtension(textureFilename, ".png");
  59. destinationPath = Path.ChangeExtension(destinationPath, ".png");
  60. // m_ImageBytes was read in the constructor
  61. File.WriteAllBytes(destinationPath, m_ImageBytes);
  62. }
  63. // Create the Contents.json
  64. var contents = new Json.ReferenceImage
  65. {
  66. info = new Json.AuthorInfo
  67. {
  68. version = 1,
  69. author = "unity"
  70. },
  71. images = new Json.FilenameWithIdiom[]
  72. {
  73. new Json.FilenameWithIdiom
  74. {
  75. filename = textureFilename,
  76. idiom = "universal"
  77. }
  78. },
  79. properties = new Json.ImageProperties
  80. {
  81. width = m_Width
  82. }
  83. };
  84. File.WriteAllText(Path.Combine(pathToReferenceImage, "Contents.json"), JsonUtility.ToJson(contents));
  85. }
  86. public ARReferenceImage(XRReferenceImage referenceImage)
  87. {
  88. if (!referenceImage.specifySize || (referenceImage.width <= 0f))
  89. throw new InvalidWidthException();
  90. if (referenceImage.textureGuid.Equals(Guid.Empty))
  91. throw new MissingTextureException();
  92. var textureGuid = referenceImage.textureGuid.ToString("N");
  93. m_TexturePath = AssetDatabase.GUIDToAssetPath(textureGuid);
  94. if (string.IsNullOrEmpty(m_TexturePath))
  95. throw new BadTexturePathException();
  96. m_Texture = AssetDatabase.LoadAssetAtPath<Texture2D>(m_TexturePath) as Texture2D;
  97. if (m_Texture == null)
  98. throw new LoadTextureException(m_TexturePath);
  99. var textureExtension = Path.GetExtension(m_TexturePath);
  100. if (!string.Equals(textureExtension, ".jpg", StringComparison.OrdinalIgnoreCase) &&
  101. !string.Equals(textureExtension, ".png", StringComparison.OrdinalIgnoreCase))
  102. {
  103. // Attempt to read the bytes
  104. m_ImageBytes = ImageConversion.EncodeToPNG(m_Texture);
  105. if (m_ImageBytes == null)
  106. throw new TextureNotExportableException();
  107. }
  108. m_Width = referenceImage.width;
  109. name = referenceImage.name + "_" + referenceImage.guid.ToUUIDString();
  110. }
  111. byte[] m_ImageBytes;
  112. float m_Width;
  113. string m_TexturePath;
  114. Texture2D m_Texture;
  115. }
  116. }