在 swift 中生成 STL 缩略图

Generating STL thumbnails in swift

是否可以在我的应用程序中从导入的 stl 文件生成缩略图? 我有项目的集合视图,我想为添加的新项目生成缩略图。我的应用程序导入类型为 stl(标准镶嵌几何文件格式)和 .obj(几何定义文件格式)的文件

import SceneKit.ModelIO

private let device = MTLCreateSystemDefaultDevice()!

//MARK: thumbnail
/// Create a thumbnail image of the asset with the specified URL at the specified
/// animation time. Supports loading of .scn, .usd, .usdz, .obj, and .abc files,
/// and other formats supported by ModelIO.
/// - Parameters:
///     - url: The file URL of the asset.
///     - size: The size (in points) at which to render the asset.
///     - time: The animation time to which the asset should be advanced before snapshotting.

func thumbnail(for url: URL, size: CGSize, time: TimeInterval = 0) -> UIImage? {
    let renderer = SCNRenderer(device: device, options: [:])
    renderer.autoenablesDefaultLighting = true

    if (url.pathExtension == "scn") {
        let scene = try? SCNScene(url: url, options: nil)
        renderer.scene = scene
    } else {
        let asset = MDLAsset(url: url)
        let scene = SCNScene(mdlAsset: asset)
        renderer.scene = scene
    }

    let image = renderer.snapshot(atTime: time, with: size, antialiasingMode: .multisampling4X)
    return image
}