locked
Cube with texture not visible RRS feed

  • Question

  • I want to texture a cube with an image. My problem is, that the cube is invisible in case I use the ImageBrush. When using a SolidColorBrush everything is fine. I need to do this programmatically because in the main project all objects and materials are created on runtime. I don't know where my fault is. May someone help me to figure it out?

    possible texture: https://en.wikipedia.org/wiki/Portable_Network_Graphics#/media/File:PNG_transparency_demonstration_1.png

    MainWindow.xaml

    <Window x:Class="WpfApp1.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:WpfApp1"
            mc:Ignorable="d"
            Title="MainWindow" Height="450" Width="800">
        <Grid>
            <Label 
                    HorizontalAlignment="Center" 
                    TextBlock.TextAlignment="Center" 
                    FontSize="20" 
                    Foreground="Red" 
                    Content="Model: Cube"/>
            <Viewport3D x:Name="MainViewport">
                <Viewport3D.Camera>
                    <PerspectiveCamera 
                            FarPlaneDistance="20" 
                            LookDirection="0,0,1" 
                            UpDirection="0,1,0" 
                            NearPlaneDistance="1" 
                            Position="0,0,-3" 
                            FieldOfView="45" />
                </Viewport3D.Camera>
            </Viewport3D>
        </Grid>
    </Window>
    

    MainWindow.xaml.cs

    using System;
    using System.Windows;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Media.Media3D;
    
    namespace WpfApp1
    {
        /// <summary>
        /// Interaktionslogik für MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
    
                //setup the 3D Object
                ModelVisual3D model = new ModelVisual3D();
                Model3DGroup mdlGroup = new Model3DGroup();
    
                //Jpg Texture as main purpose
                BitmapImage imgSource = new BitmapImage();
                string texturePath = @"./Texture2.PNG";
                imgSource.BeginInit();
                imgSource.UriSource = new Uri(texturePath, UriKind.Relative);
                imgSource.EndInit();
                ImageBrush imgBrush = new ImageBrush(imgSource);
    
                //Solid Black as alternative for testing
                SolidColorBrush colorBrush = new SolidColorBrush(Color.FromRgb(0,0,0));
    
                //Create Material
                DiffuseMaterial material = new DiffuseMaterial(colorBrush);
    
                //Generating the model
                GeometryModel3D internalModel = new GeometryModel3D(new MeshGeometry3D(), material)
                {
                    BackMaterial = material
                };
    
                //Generating the mesh of the model
                MeshGeometry3D mesh = (MeshGeometry3D)internalModel.Geometry;
                mesh.Positions.Add(new Point3D(-0.5, -0.5, -0.5));
                mesh.Positions.Add(new Point3D(-0.5, 0.5, -0.5));
                mesh.Positions.Add(new Point3D(0.5, 0.5, -0.5));
                mesh.Positions.Add(new Point3D(0.5, 0.5, -0.5));
                mesh.Positions.Add(new Point3D(0.5, -0.5, -0.5));
                mesh.Positions.Add(new Point3D(-0.5, -0.5, -0.5));
                mesh.TriangleIndices.Add(0);
                mesh.TriangleIndices.Add(1);
                mesh.TriangleIndices.Add(2);
                mesh.TriangleIndices.Add(3);
                mesh.TriangleIndices.Add(4);
                mesh.TriangleIndices.Add(5);
                mesh.Normals.Add(new Vector3D(0, 0, -1));
                mesh.Normals.Add(new Vector3D(0, 0, -1));
                mesh.Normals.Add(new Vector3D(0, 0, -1));
                mesh.Normals.Add(new Vector3D(0, 0, -1));
                mesh.Normals.Add(new Vector3D(0, 0, -1));
                mesh.Normals.Add(new Vector3D(0, 0, -1));
                mesh.Freeze();
    
                //including everything
                mdlGroup.Children.Add(internalModel);
                DirectionalLight myDirLight = new DirectionalLight
                {
                    Color = Colors.White,
                    Direction = new Vector3D(-3, -4, -5)
                };
                mdlGroup.Children.Add(myDirLight);
                model.Content = mdlGroup;
                this.MainViewport.Children.Add(model);
            }
        }
    }
    

    Thursday, October 15, 2020 5:54 AM

Answers

  • It turned out that I have missed to set the texture coordinates. See the comment of Peter Fleischer: https://docs.microsoft.com/en-us/answers/questions/127317/cube-with-texture-not-visible.html?childToView=128581#answer-128581

    • Marked as answer by ITD_Chair Friday, October 16, 2020 6:11 AM
    Friday, October 16, 2020 6:11 AM

All replies

  • Hi ITD_Chair,
    For questions about WPF, I suggest you ask the questions in Microsoft Q&A forum and you can get more professional answer.
    Thank you for your understanding.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, October 15, 2020 7:33 AM
  • Thank you for the information. I've posted the question to the Q&A Forum as well.
    Thursday, October 15, 2020 7:45 AM
  • It turned out that I have missed to set the texture coordinates. See the comment of Peter Fleischer: https://docs.microsoft.com/en-us/answers/questions/127317/cube-with-texture-not-visible.html?childToView=128581#answer-128581

    • Marked as answer by ITD_Chair Friday, October 16, 2020 6:11 AM
    Friday, October 16, 2020 6:11 AM