public StateGroupEditor(StateGroupAsset stateGroup)
        {
            InitializeComponent();

            asset = stateGroup;

            switch (stateGroup.ShaderCombination)
            {
            case ShaderCombination.VertexPixel:
            {
                VertexPixel.IsChecked = true;
                break;
            }

            case ShaderCombination.VertexGeometryPixel:
            {
                VertexGeometryPixel.IsChecked = true;
                break;
            }

            case ShaderCombination.VertexGeometry:
            {
                VertexGeometry.IsChecked = true;
                break;
            }
            }

            CombinationSelected(null, null);

            InEditMode       = true;
            this.DataContext = this;

            AvailableDepthWriteMasks = Enum.GetValues(typeof(DepthWriteMask)).Cast <DepthWriteMask>().ToList();
            AvailableDepthFuncs      = Enum.GetValues(typeof(ComparisonFunc)).Cast <ComparisonFunc>().ToList();
        }
        public StateGroupEditor()
        {
            InitializeComponent();

            asset                 = new StateGroupAsset();
            VertexEnabled         = true;
            PixelEnabled          = true;
            VertexPixel.IsChecked = true;
            InEditMode            = false;
            this.DataContext      = this;

            AvailableDepthWriteMasks = Enum.GetValues(typeof(DepthWriteMask)).Cast <DepthWriteMask>().ToList();
            AvailableDepthFuncs      = Enum.GetValues(typeof(ComparisonFunc)).Cast <ComparisonFunc>().ToList();
        }
        public MaterialEditor(StateGroupAsset basedStateGroup)
        {
            InitializeComponent();

            asset = new MaterialAsset();

            foreach (var binding in basedStateGroup.TextureBindings)
            {
                asset.Textures.Add(new Texture()
                {
                    Binding = binding.Binding
                });
            }

            InEditMode       = false;
            this.DataContext = this;
        }
        public static bool Import(StateGroupAsset asset)
        {
            using (var stream = File.Open(asset.ImportedFilename, FileMode.Create))
            {
                using (var writer = new BinaryWriter(stream))
                {
                    writer.Write((byte)83);
                    writer.Write((byte)84);
                    writer.Write((byte)71);
                    writer.Write((byte)80);

                    writer.Write(version);

                    writer.Write((int)asset.TextureBindings.Count);

                    foreach (var binding in asset.TextureBindings)
                    {
                        writer.Write((int)binding.Slot);
                        writeString(writer, binding.Binding);
                    }

                    var isTransparent = asset.BlendState.RenderTargets.Any(r => r.BlendEnabled);

                    writer.Write((bool)isTransparent);

                    if (asset.ShaderCombination == ShaderCombination.VertexPixel)
                    {
                        writer.Write((int)0);

                        writeString(writer, Path.GetFileName(asset.VertexShader.ImportedFilename) + ".cvs");
                        writeString(writer, Path.GetFileName(asset.PixelShader.ImportedFilename) + ".cps");
                    }
                    else if (asset.ShaderCombination == ShaderCombination.VertexGeometryPixel)
                    {
                        writer.Write((int)1);

                        writeString(writer, asset.VertexShader.ImportedFilename + ".cvs");
                        writeString(writer, asset.GeometryShader.ImportedFilename + ".cgs");
                        writeString(writer, asset.PixelShader.ImportedFilename + ".cps");
                    }
                    else if (asset.ShaderCombination == ShaderCombination.VertexGeometry)
                    {
                        writer.Write((int)2);
                        writeString(writer, asset.VertexShader.ImportedFilename + ".cvs");
                        writeString(writer, asset.GeometryShader.ImportedFilename + ".cgs");
                    }

                    writer.Write((int)asset.Samplers.Count);

                    foreach (var sampler in asset.Samplers)
                    {
                        writeString(writer, sampler.Name);
                        writer.Write((int)sampler.Filter);
                        writer.Write((int)sampler.AddressU);
                        writer.Write((int)sampler.AddressV);
                        writer.Write((int)sampler.AddressW);
                    }

                    writer.Write((int)asset.BlendState.RenderTargets.Count);

                    foreach (var renderTarget in asset.BlendState.RenderTargets)
                    {
                        writer.Write((int)Convert.ToInt32(renderTarget.BlendEnabled));
                        writer.Write((int)renderTarget.BlendOperation);
                        writer.Write((int)renderTarget.BlendOperationAlpha);
                        writer.Write((int)renderTarget.SourceBlend);
                        writer.Write((int)renderTarget.DestinationBlend);
                        writer.Write((int)renderTarget.SourceBlendAlpha);
                        writer.Write((int)renderTarget.DestinationBlendAlpha);
                        writer.Write((int)renderTarget.RenderTargetWriteMask);
                    }

                    writer.Write((int)Convert.ToInt32(asset.DepthStencilState.DepthEnable));
                    writer.Write((int)asset.DepthStencilState.DepthWriteMask);
                    writer.Write((int)asset.DepthStencilState.DepthFunc);
                }
            }

            asset.LastUpdated     = DateTime.Now;
            asset.ImporterVersion = ImporterVersion;

            return(true);
        }
 static internal void deletestateGroupMetadata(StateGroupAsset stateGroup)
 {
     deleteMetadata(stateGroup, "stateGroups/");
 }
        static internal void createStateGroupMetadata(StateGroupAsset stateGroup)
        {
            var metadata = new StringBuilder();

            metadata.AppendLine("Description= " + stateGroup.Description)
            .AppendLine("LastUpdated= " + stateGroup.LastUpdated.ToString("o"))
            .AppendLine("VertexShader= " + stateGroup.VertexShaderId)
            .AppendLine("GeometryShader= " + stateGroup.GeometryShaderId)
            .AppendLine("PixelShader= " + stateGroup.PixelShaderId)
            .AppendLine("ShaderCombination= " + stateGroup.ShaderCombination.ToString())
            .AppendLine("ImportedFilename= " + stateGroup.ImportedFilename)
            .AppendLine("ImporterVersion= " + stateGroup.ImporterVersion);

            var samplers = new StringBuilder();

            //sampler format: Samplers= name, filter, addressU, addressV, addressW; (sampler 2); (sampler 3)
            foreach (var sampler in stateGroup.Samplers)
            {
                samplers.Append(sampler.Name)
                .Append(",")
                .Append(sampler.Filter.ToString())
                .Append(",")
                .Append(sampler.AddressU.ToString())
                .Append(",")
                .Append(sampler.AddressV.ToString())
                .Append(",")
                .Append(sampler.AddressW.ToString())
                .Append(";");
            }

            metadata.AppendLine("Samplers= " + samplers.ToString());

            var textures = new StringBuilder();

            //texture format: Textures= slot, sourceId; (texture 2); ...
            foreach (var texture in stateGroup.TextureBindings)
            {
                textures.Append(texture.Slot)
                .Append(",")
                .Append(texture.Binding)
                .Append(";");
            }

            metadata.AppendLine("Textures= " + textures.ToString());

            var blendState = new StringBuilder();

            //blendstate format: BlendState= index, enabled, op, opAlpha, srcBlend, dstBlend, srcBlendAlpha, destBlendAlpha, writeMask; ...
            foreach (var renderTarget in stateGroup.BlendState.RenderTargets)
            {
                blendState.Append(renderTarget.Index)
                .Append(",")
                .Append(renderTarget.BlendEnabled)
                .Append(",")
                .Append(renderTarget.BlendOperation.ToString())
                .Append(",")
                .Append(renderTarget.BlendOperationAlpha.ToString())
                .Append(",")
                .Append(renderTarget.SourceBlend.ToString())
                .Append(",")
                .Append(renderTarget.DestinationBlend.ToString())
                .Append(",")
                .Append(renderTarget.SourceBlendAlpha.ToString())
                .Append(",")
                .Append(renderTarget.DestinationBlendAlpha.ToString())
                .Append(",")
                .Append(renderTarget.RenderTargetWriteMask.ToString())
                .Append(";");
            }

            metadata.AppendLine("BlendState= " + blendState.ToString());

            File.WriteAllText(MetadataPath + "stateGroups/" + stateGroup.Name + ".meta", metadata.ToString());
        }
Beispiel #7
0
        List <StateGroupAsset> loadStateGroups()
        {
            var stateGroups = new List <StateGroupAsset>();

            if (!Directory.Exists(metadataPath + "StateGroups"))
            {
                Directory.CreateDirectory(metadataPath + "StateGroups");
            }

            foreach (var filename in Directory.EnumerateFiles(metadataPath + "StateGroups"))
            {
                var metadata = File.ReadAllLines(filename);

                var stateGroup = new StateGroupAsset();

                stateGroup.Name              = System.IO.Path.GetFileNameWithoutExtension(filename);
                stateGroup.Description       = metadata[0].Split('=')[1].Trim();
                stateGroup.LastUpdated       = parseLastUpdatedDate(metadata[1].Split('=')[1].Trim(), "stateGroup", stateGroup.Name);
                stateGroup.VertexShaderId    = metadata[2].Split('=')[1].Trim();
                stateGroup.GeometryShaderId  = metadata[3].Split('=')[1].Trim();
                stateGroup.PixelShaderId     = metadata[4].Split('=')[1].Trim();
                stateGroup.ShaderCombination = (ShaderCombination)Enum.Parse(typeof(ShaderCombination), metadata[5].Split('=')[1].Trim());
                stateGroup.ImportedFilename  = metadata[6].Split('=')[1].Trim();
                stateGroup.ImporterVersion   = int.Parse(metadata[7].Split('=')[1].Trim());

                var samplerLine    = metadata[8].Split('=')[1].Trim();
                var samplerConfigs = samplerLine.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

                foreach (var config in samplerConfigs)
                {
                    var data    = config.Split(',');
                    var sampler = new Sampler()
                    {
                        Name     = data[0].Trim(),
                        Filter   = (Filter)Enum.Parse(typeof(Filter), data[1].Trim()),
                        AddressU = (TextureAddressMode)Enum.Parse(typeof(TextureAddressMode), data[2].Trim()),
                        AddressV = (TextureAddressMode)Enum.Parse(typeof(TextureAddressMode), data[3].Trim()),
                        AddressW = (TextureAddressMode)Enum.Parse(typeof(TextureAddressMode), data[4].Trim())
                    };

                    stateGroup.Samplers.Add(sampler);
                }

                var textureLine    = metadata[9].Split('=')[1].Trim();
                var textureConfigs = textureLine.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

                foreach (var config in textureConfigs)
                {
                    var data    = config.Split(',');
                    var texture = new TextureBinding()
                    {
                        Slot    = Int32.Parse(data[0].Trim()),
                        Binding = data[1].Trim()
                    };

                    stateGroup.TextureBindings.Add(texture);
                }

                var blendStateLine = metadata[10].Split('=')[1].Trim();
                var blendConfigs   = blendStateLine.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

                foreach (var config in blendConfigs)
                {
                    var data = config.Split(',');

                    var renderTarget = new RenderTarget()
                    {
                        Index                 = int.Parse(data[0].Trim()),
                        BlendEnabled          = bool.Parse(data[1].Trim()),
                        BlendOperation        = (BlendOperation)Enum.Parse(typeof(BlendOperation), data[2].Trim()),
                        BlendOperationAlpha   = (BlendOperation)Enum.Parse(typeof(BlendOperation), data[3].Trim()),
                        SourceBlend           = (BlendOption)Enum.Parse(typeof(BlendOption), data[4].Trim()),
                        DestinationBlend      = (BlendOption)Enum.Parse(typeof(BlendOption), data[5].Trim()),
                        SourceBlendAlpha      = (BlendOption)Enum.Parse(typeof(BlendOption), data[6].Trim()),
                        DestinationBlendAlpha = (BlendOption)Enum.Parse(typeof(BlendOption), data[7].Trim()),
                        RenderTargetWriteMask = (WriteMask)Enum.Parse(typeof(WriteMask), data[8].Trim())
                    };

                    stateGroup.BlendState.RenderTargets.Add(renderTarget);
                }

                stateGroups.Add(stateGroup);
            }

            return(stateGroups);
        }