Three.js相机外观无法正常工作

惠特

我正在尝试使用Vector3更改相机看起来的位置,但是相机会继续观察场景的原点。我不确定为什么这行不通。我所看到的所有示例似乎都非常简单。有任何想法吗?

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script type="text/javascript" src="/three.js"></script>
    <script type="text/javascript" src="/MTLLoader.js"></script>
    <script type="text/javascript" src="/OBJLoader.js"></script>

    <script type="text/javascript" src="/stats.js"></script>
    <style>
        body {
            /* set margin to 0 and overflow to hidden, to go fullscreen */
            margin: 0;
            overflow: hidden;
        }
    </style>
</head>
<body>
<script type="text/javascript" src="/World.js"></script>
<script type="text/javascript" src="/Controls.js"></script>
<script type="text/javascript">
    // Initialize our new world object which will 
    //  setup our scene, camera, lights, and renderer.
    var world = new World(true);
    var controls = new Controls();

    // Load the map for this given level. 
    // A reference to every model loaded is saved in world.model[*name_of_file*]
    world.modelLoader('meter_grid');
    world.modelLoader('skybox1');
    world.modelLoader('test_character');

    // Render loop. Important things happens in here.
    (function render() {
        if(world.stats){
            world.stats.update();
        }
        //console.log(JSON.stringify(world.camera.position));
        world.updateRotation(controls.left, controls.right);
        requestAnimationFrame(render);
        world.renderer.render(world.scene, world.camera);
    }());
</script>
<script src="/domevents.js"></script>
</body>
</html>

下面是正在构建场景的世界“类”

// =======================================================
//  The World.
// =======================================================
var World = function(showStats){

    this.currentTime = (+new Date()) / 1000.0;
    this.stats = showStats ? this.initStats() : false;

    // Create scene object, add fog to scene: Fog( hex, near, far )
    this.scene = new THREE.Scene();

    // Create camera object: PerspectiveCamera( fov, aspect, near, far )
    this.camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 1000);
    this.camera.position.x = 0;
    this.camera.position.y = 5;
    this.camera.position.z = 5;

    this.look = new THREE.Vector3(5, 0, -5);
    this.camera.lookAt(this.look);

    this.scene.add(this.camera);

    // Create ambient light
    this.light = new THREE.AmbientLight( 0x444444 );
    this.light.intensity = 5;
    this.scene.add( this.light );

    // Create renderer and bind to dom element
    this.renderer = new THREE.WebGLRenderer();
    this.renderer.setClearColor(0xffffff);
    this.renderer.setPixelRatio(window.devicePixelRatio);
    this.renderer.setSize(window.innerWidth, window.innerHeight);
    document.body.appendChild(this.renderer.domElement);
    this.rotationSpeed = .02;
};

World.prototype.modelLoader = function(filename){
    var scope = this;
    var mtlLoader = new THREE.MTLLoader();
    mtlLoader.setBaseUrl( '/obj_assets/' );
    mtlLoader.setPath( '/obj_assets/' );
    mtlLoader.load( filename + '.mtl', function( materials ) {
        materials.preload();

        var objLoader = new THREE.OBJLoader();
        objLoader.setMaterials( materials );
        objLoader.setPath( '/obj_assets/' );
        objLoader.load( filename + '.obj', function ( object ) {

            object.name = filename;
            scope.scene.add( object );

            if(filename === 'test_character'){
                scope.moveCharacter(filename, 0, 0);
            }
        });
    }); 
};
World.prototype.render = function(){  
    if(this.stats){
        this.stats.update();
    }
    this.controls.update();
    requestAnimationFrame(this.render);
    this.renderer.render(this.scene, this.camera);
};

World.prototype.initStats = function(){
    var stats = new Stats();
    stats.setMode(0); // 0: fps, 1: ms

    // Align top left
    stats.domElement.style.position = 'absolute';
    stats.domElement.style.left = '0px';
    stats.domElement.style.top = '0px';

    document.body.appendChild(stats.domElement);
    return stats;
};

World.prototype.updateRotation = function(key_left, key_right){
    var x = this.camera.position.x,
        y = this.camera.position.y,
        z = this.camera.position.z;
    if (key_right){ 
        this.camera.position.x = x * Math.cos(this.rotationSpeed) + z * Math.sin(this.rotationSpeed);
        this.camera.position.z = z * Math.cos(this.rotationSpeed) - x * Math.sin(this.rotationSpeed);
    } else if (key_left){
        this.camera.position.x = x * Math.cos(this.rotationSpeed) - z * Math.sin(this.rotationSpeed);
        this.camera.position.z = z * Math.cos(this.rotationSpeed) + x * Math.sin(this.rotationSpeed);
    }
    this.camera.lookAt(this.scene.position);
};
// Move the character from a 2D xy grid perspective
World.prototype.moveCharacter = function(name, x, y){

    this.scene.getObjectByName(name).position.x = x;
    this.scene.getObjectByName(name).position.z = -y;    
};
惠特

求助:原来我的updateRotation方法(如您在示例中看到的)正在覆盖初始的camera.lookAt调用,并包含场景的位置。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Three.js相机外观无法正常工作

来自分类Dev

在three.js中旋转相机无法正常工作

来自分类Dev

THREE.js OrbitControls无法正常工作

来自分类Dev

为什么Three.js中的DirectionalLight无法正常工作?

来自分类Dev

three.js渲染到纹理Alpha无法正常工作

来自分类Dev

无法使Three.js相机向上

来自分类Dev

THREE.meshphongmaterial无法正常工作-黑色

来自分类Dev

THREE.SpriteCanvasMaterial无法正常工作

来自分类Dev

Three.js相机的理解

来自分类Dev

Three.js,从Collada导入的相机无法查看对象场景

来自分类Dev

除非修改了源代码,否则Three.js骨骼动画无法正常工作

来自分类Dev

THREE的3个js修订版71。MeshFaceMaterial无法正常工作

来自分类Dev

3d法线贴图在Three.js中无法正常工作

来自分类Dev

在Three.js中使用二进制加载器加载对象无法正常工作

来自分类Dev

大跃进运动+ oculus + THREE.JS:无法使“ optimizeHMD = true”正常工作(头戴式)

来自分类Dev

Three.js:在MeshFaceMaterial中使用cubeMaterials和不透明度-THREE.DoubleSide无法正常工作吗?

来自分类Dev

Three.js:在MeshFaceMaterial中使用cubeMaterials和不透明度-THREE.DoubleSide无法正常工作吗?

来自分类Dev

保持Three.js纹理的外观

来自分类Dev

Three.js:为相机添加光线

来自分类Dev

Three.js相机lookAt无需滚动?

来自分类Dev

Three.js-如何选择灯光/相机

来自分类Dev

Three.js相机旋转错误

来自分类Dev

Three.js:平滑相机围绕原点

来自分类Dev

带有THREE.js的等距相机

来自分类Dev

Three.js-相机旋转和TransformControls

来自分类Dev

相机比较无法正常工作

来自分类Dev

Three.js移动相机或两个不同的相机

来自分类Dev

Three.js-获得低多边形外观

来自分类Dev

THREE.ObjectLoader的Three.js错误

Related 相关文章

  1. 1

    Three.js相机外观无法正常工作

  2. 2

    在three.js中旋转相机无法正常工作

  3. 3

    THREE.js OrbitControls无法正常工作

  4. 4

    为什么Three.js中的DirectionalLight无法正常工作?

  5. 5

    three.js渲染到纹理Alpha无法正常工作

  6. 6

    无法使Three.js相机向上

  7. 7

    THREE.meshphongmaterial无法正常工作-黑色

  8. 8

    THREE.SpriteCanvasMaterial无法正常工作

  9. 9

    Three.js相机的理解

  10. 10

    Three.js,从Collada导入的相机无法查看对象场景

  11. 11

    除非修改了源代码,否则Three.js骨骼动画无法正常工作

  12. 12

    THREE的3个js修订版71。MeshFaceMaterial无法正常工作

  13. 13

    3d法线贴图在Three.js中无法正常工作

  14. 14

    在Three.js中使用二进制加载器加载对象无法正常工作

  15. 15

    大跃进运动+ oculus + THREE.JS:无法使“ optimizeHMD = true”正常工作(头戴式)

  16. 16

    Three.js:在MeshFaceMaterial中使用cubeMaterials和不透明度-THREE.DoubleSide无法正常工作吗?

  17. 17

    Three.js:在MeshFaceMaterial中使用cubeMaterials和不透明度-THREE.DoubleSide无法正常工作吗?

  18. 18

    保持Three.js纹理的外观

  19. 19

    Three.js:为相机添加光线

  20. 20

    Three.js相机lookAt无需滚动?

  21. 21

    Three.js-如何选择灯光/相机

  22. 22

    Three.js相机旋转错误

  23. 23

    Three.js:平滑相机围绕原点

  24. 24

    带有THREE.js的等距相机

  25. 25

    Three.js-相机旋转和TransformControls

  26. 26

    相机比较无法正常工作

  27. 27

    Three.js移动相机或两个不同的相机

  28. 28

    Three.js-获得低多边形外观

  29. 29

    THREE.ObjectLoader的Three.js错误

热门标签

归档