我有一个在3ds Max中创建的模型。该模型是一个简单的矩形。它的纹理具有重叠的纹理坐标-模型应并排显示图像文件的右半部分两次。我将此模型导出为.obj,并使用Microsoft Expression Blend将其转换为XAML。

我从blend中获取了MeshGeometry3D,并使用两种方法将其添加到Viewport3D中:

  • 使用带有Viewport2DVisual3DLabel作为其Visual。将Label的背景设置为纹理图像。使用此方法,一切都会按预期进行。
  • 使用带有ModelVisual3DGeometryModel3D作为其Content。将GeometryModel3D的 Material 设置为将图像用作画笔的DiffuseMaterial。使用此方法,TextureCoordinatesMeshGeometry3D似乎被不同地解释。

  • 完整的代码在下面提供。除了InitializeComponent(),后面的代码为空:
    <Window x:Class="TestTextureCoordinates.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.Resources>
            <MeshGeometry3D x:Key="geometry"
                            Normals="0,0,1 0,0,1 0,0,1 0,0,1 0,0,1 0,0,1 0,0,1 0,0,1"
                            Positions="113.8997,102.4171,0 148.9045,102.4171,0 148.9045,148.41161,0 113.8997,148.41161,0 184.5722,102.4171,0 184.5722,148.41161,0 148.9045,148.41161,0 148.9045,102.4171,0"
                            TextureCoordinates="0.50639999,0.9995 1.0065,0.9995 1.0065,0.00050002337 0.50639999,0.00050002337 1.0022,0.9995 1.0022,0.00050002337 0.5,0.00050002337 0.5,0.9995"
                            TriangleIndices="0 1 2 0 2 3 4 5 6 4 6 7"/>
            <ImageBrush x:Key="brush" ImageSource="img/test.jpg" />
        </Grid.Resources>
    
        <Viewport3D>
            <Viewport3D.Camera>
                <PerspectiveCamera Position="195,125,210" LookDirection="0,0,-1" />
            </Viewport3D.Camera>
    
            <ModelVisual3D>
                <ModelVisual3D.Content>
                    <AmbientLight Color="White" />
                </ModelVisual3D.Content>
            </ModelVisual3D>
    
            <!-- The first model, using a Viewport2DVisual3D. This works as intended. -->
            <Viewport2DVisual3D Geometry="{StaticResource geometry}">
                <Viewport2DVisual3D.Visual>
                    <Label Background="{StaticResource brush}" />
                </Viewport2DVisual3D.Visual>
                <Viewport2DVisual3D.Material>
                    <DiffuseMaterial Viewport2DVisual3D.IsVisualHostMaterial="True" />
                </Viewport2DVisual3D.Material>
            </Viewport2DVisual3D>
    
            <!-- The second model, using a ModelVisual3D and GeometryModel3D. The TextureCoordinates do not work as intended. -->
            <ModelVisual3D>
                <!-- We apply a transform to offset this model from the first model. -->
                <ModelVisual3D.Transform>
                    <TranslateTransform3D OffsetX="90" />
                </ModelVisual3D.Transform>
                <ModelVisual3D.Content>
                    <GeometryModel3D Geometry="{StaticResource geometry}">
                        <GeometryModel3D.Material>
                            <DiffuseMaterial Brush="{StaticResource brush}" />
                        </GeometryModel3D.Material>
                    </GeometryModel3D>
                </ModelVisual3D.Content>
            </ModelVisual3D>
        </Viewport3D>
    </Grid>
    

    这是test.jpg:

    这是最终结果。左侧是Viewport2DVisual3D,其外观与3ds Max中的相同。右边是ModelVisual3D,它似乎以不同的方式解释了TextureCoordinates

    这里发生了什么?由于我正在使用的软件的其他要求,我不能使用Viewport2DVisual3D。如何使GeometryModel3D正确解释TextureCoordinates

    最佳答案

    我只需要将ViewportUnits上的ImageBrush设置为Absolute:

    <ImageBrush x:Key="brush" ImageSource="img/test.jpg" ViewportUnits="Absolute" />
    

    请参阅WPF3D团队博客中的this post:

    关于c# - 为什么TextureCoordinates对于Viewport2DVisual3D可以正常工作,而对于GeometryModel3D却不能正常工作?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7001186/

    10-11 12:24