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

              
                <div id="ThreeJS" style="position: absolute; left:0px; top:0px"><canvas width="760" height="75"></canvas></div>
              
            
!

CSS

              
                
              
            
!

JS

              
                
/*
	Three.js "tutorials by example"
	Author: Lee Stemkoski
	Date: July 2013 (three.js v59dev)
*/

// MAIN

// standard global variables
var container, scene, camera, renderer, controls, stats;
var keyboard = new THREEx.KeyboardState();
var clock = new THREE.Clock();
// custom global variables
var cube;

init();
animate();

// FUNCTIONS 		
function init() 
{
	// SCENE
	scene = new THREE.Scene();
	// CAMERA
	var SCREEN_WIDTH = window.innerWidth, SCREEN_HEIGHT = window.innerHeight;
	var VIEW_ANGLE = 45, ASPECT = SCREEN_WIDTH / SCREEN_HEIGHT, NEAR = 0.1, FAR = 20000;
	camera = new THREE.PerspectiveCamera( VIEW_ANGLE, ASPECT, NEAR, FAR);
	scene.add(camera);
	camera.position.set(0,150,400);
	camera.lookAt(scene.position);	
	// RENDERER
	if ( Detector.webgl )
		renderer = new THREE.WebGLRenderer( {antialias:true} );
	else
		renderer = new THREE.CanvasRenderer(); 
	renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
	container = document.getElementById( 'ThreeJS' );
	container.appendChild( renderer.domElement );
	// EVENTS
	THREEx.WindowResize(renderer, camera);
	THREEx.FullScreen.bindKey({ charCode : 'm'.charCodeAt(0) });
	// CONTROLS
	controls = new THREE.OrbitControls( camera, renderer.domElement );
	// STATS
	stats = new Stats();
	stats.domElement.style.position = 'absolute';
	stats.domElement.style.bottom = '0px';
	stats.domElement.style.zIndex = 100;
	container.appendChild( stats.domElement );
	// LIGHT
	var light = new THREE.PointLight(0xffffff);
	light.position.set(0,250,0);
	scene.add(light);
	// FLOOR
	var floorTexture = new THREE.ImageUtils.loadTexture( 'images/checkerboard.jpg' );
	floorTexture.wrapS = floorTexture.wrapT = THREE.RepeatWrapping; 
	floorTexture.repeat.set( 10, 10 );
	var floorMaterial = new THREE.MeshBasicMaterial( { map: floorTexture, side: THREE.DoubleSide } );
	var floorGeometry = new THREE.PlaneGeometry(1000, 1000, 10, 10);
	var floor = new THREE.Mesh(floorGeometry, floorMaterial);
	floor.position.y = -0.5;
	floor.rotation.x = Math.PI / 2;
	scene.add(floor);
	// SKYBOX/FOG
	var skyBoxGeometry = new THREE.CubeGeometry( 10000, 10000, 10000 );
	var skyBoxMaterial = new THREE.MeshBasicMaterial( { color: 0x9999ff, side: THREE.BackSide } );
	var skyBox = new THREE.Mesh( skyBoxGeometry, skyBoxMaterial );
	// scene.add(skyBox);
	scene.fog = new THREE.FogExp2( 0x9999ff, 0.00025 );
	
	////////////
	// CUSTOM //
	////////////
	
	//////////////////////////////////////////////////////////////////////
	
	// this material causes a mesh to use colors assigned to faces
	var cubeMaterial = new THREE.MeshBasicMaterial( 
	{ color: 0xfffff, vertexColors: THREE.FaceColors } );
	
	var cubeGeometry = new THREE.CubeGeometry( 80, 80, 80, 3, 3, 3 );
	for ( var i = 0; i < cubeGeometry.faces.length; i++ ) 
	{
		face  = cubeGeometry.faces[ i ];	
		face.color.setRGB( Math.random(), Math.random(), Math.random() );		
	}
	cube = new THREE.Mesh( cubeGeometry, cubeMaterial );
	cube.position.set(-100, 50, 0);
	scene.add(cube);
	
	//////////////////////////////////////////////////////////////////////

	// this material causes a mesh to use colors assigned to vertices
	//   different colors at face vertices create gradient effect
	var cubeMaterial = new THREE.MeshBasicMaterial( 
	{ color: 0xffffff, vertexColors: THREE.VertexColors } );
	
	var color, face, numberOfSides, vertexIndex;
	
	// faces are indexed using characters
	var faceIndices = [ 'a', 'b', 'c', 'd' ];
	
	// randomly color cube
	var cubeGeometry = new THREE.CubeGeometry( 80, 80, 80, 3, 3, 3 );
	for ( var i = 0; i < cubeGeometry.faces.length; i++ ) 
	{
		face  = cubeGeometry.faces[ i ];	
		// determine if current face is a tri or a quad
		numberOfSides = ( face instanceof THREE.Face3 ) ? 3 : 4;
		// assign color to each vertex of current face
		for( var j = 0; j < numberOfSides; j++ ) 
		{
			vertexIndex = face[ faceIndices[ j ] ];
			// initialize color variable
			color = new THREE.Color( 0xffffff );
			color.setHex( Math.random() * 0xffffff );
			face.vertexColors[ j ] = color;
		}
	}
	cube = new THREE.Mesh( cubeGeometry, cubeMaterial );
	cube.position.set(0, 50, 0);
	scene.add(cube);

	//////////////////////////////////////////////////////////////////////
	
	// RGB color cube
	var size = 80;
	var point;
	var cubeGeometry = new THREE.CubeGeometry( size, size, size, 1, 1, 1 );
	for ( var i = 0; i < cubeGeometry.faces.length; i++ ) 
	{
		face = cubeGeometry.faces[ i ];
		// determine if current face is a tri or a quad
		numberOfSides = ( face instanceof THREE.Face3 ) ? 3 : 4;
		// assign color to each vertex of current face
		for( var j = 0; j < numberOfSides; j++ ) 
		{
			vertexIndex = face[ faceIndices[ j ] ];
			// store coordinates of vertex
			point = cubeGeometry.vertices[ vertexIndex ];
			// initialize color variable
			color = new THREE.Color( 0xffffff );
			color.setRGB( 0.5 + point.x / size, 0.5 + point.y / size, 0.5 + point.z / size );
			face.vertexColors[ j ] = color;
		}
	}
	cube = new THREE.Mesh( cubeGeometry, cubeMaterial );
	cube.position.set( 100, 50, 0 );
	scene.add(cube);
  
  // Using wireframe materials to illustrate shape details.
	var darkMaterial = new THREE.MeshBasicMaterial( { color: 0xffffcc } );
	var wireframeMaterial = new THREE.MeshBasicMaterial( { color: 0x000000, wireframe: true, transparent: true } ); 
  
	var multiMaterial = [ darkMaterial, wireframeMaterial ]; 
  // icosahedron
	var shape = THREE.SceneUtils.createMultiMaterialObject( 
		new THREE.IcosahedronGeometry( 40, 0 ), // radius, subdivisions
		multiMaterial );
  var cubeGeometry = new THREE.CubeGeometry( size, size, size, 1, 1, 1 );
	for ( var i = 0; i < cubeGeometry.faces.length; i++ ) 
	{
		face = cubeGeometry.faces[ i ];
		// determine if current face is a tri or a quad
		numberOfSides = ( face instanceof THREE.Face3 ) ? 3 : 4;
		// assign color to each vertex of current face
		for( var j = 0; j < numberOfSides; j++ ) 
		{
			vertexIndex = face[ faceIndices[ j ] ];
			// store coordinates of vertex
			point = cubeGeometry.vertices[ vertexIndex ];
			// initialize color variable
			color = new THREE.Color( 0xffffff );
			color.setRGB( 0.5 + point.x / size, 0.5 + point.y / size, 0.5 + point.z / size );
			face.vertexColors[ j ] = color;
		}
	}
	cube = new THREE.Mesh( cubeGeometry, cubeMaterial );
	shape.position.set(-100, 50, 100);
	scene.add( shape );

	// octahedron
	var shape = THREE.SceneUtils.createMultiMaterialObject( 
		new THREE.OctahedronGeometry( 40, 0 ), 
		multiMaterial );
	shape.position.set(0, 50, 100);
	scene.add( shape );

	// tetrahedron
	var shape = THREE.SceneUtils.createMultiMaterialObject( 
		new THREE.TetrahedronGeometry( 40, 0 ), 
		multiMaterial );
	shape.position.set(100, 50, 100);
	scene.add( shape );

	// sphere
	var shape = THREE.SceneUtils.createMultiMaterialObject( 
		new THREE.SphereGeometry( 40, 32, 16 ), 
		multiMaterial );
	shape.position.set(200, 50, 100);
	scene.add( shape );
	
	///////////
	// ROW 2 //
	///////////
	
	// cube
	var shape = THREE.SceneUtils.createMultiMaterialObject( 
		new THREE.CubeGeometry(50, 50, 50, 2, 2, 2), 
		multiMaterial );
	shape.position.set(-200, 50, 0);
	scene.add( shape );

	// icosahedron
	var shape = THREE.SceneUtils.createMultiMaterialObject( 
		new THREE.IcosahedronGeometry( 40, 1 ), // radius, subdivisions
		multiMaterial );
	shape.position.set(-100, 50, 0);
	scene.add( shape );

	// octahedron
	var shape = THREE.SceneUtils.createMultiMaterialObject( 
		new THREE.OctahedronGeometry( 40, 1 ), 
		multiMaterial );
	shape.position.set(0, 50, 0);
	scene.add( shape );

	// tetrahedron
	var shape = THREE.SceneUtils.createMultiMaterialObject( 
		new THREE.TetrahedronGeometry( 40, 1 ), 
		multiMaterial );
	shape.position.set(100, 50, 0);
	scene.add( shape );

	// dome
	var shape = THREE.SceneUtils.createMultiMaterialObject( 
		new THREE.SphereGeometry( 40, 32, 16, 0, 2 * Math.PI, 0, Math.PI / 2 ), 
		multiMaterial );
	// should set material to doubleSided = true so that the 
	//   interior view does not appear transparent.
	shape.position.set(200, 50, 0);
	scene.add( shape );
	
	///////////
	// ROW 3 //
	///////////
	
	// cylinder
	var shape = THREE.SceneUtils.createMultiMaterialObject( 
		// radiusAtTop, radiusAtBottom, height, segmentsAroundRadius, segmentsAlongHeight,
		new THREE.CylinderGeometry( 30, 30, 80, 20, 4 ), 
		multiMaterial );
	shape.position.set(-200, 50, -100);
	scene.add( shape );
	
	// cone
	var shape = THREE.SceneUtils.createMultiMaterialObject( 
		// radiusAtTop, radiusAtBottom, height, segmentsAroundRadius, segmentsAlongHeight,
		new THREE.CylinderGeometry( 0, 30, 100, 20, 4 ), 
		multiMaterial );
	shape.position.set(-100, 50, -100);
	scene.add( shape );
	
	// pyramid
	var shape = THREE.SceneUtils.createMultiMaterialObject( 
		// radiusAtTop, radiusAtBottom, height, segmentsAroundRadius, segmentsAlongHeight,
		new THREE.CylinderGeometry( 0, 30, 100, 4, 4 ), 
		multiMaterial );
	shape.position.set(0, 50, -100);
	scene.add( shape );

	// torus
	var shape = THREE.SceneUtils.createMultiMaterialObject( 
	    // radius of entire torus, diameter of tube (less than total radius), 
		// segments around radius, segments around torus ("sides")
		new THREE.TorusGeometry( 25, 10, 8, 4 ),
		multiMaterial );
	shape.position.set(100, 50, -100);
	scene.add( shape );

	// torus knot
	var shape = THREE.SceneUtils.createMultiMaterialObject( 
		// total knot radius, tube radius, number cylinder segments, sides per cyl. segment,
		//  p-loops around torus, q-loops around torus
		new THREE.TorusKnotGeometry( 30, 8, 60, 10, 2, 3 ), 
		multiMaterial );
	shape.position.set(200, 50, -100);
	scene.add( shape );

	///////////
	// ROW 4 //
	///////////
	
	// prism
	var shape = THREE.SceneUtils.createMultiMaterialObject( 
		// radiusAtTop, radiusAtBottom, height, segmentsAroundRadius, segmentsAlongHeight,
		new THREE.CylinderGeometry( 30, 30, 80, 6, 4 ), 
		multiMaterial );
	shape.position.set(-200, 50, -200);
	scene.add( shape );
	
	// cone - truncated
	var shape = THREE.SceneUtils.createMultiMaterialObject( 
		// radiusAtTop, radiusAtBottom, height, segmentsAroundRadius, segmentsAlongHeight,
		new THREE.CylinderGeometry( 10, 30, 100, 20, 4 ), 
		multiMaterial );
	shape.position.set(-100, 50, -200);
	scene.add( shape );
	
	// pyramid - truncated
	var shape = THREE.SceneUtils.createMultiMaterialObject( 
		// radiusAtTop, radiusAtBottom, height, segmentsAroundRadius, segmentsAlongHeight,
		new THREE.CylinderGeometry( 15, 30, 100, 6, 4 ), 
		multiMaterial );
	shape.position.set(0, 50, -200);
	scene.add( shape );

	// torus
	var shape = THREE.SceneUtils.createMultiMaterialObject( 
	    // radius of entire torus, diameter of tube (less than total radius), 
		// sides per cylinder segment, cylinders around torus ("sides")
		new THREE.TorusGeometry( 30, 20, 16, 40 ),
		multiMaterial );
	shape.position.set(100, 50, -200);
	scene.add( shape );

	// torus knot
	var shape = THREE.SceneUtils.createMultiMaterialObject( 
		// total knot radius, tube radius, number cylinder segments, sides per cyl. segment,
		//  p-loops around torus, q-loops around torus
		new THREE.TorusKnotGeometry( 30, 6, 160, 10, 3, 7 ), 
		multiMaterial );
	shape.position.set(200, 50, -200);
	scene.add( shape );
	
	
}

function animate() 
{
    requestAnimationFrame( animate );
	render();		
	update();
}

function update()
{
	if ( keyboard.pressed("z") ) 
	{ 
		// do something
	}
	
	controls.update();
	stats.update();
}

function render() 
{
	renderer.render( scene, camera );
}


              
            
!
999px

Console