dongliang1941 2016-08-14 20:18
浏览 8

程序生成

I'm interested in generating procedural map using php, it can be static. I just need to get me on the right way... I red a lot of articles about it and understand that I need to use noise generation to get basic map shape. but what after that?! I will be using simple tiles 32x32. It it even possible/efficient to do it via php?

  • 写回答

1条回答 默认 最新

  • duanlei8119 2018-01-22 17:52
    关注

    Look into using Perlin or OpenSimplex noise.

    You can either implement this yourself or simply use a library:

    https://github.com/martinlindhe/php-noisegenerator

    You can generate a heightmap into an array using this library:

    $perlin = new \NoiseGenerator\PerlinNoise(3000);
    $width = 100;
    $height = 100;
    
    $octaves = array(64, 16, 4, 2);
    $noise = array();
    for ($y = 0; $y < $height; $y += 1) {
        for ($x = 0; $x < $width; $x += 1) {
            $num = $perlin->noise($x, $y, 0, $octaves);
            $raw = ($num / 2) + .5;
            if ($raw < 0) $raw = 0;
            $noise[$x][$y] = $raw;
        }
    }
    

    Now that you have your noise array populated, you can represent this with color on your front end using JS or whatever you want. I'll let you figure out the details on that end, but essentially, find the range of your numbers in your heightmap and determine thresholds for different types of terrain.

    I wrote a tutorial on generating heightmaps and rendering them using noise over here: https://plaxdev.wordpress.com/2017/05/04/procedurally-generating-islands/

    This doesn't use PHP, only JavaScript, but the idea behind rendering the heightmap in JS is included in that article if you're struggling with that.

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大