Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id='container'></div>
              
            
!

CSS

              
                body { margin:0;padding:0;background:#000;}
.button {
  font-family: verdana;
  display: inline-block;
  height: 20px;
  padding: 5px;
  margin: 5px;
  background:rgba(200,100,100,0.3);
  cursor:pointer;
}
.button:hover {
  background:rgba(200,100,100,0.5);
}
              
            
!

JS

              
                /*
// Thansk to @Xanmia for the 3d setup


i don't know shaders.
this whole thing could be done much better with a pure shader.
with 10000s more cubes.


*/

var container = document.getElementById('container');


var scalingMode = 0, scalingModes = 3;
var colourMode = 0, colourModes = 3;
var cameraMode = 0, cameraModes = 2;

// viewable canvas size
var stageWidth = 1200, stageHeight = 600;

// don't tweak the below too numbers - currently rendering 25^2 cubes, this number is already too large.
var xRows = 25, zRows = 25;

var cubeSize = 600;
var cubeGap = 200;
var cubeRow = cubeSize + cubeGap;



var camera = new THREE.PerspectiveCamera(55, stageWidth/stageHeight, 1, 30000);
camera.position.y = 5000;
camera.lookAt( new THREE.Vector3(0,0,0) );

var scene = new THREE.Scene();
//scene.fog = new THREE.Fog( 0x000000, 5000, 10000 );
// fog is disabled in this demo due to camera toggling
// when the camera goes to birds eye view, you can't see nothing.

var pointLight =  new THREE.PointLight(0xffffff);
pointLight.position.x = 0;
pointLight.position.y = 1800;
pointLight.position.z = -1800;
scene.add(pointLight);

group = new THREE.Object3D();
scene.add( group );

var cubes = [];

// offsets for centre point
var halfXRows = (cubeRow * -xRows / 2);
var halfZRows = (cubeRow * -zRows / 2);

for (var x = 0; x < xRows; x++) {
  // creating a 2d array
  cubes[x] = []
  for (var z = 0; z < zRows; z++) { 
    var cubeHeight = 100 + Math.random() * 700;
    
    //cubeHeight = 10 + (Math.sin(x / xRows * Math.PI) + Math.sin(z / zRows * Math.PI)) * 600;
    
    var geometry = new THREE.BoxGeometry(cubeSize, cubeHeight, cubeSize);

    var material = new THREE.MeshPhongMaterial( {
      ambient: 0x030303,
      color: 0x4444ff, 
      specular: 0xffffff,
      shininess: 10, //~~(Math.random() * 200),
      shading: THREE.SmoothShading
    } )

    var cube = new THREE.Mesh(geometry, material);
    cube.position.x = halfXRows + x * cubeRow;
    cube.position.y = cubeHeight / 2;
    cube.position.z = (cubeRow * -zRows / 2) + z * cubeRow;
    
    cube.height = cubeHeight;
    group.add(cube);
    
    cubes[x][z] = cube; 

  }
}



var renderer = new THREE.WebGLRenderer();
renderer.setSize(stageWidth, stageHeight);
container.appendChild( renderer.domElement );

var grid = { x: 0, z: 0};
var position = { x: 0, y: 0, z: 0};

function checkRow() {
  
  var xIndex = (position.x / cubeRow);    
  var xLoops = Math.floor(xIndex / xRows);

  var zIndex = (position.z / cubeRow);    
  var zLoops = Math.floor(zIndex / zRows);

  for (var x = 0; x < xRows; x++) {
    for (var z = 0; z < zRows; z++) {
    
      var dx, dz = 0;
      if ( x >= xIndex - xLoops * xRows ) {
        dx = xRows * (1 - xLoops);
      } else {
        dx = xRows * (0 - xLoops)
      }
      if ( z >= zIndex - zLoops * zRows ) {
        dz = zRows * (1 - zLoops);
      } else {
        dz = zRows * (0 - zLoops)
      }
      
      cubes[x][z].position.x = (x - dx) * cubeRow - halfXRows;
      cubes[x][z].position.z = (z - dz) * cubeRow - halfZRows
      
      if (scalingMode == 0) { // scaling to horizon
        
        var scale = (cubes[x][z].position.z + group.position.z) / 1500;
        if (scale < 1 ) scale = 1;
        scale = Math.pow(scale, 1.2);
        cubes[x][z].scale.y = scale;
        cubes[x][z].position.y = (cubes[x][z].height * scale) / 2;
       
      } else if (scalingMode == 1) { // scaling to horizon
        
        var dz1 = cubes[x][z].position.z + group.position.z;
        if (dz1 < 0 ) dz1 = 0; // don't make the front edge bulge up.
        var dx1 = cubes[x][z].position.x + group.position.x;
        
        var scale = Math.sqrt(dz1 * dz1 + dx1 * dx1) / 1500;
        if (scale < 1 ) scale = 1;
        scale = Math.pow(scale, 1.2);
        cubes[x][z].scale.y = scale;
        cubes[x][z].position.y = (cubes[x][z].height * scale) / 2;
       
      } else {
        
        cubes[x][z].scale.y = 1;
        cubes[x][z].position.y = cubes[x][z].height / 2;
        
      }

    }
  }

}
var camPos = new THREE.Vector3(0,0,0);
var mouse = {x:0,y:0}
var isRunning = true;
function render(t) {
  if (isRunning) requestAnimationFrame( render );
  
  position.x += (Math.sin(t * 0.001)) * 20;
  position.z += (Math.cos(t * 0.0008) + 5) * 20;
  group.position.x = -position.x;
  group.position.z = -position.z;
  
  checkRow();

  switch (cameraMode) {
    case 0 : // swaying 
      camera.position.x = Math.sin(t * 0.0003) * 1000;// + mouse.y;
      camera.position.z = -4000;
      camera.position.y = (Math.cos(t * 0.0004) + 1.3) * 3000;
      break;
    case 1 : // birds eye
      camera.position.x = 0;
      camera.position.z = -100;
      camera.position.y = 25000;
      break;
  }
  camera.lookAt(camPos);
  
  renderer.render( scene, camera );

}
render(0);
//this demo intentionally is disabled

function createOption(txt, callback) {
  //return
  var option = document.createElement('div');
  option.className = "button";
  option.innerHTML = txt;
  options.appendChild( option );
  option.addEventListener("mousedown", callback);
}


window.addEventListener("mousedown", function() {
  //isRunning = false;
})

window.addEventListener("mousemove", function(event) {
  mouse = event;
})

var options = document.createElement('div');
document.body.appendChild( options );

createOption("Scale to horizon", function() {
  scalingMode++;
  scalingMode %= scalingModes;
});

createOption("Toggle cube colours", function() {
  
  var colours = [ 0x4444ff, 0x4477ff, 0x7744ff, 0xff8080 ];
  colourMode++;
  colourMode %= colourModes;
  
  var colour;
  for (var x = 0; x < xRows; x++) {
    for (var z = 0; z < zRows; z++) {
      switch (colourMode) {
        case 0 : colour = 0x4444ff; break;
        case 1 : colour = colours[~~(Math.random() * colours.length)]; break;
        case 2 : colour = 0xffffff; break;
      }
      cubes[x][z].material.color.setHex(colour);
    }
  }
});

//adsfds + 232.djfd

createOption("Toggle camera mode", function() {
  cameraMode++;
  cameraMode %= cameraModes;
});

createOption("View original version (new window)", function() {
  window.open("https://codepen.io/raurir/full/IAtie");
});








              
            
!
999px

Console