• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

WPF实现3D立方体波浪墙效果

c# 搞代码 4年前 (2022-01-09) 23次浏览 已收录 0个评论

本文实例为大家分享了WPF实现3D立方体波浪墙效果的具体代码,供大家参考,具体内容如下

实现效果如下:

思路:仿照3D粒子系统,将粒子颗粒的Geometry改造为立方体,鼠标移动时将鼠标位置转为3D场景中的坐标。

步骤:

1、粒子类Particle.cs

public Point3D Position;//位置
public double Width;//长方体底面宽
public double Height;//长方体侧面高

2、粒子系统ParticleSystem.cs

private readonly List<Particle> _particleList;
private readonly GeometryModel3D _particleModel;
private readonly int CUBOIDHEIGHT = 20;
private readonly int MOUSERADIUS = 1000;
private int XParticleCount;
private int YParticleCount;
public Model3D ParticleModel => _particleModel;
 
public ParticleSystem(int amountX, int amountY, Color color)
    {
      XParticleCount = amountX;
      YParticleCount = amountY;
 
      _particleList = new List<Particle>();
      _particleModel = new GeometryModel3D { Geometry = new MeshGeometry3D() };
      var material = new DiffuseMaterial(new SolidColorBrush(color));
      _particleModel.Material = material;
    }
 
public void SpawnParticle(double size)
    {
      // 初始化粒子位置和大小
      for (int ix = 0; ix < XParticleCount; ix++)
      {
        for (int iy = 0; iy < YParticleCount; iy++)
        {
          var p = new Particle
          {
            Position = new Point3D(ix * size, iy * size, 0),
            Width = size,
            Height = CUBOIDHEIGHT,
          };
          _particleList.Add(p);
        }
      }
    }
 
public void Update(Point mp)
    {
      foreach (var p in _particleList)
      {
        //求点到圆心的距离
        double c = Math.Pow(Math.Pow(mp.X - p.Position.X, 2) + Math.Pow(mp.Y - p.Position.Y, 2), 0.5);
        p.Height = (MOUSERADIUS / (c + CUBOIDHEIGHT)) * CUBOIDHEIGHT;
      }
      UpdateGeometry();
    }
 
private void UpdateGeometry()
    {
      var positions = new Point3DCollection();
      var indices = new Int32Collection();
 
      for (var i = 0; i < _particleList.Count; ++i)
      {
        var positionIndex = i * 8;
        var p = _particleList[i];
 
        var p1 = new Point3D(p.Position.X, p.Position.Y, p.Position.Z);
        var p2 = new Point3D(p.Position.X + p.Width, p.Position.Y, p.Position.Z);
        var p3 = new Point3D(p.Position.X + p.Width, p.Position.Y + p.Width, p.Position.Z);
        var p4 = new Point3D(p.Position.X, p.Position.Y + p.Width, p.Position.Z);
        var p5 = new Point3D(p.Position.X, p.Position.Y, p.Position.Z + p.Height);
        var p6 = new Point3D(p.Position.X + p.Width, p.Position.Y, p.Position.Z + p.Height);
        var p7 = new Point3D(p.Position.X + p.Width, p.Position.Y + p.Width, p.Position.Z + p.Height);
        var p8 = new Point3D(p.Position.X, p.Position.Y + p.Width, p.Position.Z + p.Height);
 
        positions.Add(p1);
        positions.Add(p2);
        positions.Add(p3);
        positions.Add(p4);
        positions.Add(p5);
        positions.Add(p6);
        positions.Add(p7);
        positions.Add(p8);
 
        indices.Add(positionIndex);
        indices.Add(positionIndex + 1);
        indices.Add(positionIndex + 3);
        indices.Add(positionIndex + 1);
        indices.Add(positionIndex + 2);
        indices.Add(positionIndex + 3);
        indices.Add(positionIndex);
        indices.Add(positionIndex + 4);
        indices.Add(positionIndex + 3);
        indices.Add(positionIndex + 4);
        indices.Add(positionIndex + 7);
        indices.Add(positionIndex + 3);
        indices.Add(positionIndex + 4);
        indices.Add(positionIndex + 6);
        indices.Add(positionIndex + 7);
        indices.Add(positionIndex + 4);
        indices.Add(positionIndex + 5);
        indices.Add(positionIndex + 6);
        indices.Add(positionIndex);
        indices.Add(positionIndex + 4);
        indices.Add(positionIndex + 1);
        indices.Add(positionIndex + 1);
        indices.Add(positionIndex + 4);
        indices.Add(posit<i>本文来源gaodai$ma#com搞$$代**码网</i>ionIndex + 5);
        indices.Add(positionIndex + 1);
        indices.Add(positionIndex + 2);
        indices.Add(positionIndex + 6);
        indices.Add(positionIndex + 6);
        indices.Add(positionIndex + 5);
        indices.Add(positionIndex + 1);
        indices.Add(positionIndex + 2);
        indices.Add(positionIndex + 3);
        indices.Add(positionIndex + 7);
        indices.Add(positionIndex + 7);
        indices.Add(positionIndex + 6);
        indices.Add(positionIndex + 2);
      }
 
      ((MeshGeometry3D)_particleModel.Geometry).Positions = positions;
      ((MeshGeometry3D)_particleModel.Geometry).TriangleIndices = indices;
 }

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:WPF实现3D立方体波浪墙效果
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址