• Advertisement
Sign in to follow this  

Unity Procedural Meshes

This topic is 665 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys! I finished my first implementation of rendering a cubicle world, and... well, it's broken. I can't exactly see what I've done wrong, so I'm hoping you guys could help explain to me where it all broke. It somewhat works, and doesn't immediately crash or anything, but throws out "Over vertice limit" errors when it's physically impossible for it to go over (I did the math). Only visible cubes are rendered, and I was careful to make sure each triangle had its vertices set in clockwise order. Yet, some vertices are way off where they should be. 

 

Block Code:

 

(One face since pretty much all faces are the same code except the vertex positions)

            if (blockupsolid == false)
            {
                int v0;
                int v1;
                int v2;
                int v3;


                Vector3 vert1 = new Vector3(x + -0.5f, y + 0.5f, z + 0.5f);
                meshData.vertices[meshData.verts++] = vert1;
                v1 = meshData.verts;

                Vector3 vert2 = new Vector3(x + 0.5f, y + 0.5f, z + 0.5f);
                meshData.vertices[meshData.verts++] = vert2;
                v2 = meshData.verts;

                Vector3 vert3 = new Vector3(x + 0.5f, y + 0.5f, z + -0.5f);
                meshData.vertices[meshData.verts++] = vert3;
                v3 = meshData.verts;

                Vector3 vert0 = new Vector3(x + -0.5f, y + 0.5f, z + -0.5f);
                meshData.vertices[meshData.verts++] = vert0;
                v0 = meshData.verts;

                meshData.triangles[meshData.tris++] = v3;
                meshData.triangles[meshData.tris++] = v0;
                meshData.triangles[meshData.tris++] = v1;

                meshData.triangles[meshData.tris++] = v1;
                meshData.triangles[meshData.tris++] = v2;
                meshData.triangles[meshData.tris++] = v3;

                meshData.AddUVRange(FaceUVs(Direction.up));

            }
        if (blockupsolid == false)
        {
            meshData.AddFaceCount();
        }

And MeshData, which handles the... well, mesh data.

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

public class MeshData
{
    public Vector3[] vertices;
    public int[] triangles;
    public Vector2[] uv;

    public int vertcount = 0;
    public int tricount = 0;
    public int uvcount = 0;

    public int verts = 0;
    public int tris = 0;
    public int uvs = 0;

    public bool light;

    public bool useRenderDataForCol;
    

    public void AddFaceCount()
    {
        vertcount = vertcount + 4;
        uvcount = uvcount + 4;
        tricount = tricount + 6;
    }
    public void Rebuild()
    {
        vertices = new Vector3[vertcount];
        triangles = new int[tricount];
        uv = new Vector2[uvcount];
    }

    public void AddUVRange(Vector2[] vect2s)
    {
        uv[uvs++] = vect2s[0];

        uv[uvs++] = vect2s[1];

        uv[uvs++] = vect2s[2];

        uv[uvs++] = vect2s[3];

    }
}

Mesh being created:

        if (Generated == true)
        {

            meshData.vertcount = 0;
            meshData.uvcount = 0;
            meshData.tricount = 0;
            meshData.tris = 0;
            meshData.verts = 0;
            meshData.uvs = 0;

            for (updatex = 0; updatex < chunklength; updatex++)
            {
                for (updatey = 0; updatey < chunklength; updatey++)
                {
                    for (updatez = 0; updatez < chunklength; updatez++)
                    {

                        if (blocks[updatex, updatey, updatez].type != BlockTypes.typeEmpty)
                        {
                            blocks[updatex, updatey, updatez].PreCount(this, updatex, updatey, updatez, meshData);
                        }

                    }
                }
            }

            meshData.Rebuild();

            for (updatex = 0; updatex < chunklength; updatex++)
            {
                for (updatey = 0; updatey < chunklength; updatey++)
                {
                    for (updatez = 0; updatez < chunklength; updatez++)
                    {

                        if (blocks[updatex, updatey, updatez].type != BlockTypes.typeEmpty)
                        {
                            meshData = blocks[updatex, updatey, updatez].Blockdata(this, updatex, updatey, updatez, meshData);
                        }
                        
                    }
                }
            }


            RenderMesh();

        }

    }

(Goes thru each chunk, figures out how many vertices and stuff there will be, then actually sets them)?

Edited by SpikeViper

Share this post


Link to post
Share on other sites
Advertisement

I changed v0 = meshData.verts to v0 = meshData.verts - 1. This helped immensely, but I still have missing faces, and my vertex count is still MUCH higher than it should be.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement