5 using SiliconStudio.Assets;
6 using SiliconStudio.Core.IO;
7 using SiliconStudio.Core.Mathematics;
8 using SiliconStudio.Core.Serialization.Assets;
9 using SiliconStudio.Paradox.Assets.Effect;
10 using SiliconStudio.Paradox.Assets.Materials.Nodes;
11 using SiliconStudio.Paradox.Assets.Materials.Processor.Visitors;
12 using SiliconStudio.Paradox.Assets.Texture;
13 using SiliconStudio.Paradox.Graphics;
14 using SiliconStudio.TextureConverter;
16 namespace SiliconStudio.
Paradox.Assets.Materials
29 public const string SplittedTextureNamePrefix =
"__splitted_textures__";
30 public const string SplittedColorTextureNameSuffix =
"__Color";
31 public const string SplittedAlphaTextureNameSuffix =
"__Alpha";
40 this.assetSession = assetSession;
45 if (material == null)
throw new ArgumentNullException(
"material");
48 var modifiedMaterial = material.Clone();
51 var textureNodes = textureVisitor.GetAllModelTextureValues();
53 foreach (var textureNode
in textureNodes)
55 var itemAsset = assetSession.FindAsset(textureNode.TextureReference.Id);
57 throw new InvalidOperationException(
"The referenced texture is not included in the project session.");
63 var originalLocation = textureNode.TextureReference.Location;
65 using (var image = assetManager.Load<
Image>(originalLocation))
67 CreateAndSaveSeparateTextures(image, originalLocation, textureAsset.GenerateMipmaps, outputFormat);
68 assetManager.Unload(image);
75 substituteAlphaNode.CompositionNodes.Add(
"color1", colorNode);
76 substituteAlphaNode.CompositionNodes.Add(
"color2", alphaNode);
79 var children =
new[] { colorNode, alphaNode };
80 foreach (var childTexture
in children)
82 childTexture.Sampler.AddressModeU = textureNode.Sampler.AddressModeU;
83 childTexture.Sampler.AddressModeV = textureNode.Sampler.AddressModeV;
84 childTexture.Sampler.Filtering = textureNode.Sampler.Filtering;
85 childTexture.Offset = textureNode.Offset;
86 childTexture.Sampler.SamplerParameterKey = textureNode.Sampler.SamplerParameterKey;
87 childTexture.Scale = textureNode.Scale;
88 childTexture.TexcoordIndex = textureNode.TexcoordIndex;
92 colorNode.Key = textureNode.Key;
96 nodeReplacer.Replace(textureNode, substituteAlphaNode);
99 return modifiedMaterial;
104 return GenerateSeparateTextureURL(originalLocation, SplittedAlphaTextureNameSuffix);
109 return GenerateSeparateTextureURL(originalLocation, SplittedColorTextureNameSuffix);
112 private static string GenerateSeparateTextureURL(
UFile originalLocation,
string suffixName)
114 return originalLocation.GetDirectory() +
"/" + SplittedTextureNamePrefix + originalLocation.
GetFileName() + suffixName;
120 using (var texImage = texTool.Load(image))
122 CreateAndSaveSeparateTextures(texTool, texImage, originalTextureURL, shouldGenerateMipMaps, outputFormat);
128 var alphaTextureURL = GenerateAlphaTextureURL(originalTextureURL);
129 var colorTextureURL = GenerateColorTextureURL(originalTextureURL);
132 texTool.Decompress(texImage);
133 using (var alphaImage = texTool.CreateImageFromAlphaComponent(texImage))
136 if (shouldGenerateMipMaps)
137 texTool.GenerateMipMaps(alphaImage, Filter.MipMapGeneration.Box);
140 texTool.Compress(alphaImage, outputFormat);
141 using (var outputImage = texTool.ConvertToParadoxImage(alphaImage))
142 assetManager.Save(alphaTextureURL, outputImage);
146 texTool.Decompress(texImage);
147 texTool.Compress(texImage, outputFormat);
148 using (var outputImage = texTool.ConvertToParadoxImage(texImage))
149 assetManager.Save(colorTextureURL, outputImage);
static void CreateAndSaveSeparateTextures(TextureTool texTool, TexImage texImage, string originalTextureURL, bool shouldGenerateMipMaps, PixelFormat outputFormat=PixelFormat.ETC1)
static void CreateAndSaveSeparateTextures(Image image, string originalTextureURL, bool shouldGenerateMipMaps, PixelFormat outputFormat=PixelFormat.ETC1)
static readonly Vector2 Zero
A SiliconStudio.Core.Mathematics.Vector2 with all of its components set to zero.
static string GenerateColorTextureURL(UFile originalLocation)
Represents a two dimensional mathematical vector.
Provides method to load images or textures, to modify them and to convert them with different texture...
Provides method to instantiate an image 1D/2D/3D supporting TextureArray and mipmaps on the CPU or to...
Description of a material.
Utility class to split this material texture containing alpha into two texture materials: one contain...
static string GenerateAlphaTextureURL(UFile originalLocation)
A session for editing a package.
static readonly Vector2 One
A SiliconStudio.Core.Mathematics.Vector2 with all of its components set to one.
Defines a normalized directory path. See UPath for details. This class cannot be inherited.
string GetFileName()
Gets the name of the file without its extension. Can be null.
TextureAlphaComponentSplitter(PackageSession assetSession)
Describes a texture asset.
Temporary format containing texture data and information. Used as buffer between texture libraries...
MaterialDescription Run(MaterialDescription material, UDirectory materialPath, PixelFormat outputFormat=PixelFormat.ETC1)
PixelFormat
Defines various types of pixel formats.
Defines a normalized file path. See UPath for details. This class cannot be inherited.