PROCEDURAL TERRAIN in Unity! - Mesh Generation

preview_player
Показать описание
Generate a landscape through code!

This video is based on this greatwritten tutorial by Catlike Coding:

····················································································

········································­­·······································­·­····

► All content by Brackeys is 100% free. We believe that education should be available for everyone.

········································­­·······································­·­····

♪ "ES_Dress Code_Black - oomiee" by Epidemic Sound
Рекомендации по теме
Комментарии
Автор

I like how you don't skip steps like other tutors. You're pretty good at getting into the mindset of a beginner and figuring out possible points of confusion. Thanks for putting so much effort in.

lorenzvo
Автор

1:25 i ain't killing my first triangle! *puts script in a backup folder and hugs it protectively*

vidSpac
Автор

Dang now this is how you make a tutorial
I watch it 3 times in a row
Its entertaining

RandomTot
Автор

If you want to texturize your mesh, you need to generate UV's
The simplest way to do this is to make a Vector 2 at the first variables and put this line below the vertices array setup:


vertices[i] = new Vector3(x, 0, z);
uv[i] = new Vector2(z / (float)zSize, x / (float)xSize);


And in the UpdateMesh function you add:
mesh.uv = uv;

bruninhohenrri
Автор

I have been trying to get a hold of vertices and these triangles for some time, but I have not seen anyone teach and explain it better than you do! thank you for making my life a little easier :) it is so much easier to learn when you explain what it is we are doing, and why, rather than just "jumping into it" head on. it helps us to understand what is going on and allows us to one day become less and less dependent on step-by-step tutorials. again, thank you!

Algathar
Автор

code:

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

public class MeshGenerator : MonoBehaviour
{
Mesh mesh;

Vector3[] vertices;
int[] triangles;

public int xSize = 20;
public int zSize = 20;

public float strength = 0.3f;

void Start()
{
mesh = new Mesh();
= mesh;

CreateShape();
UpdateMesh();
}

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


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

triangles = new int[xSize * zSize * 6];

int vert = 0;
int tris = 0;

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++;
}
}

void UpdateMesh()
{
mesh.Clear();

mesh.vertices = vertices;
mesh.triangles = triangles;

mesh.RecalculateNormals();
}
}

happyyt
Автор

Fyi, instead of incrementing "i" separately, you can increment it within the definition of the inner loop, just change "x++" to "x++, i++". Even though the value is defined in the outer loop, you can still increment it in the inner loop. 👍

BrainSlugs
Автор

When adding the vert + or the tris + you can hold down alt and select all the rows to update to only type tris + or vert + one time.

jonathanhart
Автор

thank you! could you make a part 2 of this video? like adding color via height and saving landscapes?

luciacover
Автор

It's impressive how you've been able to explain this in 13 min. Amazing

carduran_
Автор

"Without further ado"
"But 1st Skillshare ad"

pymzojo
Автор

It definately does NOT LIKE a X and Z size of 1, 000 (an extra zero was an un-intentional error on my part), on my machine lol but definately something I'll be playing about with in my spare time. Thanks for an amazingly well presented video on something that can become very complex quickly. I'll probably watch this a couple of times just to make sure it makes sense

erikweeks
Автор

I remember programming what my lecturer called heightfields in OpenGL/Glut back a few semesters in uni. We did this in C++ and this was one of the hardest programming things we ever did because nothing were no automatic functions for stuff like normal calculations like in Unity. Going off this there was the concept of averaging normals so textures between triangles blended with each other giving off a natural or smooth transition between colours. Hardest thing ever.. never got it working though :D

thecalcium
Автор

I really like that you are slowing down when explaining topics. It is really helpful thanks

CubeMasterLewis
Автор

I have no idea about coding and stuff but your channel is very interesting

pegton
Автор

I love how you just say "yaay" when you are done

agitated_cat
Автор

Had to watch at 0.5 speed to keep up 😂

thegamingsnorlax
Автор

after 5 years, I want to say once again, thank you

ramzhamsi
Автор

FINALLY!!! Please do more procedural tutorials!

dcwwekmods
Автор

Pretty well explained, thanks! What will be the NEXT step in this topic?

CouchFerretmakesGames