-
Notifications
You must be signed in to change notification settings - Fork 173
/
Copy pathviewer-utils.ts
391 lines (330 loc) · 10.1 KB
/
viewer-utils.ts
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/**
* @file Viewer Utils
* @author Alexander Rose <alexander.rose@weirdbyte.de>
* @private
*/
import {
Vector2, Vector3, Matrix4, Points, Scene, Camera,
Object3D, WebGLRenderer
} from 'three'
import { createParams } from '../utils'
import TiledRenderer from './tiled-renderer'
import { quicksortCmp } from '../math/array-utils'
import Viewer from './viewer'
function _trimCanvas (canvas: HTMLCanvasElement, r: number, g: number, b: number, a: number) {
const canvasHeight = canvas.height
const canvasWidth = canvas.width
const ctx = canvas.getContext('2d')!
const pixels = ctx.getImageData(0, 0, canvasWidth, canvasHeight).data
let x, y, doBreak, off
doBreak = false
for (y = 0; y < canvasHeight; y++) {
for (x = 0; x < canvasWidth; x++) {
off = (y * canvasWidth + x) * 4
if (pixels[ off ] !== r || pixels[ off + 1 ] !== g ||
pixels[ off + 2 ] !== b || pixels[ off + 3 ] !== a
) {
doBreak = true
break
}
}
if (doBreak) {
break
}
}
const topY = y
doBreak = false
for (x = 0; x < canvasWidth; x++) {
for (y = 0; y < canvasHeight; y++) {
off = (y * canvasWidth + x) * 4
if (pixels[ off ] !== r || pixels[ off + 1 ] !== g ||
pixels[ off + 2 ] !== b || pixels[ off + 3 ] !== a
) {
doBreak = true
break
}
}
if (doBreak) {
break
}
}
const topX = x
doBreak = false
for (y = canvasHeight - 1; y >= 0; y--) {
for (x = canvasWidth - 1; x >= 0; x--) {
off = (y * canvasWidth + x) * 4
if (pixels[ off ] !== r || pixels[ off + 1 ] !== g ||
pixels[ off + 2 ] !== b || pixels[ off + 3 ] !== a
) {
doBreak = true
break
}
}
if (doBreak) {
break
}
}
const bottomY = y
doBreak = false
for (x = canvasWidth - 1; x >= 0; x--) {
for (y = canvasHeight - 1; y >= 0; y--) {
off = (y * canvasWidth + x) * 4
if (pixels[ off ] !== r || pixels[ off + 1 ] !== g ||
pixels[ off + 2 ] !== b || pixels[ off + 3 ] !== a
) {
doBreak = true
break
}
}
if (doBreak) {
break
}
}
const bottomX = x
const trimedCanvas = document.createElement('canvas')
trimedCanvas.width = bottomX - topX
trimedCanvas.height = bottomY - topY
const trimedCtx = trimedCanvas.getContext('2d')!
trimedCtx.drawImage(
canvas,
topX, topY,
trimedCanvas.width, trimedCanvas.height,
0, 0,
trimedCanvas.width, trimedCanvas.height
)
return trimedCanvas
}
/**
* Image parameter object.
* @typedef {Object} ImageParameters - image generation parameters
* @property {Boolean} trim - trim the image
* @property {Integer} factor - scaling factor to apply to the viewer canvas
* @property {Boolean} antialias - antialias the image
* @property {Boolean} transparent - transparent image background
*/
const ImageDefaultParameters = {
trim: false,
factor: 1,
antialias: false,
transparent: false,
onProgress: undefined as Function|undefined
}
export type ImageParameters = typeof ImageDefaultParameters
/**
* Make image from what is shown in a viewer canvas
* @param {Viewer} viewer - the viewer
* @param {ImageParameters} params - parameters object
* @return {Promise} A Promise object that resolves to an image {@link Blob}.
*/
export function makeImage (viewer: Viewer, params: Partial<ImageParameters> = {}) {
const {trim, factor, antialias, transparent} = createParams(params, ImageDefaultParameters)
const renderer = viewer.renderer
const camera = viewer.camera
const originalClearAlpha = renderer.getClearAlpha()
const backgroundColor = renderer.getClearColor()
function setLineWidthAndPixelSize (invert = false) {
let _factor = factor
if (antialias) _factor *= 2
if (invert) _factor = 1 / _factor
viewer.scene.traverse(function (o: any) { // TODO
const m = o.material
if (m && m.linewidth) {
m.linewidth *= _factor
}
if (m && m.uniforms && m.uniforms.size) {
if (m.uniforms.size.__seen === undefined) {
m.uniforms.size.value *= _factor
m.uniforms.size.__seen = true
}
}
if (m && m.uniforms && m.uniforms.linewidth) {
if (m.uniforms.linewidth.__seen === undefined) {
m.uniforms.linewidth.value *= _factor
m.uniforms.linewidth.__seen = true
}
}
})
viewer.scene.traverse(function (o: any) { // TODO
const m = o.material
if (m && m.uniforms && m.uniforms.size) {
delete m.uniforms.size.__seen
}
if (m && m.uniforms && m.uniforms.linewidth) {
delete m.uniforms.linewidth.__seen
}
})
}
function trimCanvas (canvas: HTMLCanvasElement) {
if (trim) {
const bg = backgroundColor
const r = transparent ? 0 : bg.r * 255
const g = transparent ? 0 : bg.g * 255
const b = transparent ? 0 : bg.b * 255
const a = transparent ? 0 : 255
return _trimCanvas(canvas, r, g, b, a)
} else {
return canvas
}
}
function onProgress (i: number, n: number, finished: boolean) {
if (typeof params.onProgress === 'function') {
params.onProgress(i, n, finished)
}
}
return new Promise<Blob>(function (resolve, reject) {
const tiledRenderer = new TiledRenderer(
renderer, camera, viewer,
{ factor, antialias, onProgress, onFinish }
)
renderer.setClearAlpha(transparent ? 0 : 1)
setLineWidthAndPixelSize()
tiledRenderer.renderAsync()
function onFinish (i: number, n: number) {
const canvas = trimCanvas(tiledRenderer.canvas)
canvas.toBlob(
function (blob) {
renderer.setClearAlpha(originalClearAlpha)
setLineWidthAndPixelSize(true)
viewer.requestRender()
onProgress(n, n, true)
if (blob) {
resolve(blob)
} else {
reject('error creating image')
}
},
'image/png'
)
}
})
}
const vertex = new Vector3()
const matrix = new Matrix4()
const modelViewProjectionMatrix = new Matrix4()
export function sortProjectedPosition (scene: Scene, camera: Camera) {
// console.time( "sort" );
scene.traverseVisible(function (o) {
if (!(o instanceof Points) || !o.userData.buffer.parameters.sortParticles) {
return
}
const attributes = (o.geometry as any).attributes // TODO
const n = attributes.position.count
if (n === 0) return
matrix.multiplyMatrices(
camera.matrixWorldInverse, o.matrixWorld
)
modelViewProjectionMatrix.multiplyMatrices(
camera.projectionMatrix, matrix
)
let sortData, sortArray, zArray: Float32Array, cmpFn
if (!o.userData.sortData) {
zArray = new Float32Array(n)
sortArray = new Uint32Array(n)
cmpFn = function (ai: number, bi: number) {
const a = zArray[ ai ]
const b = zArray[ bi ]
if (a > b) return 1
if (a < b) return -1
return 0
}
sortData = {
__zArray: zArray,
__sortArray: sortArray,
__cmpFn: cmpFn
}
o.userData.sortData = sortData
} else {
sortData = o.userData.sortData
zArray = sortData.__zArray
sortArray = sortData.__sortArray
cmpFn = sortData.__cmpFn
}
for (let i = 0; i < n; ++i) {
vertex.fromArray(attributes.position.array, i * 3)
vertex.applyMatrix4(modelViewProjectionMatrix)
// negate, so that sorting order is reversed
zArray[ i ] = -vertex.z
sortArray[ i ] = i
}
quicksortCmp(sortArray, cmpFn)
let index, indexSrc, indexDst, tmpTab
for (let name in attributes) {
const attr = attributes[ name ]
const array = attr.array
const itemSize = attr.itemSize
if (!sortData[ name ]) {
sortData[ name ] = new Float32Array(itemSize * n)
}
tmpTab = sortData[ name ]
sortData[ name ] = array
for (let i = 0; i < n; ++i) {
index = sortArray[ i ]
for (let j = 0; j < itemSize; ++j) {
indexSrc = index * itemSize + j
indexDst = i * itemSize + j
tmpTab[ indexDst ] = array[ indexSrc ]
}
}
attributes[ name ].array = tmpTab
attributes[ name ].needsUpdate = true
}
})
// console.timeEnd( "sort" );
}
const resolution = new Vector2()
const projectionMatrixInverse = new Matrix4()
const projectionMatrixTranspose = new Matrix4()
export function updateMaterialUniforms (group: Object3D, camera: Camera, renderer: WebGLRenderer, cDist: number, bRadius: number) {
const {width, height} = renderer.getSize()
const canvasHeight = height
const pixelRatio = renderer.getPixelRatio()
const ortho = camera.type === 'OrthographicCamera'
resolution.set(width, height)
projectionMatrixInverse.getInverse(camera.projectionMatrix)
projectionMatrixTranspose.copy(camera.projectionMatrix).transpose()
group.traverse(function (o: any) {
const m = o.material
if (!m) return
const u = m.uniforms
if (!u) return
if (m.clipNear) {
const nearFactor = (50 - m.clipNear) / 50
const nearClip = cDist - (bRadius * nearFactor)
u.clipNear.value = nearClip
}
if (u.canvasHeight) {
u.canvasHeight.value = canvasHeight
}
if (u.resolution) {
u.resolution.value.copy(resolution)
}
if (u.pixelRatio) {
u.pixelRatio.value = pixelRatio
}
if (u.projectionMatrixInverse) {
u.projectionMatrixInverse.value.copy(projectionMatrixInverse)
}
if (u.projectionMatrixTranspose) {
u.projectionMatrixTranspose.value.copy(projectionMatrixTranspose)
}
if (u.ortho) {
u.ortho.value = ortho
}
})
}
export function updateCameraUniforms (group: Object3D, camera: Camera) {
projectionMatrixInverse.getInverse(camera.projectionMatrix)
projectionMatrixTranspose.copy(camera.projectionMatrix).transpose()
group.traverse(function (o: any) {
const m = o.material
if (!m) return
const u = m.uniforms
if (!u) return
if (u.projectionMatrixInverse) {
u.projectionMatrixInverse.value.copy(projectionMatrixInverse)
}
if (u.projectionMatrixTranspose) {
u.projectionMatrixTranspose.value.copy(projectionMatrixTranspose)
}
})
}