Looping Around the Edge of a Grid

Hello, is there any possible way to set the height between specific vertices?

This is basic code for creating a grid below, but how could i set the heights of just the edge cells, and leave the middle flat? or vice versa. Is there a way to specify which vertices get adjusted??

im pretty new at C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


[RequireComponent(typeof(MeshFilter))]
public class GridPlacement : MonoBehaviour
{
    public int xSize, zSize;

    private Vector3[] vertices;
    private int[] triangles;
    Mesh mesh;


    public void Start()
    {
        mesh = new Mesh();
        GetComponent<MeshFilter>().mesh = mesh;

        Generate();
        UpdateMesh();
    }

    void UpdateMesh()
    {
        mesh.Clear();

        mesh.vertices = vertices;
        mesh.triangles = triangles;
        mesh.RecalculateNormals();
    }

    private void Generate()
    {
      

        vertices = new Vector3[(xSize + 1) * (zSize + 1)];

        for (int i = 0, z = 0; z <= zSize; z++)
        {
            for (int x = 0; x <= xSize; x++, i++)
            {
                float y = Mathf.PerlinNoise(x * 0.3f, z * 0.3f) * 2f;
                vertices[i] = new Vector3(x, y, z);
              
            }
        }

        int vert = 0;
        int tris = 0;
        triangles = new int[xSize * zSize * 6];


        for (int z = 0; z < zSize; z++)
        {
            for (int x = 0; x < xSize; x++)
            {

                triangles[tris + 0] = vert + 0;
                triangles[tris + 1] = vert + xSize + 1;
                triangles[tris + 2] = vert + 1;
                triangles[tris + 3] = vert + 1;
                triangles[tris + 4] = vert + xSize + 1;
                triangles[tris + 5] = vert + xSize + 2;

                vert++;
                tris += 6;
            }
            vert++;
        }
      
    }


    private void OnDrawGizmos()
    {
        if (vertices == null)
        {
            return;
        }

        Gizmos.color = Color.green;
        for (int i = 0; i < vertices.Length; i++)
        {
            Gizmos.DrawSphere(vertices[i], 0.1f);
        }
    }

}

Welcome to procedural geometry programming! Unity3D is the coolest and easiest engine in the entire world to do procedural geometry in, and I absolutely love it. You are going to have a BALL!

In the code above, to get all the edge pieces up a bit more than the others, how about going to Line 45 or so and writing something like “if z == 0 or x == 0 or z == ZSize, etc.” then raise the Y some amount before making the Vector3() and sticking it in your vert array.

As you gain more comfort in this stuff, you’re gonna keep coming up with greater and cooler ideas for making geometry, and I can see you’re already well on your way. I have a little open source project called MakeGeo where I fiddle with this stuff.

MakeGeo is presently hosted at these locations:

https://bitbucket.org/kurtdekker/makegeo

1 Like

You are probably going for something similar to procedural height map mesh generation?

In the video, a noise function is used to get some value per coordinate, which is then used to increase the height of that vertice in the mesh, effectively using the noise function to create a height map, which can be considered terrain.
I believe it may even be the video you got your code from? Anyways, that way you can set the height of a vertice. If you only want to change specific vertices, then you need to somehow define which vertices you mean.
If, for example, you really only want the corners to be affected, then do it like Kurt-Dekker suggested. Similarly, you could create a slope by calculating each vertices minimum distance to some corner, and base its height on that. If you, for some reason, want every second vertice to be affected, then only adjust the height on even numbers in the loop. If you wanted to create a 3D parabola-like shape from your mesh, you’d plug the coordinates into a function for a 3D parabola to get your height values. If you wanted the 3D parabola-like shape to be flat on the ground in the middle, you’d exclude any result below 0 by remapping it to be 0. And so on. As long as you can define what you want, you can really do whatever you want.

If you have a specific problem you need help on you can try to explain it so we can help you better.

thanks, that was perfect