#Mesh Generation
1 messages · Page 1 of 1 (latest)
wait
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TerrainGenerator : MonoBehaviour
{
[SerializeField]
Mesh mesh;
Vector3[] vertices;
int[] triangles;
public GameObject TerrainFolder;
public GameObject grass;
public GameObject ground;
public int chunkHeight;
public int chunkWidth;
void Start()
{
mesh = new Mesh();
GetComponent<MeshFilter>().mesh = mesh;
BuildMesh();
}
void BuildMesh()
{
Mesh mesh = new Mesh();
mesh.vertices = new Vector3[]
{
new Vector3(0, 0),
new Vector3(0, 1),
new Vector3(1, 1),
new Vector3(1, 0),
};
mesh.triangles = new int[] { 0, 1, 2, 0, 2, 3 };
GetComponent<MeshFilter>().mesh = mesh;
}
void UpdateMesh()
{
mesh.Clear();
mesh.vertices = vertices;
mesh.triangles = triangles;
}
private void OnDrawGizmos()
{
for (int i = 0; i < vertices.Length; i++)
{
Gizmos.DrawSphere(vertices[i], .1f);
}
}
}```
@carmine urchin
need to statup unity will try it out
@bleak hatch
I tested it
where is your meshfilter you access? I don't see it in your gameobject
your object miss a MeshFilter component actually (from what i see on your screen)
You need both
oh it started to work now
But i want it to be as a different object
not the scripted object
wdym?
that gets rendered from the filter?
so you essentially want to render some mesh you specified with your public gameobjects?
This is not clearer, even with more t
What scripted one ?
Adding different object, sure, but to what ?
you mean something like this? so a different mesh is rendered?
just set the mesh of the filter to something different
(that's a cool shader)
it's jsut a learning project, who needs naming conventions there xD
guys
i updated code
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public class TerrainGenerator : MonoBehaviour
{
[SerializeField]
Mesh mesh;
Vector3[] vertices;
int[] triangles;
public GameObject TerrainFolder;
public GameObject grass;
public GameObject ground;
public int chunkHeight;
public int chunkWidth;
void Start()
{
mesh = new Mesh();
GetComponent<MeshFilter>().mesh = mesh;
//BuildMesh();
for (int x = 0; x < chunkWidth; x++)
{
for (int z = 0; z < chunkWidth; z++)
{
mesh.vertices = new Vector3[]
{
new Vector3(x, 0, z),
};
}
}
}
void BuildMesh()
{
Mesh mesh = new Mesh();
mesh.vertices = new Vector3[]
{
new Vector3(0, 0),
new Vector3(0, 1),
new Vector3(1, 1),
new Vector3(1, 0),
};
mesh.triangles = new int[] { 0, 1, 2, 0, 2, 3 };
GetComponent<MeshFilter>().mesh = mesh;
}
void UpdateMesh()
{
mesh.Clear();
mesh.vertices = vertices;
mesh.triangles = triangles;
}
private void OnDrawGizmos()
{
for (int i = 0; i < vertices.Length; i++)
{
Gizmos.DrawSphere(vertices[i], .1f);
}
}
}```
why does it not draw vertices
but if you want to do terrain generation, the process is the following:
-generate a large grid
-set the vertice heights with a random noise function (mostly perlin in some variation)
-recalculate the normals
-update the mesh collider (so you can walk on it)
-do object spawning:
-get random points on your generated mesh and instantiate some prefabs there
you do not call buildMesh
you need to call it
but i want vertices
not triangles
ok ill just copy the create shape in the tutorial you sent
nvm i cant
actually i can for now
Vector3[][] vertices = new Vector3[chunkWidth][chunkWidth];
for (int x = 0; x < chunkWidth; x++)
{
for (int z = 0; z < chunkWidth; z++)
{
vertices[x][z] = new Vector3(x,0,z);
}
}
mesh.vertices = vertices;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public class TerrainGenerator : MonoBehaviour
{
[SerializeField]
Mesh mesh;
Vector3[] vertices;
int[] triangles;
public GameObject TerrainFolder;
public GameObject grass;
public GameObject ground;
public int chunkHeight;
public int chunkWidth;
void Start()
{
mesh = new Mesh();
GetComponent<MeshFilter>().mesh = mesh;
BuildMesh();
}
void BuildMesh()
{
vertices = new Vector3[(chunkWidth + 1) * (chunkWidth + 1)];
for (int i = 0, x = 0; x <= chunkWidth; x++)
{
for (int z = 0; z <= chunkWidth; z++)
{
vertices[i] = new Vector3(x, 0, z);
i++;
}
}
/* Mesh mesh = new Mesh();
mesh.vertices = new Vector3[]
{
new Vector3(0, 0),
new Vector3(0, 1),
new Vector3(1, 1),
new Vector3(1, 0),
};
mesh.triangles = new int[] { 0, 1, 2, 0, 2, 3 };
// GetComponent<MeshFilter>().mesh = mesh;*/
}
void UpdateMesh()
{
mesh.Clear();
mesh.vertices = vertices;
mesh.triangles = triangles;
}
private void OnDrawGizmos()
{
if (vertices == null) return;
for (int i = 0; i < vertices.Length; i++)
{
Gizmos.DrawSphere(vertices[i], .1f);
}
}
}```
is this supposed to work
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public class test : MonoBehaviour
{
[SerializeField]
Mesh mesh;
Vector3[] vertices;
int[] triangles;
public GameObject TerrainFolder;
public GameObject grass;
public GameObject ground;
public int chunkHeight;
public int chunkWidth;
void Start()
{
mesh = new Mesh();
GetComponent<MeshFilter>().mesh = mesh;
//BuildMesh();
vertices = new Vector3[chunkWidth*chunkWidth];
for (int i = 0, x = 0; x < chunkWidth; x++)
{
for (int z = 0; z < chunkWidth; z++)
{
vertices[i++] = new Vector3(x,0,z);
}
}
mesh.vertices = vertices;
}
void BuildMesh()
{
Mesh mesh = new Mesh();
mesh.vertices = new Vector3[]
{
new Vector3(0, 0),
new Vector3(0, 1),
new Vector3(1, 1),
new Vector3(1, 0),
};
mesh.triangles = new int[] { 0, 1, 2, 0, 2, 3 };
GetComponent<MeshFilter>().mesh = mesh;
}
void UpdateMesh()
{
mesh.Clear();
mesh.vertices = vertices;
mesh.triangles = triangles;
}
private void OnDrawGizmos()
{
for (int i = 0; i < vertices.Length; i++)
{
Gizmos.DrawSphere(vertices[i], .1f);
}
}
}
this works
you didn't set the vertice array
that is good
triangles[tris + 4] = vert + chunkWidth + 1;
void BuildMesh()
{
vertices = new Vector3[(chunkWidth + 1) * (chunkWidth + 1)];
for (int i = 0, x = 0; x <= chunkWidth; x++)
{
for (int z = 0; z <= chunkWidth; z++)
{
vertices[i] = new Vector3(x, 0, z);
i++;
}
}
triangles = new int[chunkWidth * chunkWidth + 6];
int vert = 0;
int tris = 0;
for (int x = 0; x <= chunkWidth; x++)
{
for (int z = 0; z <= chunkWidth; z++)
{
triangles[tris + 0] = vert + 0;
triangles[tris + 1] = vert + chunkWidth + 1;
triangles[tris + 2] = vert + 1;
triangles[tris + 3] = vert + 1;
triangles[tris + 4] = vert + chunkWidth + 1;
triangles[tris + 5] = vert + chunkWidth + 2;
vert++;
tris += 6;
}
vert++;
}
} ```
whats wrong?
it needs to be x < chunkWidth not x <= chunkWidth
void BuildMesh()
{
vertices = new Vector3[(chunkWidth + 1) * (chunkWidth + 1)];
for (int i = 0, x = 0; x <= chunkWidth; x++)
{
for (int z = 0; z <= chunkWidth; z++)
{
vertices[i] = new Vector3(x, 0, z);
i++;
}
}
triangles = new int[chunkWidth * chunkWidth + 6];
int vert = 0;
int tris = 0;
for (int x = 0; x < chunkWidth; x++)
{
for (int z = 0; z < chunkWidth; z++)
{
triangles[tris + 0] = vert + 0;
triangles[tris + 1] = vert + chunkWidth + 1;
triangles[tris + 2] = vert + 1;
triangles[tris + 3] = vert + 1;
triangles[tris + 4] = vert + chunkWidth + 1;
triangles[tris + 5] = vert + chunkWidth + 2;
vert++;
tris += 6;
}
vert++;
}
} ```
still the same
are you calling update mesh?
void Start()
{
mesh = new Mesh();
GetComponent<MeshFilter>().mesh = mesh;
BuildMesh();
UpdateMesh();
}```
actually
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public class TerrainGenerator : MonoBehaviour
{
[SerializeField]
Mesh mesh;
Vector3[] vertices;
int[] triangles;
public GameObject TerrainFolder;
public GameObject grass;
public GameObject ground;
public int chunkHeight;
public int chunkWidth;
void Start()
{
mesh = new Mesh();
GetComponent<MeshFilter>().mesh = mesh;
BuildMesh();
UpdateMesh();
}
void BuildMesh()
{
vertices = new Vector3[(chunkWidth + 1) * (chunkWidth + 1)];
for (int i = 0, x = 0; x <= chunkWidth; x++)
{
for (int z = 0; z <= chunkWidth; z++)
{
vertices[i] = new Vector3(x, 0, z);
i++;
}
}
triangles = new int[chunkWidth * chunkWidth * 6];
int vert = 0;
int tris = 0;
for (int x = 0; x < chunkWidth; x++)
{
for (int z = 0; z < chunkWidth; z++)
{
triangles[tris + 0] = vert + 0;
triangles[tris + 1] = vert + chunkWidth + 1;
triangles[tris + 2] = vert + 1;
triangles[tris + 3] = vert + 1;
triangles[tris + 4] = vert + chunkWidth + 1;
triangles[tris + 5] = vert + chunkWidth + 2;
vert++;
tris += 6;
}
vert++;
}
}
void UpdateMesh()
{
mesh.Clear();
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.RecalculateNormals();
}
private void OnDrawGizmos()
{
if (vertices == null) return;
for (int i = 0; i < vertices.Length; i++)
{
Gizmos.DrawSphere(vertices[i], .1f);
}
}
}```
all code
pls help
somehow it renders on the wrong side
the triangles are visible from below
I just don't know why
yup chat gpt noticed this
triangles[tris + 0] = vert + 0;
triangles[tris + 1] = vert + 1;
triangles[tris + 2] = vert + chunkWidth + 1;
triangles[tris + 3] = vert + 1;
triangles[tris + 4] = vert + chunkWidth + 2;
triangles[tris + 5] = vert + chunkWidth + 1;
this fixes it
I also noticed
those triangles are far away from the vertices lmao
you need to set the vertex position accordingly then
currently your script assumes the position of your mesh is 0 0 0
than it should work
the question is why, I use the old version in my procedural terrain and it works
idk
but well atleast t works
well
i added perlin noise but...
yeah
this is now illegal minecraft
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public class TerrainGenerator : MonoBehaviour
{
[SerializeField]
Mesh mesh;
Vector3[] vertices;
int[] triangles;
public GameObject TerrainFolder;
public GameObject grass;
public GameObject ground;
public int chunkHeight;
public int chunkWidth;
public float Scale;
public float Amplitude;
void Start()
{
mesh = new Mesh();
GetComponent<MeshFilter>().mesh = mesh;
BuildMesh();
UpdateMesh();
}
void BuildMesh()
{
vertices = new Vector3[(chunkWidth + 1) * (chunkWidth + 1)];
for (int i = 0, x = 0; x <= chunkWidth; x++)
{
for (int z = 0; z <= chunkWidth; z++)
{
float Noise = Mathf.Round(Mathf.PerlinNoise(x * Scale, z * Scale) * Amplitude);
vertices[i] = new Vector3(x, Noise, z);
i++;
}
}
triangles = new int[chunkWidth * chunkWidth * 6];
int vert = 0;
int tris = 0;
for (int x = 0; x < chunkWidth; x++)
{
for (int z = 0; z < chunkWidth; z++)
{
triangles[tris + 0] = vert + 0;
triangles[tris + 1] = vert + 1;
triangles[tris + 2] = vert + chunkWidth + 1;
triangles[tris + 3] = vert + 1;
triangles[tris + 4] = vert + chunkWidth + 2;
triangles[tris + 5] = vert + chunkWidth + 1;
vert++;
tris += 6;
}
vert++;
}
}
void UpdateMesh()
{
mesh.Clear();
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.RecalculateNormals();
}
}```
i deleted the gizmos part because too big code
(its still in my unity but discord 2000 word limit)
how can i make it so it is lik eminecraft terrain generator
its blocky
not those ugly triangles
Do you want to make a minecraft style terrain? With blocks?
I know the theory about voxel generation but helping you might be hsrd
If you want a more realistic map you just need to scale the perlin noise
yes
with blocks
but so it uses those squares
cause they are better performant
Oh yea how it works is basically you check all your heights generated by perlin and map it to the values your blocks or end at
So you mighr have blocks ending at height 0, 1, 2 and so on
Next thing is you draw the faces up there.
So you have a mesh with all the faces on top now you need to fill the gaps by adding more squares
I only know the theory tho, can't help you with how it is done
never thought triangulation would be easier than voxels
And if you don't just want an overworld but also caves and shit like that you might want to look into 3d perlin noise
Voxels are harder because they don't have so much funding behind them, the whole industry uses triangulation, so gpus are build for exactly that, engines are written for that, voxels is a concept you rarely see and even they get rendered with triangulation
well
with triangulation
you cant make a proper game
I mean like
when you like want to make a mining system
how are you gonna do it
the triangles are randomly placed
with voxels you have direct control
You can it just requires a diferent concept, you can directly manipulate vertices, so that is no issue
no you dont understand
how are you gonna make a tree for example
if its a triangle
if you make it a block it will float
You would spawn it
yes but since the world is made from triangles
and if you make the trunk a block
then what happens is the tree is gonna pretty much float
You know that everything that gets rendered is lots of triangles, right?
yeah
i actually would prefer to make a triangulated game but i see this as a hard task to do
So you can make everything out of it, it just requires some creative thinking
Good luck with your voxel project
Since everyone is doing minecraft clones you probably find a tutorial on youtube
oh well
Can I make Minecraft in one day without the internet?
So I was bored and decided to bust out the good ol' unity game engine and see how just hard it would be to create the classic blocky voxel sandbox game. To make it interesting and really test my skills, I wasn't allowed to access any external resources/tutorials/code.
→ Download for some re...
tihs dude open sourced his code
i have no other option
Oh yea it, you wouldn't randomly generate a tree most times (it would be an awfully complex algorithm) but you model it in blender and make it interactable
no you still dont get it
even if you have a trunk block
and try to place it on the ground
then a bit of the trunk is either gonna be inside the ground
or its gonna be floating
unless you calculate a triangle between the vertices but uhhhh
there may be vertices below or above
and then it just starts going downhill
Oh you mean that, it is pretty normal to put stuf a little bit into the ground if it looks better
Its honestly not too complicated, but I don't know how to explain it