将移动模型旋转为在平面几何体上平行

时间:2015-07-29 13:00:57

标签: rotation three.js

我正在尝试在代表山丘的平面几何体上旋转模型。我使用以下代码。我的问题是,尽管模型在开始动画时看到正确的旋转并且与面部平行,但是当它靠近并克服点(0,0,0)时它正在移动,它很奇怪地旋转。也许我将模型设置为向量(0,0,1)的问题(您可以将粘贴复制到编辑器并在浏览器上查看示例):

        <!DOCTYPE html>
        <html lang="en">
            <head>
                <title>three.js webgl - trackball controls</title>
                <meta charset="utf-8">
                <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
                <style>
                    body {
                        color: #000;
                        font-family:Monospace;
                        font-size:13px;
                        text-align:center;
                        font-weight: bold;

                        background-color: #fff;
                        margin: 0px;
                        overflow: hidden;
                    }

                    #info {
                        color:#000;
                        position: absolute;
                        top: 0px; width: 100%;
                        padding: 5px;

                    }

                    a {
                        color: red;
                    }
                </style>
        </head>

            <body>
                <div id="container"></div>
                <div id="info">
                    <a href="http://threejs.org" target="_blank">three.js</a> - trackball controls example</br>
                    MOVE mouse &amp; press LEFT/A: rotate, MIDDLE/S: zoom, RIGHT/D: pan
                </div>

                <script src="http://threejs.org/build/three.min.js"></script>

                <script src="http://threejs.org/examples/js/controls/TrackballControls.js"></script>

                <script src="http://threejs.org/examples/js/Detector.js"></script>
                <script src="http://threejs.org/examples/js/libs/stats.min.js"></script>

                <script>

                    if ( ! Detector.webgl ) Detector.addGetWebGLMessage();

                    var container, stats;

                    var camera, controls, scene, renderer,mesh,animation,morph;

                    var cross;

                    init();
                    animate();

                    function init() {

                        camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 1000 );
                        camera.position.z = 500;

                        controls = new THREE.TrackballControls( camera );

                        controls.rotateSpeed = 1.0;
                        controls.zoomSpeed = 1.2;
                        controls.panSpeed = 0.8;

                        controls.noZoom = false;
                        controls.noPan = false;

                        controls.staticMoving = true;
                        controls.dynamicDampingFactor = 0.3;

                        controls.keys = [ 65, 83, 68 ];

                        controls.addEventListener( 'change', render );

                        // world

                        scene = new THREE.Scene();
                        var geometry = new THREE.PlaneBufferGeometry(100,100,2,2);
                        var material = new THREE.MeshPhongMaterial({color: 0xff0000,side:THREE.DoubleSide,
                                        polygonOffset: true,
                                        polygonOffsetFactor: 1, // positive value pushes polygon further away
                                        polygonOffsetUnits: 1});
                        var vertices = geometry.attributes.position.array;  
                        vertices[ 14 ] =10;
                        mesh = new THREE.Mesh(geometry,material);
                        scene.add(mesh);
                        // wireframe
                        var helper1 = new THREE.WireframeHelper( mesh, 0x000000 ); // or THREE.WireframeHelper
                        helper1.material.linewidth = 2;
                        scene.add( helper1 );

                        // lights

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

                        light = new THREE.DirectionalLight( 0x002288 );
                        light.position.set( -1, -1, -1 );
                        scene.add( light );

                        light = new THREE.AmbientLight( 0x222222 );
                        scene.add( light );


                        // renderer

                        renderer = new THREE.WebGLRenderer( { antialias: false } );
                        renderer.setClearColor( 0xffffff );
                        renderer.setPixelRatio( window.devicePixelRatio );
                        renderer.setSize( window.innerWidth, window.innerHeight );

                        container = document.getElementById( 'container' );
                        container.appendChild( renderer.domElement );
                        container.addEventListener( 'mousemove', onMouseMove, false );
                        stats = new Stats();
                        stats.domElement.style.position = 'absolute';
                        stats.domElement.style.top = '0px';
                        stats.domElement.style.zIndex = 100;
                        container.appendChild( stats.domElement );
                        container.addEventListener( 'mousemove', onMouseMove, false );
                        //

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

                        render();

                        }

                        var material = new THREE.LineBasicMaterial({
                            color: 0x0000ff
                        });
                        var geometry = new THREE.Geometry();
                        geometry.vertices.push(
                            new THREE.Vector3( 0, 0, -20 ),
                            new THREE.Vector3( 0, 0, 20 )
                        );

                        var helper = new THREE.Line( geometry, material );
                        scene.add( helper );

                        //////////

                        var loader = new THREE.JSONLoader( true );
                        loader.load( "http://threejs.org/examples/models/animated/horse.js", function( geometry ) {

                            morph = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: 0x606060, morphTargets: true } ) );
                            morph.scale.set( 0.02, 0.02, 0.02 );
                            //morph.rotation.set(Math.PI/2,Math.PI/2+Math.PI/4,0);//rotate to look at the direction moving.
                            morph.position.set(-50,-50,0);
                            scene.add( morph );

                            animation = new THREE.MorphAnimation( morph );
                            animation.play();

                        } );

                    /////////
                    //raycaster function 
                    var raycaster = new THREE.Raycaster();
                    var mouse = new THREE.Vector2();

                    function onMouseMove( event ) {

                        mouse.x = ( event.clientX / renderer.domElement.width ) * 2 - 1;
                        mouse.y = - ( event.clientY / renderer.domElement.height ) * 2 + 1;

                        raycaster.setFromCamera( mouse, camera );

                        // See if the ray from the camera into the world hits one of our meshes
                        var intersects = raycaster.intersectObject( mesh );

                        // Toggle rotation bool for meshes that we clicked
                        if ( intersects.length > 0 ) {



                            helper.position.set( 0, 0, 0 );
                            helper.lookAt( intersects[ 0 ].face.normal );

                            document.body.style.cursor = "crosshair";
                            helper.position.copy( intersects[ 0 ].point );
                            render();
                        }
                        else{document.body.style.cursor = "auto";}

                    }

                    function onWindowResize() {

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

                        renderer.setSize( window.innerWidth, window.innerHeight );

                        controls.handleResize();

                        render();

                    }

                    function animate() {

                        requestAnimationFrame( animate );
                        render();
                        controls.update();
                    }
                    var prevTime = Date.now();
                    function render() {



                        if ( animation ) {

                            var time = Date.now();

                            animation.update( time - prevTime );

                            prevTime = time;

                        }
                        if(morph){
                        if(morph.position.x>50){morph.position.x=-50;morph.position.y = -50;}
                        morph.position.x+=0.3;
                        morph.position.y+=0.3;
                        var help = helper.clone();
                        help.position.set(morph.position.x,morph.position.y,-10);


                        var ray= new THREE.Raycaster();
                        ray.set(help.position,new THREE.Vector3(0,0,1).normalize());
                        var intersect = ray.intersectObject( mesh );

                        // Toggle rotation bool for meshes that we clicked
                        if ( intersect.length > 0 ) {
                            morph.up.set(0,0,1);
                            morph.position.copy( intersect[ 0 ].point );
                            morph.lookAt( intersect[ 0 ].face.normal );
                        }
                        }
                        renderer.render( scene, camera );
                        stats.update();
                    }




                </script>

            </body>
        </html>

保持模型旋转平行于其所在面的任何想法?

this one无法回答的类似问题。

1 个答案:

答案 0 :(得分:1)

我已经使用了上面的代码并且在玩了一段时间之后能够获得你想要的效果,但也许不是回答你的问题的方式......这里是我发现的无论如何......另一个说明我认为我正在使用首次发布的代码.. 因此,我已经处理了3d代码(2001-2002时间框架)的很长一段时间,所以我的知识可能既生锈又过时的新趋势。加上我是这个框架工作的新手。

            <!DOCTYPE html>
    <html lang="en">
        <head>
            <title>three.js webgl - trackball controls</title>
            <meta charset="utf-8">
            <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
            <style>
                body {
                    color: #000;
                    font-family:Monospace;
                    font-size:13px;
                    text-align:center;
                    font-weight: bold;

                    background-color: #fff;
                    margin: 0px;
                    overflow: hidden;
                }

                #info {
                    color:#000;
                    position: absolute;
                    top: 0px; width: 100%;
                    padding: 5px;

                }

                a {
                    color: red;
                }
            </style>
    </head>

        <body>
            <div id="container"></div>
            <div id="info">
                <a href="http://threejs.org" target="_blank">three.js</a> - trackball controls example</br>
                MOVE mouse &amp; press LEFT/A: rotate, MIDDLE/S: zoom, RIGHT/D: pan
            </div>

            <script src="http://threejs.org/build/three.min.js"></script>

            <script src="http://threejs.org/examples/js/controls/TrackballControls.js"></script>

            <script src="http://threejs.org/examples/js/Detector.js"></script>
            <script src="http://threejs.org/examples/js/libs/stats.min.js"></script>

            <script>

                if ( ! Detector.webgl ) Detector.addGetWebGLMessage();

                var container, stats;

                var camera, controls, scene, renderer,mesh,animation,morph;

                var cross;

                init();
                animate();

                function init() {

                    camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 1000 );
                    camera.position.z = 500;

                    controls = new THREE.TrackballControls( camera );

                    controls.rotateSpeed = 1.0;
                    controls.zoomSpeed = 1.2;
                    controls.panSpeed = 0.8;

                    controls.noZoom = false;
                    controls.noPan = false;

                    controls.staticMoving = true;
                    controls.dynamicDampingFactor = 0.3;

                    controls.keys = [ 65, 83, 68 ];

                    controls.addEventListener( 'change', render );

                    // world

                    scene = new THREE.Scene();
                    var geometry = new THREE.PlaneBufferGeometry(100,100,2,2);
                    var material = new THREE.MeshPhongMaterial({color: 0xff0000,side:THREE.DoubleSide,
                                    polygonOffset: true,
                                    polygonOffsetFactor: 1, // positive value pushes polygon further away
                                    polygonOffsetUnits: 1});
                    var vertices = geometry.attributes.position.array;  
                    vertices[ 14 ] =10;

                    mesh = new THREE.Mesh(geometry,material);
                    scene.add(mesh);
                    // wireframe
                    var helper1 = new THREE.WireframeHelper( mesh, 0x000000 ); // or THREE.WireframeHelper
                    helper1.material.linewidth = 2;
                    scene.add( helper1 );

                    // lights

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

                    light = new THREE.DirectionalLight( 0x002288 );
                    light.position.set( -1, -1, -1 );
                    scene.add( light );

                    light = new THREE.AmbientLight( 0x222222 );
                    scene.add( light );


                    // renderer

                    renderer = new THREE.WebGLRenderer( { antialias: false } );
                    renderer.setClearColor( 0xffffff );
                    renderer.setPixelRatio( window.devicePixelRatio );
                    renderer.setSize( window.innerWidth, window.innerHeight );

                    container = document.getElementById( 'container' );
                    container.appendChild( renderer.domElement );
                    container.addEventListener( 'mousemove', onMouseMove, false );
                    stats = new Stats();
                    stats.domElement.style.position = 'absolute';
                    stats.domElement.style.top = '0px';
                    stats.domElement.style.zIndex = 100;
                    container.appendChild( stats.domElement );
                    container.addEventListener( 'mousemove', onMouseMove, false );
                    //

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

                    render();

                    }

                    var material = new THREE.LineBasicMaterial({
                        color: 0x0000ff
                    });
                    var geometry = new THREE.Geometry();
                    geometry.vertices.push(
                        new THREE.Vector3( 0, 0, -20 ),
                        new THREE.Vector3( 0, 0, 20 )
                    );

                    var helper = new THREE.Line( geometry, material );
                    scene.add( helper );

                    //////////

                    var loader = new THREE.JSONLoader( true );
                    loader.load( "http://threejs.org/examples/models/animated/horse.js", function( geometry ) {

                        morph = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: 0x606060, morphTargets: true } ) );
                        morph.scale.set( 0.02, 0.02, 0.02 );
                        //morph.rotation.set(Math.PI/2,Math.PI/2+Math.PI/4,0);//rotate to look at the direction moving.
                        morph.position.set(-50,-50,0);
                        scene.add( morph );

                        animation = new THREE.MorphAnimation( morph );
                        animation.play();

                    } );

                /////////
                //raycaster function 
                var raycaster = new THREE.Raycaster();
                var mouse = new THREE.Vector2();

                function onMouseMove( event ) {

                    mouse.x = ( event.clientX / renderer.domElement.width ) * 2 - 1;
                    mouse.y = - ( event.clientY / renderer.domElement.height ) * 2 + 1;

                    raycaster.setFromCamera( mouse, camera );

                    // See if the ray from the camera into the world hits one of our meshes
                    var intersects = raycaster.intersectObject( mesh );

                    // Toggle rotation bool for meshes that we clicked
                    if ( intersects.length > 0 ) {



                        helper.position.set( 0, 0, 0 );
                        helper.lookAt( intersects[ 0 ].face.normal );

                        document.body.style.cursor = "crosshair";
                        helper.position.copy( intersects[ 0 ].point );
                        render();
                    }
                    else{document.body.style.cursor = "auto";}

                }

                function onWindowResize() {

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

                    renderer.setSize( window.innerWidth, window.innerHeight );

                    controls.handleResize();

                    render();

                }

                function animate() {

                    requestAnimationFrame( animate );
                    render();
                    controls.update();
                }
                var prevTime = Date.now();
                function render() {
var DelX,DelY,DelZ,LastZ;
DelX=0.3;DelY=0.3;

                    if ( animation ) {

                        var time = Date.now();

                        animation.update( time - prevTime );

                        prevTime = time;

                    }
                    if(morph){
LastZ=morph.position.z;
                    if(morph.position.x>50){morph.position.x=-50;morph.position.y = -50.1;}
                    morph.position.x+=DelX;
                    morph.position.y+=DelY;
                    var help = helper.clone();
                    help.position.set(morph.position.x,morph.position.y,-10);


                    var ray= new THREE.Raycaster();
                    ray.set(help.position,new THREE.Vector3(0,0,1).normalize());
                    var intersect = ray.intersectObject( mesh );

                    // Toggle rotation bool for meshes that we clicked
                    if ( intersect.length > 0 ) {
                        morph.up.set(0,0,1);
                        morph.position.copy( intersect[ 0 ].point );
DelZ=morph.position.z-LastZ;
var PointToLookat = new THREE.Vector3(morph.position.x+DelX,morph.position.y+DelY,morph.position.z+DelZ);
morph.lookAt( PointToLookat );
                        // old morph.lookAt( intersect[ 0 ].face.normal );
                    }
                    }
                    renderer.render( scene, camera );
                    stats.update();
                }




            </script>

        </body>
    </html>

所以基本上你的LookAt首先在三维空间的某个点“指向”马我认为它是一个矢量方向,也许你也是假设,我至少是错的,所以你必须把你的观点对象当前位置的“前面”。我添加了一些delta变量并跟踪lastZ位置值(因为我写这个我意识到我可以使用一个向量)所以我找到了对象将在的“下一个点”并将其用作PointToLookat。 / p>