【问题标题】:Generating STL thumbnails in swift快速生成 STL 缩略图
【发布时间】:2020-12-14 09:25:37
【问题描述】:

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

【问题讨论】:

  • 尝试在 SceneKit 中使用灯光和相机创建一个空的“占位符”场景。然后使用 ModelO 将文件加载到场景中并创建您的SCNView 的快照。
  • @jlsiewert 在内存方面不会非常低效
  • 这将是资源密集型的,但 stl 文件仅包含顶点列表。如果要生成图像,则需要将其导入 3D 引擎并使用引擎渲染图像。但是,在导入时只执行一次应该没问题,只需将缩略图存储在 stl 文件旁边即可。
  • @jlsiewert 我最终使用了 SCNRenderer 并拍摄了快照。它支持 ModelIO 支持的所有格式。我只是有一个问题,如果您可以在快照之前旋转对象。有什么想法吗?

标签: swift stl scenekit thumbnails


【解决方案1】:
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
}

【讨论】:

    猜你喜欢
    • 2011-07-10
    • 1970-01-01
    • 1970-01-01
    • 2017-05-22
    • 2012-05-09
    • 2013-11-23
    • 1970-01-01
    • 2012-01-22
    • 1970-01-01
    相关资源
    最近更新 更多