-->

Scene rendering strangely in babylonjs

2019-09-14 12:23发布

问题:

So, I've loaded my scene I made in Blender into Babylonjs, and it's producing some interesting effects. Basically, I am trying to apply gravity and such to the scene, move the player to it's proper location, and make the entire scene lit and visible, but none of that is working. This is the script:

var BABYLON;
var canvas = document.getElementById('gamecanvas');
var engine = new BABYLON.Engine(canvas, true);
var player_height = 2;
var player_speed = 1;
var player_inertia = 0.9;

function INIT_GAME(){
    BABYLON.SceneLoader.Load('Scenes/', 'zombie_map.babylon', engine, function(newScene){
        var scene = newScene; 
        var light = new BABYLON.PointLight('light', new BABYLON.Vector3(0,0,10), scene);
        var player = new BABYLON.FreeCamera('player', new BABYLON.Vector3(1,1,1), scene);
        scene.activeCamera = player;
        scene.activeCamera.attachControl(canvas, true);
        scene.enablePhysics();
        scene.setGravity(new BABYLON.Vector3(0, -10, 0));
        player.ellipsoid = new BABYLON.Vector3(1, player_height, 1);
        player.checkCollisions = true;
        player.applyGravity = true;
        player.keysUp = [87];
        player.keysDown = [83];
        player.keysLeft = [65];
        player.keysRight = [68];
        player.inertia = player_inertia;
        player.speed = player_speed;
        newScene.executeWhenReady(function(){
            engine.runRenderLoop(function(){
                newScene.render();
            });
        });
    });

canvas.height = window.innerHeight;
canvas.width = window.innerWidth;
canvas.requestPointerLock = canvas.requestPointerLock || canvas.mozRequestPointerLock;
canvas.requestPointerLock();

window.addEventListener('resize', function(){
    engine.resize();
});

}

The questions I have are:

  1. How do I make the render distance higher, so that I can view the whole scene?
  2. Why are none of my scene properties working (gravity, movement, etc.)?

回答1:

Your player camera should be set as the active camera:

var player = new BABYLON.FreeCamera('player', new BABYLON.Vector3(1,1,1), scene);
scene.activeCamera = player;
scene.activeCamera.attachControl(canvas, true);