Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
154
rated 0 times [  161] [ 7]  / answers: 1 / hits: 18151  / 13 Years ago, thu, november 24, 2011, 12:00:00

I've built this piece of code...(javascript)



Now we have a red sphere on the screen...the question is
how to make it spinning around?



var camera, scene, renderer,
mouseX = 0, mouseY = 0;



var geometry,material,mesh;



init();

function init() {

// Camera params :
// field of view, aspect ratio for render output, near and far clipping plane.
camera = new THREE.Camera( 75, window.innerWidth / window.innerHeight, 1, 1000 );

// move the camera backwards so we can see stuff!
// default position is 0,0,0.
camera.position.z = 300;

// the scene contains all the 3D object data
scene = new THREE.Scene();

// and the CanvasRenderer figures out what the
// stuff in the scene looks like and draws it!
renderer = new THREE.CanvasRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );

// the renderer's canvas domElement is added to the body
document.body.appendChild( renderer.domElement );

// creating shapes
makeShapes();

// add the mouse move listener
document.addEventListener( 'mousemove', onMouseMove, false );

// render 30 times a second (should also look
// at requestAnimationFrame)
setInterval(update,1000/30);

}

function update(){

updateParticles();

// and render the scene from the perspective of the camera
renderer.render( scene, camera );

}

function makeShapes() {

// create a sphere shape
geometry = new THREE.SphereGeometry( 50, 16, 16 );

// give a shape red color
material = new THREE.MeshLambertMaterial({color: 0xFF1111});

// create an object
mesh = new THREE.Mesh( geometry, material );

mesh.position.x = 0;

// add it to the scene
scene.addObject( mesh );
}



function updateParticles(){

}

// called when the mouse moves
function onMouseMove( event ) {

// store the mouseX and mouseY position
mouseX = event.clientX;
mouseY = event.clientY;
}

More From » three.js

 Answers
40

Try this :



var halfWidth = window.innerWidth/2, halfHeight = window.innerHeight/2;

function update(){
camera.position.x += ( mouseX - camera.position.x ) * 0.05;
camera.position.y += ( - mouseY - camera.position.y ) * 0.05;
camera.lookAt( scene.position );

mesh.rotation.y -= 0.005;

renderer.render( scene, camera );
}

function onMouseMove( event ) {
mouseX = event.clientX - halfWidth;
mouseY = event.clientY - halfHeight;
}

[#88923] Wednesday, November 23, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dominics

Total Points: 424
Total Questions: 99
Total Answers: 107

Location: South Korea
Member since Fri, Sep 11, 2020
4 Years ago
dominics questions
Wed, Apr 6, 22, 00:00, 2 Years ago
Thu, Jan 13, 22, 00:00, 2 Years ago
Fri, Sep 18, 20, 00:00, 4 Years ago
;