PLAYCUBE
PLAYCUBE

Reputation: 875

Three.js won't render in div

I am using three.js to render a cube. The cube shows up, but it won't show in the div and the style of it.

HTML:

<div id="render">

<script>
var camera;
var scene;
var renderer;
var mesh;
var geometry;
var material1;
var material2;
var material3;
var material4;
var material5;
var material6;
var materials;
var meshFaceMaterial;



init();
animate();

function inicio(){
                    Render.setSize(Ancho,Alto);

                    document.getElementById('render').appendChild(Render.domElement);

                    Camara.position.z=1500;

                    Escenario.add(Camara);

                    crear_plano();

                    controls=new THREE.OrbitControls(Camara,Render.domElement);
            }

function init() {


    scene = new THREE.Scene();
    camera = new THREE.PerspectiveCamera( 30, window.innerWidth / window.innerHeight, 1, 1000);

    var light = new THREE.DirectionalLight( 0xffffff );
    light.position.set( 0, 1, 1 ).normalize();
    scene.add(light);

    geometry = new THREE.CubeGeometry( 10, 10, 10);
    material1 = new THREE.MeshPhongMaterial( { map: THREE.ImageUtils.loadTexture('http://vignette1.wikia.nocookie.net/scifiminibuilders/images/8/88/Your_Picture_Here.png/revision/latest?cb=20130507015051') } );
    material2 = new THREE.MeshPhongMaterial( { map: THREE.ImageUtils.loadTexture('http://vignette1.wikia.nocookie.net/scifiminibuilders/images/8/88/Your_Picture_Here.png/revision/latest?cb=20130507015051') } );
    material3 = new THREE.MeshPhongMaterial( { map: THREE.ImageUtils.loadTexture('http://vignette1.wikia.nocookie.net/scifiminibuilders/images/8/88/Your_Picture_Here.png/revision/latest?cb=20130507015051') } );
    material4 = new THREE.MeshPhongMaterial( { map: THREE.ImageUtils.loadTexture('http://vignette1.wikia.nocookie.net/scifiminibuilders/images/8/88/Your_Picture_Here.png/revision/latest?cb=20130507015051') } );
    material5 = new THREE.MeshPhongMaterial( { map: THREE.ImageUtils.loadTexture('http://vignette1.wikia.nocookie.net/scifiminibuilders/images/8/88/Your_Picture_Here.png/revision/latest?cb=20130507015051') } );
    material6 = new THREE.MeshPhongMaterial( { map: THREE.ImageUtils.loadTexture('http://vignette1.wikia.nocookie.net/scifiminibuilders/images/8/88/Your_Picture_Here.png/revision/latest?cb=20130507015051') } );

    materials = [material1, material2, material3, material4, material5, material6];

    meshFaceMaterial = new THREE.MeshFaceMaterial( materials );

    mesh = new THREE.Mesh(geometry, meshFaceMaterial );
    mesh.position.z = -50;
    scene.add( mesh );

    renderer = new THREE.WebGLRenderer();
    renderer.setSize( window.innerWidth, window.innerHeight );
    document.body.appendChild( renderer.domElement );

    window.addEventListener( 'resize', onWindowResize, false );

    render();
}

function animate() {
    mesh.rotation.x += .015;
    mesh.rotation.y += .015;
    mesh.rotation.y += .015;

    render();
    requestAnimationFrame( animate );
}

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

function onWindowResize() {
    camera.aspect = window.innerWidth / window.innerHeight;
    camera.updateProjectionMatrix();
    renderer.setSize( window.innerWidth, window.innerHeight );
    render();
}
</script>
</div>

CSS:

#render {
    width: 200px;
    height: 200px;
    border: 1px solid black;
    padding: 0px;
}

I can't see why it doesn't work. Besides, I am trying to get the script as an external file. document.addEventListener('DOMContentLoaded', function() { function _(variable) { console.log(variable); } Doesn't work unfortunately. Not proficient here...maybe you know more.

Upvotes: 0

Views: 296

Answers (1)

Andy Ray
Andy Ray

Reputation: 32076

You're appending the renderer to the page body, not the div:

document.body.appendChild(...)

Append it to the div instead:

document.getElementById('render').appendChild( renderer.domElement );

It's important to understand the code in your program. Consider walking through each line instead of copying and pasting.

Upvotes: 3

Related Questions