Brak opisu
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 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  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. public TextureMipmapSettings()
  269. {
  270. filter = TextureImporterMipFilter.BoxFilter;
  271. borderMipmap = false;
  272. fadeout = false;
  273. preserveCoverage = false;
  274. fadeDistanceStart = 1;
  275. fadeDistanceEnd = 3;
  276. }
  277. public TextureMipmapSettings(TextureImporterMipFilter filter, bool borderMipmap, bool fadeout, bool preserveCoverage, int fadeDistanceStart, int fadeDistanceEnd)
  278. {
  279. this.filter = filter;
  280. this.borderMipmap = borderMipmap;
  281. this.fadeout = fadeout;
  282. this.preserveCoverage = preserveCoverage;
  283. this.fadeDistanceStart = fadeDistanceStart;
  284. this.fadeDistanceEnd = fadeDistanceEnd;
  285. }
  286. void ITextureSettings.FillTextureGenerationSettings(ref TextureGenerationSettings settings)
  287. {
  288. settings.textureImporterSettings.mipmapEnabled = true;
  289. settings.textureImporterSettings.mipmapFilter = filter;
  290. settings.textureImporterSettings.borderMipmap = borderMipmap;
  291. settings.textureImporterSettings.fadeOut = fadeout;
  292. settings.textureImporterSettings.mipmapFadeDistanceStart = fadeDistanceStart;
  293. settings.textureImporterSettings.mipmapFadeDistanceEnd = fadeDistanceEnd;
  294. settings.textureImporterSettings.mipMapsPreserveCoverage = preserveCoverage;
  295. }
  296. }
  297. [Serializable]
  298. internal class TextureNormalSettings : ITextureSettings
  299. {
  300. [SerializeField]
  301. TextureImporterNormalFilter m_Filter;
  302. public TextureImporterNormalFilter filter
  303. {
  304. get { return m_Filter; }
  305. set { m_Filter = value; }
  306. }
  307. [SerializeField]
  308. bool m_GenerateFromGrayScale;
  309. public bool generateFromGrayScale
  310. {
  311. get { return m_GenerateFromGrayScale; }
  312. set { m_GenerateFromGrayScale = value; }
  313. }
  314. [SerializeField]
  315. float m_Bumpiness;
  316. public float bumpiness
  317. {
  318. get { return m_Bumpiness; }
  319. set { m_Bumpiness = value; }
  320. }
  321. public TextureNormalSettings()
  322. {
  323. filter = TextureImporterNormalFilter.Standard;
  324. generateFromGrayScale = false;
  325. bumpiness = 0.25f;
  326. }
  327. public TextureNormalSettings(TextureImporterNormalFilter filter, bool generateFromGrayScale, float bumpiness)
  328. {
  329. this.filter = filter;
  330. this.generateFromGrayScale = generateFromGrayScale;
  331. this.bumpiness = bumpiness;
  332. }
  333. void ITextureSettings.FillTextureGenerationSettings(ref TextureGenerationSettings settings)
  334. {
  335. settings.textureImporterSettings.normalMapFilter = filter;
  336. settings.textureImporterSettings.convertToNormalMap = generateFromGrayScale;
  337. settings.textureImporterSettings.heightmapScale = bumpiness;
  338. }
  339. }
  340. // If this is provided, textureType will be cubemap
  341. [Serializable]
  342. internal class TextureCubemapSettings : ITextureSettings
  343. {
  344. [SerializeField]
  345. TextureImporterCubemapConvolution m_Convolution;
  346. public TextureImporterCubemapConvolution convolution
  347. {
  348. get { return m_Convolution; }
  349. set { m_Convolution = value; }
  350. }
  351. [SerializeField]
  352. TextureImporterGenerateCubemap m_Mode;
  353. public TextureImporterGenerateCubemap mode
  354. {
  355. get { return m_Mode; }
  356. set { m_Mode = value; }
  357. }
  358. [SerializeField]
  359. bool m_Seamless;
  360. public bool seamless
  361. {
  362. get { return m_Seamless; }
  363. set { m_Seamless = value; }
  364. }
  365. public TextureCubemapSettings()
  366. {
  367. convolution = TextureImporterCubemapConvolution.None;
  368. mode = TextureImporterGenerateCubemap.AutoCubemap;
  369. seamless = false;
  370. }
  371. public TextureCubemapSettings(TextureImporterCubemapConvolution convolution, TextureImporterGenerateCubemap mode, bool seamless)
  372. {
  373. this.convolution = convolution;
  374. this.mode = mode;
  375. this.seamless = seamless;
  376. }
  377. void ITextureSettings.FillTextureGenerationSettings(ref TextureGenerationSettings settings)
  378. {
  379. settings.textureImporterSettings.textureShape = TextureImporterShape.TextureCube;
  380. settings.textureImporterSettings.cubemapConvolution = convolution;
  381. settings.textureImporterSettings.generateCubemap = mode;
  382. settings.textureImporterSettings.seamlessCubemap = seamless;
  383. }
  384. }
  385. internal static class TextureGeneratorHelper
  386. {
  387. public static TextureGenerationOutput GenerateTextureSprite(NativeArray<Color32> buffer, int bufferWidth, int bufferHeight, TextureSettings settings, TextureImporterPlatformSettings platformSettings,
  388. TextureSpriteSettings spriteSettings, TextureAlphaSettings alphaSettings = null, TextureMipmapSettings mipmapSettings = null, TextureWrapSettings wrapSettings = null)
  389. {
  390. if (alphaSettings == null)
  391. alphaSettings = new TextureAlphaSettings(TextureImporterAlphaSource.FromInput, 0.5f);
  392. if (wrapSettings == null)
  393. wrapSettings = new TextureWrapSettings(TextureWrapMode.Clamp, TextureWrapMode.Clamp, TextureWrapMode.Clamp, TextureWrapMode.Clamp);
  394. return GenerateTexture(buffer, bufferWidth, bufferHeight, TextureImporterType.Sprite, platformSettings, settings, spriteSettings, alphaSettings, mipmapSettings, wrapSettings);
  395. }
  396. public static TextureGenerationOutput GenerateLightmap(NativeArray<Color32> buffer, int bufferWidth, int bufferHeight, TextureSettings settings, TextureImporterPlatformSettings platformSettings,
  397. TextureMipmapSettings mipmapSettings = null, TextureWrapSettings wrapSettings = null)
  398. {
  399. settings.colorTexture = true;
  400. return GenerateTexture(buffer, bufferWidth, bufferHeight, TextureImporterType.Lightmap, platformSettings, settings, mipmapSettings, wrapSettings);
  401. }
  402. public static TextureGenerationOutput GenerateCookie(NativeArray<Color32> buffer, int bufferWidth, int bufferHeight, TextureSettings settings, TextureImporterPlatformSettings platformSettings,
  403. TextureAlphaSettings alphaSettings = null, TextureMipmapSettings mipmapSettings = null, TextureCubemapSettings cubemapSettings = null, TextureWrapSettings wrapSettings = null)
  404. {
  405. return GenerateTexture(buffer, bufferWidth, bufferHeight, TextureImporterType.Cookie, platformSettings, settings, alphaSettings, mipmapSettings, cubemapSettings, wrapSettings);
  406. }
  407. public static TextureGenerationOutput GenerateNormalMap(NativeArray<Color32> buffer, int bufferWidth, int bufferHeight, TextureSettings settings, TextureImporterPlatformSettings platformSettings,
  408. TextureNormalSettings normalSettings, TextureMipmapSettings mipmapSettings = null, TextureCubemapSettings cubemapSettings = null, TextureWrapSettings wrapSettings = null)
  409. {
  410. settings.colorTexture = false;
  411. return GenerateTexture(buffer, bufferWidth, bufferHeight, TextureImporterType.NormalMap, platformSettings, settings, normalSettings, mipmapSettings, cubemapSettings, wrapSettings);
  412. }
  413. public static TextureGenerationOutput GenerateTextureGUI(NativeArray<Color32> buffer, int bufferWidth, int bufferHeight, TextureSettings settings, TextureImporterPlatformSettings platformSettings,
  414. TextureAlphaSettings alphaSettings = null, TextureMipmapSettings mipmapSettings = null, TextureWrapSettings wrapSettings = null)
  415. {
  416. settings.colorTexture = false;
  417. if (wrapSettings == null)
  418. wrapSettings = new TextureWrapSettings(TextureWrapMode.Clamp, TextureWrapMode.Clamp, TextureWrapMode.Clamp, TextureWrapMode.Clamp);
  419. return GenerateTexture(buffer, bufferWidth, bufferHeight, TextureImporterType.GUI, platformSettings, settings, alphaSettings, mipmapSettings, wrapSettings);
  420. }
  421. public static TextureGenerationOutput GenerateTextureSingleChannel(NativeArray<Color32> buffer, int bufferWidth, int bufferHeight, TextureSettings settings, TextureImporterPlatformSettings platformSettings,
  422. TextureAlphaSettings alphaSettings = null, TextureMipmapSettings mipmapSettings = null, TextureCubemapSettings cubemapSettings = null, TextureWrapSettings wrapSettings = null)
  423. {
  424. settings.colorTexture = false;
  425. return GenerateTexture(buffer, bufferWidth, bufferHeight, TextureImporterType.SingleChannel, platformSettings, settings, alphaSettings, mipmapSettings, cubemapSettings, wrapSettings);
  426. }
  427. public static TextureGenerationOutput GenerateTextureCursor(NativeArray<Color32> buffer, int bufferWidth, int bufferHeight, TextureSettings settings, TextureImporterPlatformSettings platformSettings,
  428. TextureAlphaSettings alphaSettings = null, TextureMipmapSettings mipmapSettings = null, TextureWrapSettings wrapSettings = null)
  429. {
  430. if (alphaSettings == null)
  431. alphaSettings = new TextureAlphaSettings(TextureImporterAlphaSource.FromInput, 0.5f);
  432. if (wrapSettings == null)
  433. wrapSettings = new TextureWrapSettings(TextureWrapMode.Clamp, TextureWrapMode.Clamp, TextureWrapMode.Clamp, TextureWrapMode.Clamp);
  434. return GenerateTexture(buffer, bufferWidth, bufferHeight, TextureImporterType.Cursor, platformSettings, settings, alphaSettings, mipmapSettings, wrapSettings);
  435. }
  436. public static TextureGenerationOutput GenerateTextureDefault(NativeArray<Color32> buffer, int bufferWidth, int bufferHeight, TextureSettings settings, TextureImporterPlatformSettings platformSettings,
  437. TextureAlphaSettings alphaSettings = null, TextureMipmapSettings mipmapSettings = null, TextureCubemapSettings cubemapSettings = null, TextureWrapSettings wrapSettings = null)
  438. {
  439. if (mipmapSettings == null)
  440. mipmapSettings = new TextureMipmapSettings(TextureImporterMipFilter.BoxFilter, false, false, false, 1, 3);
  441. return GenerateTexture(buffer, bufferWidth, bufferHeight, TextureImporterType.Default, platformSettings, settings, alphaSettings, mipmapSettings, cubemapSettings, wrapSettings);
  442. }
  443. static TextureGenerationOutput GenerateTexture(NativeArray<Color32> imageBuffer, int imageBufferWidth, int imageBufferHeight, TextureImporterType type, TextureImporterPlatformSettings platformSettings, params ITextureSettings[] otherSettings)
  444. {
  445. var textureGenerationSettings = new TextureGenerationSettings();
  446. textureGenerationSettings.platformSettings = platformSettings;
  447. textureGenerationSettings.sourceTextureInformation = new SourceTextureInformation();
  448. textureGenerationSettings.sourceTextureInformation.height = imageBufferHeight;
  449. textureGenerationSettings.sourceTextureInformation.width = imageBufferWidth;
  450. textureGenerationSettings.textureImporterSettings = new TextureImporterSettings();
  451. textureGenerationSettings.textureImporterSettings.textureType = type;
  452. textureGenerationSettings.textureImporterSettings.textureShape = TextureImporterShape.Texture2D;
  453. textureGenerationSettings.textureImporterSettings.alphaIsTransparency = false;
  454. textureGenerationSettings.textureImporterSettings.convertToNormalMap = false;
  455. textureGenerationSettings.textureImporterSettings.mipmapEnabled = false;
  456. textureGenerationSettings.textureImporterSettings.sRGBTexture = true;
  457. textureGenerationSettings.textureImporterSettings.readable = false;
  458. textureGenerationSettings.textureImporterSettings.fadeOut = false;
  459. textureGenerationSettings.textureImporterSettings.wrapMode = TextureWrapMode.Repeat;
  460. textureGenerationSettings.textureImporterSettings.wrapModeU = TextureWrapMode.Repeat;
  461. textureGenerationSettings.textureImporterSettings.wrapModeV = TextureWrapMode.Repeat;
  462. textureGenerationSettings.textureImporterSettings.wrapModeW = TextureWrapMode.Repeat;
  463. textureGenerationSettings.textureImporterSettings.swizzleR = TextureImporterSwizzle.R;
  464. textureGenerationSettings.textureImporterSettings.swizzleG = TextureImporterSwizzle.G;
  465. textureGenerationSettings.textureImporterSettings.swizzleB = TextureImporterSwizzle.B;
  466. textureGenerationSettings.textureImporterSettings.swizzleA = TextureImporterSwizzle.A;
  467. foreach (var otherSetting in otherSettings)
  468. {
  469. if (otherSetting != null)
  470. otherSetting.FillTextureGenerationSettings(ref textureGenerationSettings);
  471. }
  472. return TextureGenerator.GenerateTexture(textureGenerationSettings, imageBuffer);
  473. }
  474. static public TextureSettings ExtractTextureSettings(this TextureImporterSettings tis)
  475. {
  476. var ts = new TextureSettings();
  477. ts.colorTexture = tis.sRGBTexture;
  478. ts.readable = tis.readable;
  479. ts.npotScale = tis.npotScale;
  480. ts.filterMode = tis.filterMode;
  481. ts.aniso = tis.aniso;
  482. ts.swizzleR = tis.swizzleR;
  483. ts.swizzleG = tis.swizzleG;
  484. ts.swizzleB = tis.swizzleB;
  485. ts.swizzleA = tis.swizzleA;
  486. return ts;
  487. }
  488. static public TextureSpriteSettings ExtractTextureSpriteSettings(this TextureImporterSettings tis)
  489. {
  490. var ts = new TextureSpriteSettings();
  491. ts.pixelsPerUnit = tis.spritePixelsPerUnit;
  492. ts.meshType = tis.spriteMeshType;
  493. ts.extrudeEdges = tis.spriteExtrude;
  494. return ts;
  495. }
  496. static public TextureWrapSettings ExtractTextureWrapSettings(this TextureImporterSettings tis)
  497. {
  498. var ts = new TextureWrapSettings();
  499. ts.wrapMode = tis.wrapMode;
  500. ts.wrapModeU = tis.wrapModeU;
  501. ts.wrapModeV = tis.wrapModeV;
  502. ts.wrapModeW = tis.wrapModeW;
  503. return ts;
  504. }
  505. static public TextureAlphaSettings ExtractTextureAlphaSettings(this TextureImporterSettings settings)
  506. {
  507. if (settings.alphaIsTransparency == false)
  508. return null;
  509. var ts = new TextureAlphaSettings();
  510. ts.alphaSource = settings.alphaSource;
  511. ts.alphaTolerance = settings.alphaTestReferenceValue;
  512. return ts;
  513. }
  514. static public TextureMipmapSettings ExtractTextureMipmapSettings(this TextureImporterSettings settings)
  515. {
  516. if (!settings.mipmapEnabled)
  517. return null;
  518. var ts = new TextureMipmapSettings();
  519. ts.filter = settings.mipmapFilter;
  520. ts.borderMipmap = settings.borderMipmap;
  521. ts.fadeout = settings.fadeOut;
  522. ts.fadeDistanceStart = settings.mipmapFadeDistanceStart;
  523. ts.fadeDistanceEnd = settings.mipmapFadeDistanceEnd;
  524. ts.preserveCoverage = settings.mipMapsPreserveCoverage;
  525. return ts;
  526. }
  527. static public TextureNormalSettings ExtractTextureNormalSettings(this TextureImporterSettings settings)
  528. {
  529. var ts = new TextureNormalSettings();
  530. ts.filter = settings.normalMapFilter;
  531. ts.generateFromGrayScale = settings.convertToNormalMap;
  532. ts.bumpiness = settings.heightmapScale;
  533. return ts;
  534. }
  535. static public TextureCubemapSettings ExtractTextureCubemapSettings(this TextureImporterSettings settings)
  536. {
  537. if (settings.textureShape != TextureImporterShape.TextureCube)
  538. return null;
  539. var ts = new TextureCubemapSettings();
  540. ts.convolution = settings.cubemapConvolution;
  541. ts.mode = settings.generateCubemap;
  542. ts.seamless = settings.seamlessCubemap;
  543. return ts;
  544. }
  545. }
  546. }