我在使用HelixToolkit.Wpf.SharpDX绘制点云时,希望实现根据点云不同深度作颜色差异化展示,但无论赋什么值给Colors,点云始终是黑色的。以下是原来的部分代码:
.xaml
<hx:PointGeometryModel3D Geometry="{Binding Geometry}"/>
.xaml.cs
var (vectors,colors) = ConvertToVector3DCollection(points);
viewModel.Geometry = new PointGeometry3D{ Positions = vectors,Colors=colors};
private (Vector3Collection vectors, Color4Collection colors) ConvertToVector3DCollection(System.Windows.Media.Media3D.Point3DCollection points)
{
Vector3Collection vectors = new Vector3Collection();
var colors = new Color4Collection();
Color4 MapDepthToColor(System.Windows.Media.Media3D.Point3D point)
{
float midZ = (minZ + maxZ)/2;
//根据z轴的值来上色
if ((point.Z - minZ) > midZ)
{
float r = (float)((point.Z - minZ - midZ) / midZ);
float g = (float)(1 - ((point.Z - minZ - midZ) / midZ));
return new Color4(r, g, 0, 1.0f);
}else
{
float g = (float)((point.Z - minZ) / midZ);
float b = (float)(1 - ((point.Z - minZ) / midZ));
return new Color4(0, g, b, 1.0f);
}
}
foreach (var point in points)
{
// Convert Point3D to Vector3D
Vector3 vector = new Vector3((float)point.X, (float)point.Y, (float)point.Z);
if (vector.Length() > 0.0001 || vector.Length() < -0.0001)
{
vectors.Add(vector);
colors.Add(MapDepthToColor(point));
}
}
return (vectors,colors);
}
后来找了很久才找到https://stackoverflow.org.cn/questions/64926932
这篇上方法,试了下,果真可以!!!但是就不知道原理是啥!
就是加上一行代码: Color=“{x:Static Colors.White}”
.xaml改成如下
<hx:PointGeometryModel3D Geometry="{Binding Geometry}" Color="{x:Static Colors.White}"/>