public void DrawMapInEditor()
    {
        MapData mapData = GenerateMapData(Vector2.zero);

        MapDisplay display = FindObjectOfType <MapDisplay>();

        if (drawMode == DrawMode.NoiseMap)
        {
            display.DrawTexture(TextureGenerator.TextureFromHeightMap(mapData.heightMap));
        }
        else if (drawMode == DrawMode.ColourMap)
        {
            display.DrawTexture(TextureGenerator.TextureFromColourMap(mapData.colourMap, mapChunkSize, mapChunkSize));
        }
        else if (drawMode == DrawMode.Mesh)
        {
            display.DrawMesh(MeshGenerator.GenerateTerrainMesh(mapData.heightMap, meshHeightMultiplier, meshHeightCurve, editorPreviewLOD), TextureGenerator.TextureFromColourMap(mapData.colourMap, mapChunkSize, mapChunkSize));
            if (generateGameObjects)
            {
                GenerateGameObjects genGOScript = display.meshRenderer.transform.gameObject.GetComponent <GenerateGameObjects>();
                if (genGOScript == null)
                {
                    genGOScript = display.meshRenderer.transform.gameObject.AddComponent <GenerateGameObjects>();
                }
                genGOScript.seed = seed;
                genGOScript.StartGenerateObjects();
                genGOScript.offset        = offset;
                genGOScript.levelOfDetail = editorPreviewLOD;
                genGOScript.mapData       = mapData;
                genGOScript.genClouds     = generateClouds;
            }
        }
        else if (drawMode == DrawMode.Falloff)
        {
            display.DrawTexture(TextureGenerator.TextureFromHeightMap(FalloffGenerator.GenerateFalloffMap(mapChunkSize)));
        }

        if (generateWater)
        {
            bool genNewWater = true;
            foreach (Transform child in gameObject.transform)
            {
                if (child.tag == "Water")
                {
                    genNewWater = false;
                }
            }
            if (genNewWater)
            {
                EndlessTerrain endTer   = GetComponent <EndlessTerrain>();
                GameObject     waterIns = GameObject.Instantiate(endTer.waterPlane, new Vector3(0, -0.22f * EndlessTerrain.scale, 0), Quaternion.identity);
                waterIns.transform.parent = gameObject.transform;
                waterIns.AddComponent <DisableOnStartup>();
            }
        }
    }
Exemple #2
0
        public void UpdateTerrainChunk()
        {
            float viewerDstFromNearestEdge = Mathf.Sqrt(bounds.SqrDistance(viewerPosition));
            bool  visible = viewerDstFromNearestEdge <= maxViewDst;

            meshObject.transform.position.y.Equals(-44f);
            if (mapDataRecieved)
            {
                if (visible)
                {
                    int lodIndex = 0;
                    for (int i = 0; i < detailLevels.Length - 1; i++)
                    {
                        if (viewerDstFromNearestEdge > detailLevels[i].visibleDstThreshold)
                        {
                            lodIndex = i + 1;
                        }
                        else
                        {
                            break;
                        }
                    }
                    if (lodIndex != previousLODIndex)
                    {
                        if (genGO != null)
                        {
                            genGO.levelOfDetail = lodIndex;
                        }
                        LODMesh lodMesh = lodMeshes[lodIndex];
                        if (lodMesh.hasMesh)
                        {
                            previousLODIndex = lodIndex;
                            meshFilter.mesh  = lodMesh.mesh;
                            if (mapGenerator.generateGameObjects && doneGOGen == false)
                            {
                                genGO            = meshObject.AddComponent <GenerateGameObjects>();
                                genGO.seed       = mapGenerator.seed;
                                genGO.offset     = position;
                                genGO.mapData    = mapData;
                                genGO.scale      = scale;
                                genGO.genClouds  = mapGenerator.generateClouds;
                                genGO.genWater   = mapGenerator.generateWater;
                                genGO.isIsland   = isIsland;
                                genGO.waterPlane = waterPlane;
                                genGO.StartGenerateObjects();
                                if (previousLODIndex != -1)
                                {
                                    genGO.levelOfDetail = previousLODIndex;
                                }
                                else
                                {
                                    genGO.levelOfDetail = 0;
                                }
                                doneGOGen = true;
                            }
                            if (doneGOGen)
                            {
                                genGO.StartGenerateObjects();
                            }
                        }
                        else if (!lodMesh.hasRequestedMesh)
                        {
                            lodMesh.RequestMesh(mapData);
                        }
                    }

                    if (lodIndex == 0)
                    {
                        if (collisionMesh.hasMesh)
                        {
                            meshCollider.sharedMesh = collisionMesh.mesh;
                        }
                        else if (collisionMesh.hasRequestedMesh)
                        {
                            collisionMesh.RequestMesh(mapData);
                        }
                    }

                    terrainChunksVisibleLastUpdate.Add(this);
                }
            }
            SetVisible(visible);
        }