public void Initialize() { this.controlPoints = new Vector3Grid(this.numberOfControlPointsWidth, this.numberOfControlPointsHeight); for (int m = 0; m < this.numberOfControlPointsWidth; m++) { for (int n = 0; n < this.numberOfControlPointsHeight; n++) { this.controlPoints[m, n] = new Vector3(position.x + m * gridSize, 0f, position.z + n * gridSize); } } RecalculateSurface(); }
public static Mesh GenerateGridMesh(Mesh mesh, Vector3 transformPosition, Vector3Grid points) { if (points == null) { throw new UnityException("No Points! Points not initialized?"); } mesh.Clear(); List <Vector3> vertices = new List <Vector3>(); List <Vector2> uvs = new List <Vector2>(); List <int> triangles = new List <int>(); int width = points.Width; int height = points.Height; for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { Vector3 point = points[x, y]; vertices.Add(point - transformPosition); uvs.Add(new Vector2(point.x, point.z)); } } for (int x = 0; x < width - 1; x++) { for (int y = 0; y < height - 1; y++) { triangles.Add(x * height + y); triangles.Add(x * height + y + 1); triangles.Add((x + 1) * height + y); triangles.Add((x + 1) * height + y); triangles.Add(x * height + y + 1); triangles.Add((x + 1) * height + y + 1); } } mesh.vertices = vertices.ToArray(); mesh.triangles = triangles.ToArray(); mesh.uv = uvs.ToArray(); mesh.RecalculateNormals(); mesh.RecalculateBounds(); ; return(mesh); }
//public static Vector3 P(Array2D<Vector3> controlPoints, int i, int j, float u, float w) //{ // Vector3 P3 = CardinalSpline.P(controlPoints.Get(i + 3, j), controlPoints.Get(i + 3, j + 1), controlPoints.Get(i + 3, j + 2), controlPoints.Get(i + 3, j + 3), w, 0.5f); // Vector3 P2 = CardinalSpline.P(controlPoints.Get(i + 2, j), controlPoints.Get(i + 2, j + 1), controlPoints.Get(i + 2, j + 2), controlPoints.Get(i + 2, j + 3), w, 0.5f); // Vector3 P1 = CardinalSpline.P(controlPoints.Get(i + 1, j), controlPoints.Get(i + 1, j + 1), controlPoints.Get(i + 1, j + 2), controlPoints.Get(i + 1, j + 3), w, 0.5f); // Vector3 P0 = CardinalSpline.P(controlPoints.Get(i + 0, j), controlPoints.Get(i + 0, j + 1), controlPoints.Get(i + 0, j + 2), controlPoints.Get(i + 0, j + 3), w, 0.5f); // Vector3 result = CardinalSpline.P(P0, P1, P2, P3, u, 0.5f); // return result; //} public void RecalculateSurface() { float deltaU = 1f / numberOfSegmentsWidth; float deltaW = 1f / numberOfSegmentsHeight; int arrayWidth = (numberOfSegmentsWidth) * (this.numberOfControlPointsWidth - 3) + 1; int arrayHeight = (numberOfSegmentsHeight) * (this.numberOfControlPointsHeight - 3) + 1; surfacePoints = new Vector3Grid(numberOfSegmentsWidth * (this.numberOfControlPointsWidth - 3) + 1, numberOfSegmentsHeight * (this.numberOfControlPointsHeight - 3) + 1); for (int i = 0; i < this.numberOfControlPointsWidth - 3; i++) { for (int j = 0; j < this.numberOfControlPointsHeight - 3; j++) { for (int u = 0; u < numberOfSegmentsWidth + 1; u++) { for (int w = 0; w < numberOfSegmentsHeight + 1; w++) { surfacePoints[i * numberOfSegmentsWidth + u, j *numberOfSegmentsHeight + w] = P(i, j, u * deltaU, w * deltaW); } } } } }