Pixar 的通用场景描述,适用于 swift 和开源元宇宙


contributors discord youtube

重要提示

支持 SwiftUSD ❤️

如果您觉得 SwiftUSD 有用,请考虑成为赞助者来支持我。 我花费了大量时间致力于开源项目,每一份赞助都帮助我投入更多时间为社区制作高质量的工具和软件。


Swift 化元宇宙

通用场景描述 (USD) 是一个高效、可扩展的系统,用于创作、读取和流式传输时间采样的场景描述,以便在图形应用程序之间进行交换。

有关更多详细信息,请访问网站此处

要在 swift 中使用 Pixar 的 USD,请将 SwiftUSD 作为包依赖项添加到项目的 Package.swift 文件中。
dependencies: [
  .package(url: "https://github.com/wabiverse/SwiftUSD.git", from: "24.8.13"),
]
然后,对于您想要的任何目标,添加整体的 USD Pixar 产品作为目标依赖项,这是一个完整的示例。
// swift-tools-version: 5.10
import PackageDescription

let package = Package(
  name: "MyMetaversePackage",
  platforms: [
    .macOS(.v14),
    .visionOS(.v1),
    .iOS(.v17),
    .tvOS(.v17),
    .watchOS(.v10)
  ],
  // --- 📦 Package Products. ---
  products: [
    .library(
      name: "MyMetaverseLibrary",
      targets: ["MyMetaverseLibrary"]
    ),
    .executable(
      name: "MyMetaverseApp",
      targets: ["MyMetaverseApp"]
    ),
  ],
  dependencies: [
    .package(url: "https://github.com/wabiverse/SwiftUSD.git", from: "24.8.13")
  ],
  targets: [
    /* 📕 For library products... */
    .target(
      name: "MyMetaverseLibrary",
      dependencies: [
        /* add pixar usd as a library dependency. */
        .product(name: "PixarUSD", package: "SwiftUSD"),
      ],
      cxxSettings: [
        /* for windows support, add these three defines until swift's clang is updated. */
        .define("_ALLOW_COMPILER_AND_STL_VERSION_MISMATCH", .when(platforms: [.windows])),
        .define("_ALLOW_KEYWORD_MACROS", to: "1", .when(platforms: [.windows])),
        .define("static_assert(_conditional, ...)", to: "", .when(platforms: [.windows])),
      ],
      swiftSettings: [
        /* enable swift/c++ interop. */
        .interoperabilityMode(.Cxx)
      ]
    ),

    /* 📗 Or executable products... */
    .executableTarget(
      name: "MyMetaverseApp",
      dependencies: [
        /* add pixar usd as an executable dependency. */
        .product(name: "PixarUSD", package: "SwiftUSD"),
      ],
      cxxSettings: [
        /* for windows support, add these three defines until swift's clang is updated. */
        .define("_ALLOW_COMPILER_AND_STL_VERSION_MISMATCH", .when(platforms: [.windows])),
        .define("_ALLOW_KEYWORD_MACROS", to: "1", .when(platforms: [.windows])),
        .define("static_assert(_conditional, ...)", to: "", .when(platforms: [.windows])),
      ],
      swiftSettings: [
        /* enable swift/c++ interop. */
        .interoperabilityMode(.Cxx)
      ],
      plugins: [
        /* 📙 And, plugins are added like so. */
        .plugin(name: "UsdGenSchemaPlugin", package: "SwiftUSD")
      ]
    ),
  ],
  /* use cxx17 language standard. */
  cxxLanguageStandard: .cxx17
)
@@ Dependency Notes @@
- Library Products allow clients that declare a dependency on this package to use the package’s functionality.
+ Executable Products vend an executable target. Use this only if you want to make the executable available to clients.
! Plugin Products vend plugin targets. This makes the plugin available to clients that integrate the Swift package.
# Swift's package manager, SwiftPM, is capabable of building Swift, Objective-C/C++, and C/C++ code.
最后,创作场景描述,这是一个使用 swift 创建带有变换和球体的新 USD 舞台的工作示例。
import Foundation
import PixarUSD

@main
enum Creator
{
  static func main()
  {
    /* Setup all usd resources (python, plugins, resources). */

    Pixar.Bundler.shared.setup(.resources)

    /* Create a new USD stage with a transform and a sphere. */

    let stage = Usd.Stage.createNew("HelloPixarUSD.usda")

    UsdGeom.Xform.define(stage, path: "/Hello")
    UsdGeom.Sphere.define(stage, path: "/Hello/World")

    stage.getPseudoRoot().set(doc: "Hello World Example (Swift)!")

    stage.save()
  }
}
或者,如果您更喜欢那些 Swift 化的声明式 API...
import Foundation
import PixarUSD

@main
enum Creator
{
  static func main()
  {
    /* Setup all usd resources (python, plugins, resources). */

    Pixar.Bundler.shared.setup(.resources)

    /* Create a new USD stage with a transform and a sphere. */

    USDStage("HelloPixarUSD", ext: .usda)
    {
      USDPrim("Hello", type: .xform)
      {
        USDPrim("World", type: .sphere)
      }
    }
    .set(doc: "Stay Swifty.")
    .save()
  }
}

UsdView(正在开发中)

此项目中的 UsdView 应用程序完全用 Swift 编写,并且是 Swift/C++ 互操作以及在 Swift 中创建跨平台 UI 应用程序(利用 SwiftCrossUI)的生动示例。

Linux

重要提示

bundler 目前正在开发中,以支持在 Linux 上捆绑应用程序,在此之前,请按照这些说明在 Linux 上运行 UsdView

git clone https://github.com/wabiverse/SwiftUSD
cd SwiftUSD

swift run -c release UsdView

Apple 设备 (macOS, visionOS, iOS)

提示

通过在终端中运行以下命令,在本地安装 bundler

git clone https://github.com/stackotter/swift-bundler
cd swift-bundler

swift build -c release
sudo cp .build/release/swift-bundler /usr/local/bin/

最后,要使用本地安装的 bundler 运行和捆绑 UsdView 或任何其他应用程序(例如您自己的应用程序!),请运行以下命令

git clone https://github.com/wabiverse/SwiftUSD
cd SwiftUSD

macOS

macOS 上运行和捆绑 UsdView

swift bundler run -c release UsdView

visionOSiOS

visionOSiOS 上运行和捆绑 UsdView

# list available iOS and visionOS simulators.
swift bundler simulators

# boot a simulator from the list.
swift bundler simulators boot [id-of-device]

# if you booted a visionOS device.
swift bundler run -p visionOSSimulator -c release UsdView

# if you booted a iOS device.
swift bundler run -p iOSSimulator -c release UsdView

OH SH!

您甚至可以在一个微小的脚本中运行它,无需任何包,导入语句旁边的注释描述了要从中拉取的 git 源,所有传递链接的内容都将飞速而来 - 查看 swift-sh

重要提示

您需要使用 Wabi swift-sh 分支才能使其与 Swift 5.9 cxx 互操作一起工作。

image


注意

Swift 是一种开源编程语言,在 Linux服务器端 Swift、整个 Apple 设备系列:macOSvisionOSiOStvOSwatchOS 以及对 Microsoft Windows 的支持中都得到完全支持。 要了解有关 Swift 的更多信息,请访问 swift.org



如果您可以 mmap 一个像素,您就可以 mmap 一个元宇宙
SwiftUSD 根据 Tomorrow 开源技术 1.0 许可证 的条款获得许可。