Example #1
0
    // Use this for initialization
    void Start()
    {
#if !UNITY_EDITOR
        Screen.showCursor = false;
#endif
        map = GameObject.Find("Map").GetComponent <TerrainManager>().map;
    }
Example #2
0
    public void CreateTerrain(ja2.Map Map_, ja2.TerrainMaterialManager MatManager)
    {
        if (Map_.width % ja2.TerrainPartition.PARTITION_WIDTH != 0 || Map_.width % ja2.TerrainPartition.PARTITION_WIDTH != 0)
        {
            throw new System.ArgumentException("Map width/height must be normalized to terrain partition width/height.");
        }
        // Create component
        mapInstance     = ScriptableObject.CreateInstance <MapInstance>();
        mapInstance.map = Map_;

        // Need to create terrain partitions
        int partition_width  = Map_.width / ja2.TerrainPartition.PARTITION_WIDTH;
        int partition_height = Map_.height / ja2.TerrainPartition.PARTITION_HEIGHT;

        for (int i = 0; i < partition_height; ++i)
        {
            for (int j = 0; j < partition_width; ++j)
            {
                // Create terrain GO
                GameObject terrain_go = new GameObject(PARTITION_NAME + j + "_" + i);
                terrain_go.isStatic = true;
                // Set parent
                terrain_go.transform.parent = transform;
                // Update position
                Vector3 tile_vertex_0 = ja2.TerrainPartition.TileVertex(j * ja2.TerrainPartition.PARTITION_WIDTH, i * ja2.TerrainPartition.PARTITION_HEIGHT, 0);
                Vector3 tile_vertex_1 = ja2.TerrainPartition.TileVertex(j * ja2.TerrainPartition.PARTITION_WIDTH, i * ja2.TerrainPartition.PARTITION_HEIGHT, 1);
                terrain_go.transform.position = new Vector3(tile_vertex_0.x, 0, tile_vertex_1.z);
                // Set layer
                terrain_go.layer = Terrain.LAYER;
                // Create component
                var terrain = terrain_go.AddComponent <Terrain>();
                terrain.CreatePartition(j, i, mapInstance, MatManager);
            }
        }
    }
Example #3
0
        //! Convert Map.Direction to LookDirection.
        public static LookDirection Convert(Map.Direction Dir)
        {
            LookDirection direction = LookDirection.EAST;

            switch(Dir)
            {
                case ja2.Map.Direction.EAST:
                    direction = LookDirection.EAST;
                    break;
                case ja2.Map.Direction.NORTH:
                    direction = LookDirection.NORTH;
                    break;
                case ja2.Map.Direction.NORTH_EAST:
                    direction = LookDirection.NORTHEAST;
                    break;
                case ja2.Map.Direction.NORTH_WEST:
                    direction = LookDirection.NORTHWEST;
                    break;
                case ja2.Map.Direction.SOUTH:
                    direction = LookDirection.SOUTH;
                    break;
                case ja2.Map.Direction.SOUTH_EAST:
                    direction = LookDirection.SOUTHEAST;
                    break;
                case ja2.Map.Direction.SOUTH_WEST:
                    direction = LookDirection.SOUTHWEST;
                    break;
                case ja2.Map.Direction.WEST:
                    direction = LookDirection.WEST;
                    break;
            }

            return direction;
        }
Example #4
0
    public bool CheckDirection(Direction Dir, float Ratio)
    {
        bool    ret = false;
        Vector3 point_to_check;

        if (Dir == Direction.LEFT || Dir == Direction.BOTTOM)
        {
            point_to_check = new Vector3(0, 0, 0);
        }
        else
        {
            point_to_check = new Vector3(1, 1, 0);
        }
        // Find if we are beyond the terrain
        Ray   ray = Camera.main.ViewportPointToRay(point_to_check);
        float point;

        new Plane(Vector3.up, 0).Raycast(ray, out point);
        Vector3 point_on_plane  = ray.GetPoint(point);
        var     terrain_manager = GameObject.Find("Map").GetComponent <TerrainManager>();

        ja2.Map         map             = terrain_manager.map;
        ja2.TerrainTile last_tile       = map.GetTile(map.width - 1, map.height - 1);
        Vector3         last_tile_pos_1 = terrain_manager.GetPosition(last_tile, 1);
        Vector3         last_tile_pos_2 = terrain_manager.GetPosition(last_tile, 2);

        float amount_ratio = amount * Ratio;

        // Check position
        switch (Dir)
        {
        case Direction.LEFT:
            ret = point_on_plane.z - amount_ratio > ja2.TerrainPartition.TILE_WIDTH;
            break;

        case Direction.TOP:
            ret = point_on_plane.x - amount_ratio > ja2.TerrainPartition.TILE_HEIGHT;
            break;

        case Direction.BOTTOM:
            ret = point_on_plane.x + amount_ratio < last_tile_pos_1.x;
            break;

        case Direction.RIGHT:
            ret = point_on_plane.z + amount_ratio < last_tile_pos_2.z;
            break;
        }

        return(ret);
    }
Example #5
0
        public Mesh Create(int X, int Y, Map Map_, TerrainTileSet TileSet)
        {
            Map map = Map_;
            // Create vertex array
            Vector3[] array_vec = new Vector3[PARTITION_WIDTH * PARTITION_HEIGHT * 4];
            // Create triangles array
            int[] array_tri = new int[PARTITION_WIDTH * PARTITION_HEIGHT * 6];
            // Create UV arrays
            Vector2[] uv1 = new Vector2[array_vec.Length];
            Vector2[] uv2 = new Vector2[array_vec.Length];
            Vector4[] uv3 = new Vector4[array_vec.Length];
            // Triangle map
            m_TerrainMap = new TriangleMap[PARTITION_WIDTH * PARTITION_HEIGHT * 2];
            // Go through all tiles
            int partition_begin_x = 0;
            int partition_begin_y = 0;
            int partition_height = PARTITION_HEIGHT;
            int partition_width = PARTITION_WIDTH;
            for (int i = partition_begin_y, last_vertex = 0; i < partition_height; ++i)
            {
                for (int j = partition_begin_x; j < partition_width; ++j)
                {
                    // Get Tile
                    ja2.TerrainTile tile = map.GetTile(j + X * PARTITION_WIDTH, i + Y * PARTITION_HEIGHT);
                    // Create vertices
                    array_vec[GetVertexIndex(j, i)] = TileVertex(j, i, 0);
                    array_vec[GetVertexIndex(j, i) + 1] = TileVertex(j, i, 1);
                    array_vec[GetVertexIndex(j, i) + 2] = TileVertex(j, i, 2);
                    array_vec[GetVertexIndex(j, i) + 3] = TileVertex(j, i, 3);

                    last_vertex += 4;
                    // Get the tile material types
                    byte mat_v0 = tile.GetTerrainType(ja2.TerrainTile.Vertex.NORTH),
                        mat_v1 = tile.GetTerrainType(ja2.TerrainTile.Vertex.WEST),
                        mat_v2 = tile.GetTerrainType(ja2.TerrainTile.Vertex.SOUTH),
                        mat_v3 = tile.GetTerrainType(ja2.TerrainTile.Vertex.EAST);
                    // Get 1. and 2. material
                    byte mat_1 = mat_v0;
                    byte mat_2 = mat_1;
                    if (mat_v0 != mat_v1)
                        mat_2 = mat_v1;
                    else if (mat_v0 != mat_v2)
                        mat_2 = mat_v2;
                    else if (mat_v0 != mat_v3)
                        mat_2 = mat_v3;
                    // Get alpha splat index
                    byte alpha_index = 1;
                    alpha_index |= (byte)((mat_v1 == mat_1) ? 2 : 0);
                    alpha_index |= (byte)((mat_v2 == mat_1) ? 4 : 0);
                    alpha_index |= (byte)((mat_v3 == mat_1) ? 8 : 0);
                    // If materials need to be inverted
                    if (alpha_index > 7)
                    {
                        byte mat_helper = mat_1;
                        mat_1 = mat_2;
                        mat_2 = mat_helper;
                        alpha_index = (byte)(~alpha_index & 15);
                    }
                    // Get the primary tile type information
                    TextureAtlasInfo primary_mat_info = TileSet.GetTileType(mat_1, tile.variant);
                    // Get secondary tile type information
                    TextureAtlasInfo secondary_mat_info = TileSet.GetTileType(mat_2, tile.variant);
                    // Get the alpha splat info for tile
                    TextureAtlasInfo alpha_splat_mat_info = TileSet.splatUsed.GetSplat(alpha_index);

                    // Texture coordinates
                    uv1[last_vertex - 4] = new Vector2(primary_mat_info.uvOffsetW + primary_mat_info.uvWidth / 2, primary_mat_info.uvOffsetH);
                    uv1[last_vertex - 3] = new Vector2(primary_mat_info.uvOffsetW, primary_mat_info.uvOffsetH - primary_mat_info.uvHeight / 2);
                    uv1[last_vertex - 2] = new Vector2(primary_mat_info.uvOffsetW + primary_mat_info.uvWidth / 2, primary_mat_info.uvOffsetH - primary_mat_info.uvHeight);
                    uv1[last_vertex - 1] = new Vector2(primary_mat_info.uvOffsetW + primary_mat_info.uvWidth, primary_mat_info.uvOffsetH - primary_mat_info.uvHeight / 2);

                    uv2[last_vertex - 4] = new Vector2(secondary_mat_info.uvOffsetW + secondary_mat_info.uvWidth / 2, secondary_mat_info.uvOffsetH);
                    uv2[last_vertex - 3] = new Vector2(secondary_mat_info.uvOffsetW, secondary_mat_info.uvOffsetH - secondary_mat_info.uvHeight / 2);
                    uv2[last_vertex - 2] = new Vector2(secondary_mat_info.uvOffsetW + secondary_mat_info.uvWidth / 2, secondary_mat_info.uvOffsetH - secondary_mat_info.uvHeight);
                    uv2[last_vertex - 1] = new Vector2(secondary_mat_info.uvOffsetW + secondary_mat_info.uvWidth, secondary_mat_info.uvOffsetH - secondary_mat_info.uvHeight / 2);

                    uv3[last_vertex - 4] = new Vector4(alpha_splat_mat_info.uvOffsetW + alpha_splat_mat_info.uvWidth / 2, alpha_splat_mat_info.uvOffsetH, 0);
                    uv3[last_vertex - 3] = new Vector4(alpha_splat_mat_info.uvOffsetW, alpha_splat_mat_info.uvOffsetH - alpha_splat_mat_info.uvHeight / 2, 0, 0);
                    uv3[last_vertex - 2] = new Vector4(alpha_splat_mat_info.uvOffsetW + alpha_splat_mat_info.uvWidth / 2, alpha_splat_mat_info.uvOffsetH - alpha_splat_mat_info.uvHeight, 0, 0);
                    uv3[last_vertex - 1] = new Vector4(alpha_splat_mat_info.uvOffsetW + alpha_splat_mat_info.uvWidth, alpha_splat_mat_info.uvOffsetH - alpha_splat_mat_info.uvHeight / 2, 0, 0);
                    // Create triangles
                    int triangle_index = GetTriIndex(j, i);
                    array_tri[triangle_index] = last_vertex - 4;
                    array_tri[triangle_index + 1] = last_vertex - 1;
                    array_tri[triangle_index + 2] = last_vertex - 3;

                    array_tri[triangle_index + 3] = last_vertex - 3;
                    array_tri[triangle_index + 4] = last_vertex - 1;
                    array_tri[triangle_index + 5] = last_vertex - 2;
                    // Save triangles
                    int triangle_index_raw = j * 2 + i * PARTITION_WIDTH * 2;
                    m_TerrainMap[triangle_index_raw] = new TriangleMap(tile.x, tile.y);
                    m_TerrainMap[triangle_index_raw + 1] = new TriangleMap(tile.x, tile.y);
                }
            }

            Mesh mesh = new Mesh();
            mesh.vertices = array_vec;
            mesh.triangles = array_tri;
            mesh.uv = uv1;
            mesh.uv2 = uv2;
            mesh.tangents = uv3;
            mesh.RecalculateNormals();

            return mesh;
        }
Example #6
0
 // Use this for initialization
 void Start()
 {
     #if !UNITY_EDITOR
     Screen.showCursor = false;
     #endif
     map = GameObject.Find("Map").GetComponent<TerrainManager>().map;
 }