Line data Source code
1 : import { toRaw } from 'vue'
2 : import type { Object3D } from 'three'
3 :
4 : import type Load3d from '@comfyorg/viewport3d/load3d/Load3d'
5 : import { adoptClonedModel } from '@comfyorg/viewport3d/load3d/quadWireframe/adoptClonedModel'
6 : import { cloneObject3DResources } from '@comfyorg/viewport3d/load3d/SceneModelManager'
7 : import { reportLoadModelOutcome } from '@/extensions/core/load3d/reportLoadModelOutcome'
8 : import type { LGraphNode } from '@/lib/litegraph/src/litegraph'
9 :
10 : type SkeletonUtilsModule = { clone: (source: Object3D) => Object3D }
11 :
12 : interface SceneConfig {
13 : backgroundImage?: string
14 : }
15 :
16 24 : let cachedNodeToLoad3dMap: Map<LGraphNode, Load3d> | null = null
17 24 : let cachedSkeletonUtils: SkeletonUtilsModule | null = null
18 24 : const load3dsKeepingModel = new WeakSet<Load3d>()
19 :
20 0 : export function keepingLoadedModel(load3d: Load3d, run: () => void): void {
21 0 : load3dsKeepingModel.add(load3d)
22 0 : try {
23 0 : run()
24 : } finally {
25 0 : load3dsKeepingModel.delete(load3d)
26 : }
27 : }
28 :
29 25 : export function isKeepingLoadedModel(load3d: Load3d): boolean {
30 25 : return load3dsKeepingModel.has(load3d)
31 : }
32 :
33 : /** Returns null until `useLoad3dService().getLoad3dAsync` has loaded the map. */
34 14 : export function getNodeToLoad3dMapSync(): Map<LGraphNode, Load3d> | null {
35 14 : return cachedNodeToLoad3dMap
36 : }
37 :
38 2 : export function cacheNodeToLoad3dMap(map: Map<LGraphNode, Load3d>): void {
39 2 : cachedNodeToLoad3dMap = map
40 : }
41 :
42 2 : async function loadSkeletonUtils() {
43 2 : if (!cachedSkeletonUtils) {
44 2 : cachedSkeletonUtils = await import('three/examples/jsm/utils/SkeletonUtils')
45 : }
46 2 : return cachedSkeletonUtils
47 : }
48 :
49 0 : export function getNodeByLoad3d(load3d: Load3d): LGraphNode | null {
50 0 : const map = getNodeToLoad3dMapSync()
51 0 : if (!map) return null
52 0 : for (const [node, instance] of map) {
53 0 : if (instance === load3d) {
54 0 : return node
55 : }
56 : }
57 0 : return null
58 : }
59 :
60 0 : export function removeLoad3d(node: LGraphNode) {
61 0 : const rawNode = toRaw(node)
62 0 : const map = getNodeToLoad3dMapSync()
63 0 : if (!map) return
64 :
65 0 : const instance = map.get(rawNode)
66 :
67 0 : if (instance) {
68 0 : instance.remove()
69 0 : map.delete(rawNode)
70 : }
71 : }
72 :
73 2 : export async function copyLoad3dState(source: Load3d, target: Load3d) {
74 2 : const sourceModel = source.modelManager.currentModel
75 :
76 2 : const gizmoWasEnabled = target.getGizmoManager().isEnabled()
77 2 : const showSkeleton = target.getModelManager().showSkeleton
78 2 : target.getGizmoManager().detach()
79 :
80 2 : if (sourceModel) {
81 2 : target.getModelManager().disposeCurrentModel()
82 :
83 2 : if (source.isSplatModel()) {
84 0 : const originalURL = source.modelManager.originalURL
85 0 : if (originalURL) {
86 0 : const outcome = await target.loadModel(originalURL)
87 0 : reportLoadModelOutcome(outcome)
88 0 : if (outcome !== 'loaded') return
89 : }
90 : } else {
91 : const SkeletonUtils = await loadSkeletonUtils()
92 2 : const modelClone = SkeletonUtils.clone(sourceModel)
93 2 : adoptClonedModel(
94 : modelClone,
95 : sourceModel,
96 : source.getModelManager().originalMaterials
97 : )
98 2 : cloneObject3DResources(modelClone)
99 2 : target.getModelManager().materialMode = 'original'
100 :
101 2 : target.getModelManager().currentModel = modelClone
102 2 : target.getSceneManager().scene.add(modelClone)
103 2 : target.getModelManager().setupModelMaterials(modelClone)
104 :
105 2 : const sourceOriginalModel = source.getModelManager().originalModel
106 :
107 2 : if (sourceOriginalModel) {
108 0 : target.getModelManager().originalModel = sourceOriginalModel
109 : }
110 :
111 2 : target.getModelManager().currentUpDirection =
112 : source.getModelManager().currentUpDirection
113 :
114 2 : const appliedTexture = source.getModelManager().appliedTexture
115 2 : if (appliedTexture) {
116 0 : target
117 : .getModelManager()
118 : .borrowAppliedTexture(appliedTexture.clone(), true)
119 : }
120 :
121 2 : target.setMaterialMode(source.getModelManager().materialMode)
122 2 : target.setUpDirection(source.getModelManager().currentUpDirection)
123 2 : target.setShowSkeleton(showSkeleton)
124 :
125 2 : const sourceInitial = source.getGizmoManager().getInitialTransform()
126 2 : modelClone.position.set(
127 : sourceInitial.position.x,
128 : sourceInitial.position.y,
129 : sourceInitial.position.z
130 : )
131 2 : modelClone.rotation.set(
132 : sourceInitial.rotation.x,
133 : sourceInitial.rotation.y,
134 : sourceInitial.rotation.z
135 : )
136 2 : modelClone.scale.set(
137 : sourceInitial.scale.x,
138 : sourceInitial.scale.y,
139 : sourceInitial.scale.z
140 : )
141 :
142 2 : target.getGizmoManager().setupForModel(modelClone)
143 2 : const gizmoTransform = source.getGizmoTransform()
144 2 : target.applyGizmoTransform(
145 : gizmoTransform.position,
146 : gizmoTransform.rotation,
147 : gizmoTransform.scale
148 : )
149 : const shouldEnable =
150 : gizmoWasEnabled || source.getGizmoManager().isEnabled()
151 2 : if (shouldEnable) {
152 0 : target.setGizmoEnabled(true)
153 : }
154 :
155 2 : if (source.hasAnimations()) {
156 0 : target.animationManager.setupModelAnimations(
157 : modelClone,
158 : sourceOriginalModel
159 : )
160 : }
161 : }
162 : }
163 :
164 2 : const sourceCameraType = source.getCurrentCameraType()
165 2 : const sourceCameraState = source.getCameraState()
166 :
167 2 : target.toggleCamera(sourceCameraType)
168 2 : target.setCameraState(sourceCameraState)
169 :
170 2 : target.setBackgroundColor(source.getSceneManager().currentBackgroundColor)
171 :
172 2 : target.toggleGrid(source.getSceneManager().gridHelper.visible)
173 :
174 : const sourceBackgroundInfo = source
175 : .getSceneManager()
176 : .getCurrentBackgroundInfo()
177 2 : if (sourceBackgroundInfo.type === 'image') {
178 : const sourceNode = getNodeByLoad3d(source)
179 : const sceneConfig = sourceNode?.properties['Scene Config'] as
180 : | SceneConfig
181 : | undefined
182 0 : const backgroundPath = sceneConfig?.backgroundImage
183 0 : if (backgroundPath) {
184 0 : await target.setBackgroundImage(backgroundPath)
185 : }
186 : } else {
187 2 : await target.setBackgroundImage('')
188 : }
189 :
190 2 : target.setLightIntensity(
191 : source.getLightingManager().lights[1]?.intensity || 1
192 : )
193 :
194 2 : if (sourceCameraType === 'perspective') {
195 2 : target.setFOV(source.getCameraManager().perspectiveCamera.fov)
196 : }
197 : }
198 :
199 0 : export function handleViewportRefresh(load3d: Load3d | null) {
200 0 : if (!load3d) return
201 :
202 0 : load3d.handleResize()
203 :
204 0 : const currentType = load3d.getCurrentCameraType()
205 :
206 0 : load3d.toggleCamera(
207 : currentType === 'perspective' ? 'orthographic' : 'perspective'
208 : )
209 0 : load3d.toggleCamera(currentType)
210 :
211 0 : load3d.getControlsManager().controls.update()
212 : }
|