Skip to content

Sbm 模型

警告

v2.5.0 之后 Sbm 相关的 API 已经合并到 Model

您可以使用 Model 的相关方法替代 Sbm

后续 Sbm 的相关方法将会被废弃

loadSbm

加载 Sbm 模型。

样例:

定义:

ts
interface SbmInfo extends BaseObject3DInfo, ObjectEvents<Sbm> {
  url: string;
}

function loadSbm(sbmInfo: SbmInfo, onProgress?: ModelLoadingProgressCallback): Promise<Sbm>;

用法:

js
ssp
  .loadSbm(
    // sbmInfo
    {
      id: 'xx',
      name: 'xx',
      url: 'xx/x.sbm',
      level: {
        max: 1000,
        min: null,
      },
      position: { x: 1000, y: 0, z: 1000 },
      rotation: { x: 0, y: 0, z: 0 },
      scale: { x: 2, y: 2, z: 2 },
      onClick(e) {
        /**
         * 对象的独立事件触发后,默认不传播(类似 DOM 的事件冒泡)到全局事件,
         * 调用 eventPropagation 方法通知事件继续传播到全局。
         *
         * warn:
         *  在 **非箭头函数** 中参数 e 与 this 的指向都是当前模型对象,
         *  在 *箭头函数** 参数 e 依然是模型对象,但 this 指向会发生改变。
         */
        this.eventPropagation();

        console.log('模型自身的点击事件触发', this);
      },
      onDblClick: (e) => {
        /**
         * 这里模拟在 **箭头函数** 中
         */
        e.eventPropagation();

        console.log('模型自身的双击事件触发', e);
      },
      userData: {},
    }
  )
  .then((sbm) => console.log(sbm))
  .catch((err) => console.error(err));

参数:

sbmInfo

  • 描述: 实例 Sbm 对象所需信息
  • 类型: SbmInfo
  • 必填:
SbmInfo
属性描述类型必填默认值
id唯一IDstring | number
name名称string
url资源路径string
level显示层级范围Level{ max: null, min: null }
visible是否可见booleantrue
position位置坐标Position{ x: 0, y: 0, z: 0 }
rotation旋转弧度Rotation{ x: 0, y: 0, z: 0 }
scale缩放比例Scale{ x: 1, y: 1, z: 1 }
userData用户数据any{}
onClick左键单击事件(object: Sbm) => voidnull
onDblClick左键双击事件(object: Sbm) => voidnull
onRightClick右键单击事件(object: Sbm) => voidnull
onLoad加载完成事件(object: Sbm) => voidnull

onProgress

  • 描述: 模型加载时进度回调函数,回填参数包含如下字段。
  • 类型: (progress: ModelLoadingProgress) => void
  • 必填:
ModelLoadingProgress
字段名描述类型
total需要加载总数number
loaded已加载完成数量number
timeStamp单步消耗时长number

parseSbm

解析 Sbm 模型

定义:

ts
function parseSbm(data: ArrayBuffer, sbmInfo: SbmInfo, onProgress?: ModelLoadingProgressCallback): Promise<Sbm>;

用法:

js
const sbm = await ssp.parseSbm(new ArrayBuffer(8), sbmInfo, () => {});

参数:

data

  • 描述: 模型数据
  • 类型: ArrayBuffer
  • 必填:

sbmInfo

同上

onProgress

同上

cloneSbm

克隆 Sbm 模型

定义:

ts
interface CloneSbmInfo extends Omit<SbmInfo, 'url'> {}

function cloneSbm(model: Sbm, cloneSbmInfo: CloneSbmInfo, parent?: BaseObject3D | null): Promise<Sbm>;

用法:

js
const clonedSbm = await ssp.cloneSbm(sbm, {
  id: 'clone_sbm',
  position: {
    x: 100,
    y: 0,
    z: 0,
  },
});

参数:

model

  • 描述: Sbm 对象
  • 类型: Sbm
  • 必填:

sbmInfo

SbmInfo, 但不需要字段 url

parent

  • 描述:Sbm 克隆到的 parent
  • 类型: Sbm
  • 必填:

getSbmById

通过 id 查找

定义:

ts
function getSbmById(id: SbmInfo['id']): Sbm | null;

用法:

js
const sbm = ssp.getSbmById('xxx');

弃用警告

请使用 getObjectById 替代

getSbmByName

通过 name 查找

定义:

ts
function getSbmByName(name: string): Sbm[];

用法:

js
const sbmList = ssp.getSbmByName('xxx');

弃用警告

请使用 getObjectByName 替代

getAllSbm

获取所有 Sbm 对象

定义:

ts
function getAllSbm(): Sbm[];

用法:

js
const allSbmList = ssp.getAllSbm();

getSbmByUserDataProperty

通过 userData 属性查找

定义:

ts
function getSbmByUserDataProperty(propNameOrFindFunc: string | UserDataPropertyFindFunc, value?: any): Sbm[];

用法:

js
const sbmList = ssp.getSbmByUserDataProperty('propKey''propVal')
// or
const sbmList = ssp.getSbmByUserDataProperty(item => item['itemPropKey'] === 'itemPropVal')

参数:

propNameOrFindFunc

  • 描述: userData 内属性名 或 find 函数
  • 类型: string | function
  • 必填:

propValue

  • 描述: userData 内属性值。
  • 类型: any
  • 必填:

find 函数使用场景

js
sbm.userData = {
  people: {
    name: 'xiaoming',
    age: 18,
  },
};
const sbmList = ssp.getSbmByUserDataProperty((userData) => userData?.people?.name === 'xiaoming');

弃用警告

请使用 getObjectByUserDataProperty 替代

removeSbmById

通过 id 移除

定义:

ts
function removeSbmById(id: SbmInfo['id']): boolean;

用法:

js
ssp.removeSbmById('xxx');

弃用警告

请使用 removeObjectById 替代

loadSbmToGroup

加载 Sbm 到一个组内。

定义:

ts
function loadSbmToGroup(
  groupInfo: GroupInfo,
  sbmInfoList: SbmInfo[],
  onProgress?: GroupProgressCallback
): Promise<Group>;

用法:

js
ssp
  .loadSbmToGroup(
    // groupInfo
    {
      id: 'firstSbmGroup',
      name: 'name_firstSbmGroup',
      // ...
    },
    // sbmInfoList
    [sbmInfo1, sbmInfo2, sbmInfo3]
  )
  .then((group) => console.log(group));

参数

groupInfo

  • 描述: 实例组对象所需信息
  • 类型: GroupInfo
  • 必填:
GroupInfo
属性描述类型必填默认值
id组唯一IDstring | number
name组名称string
level显示层级范围Level{ max: null, min: null }
position位置坐标Position{ x: 0, y: 0, z: 0 }
rotation旋转弧度Rotation{ x: 0, y: 0, z: 0 }
scale缩放比例Scale{ x: 1, y: 1, z: 1 }
userData用户数据any{}

sbmInfoList

  • 描述: sbmInfo 集合
  • 类型: sbminfo[]
  • 必填:

onProgress

  • 描述: 模型加载到组内时进度回调函数,回填参数包含如下字段。
  • 类型: (groupProgress: GroupProgress) => void
  • 必填:
GroupProgress
字段名描述类型
modelTotal需要加载的模型总数 (sbmInfoList长度) number
loadingModelIndex当前正在加载的模型索引number
current当前在在加载模型的详细进度ModelLoadingProgress

createGroupForSbm

Sbm 提前创建一个空组。

使用场景

loadSbmToGroup 不同,有些时候可能你还没有具体的 sbmInfo 数据,但你想提前创建一个批量管理的空组,当有数据时再使用 addSbmForGroup 插入。

定义:

ts
function createGroupForSbm(groupInfo: GroupInfo): Group;

用法:

js
ssp.createGroupForSbm({
  id: 'firstSbmGroup',
  name: 'name_firstSbmGroup',
  // ...
});

参数

groupInfo

  • 描述: 实例组对象所需信息
  • 类型: GroupInfo
  • 必填:

弃用警告

请使用 createGroup 替代

addSbmForGroup

向一个已经存在的组内添加 Sbm 对象。

定义:

ts
function addSbmForGroup(
  groupId: GroupInfo['id'],
  sbmInfoList: SbmInfo[],
  onProgress?: GroupProgressCallback
): Promise<Group | null>;

用法:

js
ssp
  .addSbmForGroup(
    // groupId
    'firstSbmGroup',
    // sbmInfoList
    [sbmInfo4, sbmInfo5]
    // onProgress
  )
  .then((group) => console.log(group));

参数

groupId

  • 描述:id
  • 类型: groupId[‘id’]
  • 必填:

sbmInfoList

  • 类型: sbminfo[]
  • 描述: sbmInfo 集合
  • 必填:

onProgress

  • 描述: 模型加载到组内时进度回调函数,回填参数包含如下字段。
  • 类型: GroupProgressCallback
  • 必填:

createSbmGroupFromXml

创建 Sbm 组,从 xml 文件资源

定义:

ts
function createSbmGroupFromXml(groupInfo: GroupInfo, url: string): Promise<Group>;

用法:

js
const group = await ssp.createSbmGroupFromXml(
  // groupInfo
  {
    id: 'firstSbmGroup',
    name: 'name_firstSbmGroup',
    // ...
  },
  // url
  'xxx.xml'
);

参数:

groupInfo

同上

url

  • 描述: xml 文件的 url 地址
  • 类型: string
  • 必填:

getSbmGroupById

通过 id 查找 Sbm

定义:

ts
function getSbmGroupById(id: GroupInfo['id']): Group | null;

用法:

js
const group = ssp.getSbmGroupById('firstSbmGroup');

弃用警告

请使用 getObjectById 替代

getSbmGroupByName

通过 name 查找 Sbm

定义:

ts
function getSbmGroupByName(name: string): Group[];

用法:

js
const groupList = ssp.getSbmGroupByName('name_firstSbmGroup');

弃用警告

请使用 getObjectByName 替代

getAllSbmGroup

获取所有 Sbm 对象组

定义:

ts
function getAllSbmGroup(): Group[];

用法:

js
const allSbmGroupList = ssp.getAllSbmGroup();

弃用警告

请使用 getAllGroup 替代

removeSbmGroupById

通过 id 移除 Sbm

定义:

ts
function removeSbmGroupById(id: GroupInfo['id']): boolean;

用法:

js
const isRemoveSuccess = ssp.removeSbmGroupById('firstSbmGroup');

弃用警告

请使用 removeObjectById 替代

clearSbm

清除当前场景内所有 Sbm 对象。

定义:

ts
function clearSbm(): void;

用法:

js
ssp.clearSbm();

弃用警告

请使用 clearModel 替代

showAllSbm

显示当前场景内所有 Sbm 对象。

定义:

ts
function showAllSbm(): void;

用法:

js
ssp.showAllSbm();

弃用警告

请使用 showAllModel 替代

hideAllSbm

隐藏当前场景内所有 Sbm 对象。

定义:

ts
function hideAllSbm(): void;

用法:

js
ssp.hideAllSbm();

弃用警告

请使用 hideAllModel 替代

getSbmModelMaps

获取 Sbm 模型缓存

定义:

ts
function getSbmModelMaps(): Map<string, Sbm>;

用法:

js
const sbmMaps = ssp.getSbmModelMaps();

setSbmModelMaps

设置 Sbm 模型缓存

定义:

ts
function setSbmModelMaps(maps: Map<string, Sbm>): void;

用法:

js
const maps = new Map();

ssp.setSbmModelMaps(maps);

浙ICP备16043491号