Rotating 3d object with texture issue

62 views Asked by At

I'm trying to imitate http://threejs.org/examples/canvas_geometry_cube.html via using a 3d.obj file with a texture mapping.

But i keep receiving the following error plus the rotation is totally off.

Uncaught TypeError: Cannot read property 'rotation' of undefined

Full demo is here http://wunderfauks.com/test/examples/test.html

2

There are 2 answers

1
john On

I've done it, if anyone needs to know you need to apply a matrix rotation.

Here's an example.

function onWindowResize() {

    windowHalfX = window.innerWidth / 2;
    windowHalfY = window.innerHeight / 2;

    camera.aspect = window.innerWidth / window.innerHeight;
    camera.updateProjectionMatrix();

    renderer.setSize( window.innerWidth, window.innerHeight );
}
//
function onDocumentMouseDown( event ) {
    event.preventDefault();
    document.addEventListener( 'mousemove', onDocumentMouseMove, false );
    document.addEventListener( 'mouseup', onDocumentMouseUp, false );
    document.addEventListener( 'mouseout', onDocumentMouseOut, false );
    mouseXOnMouseDown = event.clientX - windowHalfX;
    targetRotationOnMouseDown = targetRotation;
}

function onDocumentMouseMove( event ) {
    mouseX = event.clientX - windowHalfX;
    targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.02;
}

function onDocumentMouseUp( event ) {
    document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
    document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
    document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
}

function onDocumentMouseOut( event ) {
    document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
    document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
    document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
}

function onDocumentTouchStart( event ) {
    if ( event.touches.length === 1 ) {
        event.preventDefault();
        mouseXOnMouseDown = event.touches[ 0 ].pageX - windowHalfX;
        targetRotationOnMouseDown = targetRotation;
    }

}

function onDocumentTouchMove( event ) {
    if ( event.touches.length === 1 ) {
        event.preventDefault();
        mouseX = event.touches[ 0 ].pageX - windowHalfX;
        targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.05;
    }

}

function animate() {
    requestAnimationFrame( animate );
    render();
    stats.update();
}

function render() {
    //plane.rotation.y = group.rotation.y += ( targetRotation - group.rotation.y ) * 0.05;

    var rotation = new THREE.Matrix4().makeRotationY(Math.PI/2);
    var translation = new THREE.Matrix4().makeTranslation(0, 0, 0);
    rotation = group.rotation.y += (targetRotation - group.rotation.y);
    var transformation = new THREE.Matrix4().makeTranslation(0, 0, 0).makeRotationY(rotation * 0.01 * 0.3) ;
    group.applyMatrix(transformation);
    renderer.render( scene, camera );

}
1
gaitat On

Where you assign the value for group the code for loading has not executed yet and obj does not have a value. Move those 10 lines of code inside your loading function.