Initialize() public method

public Initialize ( ) : void
return void
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);
    }
    void UpdateVisibleChunks()
    {
        // Set previously visible chunks invisible
        for (int i = 0; i < mapChunksVisibleLastUpdate.Count; i++)
        {
            mapChunksVisibleLastUpdate[i].SetVisible(false);
        }
        mapChunksVisibleLastUpdate.Clear();

        // Set now visible chunks visible
        int currentChunkCoordX = Mathf.RoundToInt(ViewerPosition.x / ChunkSize);
        int currentChunkCoordY = Mathf.RoundToInt(ViewerPosition.y / ChunkSize);

        for (int yOffset = -ChunksVisibleInViewDistance; yOffset <= ChunksVisibleInViewDistance; yOffset++)
        {
            for (int xOffset = -ChunksVisibleInViewDistance; xOffset <= ChunksVisibleInViewDistance; xOffset++)
            {
                // Coordinates of current chunk in loop
                Vector2 viewedChunkCoord = new Vector2(currentChunkCoordX + xOffset, currentChunkCoordY + yOffset);

                // Only load chunk if it is in world map bounds
                if (viewedChunkCoord.x >= 0 && viewedChunkCoord.x < WorldMapWidth && viewedChunkCoord.y >= 0 && viewedChunkCoord.y < WorldMapHeight)
                {
                    if (TerrainChunkDictionary.ContainsKey(viewedChunkCoord))
                    {
                        TerrainChunk loadedChunk = TerrainChunkDictionary[viewedChunkCoord];
                        loadedChunk.UpdateChunk();
                        if (loadedChunk.IsVisible())
                        {
                            mapChunksVisibleLastUpdate.Add(loadedChunk);
                        }
                    }
                    else
                    {
                        GameObject   newTerrainChunkObject = new GameObject();
                        TerrainChunk newTerrainChunk       = newTerrainChunkObject.AddComponent <TerrainChunk>();
                        newTerrainChunk.Initialize(viewedChunkCoord, ChunkSize, this, MapMaterial);
                        TerrainChunkDictionary.Add(viewedChunkCoord, newTerrainChunk);
                    }
                }
            }
        }
    }
Ejemplo n.º 3
0
    public void Initialize(GameObject parentGO, EnvironmentGenome genome, Material mat, Vector2 position, Vector2 scale, int maxLOD)
    {
        this.maxLOD         = maxLOD;
        this.groundMaterial = mat;
        this.position       = position;
        this.scale          = scale;

        gameObject.transform.parent = parentGO.transform;


        GameObject rootChunkGO = new GameObject("rootChunk");

        rootChunkGO.transform.parent = parentGO.transform;
        rootChunk        = rootChunkGO.AddComponent <TerrainChunk>();
        rootChunk.maxLOD = this.maxLOD;
        //position = new Vector2(0f, 0f);
        //scale = new Vector2(320f, 320f);
        rootChunk.maxLOD = this.maxLOD;
        rootChunk.Initialize(gameObject, genome, 0, 0, 61, 61, new Vector2(40f, 40f), position, scale, groundMaterial);
    }
Ejemplo n.º 4
0
    public void Initialize(GameObject parentChunk, EnvironmentGenome genome, int id, int lod, int resolutionX, int resolutionZ, Vector2 arenaBounds, Vector2 position, Vector2 scale, Material mat)
    {
        this.parentChunk = parentChunk;
        this.id          = id;
        this.resolutionX = resolutionX;
        this.resolutionZ = resolutionZ;
        this.lod         = lod;
        if (idList == null)
        {
            idList = new List <int>();
        }
        idList.Add(id);

        // Calculate bounds of this chunk:
        thisNorth = position.y + scale.y;
        thisEast  = position.x + scale.x;
        thisSouth = position.y - scale.y;
        thisWest  = position.x - scale.x;

        bool end = false;

        if (!this.CheckForContainsRect(arenaBounds.y * 0.5f, arenaBounds.x * 0.5f, -arenaBounds.y * 0.5f, -arenaBounds.x * 0.5f))
        {
            end = true;
        }
        if (this.lod >= maxLOD)
        {
            end = true;
        }
        //Debug.Log("TerrainManager! id: " + id.ToString() + ", lod: " + lod.ToString() + ", pos: " + position.ToString() + ", scale: " + scale.ToString() + " end: " + end.ToString());
        if (end)
        {
            // Build!
            BuildChunk(genome, position, scale, mat);
        }
        else
        {
            //Debug.Log("Split!");
            // Split!
            //NE:
            int        childID   = 1; // 0 Center 1 NE,  2 SE,  3 SW,  4 NW
            GameObject chunkNEGO = new GameObject("chunk_" + childID.ToString() + "." + lod.ToString());
            chunkNE = chunkNEGO.AddComponent <TerrainChunk>();
            chunkNEGO.transform.parent        = gameObject.transform;
            chunkNEGO.transform.localPosition = new Vector3(0f, 0f, 0f);
            chunkNE.maxLOD = this.maxLOD;
            chunkNE.Initialize(gameObject, genome, childID, this.lod + 1, this.resolutionX, this.resolutionZ, arenaBounds, position + (scale * 0.5f), scale * 0.5f, mat);

            //SE:
            childID = 2;  // 0 NE,  1 SE,  2 SW,  3 NW
            GameObject chunkSEGO = new GameObject("chunk_" + childID.ToString() + "." + lod.ToString());
            chunkSE = chunkSEGO.AddComponent <TerrainChunk>();
            chunkSEGO.transform.parent        = gameObject.transform;
            chunkSEGO.transform.localPosition = new Vector3(0f, 0f, 0f);
            chunkSE.maxLOD = this.maxLOD;
            chunkSE.Initialize(gameObject, genome, childID, this.lod + 1, this.resolutionX, this.resolutionZ, arenaBounds, new Vector2(position.x + (scale.x * 0.5f), position.y - (scale.y * 0.5f)), scale * 0.5f, mat);

            //SE:
            childID = 3;  // 0 NE,  1 SE,  2 SW,  3 NW
            GameObject chunkSWGO = new GameObject("chunk_" + childID.ToString() + "." + lod.ToString());
            chunkSW = chunkSWGO.AddComponent <TerrainChunk>();
            chunkSWGO.transform.parent        = gameObject.transform;
            chunkSWGO.transform.localPosition = new Vector3(0f, 0f, 0f);
            chunkSW.maxLOD = this.maxLOD;
            chunkSW.Initialize(gameObject, genome, childID, this.lod + 1, this.resolutionX, this.resolutionZ, arenaBounds, new Vector2(position.x - (scale.x * 0.5f), position.y - (scale.y * 0.5f)), scale * 0.5f, mat);

            //SE:
            childID = 4;  // 0 NE,  1 SE,  2 SW,  3 NW
            GameObject chunkNWGO = new GameObject("chunk_" + childID.ToString() + "." + lod.ToString());
            chunkNW = chunkNWGO.AddComponent <TerrainChunk>();
            chunkNWGO.transform.parent        = gameObject.transform;
            chunkNWGO.transform.localPosition = new Vector3(0f, 0f, 0f);
            chunkNW.maxLOD = this.maxLOD;
            chunkNW.Initialize(gameObject, genome, childID, this.lod + 1, this.resolutionX, this.resolutionZ, arenaBounds, new Vector2(position.x - (scale.x * 0.5f), position.y + (scale.y * 0.5f)), scale * 0.5f, mat);
        }
    }