TerrainChunk.Initialize C# (CSharp) Method

Initialize() public method

public Initialize ( ) : void
return void
    public void Initialize()
    {
        //this.chunk_size = chunk_sz;
        //gridSize = size;
        //voxelSize = size / (float)chunk_sz;

        mesh = null;
        meshes = transform.FindChild("Meshes");

        // TODO: optimize this with preallocated array
        gen_vertices  = new List<Vector3>();
        gen_uv        = new List<Vector2>();
        vox_uv        = new Vector2[4];
        gen_triangles = new List<int>();
    }

Usage Example

Ejemplo n.º 1
0
    private TerrainChunk CreateChunk(Vector2[] vertices2D, int idx)
    {
        int vertexIdx = 1;

        int[] indices = new int[vertices2D.Length * 3];

        for (int i = 0; i < indices.Length; i += 3)
        {
            indices[i]     = 0; // we know the first vertex is the centroid
            indices[i + 1] = vertexIdx;

            // wrap around
            int nextVertex = (vertexIdx + 1 < vertices2D.Length) ? vertexIdx + 1 : 1;

            indices[i + 2] = nextVertex;

            vertexIdx = nextVertex;
        }

        // Create the Vector3 vertices
        Vector3[] vertices = new Vector3[vertices2D.Length];
        for (int i = 0; i < vertices.Length; i++)
        {
            vertices[i] = new Vector3(vertices2D[i].x, vertices2D[i].y, 0.0f);
        }

        // create the uvs
        Vector2[] uvs = new Vector2[vertices.Length];
        for (int i = 0; i < uvs.Length; i++)
        {
            uvs[i] = new Vector2(vertices[i].x / textureScale, vertices[i].y / textureScale);
        }

        Mesh msh = new Mesh();

        msh.vertices  = vertices;
        msh.triangles = indices;
        msh.RecalculateNormals();
        msh.RecalculateBounds();
        msh.uv = uvs;

        // Set up game object with mesh;
        TerrainChunk chunkObj = (TerrainChunk)Instantiate(chunkPrototype);

        chunkObj.gameObject.AddComponent(typeof(MeshRenderer));
        MeshFilter filter = chunkObj.gameObject.AddComponent(typeof(MeshFilter)) as MeshFilter;

        filter.mesh = msh;

        chunkObj.Initialize(idx, vertices2D, indices);

        return(chunkObj);
    }
All Usage Examples Of TerrainChunk::Initialize