Pen Settings

HTML

CSS

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.

+ 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

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.

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

              
                <!-- My scene -->
	<canvas id="scene"></canvas>

<!--
  I created a collection of small examples in Three.js to understand very quickly the purpose of some functions.
  You can visit the 'website' of the entire collection of pens here : http://mamboleoo.be/learnThree/

  You can also see the collection in CodePen
  https://codepen.io/collection/DrxLEd/

  Do not hesitate to comment if there is something wrong (I'm still learning)

  Thanks !
-->
              
            
!

CSS

              
                body,html{width:100%;height:100%;padding:0;margin:0;overflow: hidden;}
              
            
!

JS

              
                
var ww = window.innerWidth,
	wh = window.innerHeight;

function init(){

	/* WEBGL RENDERER */
	renderer = new THREE.WebGLRenderer({canvas : document.getElementById('scene')});
	renderer.setClearColor(0x3F3F3F);
	renderer.setSize(ww,wh);

	/* SCENE */
	scene = new THREE.Scene();
	//Add fog in my scene
	//0x3f3f3f = the color that will be added on my object
	//The minimum distance between the camera and the object to start the fog
	//Any element beyond 950px from the camera will be completely grey
	// < 500px = no fog
	// > 500px = fog
	// > 950px = invisible
	scene.fog = new THREE.Fog( 0x3f3f3f, 500, 1000 );

	/* CAMERA */
	camera = new THREE.PerspectiveCamera(50, ww/wh, 1, 10000 );
	camera.position.set(0, 250, 700);
	camera.lookAt(new THREE.Vector3(0,0,0));
	scene.add(camera);


	/* LIGHT */
	light = new THREE.DirectionalLight(0xffffff, 1);
	light.position.set( 0, 500, 100 );
	scene.add(light);

	createBox();

	renderer.render(scene,camera);

	animate();
};

function createBox(){

	cubes = new THREE.Object3D();

	geometry = new THREE.BoxGeometry(50,50,50);
	texture = new THREE.MeshLambertMaterial({color:0x00ff00 });

	for(var i=0;i<70;i++){
		cube = new THREE.Mesh(geometry, texture);

		cube.position.x = (Math.random()-.5)*ww;
		cube.position.y = (Math.random()-.5)*wh;

		//Create a random variable for each cube so they will move "randomly"
		//Create a random speed for my cube (just for fun)
		cube.random = parseInt((Math.random()-.5)*500);
		cube.speed = Math.random()+.1;

		cubes.add(cube);
	}

	scene.add(cubes);

};

var animate = function () {

	requestAnimationFrame(animate);

	for(var i=0;i<70;i++){

		var cube = cubes.children[i];

		//Move my cube
		cube.position.z = Math.sin(cube.random/70)*500;

		//Increase the start position of the cube
		cube.random += cube.speed;

	}


	renderer.render(scene, camera);
};

init();
              
            
!
999px

Console