public TerrainChunk(Vector2 coord, int size, lodInfo[] detailLevels, Transform parent, Material material)
        {
            this.detailLevels = detailLevels;

            position = coord * size;
            bounds   = new Bounds(position, Vector2.one * size);
            Vector3 positionV3 = new Vector3(position.x, 0, position.y);

            meshObject            = new GameObject("Terrain Chunk");
            meshRenderer          = meshObject.AddComponent <MeshRenderer>();
            meshFilter            = meshObject.AddComponent <MeshFilter>();
            meshRenderer.material = material;

            meshObject.transform.position = positionV3;
            meshObject.transform.parent   = parent;
            SetVisible(false);

            meshLods = new LodMesh[detailLevels.Length];
            for (int i = 0; i < detailLevels.Length; i++)
            {
                meshLods[i] = new LodMesh(detailLevels[i].lod, UpdateTerrainChunk);
            }

            islandGen.RequestMapData(position, OnMapDataRecieved);
        }
Beispiel #2
0
        public TerrainChunk(Vector2 coord, int size, LodInfo[] detailLevels, Transform parent, Material material)
        {
            this.detailLevels = detailLevels;
            position          = coord * size;
            bounds            = new Bounds(position, Vector2.one * size);
            var positionV3 = new Vector3(position.x, 0, position.y);

            meshObject                      = new GameObject("Terrain Chunk");
            meshRenderer                    = meshObject.AddComponent <MeshRenderer>();
            meshFilter                      = meshObject.AddComponent <MeshFilter>();
            meshCollider                    = meshObject.AddComponent <MeshCollider>();
            meshRenderer.material           = material;
            meshObject.transform.position   = positionV3 * _mapGenerator.terrainData.uniformScale;
            meshObject.transform.parent     = parent;
            meshObject.transform.localScale = Vector3.one * _mapGenerator.terrainData.uniformScale;
            SetVisible(false);
            lodMeshes = new LodMesh[detailLevels.Length];
            for (var i = 0; i < detailLevels.Length; i++)
            {
                lodMeshes[i] = new LodMesh(detailLevels[i].lod, UpdateTerrainChunk);
                if (detailLevels[i].useForCollider)
                {
                    collisionLODMesh = lodMeshes[i];
                }
            }

            _mapGenerator.RequestMapData(position, OnMapDataReceived);
        }
        public void UpdateTerrainChunk()
        {
            if (mapDataRecieved)
            {
                float viewerDistanceFromEdge = Mathf.Sqrt(bounds.SqrDistance(viewerPosition));
                bool  visible = viewerDistanceFromEdge <= maxViewDist;

                if (visible)
                {
                    int index = 0;
                    for (int i = 0; i < detailLevels.Length - 1; i++)
                    {
                        if (viewerDistanceFromEdge > detailLevels[i].visibleDistanceThreshhold)
                        {
                            index = i + 1;
                        }
                        else
                        {
                            break;
                        }
                    }

                    if (index != prevLODIndex)
                    {
                        LodMesh lodMesh = meshLods[index];
                        if (lodMesh.hasMesh)
                        {
                            prevLODIndex    = index;
                            meshFilter.mesh = lodMesh.mesh;
                        }
                        else if (!lodMesh.hasRequestMesh)
                        {
                            lodMesh.RequestMesh(mapData);
                        }
                    }
                    visibleChunks.Add(this);
                }
                SetVisible(visible);
            }
        }
Beispiel #4
0
        public TerrainChunk(ChunkIdx idx, float chunkSize, Material material)
        {
            chunkIdx = idx;

            center = new Vector2(chunkIdx.X * chunkSize, chunkIdx.Z * chunkSize);

            obj          = new GameObject(chunkIdx.ToString());
            meshFilter   = obj.AddComponent <MeshFilter>();
            meshRenderer = obj.AddComponent <MeshRenderer>();
            meshCollider = obj.AddComponent <MeshCollider>();

            previousLod           = -1;
            meshRenderer.material = material;

            obj.transform.position = new Vector3(center.x, 0, center.y);

            meshData = new LodMesh[MAX_LOD + 1];
            for (int i = 0; i < MAX_LOD + 1; ++i)
            {
                meshData[i] = new LodMesh(i, UpdateChunk);
            }

            generator.RequestMapData(center, OnReceivedMapData);
        }
Beispiel #5
0
        public void UpdateMeshes(LodInfo[] detailLevels)
        {
            m_meshesUpdated = false;

            m_detailLevels = detailLevels;

            m_lodMeshes = m_lodMeshes ?? new LodMesh[m_detailLevels.Length];

            for (int i = 0; i < detailLevels.Length; i++)
            {
                var lodMesh = m_lodMeshes[i];

                if (lodMesh == null)
                {
                    lodMesh = new LodMesh(detailLevels[i].m_lod, UpdateTerrainChunk, m_addCollider);
                }
                else
                {
                    lodMesh.ResetLodMesh(detailLevels[i].m_lod, UpdateTerrainChunk);
                }

                m_lodMeshes[i] = lodMesh;
            }
        }