WPF 3D球及進階玩法


在WPF中3D球的構建算法請參考:

https://www.cnblogs.com/lonelyxmas/p/9844951.html

好玩以及值得借鑒的Demo:   (CSDN下載需要積分,避免你浪費積分,我替你試了)

https://download.csdn.net/download/dreamerde/4661198

運行效果:

image.png

https://download.csdn.net/download/yangyisen0713/6867605

運行效果:

image.png

上述內容均為其他作者的Demo參考,以便您借鑒。 后續為我的源碼實踐。

 

先上我的Demo效果圖:

Sphere3D1.gifSphere3D2.gifSphere3D3.gifSphere3D4.gifSphere3D5.gifSphere3D6.gif

實現原理:

      WPF里面的3D物體均是由一個個三角面拼裝起來的。網上很多公開的球體構建算法,可以很輕易的獲取到。我得到所有3D球構成的頂點(Point3D)后,在球體的外層附件上一層內容即可到達上述gif圖的效果。  每一個附加元素的坐標即構建3D球的三維坐標。

     如上文,在頂點位置上放5px * 5px的Ellipse即可構建點雲,在頂點位置放置ModelVisual3D,材質貼圖加載Image,即呈現照片球的效果。

     最后給Camera加一個旋轉動畫。

     關鍵源碼:

     

       private void CreateElementss()
        {
            Vector3D oVectorCenter = new Vector3D(0, 0, PointRadius);

            int nImgIndex = -1;
            for (int stack = 0; stack <= Stacks; stack++)
            {
                double phi = Math.PI / 2 - stack * Math.PI / Stacks;
                double y = PointRadius * Math.Sin(phi);
                double scale = -PointRadius * Math.Cos(phi);

                for (int slice = 0; slice <= Slices; slice++)
                {
                    double theta = slice * 2 * Math.PI / Slices;
                    double x = scale * Math.Sin(theta);
                    double z = scale * Math.Cos(theta);

                    Vector3D oPoint = new Vector3D(x, y, z);

                    InteractiveCustom3DPlane o3DPlane = new InteractiveCustom3DPlane(0.3, 0.24, 0, 0);
                    o3DPlane.OffsetX = oPoint.X;
                    o3DPlane.OffsetY = oPoint.Y;
                    o3DPlane.OffsetZ = oPoint.Z;

                    nImgIndex++;
                    Border bdr = new Border() { Width = 5, Height = 5 };
                    bdr.Tag = nImgIndex;
                    string sImgFile = this.GetImg(nImgIndex);
                    Image img = new Image();
                    img.Stretch = Stretch.Fill;
                    BitmapImage bitmap = new BitmapImage();
                    bitmap.DecodePixelWidth = 100;
                    bitmap.DecodePixelHeight = 100;
                    bitmap.BeginInit();
                    bitmap.UriSource = new Uri(sImgFile);
                    bitmap.EndInit();
                    bdr.MouseLeftButtonUp += Bdr_MouseLeftButtonUp;

                    img.Source = bitmap;
                    bdr.Child = img;
                    o3DPlane.Visual = bdr;
                    this.Mv3dContent.Children.Add(o3DPlane);
                }
            }
        }

  

      工具:Visual Studio 2017

      工程:WPF C#

      源代碼下載:

       


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM