Gabriel Rosario
Gabriel Rosario

Reputation: 99

Three.js add more than one geometry

Hello everyone I'm starting with Three.js and I don't know why my second geometry don't show. Please help me if I miss something. I added the first geometry with all the materials and added to the scene, but my second geometry don't want to appear. Need a little help here, thanks a lot!!!

// Scene and Camera
var scene = new THREE.Scene(); 
var camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000);

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

// Firt geometry with material and added to the scene.
var geometry = new THREE.BoxGeometry( 1, 1, 1 );
var material = new THREE.MeshBasicMaterial( { color: 0x00ff00 } );
var cube = new THREE.Mesh(geometry, material);
scene.add( cube );

// Seconde geometry with material and added to the scene as well.
var mysphere = new THREE.SphereGeometry(5, 32, 32);
var color = new THREE.Color('#ee7800');
var hex = color.getHex;
var sphereMaterial = new THREE.MeshLambertMaterial( { color: hex } );
var sphere = new THREE.Mesh(mysphere, sphereMaterial);
sphere.position.y = 10;
scene.add( sphere );

// Better camera position
camera.position.z = 5;


// Render all and move the cube
function render() {
  requestAnimationFrame( render );

  cube.rotation.x += 0.03;
  cube.rotation.y += 0.01;

  renderer.render( scene, camera);
};

render();

Upvotes: 0

Views: 684

Answers (2)

msun
msun

Reputation: 841

You have the sphere positioned at y=10, but the camera is positioned at y=0;z=5, which means it's point-of-view is from slightly underneath the sphere. i.e., the sphere is offscreen. Try setting camera.position.z=30 to get the sphere into the field-of-view.

Upvotes: 1

prisoner849
prisoner849

Reputation: 17596

Just to clarify. Any error messages in your console log?

var hex = color.getHex();

as getHex() is a method.

Also, why not

var sphereMaterial = new THREE.MeshLambertMaterial( { color: 0xee7800} );

And constuctor of Color looks strange. It should be like this:

var color = new THREE.Color( 0xee7800 );

Upvotes: 1

Related Questions