private void OpenFileButton_OnClick(object sender, RoutedEventArgs e) { var openFileDialog = new Microsoft.Win32.OpenFileDialog(); openFileDialog.Filter = "All texture files (*.*)|*.*"; openFileDialog.Title = "Select texture file"; if (!string.IsNullOrEmpty(BaseFolder) && System.IO.Directory.Exists(BaseFolder)) { openFileDialog.InitialDirectory = BaseFolder; } if ((openFileDialog.ShowDialog() ?? false) && !string.IsNullOrEmpty(openFileDialog.FileName)) { TextureCheckBox.SetCurrentValue(System.Windows.Controls.Primitives.ToggleButton.IsCheckedProperty, true); FileNameTextBox.SetCurrentValue(TextBox.TextProperty, openFileDialog.FileName); LoadCurrentTexture(); } }
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.SetCurrentValue(ToolTipProperty, 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.SetCurrentValue(ForegroundProperty, Brushes.Red); FileNameTextBox.SetCurrentValue(ToolTipProperty, 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.SetCurrentValue(System.Windows.Controls.Primitives.ToggleButton.IsCheckedProperty, 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(); } }