Esempio n. 1
0
    public static float[] Erode(float[] values, HightMapSettings hightMapSettings, MeshSettings meshSettings, ComputeShader erosionShader, int seed)
    {
        if (hightMapSettings.erossionSettings.numberOfIterationsMode == ErossionSettings.NumberOfIterationsMode.perSurface)
        {
            hightMapSettings.erossionSettings.numErosionIterations = Mathf.RoundToInt(hightMapSettings.erossionSettings.numberOfIterations * meshSettings.numberVerticisPerLine * meshSettings.numberVerticisPerLine);
        }
        else
        {
            hightMapSettings.erossionSettings.numErosionIterations = Mathf.RoundToInt(hightMapSettings.erossionSettings.numberOfIterations);
        }

        ErossionSettings.ErosionMode erosionMode = hightMapSettings.erossionSettings.erosionMode;
        if (erosionMode == ErossionSettings.ErosionMode.CPU)
        {
            return(ErodeCPU(values, meshSettings.numberVerticisPerLine, hightMapSettings.erossionSettings, seed));
        }
        else if (erosionMode == ErossionSettings.ErosionMode.GPU)
        {
            return(ErodeGPU(values, meshSettings.numberVerticisPerLine, hightMapSettings.erossionSettings, erosionShader));
        }
        else
        {
            return(values);
        }
    }
Esempio n. 2
0
    public static HightMap GenHightMap(MeshSettings meshSettings, HightMapSettings settings, Vector2 center)
    {
        AnimationCurve animationCurve_t = new AnimationCurve(settings.animationCurve.keys);

        float minV = float.MaxValue;
        float maxV = float.MinValue;

        float[,] falloffMap = null;

        if (settings.useFalloff)
        {
            if (falloffMap == null)
            {
                falloffMap = fallofGenerator.GenerateFalloff(meshSettings.numberVerticisPerLine + 2, settings.FalloffBlur, settings.FalloffSize);
            }
        }

        float[,] valuse = PerlinNoise.GeneratorNoise(meshSettings.numberVerticisPerLine + 2, meshSettings.numberVerticisPerLine + 2, settings.noiseSettings, center);

        //Erode
        valuse = HightMap.ConwertTabBack(Erosion.Erode(HightMap.ConwertTab(valuse, meshSettings.numberVerticisPerLine + 2), settings, meshSettings, settings.erosionShader, 1), meshSettings.numberVerticisPerLine + 2);



        for (int i = 0; i < meshSettings.numberVerticisPerLine + 2; i++)
        {
            for (int j = 0; j < meshSettings.numberVerticisPerLine + 2; j++)
            {
                if (settings.useFalloff)
                {
                    valuse[i, j] = Mathf.Clamp(valuse[i, j] - falloffMap[i, j], 0, 1) * animationCurve_t.Evaluate(valuse[i, j]) * settings.heightMultiplayer;
                }
                else
                {
                    valuse[i, j] *= animationCurve_t.Evaluate(valuse[i, j]) * settings.heightMultiplayer;
                }
                if (valuse[i, j] > maxV)
                {
                    maxV = valuse[i, j];
                }
                if (valuse[i, j] < minV)
                {
                    minV = valuse[i, j];
                }
            }
        }



        return(new HightMap(valuse, minV, maxV));
    }
    private void Start()
    {
        display = FindObjectOfType <display>() as display;

        mSettings = display.meshSettings;
        hSettings = display.hightMapSettings;
        material  = display.terrainMateial;


        maxViveDst    = detail[detail.Length - 1].visibleDistance;
        meshWorldSize = mSettings.mechWorldSize;
        chunksVisible = Mathf.RoundToInt(maxViveDst / meshWorldSize);
        if (display.IsEndlessMode)
        {
            UpdateVisbleChunk();
        }
    }
        public TerrainChunk(Vector2 coord, HightMapSettings hightMapSettings, MeshSettings meshSettings, float size, LODInfo[] lodInfo, int colliderMeshIndex, Transform parent, Material material)
        {
            this.coord             = coord;
            this.hightMapSettings  = hightMapSettings;
            this.meshSettings      = meshSettings;
            this.lodInfo           = lodInfo;
            this.colliderMeshIndex = colliderMeshIndex;



            position = coord * size / meshSettings.uniformScale;
            Vector2 center = coord * size;

            bounds = new Bounds(position, Vector2.one * size);

            Vector3 position3D = new Vector3(position.x, 0, position.y);

            meshObj               = new GameObject("Chunk");
            meshRenderer          = meshObj.AddComponent <MeshRenderer>();
            meshFilter            = meshObj.AddComponent <MeshFilter>();
            meshCollider          = meshObj.AddComponent <MeshCollider>();
            meshRenderer.material = material;

            meshObj.transform.position   = position3D * scale;
            meshObj.transform.parent     = parent;
            meshObj.transform.localScale = Vector3.one * scale;
            SetVisible(false);

            lodMeshes = new LODMesh[lodInfo.Length];
            for (int i = 0; i < lodInfo.Length; i++)
            {
                lodMeshes[i] = new LODMesh(lodInfo[i].lod);
                lodMeshes[i].updateCalback += UpdateChunk;
                if (i == colliderMeshIndex)
                {
                    lodMeshes[i].updateCalback += UpdateColissionMesh;
                }
            }

            TreadedDataRequest.RequestData(() => hightMapGenerator.GenHightMap(meshSettings, hightMapSettings, center), OnMapDataRecive);
        }
    public IEnumerator GenerateEnvironment(HightMap heightMap, MechData meshData, MeshSettings meshSettings, HightMapSettings hightMapSettings, EnvironmentData environmentData, int mapChunkSize, float scale, float min, float max)
    {
        int seed;

        if (environmentData.usingHeightMapSeed)
        {
            seed = hightMapSettings.noiseSettings.seed;
        }
        else
        {
            seed = environmentData.seed;
        }

        System.Random prng = new System.Random(seed);


        bool useFlatSheadedB            = false;
        int  flatSheadedChunkSizeIndexB = 0;

        if (meshSettings.useFlatShader)
        {
            flatSheadedChunkSizeIndexB  = meshSettings.chunkSizeIndex;
            useFlatSheadedB             = true;
            meshSettings.chunkSizeIndex = meshSettings.flatSheadedChunkSizeIndex;
            meshSettings.useFlatShader  = false;
        }

        for (int i = 0; i < environmentData.types.Length; i++)
        {
            panelProcess.setNumer(i + 1 + "/" + environmentData.types.Length);
            panelProcess.setLabel("Environment ");

            EnvironmentData.Types now = environmentData.types[i];
            float[,] noise = PerlinNoise.GeneratorNoise(mapChunkSize + 2, mapChunkSize + 2, now.noiseData.noiseSettings, Vector2.zero);

            float[] noisMapEnd = HightMap.ConwertTab(noise, mapChunkSize + 2);
            int     msi        = (now.LOD == 0) ? 1 : now.LOD * 2;
            Vector3 lastPos    = Vector3.zero;
            int     len        = ((int)(mapChunkSize + 2) / msi) + 1;
            len = len * len;

            Vector3[] points          = mechDraw.GenerateMech(heightMap.value, meshSettings, now.LOD).vertices;
            Vector3[] orginalVerticis = meshData.vertices;

            for (int j = 0; j < len; j++)
            {
                Vector3 nowPos = Vector3.zero;
                if ((positioningMode == PositioningMode.Calculation) || (positioningMode == PositioningMode.BothMode))
                {
                    nowPos = orginalVerticis[CalculationPos(points, orginalVerticis, j, len)];
                }
                else
                {
                    nowPos = points[j];
                }


                panelProcess.setValue((float)j / len);

                float wynik = map(min, max, 0, 1, nowPos.y);


                //if (true)
                if (noisMapEnd[j] < now.curve.Evaluate(wynik))
                {
                    if (lastPos != nowPos)
                    {
                        Vector3 randPos = new Vector3(prng.Next(-now.LOD * environmentData.prngPrecision, now.LOD * environmentData.prngPrecision) / environmentData.prngPrecision, 0, prng.Next(-now.LOD * environmentData.prngPrecision, now.LOD * environmentData.prngPrecision) / environmentData.prngPrecision) * now.randPos;

                        float x = 0, y = 0, z = 0;
                        if (now.randRotX)
                        {
                            x = prng.Next(0, 359);
                        }
                        if (now.randRotY)
                        {
                            y = prng.Next(0, 359);
                        }
                        if (now.randRotZ)
                        {
                            z = prng.Next(0, 359);
                        }
                        if ((positioningMode == PositioningMode.Colision) || (positioningMode == PositioningMode.BothMode))
                        {
                            nowPos.y = ColidePos(nowPos.x + randPos.x, nowPos.z + randPos.z, heightMap.minV, heightMap.maxV) - randPos.y;
                        }
                        Vector3 randRot = new Vector3(x, y, z);
                        lastPos = nowPos;

                        GameObject o = Instantiate(now.Object, (nowPos + randPos) * scale, Quaternion.Euler(now.rotation + randRot));

                        Transform tObject = o.GetComponent <Transform>() as Transform;
                        tObject.SetParent(parent, true);
                        tObject.localScale = now.scale;

                        if (j % skip == 0)
                        {
                            yield return(null);
                        }
                    }
                }
            }
        }
        yield return(null);

        if (useFlatSheadedB)
        {
            meshSettings.chunkSizeIndex = flatSheadedChunkSizeIndexB;
            meshSettings.useFlatShader  = true;
        }
        panelProcess.setActive(false);
    }