Three JS Textured Cube
by GyuSeonShin
CSS
body
{
background-image:url('http://4.bp.blogspot.com/-EatAgzrwxrE/T4fb7BZEj7I/AAAAAAAADX8/ha3rPKTJxYc/s640/sloth-green-woods-forest.png');
background-color:#cccccc;
}
JavaScript
// this function is executed on each animation frame
function animate() {
// update
// render
renderer.render(scene, camera);
// request new frame
requestAnimationFrame(function () {
animate();
});
}
// renderer
var renderer = new THREE.CanvasRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// scene
var scene = new THREE.Scene();
// camera
var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 9000);
camera.position.set(0, 50, 800);
camera.lookAt(scene.position);
// material
var material = new THREE.MeshLambertMaterial({
map: THREE.ImageUtils.loadTexture('http://i223.photobucket.com/albums/dd176/dyslixec/wood_crate_base_made_from_tutorial.jpg'),
overdraw: true
});
// cube
var cube = new THREE.Mesh(new THREE.CubeGeometry(80, 80, 80, 4, 4, 4), material);
cube.overdraw = true;
cube.position.set(0,0,500);
scene.add(cube);
// grass
var texture, material2, grass;
texture = THREE.ImageUtils.loadTexture( "https://www.planwallpaper.com/static/images/violet-vector-leaves-circles-backgrounds-for-powerpoint_PdfkI4q.jpg" );
// assuming you want the texture to repeat in both directions:
texture.wrapS = THREE.RepeatWrapping;
texture.wrapT = THREE.RepeatWrapping;
// how many times to repeat in each direction; the default is (1,1),
texture.repeat.set( 8, 8 );
material2 = new THREE.MeshLambertMaterial({ map : texture });
grass = new THREE.Mesh(new THREE.PlaneGeometry(1024, 1024), material2);
grass.side = THREE.DoubleSide;
grass.rotation.x-=Math.PI/2;
grass.position.y-=100;
scene.add(grass);
// add subtle ambient lighting
var ambientLight = new THREE.AmbientLight(0xbbbbbb);
scene.add(ambientLight);
// directional lighting
var directionalLight = new THREE.DirectionalLight(0xffffff);
directionalLight.position.set(1, 1, 1).normalize();
scene.add(directionalLight);
// start animation
animate();