简体   繁体   中英

WPF Texture mapping of a cube

I have a cube defined as

<MeshGeometry3D Positions="0,0,0 180.31222920257,180.31222920257,0 90.1561146012848,360.624458405139,156.154971102428 -90.1561146012848,180.31222920257,156.154971102428 -90.1561146012848,180.31222920257,-156.154971102428 90.1561146012848,360.624458405139,-156.154971102428 0,540.936687607709,0 -180.31222920257,360.624458405139,0 "
                    TriangleIndices="0 1 3 1 2 3  0 3 4 3 7 4  0 4 1 4 5 1  
                                     1 5 2 5 6 2  3 2 7 2 6 7  4 7 5 5 7 6"/>

Onto which I want to map this texture:

立方体纹理

Such that it forms a continuous gradient with the following colors at these respective locations:

Black   0,0,0
Red     180.31222920257,180.31222920257,0
Yellow  90.1561146012848,360.624458405139,156.154971102428
Green   -90.1561146012848,180.31222920257,156.154971102428
Blue    -90.1561146012848,180.31222920257,-156.154971102428
Magenta 90.1561146012848,360.624458405139,-156.154971102428
White   0, 540.936687607709, 0
Cyan    180.31222920257, 360.624458405139, 0

After reading some articles about wpf texture mapping, I came up with the following, which doesn't work:

TextureCoordinates="0   ,   0.666666667 0.25, 0.666666667   0   , 0.333333333
                    0.25,   0.666666667 0   , 0.666666667   0.25, 0.333333333
                    1   ,   0.666666667 1   , 0.333333333   0.75, 0.666666667
                    1   ,   0.333333333 0.75, 0.333333333   0.75, 0.666666667
                    0.75,   1           0.75, 0.666666667   0.5 , 1
                    0.75,   0.666666667 0.5 , 0.666666667   0.5 , 1
                    0.25,   0.666666667 0.5 , 0.666666667   0.25, 0.333333333
                    0.5 ,   0.666666667 0.5 , 0.333333333   0.25, 0.333333333
                    0.75,   0           0.5 , 0             0.75, 0.333333333
                    0.5 ,   0           0.5 , 0.333333333   0.75, 0.333333333
                    0.75,   0.666666667 0.75, 0.333333333   0.5 , 0.666666667
                    0.5 ,   0.666666667 0.75, 0.333333333   0.5 , 0.333333333"

That mapping has the texture going at 45 degree angles with weird stretching.

错误的映射

What is the appropriate mapping for what I want, and how do you generate it?

Full XAML, as requested

<Window x:Class="cube.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>
    <Viewport3D>
        <Viewport3D.Camera>
            <PerspectiveCamera Position="-800,1000,800" LookDirection="40,-40,-40 " 
                     UpDirection="0,1,0" />
        </Viewport3D.Camera>
        <ModelVisual3D>
            <ModelVisual3D.Content>
                <Model3DGroup>
                    <DirectionalLight Color="White" Direction="-1,-1,-3" />
                    <GeometryModel3D>
                        <GeometryModel3D.Geometry>
                            <MeshGeometry3D Positions="0,0,0 180.31222920257,180.31222920257,0 90.1561146012848,360.624458405139,156.154971102428 -90.1561146012848,180.31222920257,156.154971102428 -90.1561146012848,180.31222920257,-156.154971102428 90.1561146012848,360.624458405139,-156.154971102428 0,540.936687607709,0 -180.31222920257,360.624458405139,0 "
                TriangleIndices="0 1 3 1 2 3  0 3 4 3 7 4  0 4 1 4 5 1  
                                 1 5 2 5 6 2  3 2 7 2 6 7  4 7 5 5 7 6"
                TextureCoordinates="0   ,   0.666666667 0.25, 0.666666667   0   , 0.333333333
                0.25,   0.666666667 0   , 0.666666667   0.25, 0.333333333
                1   ,   0.666666667 1   , 0.333333333   0.75, 0.666666667
                1   ,   0.333333333 0.75, 0.333333333   0.75, 0.666666667
                0.75,   1           0.75, 0.666666667   0.5 , 1
                0.75,   0.666666667 0.5 , 0.666666667   0.5 , 1
                0.25,   0.666666667 0.5 , 0.666666667   0.25, 0.333333333
                0.5 ,   0.666666667 0.5 , 0.333333333   0.25, 0.333333333
                0.75,   0           0.5 , 0             0.75, 0.333333333
                0.5 ,   0           0.5 , 0.333333333   0.75, 0.333333333
                0.75,   0.666666667 0.75, 0.333333333   0.5 , 0.666666667
                0.5 ,   0.666666667 0.75, 0.333333333   0.5 , 0.333333333"/>
                        </GeometryModel3D.Geometry>
                        <GeometryModel3D.Material>
                            <DiffuseMaterial>
                                <DiffuseMaterial.Brush>
                                    <ImageBrush ImageSource="texture.png"/>
                                </DiffuseMaterial.Brush>
                            </DiffuseMaterial>
                        </GeometryModel3D.Material>
                    </GeometryModel3D>
                </Model3DGroup>
            </ModelVisual3D.Content>
        </ModelVisual3D>
    </Viewport3D>
</Grid>

This should get you started with correct cube coordinates, I found it on seans stuff

If you can add your XAML in full to your question I think I could alter this answer to help you solve the problem further.

<Grid>
        <Viewport3D x:Name="viewport3D1">
            <Viewport3D.Camera>
                <PerspectiveCamera x:Name="camMain" Position="6 5 4" LookDirection="-6 -5 -4"/>
            </Viewport3D.Camera>
            <ModelVisual3D>
                <ModelVisual3D.Content>
                    <DirectionalLight x:Name="dirLightMain" Direction="-1,-1,-1"/>
                </ModelVisual3D.Content>
            </ModelVisual3D>
            <ModelVisual3D>
                <ModelVisual3D.Content>
                    <GeometryModel3D>
                        <GeometryModel3D.Geometry>
                            <MeshGeometry3D x:Name="meshMain"
                                Positions="0 0 0  1 0 0  0 1 0  1 1 0  0 0 1  1 0 1  0 1 1  1 1 1"
                                TriangleIndices="2 3 1  2 1 0  7 1 3  7 5 1  6 5 7  6 4 5  6 2 0  2 0 4  2 7 3  2 6 7  0 1 5  0 5 4">
                            </MeshGeometry3D>
                        </GeometryModel3D.Geometry>
                        <GeometryModel3D.Material>
                            <DiffuseMaterial x:Name="matDiffuseMain" Brush="Red"/>
                        </GeometryModel3D.Material>
                        <GeometryModel3D.Transform>
                            <Transform3DGroup>
                                <TranslateTransform3D OffsetZ="0" OffsetX="0" OffsetY="0"/>
                                <ScaleTransform3D ScaleZ="1" ScaleY="1" ScaleX="1"/>
                                <RotateTransform3D d:EulerAngles="0,0,0">
                                    <RotateTransform3D.Rotation>
                                        <AxisAngleRotation3D Axis="0,1,0" Angle="0"/>
                                    </RotateTransform3D.Rotation>
                                </RotateTransform3D>
                                <TranslateTransform3D OffsetZ="0" OffsetX="0" OffsetY="0"/>
                                <TranslateTransform3D OffsetZ="0.423" OffsetY="0.551" OffsetX="0.604"/>
                            </Transform3DGroup>
                        </GeometryModel3D.Transform>
                    </GeometryModel3D>
                </ModelVisual3D.Content>
            </ModelVisual3D>
        </Viewport3D>
    </Grid>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM