0

我这里没有代码,所以我希望你能理解,我回家后编辑和添加代码。

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

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

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

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

当我在另一个应用程序中执行此操作但通过主脚本中的 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;
    }
}
4

0 回答 0