babylonjs 常用基础相机类型

1,001 阅读1分钟

1.UniversalCamera

默认相机

操作:

   键盘——← →控制相机左右移动,↑ ↓ 控制相机向前和向后移动;

   鼠标——以摄像机为原点旋转摄像机;

   触摸——左右滑动,左右移动相机,上下滑动,前后移动;

   手柄——设备上对应的按键控制方向和移动。

需要点击渲染的场景才能使控制生效。

UniversalCamera(name: string, position: BABYLON.Vector3, scene: BABYLON.Scene)

//示例

let camera = new BABYLON.UniversalCamera('Camera', new BABYLON.Vector3(0,5,0), scene)

// Attach the camera to the canvas

camera.attachControl(canvas, false)

2.FollowCamera

跟随相机,可以通过设置跟随目标来让相机在目标移动时跟随目标一起移动。

FollowCamera(name: string, position: BABYLON.Vector3, scene: BABYLON.Scene, lockedTarget?: BABYLON.Nullable<BABYLON.AbstractMesh> | undefined)

//position:相机的初始位置,三维坐标。

//lockedTarget:相机跟随目标。

//示例

let camera = new BABYLON.FollowCamera('Camera', new BABYLON.Vector3(0,5,0), scene,sphere3)

// Attach the camera to the canvas

camera.attachControl()

camera.radius = 100   //相机距目标距离

camera.rotationOffset = 90   //相机位与目标角度,90为从初始角度顺时针旋转90度。

camera.heightOffset = 50 //相机位距目标相对高度。

3.ArcRotateCamera

此相机始终会朝向给定目标的位置点,可以当前目标作为旋转中心旋转。它可以通过光标,鼠标和触摸事件控制。

2019022621510211.jpg

ArcRotateCamera(name: string, alpha: number, beta: number, radius: number, target: BABYLON.Vector3, scene: BABYLON.Scene, setActiveOnSceneIfNoneActive?: boolean | undefined)

//alpha:相机经度。

//beta:相机纬度。

//radius:离目标的距离。

//target:目标三维坐标,如 new BABYLON.Vector3(-5,5, 35)。

//setActiveOnSceneIfNoneActive:设置在场景内没有其它相机被激活时此相机是否要自动激活。


//示例

let camera = new BABYLON.ArcRotateCamera('Camera', -Math.PI / 2, Math.PI * 0.6,50, new BABYLON.Vector3(-5,5, 35), scene)

camera.attachControl(canvas)