/// <summary> /// Apply the rectangle to the terrain /// </summary> /// <param name="rect">The rectangle to apply</param> public void ApplyToTerrain(RectVector3 rect, bool isPlateau) { for (float i = 0; i < Details.Divider; i++) { float iDivider = i / Details.Divider; Vector3 top = Vector3.Lerp(rect.TopLeft, rect.TopRight, iDivider); Vector3 bottom = Vector3.Lerp(rect.BottomLeft, rect.BottomRight, iDivider); for (float j = 0; j < Details.Divider; j++) { Vector3 mid = Vector3.Lerp(top, bottom, j / Details.Divider); Vector3 start = GetPos(mid); float heightV = _terrainData.size.y; SetHeight((int)(start.z), (int)(start.x), (start.y / heightV) - Details.OffSet, isPlateau); } } }
/// <summary> /// Apply the terrain height smothly /// </summary> /// <param name="rect">The rectangle to apply</param> public void ApplyToTerrainSoft(RectVector3 rect) { ApplyHeightAsOrigianlHeight(); for (float i = 0; i < Details.Divider; i++) { Vector3 top = Vector3.Lerp(rect.TopLeft, rect.TopRight, i / Details.Divider); // get the height Vector3 topTerrain = top; topTerrain.y = GetTerrainHeight(top); Vector3 bottom = Vector3.Lerp(rect.BottomLeft, rect.BottomRight, i / Details.Divider); for (float j = 0; j < Details.Divider; j++) { float percentage = j / Details.Divider; Vector3 mid = Vector3.Lerp(topTerrain, bottom, percentage); Vector3 start = GetPos(mid); float heightV = _terrainData.size.y; SetHeight((int)start.z, (int)start.x, (start.y / heightV) - Details.OffSet, false); } } }
/// <summary> /// Apply the road cross sections to the terrain /// </summary> /// <param name="rscA">The first cross section</param> /// <param name="rscB">The second cross section</param> public void ApplyToTerrain(RoadCrossSection rscA, RoadCrossSection rscB) { ApplyHeightAsOrigianlHeight(); float angle = rscA.Angle; float x = Mathf.Sin(angle); float z = Mathf.Cos(angle); Vector3 plateauOffSetA = new Vector3(0, 0, 0); plateauOffSetA.x -= (float)(x * Plateau); plateauOffSetA.z += (float)(z * Plateau); Vector3 slopOffSetA = new Vector3(0, 0, 0); slopOffSetA.x -= (float)(x * Slope); slopOffSetA.z += (float)(z * Slope); angle = rscB.Angle; x = Mathf.Sin(angle); z = Mathf.Cos(angle); Vector3 plateauOffSetB = new Vector3(0, 0, 0); plateauOffSetB.x -= (float)(x * Plateau); plateauOffSetB.z += (float)(z * Plateau); Vector3 slopOffSetB = new Vector3(0, 0, 0); slopOffSetB.x -= (float)(x * Slope); slopOffSetB.z += (float)(z * Slope); RectVector3 r; if (!rscB.IsRoadTwisted(rscA)) { r = new RectVector3(rscA.Left, rscB.Left, rscA.Right, rscB.Right); ApplyToTerrain(r, true); r = new RectVector3(rscA.CurbRightEnd - plateauOffSetA, rscB.CurbRightEnd - plateauOffSetB, rscA.CurbRightLip, rscB.CurbRightLip); ApplyToTerrain(r, true); r = new RectVector3(rscA.CurbLeftEnd + plateauOffSetA, rscB.CurbLeftEnd + plateauOffSetB, rscA.CurbLeftLip, rscB.CurbLeftLip); ApplyToTerrain(r, true); if (Slope != 0) { // slope Vector3 slopedA = rscA.CurbRightEnd - (plateauOffSetA + slopOffSetA); Vector3 slopedB = rscB.CurbRightEnd - (plateauOffSetB + slopOffSetB); slopedA.y = 0; slopedB.y = 0; r = new RectVector3(slopedA, slopedB, rscA.CurbRightEnd - plateauOffSetA, rscB.CurbRightEnd - plateauOffSetB); ApplyToTerrainSoft(r); // slope slopedA = rscA.CurbLeftEnd + (plateauOffSetA + slopOffSetA); slopedB = rscB.CurbLeftEnd + (plateauOffSetB + slopOffSetB); slopedA.y = 0; slopedB.y = 0; r = new RectVector3(slopedA, slopedB, rscA.CurbLeftEnd + plateauOffSetA, rscB.CurbLeftEnd + plateauOffSetB); ApplyToTerrainSoft(r); } } else { r = new RectVector3(rscA.Right, rscB.Left, rscA.Left, rscB.Right); ApplyToTerrain(r, true); r = new RectVector3(rscA.CurbRightEnd - plateauOffSetA, rscB.CurbLeftEnd + plateauOffSetB, rscA.CurbRightLip, rscB.CurbLeftLip); ApplyToTerrain(r, true); r = new RectVector3(rscA.CurbLeftEnd + plateauOffSetA, rscB.CurbRightEnd - plateauOffSetB, rscA.CurbLeftLip, rscB.CurbRightLip); ApplyToTerrain(r, true); if (Slope != 0) { // slope Vector3 slopedA = rscA.CurbRightEnd - (plateauOffSetA + slopOffSetA); Vector3 slopedB = rscB.CurbLeftEnd + (plateauOffSetB + slopOffSetB); slopedA.y = 0; slopedB.y = 0; r = new RectVector3(slopedA, slopedB, rscA.CurbRightEnd - plateauOffSetA, rscB.CurbLeftEnd + plateauOffSetB); ApplyToTerrainSoft(r); // slope slopedA = rscA.CurbLeftEnd + (plateauOffSetA + slopOffSetA); slopedB = rscB.CurbRightEnd - (plateauOffSetB + slopOffSetB); slopedA.y = 0; slopedB.y = 0; r = new RectVector3(slopedA, slopedB, rscA.CurbLeftEnd + plateauOffSetA, rscB.CurbRightEnd - plateauOffSetB); ApplyToTerrainSoft(r); } } }