Ejemplo n.º 1
0
    IEnumerator AddTerrainCoroutine(int col, int row)
    {
        mTerrainNum++;

        GetTerrainName(col, row);

#if LOAD_ADDITIVE_ASYNC
        TerrainIndex tr = new TerrainIndex();
        tr.col = col;
        tr.row = row;
        sTerrainRecords.Add(tr);

        TerrainAttachment ta = new TerrainAttachment();
        ta.attachments = new List <GameObject>(4);
        mTerrainAttachments.Add(ta);

        lock ( sTerrainTasks )
        {
            TerrainTask tt = new TerrainTask();
            tt.col         = col;
            tt.row         = row;
            tt.terrainName = terrainName.ToString();
            sTerrainTasks.Add(tt);
        }
        AsyncOperation async = Application.LoadLevelAdditiveAsync(sceneName.ToString());
        yield return(async);
#else
        float z = (col > 0 ? col - 1 : col) * TILE_SIZE;
        float x = -(row < 0 ? row + 1 : row) * TILE_SIZE;

        // for memory limitation, don't cache anything.
        Object        t  = Resources.Load(terrainName.ToString());   // the prefab has been deleted from project by me, so it won't work here.
        TerrainRecord tr = new TerrainRecord();
        tr.col = col;
        tr.row = row;
        sTerrainRecords.Add(tr);
        TerrainAttachment ta = new TerrainAttachment();
        yield return(ta.terrain = Instantiate(t, new Vector3(x, 0, z), Quaternion.identity));

        ta.attachments = new List <GameObject>(4);

        t = null;
        mTerrainAttachments.Add(ta);
#endif
    }
Ejemplo n.º 2
0
    // Use this for initialization
    void Start()
    {
        Camera camera = Camera.main;

        mHalfFOV           = camera.fieldOfView * 0.5f; // half
        mHalfHorizontalFOV = mHalfFOV * camera.aspect;
        mHalfHorizontalFOV = Mathf.Deg2Rad * mHalfHorizontalFOV;
        mCameraMinForward  = Vector3.Angle(camera.transform.forward, -Vector3.up);
        mCameraMinForward -= mHalfFOV;
        mCameraMinForward  = Mathf.Deg2Rad * mCameraMinForward;
        mCameraMaxForward  = mCameraMinForward + Mathf.Deg2Rad * camera.fieldOfView;

        sTerrainRecords = ArrayList.Synchronized(new ArrayList());
        UpdateCameraTile();
        sTerrainRecords.Add(new TerrainIndex(mCameraCol, mCameraRow));
        mTerrainAttachments = new List <TerrainAttachment>();
        TerrainAttachment ta = new TerrainAttachment();

        ta.terrain     = UnityEngine.GameObject.Find("Terrain_original");
        ta.attachments = new List <UnityEngine.GameObject>();
        mTerrainAttachments.Add(ta);
    }
Ejemplo n.º 3
0
    // Update is called once per frame
    void Update()
    {
        UpdateCameraTile();

        Vector3 cameraPosition = GetComponent <Camera>().transform.position;
        float   viewHeight1    = cameraPosition.y * Mathf.Tan(mCameraMinForward);      // the top edge
        float   viewHeight2    = cameraPosition.y * Mathf.Tan(mCameraMaxForward);      // the bottom edge
        float   viewWidth      = cameraPosition.y / Mathf.Cos(mCameraMaxForward) * Mathf.Tan(mHalfHorizontalFOV);

        float viewHeightCompensator1 = 512.0f;       // the compensator for the top edge
        float viewHeightCompensator2 = 512.0f;       // the compensator for the bottom edge
        float viewWidthCompensator   = 512.0f;       // the compensator for width

        sTopSide    = cameraPosition.x - viewHeight2 - viewHeightCompensator2 - TILE_SIZE;
        sBottomSide = cameraPosition.x - viewHeight1 + viewHeightCompensator1 + TILE_SIZE;
        sLeftSide   = cameraPosition.z - viewWidth - viewWidthCompensator - TILE_SIZE;
        sRightSide  = cameraPosition.z + viewWidth + viewWidthCompensator + TILE_SIZE;

        /*//(column, row)//
         *
         * -1, 1	|  1, 1
         |
         | -----------------
         |
         | -1, -1	|  1, -1
         |
         | //////////////////*/

        TerrainIndex leftTopTile    = GetTileInfo(cameraPosition.x - viewHeight2 - viewHeightCompensator2, cameraPosition.z - viewWidth - viewWidthCompensator);
        TerrainIndex rightTopTile   = GetTileInfo(cameraPosition.x - viewHeight2 - viewHeightCompensator2, cameraPosition.z + viewWidth + viewWidthCompensator);
        TerrainIndex leftBottomTile = GetTileInfo(cameraPosition.x - viewHeight1 + viewHeightCompensator1, cameraPosition.z - viewWidth - viewWidthCompensator);

        for (int row = leftTopTile.row; row >= leftBottomTile.row; row--)
        {
            if (row == 0)
            {
                continue;
            }

            bool isBreak = false;
            for (int col = leftTopTile.col; col <= rightTopTile.col; col++)
            {
                if (col == 0)
                {
                    continue;
                }

                if (!IsExistInScene(col, row))
                {
                    mTerrainNum++;

                    GetTerrainName(col, row);

                    float z = (col > 0 ? col - 1 : col) * TILE_SIZE;
                    float x = -(row < 0 ? row + 1 : row) * TILE_SIZE;

                    // for memory limitation, don't cache anything.
                    UnityEngine.Object t  = Resources.Load("Prefabs/Maps/Simple/" + terrainName.ToString());
                    TerrainIndex       tr = new TerrainIndex();
                    tr.col = col;
                    tr.row = row;
                    sTerrainRecords.Add(tr);
                    TerrainAttachment ta = new TerrainAttachment();

                    ta.attachments = new List <UnityEngine.GameObject>(4);
                    ta.terrain     = t as UnityEngine.GameObject;
                    UnityEngine.GameObject s = (UnityEngine.GameObject)Instantiate(t, new Vector3(x, 0, z), Quaternion.identity);
                    t = null;

                    isBreak = true;
                }
            }

            if (isBreak)
            {
                // add more tiles next frame
                break;
            }
        }
    }