lixiaojun
8 天以前 0329c48a57f33a4c94e44c5e4d3d3c116184986f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
namespace Yw.WpfUI.Hydro
{
    /// <summary>
    /// 抽象3D粒子
    /// </summary>
    internal class LogicalParticle3D
    {
        /// <summary>
        /// 
        /// </summary>
        public LogicalParticle3D(LogicalMaterialHelper materialHelper)
        {
            _materialHelper = materialHelper;
        }
 
        private readonly LogicalMaterialHelper _materialHelper = null;
 
        /// <summary>
        /// 位置
        /// </summary>
        public Point3D Position { get; set; }
 
        /// <summary>
        /// 流速
        /// </summary>
        public Vector3D Velocity { get; set; }
 
        /// <summary>
        /// 颜色
        /// </summary>
        public Color Color { get; set; }
 
        /// <summary>
        /// 尺寸
        /// </summary>
        public double Size { get; set; }
 
        /// <summary>
        /// 年龄
        /// </summary>
        public double Age { get; set; }
 
        /// <summary>
        /// 生命周期
        /// </summary>
        public double Lifetime { get; set; }
 
 
        /// <summary>
        /// 内容
        /// </summary>
        public GeometryModel3D Content
        {
            get
            {
                if (_content == null)
                {
                    _content = new GeometryModel3D()
                    {
                        Material = GetMaterial(this.Color),
                        Geometry = CreateGeometry(this.Size),
                        Transform = CreateTransform()
                    };
                }
                return _content;
            }
        }
        private GeometryModel3D _content;
 
        //获取材质
        private Material GetMaterial(Color color)
        {
            return _materialHelper.GetMaterial(color);
        }
 
        //获取材质
        private Material GetMaterial(Color color, double opacity)
        {
            return _materialHelper.GetMaterial(color, opacity);
        }
 
        //创建几何图形
        private MeshGeometry3D CreateGeometry(double radius)
        {
            var builder = new MeshBuilder();
            builder.AddSphere(new Point3D(0, 0, 0), radius, 12, 12);
            return builder.ToMesh();
        }
 
        //创建转换
        private TranslateTransform3D CreateTransform()
        {
            return new TranslateTransform3D()
            {
                OffsetX = this.Position.X,
                OffsetY = this.Position.Y,
                OffsetZ = this.Position.Z
            };
        }
 
        /// <summary>
        /// 更新
        /// </summary>
        public void Update()
        {
            var content = this.Content;
            double lifeRatio = this.Age / this.Lifetime;
            if (lifeRatio > 0.8)
            {
                var opacity = 1.0 - (lifeRatio - 0.8) * 5;
                content.Material = GetMaterial(this.Color, opacity);
            }
            content.Geometry = CreateGeometry(this.Size);
            var transform = content.Transform as TranslateTransform3D;
            transform.OffsetX = this.Position.X;
            transform.OffsetY = this.Position.Y;
            transform.OffsetZ = this.Position.Z;
        }
 
 
    }
}