File size: 6,682 Bytes
1e3b872
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import { app } from '../../../scripts/app.js'
import { api } from '../../../scripts/api.js'
import { $el } from '../../../scripts/ui.js'


function get_position_style (ctx, widget_width, y, node_height) {
  const MARGIN = 14 // the margin around the html element

  /* Create a transform that deals with all the scrolling and zooming */
  const elRect = ctx.canvas.getBoundingClientRect()
  const transform = new DOMMatrix()
    .scaleSelf(
      elRect.width / ctx.canvas.width,
      elRect.height / ctx.canvas.height
    )
    .multiplySelf(ctx.getTransform())
    .translateSelf(MARGIN, MARGIN + y)

  return {
    transformOrigin: '0 0',
    transform: transform,
    left: `0`,
    top: `0`,
    cursor: 'pointer',
    position: 'absolute',
    maxWidth: `${widget_width - MARGIN * 2}px`,
    // maxHeight: `${node_height - MARGIN * 2}px`, // we're assuming we have the whole height of the node
    width: `${widget_width - MARGIN * 2}px`,
    // height: `${node_height * 0.3 - MARGIN * 2}px`,
    // background: '#EEEEEE',
    // outline: '1px solid red',
    display: 'flex',
    flexDirection: 'column',
    // alignItems: 'center',
    justifyContent: 'space-around'
  }
}


app.registerExtension({
  name: 'Mixlab.3D.SaveTripoSRMesh',
  async beforeRegisterNodeDef (nodeType, nodeData, app) {
    if (nodeType.comfyClass == 'SaveTripoSRMesh') {
      const orig_nodeCreated = nodeType.prototype.onNodeCreated
      nodeType.prototype.onNodeCreated = async function () {
        orig_nodeCreated?.apply(this, arguments)

        const widget = {
          type: 'div',
          name: 'preview',
          draw (ctx, node, widget_width, y, widget_height) {
            Object.assign(
              this.div.style,
              get_position_style(ctx, widget_width, 88, node.size[1])
            )
          }
          //   value: [],
          //   async serializeValue (nodeId, widgetIndex) {
          //     return widget.value
          //   }
        }

        widget.div = $el('div', {})
        widget.div.style.width = `120px`

        document.body.appendChild(widget.div)

        // preview.style = `margin-top: 12px;display: flex;
        //   justify-content: center;
        //   align-items: center;background-repeat: no-repeat;background-size: contain;`

        this.addCustomWidget(widget)

        const onResize = this.onResize
        this.onResize = () => {
          widget.div.style.width = `${this.size[0]}px`
          widget.div.style.height = `${this.size[1] - 112}px`
          let mvs = widget.div.querySelectorAll('model-viewer')
          for (const m of mvs) {
            m.style.height = `${Math.round(
              (this.size[1] - 112) / mvs.length
            )}px`
            // console.log(m.style.height)
          }
          //   console.log('resize', this.size)
          return onResize?.apply(this, arguments)
        }

        const onRemoved = this.onRemoved
        this.onRemoved = () => {
          widget.div.remove()
          return onRemoved?.()
        }

        if (this.onResize) {
          this.onResize(this.size)
        }
        // this.isVirtualNode = true
        this.serialize_widgets = false //需要保存参数
      }

      const onExecuted = nodeType.prototype.onExecuted
      nodeType.prototype.onExecuted = function (message) {
        const r = onExecuted?.apply?.(this, arguments)

        let widget = this.widgets.filter(d => d.name == 'preview')[0]
        console.log('Test', widget, message)

        let meshes = message.mesh
        widget.div.innerHTML = ''

        for (const mesh of meshes) {
          if (mesh) {
            const { filename, subfolder, type } = mesh
            const fileURL = api.apiURL(
              `/view?filename=${encodeURIComponent(
                filename
              )}&type=${type}&subfolder=${subfolder}${app.getPreviewFormatParam()}${app.getRandParam()}`
            )

            let modelViewer = document.createElement('div')
            modelViewer.innerHTML = `<model-viewer  src="${fileURL}" 
              min-field-of-view="0deg" max-field-of-view="180deg"
              shadow-intensity="1" 
              camera-controls 
              touch-action="pan-y"
                  style="width:100%;margin:4px;min-height:88px"
              >
              
              <div class="controls">
            
              <div><button class="export" style="
              background-color: var(--comfy-input-bg);
              border-radius: 8px;
              border-color: var(--border-color);
              border-style: solid;
              color: var(--descrip-text);cursor: pointer;">Export GLB</button></div>
              
              </div></model-viewer>`
            widget.div.appendChild(modelViewer)
            let modelViewerVariants= modelViewer
            .querySelector('model-viewer');
            
            modelViewer
              .querySelector('.export')
              .addEventListener('click', async e => {
                e.preventDefault()
                const glTF = await modelViewerVariants.exportScene()
                const file = new File([glTF], filename)
                const link = document.createElement('a')
                link.download = file.name
                link.href = URL.createObjectURL(file)
                link.click()
              })
          }
        }

        // widget.value = [meshes]

        this.onResize?.(this.size)

        return r
      }
    }
  },
  async loadedGraphNode (node, app) {
    const sleep = (t = 1000) => {
      return new Promise((res, rej) => {
        setTimeout(() => res(1), t)
      })
    }
    // if (node.type === 'SaveTripoSRMesh') {
    //   await sleep(0)
    //   let widget = node.widgets.filter(w => w.name === 'preview')[0]
    //   widget.div.innerHTML = ''

    //   for (const mesh of widget.value) {
    //     if (mesh) {
    //       const { filename, subfolder, type } = mesh
    //       const fileURL = api.apiURL(
    //         `/view?filename=${encodeURIComponent(
    //           filename
    //         )}&type=${type}&subfolder=${subfolder}${app.getPreviewFormatParam()}${app.getRandParam()}`
    //       )

    //       let modelViewer = document.createElement('div')
    //       modelViewer.innerHTML = `<model-viewer  src="${fileURL}"
    // 	 min-field-of-view="0deg" max-field-of-view="180deg"
    // 	 shadow-intensity="1"
    // 	 camera-controls
    // 	 touch-action="pan-y">

    // 	 <div class="controls">

    // 	  <div><button class="export">Export GLB</button></div>

    // 	</div></model-viewer>`
    //       widget.div.appendChild(modelViewer)
    //     }
    //   }
    // }
  }
})