public BcresTextureMapWrapper(BCRES bcres, TextureMapInfo texInfo, STGenericMatTexture texture) { CanRename = false; CanReplace = false; BcresParent = bcres; Text = texture.Name; GenericMatTexture = texture; TextureMapInfo = texInfo; ImageKey = "TextureMaterialMap"; SelectedImageKey = "TextureMaterialMap"; }
private BcresTextureMapWrapper CreateGenericMatTexture(int textureUnit, BCRES bcres, TextureMapInfo TextureMapInfo) { STGenericMatTexture tex1 = new STGenericMatTexture(); var TexRef = TextureMapInfo.TextureRef; var Sampler = TextureMapInfo.Sampler; tex1.textureUnit = textureUnit++; tex1.Name = TexRef.Reference.Name; tex1.Type = STGenericMatTexture.TextureType.Diffuse; TextureMaps.Add(tex1); switch (TextureMapInfo.WrapU) { case PICATextureWrap.Repeat: tex1.wrapModeS = 0; break; case PICATextureWrap.Mirror: tex1.wrapModeS = 1; break; case PICATextureWrap.ClampToEdge: tex1.wrapModeS = 2; break; case PICATextureWrap.ClampToBorder: tex1.wrapModeS = 2; break; } switch (TextureMapInfo.WrapV) { case PICATextureWrap.Repeat: tex1.wrapModeT = 0; break; case PICATextureWrap.Mirror: tex1.wrapModeT = 1; break; case PICATextureWrap.ClampToEdge: tex1.wrapModeT = 2; break; case PICATextureWrap.ClampToBorder: tex1.wrapModeT = 2; break; } switch (TextureMapInfo.MagFilter) { case PICATextureFilter.Linear: tex1.magFilter = 0; break; case PICATextureFilter.Nearest: tex1.magFilter = 1; break; } switch (TextureMapInfo.MinFilter) { case PICATextureFilter.Linear: tex1.minFilter = 0; break; case PICATextureFilter.Nearest: tex1.minFilter = 1; break; } switch (TextureMapInfo.MipFilter) { case PICATextureFilter.Linear: tex1.mipDetail = 0; break; case PICATextureFilter.Nearest: tex1.mipDetail = 1; break; } switch (TextureMapInfo.WrapV) { case PICATextureWrap.Repeat: tex1.wrapModeT = 0; break; case PICATextureWrap.Mirror: tex1.wrapModeT = 1; break; case PICATextureWrap.ClampToEdge: tex1.wrapModeT = 2; break; case PICATextureWrap.ClampToBorder: tex1.wrapModeT = 2; break; } var wrapperTexMap = new BcresTextureMapWrapper(bcres, TextureMapInfo, tex1); return(wrapperTexMap); }
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(); } }
public TextureMapSelectionControl(IMultiMapMaterial material, TextureMapTypes textureMapType, string baseFolder = null) { Material = material; _physicallyBasedMaterial = material as PhysicallyBasedMaterial; TextureMapType = textureMapType; if (baseFolder != null && !baseFolder.EndsWith("\\") && !baseFolder.EndsWith("/")) { baseFolder += '\\'; } BaseFolder = baseFolder; _textureMapInfo = Material.TextureMaps.FirstOrDefault(m => m.MapType == TextureMapType); InitializeComponent(); ShowTextureTextBox = true; if (_physicallyBasedMaterial != null) { ShowFilter = textureMapType == TextureMapTypes.Metalness || textureMapType == TextureMapTypes.Glossiness || textureMapType == TextureMapTypes.Roughness || textureMapType == TextureMapTypes.MetalnessRoughness || textureMapType == TextureMapTypes.AmbientOcclusion; ShowMask = textureMapType == TextureMapTypes.Albedo || textureMapType == TextureMapTypes.BaseColor || textureMapType == TextureMapTypes.DiffuseColor; if (textureMapType == TextureMapTypes.Metalness || textureMapType == TextureMapTypes.MetalnessRoughness) { CurrentFilterValue = _physicallyBasedMaterial.Metalness; } if (textureMapType == TextureMapTypes.Roughness || textureMapType == TextureMapTypes.MetalnessRoughness) { CurrentFilterValue = _physicallyBasedMaterial.Roughness; } if (ShowMask) { CurrentMaskColor = _physicallyBasedMaterial.BaseColor.ToWpfColor(); } } else { ShowFilter = false; ShowMask = false; } // To add support for drop into TextBox, we need to use PreviewDrop and PreviewDragOver events in DragAndDropHelper //var dragAndDropHelper = new DragAndDropHelper(FileNameTextBox, ".*"); //dragAndDropHelper.FileDroped += delegate (object sender, FileDropedEventArgs e) //{ // FileNameTextBox.Text = e.FileName; // MasterCheckBox.IsChecked = true; // LoadCurrentTexture(); //}; MapTypeTextBlock.Text = textureMapType.ToString(); if (_textureMapInfo == null) { FileNameTextBox.Text = ""; TextureCheckBox.IsChecked = false; } else { if (BaseFolder != null && _textureMapInfo.TextureResourceName.StartsWith(BaseFolder)) { FileNameTextBox.Text = _textureMapInfo.TextureResourceName.Substring(BaseFolder.Length); } else { FileNameTextBox.Text = _textureMapInfo.TextureResourceName; } TextureCheckBox.IsChecked = true; } UpdateMaskHeadingTextBlock(); }
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(); } }