Skip to main content

特殊进房模式

两种特殊模式:本地全景图进房无网模式进房

  • 本地全景图模式和正常进房流程完全相同,后台服务也全部可以正常使用,只是后台不会推送全景图,SDK会渲染开发者指定的本地全景图
  • 无网模式和本地全景图模式一样,会渲染一张本地全景图,用法也类似,但完全关闭了后端服务。所有的后台服务(如同步、广播、聊天等)都会失效

本地全景图进房

具体步骤

有一些场景,只需要一张全景图,这时我们可以把全景图放入资产预加载列表,进房时已经预加载到indexDB,可以直接渲染出来,无需向后台请求。 但是业务仍需要其他的后台能力,如同步、广播等,因此仍需要正常走进房流程,只是不需要后台下发的全景图。SDK支持了该能力,进房前通过 setSkinInfo() 设置 localTextureBomId 参数,使用本地贴图路径而不通过网络下载贴图资源。

代码示例:

const targetRoom = worldInstance.getRoomInstance(targetRoomId, TargetRoomClass)

targetRoom.setSkinInfo({
// 正常情况下,已经在XConsole配置了一条路线,对应要本地加载的全景图
// 需要传入该pathId,SDK会根据该id获取渲染全景图时的相机位置和朝向
pathId: 'xxxx',

// 全景图bomId,sdk会到config查找该bomid对应的全景图url/blob,如果找到则应用,否则报错
localTextureBomId: 'xxxx',

// 全景图参数,指定分辨率
localTextureParam: { localTextureWidth: 4096, localTextureHeight: 2048 }
})

targetRoom.enter()

上文解释了为何本地全景图还要传入pathId,是为了获取渲染全景图时的相机位置和朝向。 也支持不传,在localTextureParam中直接指定相机位置localTextureCamera即可:

// 本地全景图参数
export interface ILocalTextureParam {
// 图片宽度(不填默认4096 即4K分辨率)
localTextureWidth?: number
// 图片高度(不填默认2048 即4K分辨率)
localTextureHeight?: number
// 镜头位置(选填,如果设置则直接使用,如果没有,则必须指定pathId,会到config查询该pathId对应的镜头点位)
localTextureCamera?: IOrientedObject
}

targetRoom.setSkinInfo({
localTextureBomId: 'xxxx',

localTextureParam: {
localTextureWidth: 4096,
localTextureHeight: 2048,
localTextureCamera: {
position: { x: 19593.59, y: -36167.14, z: 10081 },
rotation: { pitch: -7, yaw: 50, roll: 0 },
},
},
})

当然,也不是必须要把该全景图放入预加载流程,只要上传至XConsole资产库并配置bomId,SDK就可以根据bomId获取它的下载链接,在进房时从CDN拉取。

进房后切path

进房后切path,同样支持切换到本地全景图。使用方法完全一样:

await worldInstance.currentRoom?.setSkinInfo({
// 正常情况下,已经在console配置了一条路线,对应要本地加载的全景图
// 需要传入该pathId,SDK会根据该id获取渲染本地全景图时的相机位置
pathId,

// 全景图bomId,sdk会到config查找该bomid对应的全景图url/blob,如果找到则应用,否则报错
localTextureBomId: localTextureBomId,

// 全景图参数,指定分辨率
localTextureParam: { width: 4096, height: 2048 }
})

无网模式进房

serverless模式和本地全景图模式一样,渲染一张本地全景图,用法也类似,只是多指定了一个world参数,但完全关闭了后端服务。 使用 serverless 模式时,初始化world时需指定option.serverless为 true,然后在每次进房前,调用 setSkinInfo 传入 pathId 和 localTextureBomId。

注意:serverless 模式下只能进进房和切房间room.enter()操作,不支持切换skin和path,所有的后台服务(如同步、广播、聊天等)都会失效,avatar也建议隐藏。

代码示例:

import { World, IWorldOptions } from '@xverse/core'

const worldOptions: IWorldOptions = {
...
serverless: true
}

const world = new World(worldOptions)

....

const targetRoom = worldInstance.getRoomInstance(targetRoomId, TargetRoomClass)
await targetRoom.setSkinInfo({
// 正常情况下,已经在console配置了一条路线,对应要本地加载的全景图
// 需要传入该pathId,SDK会根据该id查询渲染本地全景图时的相机位置
pathId,

// 全景图bomId,sdk会到config查找该bomid对应的全景图url/blob,如果找到则应用,否则报错
localTextureBomId: localTextureBomId,

// 全景图参数,指定分辨率
localTextureParam: { width: 4096, height: 2048 }
})


await targetRoom.enter()

融合模式

可以先走全景图模式进房,如果报错,则切换进入无网模式,这样可以保证进房100%成功。

...

// 检测、预初始化和预加载出错,则进b面
try {
world.checkSupport()
world.preInit()

world.preload.start()
} catch (err) {
// 重试 or 进b面
}

// 进房失败
try {
world.init()
targetRoom.enter({ localTextureBomId })
} catch (err) {
world.destroy()
world._option.serverless = true

// 再来一次preInit(),无须检测和预加载
world.preInit()
world.init()
targetRoom.enter({ localTextureBomId })
}