JSFiddle - React, Tailwind, and code Playground
HTML
<script src="http://threejs.org/build/three.js"></script>
<script src="http://threejs.org/examples/js/controls/TrackballControls.js"></script>
CSS
body{
margin:0;overflow:hidden;
}
JavaScript
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(35,innerWidth/innerHeight,.1,300);
camera.position.set( 25, 25, 125 );
camera.lookAt( scene.position );
var renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.setClearColor( 0x00bfff, 1 );
document.body.appendChild( renderer.domElement );
var controls=new THREE.TrackballControls(camera,renderer.domElement);
controls.rotateSpeed=5;
var light1 = new THREE.PointLight( 0xFFFFFF, .5 );
light1.position.set( 10, 10, 10 );
scene.add( light1 );
var light2 = new THREE.PointLight( 0xFFFFFF, 1 );
light2.position.set( -10, -10, 10 );
scene.add( light2 );
var ground = new BoxMesh( 10, 0.1, 10, 0x6C4319, 1 );
positionThenAdd( ground, [[ 0, 0 ]] );
animate();
function animate(){
requestAnimationFrame(animate);
renderer.render(scene,camera);
controls.update();
}
function BoxMesh( width, height, depth, hexColor, amount ) {
this.width = width;
this.height = height;
this.depth = depth;
this.hexColor = hexColor;
this.amount = amount; // amount of box meshes to be made
var boxSize = new THREE.BoxGeometry( width, height, depth );
var boxMaterial = new THREE.MeshLambertMaterial( { color: hexColor } );
var all = [];
for(var n = 1; n <= amount; n++)all.push(new THREE.Mesh(boxSize,boxMaterial));
return all;
}
function positionThenAdd( varMesh, posArrXByZ ) {
for(var mesh = 0; mesh < varMesh.length; mesh++) {
varMesh[mesh].position.set(
varMesh[mesh].geometry.parameters.width/2 + posArrXByZ[mesh][0],
varMesh[mesh].geometry.parameters.height/2 ,//+ ground.height,
varMesh[mesh].geometry.parameters.depth/2 + posArrXByZ[mesh][1]
);
scene.add( varMesh[mesh] );
}
};