Example #1
0
    void GeneratePlane(Vector2Int localPos, Vector3Int globalPos, int level = 0, PlacedMapEntity basePME = null)
    {
        int LODScale = (int)(horizontalSize / Mathf.Pow(2, level));   //32/1=32, 32/2=16, 32/4=8, 32/8=4, 32/16=2

        PlacedMapEntity pme = MapEntityFactory.shared.Generate(MapEntityType.ground, new Vector3(localPos.x, 0, localPos.y), LODScale);

        pme.Construct(globalPos, "");
        if (basePME == null)
        {
            data.Add(pme);
        }
        else
        {
            basePME.children[localPos.y * 2 + localPos.x] = pme;
        }

        if (level < 5)
        {
            for (int z = 0; z < 2; ++z)
            {
                for (int x = 0; x < 2; ++x)
                {
                    int        childLODScale  = (int)LODScale / 2;
                    Vector3Int childGlobalPos = globalPos + new Vector3Int(x * childLODScale, 0, z * childLODScale);
                    GeneratePlane(new Vector2Int(x, z), childGlobalPos, level + 1, pme);  //  recursive
                }
            }
        }
    }
Example #2
0
    private IEnumerator RenderCR(RenderInfo info)
    {
        Vector3    pos             = info.globalPosition;
        Vector3Int center          = info.playerPosition;
        float      sqrDist         = Mathf.Pow(pos.x - center.x, 2f) + Mathf.Pow(pos.z - center.z, 2f);
        int        desiredLODScale = (int)Mathf.Pow(2f, lodDistance.Index(sqrDist));

        //        Render(offset, desiredLODScale);
        if (LODScale <= desiredLODScale)
        {
            if (groundObject == null)
            {
                groundObject = MapEntityFactory.Instantiate(MapEntityFactory.shared.groundPrefab);
            }
            float LODOffset = LODScale * 0.5f;
            groundObject.transform.position   = new Vector3(pos.x + LODOffset, size.y * 0.5f, pos.z + LODOffset) * MapRenderer.scale;
            groundObject.transform.localScale = new Vector3(size.x * LODScale, size.y, size.z * LODScale);

            for (int i = 0; i < 4; ++i)
            {
                PlacedMapEntity child = children[i];
                if (child != null)
                {
                    child.ClearGameObject();
                }
            }
        }
        else
        {
            if (groundObject)
            {
                ClearGameObject();
            }
            for (int z = 0; z < 2; ++z)
            {
                for (int x = 0; x < 2; ++x)
                {
                    PlacedMapEntity child = children[z * 2 + x];
                    if (child != null)
                    {
                        int childLODScale = (int)LODScale / 2;
                        child.Render(info.globalPosition + new Vector3Int(x * childLODScale, 0, z * childLODScale), center);
                    }
                }
                yield return(null);
            }
        }
    }