namespace Yw.WpfUI.Hydro
{
///
/// 抽象3D粒子
///
internal class LogicalParticle3D
{
///
///
///
public LogicalParticle3D(LogicalMaterialHelper materialHelper)
{
_materialHelper = materialHelper;
}
private readonly LogicalMaterialHelper _materialHelper = null;
///
/// 位置
///
public Point3D Position { get; set; }
///
/// 流速
///
public Vector3D Velocity { get; set; }
///
/// 颜色
///
public Color Color { get; set; }
///
/// 尺寸
///
public double Size { get; set; }
///
/// 年龄
///
public double Age { get; set; }
///
/// 生命周期
///
public double Lifetime { get; set; }
///
/// 内容
///
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
};
}
///
/// 更新
///
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;
}
}
}