Skip to main content

相机

相机(Camera)是一个在 SDK 内部实现的单例模块,在 World 实例构造之后就可以通过 World 的 getCamera 方法获取到相机的引用。

快速使用

// 禁用相机旋转
worldInstance.getCamera().disableRotation()

// 开启相机旋转
worldInstance.getCamera().enableRotation()

// 获取相机位置
const position = worldInstance.getCamera().position

// 获取相机旋转
const rotation = worldInstance.getCamera().rotation

// 设置相机位姿
worldInstance.getCamera().pose = { position, rotation }

// 截屏
worldInstance.getCamera().screenShot()
// 添加相机旋转限制
worldInstance.getCamera().setMainCameraRotationLimit()
// 移除相机旋转限制
worldInstance.getCamera().removeMainCameraRotationLimit()

// 获取正对某个位置的镜头角度
worldInstance.getCamera().getDeltaRotForLookAt()

// 设置相机的最小可视距离
worldInstance.getCamera().setCameraMinZ()

// 设置相机fov
worldInstance.getCamera().setCameraFov()

// 设置相机聚焦到某actor
worldInstance.getCamera().focusOnActor(actor)

//全景图下转动镜头平滑过渡
const target = {
pitch: 300,
yaw: 300,
roll: 300,
}
const interval = 5 // 动画5秒播放结束

// 每秒插值次数,越高镜头移动效果越平滑
const times = 100

// 结束回调函数
const endCallback = () => {
console.log('镜头移动结束')
}

worldInstance.getCamera().panoramaLerpRotation(target, interval, times, endCallback)

相机内物品检测能力

提供了一个绑定在 camera 上的射线,随着镜头的转动而转动,镜头对准注册的 actor 就触发 onHit 事件

// 创建actor
const actor = world.spawn(...)
// 添加需要检测的actor
worldInstance.getCamera().getDetectRay().addDetectActor(actor)
// 开启相机射线检测
worldInstance.getCamera().getDetectRay().start()
// 注册检测到回调事件,参数包含被检查到的actor
worldInstance.getCamera().getDetectRay().on('onHit', (res) => {...})


// 取消需要检测的actor
worldInstance.getCamera().getDetectRay().deleteActor(actor)
// 关闭相机射线检测
worldInstance.getCamera().getDetectRay().stop()
// 取消回调事件
worldInstance.getCamera().getDetectRay().off('onHit', (res) => {...})