示例#1
0
        protected override Task <ResultStatus> DoCommandOverride(ICommandContext commandContext)
        {
            var assetManager = new AssetManager();

            var imageGroupData = new TImageGroupData {
                Images = new List <TImageData>()
            };

            // add the sprite data to the sprite list.
            foreach (var uiImage in asset.GroupAsset.Images)
            {
                var newImage = new TImageData
                {
                    Name          = uiImage.Name,
                    Region        = uiImage.TextureRegion,
                    IsTransparent = asset.GroupAsset.Alpha != AlphaFormat.None, // todo analyze texture region texture data to auto-determine alpha?
                    Orientation   = uiImage.Orientation,
                };

                if (UseSeparateAlphaTexture)
                {
                    var baseLocation = ImageGroupAsset.BuildTextureUrl(Url, ImageToTextureIndex[uiImage]);
                    newImage.Texture = new ContentReference <Graphics.Texture> {
                        Location = TextureAlphaComponentSplitter.GenerateColorTextureURL(baseLocation)
                    };
                    newImage.TextureAlpha = new ContentReference <Graphics.Texture> {
                        Location = TextureAlphaComponentSplitter.GenerateAlphaTextureURL(baseLocation)
                    };
                }
                else
                {
                    newImage.Texture = new ContentReference <Graphics.Texture> {
                        Location = ImageGroupAsset.BuildTextureUrl(Url, ImageToTextureIndex[uiImage])
                    };
                }

                SetImageSpecificFields(uiImage, newImage);

                imageGroupData.Images.Add(newImage);
            }

            // save the imageData into the data base
            assetManager.Save(Url, imageGroupData);

            return(Task.FromResult(ResultStatus.Successful));
        }
示例#2
0
        public override IEnumerable <ObjectUrl> GetInputFiles()
        {
            for (int i = 0; i < ImageToTextureIndex.Values.Distinct().Count(); i++)
            {
                if (UseSeparateAlphaTexture)
                {
                    var textureUrl = ImageGroupAsset.BuildTextureUrl(Url, i);
                    yield return(new ObjectUrl(UrlType.Internal, TextureAlphaComponentSplitter.GenerateColorTextureURL(textureUrl)));

                    yield return(new ObjectUrl(UrlType.Internal, TextureAlphaComponentSplitter.GenerateAlphaTextureURL(textureUrl)));
                }
                else
                {
                    yield return(new ObjectUrl(UrlType.Internal, ImageGroupAsset.BuildTextureUrl(Url, i)));
                }
            }
        }
示例#3
0
        public void TestSplitTexture()
        {
            var sessionResult = PackageSession.Load("../../sources/engine/SiliconStudio.Paradox.Assets.Tests/SiliconStudio.Paradox.Assets.Tests.pdxpkg");
            var session       = sessionResult.Session;

            var materialItem = session.FindAsset("Cube/TestMaterial");
            var material     = (MaterialAsset)materialItem.Asset;
            var solver       = new TextureAlphaComponentSplitter(session);

            var modifiedMaterial = solver.Run(material.Material, materialItem.Location.GetDirectory());

            Assert.AreEqual(3, modifiedMaterial.Nodes.Count);
            Assert.AreEqual(1, modifiedMaterial.ColorNodes.Count);

            // test that the original structure of the material hasn't changed

            var originalRootNode = modifiedMaterial.Nodes[modifiedMaterial.ColorNodes.First().Value];

            Assert.IsTrue(originalRootNode is MaterialBinaryNode);

            var originalBinaryRootNode = (MaterialBinaryNode)originalRootNode;

            Assert.AreEqual((int)MaterialBinaryOperand.Overlay, (int)originalBinaryRootNode.Operand);
            Assert.IsTrue(originalBinaryRootNode.LeftChild is MaterialBinaryNode);
            Assert.IsTrue(originalBinaryRootNode.RightChild is MaterialBinaryNode);

            var originalRootLeftChildNode  = (MaterialBinaryNode)originalBinaryRootNode.LeftChild;
            var originalRootRightChildNode = (MaterialBinaryNode)originalBinaryRootNode.RightChild;

            Assert.AreEqual((int)MaterialBinaryOperand.Screen, (int)originalRootLeftChildNode.Operand);
            Assert.AreEqual((int)MaterialBinaryOperand.Saturate, (int)originalRootRightChildNode.Operand);
            Assert.IsTrue(originalRootLeftChildNode.LeftChild is MaterialReferenceNode);
            Assert.IsTrue(originalRootLeftChildNode.RightChild is MaterialReferenceNode);
            Assert.IsTrue(originalRootRightChildNode.RightChild is MaterialFloat4Node);

            var originalRootLeftLeftChildNode   = (MaterialReferenceNode)originalRootLeftChildNode.LeftChild;
            var originalRootLeftRightChildNode  = (MaterialReferenceNode)originalRootLeftChildNode.RightChild;
            var originalRootRightLeftChildNode  = originalRootRightChildNode.LeftChild;
            var originalRootRightRightChildNode = (MaterialFloat4Node)originalRootRightChildNode.RightChild;

            Assert.AreEqual("diffuseFactor", originalRootLeftLeftChildNode.Name);
            Assert.AreEqual("diffuseTexture", originalRootLeftRightChildNode.Name);

            var rawUnreferencedRootLeftLeftChildNode  = modifiedMaterial.Nodes["diffuseFactor"];
            var rawUnreferencedRootLeftRightChildNode = modifiedMaterial.Nodes["diffuseTexture"];

            Assert.IsTrue(rawUnreferencedRootLeftLeftChildNode is MaterialFloat4Node);

            var unreferencedRootLeftLeftChildNode  = (MaterialFloat4Node)rawUnreferencedRootLeftLeftChildNode;
            var unreferencedRootLeftRightChildNode = rawUnreferencedRootLeftRightChildNode;

            Assert.AreEqual(new Vector4(0.1f, 0.2f, 0.3f, 0.4f), unreferencedRootLeftLeftChildNode.Value);
            Assert.AreEqual(new Vector4(1f, 2f, 3f, 4f), originalRootRightRightChildNode.Value);

            var originalTextureNodes = new List <MaterialTextureNode>
            {
                (MaterialTextureNode)((MaterialBinaryNode)((MaterialBinaryNode)material.Material.Nodes["diffuse"]).RightChild).LeftChild,
                (MaterialTextureNode)material.Material.Nodes["diffuseTexture"]
            };
            var modifiedTextureNodes = new List <IMaterialNode> {
                originalRootRightLeftChildNode, unreferencedRootLeftRightChildNode
            };

            // test that the old MaterialTextureReferences has been substituted

            for (int i = 0; i < originalTextureNodes.Count; i++)
            {
                var originalTextureNode = originalTextureNodes[i];
                var modifiedTextureNode = modifiedTextureNodes[i];

                Assert.IsTrue(modifiedTextureNode is MaterialShaderClassNode);

                var newShaderNode = (MaterialShaderClassNode)modifiedTextureNode;

                Assert.AreEqual("ComputeColorSubstituteAlphaWithColor", Path.GetFileNameWithoutExtension(newShaderNode.MixinReference.Location));
                Assert.IsTrue(newShaderNode.CompositionNodes.ContainsKey("color1"));
                Assert.IsTrue(newShaderNode.CompositionNodes.ContainsKey("color2"));
                Assert.IsTrue(newShaderNode.CompositionNodes["color1"] is MaterialTextureNode);
                Assert.IsTrue(newShaderNode.CompositionNodes["color2"] is MaterialTextureNode);

                var leftNode  = (MaterialTextureNode)newShaderNode.CompositionNodes["color1"];
                var rightNode = (MaterialTextureNode)newShaderNode.CompositionNodes["color2"];

                var textureNodes = new List <MaterialTextureNode> {
                    leftNode, rightNode
                };
                foreach (var textureNode in textureNodes)
                {
                    Assert.AreEqual(originalTextureNode.Sampler.AddressModeU, textureNode.Sampler.AddressModeU);
                    Assert.AreEqual(originalTextureNode.Sampler.AddressModeV, textureNode.Sampler.AddressModeV);
                    Assert.AreEqual(originalTextureNode.Sampler.Filtering, textureNode.Sampler.Filtering);
                    Assert.AreEqual(originalTextureNode.Offset, textureNode.Offset);
                    Assert.AreEqual(originalTextureNode.Sampler.SamplerParameterKey, textureNode.Sampler.SamplerParameterKey);
                    Assert.AreEqual(originalTextureNode.Scale, textureNode.Scale);
                    Assert.AreEqual(originalTextureNode.TexcoordIndex, textureNode.TexcoordIndex);
                }

                Assert.AreEqual(originalTextureNode.Key, leftNode.Key);
                Assert.AreEqual(null, rightNode.Key);

                const string textureName = "cube_Untitled";
                const string leftNodeSupposedLocation  = "Cube/" + TextureAlphaComponentSplitter.SplittedTextureNamePrefix + textureName + TextureAlphaComponentSplitter.SplittedColorTextureNameSuffix;
                const string rightNodeSupposedLocation = "Cube/" + TextureAlphaComponentSplitter.SplittedTextureNamePrefix + textureName + TextureAlphaComponentSplitter.SplittedAlphaTextureNameSuffix;
                Assert.AreEqual(leftNodeSupposedLocation, leftNode.TextureName);
                Assert.AreEqual(rightNodeSupposedLocation, rightNode.TextureName);
                Assert.IsTrue(AssetManager.FileProvider.FileExists(leftNodeSupposedLocation));
                Assert.IsTrue(AssetManager.FileProvider.FileExists(rightNodeSupposedLocation));
            }
        }
示例#4
0
        public static ResultStatus ImportAndSaveTextureImage(UFile sourcePath, string outputUrl, TextureAsset textureAsset, TextureConvertParameters parameters, bool separateAlpha, CancellationToken cancellationToken, Logger logger)
        {
            var assetManager = new AssetManager();

            using (var texTool = new TextureTool())
                using (var texImage = texTool.Load(sourcePath))
                {
                    // Apply transformations
                    texTool.Decompress(texImage);

                    if (cancellationToken.IsCancellationRequested) // abort the process if cancellation is demanded
                    {
                        return(ResultStatus.Cancelled);
                    }

                    // Resize the image
                    if (textureAsset.IsSizeInPercentage)
                    {
                        texTool.Rescale(texImage, textureAsset.Width / 100.0f, textureAsset.Height / 100.0f, Filter.Rescaling.Lanczos3);
                    }
                    else
                    {
                        texTool.Resize(texImage, (int)textureAsset.Width, (int)textureAsset.Height, Filter.Rescaling.Lanczos3);
                    }

                    if (cancellationToken.IsCancellationRequested) // abort the process if cancellation is demanded
                    {
                        return(ResultStatus.Cancelled);
                    }

                    // texture size is now determined, we can cache it
                    var textureSize = new Int2(texImage.Width, texImage.Height);

                    // Check that the resulting texture size is supported by the targeted graphics profile
                    if (!TextureSizeSupported(textureAsset.Format, parameters.GraphicsPlatform, parameters.GraphicsProfile, textureSize, textureAsset.GenerateMipmaps, logger))
                    {
                        return(ResultStatus.Failed);
                    }


                    // Apply the color key
                    if (textureAsset.ColorKeyEnabled)
                    {
                        texTool.ColorKey(texImage, textureAsset.ColorKeyColor);
                    }

                    if (cancellationToken.IsCancellationRequested) // abort the process if cancellation is demanded
                    {
                        return(ResultStatus.Cancelled);
                    }


                    // Pre-multiply alpha
                    if (textureAsset.PremultiplyAlpha)
                    {
                        texTool.PreMultiplyAlpha(texImage);
                    }

                    if (cancellationToken.IsCancellationRequested) // abort the process if cancellation is demanded
                    {
                        return(ResultStatus.Cancelled);
                    }


                    // Generate mipmaps
                    if (textureAsset.GenerateMipmaps)
                    {
                        texTool.GenerateMipMaps(texImage, Filter.MipMapGeneration.Box);
                    }

                    if (cancellationToken.IsCancellationRequested) // abort the process if cancellation is demanded
                    {
                        return(ResultStatus.Cancelled);
                    }


                    // Convert/Compress to output format
                    // TODO: Change alphaFormat depending on actual image content (auto-detection)?
                    var outputFormat = DetermineOutputFormat(textureAsset.Format, textureAsset.Alpha, parameters.Platform, parameters.GraphicsPlatform, parameters.GraphicsProfile, textureSize, texImage.Format);
                    texTool.Compress(texImage, outputFormat, (TextureConverter.Requests.TextureQuality)parameters.TextureQuality);

                    if (cancellationToken.IsCancellationRequested) // abort the process if cancellation is demanded
                    {
                        return(ResultStatus.Cancelled);
                    }


                    // Save the texture
                    if (separateAlpha)
                    {
                        TextureAlphaComponentSplitter.CreateAndSaveSeparateTextures(texTool, texImage, outputUrl, textureAsset.GenerateMipmaps);
                    }
                    else
                    {
                        using (var outputImage = texTool.ConvertToParadoxImage(texImage))
                        {
                            if (cancellationToken.IsCancellationRequested) // abort the process if cancellation is demanded
                            {
                                return(ResultStatus.Cancelled);
                            }

                            assetManager.Save(outputUrl, outputImage);

                            logger.Info("Compression successful [{3}] to ({0}x{1},{2})", outputImage.Description.Width, outputImage.Description.Height, outputImage.Description.Format, outputUrl);
                        }
                    }
                }

            return(ResultStatus.Successful);
        }