猫史档案馆


【教程贴】随机种树

用户:datacatdatacat查看:16 回复:10 评论:16 创建时间:2020-04-16T11:38:49


大家好,我又来胃功能工作是升级了

今天呢我们来学随机种树的代码

emotion_编程猫_加油

首先,大家知道随机数代码吧

if(Math.round(Math.random()*tmp_05+1)===1){

这是一个例子

emotion_编程猫_厉害了

那么,随机树代码就要用到它,random

function make_tree(leaves_width,leaves_length,leaves_height,trunk_x_from,trunk_y_from,trunk_z_from,trunk_x_to,trunk_y_to,trunk_z_to){

这是一个基础代码

emotion_编程猫_冷漠

var tmp_01=trunk_x_to - trunk_x_from;
    var tmp_02=trunk_y_to - trunk_y_from;
    var tmp_03=trunk_z_to - trunk_z_from;
    var tmp_04=Math.round((tmp_01+tmp_02+tmp_03)/3)-1;

随机生成树干的代码

emotion_编程猫_紧张

var tmp_05=127-tmp_04;//计算出随机数可能

这个代码已经注释

  for(let i=trunk_x_from;i<=trunk_x_to;i++){
        for(let j=trunk_y_from;j<=trunk_y_to;j++){
            for(let k=trunk_z_from;k<=trunk_z_to;k++){//遍历树干
                if(Math.round(Math.random()*tmp_05+1)!=1){//有一定可能在树干上缺少几个方块,表现出真实感
                    voxels.setVoxel(i,j,k,'acacia');
                }
                if(Math.round(Math.random()*tmp_05+1)===1){//也有一定可能在树干外突出几个方块,表现出真实感
                    var rand=Math.round(Math.random()*4+1);
                    if(rand===1){
                        voxels.setVoxel(i+1,j,k,'acacia');
                    } else if(rand===2){
                        voxels.setVoxel(i,j,k+1,'acacia');
                    } else if(rand===3){
                        voxels.setVoxel(i-1,j,k,'acacia');
                    } else if(rand===4){
                        voxels.setVoxel(i,j,k-1,'acacia');
                    }
                }
            }
        }
    }

再加上是这行代码,就可以生成一个树干

emotion_编程猫_搓头

下面我们将证明怎么生成树叶

    //随机生成树叶
    for(let i=trunk_x_to - leaves_length/2;i<=trunk_x_to + leaves_length/2;i++){//树叶的x接树干x的一半,这样更真实
        for(let j=trunk_y_to;j<=trunk_y_to + leaves_height;j++){//树叶的y直接接树干的y的顶部,不然就不像树了
            for(let k=trunk_z_to - leaves_width/2;k<=trunk_z_to + leaves_width/2;k++){//树叶的z同树叶的x
                if(Math.round(Math.random()*2+1)===1){//有三分之一的可能建出树叶
                    voxels.setVoxel(i,j,k,'green_leaf');
                }
            }
        }
    }
}

让后调用函数

make_tree(10,10,10,65,9,65,67,15,67);

就可以生树了

emotion_雷电猴_哇噻

惊天的教程就到这里,你学会了吗

emotion_编程猫_点赞


回复

上一页1 页 / 共 1下一页
datacatdatacat

那个喵是什么bug

点赞0


评论


hecoshecos

沙发

点赞0


评论


hecoshecos

var a_01=trunk_x_to - trunk_x_from;
    var a_02=trunk_y_to - trunk_y_from;
    var a_03=trunk_z_to - trunk_z_from;
    var a_04=Math.round((a_01+a_02+a_03)/3)-1;
    var a_05=127-a_04;//计算出随机数可能

帮你换掉了

点赞1


评论


面具狐狸云绾兮面具狐狸云绾兮

...我是岛3第一肝帝,我为什么要用代码?(滑稽)

点赞0


评论


鹅城县长鹅城县长

为什么有屏蔽词?

点赞0


评论


datacatdatacat

啊啊

点赞0


评论


datacatdatacat

dd

点赞0


评论


datacatdatacat

emotion_dogeemotion_喵喵

点赞0


评论


datacatdatacat

dd

点赞0


评论


七式草莓七式草莓

给我完整的代码!!

点赞0


评论