예제 #1
0
    public void DrawMesh(MeshData meshData, MinMaxTracker heightTracker, ColorSettings colorSettings)
    {
        colorGen.UpdateSettings(colorSettings);
        colorGen.UpdateHeight(heightTracker);
        colorGen.UpdateColors();

        meshFilter.sharedMesh = meshData.CreateMesh();
        meshFilter.GetComponent <MeshRenderer>().sharedMaterial = colorSettings.terrainMaterial;
    }
예제 #2
0
    public MeshMatData GetGeneratedData(Vector2 centre, Vector2 coord)
    {
        heightTracker = new MinMaxTracker();

        float[,] map = new float[noiseSettings.mapChunkSize, noiseSettings.mapChunkSize];
        for (int i = 0; i < noiseSettings.SettingsVariations.Length; i++)
        {
            float[,] firstLayerValue = new float[noiseSettings.mapChunkSize, noiseSettings.mapChunkSize];
            if (noiseSettings.SettingsVariations.Length > 0)
            {
                firstLayerValue = NoiseGenerator.GenerateNoise(noiseSettings.SettingsVariations[0], seed, centre);
            }

            SettingsVariation sv = noiseSettings.SettingsVariations[i];
            if (sv.enabled)
            {
                float[,] newMap = NoiseGenerator.GenerateNoise(sv, seed, centre);
                for (int y = 0; y < newMap.GetLength(0); y++)
                {
                    for (int x = 0; x < newMap.GetLength(1); x++)
                    {
                        if (sv.noiseMode == NoiseMode.Add)
                        {
                            float mask = 1;

                            if (sv.useLayerAsMask)
                            {
                                mask = firstLayerValue[x, y];
                                if (sv.InverseLayer)
                                {
                                    mask = 1 - mask;
                                    if (mask <= sv.sampleBelow)
                                    {
                                        mask = 0;
                                    }
                                }
                                else
                                {
                                    if (mask <= sv.sampleBelow)
                                    {
                                        mask = 0;
                                    }
                                }
                            }
                            newMap[x, y] = Mathf.Max(0, newMap[x, y] - sv.minValue);
                            map[x, y]   += newMap[x, y] * sv.strength * mask;
                        }
                        else
                        if (sv.noiseMode == NoiseMode.Multiply)
                        {
                            map[x, y] *= newMap[x, y];
                        }
                        else
                        if (sv.noiseMode == NoiseMode.Subtract)
                        {
                            float mask = 1;

                            if (sv.useLayerAsMask)
                            {
                                mask = firstLayerValue[x, y];
                                if (sv.InverseLayer)
                                {
                                    mask = 1 - mask;
                                    if (mask <= sv.sampleBelow)
                                    {
                                        mask = 0;
                                    }
                                }
                            }
                            newMap[x, y] += Mathf.Min(0, -newMap[x, y] * sv.minValue);
                            map[x, y]    += newMap[x, y] * sv.strength * mask;
                        }
                    }
                }
            }
        }

        Debug.Log(centre);


        for (int x = 0; x < map.GetLength(0); x++)
        {
            for (int y = 0; y < map.GetLength(1); y++)
            {
                heightTracker.AddValue(map[x, y]);
            }
        }

        if (noiseSettings.mapType != MapType.None)
        {
            for (int x = 0; x < map.GetLength(0); x++)
            {
                for (int y = 0; y < map.GetLength(1); y++)
                {
                    float m1     = (map.GetLength(0) - 1) * 3f;
                    float m2     = (map.GetLength(1) - 1) * 3f;
                    float pointX = ((float)x + (centre.x)) / m1;
                    float pointY = ((float)y + (-centre.y)) / m2;


                    map[x, y] = map[x, y] * GradientMap.sampleGradient(pointY, pointX);
                }
            }
        }

        //SmoothenTerrain(map,1,1);
        map = SmoothenTerrain(map);


        colorGen.UpdateSettings(colorSettings);
        colorGen.UpdateHeight(heightTracker);
        colorGen.UpdateColors();
        return(new MeshMatData(map, colorSettings.terrainMaterial));
    }