简体中文
主题
简体中文
主题
相关参考:
Web 插件对 glTF/GLB 模型的支持,主要体现在“动态创建模型”和“控制模型动画”两个方向。
const model = await api.createGltfModel('https://your.domain.com/model/rabbit.glb');
await api.add(model);const response = await fetch('/my-assets/model/rabbit.glb');
const buffer = await response.arrayBuffer();
const model = await api.createGltfModel(buffer);
await api.add(model);如果使用 URL,请确保资源服务器对 https://www.kivicube.com 开放 CORS。
glTF 模型通常不是一个单独对象,而是一棵对象树。因此常见用法是:
const model = await api.getObject('rabbit');
const children = await api.getChildren(model);如果你需要控制模型内部某个节点,可以结合 getChildByProperty() 查找。
await api.setPosition(model, 0, 0, 0);
await api.setRotation(model, 0, Math.PI / 2, 0);
await api.setScale(model, 1.5, 1.5, 1.5);const names = await api.getAnimationNames(model);
console.log(names);await api.playAnimation(model, {
name: 'Idle',
loop: true,
repeat: Infinity,
});动画配置还支持:
loop - 是否循环播放repeat - 循环次数repeatMode - 循环模式。0:正常, 1: 往复timeScale - 播放倍速。1为原始速度。默认为1weight - 多动画播放时的权重占比。默认为1clampWhenFinished - 播放完毕后,是否停留最后一帧。resetLoopCount - 重置循环播放的次数为0await api.pauseAnimation(model, 'Idle');
await api.stopAnimation(model, 'Idle');
// 支持的参数和playAnimation一致。
await api.playbackAnimation(model, { name: 'Idle', loop: true });const running = await api.animationIsRunning(model, 'Idle');
const loop = await api.isAnimationLoop(model, 'Idle');
const runningNames = await api.getAnimationIsRunningNames(model);const obj = await api.getObject('rabbit');play - 动画播放时触发function onPlay(event) {
// animationName - 当前播放的动画名称
const { type, target, animationName } = event;
}
await api.on('play', onPlay, obj);pause - 动画暂停时触发function onPause(event) {
// animationName - 当前暂停的动画名称
const { type, target, animationName } = event;
}
await api.on('pause', onPause, obj);ended - 动画自然播放完毕时触发function onEnded(event) {
// animationName - 当前播放完毕的动画名称
const { type, target, animationName } = event;
}
await api.on('ended', onEnded, obj);replay - 动画重新播放时触发function onReplay(event) {
// animationName - 当前重新播放的动画名称
const { type, target, animationName } = event;
}
await api.on('replay', onReplay, obj);自定义动画,和模型内置动画,在事件和控制上是同样的支持。
模型往往是最需要配合灯光、环境贴图一起使用的对象。常见组合是:
相关能力见: