Skip to content

Instantly share code, notes, and snippets.

@jkwok91
Created April 9, 2017 05:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkwok91/c968b4d9c7b8b3d7f4b1bf1e0b30db67 to your computer and use it in GitHub Desktop.
Save jkwok91/c968b4d9c7b8b3d7f4b1bf1e0b30db67 to your computer and use it in GitHub Desktop.
cuz she's so highhhhhhh / high above me / she's so lovely / she's so high / like cleopatra, joan of arc, or aphrodiiiiiiiite
// do i remember anything from three.js?????
// the answer is no
// kool weird jagged landscape that is totally not correct
var camera, scene, renderer;
init();
animate();
var mesh;
var points, unit;
var rows, cols;
function init() {
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(75, window.innerWidth/window.innerHeight, 1, 1000);
camera.position.set(0, 0, 100);
renderer = new THREE.WebGLRenderer( {antialias: true }); // still no idea what the diff types of renderers do???
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
points = [];
unit = 25;
cols = 20;
rows = 10;
getPoints();
}
function getPoints() {
points = [];
for (var j = 0; j < rows; j++) {
var row = [];
for (var i = 0; i < cols; i++) {
var z = unit*(Math.random()-0.5);
row.push(z);
}
points.push(row);
}
}
function getPt(x, y) {
return new THREE.Vector3(x*unit, y*unit, points[y][x]);
}
function drawTerrain() {
var m1 = new THREE.LineBasicMaterial({
color: 0xffffff,
transparent: true,
opacity: 0.5
});
var g1 = new THREE.Geometry();
for (var j = 0; j < rows-1; j++) {
for (var i = 0; i < cols-1; i++) {
g1.vertices.push(
getPt(i, j),
getPt(i, j+1),
getPt(i, j),
getPt(i+1, j),
getPt(i, j),
getPt(i+1, j+1)
);
if (i == cols-2) {
g1.vertices.push(
getPt(i+1, j),
getPt(i+1, j+1)
);
}
if (j == rows-2) {
g1.vertices.push(
getPt(i, j+1),
getPt(i+1, j+1)
);
}
}
}
mesh = new THREE.LineSegments(
g1,
new THREE.LineBasicMaterial({
color: 0xffffff,
transparent: true,
opacity: 0.5
})
);
mesh.position.set(-(cols*unit)/2, -(rows*unit)/4, -(rows*unit)/4);
mesh.rotation.x = -0.5;
scene.add(mesh);
}
function getNext(i, j) {
var n = points[j][i];
var startY = (j > 0) ? j-1 : j;
var startX = (i > 0) ? i-1 : i;
var endY = (j < rows-1) ? j+1 : j;
var endX = (i < cols-1) ? i+1 : i;
var neighbors = [];
for (var y = startY; y <= endY; y++) {
for (var x = startX; x <= endX; x++) {
//if (y != j && i != x) {
neighbors.push(points[y][x]);
//}
}
}
var sum = neighbors.reduce(function(acc, n) {
return acc+n;
}, 0);
var avg = sum/neighbors.length;
// ok i hve no idea how to write a good random fn lol
if (Math.abs(avg-n) < 0.1) {
n += (Math.random() - 0.5)*2*unit;
} else if (n > avg) {
n -= 0.1;
} else {
n += 0.1;
}
points[j][i] = n;
}
function animate() {
requestAnimationFrame(animate);
render();
}
function render() {
while(scene.children.length > 0){
scene.remove(scene.children[0]);
}
points.map(function(row, j) {
row.map(function(pt, i) {
getNext(i, j);
});
});
drawTerrain();
renderer.render(scene, camera);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment