#Mesh Generation

1 messages · Page 1 of 1 (latest)

carmine urchin
#

@shy copper can you post a picture of your game object and your whole code?

shy copper
#

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

carmine urchin
#

need to statup unity will try it out

shy copper
#

@bleak hatch

bleak hatch
#

I tested it

carmine urchin
#

where is your meshfilter you access? I don't see it in your gameobject

shy copper
#

what

#

you said to put renderer not filter

bleak hatch
#

your object miss a MeshFilter component actually (from what i see on your screen)

#

You need both

shy copper
#

oh it started to work now

#

But i want it to be as a different object

#

not the scripted object

carmine urchin
#

wdym?

shy copper
#

bro

#

i want it to be ad ifferent object not the scripted one

carmine urchin
#

that gets rendered from the filter?
so you essentially want to render some mesh you specified with your public gameobjects?

shy copper
#

what

#

im saying i want it to be ad ifferent objectttttttttttttt not the scripted one

bleak hatch
#

This is not clearer, even with more t

#

What scripted one ?

#

Adding different object, sure, but to what ?

carmine urchin
#

you mean something like this? so a different mesh is rendered?
just set the mesh of the filter to something different

bleak hatch
#

(that's a cool shader)

carmine urchin
#

it's jsut a learning project, who needs naming conventions there xD

shy copper
#

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

carmine urchin
#

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

carmine urchin
#

you need to call it

shy copper
#

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

carmine urchin
#

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;
shy copper
# carmine urchin ```csharp Vector3[][] vertices = new Vector3[chunkWidth][chunkWidth]; for (int ...
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

carmine urchin
#
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

shy copper
#

i changed some things and it worked

carmine urchin
#

that is good

shy copper
#

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?

carmine urchin
#

it needs to be x < chunkWidth not x <= chunkWidth

shy copper
#
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

carmine urchin
#

oh it needs to be *6 not + 6

#

because for each point you have 6

shy copper
#

oh ok thanks

#

now there is no error

#

but there are no triangles

#

@carmine urchin

carmine urchin
#

are you calling update mesh?

shy copper
#
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

carmine urchin
#

somehow it renders on the wrong side

#

the triangles are visible from below

#

I just don't know why

shy copper
#

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

carmine urchin
#

you need to set the vertex position accordingly then
currently your script assumes the position of your mesh is 0 0 0

shy copper
#

the object it as 0,0,0

#

is at*

carmine urchin
#

than it should work

carmine urchin
shy copper
#

idk

#

but well atleast t works

#

well

#

i added perlin noise but...

#

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

carmine urchin
#

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

shy copper
#

with blocks

#

but so it uses those squares

#

cause they are better performant

carmine urchin
#

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

shy copper
#

never thought triangulation would be easier than voxels

carmine urchin
#

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

shy copper
#

nah i can handle that myself

#

i just have issues setting up this optimized system

carmine urchin
shy copper
#

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

carmine urchin
#

You can it just requires a diferent concept, you can directly manipulate vertices, so that is no issue

shy copper
#

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

carmine urchin
#

You would spawn it

shy copper
#

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

carmine urchin
#

You know that everything that gets rendered is lots of triangles, right?

shy copper
#

yeah

#

i actually would prefer to make a triangulated game but i see this as a hard task to do

carmine urchin
#

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

shy copper
#

oh well

#

tihs dude open sourced his code

#

i have no other option

carmine urchin
shy copper
#

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

carmine urchin
#

Oh you mean that, it is pretty normal to put stuf a little bit into the ground if it looks better

shy copper
#

oh no

#

this code is outdated

#

fuck

carmine urchin
#

Its honestly not too complicated, but I don't know how to explain it

shy copper
#

oh i see

#

those guys generate like cubes

#

and then they remove the faces

#

i had the wrong approach

carmine urchin
#

Maybe this helps

#

This is a 2d example