void BuildTexture()
    {
        int       texWidth  = size_x * tileResolution;
        int       texHeight = size_z * tileResolution;
        Texture2D texture   = new Texture2D(texWidth, texHeight);

        Color[][] tiles = ChopUpTiles();

        for (int y = 0; y < size_z; y++)
        {
            for (int x = 0; x < size_x; x++)
            {
                if ((y * x) % 1 == 0)
                {
                    Color[] p = tiles[map.GetTile(x, y).c];
                    texture.SetPixels(x * tileResolution, ((size_z - 1) * tileResolution - y * tileResolution), tileResolution, tileResolution, p);
                }
            }
        }


        //texture.filterMode = FilterMode.Point;
        //texture.wrapMode = TextureWrapMode.Clamp;
        texture.Apply();

        MeshRenderer mesh_renderer = GetComponent <MeshRenderer>();

        mesh_renderer.sharedMaterials[0].mainTexture = texture;
        Debug.Log("Done Texture!");
    }
예제 #2
0
    void CreatePathmap()
    {
        pathmap = new aStarNode[xSize, zSize];

        GameObject  tileMapGO = GameObject.FindGameObjectWithTag("TileMap");
        TileMapData map       = tileMapGO.GetComponent <TileMapGraphics>().map;

        for (int z = 0; z < zSize; z++)
        {
            for (int x = 0; x < xSize; x++)
            {
                pathmap[x, z] = new aStarNode(map.GetTile(x, z).walkable, map.GetTile(x, z).worldPos, x, z, map.GetTile(x, z).penalty);
            }
        }

        BlurPenaltyMap(0);
    }
예제 #3
0
    public Tile[] GetNeighbours(Tile target, bool diagOkay = true)
    {
        Tile[] ns;

        if (diagOkay == false)
        {
            ns = new Tile[4];   // Tile order: N E S W
        }
        else
        {
            ns = new Tile[8];   // Tile order : N E S W NE SE SW NW
        }

        Tile n;

        n     = map.GetTile(target.TileX, target.TileY + 1);
        ns[0] = n;  // Could be null, but that's okay.
        n     = map.GetTile(target.TileX + 1, target.TileY);
        ns[1] = n;  // Could be null, but that's okay.
        n     = map.GetTile(target.TileX, target.TileY - 1);
        ns[2] = n;  // Could be null, but that's okay.
        n     = map.GetTile(target.TileX - 1, target.TileY);
        ns[3] = n;  // Could be null, but that's okay.

        if (diagOkay == true)
        {
            n     = map.GetTile(target.TileX + 1, target.TileY + 1);
            ns[4] = n;  // Could be null, but that's okay.
            n     = map.GetTile(target.TileX + 1, target.TileY - 1);
            ns[5] = n;  // Could be null, but that's okay.
            n     = map.GetTile(target.TileX - 1, target.TileY - 1);
            ns[6] = n;  // Could be null, but that's okay.
            n     = map.GetTile(target.TileX - 1, target.TileY + 1);
            ns[7] = n;  // Could be null, but that's okay.
        }


        return(ns);
    }
예제 #4
0
    private void Update()
    {
        GameObject UnitSelectionGO = GameObject.FindGameObjectWithTag("UnitSelection");

        selectedObjects = UnitSelectionGO.GetComponent <UnitSelection>().GetSelectedObjects();
        if (selectedObjects != null && selectedObjects.Count != 0)
        {
            RaycastHit vHit = new RaycastHit();
            Ray        vRay = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(vRay, out vHit, 1000))
            {
                TileMapData map = tileMap.GetComponent <TileMapGraphics>().map;
                //int sizeX = tileMap.GetComponent<TileMapGraphics>().size_x;
                //int sizeZ = tileMap.GetComponent<TileMapGraphics>().size_z;
                Vector3 meshHitPos = vHit.point;
                var     vec        = meshHitPos;
                int     xVecHit    = Mathf.FloorToInt(vec.x);
                int     zVecHit    = Mathf.FloorToInt(vec.z);
                mousePos = map.GetTile(xVecHit, zVecHit).worldPos;
                if (GameObject.Find("SelectionGridPrefab(Clone)") == null)
                {
                    GameObject GridSelectionGO = (GameObject)Instantiate(GridSelection, mousePos, Quaternion.Euler(270, 0, 0));
                    GridSelectionGO.transform.parent = gameObject.transform;
                }
                else
                {
                    Destroy(GameObject.Find("SelectionGridPrefab(Clone)"));
                    GameObject GridSelectionGO = (GameObject)Instantiate(GridSelection, mousePos, Quaternion.Euler(270, 0, 0));
                    GridSelectionGO.transform.parent = gameObject.transform;
                }
            }
        }
        else
        {
            if (GameObject.Find("SelectionGridPrefab(Clone)") != null)
            {
                Destroy(GameObject.Find("SelectionGridPrefab(Clone)"));
            }
        }
    }
예제 #5
0
    public void BuildMapMesh(TileMapData mapData,
                             int colOffset, int rowOffset, int width, int height, float tileSize)
    {
        // purge existing
        ClearMeshes();

        // count number of actual tiles
        int numTiles = 0;

        for (int r = rowOffset; r < (rowOffset + height); r++)
        {
            for (int c = colOffset; c < (colOffset + width); c++)
            {
                if (mapData.GetTile(r, c) != null)
                {
                    numTiles++;
                }
            }
        }

        // sizing
        int   numVertices  = numTiles * 4;
        int   numTriangles = numTiles * 2 * 3;
        float halfSize     = tileSize / 2f;

        // warn if we hit the Unity vert limit
        if (numVertices > 65000)
        {
            throw new TileMapException("Number of required vertices (" + numVertices + ") exceeds Unity max of 65000!");
        }

        // vert offsets
        Vector3 v0Offset = new Vector3(-halfSize, halfSize, 0f);
        Vector3 v1Offset = new Vector3(halfSize, halfSize, 0f);
        Vector3 v2Offset = new Vector3(halfSize, -halfSize, 0f);
        Vector3 v3Offset = new Vector3(-halfSize, -halfSize, 0f);

        // create vertices and triangles for each tile
        Vector3[] vertices  = new Vector3[numVertices];
        Vector3[] normals   = new Vector3[numVertices];
        Vector2[] uvs       = new Vector2[numVertices];
        int[]     triangles = new int[numTriangles];

        /*  Tile mesh setup per tile:
         *
         *  v0             v1
         *    +-----------+
         *    | \         |
         *    |   \   T0  |
         *    |     X     |
         *    |  T1   \   |
         *    |         \ |
         *    +-----------+
         *  v3             v2
         *
         *  X = tile world position (centered)
         *  Triangle 0 = [v0, v1, v2]
         *  Triangle 1 = [v0, v2, v3]
         */

        int curVertIdx = 0;
        int curTriIdx  = 0;

        // start at 0,0 building up rows and columns of tiles from our current
        // transform origin
        for (int r = 0; r < height; r++)
        {
            for (int c = 0; c < width; c++)
            {
                // get tile index
                int tileIdx = mapData.TileIndex(r + rowOffset, c + colOffset);
                // get tile
                Tile t = mapData.GetTile(tileIdx);

                // only render mesh and texture here if there's actually a tile
                if (t != null)
                {
                    // calc tile world position
                    Vector3 tilePos = new Vector3(c * tileSize, r * tileSize, 0f);

                    // set tile vertices
                    vertices[curVertIdx]     = tilePos + v0Offset;
                    vertices[curVertIdx + 1] = tilePos + v1Offset;
                    vertices[curVertIdx + 2] = tilePos + v2Offset;
                    vertices[curVertIdx + 3] = tilePos + v3Offset;

                    // set tile uvs
                    uvs[curVertIdx]     = t.UV(0);
                    uvs[curVertIdx + 1] = t.UV(1);
                    uvs[curVertIdx + 2] = t.UV(2);
                    uvs[curVertIdx + 3] = t.UV(3);

                    // set tile triangles
                    // t0
                    triangles[curTriIdx]     = (curVertIdx);
                    triangles[curTriIdx + 1] = (curVertIdx + 1);
                    triangles[curTriIdx + 2] = (curVertIdx + 2);
                    // t1
                    triangles[curTriIdx + 3] = (curVertIdx);
                    triangles[curTriIdx + 4] = (curVertIdx + 2);
                    triangles[curTriIdx + 5] = (curVertIdx + 3);

                    curVertIdx += 4;
                    curTriIdx  += 6;
                }
            }
        }

        // all normals are the same
        for (int i = 0; i < numVertices; i++)
        {
            normals[i] = Vector3.back;
        }

        // create the Mesh
        Mesh tileMesh = new Mesh();

        tileMesh.vertices  = vertices;
        tileMesh.triangles = triangles;
        tileMesh.uv        = uvs;
        tileMesh.normals   = normals;

        // assign the mesh
        meshFilter.mesh = tileMesh;
        UpdateCollider();
    }
    public void BuildMesh()
    {
        map = new TileMapData(size_x, size_z);

        int numTiles = size_x * size_z;
        int numTris  = numTiles * 2;

        int vsize_x  = size_x + 1;
        int vsize_z  = size_z + 1;
        int numVerts = vsize_x * vsize_z;

        // Generate the mesh data
        Vector3[] vertices = new Vector3[numVerts];
        Vector3[] normals  = new Vector3[numVerts];
        Vector2[] uv       = new Vector2[numVerts];

        int[] triangles = new int[numTris * 3];

        int x, z;

        for (z = 0; z < vsize_z; z++)
        {
            for (x = 0; x < vsize_x; x++)
            {
                vertices[z * vsize_x + x] = new Vector3(x * tileSize, 0, z * tileSize);

                if (z < size_z && x < size_x)
                {
                    map.GetTile(x, z).SetWorldPos(vertices[z * vsize_x + x] + new Vector3(0.5f, -0.5f, 0.5f));
                }

                normals[z * vsize_x + x] = Vector3.up;
                uv[z * vsize_x + x]      = new Vector2((float)x / size_x, 1f - (float)z / size_z);
            }
        }
        //Debug.Log("Done Verts!");

        for (z = 0; z < size_z; z++)
        {
            for (x = 0; x < size_x; x++)
            {
                int squareIndex = z * size_x + x;
                int triOffset   = squareIndex * 6;
                //triangles[triOffset + 0] = z * vsize_x + x + 0;
                //triangles[triOffset + 2] = z * vsize_x + x + vsize_x + 0;
                //triangles[triOffset + 1] = z * vsize_x + x + vsize_x + 1;

                //triangles[triOffset + 3] = z * vsize_x + x + 0;
                //triangles[triOffset + 5] = z * vsize_x + x + vsize_x + 1;
                //triangles[triOffset + 4] = z * vsize_x + x + 1;

                triangles[triOffset + 0] = z * vsize_x + x + 0;
                triangles[triOffset + 1] = z * vsize_x + x + vsize_x + 0;
                triangles[triOffset + 2] = z * vsize_x + x + vsize_x + 1;

                triangles[triOffset + 3] = z * vsize_x + x + 0;
                triangles[triOffset + 4] = z * vsize_x + x + vsize_x + 1;
                triangles[triOffset + 5] = z * vsize_x + x + 1;
            }
        }

        //Debug.Log("Done Triangles!");

        // Create a new Mesh and populate with the data
        Mesh mesh = new Mesh();

        mesh.vertices  = vertices;
        mesh.triangles = triangles;
        mesh.normals   = normals;
        mesh.uv        = uv;

        // Assign our mesh to our filter/renderer/collider
        MeshFilter   mesh_filter   = GetComponent <MeshFilter>();
        MeshCollider mesh_collider = GetComponent <MeshCollider>();

        mesh_filter.mesh         = mesh;
        mesh_collider.sharedMesh = mesh;
        //Debug.Log("Done Mesh!");

        BuildTexture();
    }