部分場景
image.png
image.png
體驗地址
操作說明 視頻
https://www.bilibili.com/video/BV1kY4y1v78z/
我做了哪些(功能)
- draco解析glb模型 同時處理部分紋理請求 減輕一次加載紋理壓力
- 手動控制軌道控制器鏡頭動畫
- 多音頻拼接 控制
- 封裝動畫播放器 控制進度切換
- 動畫進度控制器 同步音頻 模擬視頻體驗
- useContext狀態(tài)共享
- 自定義多級右鍵菜單 模擬原生菜單體驗
- 空閑時間加載后續(xù)用到的模型
- 模型紋理&位置動態(tài)切換
- echart圖表使用
- 瀏覽器自適應(yīng)單位vw vmax使用(大面積使用)
- 兼容移動端手機瀏覽
- 模型的銷毀和動畫加載
- useRef暴露方法多方調(diào)用(大量使用)
- css-in-js 方案實踐 css引擎styled-component
- 未完成請求避免產(chǎn)生影響
- 未執(zhí)行計數(shù)器清理 字幕播放中 鏡頭切換動畫執(zhí)行中切換 后續(xù)的功能等
- 點擊不同模型產(chǎn)生不同效果 點擊的事件監(jiān)聽 鼠標(biāo)hover的樣式
- ...
局限
- 性能拉垮 考慮到諸多原因 未采用按需渲染在低配機上幀率大概只有30幀左右
- 模型做的不精細-第一次建模
- 在手機或者高刷設(shè)備上動畫模型播放渲染速度與60幀設(shè)備不一致
源代碼地址
https://gitee.com/honbingitee/kmyc
部分邏輯
按需渲染
controls.addEventListener('change', () => {
renderer.render(scene, camera);
});
模型加載
https://hongbin.blog.csdn.net/article/details/122594047
動畫控制器
https://hongbin.blog.csdn.net/article/details/123662686
模型紋理條件切換
/**
* @description: 加載相冊模型 返回相關(guān)操作回調(diào)
* @param {number} animationIndex 戰(zhàn)役索引
* @param {THREE.TextureLoader} textureLoader 紋理加載器
* @return {XCBack} XCBack
*/
export async function loadXCModel(
animationIndex: number,
textureLoader: THREE.TextureLoader
): Promise<XCBack> {
const gltf = await window.gltfLoader.loadAsync(xcModel);
const [model] = gltf.scene.children;
const multiple = 5;
model.position.y = 1.8 * multiple;
if (animationIndex === 1) {
model.rotateY(-Math.PI / 2);
model.rotateX(-Math.PI / 10);
model.rotateZ(Math.PI / 10);
}
const setZero = (mash: typeof model) => {
mash.scale.x = 0;
mash.scale.y = 0;
mash.scale.z = 0;
};
setZero(model);
/**
* 設(shè)置不同的紋理 -- 切換圖片
*/
const setMaterial = (mash: Object3D, url: string) => {
const texture = textureLoader.load(url);
texture.flipY = false;
texture.encoding = 3001;
//@ts-ignore
const mater = mash.material.clone(); //不能共用一個material 以為 instance.material 指向的都是同一個對象
mater.map = texture;
//@ts-ignore
mash.material = mater;
};
const pictures: XCBack["models"] = [];
for (let i = 0; i < 4; i++) {
const instance = model.clone();
//hover tip
instance.userData.type = ModelType["Picture"];
instance.userData.desc = XCDesc[animationIndex]
? XCDesc[animationIndex][i]
: "";
setMaterial(
instance,
`${process.env.REACT_APP_URL}xc/${animationIndex}-${i}-y.jpg`
);
if (animationIndex === 1) {
instance.position.x = (i - 1) * -5 * multiple;
instance.position.z = -14 * multiple;
} else if (animationIndex === 2) {
instance.position.x = -10 * multiple;
instance.position.z = (i - 2) * 5 * multiple;
} else {
instance.position.x = -10 * multiple;
instance.position.z = (i - 1) * 5 * multiple;
}
pictures.push(instance);
}
let timer1: number;
let count = 0;
const range = 30;
const show = () => {
if (count < range) {
pictures.forEach(item => {
item.scale.y += multiple / range;
item.scale.z += (multiple / range) * 1.8;
item.scale.x += multiple / range / 10;
});
count++;
timer1 = requestAnimationFrame(show);
}
};
const hide = () => {
pictures.forEach(setZero);
count = 0;
cancelAnimationFrame(timer1);
requestAnimationFrame(() => {
cancelAnimationFrame(timer1);
});
};
let prevIndex = animationIndex;
const toggle: XCBack["toggle"] = nextIndex => {
pictures.forEach((item, index) => {
/**
* hover 顯示圖片介紹
*/
item.userData.type = ModelType["Picture"];
item.userData.desc = XCDesc[nextIndex][index];
setMaterial(
item,
`${process.env.REACT_APP_URL}xc/${nextIndex}-${index}-y.jpg`
);
//旋轉(zhuǎn)角度
if (nextIndex === 1) {
if (prevIndex !== 1) {
item.rotateY(-Math.PI / 2);
item.rotateX(-Math.PI / 10);
item.rotateZ(Math.PI / 10);
}
} else {
if (prevIndex === 1) {
item.rotateY(Math.PI / 2);
item.rotateX(Math.PI / 10);
item.rotateZ(Math.PI / 10);
}
}
//位置
if (nextIndex === 1) {
item.position.x = (index - 1) * -5 * multiple;
item.position.z = -14 * multiple;
} else if (nextIndex === 2) {
item.position.x = -10 * multiple;
item.position.z = (index - 2) * 5 * multiple;
} else {
item.position.x = -10 * multiple;
item.position.z = (index - 1) * 5 * multiple;
}
});
prevIndex = nextIndex;
};
return {
show,
hide,
models: pictures,
toggle,
};
}
模型加載同時請求部分紋理 生成進度條
//加載10個紋理
const loadTexture = () => {
const textureLoader = new TextureLoader();
for (let i = 0; i < 10; i++) {
const index = i.toString().padStart(2, "0");
const url = `${process.env.REACT_APP_URL}q/${i}.jpg`;
const texture = textureLoader.load(
url,
_ => {
setProgress(timeCheck(5));
},
undefined,
err => {
console.error("load texture fail:", err);
setProgress(timeCheck(5));
}
);
texture.flipY = false;
texture.encoding = sRGBEncoding;
addTexture(index, texture);
}
};
//draco解析模型
const dracoLoader = () => {
let prevModel = 0;
const manager = new LoadingManager();
manager.onProgress = (_, loaded, total) => {
const progress = Math.floor((loaded / total) * 100);
if (progress === 100) return setProgress(timeCheck(50 - prevModel));
prevModel += progress / 4;
setProgress(timeCheck(progress / 4));
};
//設(shè)置錯誤信息
manager.onError = setIsLoadFail;
//創(chuàng)建draco解析器
const dracoLoader = new DRACOLoader(manager);
dracoLoader.setDecoderConfig({ type: "js" });
dracoLoader.setDecoderPath(process.env.REACT_APP_URL as string);
// gltf 加載器
const gltfLoader = new GLTFLoader(manager);
gltfLoader.setDRACOLoader(dracoLoader);
gltfLoader.load(mapModel, setMap);
//不帶LoadingManager的加載器 如果使用gltfLoader會觸發(fā)事件改變progress狀態(tài)造成內(nèi)存泄漏
const normalGltfLoader = new GLTFLoader();
normalGltfLoader.setDRACOLoader(dracoLoader);
window.gltfLoader = normalGltfLoader;
};
/**
* 進度增長檢測
* @param {number} increase 增長的數(shù)值
* @param {number} prev state原本數(shù)值
* @return {number} newValue
*/
const timeCheck = (increase: number) => (prev: number) => {
if (increase + prev < 100) return prev + increase;
// >= 100 檢測時間
if (Date.now() - enterTime > maxLoadTime) return 100;
//time < maxLoadTime
timer = setTimeout(() => {
setProgress(100);
}, maxLoadTime - (Date.now() - enterTime));
//顯示跳過按鈕
setIsCanJump(true);
return prev;
};
模型縮放小動畫
let timer: number;
let i = 0;
const r = () => {
if (i < 15) {
timer = requestAnimationFrame(r);
}
for (const item of iconScene.children) {
item.scale.x += 0.03;
item.scale.y += 0.03;
item.scale.z += 0.03;
}
i++;
};
r();