三js相机运动

时间:2013-02-19 12:53:56

标签: three.js webgl

我没有这里的代码,所以我希望你能理解,我会在我回家时编辑并添加代码。

我已经构建了一个类移动,一旦初始化,就会向渲染的dom元素添加一个jquery事件(keyDown)。

事件检查开关盒中的键是什么,

如果键是其中一种情况,相机将相应移动。

相机确实在移动,但由于某种原因,它会有点闪烁,就像小跳跃一样。 每次摄像机移动的速度为0.05;

当我在anouter应用程序中执行此操作但通过主脚本中的javascript keydown事件(没有特殊类)时,它工作得很好..

任何想法为什么会这样做?

编辑:代码

主脚本:

<script>

    var moveMec = null;
    var loopProg = null;
    var renderer = null;
    var scene = null;
    var camera = null;
    var mesh = null;
    var earth = null;
    var sun = null;
    $(document).ready(
            function() {
                var container = document.getElementById("container");
                renderer = new THREE.WebGLRenderer({ antialias: true } );
                renderer.setSize(container.offsetWidth,container.offsetHeight);
                container.appendChild(renderer.domElement)
                scene = new THREE.Scene();
                camera = new THREE.PerspectiveCamera( 45,
                        container.offsetWidth / container.offsetHeight, 1, 4000 );

                earth = new Earth();
                sun     = new Sun({x:-10,y:0,z:20});
                scene.add(earth.getEarth);
                scene.add(sun.sunLight);
                camera.position.set( 0, 0, 3 );

                moveMec = new moveMechanics(camera,renderer.domElement)
                loopProg = new loopProgram();
                loopProg.add(function(){earth.update()});
                //loopProg.add(function(){renderer.render( scene, camera );});
                loopProg.solarLoop();
            }
    );


</script>

移动脚本:

var moveMechanics = function (camera,domElement,speed)
{
    moveMechanics.camera = camera;
    moveMechanics.speed = speed != undefined ? speed : 0.01;
    moveMechanics.domElement = domElement;

    $(document).keydown(function(event)
{
   switch(event.which)
   {
       case KeyEvent.DOM_VK_W:
           camera.position.z -= moveMechanics.speed;
           break;
       case KeyEvent.DOM_VK_S:
           camera.position.z += moveMechanics.speed;
           break;
       case KeyEvent.DOM_VK_D:
           camera.position.x += moveMechanics.speed;
           break;
       case KeyEvent.DOM_VK_A:
           camera.position.x -= moveMechanics.speed;
           break;
   }
});
}

循环代码:

function loopProgram()
{
    this.functionsToRun = new Array();
    this.solarLoop= function()
    {
        jQuery.each(loopProg.functionsToRun, function(index,value)
        {
            value ? value() : null;
        });
        requestAnimationFrame(loopProg.solarLoop);
    }

    this.add = function(func)
    {
        this.functionsToRun[this.functionsToRun.length] = func;
    }
}

0 个答案:

没有答案