4 using System.Threading;
6 using SiliconStudio.BuildEngine;
7 using SiliconStudio.Core;
8 using SiliconStudio.Core.Diagnostics;
9 using SiliconStudio.Core.IO;
10 using SiliconStudio.Core.Mathematics;
11 using SiliconStudio.Core.Serialization.Assets;
12 using SiliconStudio.Paradox.Assets.Materials;
13 using SiliconStudio.Paradox.Graphics;
14 using SiliconStudio.TextureConverter;
30 return (x & (x - 1)) == 0;
40 return textureSize.X == textureSize.Y && IsPowerOfTwo(textureSize.
X);
57 && ((textureSize.
X % 4) != 0 || (textureSize.
Y % 4) != 0))
59 logger.Error(
"DDS compression does not support texture files that do not have a size multiple of 4." +
60 "Please disable texture compression or adjust your texture size to multiple of 4.");
65 switch (graphicsProfile)
67 case GraphicsProfile.Level_9_1:
68 case GraphicsProfile.Level_9_2:
69 case GraphicsProfile.Level_9_3:
70 if (generateMipmaps && (!IsPowerOfTwo(textureSize.
Y) || !IsPowerOfTwo(textureSize.
X)))
72 logger.Error(
"Graphic profiles 9.1/9.2/9.3 do not support mipmaps with textures that are not power of 2. " +
73 "Please disable mipmap generation, modify your texture resolution or upgrade your graphic profile to a value >= 10.0.");
77 case GraphicsProfile.Level_10_0:
78 case GraphicsProfile.Level_10_1:
79 case GraphicsProfile.Level_11_0:
80 case GraphicsProfile.Level_11_1:
83 throw new ArgumentOutOfRangeException(
"graphicsProfile");
102 switch (textureFormat)
104 case TextureFormat.Compressed:
107 case PlatformType.Android:
108 outputFormat = alphaFormat == AlphaFormat.None ? PixelFormat.ETC1 : PixelFormat.R8G8B8A8_UNorm;
111 case PlatformType.iOS:
113 if (SupportPVRTC(imageSize))
117 case AlphaFormat.None:
119 outputFormat = PixelFormat.PVRTC_4bpp_RGB;
121 case AlphaFormat.Mask:
124 outputFormat = PixelFormat.PVRTC_4bpp_RGBA;
126 case AlphaFormat.Explicit:
127 case AlphaFormat.Interpolated:
130 outputFormat = PixelFormat.PVRTC_4bpp_RGBA;
133 throw new ArgumentOutOfRangeException();
138 outputFormat = PixelFormat.R8G8B8A8_UNorm;
141 case PlatformType.Windows:
142 case PlatformType.WindowsPhone:
143 case PlatformType.WindowsStore:
144 switch (graphicsPlatform)
146 case GraphicsPlatform.Direct3D11:
149 case AlphaFormat.None:
150 case AlphaFormat.Mask:
152 outputFormat = PixelFormat.BC1_UNorm;
154 case AlphaFormat.Explicit:
156 outputFormat = PixelFormat.BC2_UNorm;
158 case AlphaFormat.Interpolated:
160 outputFormat = PixelFormat.BC3_UNorm;
163 throw new ArgumentOutOfRangeException();
169 outputFormat = PixelFormat.R8G8B8A8_UNorm;
174 throw new NotSupportedException(
"Platform " + platform +
" is not supported by TextureTool");
177 case TextureFormat.HighColor:
179 outputFormat = PixelFormat.B5G6R5_UNorm;
181 outputFormat = PixelFormat.B5G5R5A1_UNorm;
183 throw new NotImplementedException(
"This alpha format requires a TrueColor texture format.");
185 case TextureFormat.TrueColor:
186 outputFormat = PixelFormat.R8G8B8A8_UNorm;
191 case TextureFormat.AsIs:
192 outputFormat = inputImageFormat;
195 throw new ArgumentOutOfRangeException();
205 using (var texImage = texTool.Load(sourcePath))
208 texTool.Decompress(texImage);
210 if (cancellationToken.IsCancellationRequested)
211 return ResultStatus.Cancelled;
215 texTool.Rescale(texImage, textureAsset.Width / 100.0f, textureAsset.Height / 100.0f, Filter.Rescaling.Lanczos3);
217 texTool.Resize(texImage, (int)textureAsset.
Width, (
int)textureAsset.Height, Filter.Rescaling.Lanczos3);
219 if (cancellationToken.IsCancellationRequested)
220 return ResultStatus.Cancelled;
223 var textureSize =
new Int2(texImage.Width, texImage.Height);
227 return ResultStatus.Failed;
232 texTool.ColorKey(texImage, textureAsset.ColorKeyColor);
234 if (cancellationToken.IsCancellationRequested)
235 return ResultStatus.Cancelled;
240 texTool.PreMultiplyAlpha(texImage);
242 if (cancellationToken.IsCancellationRequested)
243 return ResultStatus.Cancelled;
248 texTool.GenerateMipMaps(texImage, Filter.MipMapGeneration.Box);
250 if (cancellationToken.IsCancellationRequested)
251 return ResultStatus.Cancelled;
259 if (cancellationToken.IsCancellationRequested)
260 return ResultStatus.Cancelled;
266 TextureAlphaComponentSplitter.CreateAndSaveSeparateTextures(texTool, texImage, outputUrl, textureAsset.GenerateMipmaps);
270 using (var outputImage = texTool.ConvertToParadoxImage(texImage))
272 if (cancellationToken.IsCancellationRequested)
273 return ResultStatus.Cancelled;
275 assetManager.Save(outputUrl, outputImage);
277 logger.Info(
"Compression successful [{3}] to ({0}x{1},{2})", outputImage.Description.Width, outputImage.Description.Height, outputImage.Description.Format, outputUrl);
282 return ResultStatus.Successful;
PlatformType
Describes the platform operating system.
Provides method to load images or textures, to modify them and to convert them with different texture...
AlphaFormat Alpha
Gets or sets the alpha format.
int Y
The Y component of the vector.
float Width
Gets or sets the width.
int X
The X component of the vector.
GraphicsPlatform
The graphics platform.
static PixelFormat DetermineOutputFormat(TextureFormat textureFormat, AlphaFormat alphaFormat, PlatformType platform, GraphicsPlatform graphicsPlatform, Int2 imageSize, PixelFormat inputImageFormat)
Determine the output format of the texture depending on the platform and asset properties.
TextureFormat Format
Gets or sets the texture format.
static bool TextureSizeSupported(TextureFormat textureFormat, GraphicsPlatform platform, GraphicsProfile graphicsProfile, Int2 textureSize, bool generateMipmaps, Logger logger)
Utility function to check that the texture size is supported on the graphics platform for the provide...
TextureQuality
The desired texture quality.
ResultStatus
Status of a command.
static bool SupportPVRTC(Int2 textureSize)
Returns true if the PVRTC can be used for the provided texture size.
bool GenerateMipmaps
Gets or sets a value indicating whether to generate mipmaps.
Base implementation for ILogger.
TextureQuality TextureQuality
An helper for the compile commands that needs to process textures.
Describes a texture asset.
GraphicsProfile GraphicsProfile
Parameters used for converting/processing the texture in the storage.
Represents a three dimensional mathematical vector.
static ResultStatus ImportAndSaveTextureImage(UFile sourcePath, string outputUrl, TextureAsset textureAsset, TextureConvertParameters parameters, bool separateAlpha, CancellationToken cancellationToken, Logger logger)
bool ColorKeyEnabled
Gets or sets a value indicating whether to enable color key. Default is false.
GraphicsPlatform GraphicsPlatform
bool PremultiplyAlpha
Gets or sets a value indicating whether to convert the texture in premultiply alpha.
GraphicsProfile
Identifies the set of supported devices for the demo based on device capabilities.
PixelFormat
Defines various types of pixel formats.
bool IsSizeInPercentage
Gets or sets a value indicating whether this instance is using size in percentage. Default is true. See remarks.
Defines a normalized file path. See UPath for details. This class cannot be inherited.
Base class for texture resources.
static bool IsPowerOfTwo(int x)
Returns true if the provided int is a power of 2.