Ejemplo n.º 1
0
    Vector3 RandomPosition()
    {
        Vector3 pos = Vector3.zero;

        pos.x = Random.Range(10, ChunkLOD.GLOBAL_WIDTH * ChunkLOD.SIZE_W - 10);
        pos.z = Random.Range(10, ChunkLOD.GLOBAL_HEIGHT * ChunkLOD.SIZE_H - 10);
        pos.y = ChunkLOD.GetHeight(pos.x, pos.z) + 2;

        return(pos);
    }
Ejemplo n.º 2
0
    void GenerateLand(Mesh mesh, float[,] map)
    {
        Vector3[] vertices     = new Vector3[WIDTH * HEIGHT];
        int       triangleSize = 6 * (WIDTH - 1) * (HEIGHT - 1);

        int[] triangles = new int[triangleSize];

        Vector2[] uv = new Vector2[WIDTH * HEIGHT];

        int inTriang = 0;

        for (int j = 0; j < HEIGHT; j++)
        {
            for (int i = 0; i < WIDTH; i++)
            {
                int iPos  = i * ChunkLOD.SIZE_W / (WIDTH - 1);
                int jPos  = j * ChunkLOD.SIZE_H / (HEIGHT - 1);
                int index = j * WIDTH + i;

                map [i, j] = ChunkLOD.GetHeight(iPos + transform.position.x, jPos + transform.position.z);

                vertices [index] = new Vector3(iPos, map [i, j], jPos);

                float uv_i = ((float)i / (WIDTH - 1) + transform.position.x / ChunkLOD.SIZE_W) / ChunkLOD.GLOBAL_WIDTH;
                float uv_j = ((float)j / (HEIGHT - 1) + transform.position.z / ChunkLOD.SIZE_H) / ChunkLOD.GLOBAL_HEIGHT;
                uv [index] = new Vector2(uv_i, uv_j);

                if (i < WIDTH - 1 && j < HEIGHT - 1)
                {
                    triangles [inTriang]     = index;
                    triangles [inTriang + 1] = index + WIDTH;
                    triangles [inTriang + 2] = index + WIDTH + 1;
                    triangles [inTriang + 3] = index;
                    triangles [inTriang + 4] = index + WIDTH + 1;
                    triangles [inTriang + 5] = index + 1;
                    inTriang += 6;
                }
            }
        }
        mesh.vertices  = vertices;
        mesh.triangles = triangles;
        mesh.uv        = uv;

        mesh.RecalculateNormals();
    }
Ejemplo n.º 3
0
    void Start()
    {
        ChunkLOD.GLOBAL_HEIGHT = HEIGHT;
        ChunkLOD.GLOBAL_WIDTH  = WIDTH;
        for (int i = 0; i < WIDTH; i++)
        {
            for (int j = 0; j < HEIGHT; j++)
            {
                int x = i * (ChunkLOD.SIZE_W);
                int z = j * (ChunkLOD.SIZE_H);
                Instantiate(terrainPrefab, transform.position + new Vector3(x, 0, z), Quaternion.identity);
            }
        }
        Vector3 center = ChunkLOD.GetCenter();

        center.y += enseirbPrefab.transform.localScale.y / 2;
        Instantiate(enseirbPrefab, center, Quaternion.identity);
    }
Ejemplo n.º 4
0
    public Mesh SetMesh(Vector2 u)
    {
        int   i, j;
        Mesh  m   = new Mesh();
        float len = u.magnitude;
        int   L   = Mathf.FloorToInt(len);

        Vector3[] vertices  = new Vector3[2 * L + 4];
        int[]     triangles = new int[3 * (vertices.Length - 2)];
        Vector2[] uv        = new Vector2[vertices.Length];
        Vector2   v         = u.normalized;
        Vector2   orth      = 4 * new Vector2(-v.y, v.x); // orthogonal vector, for road width
        float     x         = this.transform.position.x;
        float     y         = this.transform.position.z;

        for (i = 0; i < len; i++)
        {
            vertices[2 * i]     = new Vector3(v.x * i - orth.x, ChunkLOD.GetHeight(x + v.x * i - orth.x, y + v.y * i - orth.y), v.y * i - orth.y);
            vertices[2 * i + 1] = new Vector3(v.x * i + orth.x, ChunkLOD.GetHeight(x + v.x * i + orth.x, y + v.y * i + orth.y), v.y * i + orth.y);
            uv[2 * i]           = new Vector2(0, i * DILATE);
            uv[2 * i + 1]       = new Vector2(1, i * DILATE);
        }
        vertices[2 * i]     = new Vector3(u.x - orth.x, ChunkLOD.GetHeight(x + u.x - orth.x, y + u.y - orth.y), u.y - orth.y);
        vertices[2 * i + 1] = new Vector3(u.x + orth.x, ChunkLOD.GetHeight(x + u.x + orth.x, y + u.y + orth.y), u.y + orth.y);
        uv[2 * i]           = new Vector2(0, i * DILATE);
        uv[2 * i + 1]       = new Vector2(1, i * DILATE);
        for (i = 0, j = 2; i <= L; i++, j += 2)
        {
            triangles[6 * i]     = j - 1;
            triangles[6 * i + 1] = j + 1;
            triangles[6 * i + 2] = j;
            triangles[6 * i + 3] = j - 2;
            triangles[6 * i + 4] = j - 1;
            triangles[6 * i + 5] = j;
        }

        m.vertices  = vertices;
        m.triangles = triangles;
        m.uv        = uv;
        m.RecalculateNormals();
        GetComponent <MeshFilter>().mesh = m;
        return(m);
    }
Ejemplo n.º 5
0
    void CreatePlayer(Vector3 pos, bool attachCamera, ReplayState state)
    {
        Vector3    center   = ChunkLOD.GetCenter();
        Quaternion rotation = Quaternion.LookRotation(center - pos, Vector3.up);
        GameObject player   = Instantiate(playerPrefab, pos, rotation);

        Destroy(player.GetComponent <ReplayContainer> ());
        if (attachCamera)
        {
            player.AddComponent <ReplayInput> ();
            player.GetComponent <ReplayInput> ().state = state;
            player.GetComponentInChildren <Camera> ().gameObject.SetActive(true);
            player.tag = "Player";
        }
        else
        {
            player.AddComponent <ReplayRead> ();
            player.GetComponent <ReplayRead> ().state = state;
            player.GetComponentInChildren <Camera> ().gameObject.SetActive(false);
        }
        players.Add(player);
    }
Ejemplo n.º 6
0
    void Start()
    {
        map    = createMap();
        pixels = createPixelMap(map);

        /* Create random points points */
        //m_points = new List<Vector2> ();
        //List<uint> colors = new List<uint> ();
        /* Randomly pick vertices */
        //GenerateRandomPoints (map, m_points, NPOINTS);
        //GenerateGridPoints (m_points, N, M);
        //GenerateCirclePoints(m_points,N,M,R);
        /* Generate Graphs */


        //Color color = Color.blue;
        /* Shows Voronoi diagram */

        /*for (int i = 0; i < m_edges.Count; i++) {
         *      LineSegment seg = m_edges [i];
         *      Vector2 left = (Vector2)seg.p0;
         *      Vector2 right = (Vector2)seg.p1;
         *      DrawLine (pixels,left, right,color);
         * }*/
        Edges(map, pixels);
        for (int i = 0; i < m_points.Count; i++)
        {
            float   scaleFactor = ChunkLOD.GLOBAL_WIDTH * ChunkLOD.SIZE_W / WIDTH;
            float   maxX        = ChunkLOD.GLOBAL_WIDTH * ChunkLOD.SIZE_W;
            float   maxY        = ChunkLOD.GLOBAL_HEIGHT * ChunkLOD.SIZE_H;
            Vector2 point       = scaleFactor * m_points [i];
            if (point.x <= maxX && point.x >= 0 && point.y <= maxY && point.y >= 0)
            {
                float value = map [(int)m_points [i].x - 1, (int)m_points [i].y - 1];
                if (value >= probA)
                {
                    GameObject building = Instantiate(buildingPrefabs [0], new Vector3(point.x, ChunkLOD.GetHeight(point.x, point.y), point.y), Quaternion.identity);
                    building.transform.localScale *= 600;
                }
                if (value < probA && value >= probB)
                {
                    GameObject building = Instantiate(buildingPrefabs [1], new Vector3(point.x, ChunkLOD.GetHeight(point.x, point.y), point.y), Quaternion.identity);
                    building.transform.localScale *= 600;
                }
                if (value < probB)
                {
                    GameObject building = Instantiate(buildingPrefabs [2], new Vector3(point.x, ChunkLOD.GetHeight(point.x, point.y), point.y), Quaternion.identity);
                    building.transform.localScale *= 400;
                }
            }
        }

        Color color = Color.red;

        /* Shows spanning tree */

        color = Color.black;

        /*if (m_spanningTree != null) {
         *      for (int i = 0; i< m_spanningTree.Count; i++) {
         *              LineSegment seg = m_spanningTree [i];
         *              Vector2 left = (Vector2)seg.p0;
         *              Vector2 right = (Vector2)seg.p1;
         *              DrawLine (pixels,left, right,color);
         *      }
         * }*/
        /* Apply pixels to texture */
        tx = new Texture2D(WIDTH, HEIGHT);
        land.SetTexture("_SplatTex", tx);
        tx.SetPixels(pixels);
        tx.Apply();
    }