【Babylon小技巧02】第一人称移动时,限定视角抬头角度

例如展馆内,不希望用户看到头顶天花板,则需要设置抬头仰角范围

只允许前后左右移动 室内不允许抬头

scene.registerBeforeRender(function () {    
        console.log(camera.rotation.x);
        if(camera.rotation.x>0.5  ){//look down
            camera.rotation.x = 0.5;
        }else if(camera.rotation.x<-0.3){//look up
             camera.rotation.x = -0.3;
        }  
})

playground案例地址:

Babylon.js Playgroundhttps://playground.babylonjs.com/#3P6SI1

参考出自官方论坛: 禁止抬头低头

How to use wheel/scroll to control camera movement along a path - Questions - Babylon.jshttps://forum.babylonjs.com/t/how-to-use-wheel-scroll-to-control-camera-movement-along-a-path/13459

Babylon.js Playgroundhttps://www.babylonjs-playground.com/#6FHKHC%2341

 playground代码如下:

var createScene = function () {
    var scene = new BABYLON.Scene(engine);

    // Lights
    var light0 = new BABYLON.DirectionalLight("Omni", new BABYLON.Vector3(-2, -5, 2), scene);
    var light1 = new BABYLON.PointLight("Omni", new BABYLON.Vector3(2, -5, -2), scene);

    // Need a free camera for collisions
    var camera = new BABYLON.FreeCamera("FreeCamera", new BABYLON.Vector3(8, -8, -16), scene);
    camera.setTarget(new BABYLON.Vector3(0, -8, 0));
    camera.attachControl(canvas, true);
            camera.keysUp.push(87);
            camera.keysDown.push(83);
            camera.keysLeft.push(65);
            camera.keysRight.push(68);
            camera.speed=0.5;
            camera.inertia =0.8;
            camera.minZ=0.05;

    scene.registerBeforeRender(function () {    
        console.log(camera.rotation.x);
        if(camera.rotation.x>0.5  ){//look down
            camera.rotation.x = 0.5;
        }else if(camera.rotation.x<-0.3){//look up
             camera.rotation.x = -0.3;
        }
        
    })
   


    //Ground
    var ground = BABYLON.Mesh.CreatePlane("ground", 20.0, scene);
    ground.material = new BABYLON.StandardMaterial("groundMat", scene);
    ground.material.diffuseColor = new BABYLON.Color3(1, 1, 1);
    ground.material.backFaceCulling = false;
    ground.position = new BABYLON.Vector3(5, -10, -15);
    ground.rotation = new BABYLON.Vector3(Math.PI / 2, 0, 0);

    //Simple crate
    var box = BABYLON.Mesh.CreateBox("crate", 2, scene);
    box.material = new BABYLON.StandardMaterial("Mat", scene);
    box.material.diffuseTexture = new BABYLON.Texture("textures/crate.png", scene);
    box.material.diffuseTexture.hasAlpha = true;
    box.position = new BABYLON.Vector3(5, -9, -10);

    //Set gravity for the scene (G force like, on Y-axis)
    scene.gravity = new BABYLON.Vector3(0, -0.9, 0);

    // Enable Collisions
    scene.collisionsEnabled = true;

    //Then apply collisions and gravity to the active camera
    camera.checkCollisions = true;
    camera.applyGravity = true;

    //Set the ellipsoid around the camera (e.g. your player's size)
    camera.ellipsoid = new BABYLON.Vector3(1, 1, 1);

    //finally, say which mesh will be collisionable
    ground.checkCollisions = true;
    box.checkCollisions = true;

    return scene;
}

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值