簡體   English   中英

紋理不可見的 3D 對象

[英]3D object with texture invisible

我只想用圖像( 此處為紋理文件)對 3D 對象進行紋理處理。 為此,我使用System.Windows.Media3D東西。 我的問題是立方體沒有圖像紋理。 當我用SolidColorBrush繪制立方體時,立方體是可見的。 ImageBrush替換SolidColorBrush后,Cube 變得不可見。 我的錯在哪里? 下面的代碼使用了編程方式,因為我想在后面的程序中動態生成紋理和網格。

主窗口.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>

主窗口.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 = @"./Texture.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(imgBrush);

            //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);
        }
    }
}

聯系MS Q&A后,原來是忘記設置紋理坐標了。感謝彼得弗萊舍

 mesh.TextureCoordinates.Add(new Point(0, 1));
 mesh.TextureCoordinates.Add(new Point(0, 0));
 mesh.TextureCoordinates.Add(new Point(1, 0));
 mesh.TextureCoordinates.Add(new Point(0, 0));
 mesh.TextureCoordinates.Add(new Point(0, 1));
 mesh.TextureCoordinates.Add(new Point(1, 1));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM