private void LoadCurrentTexture()
        {
            if (this.DXDevice == null)
            {
                return;
            }

            bool hasChanges = false;


            if (_textureMapInfo != null)
            {
                if (_textureMapInfo.ShaderResourceView != null)
                {
                    _textureMapInfo.ShaderResourceView.Dispose();
                }

                if (_textureMapInfo.SamplerState != null)
                {
                    _textureMapInfo.SamplerState.Dispose();
                }

                Material.TextureMaps.Remove(_textureMapInfo);

                _textureMapInfo = null;

                hasChanges = true;
            }

            FileNameTextBox.ClearValue(ForegroundProperty);
            FileNameTextBox.ToolTip = null;


            if (TextureCheckBox.IsChecked ?? false)
            {
                var fileName = FileNameTextBox.Text;

                if (!string.IsNullOrEmpty(fileName))
                {
                    if (BaseFolder != null && !System.IO.Path.IsPathRooted(fileName))
                    {
                        fileName = System.IO.Path.Combine(BaseFolder, fileName);
                    }

                    if (!System.IO.File.Exists(fileName))
                    {
                        FileNameTextBox.Foreground = Brushes.Red;
                        FileNameTextBox.ToolTip    = fileName + " does not exist!";
                        return;
                    }


                    var isBaseColor = (TextureMapType == TextureMapTypes.BaseColor ||
                                       TextureMapType == TextureMapTypes.Albedo ||
                                       TextureMapType == TextureMapTypes.DiffuseColor);

                    // To load a texture from file, you can use the TextureLoader.LoadShaderResourceView (this supports loading standard image files and also loading dds files).
                    // This method returns a ShaderResourceView and it can also set a textureInfo parameter that defines some of the properties of the loaded texture (bitmap size, dpi, format, hasTransparency).
                    TextureInfo textureInfo;
                    var         shaderResourceView = Ab3d.DirectX.TextureLoader.LoadShaderResourceView(this.DXDevice.Device,
                                                                                                       fileName,
                                                                                                       loadDdsIfPresent: true,
                                                                                                       convertTo32bppPRGBA: isBaseColor,
                                                                                                       generateMipMaps: true,
                                                                                                       textureInfo: out textureInfo);

                    // Only 2D textures are supported
                    if (shaderResourceView.Description.Dimension != ShaderResourceViewDimension.Texture2D)
                    {
                        MessageBox.Show("Invalid texture dimension: " + shaderResourceView.Description.Dimension.ToString());
                        TextureCheckBox.IsChecked = false;

                        return;
                    }

                    _textureMapInfo = new TextureMapInfo(TextureMapType, shaderResourceView, null, fileName);
                    Material.TextureMaps.Add(_textureMapInfo);


                    var physicallyBasedMaterial = Material as PhysicallyBasedMaterial;
                    if (isBaseColor && physicallyBasedMaterial != null)
                    {
                        // Get recommended BlendState based on HasTransparency and HasPreMultipliedAlpha values.
                        // Possible values are: CommonStates.Opaque, CommonStates.PremultipliedAlphaBlend or CommonStates.NonPremultipliedAlphaBlend.
                        var recommendedBlendState = this.DXDevice.CommonStates.GetRecommendedBlendState(textureInfo.HasTransparency, textureInfo.HasPremultipliedAlpha);

                        physicallyBasedMaterial.BlendState      = recommendedBlendState;
                        physicallyBasedMaterial.HasTransparency = textureInfo.HasTransparency;
                    }


                    if (CurrentMaskColor == Colors.Black)
                    {
                        CurrentMaskColor = Colors.White;
                    }

                    if (CurrentFilterValue <= 0.01)
                    {
                        CurrentFilterValue = 1.0f;
                    }

                    hasChanges = true;
                }
            }


            if (hasChanges)
            {
                OnMapSettingsChanged();
            }
        }
Exemple #2
0
        private void LoadCurrentTexture()
        {
            if (this.Device == null)
            {
                return;
            }

            bool hasChanges = false;


            if (_textureMapInfo != null)
            {
                if (_textureMapInfo.ShaderResourceView != null)
                {
                    _textureMapInfo.ShaderResourceView.Dispose();
                }

                if (_textureMapInfo.SamplerState != null)
                {
                    _textureMapInfo.SamplerState.Dispose();
                }

                Material.TextureMaps.Remove(_textureMapInfo);

                _textureMapInfo = null;

                hasChanges = true;
            }

            FileNameTextBox.ClearValue(ForegroundProperty);
            FileNameTextBox.ToolTip = null;


            if (TextureCheckBox.IsChecked ?? false)
            {
                var fileName = FileNameTextBox.Text;

                if (!string.IsNullOrEmpty(fileName))
                {
                    if (BaseFolder != null && !System.IO.Path.IsPathRooted(fileName))
                    {
                        fileName = System.IO.Path.Combine(BaseFolder, fileName);
                    }

                    if (!System.IO.File.Exists(fileName))
                    {
                        FileNameTextBox.Foreground = Brushes.Red;
                        FileNameTextBox.ToolTip    = fileName + " does not exist!";
                        return;
                    }


                    var convertTo32bppPRGBA = (TextureMapType == TextureMapTypes.BaseColor ||
                                               TextureMapType == TextureMapTypes.Albedo ||
                                               TextureMapType == TextureMapTypes.DiffuseColor);

                    var shaderResourceView = Ab3d.DirectX.TextureLoader.LoadShaderResourceView(this.Device, fileName, loadDdsIfPresent: false, convertTo32bppPRGBA: convertTo32bppPRGBA);

                    // Only 2D textures are supported
                    if (shaderResourceView.Description.Dimension != ShaderResourceViewDimension.Texture2D)
                    {
                        MessageBox.Show("Invalid texture dimension: " + shaderResourceView.Description.Dimension.ToString());
                        TextureCheckBox.IsChecked = false;

                        return;
                    }

                    _textureMapInfo = new TextureMapInfo(TextureMapType, shaderResourceView, null, fileName);
                    Material.TextureMaps.Add(_textureMapInfo);


                    if (CurrentMaskColor == Colors.Black)
                    {
                        CurrentMaskColor = Colors.White;
                    }

                    if (CurrentFilterValue <= 0.01)
                    {
                        CurrentFilterValue = 1.0f;
                    }

                    hasChanges = true;
                }
            }


            if (hasChanges)
            {
                OnMapSettingsChanged();
            }
        }