Bezier沿着地形表面

问题描述 投票:0回答:1

如果我将一个bezier曲线(设置起点和终点)绘制到Unity Terrain,我希望曲线能够从地面开始起伏,那么最好的方法是什么。

现在我部分实现了这个,(需要连接groundedPoints的新点,作为新的Beziers)

int SegmentCount = Mathf.Floor(BezierLength / SegmentLength);
//Rounded to the next lower integer
var groundedPoints = new List<Vector3>();

for(int i =0; i<SegmentCount;i++){
  Vector3 p = GetPoint(BezierPoints,i / SegmentCount);
    p = p.RayCastDown();
    //RayCasting Down to get the Point on the Terrain
  if(i == 0 || i < SegmentCount -1){
    groundedPoints.Add(p);
  }else{
    if(p.y != groundedPoints[groundedPoints.Count-1].y){
      groundedPoints.Add(p);
      }
   }
}

它现在有点不那么准确,但它不一定是一个真正准确的解决方案。

也许有人可以给我一个提示?谢谢

c# unity3d mathematical-optimization bezier raycasting
1个回答
1
投票

首先,我建议使用Centripetal Catmull–Rom spline,因为它更严格地遵循点,并且需要更少的点来生成(也只在p1和p2之间绘制),但我不知道你想要实现的目的:

我会把你的bezier变成一个2d bezier,只能在它的2d空间中工作,然后当你绘制(在视觉上渲染)时,你使用https://docs.unity3d.com/ScriptReference/Terrain.SampleHeight.html给它一个Y值

我用我的样条函数执行此操作,它最终给出了一个非常精确的样条曲线(道路生成)

请注意!:隐含的Vector2和Vector3转换将不适合您的需要,您需要添加一个扩展方法将Vector3转换为Vector2 :)(Vector(x,y,z)将是Vector(x,y)但是你需要矢量(x,z))

编辑1:

代码示例如何读取地形实际高度,通过Terrain.SampleHeight();通过你确定在地形上方的Vector2坐标,如果Vector2不在地形之上,它将返回null或壁橱地形高度到它我不知道哪个一个atm(现在不能测试):)

public static float GetPoint_On_Terrain(Vector2 point){
    float terrainHeightAtPoint = Terrain.activeTerrain.SampleHeight(new Vector3(point.x, 0, point.y));
    return new Vector3(point.x, terrainHeightAtPoint,point.y);
}
© www.soinside.com 2019 - 2024. All rights reserved.