Exemplo n.º 1
0
    /// <summary>
    /// 计算柏林噪声
    /// </summary>
    private void CalcNoise()
    {
        float y = .0f;

        while (y < noiseTex.height)
        {
            float x = .0f;
            int   w = 0;
            int   h = 0;

            while (x < noiseTex.width)
            {
                // 计算出X的采样值
                float xCoord = xOrg + x / noiseTex.width * scale;
                // 计算出Y的采样值
                float yCoord = yOrg + y / noiseTex.height * scale;
                // 用计算出的采样值计算柏林噪声
                float sample = Mathf.PerlinNoise(xCoord, yCoord);
                Color color  = Color.white;


                foreach (var item in GetColorByHeight_Dic)
                {
                    if (sample < item.Key)
                    {
                        GetColorByHeight_Dic.TryGetValue(item.Key, out color);
                        break;
                    }
                }

                // 填充颜色数组
                w = PlotSize > noiseTex.width - (int)x ? noiseTex.width - (int)x : PlotSize;
                h = PlotSize > noiseTex.height - (int)y ? noiseTex.height - (int)y : PlotSize;

                for (int i = 0; i < h; i++)
                {
                    for (int j = 0; j < w; j++)
                    {
                        pix[Convert.ToInt32((i + y) * noiseTex.width + j + x)] = color;
                    }
                }
                for (int i = (int)x; i < w; i++)
                {
                    for (int j = (int)y; j < h; j++)
                    {
                        pix[Convert.ToInt32(j * noiseTex.width + i)] = color;
                    }
                }
                //pix[Convert.ToInt32(y * noiseTex.width + x)] = color;

                x += w;
            }
            y += h;
        }
        noiseTex.SetPixels(pix);
        noiseTex.Apply();
    }
    /// <summary>
    /// 填充地块数据
    /// </summary>
    private void FillPlotData()
    {
        Debug.Log("进入Mediator,填充地块数据");

        int y     = 0;
        int max_x = 0;
        int max_y = 0;

        while (y < _MapHeight)
        {
            int x = 0;
            int w = 0;
            int h = 0;
            while (x < _MapWidth)
            {
                //// 计算出X的采样值
                //float xCoord = _XOrg + x / _NoiseTex.width * _Scale;
                //// 计算出Y的采样值
                //float yCoord = _YOrg + y / _NoiseTex.height * _Scale;
                //// 用计算出的采样值计算柏林噪声
                //float sample = Mathf.PerlinNoise(xCoord, yCoord);
                Color color = Color.white;


                if (x % _Scale == 0 && x != 0)
                {
                    max_x++;
                }
                if (max_x >= _MapData.GetLength(0))
                {
                    continue;
                }

                foreach (var item in GetColorByHeight_Dic)
                {
                    if (_MapData[max_x, max_y] < item.Key)
                    {
                        GetColorByHeight_Dic.TryGetValue(item.Key, out color);
                        break;
                    }
                }

                // 填充颜色数组
                w = _PlotSize > _NoiseTex.width - (int)x ? _NoiseTex.width - (int)x : _PlotSize;
                h = _PlotSize > _NoiseTex.height - (int)y ? _NoiseTex.height - (int)y : _PlotSize;

                for (int i = 0; i < h; i++)
                {
                    for (int j = 0; j < w; j++)
                    {
                        _Pix[Convert.ToInt32((i + y) * _NoiseTex.width + j + x)] = color;
                    }
                }
                for (int i = (int)x; i < w; i++)
                {
                    for (int j = (int)y; j < h; j++)
                    {
                        _Pix[Convert.ToInt32(j * _NoiseTex.width + i)] = color;
                    }
                }
                //pix[Convert.ToInt32(y * noiseTex.width + x)] = color;

                //x += w;
                x++;
            }
            if (y % _Scale == 0 && y != 0)
            {
                max_y++;
            }
            if (max_y >= _MapData.GetLength(1))
            {
                break;
            }
            max_x = 0;
            //y += h;
            y++;
        }
        _NoiseTex.SetPixels(_Pix);
        _NoiseTex.Apply();
    }