旋转3d对象与纹理问题

时间:2015-06-22 10:37:25

标签: object 3d rotation three.js loader

我想模仿http://threejs.org/examples/canvas_geometry_cube.html 通过使用带有纹理映射的3d.obj文件。

但是我一直收到以下错误加上轮换完全关闭。

Uncaught TypeError:无法读取未定义

的属性'rotation'

完整演示在这里http://wunderfauks.com/test/examples/test.html

2 个答案:

答案 0 :(得分:0)

如果为group分配值,则尚未执行加载代码,obj没有值。在加载函数中移动这10行代码。

答案 1 :(得分:0)

我已经完成了,如果有人需要知道你需要应用矩阵轮换。

以下是一个例子。

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 );

}