A Pen by coolok

Thumbnail
This awesome code was written by Coolok, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright Coolok ©

Technologies

  • HTML
  • JavaScript
<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  <title>A Pen by  coolok</title>
  
  
  
  
  
</head>

<body>

  <div id="ThreeJS" style="position: absolute; left:0px; top:0px"><canvas width="760" height="75"></canvas></div>
  <script src='https://stemkoski.github.io/Three.js/js/Three.js'></script>
<script src='https://stemkoski.github.io/Three.js/js/Detector.js'></script>
<script src='https://stemkoski.github.io/Three.js/js/Stats.js'></script>
<script src='https://stemkoski.github.io/Three.js/js/OrbitControls.js'></script>
<script src='https://stemkoski.github.io/Three.js/js/THREEx.KeyboardState.js'></script>
<script src='https://stemkoski.github.io/Three.js/js/THREEx.FullScreen.js'></script>
<script src='https://stemkoski.github.io/Three.js/js/THREEx.WindowResize.js'></script>
<script src='https://stemkoski.github.io/Three.js/js/jquery-1.9.1.js'></script>
<script src='https://stemkoski.github.io/Three.js/js/jquery-ui.js'></script>
<script src='https://stemkoski.github.io/Three.js/js/info.js'></script>

  

    <script  src="js/index.js"></script>




</body>

</html>

/*Downloaded from https://www.codeseek.co/Coolok/a-pen-by-coolok-OyxmOZ */

/*
	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 );
}

Comments