No Description
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.

TextureGeneratorHelper.cs 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. using System;
  2. using Unity.Collections;
  3. using UnityEditor.AssetImporters;
  4. using UnityEngine;
  5. namespace UnityEditor.U2D.Common
  6. {
  7. internal interface ITextureSettings
  8. {
  9. void FillTextureGenerationSettings(ref TextureGenerationSettings settings);
  10. }
  11. [Serializable]
  12. internal class TextureSettings : ITextureSettings
  13. {
  14. [SerializeField]
  15. bool m_ColorTexture;
  16. [SerializeField]
  17. bool m_Readable;
  18. [SerializeField]
  19. TextureImporterNPOTScale m_NPOTScale;
  20. [SerializeField]
  21. FilterMode m_FilterMode;
  22. [SerializeField]
  23. int m_Aniso;
  24. [SerializeField]
  25. bool m_EnablePostProcessor;
  26. [SerializeField]
  27. SecondarySpriteTexture[] m_SecondaryTextures;
  28. [SerializeField]
  29. TextureImporterSwizzle m_SwizzleR = TextureImporterSwizzle.R;
  30. [SerializeField]
  31. TextureImporterSwizzle m_SwizzleG = TextureImporterSwizzle.G;
  32. [SerializeField]
  33. TextureImporterSwizzle m_SwizzleB = TextureImporterSwizzle.B;
  34. [SerializeField]
  35. TextureImporterSwizzle m_SwizzleA = TextureImporterSwizzle.A;
  36. public TextureSettings()
  37. {
  38. colorTexture = true;
  39. readable = false;
  40. npotScale = TextureImporterNPOTScale.None;
  41. filterMode = FilterMode.Bilinear;
  42. aniso = 1;
  43. }
  44. public TextureSettings(string assetPath, bool enablePostProcessor, bool colorTexture, bool readable, TextureImporterNPOTScale npotScale, FilterMode filterMode, int aniso, bool sourceContainsAlpha, bool sourceWasHDR)
  45. {
  46. this.assetPath = assetPath;
  47. this.enablePostProcessor = enablePostProcessor;
  48. this.colorTexture = colorTexture;
  49. this.readable = readable;
  50. this.npotScale = npotScale;
  51. this.filterMode = filterMode;
  52. this.aniso = aniso;
  53. this.containsAlpha = sourceContainsAlpha;
  54. this.hdr = sourceWasHDR;
  55. }
  56. public bool colorTexture { get { return m_ColorTexture; } set { m_ColorTexture = value; } } //sRGBTexture
  57. public bool readable { get { return m_Readable; } set { m_Readable = value; } }
  58. public TextureImporterNPOTScale npotScale { get { return m_NPOTScale; } set { m_NPOTScale = value; } }
  59. public FilterMode filterMode { get { return m_FilterMode; } set { m_FilterMode = value; } }
  60. public int aniso
  61. {
  62. get { return m_Aniso; }
  63. set { m_Aniso = value; }
  64. }
  65. public bool enablePostProcessor
  66. {
  67. get { return m_EnablePostProcessor; }
  68. set { m_EnablePostProcessor = value; }
  69. }
  70. public string assetPath { get; set; }
  71. public bool containsAlpha { get; set; }
  72. public bool hdr { get; set; }
  73. public TextureImporterSwizzle swizzleR { get { return m_SwizzleR; } set { m_SwizzleR = value; } }
  74. public TextureImporterSwizzle swizzleG { get { return m_SwizzleG; } set { m_SwizzleG = value; } }
  75. public TextureImporterSwizzle swizzleB { get { return m_SwizzleB; } set { m_SwizzleB = value; } }
  76. public TextureImporterSwizzle swizzleA { get { return m_SwizzleA; } set { m_SwizzleA = value; } }
  77. public SecondarySpriteTexture[] secondaryTextures { get { return m_SecondaryTextures;} set { m_SecondaryTextures = value; } }
  78. void ITextureSettings.FillTextureGenerationSettings(ref TextureGenerationSettings settings)
  79. {
  80. settings.textureImporterSettings.sRGBTexture = colorTexture;
  81. settings.textureImporterSettings.readable = readable;
  82. settings.textureImporterSettings.npotScale = npotScale;
  83. settings.textureImporterSettings.filterMode = filterMode;
  84. settings.textureImporterSettings.aniso = aniso;
  85. settings.assetPath = assetPath;
  86. settings.enablePostProcessor = enablePostProcessor;
  87. settings.sourceTextureInformation.containsAlpha = containsAlpha;
  88. settings.sourceTextureInformation.hdr = hdr;
  89. settings.secondarySpriteTextures = secondaryTextures;
  90. settings.textureImporterSettings.swizzleR = m_SwizzleR;
  91. settings.textureImporterSettings.swizzleG = m_SwizzleG;
  92. settings.textureImporterSettings.swizzleB = m_SwizzleB;
  93. settings.textureImporterSettings.swizzleA = m_SwizzleA;
  94. }
  95. }
  96. [Serializable]
  97. internal class TextureSpriteSettings : ITextureSettings
  98. {
  99. [SerializeField]
  100. string m_PackingTag;
  101. public string packingTag
  102. {
  103. get { return m_PackingTag; }
  104. set { m_PackingTag = value; }
  105. }
  106. [SerializeField]
  107. float m_PixelsPerUnit;
  108. public float pixelsPerUnit
  109. {
  110. get { return m_PixelsPerUnit; }
  111. set { m_PixelsPerUnit = value; }
  112. }
  113. [SerializeField]
  114. SpriteMeshType m_MeshType;
  115. public SpriteMeshType meshType
  116. {
  117. get { return m_MeshType; }
  118. set { m_MeshType = value; }
  119. }
  120. [SerializeField]
  121. uint m_ExtrudeEdges;
  122. public uint extrudeEdges
  123. {
  124. get { return m_ExtrudeEdges; }
  125. set { m_ExtrudeEdges = value; }
  126. }
  127. public bool qualifyForPacking { get; set; }
  128. public SpriteImportData[] spriteSheetData { get; set; }
  129. public TextureSpriteSettings()
  130. {
  131. packingTag = "";
  132. pixelsPerUnit = 100;
  133. meshType = SpriteMeshType.Tight;
  134. extrudeEdges = 1;
  135. }
  136. public TextureSpriteSettings(string packingTag, int pixelsPerUnit, SpriteMeshType meshType, uint extrudeEdges, bool qualifyForPacking, SpriteImportData[] spriteSheetData = null)
  137. {
  138. this.packingTag = packingTag;
  139. this.pixelsPerUnit = pixelsPerUnit;
  140. this.meshType = meshType;
  141. this.extrudeEdges = extrudeEdges;
  142. this.qualifyForPacking = qualifyForPacking;
  143. this.spriteSheetData = spriteSheetData;
  144. }
  145. void ITextureSettings.FillTextureGenerationSettings(ref TextureGenerationSettings settings)
  146. {
  147. settings.textureImporterSettings.spritePixelsPerUnit = pixelsPerUnit;
  148. settings.textureImporterSettings.spriteMeshType = meshType;
  149. settings.textureImporterSettings.spriteExtrude = extrudeEdges;
  150. settings.spritePackingTag = packingTag;
  151. settings.qualifyForSpritePacking = qualifyForPacking;
  152. settings.spriteImportData = spriteSheetData;
  153. }
  154. }
  155. [Serializable]
  156. internal class TextureWrapSettings : ITextureSettings
  157. {
  158. [SerializeField]
  159. TextureWrapMode m_WrapMode;
  160. [SerializeField]
  161. TextureWrapMode m_WrapModeU;
  162. [SerializeField]
  163. TextureWrapMode m_WrapModeV;
  164. [SerializeField]
  165. TextureWrapMode m_WrapModeW;
  166. public TextureWrapSettings()
  167. {
  168. wrapMode = wrapModeU = wrapModeV = wrapModeW = TextureWrapMode.Repeat;
  169. }
  170. public TextureWrapSettings(TextureWrapMode wrapMpde, TextureWrapMode wrapModeU, TextureWrapMode wrapModeV, TextureWrapMode wrapModeW)
  171. {
  172. this.wrapMode = wrapMode;
  173. this.wrapModeU = wrapModeU;
  174. this.wrapModeV = wrapModeV;
  175. this.wrapModeW = wrapModeW;
  176. }
  177. public TextureWrapMode wrapMode { get { return m_WrapMode; } set { m_WrapMode = value; } }
  178. public TextureWrapMode wrapModeU { get { return m_WrapModeU; } set { m_WrapModeU = value; } }
  179. public TextureWrapMode wrapModeV { get { return m_WrapModeV; } set { m_WrapModeV = value; } }
  180. public TextureWrapMode wrapModeW { get { return m_WrapModeW; } set { m_WrapModeW = value; } }
  181. void ITextureSettings.FillTextureGenerationSettings(ref TextureGenerationSettings settings)
  182. {
  183. settings.textureImporterSettings.wrapMode = wrapMode;
  184. settings.textureImporterSettings.wrapModeU = wrapModeU;
  185. settings.textureImporterSettings.wrapModeV = wrapModeV;
  186. settings.textureImporterSettings.wrapModeW = wrapModeW;
  187. }
  188. }
  189. [Serializable]
  190. internal class TextureAlphaSettings : ITextureSettings
  191. {
  192. [SerializeField]
  193. float m_AlphaTolerance;
  194. public float alphaTolerance
  195. {
  196. get { return m_AlphaTolerance; }
  197. set { m_AlphaTolerance = value; }
  198. }
  199. [SerializeField]
  200. TextureImporterAlphaSource m_AlphaSource;
  201. public TextureImporterAlphaSource alphaSource
  202. {
  203. get { return m_AlphaSource; }
  204. set { m_AlphaSource = value; }
  205. }
  206. public TextureAlphaSettings()
  207. {
  208. alphaTolerance = 0.5f;
  209. alphaSource = TextureImporterAlphaSource.FromInput;
  210. }
  211. public TextureAlphaSettings(TextureImporterAlphaSource alphaSource, float alphaTolerance)
  212. {
  213. this.alphaTolerance = alphaTolerance;
  214. this.alphaSource = alphaSource;
  215. }
  216. void ITextureSettings.FillTextureGenerationSettings(ref TextureGenerationSettings settings)
  217. {
  218. settings.textureImporterSettings.alphaIsTransparency = alphaSource != TextureImporterAlphaSource.None;
  219. settings.textureImporterSettings.alphaSource = alphaSource;
  220. settings.textureImporterSettings.alphaTestReferenceValue = alphaTolerance;
  221. }
  222. }
  223. [Serializable]
  224. internal class TextureMipmapSettings : ITextureSettings
  225. {
  226. [SerializeField]
  227. TextureImporterMipFilter m_Filter;
  228. public TextureImporterMipFilter filter
  229. {
  230. get { return m_Filter; }
  231. set { m_Filter = value; }
  232. }
  233. [SerializeField]
  234. bool m_BorderMipmap;
  235. public bool borderMipmap
  236. {
  237. get { return m_BorderMipmap; }
  238. set { m_BorderMipmap = value; }
  239. }
  240. [SerializeField]
  241. bool m_Fadeout;
  242. public bool fadeout
  243. {
  244. get { return m_Fadeout; }
  245. set { m_Fadeout = value; }
  246. }
  247. [SerializeField]
  248. bool m_PreserveCoverage;
  249. public bool preserveCoverage
  250. {
  251. get { return m_PreserveCoverage; }
  252. set { m_PreserveCoverage = value; }
  253. }
  254. [SerializeField]
  255. int m_FadeDistanceStart;
  256. public int fadeDistanceStart
  257. {
  258. get { return m_FadeDistanceStart; }
  259. set { m_FadeDistanceStart = value; }
  260. }
  261. [SerializeField]
  262. int m_FadeDistanceEnd;
  263. public int fadeDistanceEnd
  264. {
  265. get { return m_FadeDistanceEnd; }
  266. set { m_FadeDistanceEnd = value; }
  267. }
  268. [SerializeField]
  269. bool m_StreamingMipmaps;
  270. public bool streamingMipmaps
  271. {
  272. get { return m_StreamingMipmaps; }
  273. set { m_StreamingMipmaps = value; }
  274. }
  275. [SerializeField]
  276. int m_StreamingMipmapsPriority;
  277. public int streamingMipmapsPriority
  278. {
  279. get { return m_StreamingMipmapsPriority; }
  280. set { m_StreamingMipmapsPriority = value; }
  281. }
  282. public TextureMipmapSettings()
  283. {
  284. filter = TextureImporterMipFilter.BoxFilter;
  285. borderMipmap = false;
  286. fadeout = false;
  287. preserveCoverage = false;
  288. fadeDistanceStart = 1;
  289. fadeDistanceEnd = 3;
  290. }
  291. public TextureMipmapSettings(TextureImporterMipFilter filter, bool borderMipmap, bool fadeout, bool preserveCoverage, int fadeDistanceStart, int fadeDistanceEnd, bool streamingMipmaps, int streamingMipmapsPriority)
  292. {
  293. this.filter = filter;
  294. this.borderMipmap = borderMipmap;
  295. this.fadeout = fadeout;
  296. this.preserveCoverage = preserveCoverage;
  297. this.fadeDistanceStart = fadeDistanceStart;
  298. this.fadeDistanceEnd = fadeDistanceEnd;
  299. this.streamingMipmaps = streamingMipmaps;
  300. this.streamingMipmapsPriority = streamingMipmapsPriority;
  301. }
  302. void ITextureSettings.FillTextureGenerationSettings(ref TextureGenerationSettings settings)
  303. {
  304. settings.textureImporterSettings.mipmapEnabled = true;
  305. settings.textureImporterSettings.mipmapFilter = filter;
  306. settings.textureImporterSettings.borderMipmap = borderMipmap;
  307. settings.textureImporterSettings.fadeOut = fadeout;
  308. settings.textureImporterSettings.mipmapFadeDistanceStart = fadeDistanceStart;
  309. settings.textureImporterSettings.mipmapFadeDistanceEnd = fadeDistanceEnd;
  310. settings.textureImporterSettings.mipMapsPreserveCoverage = preserveCoverage;
  311. settings.textureImporterSettings.streamingMipmaps = streamingMipmaps;
  312. settings.textureImporterSettings.streamingMipmapsPriority = streamingMipmapsPriority;
  313. }
  314. }
  315. [Serializable]
  316. internal class TextureNormalSettings : ITextureSettings
  317. {
  318. [SerializeField]
  319. TextureImporterNormalFilter m_Filter;
  320. public TextureImporterNormalFilter filter
  321. {
  322. get { return m_Filter; }
  323. set { m_Filter = value; }
  324. }
  325. [SerializeField]
  326. bool m_GenerateFromGrayScale;
  327. public bool generateFromGrayScale
  328. {
  329. get { return m_GenerateFromGrayScale; }
  330. set { m_GenerateFromGrayScale = value; }
  331. }
  332. [SerializeField]
  333. float m_Bumpiness;
  334. public float bumpiness
  335. {
  336. get { return m_Bumpiness; }
  337. set { m_Bumpiness = value; }
  338. }
  339. public TextureNormalSettings()
  340. {
  341. filter = TextureImporterNormalFilter.Standard;
  342. generateFromGrayScale = false;
  343. bumpiness = 0.25f;
  344. }
  345. public TextureNormalSettings(TextureImporterNormalFilter filter, bool generateFromGrayScale, float bumpiness)
  346. {
  347. this.filter = filter;
  348. this.generateFromGrayScale = generateFromGrayScale;
  349. this.bumpiness = bumpiness;
  350. }
  351. void ITextureSettings.FillTextureGenerationSettings(ref TextureGenerationSettings settings)
  352. {
  353. settings.textureImporterSettings.normalMapFilter = filter;
  354. settings.textureImporterSettings.convertToNormalMap = generateFromGrayScale;
  355. settings.textureImporterSettings.heightmapScale = bumpiness;
  356. }
  357. }
  358. // If this is provided, textureType will be cubemap
  359. [Serializable]
  360. internal class TextureCubemapSettings : ITextureSettings
  361. {
  362. [SerializeField]
  363. TextureImporterCubemapConvolution m_Convolution;
  364. public TextureImporterCubemapConvolution convolution
  365. {
  366. get { return m_Convolution; }
  367. set { m_Convolution = value; }
  368. }
  369. [SerializeField]
  370. TextureImporterGenerateCubemap m_Mode;
  371. public TextureImporterGenerateCubemap mode
  372. {
  373. get { return m_Mode; }
  374. set { m_Mode = value; }
  375. }
  376. [SerializeField]
  377. bool m_Seamless;
  378. public bool seamless
  379. {
  380. get { return m_Seamless; }
  381. set { m_Seamless = value; }
  382. }
  383. public TextureCubemapSettings()
  384. {
  385. convolution = TextureImporterCubemapConvolution.None;
  386. mode = TextureImporterGenerateCubemap.AutoCubemap;
  387. seamless = false;
  388. }
  389. public TextureCubemapSettings(TextureImporterCubemapConvolution convolution, TextureImporterGenerateCubemap mode, bool seamless)
  390. {
  391. this.convolution = convolution;
  392. this.mode = mode;
  393. this.seamless = seamless;
  394. }
  395. void ITextureSettings.FillTextureGenerationSettings(ref TextureGenerationSettings settings)
  396. {
  397. settings.textureImporterSettings.textureShape = TextureImporterShape.TextureCube;
  398. settings.textureImporterSettings.cubemapConvolution = convolution;
  399. settings.textureImporterSettings.generateCubemap = mode;
  400. settings.textureImporterSettings.seamlessCubemap = seamless;
  401. }
  402. }
  403. internal static class TextureGeneratorHelper
  404. {
  405. public static TextureGenerationOutput GenerateTextureSprite(NativeArray<Color32> buffer, int bufferWidth, int bufferHeight, TextureSettings settings, TextureImporterPlatformSettings platformSettings,
  406. TextureSpriteSettings spriteSettings, TextureAlphaSettings alphaSettings = null, TextureMipmapSettings mipmapSettings = null, TextureWrapSettings wrapSettings = null)
  407. {
  408. if (alphaSettings == null)
  409. alphaSettings = new TextureAlphaSettings(TextureImporterAlphaSource.FromInput, 0.5f);
  410. if (wrapSettings == null)
  411. wrapSettings = new TextureWrapSettings(TextureWrapMode.Clamp, TextureWrapMode.Clamp, TextureWrapMode.Clamp, TextureWrapMode.Clamp);
  412. return GenerateTexture(buffer, bufferWidth, bufferHeight, TextureImporterType.Sprite, platformSettings, settings, spriteSettings, alphaSettings, mipmapSettings, wrapSettings);
  413. }
  414. public static TextureGenerationOutput GenerateLightmap(NativeArray<Color32> buffer, int bufferWidth, int bufferHeight, TextureSettings settings, TextureImporterPlatformSettings platformSettings,
  415. TextureMipmapSettings mipmapSettings = null, TextureWrapSettings wrapSettings = null)
  416. {
  417. settings.colorTexture = true;
  418. return GenerateTexture(buffer, bufferWidth, bufferHeight, TextureImporterType.Lightmap, platformSettings, settings, mipmapSettings, wrapSettings);
  419. }
  420. public static TextureGenerationOutput GenerateCookie(NativeArray<Color32> buffer, int bufferWidth, int bufferHeight, TextureSettings settings, TextureImporterPlatformSettings platformSettings,
  421. TextureAlphaSettings alphaSettings = null, TextureMipmapSettings mipmapSettings = null, TextureCubemapSettings cubemapSettings = null, TextureWrapSettings wrapSettings = null)
  422. {
  423. return GenerateTexture(buffer, bufferWidth, bufferHeight, TextureImporterType.Cookie, platformSettings, settings, alphaSettings, mipmapSettings, cubemapSettings, wrapSettings);
  424. }
  425. public static TextureGenerationOutput GenerateNormalMap(NativeArray<Color32> buffer, int bufferWidth, int bufferHeight, TextureSettings settings, TextureImporterPlatformSettings platformSettings,
  426. TextureNormalSettings normalSettings, TextureMipmapSettings mipmapSettings = null, TextureCubemapSettings cubemapSettings = null, TextureWrapSettings wrapSettings = null)
  427. {
  428. settings.colorTexture = false;
  429. return GenerateTexture(buffer, bufferWidth, bufferHeight, TextureImporterType.NormalMap, platformSettings, settings, normalSettings, mipmapSettings, cubemapSettings, wrapSettings);
  430. }
  431. public static TextureGenerationOutput GenerateTextureGUI(NativeArray<Color32> buffer, int bufferWidth, int bufferHeight, TextureSettings settings, TextureImporterPlatformSettings platformSettings,
  432. TextureAlphaSettings alphaSettings = null, TextureMipmapSettings mipmapSettings = null, TextureWrapSettings wrapSettings = null)
  433. {
  434. settings.colorTexture = false;
  435. if (wrapSettings == null)
  436. wrapSettings = new TextureWrapSettings(TextureWrapMode.Clamp, TextureWrapMode.Clamp, TextureWrapMode.Clamp, TextureWrapMode.Clamp);
  437. return GenerateTexture(buffer, bufferWidth, bufferHeight, TextureImporterType.GUI, platformSettings, settings, alphaSettings, mipmapSettings, wrapSettings);
  438. }
  439. public static TextureGenerationOutput GenerateTextureSingleChannel(NativeArray<Color32> buffer, int bufferWidth, int bufferHeight, TextureSettings settings, TextureImporterPlatformSettings platformSettings,
  440. TextureAlphaSettings alphaSettings = null, TextureMipmapSettings mipmapSettings = null, TextureCubemapSettings cubemapSettings = null, TextureWrapSettings wrapSettings = null)
  441. {
  442. settings.colorTexture = false;
  443. return GenerateTexture(buffer, bufferWidth, bufferHeight, TextureImporterType.SingleChannel, platformSettings, settings, alphaSettings, mipmapSettings, cubemapSettings, wrapSettings);
  444. }
  445. public static TextureGenerationOutput GenerateTextureCursor(NativeArray<Color32> buffer, int bufferWidth, int bufferHeight, TextureSettings settings, TextureImporterPlatformSettings platformSettings,
  446. TextureAlphaSettings alphaSettings = null, TextureMipmapSettings mipmapSettings = null, TextureWrapSettings wrapSettings = null)
  447. {
  448. if (alphaSettings == null)
  449. alphaSettings = new TextureAlphaSettings(TextureImporterAlphaSource.FromInput, 0.5f);
  450. if (wrapSettings == null)
  451. wrapSettings = new TextureWrapSettings(TextureWrapMode.Clamp, TextureWrapMode.Clamp, TextureWrapMode.Clamp, TextureWrapMode.Clamp);
  452. return GenerateTexture(buffer, bufferWidth, bufferHeight, TextureImporterType.Cursor, platformSettings, settings, alphaSettings, mipmapSettings, wrapSettings);
  453. }
  454. public static TextureGenerationOutput GenerateTextureDefault(NativeArray<Color32> buffer, int bufferWidth, int bufferHeight, TextureSettings settings, TextureImporterPlatformSettings platformSettings,
  455. TextureAlphaSettings alphaSettings = null, TextureMipmapSettings mipmapSettings = null, TextureCubemapSettings cubemapSettings = null, TextureWrapSettings wrapSettings = null)
  456. {
  457. if (mipmapSettings == null)
  458. mipmapSettings = new TextureMipmapSettings(TextureImporterMipFilter.BoxFilter, false, false, false, 1, 3, false, 0);
  459. return GenerateTexture(buffer, bufferWidth, bufferHeight, TextureImporterType.Default, platformSettings, settings, alphaSettings, mipmapSettings, cubemapSettings, wrapSettings);
  460. }
  461. static TextureGenerationOutput GenerateTexture(NativeArray<Color32> imageBuffer, int imageBufferWidth, int imageBufferHeight, TextureImporterType type, TextureImporterPlatformSettings platformSettings, params ITextureSettings[] otherSettings)
  462. {
  463. var textureGenerationSettings = new TextureGenerationSettings();
  464. textureGenerationSettings.platformSettings = platformSettings;
  465. textureGenerationSettings.sourceTextureInformation = new SourceTextureInformation();
  466. textureGenerationSettings.sourceTextureInformation.height = imageBufferHeight;
  467. textureGenerationSettings.sourceTextureInformation.width = imageBufferWidth;
  468. textureGenerationSettings.textureImporterSettings = new TextureImporterSettings();
  469. textureGenerationSettings.textureImporterSettings.textureType = type;
  470. textureGenerationSettings.textureImporterSettings.textureShape = TextureImporterShape.Texture2D;
  471. textureGenerationSettings.textureImporterSettings.alphaIsTransparency = false;
  472. textureGenerationSettings.textureImporterSettings.convertToNormalMap = false;
  473. textureGenerationSettings.textureImporterSettings.mipmapEnabled = false;
  474. textureGenerationSettings.textureImporterSettings.sRGBTexture = true;
  475. textureGenerationSettings.textureImporterSettings.readable = false;
  476. textureGenerationSettings.textureImporterSettings.fadeOut = false;
  477. textureGenerationSettings.textureImporterSettings.wrapMode = TextureWrapMode.Repeat;
  478. textureGenerationSettings.textureImporterSettings.wrapModeU = TextureWrapMode.Repeat;
  479. textureGenerationSettings.textureImporterSettings.wrapModeV = TextureWrapMode.Repeat;
  480. textureGenerationSettings.textureImporterSettings.wrapModeW = TextureWrapMode.Repeat;
  481. textureGenerationSettings.textureImporterSettings.swizzleR = TextureImporterSwizzle.R;
  482. textureGenerationSettings.textureImporterSettings.swizzleG = TextureImporterSwizzle.G;
  483. textureGenerationSettings.textureImporterSettings.swizzleB = TextureImporterSwizzle.B;
  484. textureGenerationSettings.textureImporterSettings.swizzleA = TextureImporterSwizzle.A;
  485. foreach (var otherSetting in otherSettings)
  486. {
  487. if (otherSetting != null)
  488. otherSetting.FillTextureGenerationSettings(ref textureGenerationSettings);
  489. }
  490. return TextureGenerator.GenerateTexture(textureGenerationSettings, imageBuffer);
  491. }
  492. static public TextureSettings ExtractTextureSettings(this TextureImporterSettings tis)
  493. {
  494. var ts = new TextureSettings();
  495. ts.colorTexture = tis.sRGBTexture;
  496. ts.readable = tis.readable;
  497. ts.npotScale = tis.npotScale;
  498. ts.filterMode = tis.filterMode;
  499. ts.aniso = tis.aniso;
  500. ts.swizzleR = tis.swizzleR;
  501. ts.swizzleG = tis.swizzleG;
  502. ts.swizzleB = tis.swizzleB;
  503. ts.swizzleA = tis.swizzleA;
  504. return ts;
  505. }
  506. static public TextureSpriteSettings ExtractTextureSpriteSettings(this TextureImporterSettings tis)
  507. {
  508. var ts = new TextureSpriteSettings();
  509. ts.pixelsPerUnit = tis.spritePixelsPerUnit;
  510. ts.meshType = tis.spriteMeshType;
  511. ts.extrudeEdges = tis.spriteExtrude;
  512. return ts;
  513. }
  514. static public TextureWrapSettings ExtractTextureWrapSettings(this TextureImporterSettings tis)
  515. {
  516. var ts = new TextureWrapSettings();
  517. ts.wrapMode = tis.wrapMode;
  518. ts.wrapModeU = tis.wrapModeU;
  519. ts.wrapModeV = tis.wrapModeV;
  520. ts.wrapModeW = tis.wrapModeW;
  521. return ts;
  522. }
  523. static public TextureAlphaSettings ExtractTextureAlphaSettings(this TextureImporterSettings settings)
  524. {
  525. if (settings.alphaIsTransparency == false)
  526. return null;
  527. var ts = new TextureAlphaSettings();
  528. ts.alphaSource = settings.alphaSource;
  529. ts.alphaTolerance = settings.alphaTestReferenceValue;
  530. return ts;
  531. }
  532. static public TextureMipmapSettings ExtractTextureMipmapSettings(this TextureImporterSettings settings)
  533. {
  534. if (!settings.mipmapEnabled)
  535. return null;
  536. var ts = new TextureMipmapSettings();
  537. ts.filter = settings.mipmapFilter;
  538. ts.borderMipmap = settings.borderMipmap;
  539. ts.fadeout = settings.fadeOut;
  540. ts.fadeDistanceStart = settings.mipmapFadeDistanceStart;
  541. ts.fadeDistanceEnd = settings.mipmapFadeDistanceEnd;
  542. ts.preserveCoverage = settings.mipMapsPreserveCoverage;
  543. ts.streamingMipmaps = settings.streamingMipmaps;
  544. ts.streamingMipmapsPriority = settings.streamingMipmapsPriority;
  545. return ts;
  546. }
  547. static public TextureNormalSettings ExtractTextureNormalSettings(this TextureImporterSettings settings)
  548. {
  549. var ts = new TextureNormalSettings();
  550. ts.filter = settings.normalMapFilter;
  551. ts.generateFromGrayScale = settings.convertToNormalMap;
  552. ts.bumpiness = settings.heightmapScale;
  553. return ts;
  554. }
  555. static public TextureCubemapSettings ExtractTextureCubemapSettings(this TextureImporterSettings settings)
  556. {
  557. if (settings.textureShape != TextureImporterShape.TextureCube)
  558. return null;
  559. var ts = new TextureCubemapSettings();
  560. ts.convolution = settings.cubemapConvolution;
  561. ts.mode = settings.generateCubemap;
  562. ts.seamless = settings.seamlessCubemap;
  563. return ts;
  564. }
  565. }
  566. }