How to create and modify a plane mesh in Unity (Procedural mesh generation tutorial)

preview_player
Показать описание
In this video I go over how I procedurally generate a plane mesh with a desired size and resolution. Then I show how I use a sine wave to make it wavy in real time.

Note: While not shown in the video, I made anti-clockwise version of my triangles to be able to see the back face of my meshes regardless of material.

This other video of mine goes over mesh generation basics and I hope it clears out anything I may have skipped over in this one:

Some feedback I received and want to pass along: to help perfromance Vertex shaders might do a more efficient job at animating a mesh, also storing an unmodified mesh and modifying that one as opposed to rebuilding from scratch every time will help.

#unity #mesh #generation #tutorial

0:00 setup
0:12 Creating the plane
0:57 Modifying the plane
Рекомендации по теме
Комментарии
Автор

I would spend 2 hours learning this with any other tutorial, but you are so quick and to the point!

Hobby_Technology
Автор

As a fun follow up you should try to take that sine thing into a vertex shader! That would make it happen on the GPU (So pretty much all vertices in parallel) and not on the CPU as it is doing here. Far better performance and a way to show the usability of such a plane while also introducing people into to how to take advantage of the GPU (Directly on the rendering pipeline, not just as compute shaders)

sebastiangudino
Автор

Your channel is another hidden gem I've found recently. Thank you for those wonderful videos.

K.Z
Автор

Little tip : if you want to add light to your plane you have to calculate the normals, Unity has a function for it called RecalculateNormals()

For instance :
void AssignMesh()
{
myMesh.Clear();
myMesh.vertices = vertices.ToArray();
myMesh.triangles = triangles.ToArray();
myMesh.RecalculateNormals();
}

Lynnox_lol
Автор

Nice tutorial. I started making procedural meshes since 2 days. This tutorial helps solving the puzzle.

Nathan-kiqn
Автор

I added the uvs and normals with just a line of code in the x-loop for the verteces: uv.Add(new Vector2((float)x / resolution, (float)y / resolution)); and normals.Add(new Vector3(0, 1, 0));

MicheleZaccagnini
Автор

How comes it took me so long to discover this gem? thank you!

amatiasq
Автор

Hi, can you put the script to download?

ResuelveloTu
Автор

Thank you for the great tutorial, Sir. 🙏🏼

shitshow_
Автор

Something strange happens if I increase the resolution above 250: the plane gets chopped off in one dimension (the z plane). I am wrecking my brain looking for what might be causing this. Performance in my case is not an issue so I would like to have high definition (I am applying lots of deformation). Any idea what might be causing this?

MicheleZaccagnini
Автор

using System.Collections.Generic;
using Unity.Properties;
using Unity.VisualScripting;
using UnityEngine;



public class PlaneGenerator : MonoBehaviour
{
//references
Mesh myMesh;
MeshFilter meshFilter;

//plane settings
[SerializeField] Vector2 planeSize = new Vector2(1, 1);
[SerializeField] int planeResolution = 1;

//mesh values
List<Vector3> vertices;
List<int> triangles;

//sdafag
void Awake()
{
myMesh = new Mesh();
meshFilter = GetComponent<MeshFilter>();
meshFilter.mesh = myMesh;
}

void Update()
{
//aggshd
planeResolution = Mathf.Clamp(planeResolution, 1, 50);

GeneratePlane(planeSize, planeResolution);
AssignMesh();
}

void GeneratePlane (Vector2 size, int resolution)
{
//Create vertices
vertices = new List<Vector3>();
float xPerStep = size.x/resolution;
float yPerStep = size.y/resolution;
for(int y = 0; y<resolution+1; y++)
{
for(int x = 0; x<resolution+1; x++)
{
vertices.Add(new Vector3(x*xPerStep, 0, y*yPerStep));
}
}

//Create triangles
triangles = new List<int>();
for(int row = 0; row<resolution; row++)
{
for(int column = 0; column<resolution; column++)
{
int i = (row*resolution) + row + column;
//first triangle
triangles.Add(i);



//second triangle
triangles.Add(i);

triangles.Add(i+1);
}
}
}

void AssignMesh()
{
myMesh.Clear();
myMesh.vertices = vertices.ToArray();
myMesh.triangles = triangles.ToArray();
}
}

Mdeel
Автор

Hello.
You have a very cool explanation. But I ran into a problem, I want to add my own material to the mesh. The texture will be an atlas and the texture will be applied depending on the wave height.
However, I did not succeed. Can you tell us more about UV, how to work with texture atlases, etc :)
Really TNX!!!

ALDK
Автор

i really dont understand this, could you go more in depth?

thecobra