基於WPF實現3D畫廊動畫效果的示例代碼

接下來想做一個圖廊,所以並沒有必要用立方體,隻需做一些“墻壁”就行瞭。

而在一個平面上建起另一個矩形的平面,實則非常容易,隻需輸入墻角的兩點和高度就可以瞭,這對於寫過正方體的人來說絕對是簡單得很,無非是把四個點劈成兩個三角形

private MeshGeometry3D MakeSurface(Point3D p0, Point3D p1, Point3D p2, Point3D p3)
{
    MeshGeometry3D mesh = new MeshGeometry3D();
    Point3D[] pts ={p0, p1, p2, p3};
    foreach (var pt in pts)
        mesh.Positions.Add(pt);
    foreach (var i in new int[6] { 0, 1, 2, 2, 3, 0 })
        mesh.TriangleIndices.Add(i);

​​​​​​​    return mesh;
}

接下來可以像之前做正方體時那樣,先做一個平的地面,然後在地面上放置一些正方體。

由於地面上的正方形要求垂直於地面,所以重載一下

private MeshGeometry3D MakeSurface(Point p0, Point p1, double high)
{
    return MakeSurface(
        new Point3D(p0.X, 0, p0.Y), 
        new Point3D(p1.X, 0, p1.Y), 
        new Point3D(p1.X, high, p1.Y), 
        new Point3D(p0.X, high, p0.Y));
}

然後生成模型

代碼為

private void DefineModel(Model3DGroup group)
{
    // Make the ground.
    const double wid = 10;
    MeshGeometry3D groundMesh = MakeSurface(
        new Point3D(-wid, 0, -wid),
        new Point3D(-wid, 0, +wid),
        new Point3D(+wid, 0, +wid),
        new Point3D(+wid, 0, -wid));
    DiffuseMaterial groundMaterial = new DiffuseMaterial(Brushes.DarkGray);
    GeometryModel3D groundModel = new GeometryModel3D(groundMesh, groundMaterial);
    group.Children.Add(groundModel);

    MaterialGroup gpMaterial;

    for (int x = -2; x <= 2; x += 2)
    {
        for (int y = -2; y <= 2; y += 2)
        {
            MeshGeometry3D mesh = MakeSurface(new Point(x, y), new Point(x, y + 1), 1);
            byte r = (byte)(128 + x * 50);
            byte g = (byte)(128 + y * 50);
            byte b = (byte)(128 + x * 50);
            Color color = Color.FromArgb(255, r, g, b);
            DiffuseMaterial material = new DiffuseMaterial(
                new SolidColorBrush(color));

            GeometryModel3D model = new GeometryModel3D(mesh, material);
            group.Children.Add(model);
        }
    }
}

接下來就是掛載貼圖,結果大致如下

其方法也很簡單,就是把material變成想要的圖片,故而先把圖像放在一個字符串數組裡

static readonly string[] imgs = new string[9]
{
    "2d1.png","2d2.png","2d3.png","2d4.png","2d5.png",
    "2d6.png","2d7.png","2d8.png","2d9.png"
};

接下來需要註意一點,圖像本身需要一個坐標系,故而要把墻壁的代碼改為

private MeshGeometry3D MakeSurface(Point p0, Point p1, double high, string uri = null)
{
    var mesh = MakeSurface(
        new Point3D(p0.X, 0, p0.Y), new Point3D(p1.X, 0, p1.Y), 
        new Point3D(p1.X, high, p1.Y), new Point3D(p0.X, high, p0.Y));
    //這四個點代表圖像上的坐標和圖形中點的對應關系
    Point[] textureCoods = new Point[4]
    {
        new Point(0, 1),new Point(1, 1),
        new Point(1, 0),new Point(0, 0),
    };
    foreach (var pt  in textureCoods)
        mesh.TextureCoordinates.Add(pt);
    return mesh;
}

然後在生成正方形時,用圖像取代顏色

ImageBrush imgBrush = new ImageBrush();
imgBrush.ImageSource = new BitmapImage(new Uri(
    $"imgs//{imgs[k++]}", UriKind.Relative));

MeshGeometry3D mesh = MakeSurface(new Point(x, y), new Point(x, y + 1), 1);

GeometryModel3D model = new GeometryModel3D(mesh, new DiffuseMaterial(imgBrush));
group.Children.Add(model);

這樣二次元妹紙就花在墻壁上啦。

當然,如果想象力豐富的話可以把墻壁連在一起像迷宮那種,肯定會有沉浸式的體驗。

到此這篇關於基於WPF實現3D畫廊動畫效果的示例代碼的文章就介紹到這瞭,更多相關WPF 3D畫廊內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: