Delete hidden vertices Unity 3D

Asked

Viewed 32 times

0

I was studying procedural generation of land using mesh and decided to create a world like Minecraft. And I need to make it just create cubes that are visible to the player. Because there are cubes that have been created that are hidden and don’t need to be there. How can I recognize that there is a solid block next to all created cubes, if there is, do not create this cube?

Muitas vértices e faces desnecessárias

Voxel.Cs:

using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshCollider))]
[RequireComponent(typeof(MeshRenderer))]
public class Voxel : MonoBehaviour {

    public Vector3[] vertices;
    public int[] triangulos;

    public Vector3 pos;
    public GameObject chunk;
    Mesh mesh;

    public Voxel(Vector3 posicao, GameObject obj)
    {
        pos = posicao;
        chunk = obj;
    }

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

        //Cubo(0,0,0);
    }
    
    
    public void Cubo(int x, int y, int z)
    {

        GameObject cubo = new GameObject("Cubo");
        cubo.AddComponent(typeof(MeshFilter));
        cubo.AddComponent(typeof(MeshCollider));
        cubo.AddComponent(typeof(MeshRenderer));
        cubo.transform.parent = chunk.transform;
        mesh = cubo.GetComponent<MeshFilter>().mesh;

        //cubo.transform.position = pos;

        vertices = new Vector3[]
        {
            new Vector3(x,y,z),         // 0
            new Vector3(x,y+1,z),       // 1
            new Vector3(x+1,y+1,z),     // 2
            new Vector3(x+1,y,z),       // 3
            new Vector3(x+1,y,z+1),     // 4
            new Vector3(x+1,y+1,z+1),   // 5
            new Vector3(x,y+1,z+1),     // 6
            new Vector3(x,y,z+1)        // 7
        };

        triangulos = new int[]
        {
            0,1,2,  0,2,3,              // Face frontal
            3,2,5,  3,5,4,              // Face direita
            0,7,6,  0,6,1,              // Face esquerda
            7,4,5,  7,5,6,              // Face traseira
            1,6,5,  1,5,2,              // Face superior
            0,3,4,  0,4,7               // Face inferior
        };

        UpdateMesh();
    }

    void UpdateMesh()
    {
        mesh.Clear();

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

        mesh.RecalculateNormals();
    }
}

Chunk.Cs:

using System.Collections.Generic;
using UnityEngine;

public class Chunk : MonoBehaviour {

    public int tx, ty, tz;
    public Vector3 pos;

    IEnumerator ConstruirChunk()
    {
        for (int x = 0; x < tx; x++)
        {
            for (int y = 0; y < ty; y++)
            {
                for (int z = 0; z < tz; z++)
                {
                    pos = new Vector3(x,y,z);
                    Voxel bloco = new Voxel(pos, this.gameObject);

                    bloco.Cubo(x,y,z);
                    yield return null;
                }
            }
        }
    }

    void Start () 
    {
        StartCoroutine(ConstruirChunk());
    }
}
  • I think the center of your question is to understand what defines "visible to the player". Is it visible from a starting point? Or the fixed camera? Or the world has a limit (rectangular or circular for ex) and does not need to create beyond the final "wall"? Explains a little more of the idea.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.