I have a series of points (latitude and longitude data) that constitute the lake surface data. I want to overlay and display this lake surface in Cesium for Unity. My steps are: first, convert all the points to Unity world coordinates, then construct a mesh and add it to the scene as a child object of CesiumGeoreference, and add the CesiumGlobeAnchor component to this object.
is this way correctly?
private void showRegion(ReadOnlyCollection<IPosition> pts) {
MeshDataBuilder builder = new MeshDataBuilder(this.cesiumGeoreference, pts);
// 创建一个mesh
UnityEngine.Mesh mesh = new UnityEngine.Mesh();
mesh.vertices = builder.getVertexs();
mesh.triangles = builder.getTriangles();
mesh.name = "DynamicLake";
// 更新法线和边界
mesh.RecalculateNormals();
mesh.RecalculateBounds();
GameObject lake = new GameObject("Lake");
// 将生成的 Mesh 应用到 MeshFilter 和 MeshRenderer
MeshFilter meshFilter = lake.AddComponent<MeshFilter>();
meshFilter.mesh = mesh;
MeshRenderer meshRenderer = lake.AddComponent<MeshRenderer>();
meshRenderer.material = meshMaterial;
lake.transform.parent = cesiumGeoreference.transform;
lake.AddComponent<CesiumGlobeAnchor>();
}
after running, i can not see any lakes, is any wrong here?
I suspect there is an issue with my method of adding. When I previously tested adding a sphere, I used the model’s coordinate system and only converted the center point coordinates of the sphere. Finally, I just set the position of sphere.transform.position
, and the sphere was displayed correctly.
class MeshDataBuilder {
private CesiumGeoreference cesiumGeoreference;
private List<Vector3> vectors = new List<Vector3>();
private List<int> triangles = new List<int>();
public MeshDataBuilder(CesiumGeoreference cesiumGeoreference, ReadOnlyCollection<IPosition> pts) {
this.cesiumGeoreference= cesiumGeoreference;
convertVectors(pts);
buildTriangles();
}
private void convertVectors(ReadOnlyCollection<IPosition> pts) {
foreach (var pt in pts) {
var p = new double3(pt.Longitude, pt.Latitude, pt.Altitude.GetValueOrDefault(0));
double3 d3 = convert(p, this.cesiumGeoreference);
vectors.Add(new Vector3((float)d3.x, (float)d3.y, (float)d3.z));
}
}
public Vector3[] getVertexs() {
return vectors.ToArray();
}
public int[] getTriangles() {
return triangles.ToArray();
}
private void buildTriangles() {
List<Vertex> vertexes = this.vectors.Select(p => new Vertex(p.x, p.y)).ToList();
var triangulator = new Dwyer();
var mesh = triangulator.Triangulate(vertexes, new Configuration());
var tries = mesh.Triangles;
foreach (Triangle tri in tries) {
for (int i = 0; i < 3; i++) {
this.triangles.Add(tri.GetVertex(i).ID);
}
}
}
private double3 convert(double3 pt, CesiumGeoreference georeference) {
// 先转换经纬度为ecef
var ecet = CesiumWgs84Ellipsoid.LongitudeLatitudeHeightToEarthCenteredEarthFixed(pt);
// 再转换为unity的世界坐标
return georeference.TransformEarthCenteredEarthFixedPositionToUnity(ecet);
//return new Vector3((float)u.x, (float)u.y, (float)u.z);
}
}