-
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathThumbnailProvider.swift
More file actions
37 lines (29 loc) · 1.4 KB
/
ThumbnailProvider.swift
File metadata and controls
37 lines (29 loc) · 1.4 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
import Cocoa
import QuickLookThumbnailing
import SceneKit
import GLTFSceneKit
class ThumbnailProvider: QLThumbnailProvider {
override func provideThumbnail(for request: QLFileThumbnailRequest, _ handler: @escaping (QLThumbnailReply?, Error?) -> Void) {
let size = request.maximumSize
// Use a QLThumbnailReply with drawing context to correctly handle scaling
let reply = QLThumbnailReply(contextSize: size) { () -> Bool in
guard let context = NSGraphicsContext.current?.cgContext else { return false }
do {
let source = try GLTFSceneSource(url: request.fileURL)
let scene = try source.scene()
let renderer = SCNRenderer(device: nil, options: nil)
renderer.scene = scene
renderer.autoenablesDefaultLighting = true
let image = renderer.snapshot(atTime: 0.0, with: size, antialiasingMode: .multisampling4X)
if let cgImage = image.cgImage(forProposedRect: nil, context: nil, hints: nil) {
context.draw(cgImage, in: CGRect(origin: .zero, size: size))
return true
}
} catch {
print("Error generating thumbnail: \(error)")
}
return false
}
handler(reply, nil)
}
}