-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathAssetPreview.tsx
More file actions
137 lines (119 loc) · 3.92 KB
/
Copy pathAssetPreview.tsx
File metadata and controls
137 lines (119 loc) · 3.92 KB
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
import { useCallback, useMemo, useState } from 'react'
import { PreviewCamera, PreviewProjection } from '@dcl/schemas'
import cx from 'classnames'
import { WearablePreview } from 'decentraland-ui'
import { AiFillSound } from 'react-icons/ai'
import { IoVideocamOutline } from 'react-icons/io5'
import { FaFile } from 'react-icons/fa'
import { toEmoteWithBlobs, toWearableWithBlobs } from './utils'
import { Props } from './types'
import './AssetPreview.css'
import { useRef } from 'react'
const WIDTH = 300
const HEIGHT = 300
export function AssetPreview({ value, resources, onScreenshot, onLoad, isEmote }: Props) {
const preview = useMemo(() => {
const ext = value.name.split('.').pop()
switch (ext) {
case 'gltf':
case 'glb':
return (
<GltfPreview
value={value}
resources={resources}
onScreenshot={onScreenshot}
onLoad={onLoad}
isEmote={isEmote}
/>
)
case 'png':
case 'jpg':
case 'jpeg':
return <PngPreview value={value} onScreenshot={onScreenshot} onLoad={onLoad} />
case 'mp3':
case 'wav':
case 'ogg':
return <AiFillSound />
case 'mp4':
return <IoVideocamOutline />
default:
return <FaFile />
}
}, [])
return <div className="AssetPreview">{preview}</div>
}
function GltfPreview({ value, resources, onScreenshot, onLoad, isEmote }: Props) {
const [loading, setLoading] = useState(true)
const handleScreenshot = useCallback(
(screenshot: string) => {
setTimeout(() => {
onScreenshot(screenshot)
setLoading(false)
}, 1000)
},
[onScreenshot]
)
const handleLoad = useCallback(async () => {
onLoad?.()
const wp = WearablePreview.createController(value.name)
if (isEmote) {
const length = await wp.emote.getLength()
//takes a screenshot at the middle of the emote
void wp.emote.goTo(length * 0.5).then(() => {
void wp.scene.getScreenshot(WIDTH, HEIGHT).then(handleScreenshot)
})
} else {
void wp.scene.getScreenshot(WIDTH, HEIGHT).then(handleScreenshot)
}
}, [onLoad, value, isEmote, handleScreenshot])
const wearablePreviewExtraOptions = isEmote
? {
profile: 'default',
disableFace: true,
disableDefaultWearables: true,
skin: '000000',
wheelZoom: 2
}
: {}
return (
<>
<div className={cx('GltfPreview', { hidden: loading })}>
<WearablePreview
id={value.name}
blob={isEmote ? toEmoteWithBlobs(value, resources) : toWearableWithBlobs(value, resources)}
disableAutoRotate
disableBackground
{...wearablePreviewExtraOptions}
projection={PreviewProjection.ORTHOGRAPHIC}
camera={PreviewCamera.STATIC}
onLoad={handleLoad}
/>
</div>
</>
)
}
function PngPreview({ value, onScreenshot, onLoad }: Props) {
const canvasRef = useRef<HTMLCanvasElement>(null)
const url = URL.createObjectURL(value)
const img = new Image(WIDTH, HEIGHT)
img.src = url
img.onload = () => {
onLoad?.()
const canvas = canvasRef.current
const ctx = canvasRef.current?.getContext('2d')
const canvas2 = document.createElement('canvas')
const ctx2 = canvas2.getContext('2d')
if (canvas && ctx && ctx2) {
canvas.height = canvas.width * (img.height / img.width)
canvas2.width = img.width * 0.5
canvas2.height = img.height * 0.5
ctx2.drawImage(img, 0, 0, canvas2.width, canvas2.height)
ctx2.drawImage(canvas2, 0, 0, canvas2.width * 0.5, canvas2.height * 0.5)
ctx.drawImage(canvas2, 0, 0, canvas2.width * 0.5, canvas2.height * 0.5, 0, 0, canvas.width, canvas.height)
ctx.drawImage(img, 0, 0, WIDTH, HEIGHT)
onScreenshot(canvas.toDataURL('image/png'))
canvas2.remove()
}
}
return <canvas ref={canvasRef} id="asset-png-preview" touch-action="none"></canvas>
}