WPF Texture mapping of a cube

Viewed 2444

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:

Cube 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.

Bad mapping

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>

1 Answers
Related