cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

Quick-add: + add another resource

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.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

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.

            
              <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r71/three.min.js"></script>
<script src="LotsOCubes.js"></script>
            
          
!
            
               body {
            background-color: #000000;
            margin: 0;
            overflow: hidden;
        }
        canvas { width: 100%; height: 100% }
            
          
!
            
              /**
 * Created by RebelMoogle on 15.09.2015.
 */
var camera, scene, renderer;
var geometry, material;
var meshes = [];

var materials = [];

var raycaster = new THREE.Raycaster();
var mouse = new THREE.Vector2(),
    offset = new THREE.Vector3(),
    INTERSECTED, SELECTED;

var timeClock = new THREE.Clock();


var init = function     () {

    camera = new THREE.PerspectiveCamera( 75,  window.innerWidth / window.innerHeight, 0.1, 10000 );
    camera.position.z = 1000;


    var NumberOfMeshes = 20;


    scene = new THREE.Scene();

    geometry = new THREE.BoxGeometry( 100, 100, 100 );
    material = new THREE.MeshPhongMaterial( { color: 0x008603, specular: 0x666666, emissive: 0x000000, shininess: 10, shading: THREE.SmoothShading, opacity: 0.9, transparent: false } );
    materials.push(material);
    var MoveBy = 100;
    var StartPos = - (NumberOfMeshes * MoveBy / 2);

    for(var x = 0; x < NumberOfMeshes; x++)
    {
        for(var y = 0; y < NumberOfMeshes; y++) {
            var mesh = new THREE.Mesh(geometry, material);
            mesh.position.x = StartPos + MoveBy * x;
            mesh.position.y = StartPos + MoveBy * y;
            mesh.castShadow = true;
            mesh.receiveShadow = true;
            meshes.push(mesh);
        }
    }

    meshes.forEach(function (item, index, array)
    {
        scene.add(item);
    });

    geometry = new THREE.PlaneGeometry(20000, 20000, 8, 8);
    material = new THREE.MeshPhongMaterial( { color: 0xFFECF3, specular: 0x666666, emissive: 0x000000, shininess: 10, shading: THREE.SmoothShading, opacity: 0.9, transparent: false } );
    var plane = new THREE.Mesh(geometry, material);
    plane.castShadow = true;
    plane.receiveShadow = true;
    plane.position.z = -10;

    scene.add(plane);

    scene.add(new THREE.AmbientLight(0x404040));
    var sun = new THREE.DirectionalLight(0xFFF9D4, 0.8);
    sun.position.set(300,150,500);

    sun.castShadow = true;

    sun.shadowBias = -0.00022;
    sun.shadowDarkness = 0.5;

    sun.shadowMapWidth = 2048;
    sun.shadowMapHeight = 2048;

    sun.shadowCameraRight    =  NumberOfMeshes*101;
    sun.shadowCameraLeft     = -NumberOfMeshes*101;
    sun.shadowCameraTop      =  NumberOfMeshes*101;
    sun.shadowCameraBottom   = -NumberOfMeshes*101;

    scene.add(sun);

    // add materials
    AddMaterials();

    renderer = new THREE.WebGLRenderer({antialias: true});
    renderer.setSize( window.innerWidth, window.innerHeight );
    renderer.setClearColor( 0xFFECF3 );
    renderer.setPixelRatio( window.devicePixelRatio );
    renderer.setSize( window.innerWidth, window.innerHeight );
    renderer.sortObjects = false;

    renderer.shadowMapEnabled = true;
    renderer.shadowMapType = THREE.PCFSoftShadowMap;


    document.body.appendChild( renderer.domElement );

    renderer.domElement.addEventListener( 'mousemove', onDocumentMouseMove, false );
    //renderer.domElement.addEventListener( 'mousedown', onDocumentMouseDown, false );
    window.addEventListener( 'resize', onWindowResize, false );
};

var animate = function () {

    var delta = timeClock.getDelta();
    requestAnimationFrame( animate );
    meshes.forEach(function (item, index, array)
    {

        //item.rotation.x = (item.rotation.x + index * 0.01* delta)%360;
        if(item.position.z > 0)
        {
            item.position.z -= 100.0 * delta;
        }

    });

    renderer.render( scene, camera );

};

function AddMaterials()
{
    materials.push(new THREE.MeshPhongMaterial( { color: 0x2EB82E, specular: 0x666666, emissive: 0x000000, shininess: 20, shading: THREE.SmoothShading, opacity: 0.9, transparent: false } ));
    materials.push(new THREE.MeshPhongMaterial( { color: 0x004700, specular: 0x666666, emissive: 0x000000, shininess: 20, shading: THREE.SmoothShading, opacity: 0.9, transparent: false } ));
    materials.push(new THREE.MeshPhongMaterial( { color: 0x006464, specular: 0x666666, emissive: 0x000000, shininess: 20, shading: THREE.SmoothShading, opacity: 0.9, transparent: false } ));
    materials.push(new THREE.MeshPhongMaterial( { color: 0x228A8A, specular: 0x666666, emissive: 0x000000, shininess: 20, shading: THREE.SmoothShading, opacity: 0.9, transparent: false } ));
    materials.push(new THREE.MeshPhongMaterial( { color: 0x003535, specular: 0x666666, emissive: 0x000000, shininess: 20, shading: THREE.SmoothShading, opacity: 0.9, transparent: false } ));
    materials.push(new THREE.MeshPhongMaterial( { color: 0xA74C00, specular: 0x666666, emissive: 0x000000, shininess: 20, shading: THREE.SmoothShading, opacity: 0.9, transparent: false } ));
    materials.push(new THREE.MeshPhongMaterial( { color: 0xE68739, specular: 0x666666, emissive: 0x000000, shininess: 20, shading: THREE.SmoothShading, opacity: 0.9, transparent: false } ));
    materials.push(new THREE.MeshPhongMaterial( { color: 0x592800, specular: 0x666666, emissive: 0x000000, shininess: 20, shading: THREE.SmoothShading, opacity: 0.9, transparent: false } ));
    materials.push(new THREE.MeshPhongMaterial( { color: 0xA70000, specular: 0x666666, emissive: 0x000000, shininess: 20, shading: THREE.SmoothShading, opacity: 0.9, transparent: false } ));
    materials.push(new THREE.MeshPhongMaterial( { color: 0xE63939, specular: 0x666666, emissive: 0x000000, shininess: 20, shading: THREE.SmoothShading, opacity: 0.9, transparent: false } ));
    materials.push(new THREE.MeshPhongMaterial( { color: 0x590000, specular: 0x666666, emissive: 0x000000, shininess: 20, shading: THREE.SmoothShading, opacity: 0.9, transparent: false } ));

}

function onWindowResize() {

    camera.aspect = window.innerWidth / window.innerHeight;
    camera.updateProjectionMatrix();

    renderer.setSize( window.innerWidth, window.innerHeight );

}

function onDocumentMouseMove( event )
{

    event.preventDefault();

    mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
    mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;

    //

    raycaster.setFromCamera( mouse, camera );

    var intersects = raycaster.intersectObjects( meshes );

    if ( intersects.length > 0 )
    {
        INTERSECTED = intersects[ 0 ].object;
        if(INTERSECTED.position.z <= 0)
        {
            INTERSECTED.material = materials[(materials.indexOf(INTERSECTED.material) + 1) % materials.length];
        }
        INTERSECTED.position.z += 10;
    }

}



init();
animate();

            
          
!
999px
Loading ..................

Console