Esempio n. 1
0
    public float TerrainHeightAt(Vector3 vLoc)
    {
        SectionCoord coord = SectionFor(vLoc);

        if (!SectionExists(coord))
        {
            return(Mathf.NegativeInfinity);
        }
        TerrainSection sec = terrains[coord];

        if (sec == null || sec.terrain == null)
        {
            return(Mathf.NegativeInfinity);
        }
        return(sec.terrain.SampleHeight(vLoc) + sec.terrain.GetPosition().y);
    }
Esempio n. 2
0
 private IEnumerator Remove_CR()
 {
     while (toRemove.Count > 0)
     {
         for (int i = 0; i < maxSimultaneousGens && toRemove.Count > 0; i++)
         {
             SectionCoord   coord = toRemove.Dequeue();
             TerrainSection sec   = terrains[coord];
             if (sec != null && sec.terrain != null)
             {
                 Destroy(sec.terrain.gameObject);
                 terrains.Remove(coord);
             }
         }
         yield return(null);
     }
     removeRunning = false;
 }
Esempio n. 3
0
        public void WriteTo(BinaryWriter bw)
        {
            var header = new Header
            {
                GimmickCount = (ushort)GimmickSection.Items.Count,
                Width        = (ushort)TerrainSection.MapMatrix[0].Count,
                Height       = (ushort)TerrainSection.MapMatrix.Count,
            };

            bw.Seek(Header.HeaderLength, SeekOrigin.Begin);
            PositionSection.WriteTo(bw);
            header.TerrainSectionOffset = (ushort)bw.BaseStream.Position;
            TerrainSection.WriteTo(bw);
            header.GimmickSectionOffset = (ushort)bw.BaseStream.Position;
            GimmickSection.WriteTo(bw);

            header.FileLength = (ushort)bw.BaseStream.Length;
            bw.BaseStream.Seek(0, SeekOrigin.Begin);
            header.WriteTo(bw);
        }
Esempio n. 4
0
    private IEnumerator GenerateSection_CR(SectionCoord coord)
    {
        TerrainSection sec;

        if (terrains.TryGetValue(coord, out sec) && sec != null &&
            sec.terrain != null)
        {
            if (numGenThreads > 0)
            {
                numGenThreads--;
            }
            yield break;
        }
        if (sec == null)
        {
            sec = new TerrainSection(coord);
        }

        float[,] heightmap  = null;
        float[,,] alphamaps = null;
        int                 bx, bz;
        float               height;
        Vector3             targLoc;
        GameObject          go;
        BiomeData           bd;
        List <SectionCoord> biomeCenters;

        List <Biome> containedBiomes = null;
        List <DetailPrototypeData> detailPrototypeDatas = null;
        List <int[, ]>             detailMaps           = null;
        List <TreePrototypeData>   treePrototypeDatas   = null;
        List <TreeInstance>        treeInstances        = null;

        Thread heightThread = new Thread(() =>
        {
#if DEBUG_THREADS
            UnityEngine.Profiling.Profiler.BeginThreadProfiling(
                "Heightmap", "" + coord.x + ":" + coord.z);
#endif
            heightmap = GenerateHeightmap(coord);
#if DEBUG_THREADS
            UnityEngine.Profiling.Profiler.EndThreadProfiling();
#endif
        });
        Thread alphaThread = new Thread(() =>
        {
#if DEBUG_THREADS
            UnityEngine.Profiling.Profiler.BeginThreadProfiling(
                "Alphamap", "" + coord.x + ":" + coord.z);
#endif
            alphamaps = GenerateAlphamaps(coord, out containedBiomes);
#if DEBUG_THREADS
            UnityEngine.Profiling.Profiler.EndThreadProfiling();
#endif
        });
        Thread detailThread = new Thread(() =>
        {
#if DEBUG_THREADS
            UnityEngine.Profiling.Profiler.BeginThreadProfiling(
                "Detailmap", "" + coord.x + ":" + coord.z);
#endif
            detailMaps = GenerateDetailMaps(coord,
                                            out detailPrototypeDatas);
#if DEBUG_THREADS
            UnityEngine.Profiling.Profiler.EndThreadProfiling();
#endif
        });
        Thread treeThread = new Thread(() =>
        {
#if DEBUG_THREADS
            UnityEngine.Profiling.Profiler.BeginThreadProfiling(
                "Treemap", "" + coord.x + ":" + coord.z);
#endif
            treeInstances = GenerateTreeInstances(coord,
                                                  out treePrototypeDatas);
#if DEBUG_THREADS
            UnityEngine.Profiling.Profiler.EndThreadProfiling();
#endif
        });

        yield return(null);

        treeThread.Start();
        yield return(null);

        heightThread.Start();
        yield return(null);

        alphaThread.Start();
        yield return(null);

        detailThread.Start();
        yield return(new WaitUntil(() => (heightmap != null &&
                                          alphamaps != null && detailMaps != null &&
                                          treeInstances != null && !loadingSection)));

        TerrainLayer[] terrainLayers = new TerrainLayer[containedBiomes.Count];
        loadingSection = true;
        sectionLoading = coord;

        TerrainData data = new TerrainData();

        data.heightmapResolution = genSettings.heightMapRes;
        yield return(null);

        data.alphamapResolution = genSettings.alphaMapRes;
        data.SetDetailResolution(genSettings.detailMapRes,
                                 genSettings.detailMapResPerPatch);
        data.size = new Vector3(genSettings.length, genSettings.height,
                                genSettings.length);
        yield return(null);

        data.SetHeights(0, 0, heightmap);
        yield return(null);

        for (int i = 0; i < containedBiomes.Count; i++)
        {
            terrainLayers[i] = containedBiomes[i].terrainLayer;
        }

        DetailPrototype[] detailPrototypes = null;
        if (detailPrototypeDatas.Count > 0)
        {
            detailPrototypes = new DetailPrototype[detailPrototypeDatas.Count];
        }
        for (int i = 0; i < detailPrototypeDatas.Count; i++)
        {
            DetailPrototype     dp  = detailPrototypes[i] = new DetailPrototype();
            DetailPrototypeData dpd = detailPrototypeDatas[i];
            dp.bendFactor   = dpd.bendFactor;
            dp.dryColor     = dpd.dryColor;
            dp.healthyColor = dpd.healthyColor;
            dp.maxHeight    = dpd.maxHeight;
            dp.maxWidth     = dpd.maxWidth;
            dp.minHeight    = dpd.minHeight;
            dp.minWidth     = dpd.minWidth;
            dp.noiseSpread  = dpd.noiseSpread;
            if (dpd.prototype != null)
            {
                dp.prototype        = dpd.prototype;
                dp.usePrototypeMesh = true;
            }
            else
            {
                dp.prototypeTexture = dpd.prototypeTexture;
                dp.usePrototypeMesh = false;
            }
            dp.renderMode = dpd.renderMode;
        }
        yield return(null);

        data.terrainLayers = terrainLayers;
        data.SetAlphamaps(0, 0, alphamaps);
        yield return(null);

        TreePrototype[] treePrototypes = null;
        if (treePrototypeDatas.Count > 0)
        {
            treePrototypes = new TreePrototype[treePrototypeDatas.Count];
        }
        for (int i = 0; i < treePrototypeDatas.Count; i++)
        {
            TreePrototype tp = treePrototypes[i] = new TreePrototype();
            tp.bendFactor = treePrototypeDatas[i].bendFactor;
            tp.prefab     = treePrototypeDatas[i].prefab;
        }
        yield return(null);

        if (detailPrototypes != null)
        {
            data.detailPrototypes = detailPrototypes;
            yield return(null);

            for (int i = 0; i < detailMaps.Count; i++)
            {
                data.SetDetailLayer(0, 0, i, detailMaps[i]);
            }
            yield return(null);
        }
        if (treePrototypes != null)
        {
            data.treePrototypes = treePrototypes;
            yield return(null);

            data.SetTreeInstances(treeInstances.ToArray(), true);
        }
        yield return(null);

        data.RefreshPrototypes();

        yield return(null);

        GameObject obj = Terrain.CreateTerrainGameObject(data);

        sec.terrain = obj.GetComponent <Terrain>();

        sec.terrain.treeBillboardDistance = 150;
        sec.terrain.materialTemplate      = terrainMaterial;

        sec.terrain.allowAutoConnect = true;
        obj.transform.position       = new Vector3(coord.x * genSettings.length -
                                                   genSettings.length / 2, 0f, coord.z * genSettings.length -
                                                   genSettings.length / 2);
        yield return(null);

        sec.terrain.Flush();
        yield return(null);

        bx = Mathf.RoundToInt((coord.x * genSettings.length) /
                              biomeCenterSpacing);
        bz = Mathf.RoundToInt((coord.z * genSettings.length) /
                              biomeCenterSpacing);
        biomeCenters = SectionsInRadius(new SectionCoord(bx, bx), 2);
        for (int i = 0; i < biomeCenters.Count; i++)
        {
            BiomeCenter center = SafeGetBiomeCenter(biomeCenters[i]);
            height  = sec.terrain.SampleHeight(center.center);
            targLoc = new Vector3(center.center.x, height, center.center.z +
                                  sec.terrain.GetPosition().y);
            if (sec.terrain.terrainData.bounds.Contains(targLoc -
                                                        sec.terrain.transform.position))
            {
                if (center.biome.ambientPrefab != null)
                {
                    go = Instantiate(center.biome.ambientPrefab, targLoc,
                                     Quaternion.identity, sec.terrain.transform);
                }
                else
                {
                    go = new GameObject();
                    go.transform.position = targLoc;
                    go.transform.parent   = sec.terrain.transform;
                }
                bd            = go.AddComponent <TerrainManager.BiomeData>();
                bd.biome      = center.biome;
                bd.center     = targLoc;
                bd.boundaries = center.properBounds.Values.ToList();
                SimplifyConvexPlanes(bd.boundaries, bd.center);
            }
        }

        yield return(null);

        terrains.Add(coord, sec);
        if (numGenThreads > 0)
        {
            numGenThreads--;
        }
        generating.Remove(coord);
        if (coord.Equals(sectionLoading))
        {
            loadingSection = false;
        }
    }