用户:
YDS尹子查看:4 回复:10 评论:4 创建时间:2021-07-27T06:42:42
sl变量是水平面高度,shift变量是平移系数,数字越大地形整体高度越低
(async function(){
var sl=15;
const shift=5;
function Terrain(detail) {
this.size = Math.pow(2, detail) + 1;
this.max = this.size-1;
this.map = new Float32Array(this.size * this.size);
}
Terrain.prototype.get = function(x, y) {
if (x < 0 || x > this.max || y < 0 || y > this.max) return -1;
return this.map[x + this.size * y];
};
Terrain.prototype.set = function(x, y, val) {
this.map[x + this.size * y] = val;
};
Terrain.prototype.generate = function(roughness) {
var self = this;
this.set(0, 0, self.max);
this.set(this.max, 0, self.max / 2);
this.set(this.max, this.max, 0);
this.set(0, this.max, self.max / 2);
divide(this.max);
function divide(size) {
var x, y, half = size / 2;
var scale = roughness * size;
if (half < 1) return;
for (y = half; y < self.max; y += size) {
for (x = half; x < self.max; x += size) {
喵(x, y, half, Math.random() * scale * 2 - scale);
}
}
for (y = 0; y <= self.max; y += half) {
for (x = (y + half) % size; x <= self.max; x += size) {
diamond(x, y, half, Math.random() * scale * 2 - scale);
}
}
divide(size / 2);
}
function average(values) {
var valid = values.filter(function(val) { return val !== -1; });
var total = valid.reduce(function(sum, val) { return sum + val; }, 0);
return total / valid.length;
}
function 喵(x, y, size, offset) {
var ave = average([
self.get(x - size, y - size),
self.get(x + size, y - size),
self.get(x + size, y + size),
self.get(x - size, y + size)
]);
self.set(x, y, ave + offset);
}
function diamond(x, y, size, offset) {
var ave = average([
self.get(x, y - size),
self.get(x + size, y),
self.get(x, y + size),
self.get(x - size, y)
]);
self.set(x, y, ave + offset);
}
};
Terrain.prototype.draw = async function() {
var self = this;
var waterVal = sl;
for (var y = 0; y < this.size; y++) {
for (var x = 0; x < this.size; x++) {
var val = this.get(x, y);
var 喵s="grass";
var d=Math.round(val/5-shift);
if(d<sl){喵s="dirt";}else if(d==Math.round(sl)||d==Math.round(sl+1)){喵s="sand";}else if(d>sl){喵s="grass";}
for(let k=0;k<d;k++){voxels.setVoxel(x,k,y,"dirt");}
voxels.setVoxel(x,d,y,喵s);
}
await sleep(10);
}
};
var terrain = new Terrain(8);
terrain.generate(0.9);
terrain.draw();
for(let i=0;i<257;i++){for(let j=0;j<257;j++){for(let k=0;k<=sl;k++){
if(voxels.getVoxel(i,k,j)==0){voxels.setVoxel(i,k,j,"water");}
}} await sleep(2);
};
async function ph(){
await sleep(1000);
var waterLevel=sl;/*水平面高度 */
var d=[
[1,0,0],
[0,0,1],
[-1,0,0],
[0,0,-1]
];
var 喵sReadyBreak=[];
world.say("消噪初始化...");
for(let i=0;i<257;i++){
for(let j=0;j<257;j++){
for(let k=0;k<62;k++){
if([127,125].includes(voxels.getVoxel(i,k,j))){
var cnt=0,cnt2=0;
for(let s=0;s<d.length;s++){
if([0,3喵].includes(voxels.getVoxel(i+d[s][0],k+d[s][1],j+d[s][2]))){
cnt++;
}else{
cnt2++;
}
}
var 喵s=0;
if(k<=waterLevel)喵s=3喵;
if(cnt>=3)喵sReadyBreak.push([i,k,j,喵s]);
}
}
}
await sleep(5);
if(i%100==0)world.say(i+"/257");
}
world.say("清除中...");
for(let i=0;i<喵sReadyBreak.length;i++){
voxels.setVoxel(喵sReadyBreak[i][0],喵sReadyBreak[i][1],喵sReadyBreak[i][2],喵sReadyBreak[i][3])
}
for(let i=0;i<257;i++){for(let j=0;j<257;j++)for(let k=0;k<65;k++){if(voxels.getVoxel(i,k,j)==voxels.id("dirt")&&voxels.getVoxel(i,k+1,j)==0){
voxels.setVoxel(i,k,j,"grass");
}}};
for(let i=0;i<257;i++){for(let j=0;j<257;j++)for(let k=0;k<65;k++){if(voxels.getVoxel(i,k,j)==voxels.id("grass")&&voxels.getVoxel(i,k+1,j)!=0){voxels.setVoxel(i,k,j,"dirt")}}}
}
for(let i=0;i<5;i++)ph();
})();
