2023-12-20 16:42:21 +08:00
|
|
|
import { create } from "zustand";
|
|
|
|
import { models } from "@/constant/models.ts";
|
|
|
|
import { Vector3 } from "three";
|
|
|
|
import { devtools } from "zustand/middleware";
|
2023-12-21 17:29:10 +08:00
|
|
|
// import { v4 as uuidv4 } from "uuid";
|
2023-12-20 16:42:21 +08:00
|
|
|
export enum StickerType {
|
|
|
|
text,
|
|
|
|
logo,
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface DecalSticker {
|
|
|
|
id: string;
|
|
|
|
postion: Vector3;
|
|
|
|
text?: string;
|
|
|
|
url: string;
|
2023-12-21 17:29:10 +08:00
|
|
|
scale: number;
|
2023-12-20 16:42:21 +08:00
|
|
|
type: StickerType;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface ModelState {
|
|
|
|
modelLoading: number;
|
|
|
|
setModelLoading: (progress: number) => void;
|
|
|
|
|
|
|
|
activeModel: number;
|
|
|
|
setActiveModel: (index: number) => void;
|
|
|
|
|
|
|
|
activeArea: number;
|
|
|
|
setActiveArea: (index: number) => void;
|
|
|
|
|
|
|
|
activeTextures: number[];
|
|
|
|
setActiveTextures: (activeArea: number, activeTexture: number) => void;
|
|
|
|
|
|
|
|
decalDragging: boolean;
|
|
|
|
setDecalDragging: (enable: boolean) => void;
|
|
|
|
|
2023-12-21 17:29:10 +08:00
|
|
|
decalZooming: boolean;
|
|
|
|
setDecalZooming: (enable: boolean) => void;
|
|
|
|
|
2023-12-20 16:42:21 +08:00
|
|
|
decals: DecalSticker[];
|
|
|
|
activeDecal?: string;
|
|
|
|
setDecalPositon: (id: string, postion: Vector3) => void;
|
|
|
|
setDecals: (decals: DecalSticker[]) => void;
|
2023-12-21 17:29:10 +08:00
|
|
|
setDecalScale: (id: string, scale: number) => void;
|
2023-12-20 16:42:21 +08:00
|
|
|
setActiveDecal: (decalId: string) => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
const useModelStore = create<ModelState>()(
|
|
|
|
devtools((set) => ({
|
|
|
|
modelLoading: 0,
|
|
|
|
setModelLoading: (progress: number) =>
|
|
|
|
set(() => ({ modelLoading: progress })),
|
2023-12-21 17:29:10 +08:00
|
|
|
setDecalScale: (id: string, scale: number) =>
|
|
|
|
set((state) => {
|
|
|
|
const _decals = state.decals.map((el) => {
|
|
|
|
if (el.id === id) {
|
|
|
|
el.scale = scale;
|
|
|
|
}
|
|
|
|
return el;
|
|
|
|
});
|
|
|
|
return {
|
|
|
|
decals: _decals,
|
|
|
|
};
|
|
|
|
}),
|
2023-12-20 16:42:21 +08:00
|
|
|
activeModel: 0,
|
|
|
|
setActiveModel: (index: number) =>
|
|
|
|
set(() => ({
|
|
|
|
activeModel: index,
|
|
|
|
activeArea: 0,
|
|
|
|
activeTextures: Array(models[index].mesh.length).fill(0),
|
|
|
|
})),
|
|
|
|
|
|
|
|
activeArea: 0,
|
|
|
|
setActiveArea: (index: number) => set(() => ({ activeArea: index })),
|
|
|
|
|
|
|
|
activeTextures: Array(models[0].mesh.length).fill(0),
|
|
|
|
setActiveTextures: (activeArea: number, activeTexture: number) =>
|
|
|
|
set((state) => ({
|
|
|
|
activeTextures: state.activeTextures.length
|
|
|
|
? state.activeTextures.map((item, index) =>
|
|
|
|
index === activeArea ? activeTexture : item,
|
|
|
|
)
|
|
|
|
: Array(state.activeTextures.length).fill(0),
|
|
|
|
})),
|
|
|
|
|
|
|
|
// 是否正在拖拽sticker
|
|
|
|
decalDragging: false,
|
|
|
|
setDecalDragging: (enable: boolean) =>
|
|
|
|
set(() => ({ decalDragging: enable })),
|
|
|
|
|
2023-12-21 17:29:10 +08:00
|
|
|
// 是否正在缩放sticker
|
|
|
|
decalZooming: false,
|
|
|
|
setDecalZooming: (enable: boolean) => set(() => ({ decalZooming: enable })),
|
|
|
|
|
|
|
|
decals: [],
|
2023-12-20 16:42:21 +08:00
|
|
|
activeDecal: undefined,
|
|
|
|
setDecalPositon: (id: string, postion: Vector3) =>
|
|
|
|
set((state) => {
|
|
|
|
const _decals = state.decals.map((el) => {
|
|
|
|
if (el.id === id) {
|
|
|
|
return {
|
|
|
|
...el,
|
|
|
|
postion,
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
return el;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return {
|
|
|
|
decals: _decals,
|
|
|
|
};
|
|
|
|
}),
|
|
|
|
setDecals: (decals: DecalSticker[]) =>
|
|
|
|
set(() => ({
|
|
|
|
decals,
|
|
|
|
})),
|
2023-12-21 17:29:10 +08:00
|
|
|
|
2023-12-20 16:42:21 +08:00
|
|
|
setActiveDecal: (decalId: string) =>
|
|
|
|
set(() => ({
|
|
|
|
activeDecal: decalId,
|
|
|
|
})),
|
2023-12-21 17:29:10 +08:00
|
|
|
|
|
|
|
// // @ts-ignore
|
|
|
|
// setDecalScale: (id: string, scale: number) =>
|
|
|
|
// set(() => ({
|
|
|
|
// activeArea: 0
|
|
|
|
// })),
|
2023-12-20 16:42:21 +08:00
|
|
|
})),
|
|
|
|
);
|
|
|
|
|
|
|
|
export default useModelStore;
|