Skip to content

nhiroyasu/SwiftGLTF

Repository files navigation

SwiftGLTF

Swift

A project that enables using glTF files in Swift.

preview1 preview2 preview1

Features

SwiftGLTF offers:

  • High-performance rendering fully powered by Metal
  • Lightweight and modular design, easy to integrate into any Swift project
  • Wide glTF specification support, covering materials, animations, cameras, and more
  • Optimized GPU resource management, enabling fast and efficient rendering on Apple platforms

Performance

SwiftGLTF has been tested using the official NodePerformanceTest model from the glTF Sample Assets, demonstrating the following performance results: This model contains:

  • 10,000 nodes
  • 10,000 meshes / primitives / materials
  • 100 textures with randomized usage

Despite the extremely high node and material count, SwiftGLTF is capable of:

  • Maintaining a stable 60 FPS during rendering on supported Apple platforms

MacBookPro (M1 Pro)

swiftgltf_performance_test_mac.mov

iPhone16 (A18)

swiftgltf_performance_test_iphone.mov

Usage

Platform

  • iOS 16.0+
  • macOS 13.0+
  • Metal 3 & 4

Installation

Swift Package Manager

dependencies: [
    .package(url: "https://github.com/nhiroyasu/SwiftGLTF.git", branch: "main")
]

Sample Code

UIKit

import SwiftGLTF

let gltfUrl = // URL to your glTF or GLB file
let gltfView = GLTFView(frame: view.frame)
await gltfView.load(gltf: gltfUrl)
view.addSubview(gltfView)

SwiftUI

import SwiftGLTF

var body: some View {
    @State private var gltfUrl = // URL to your glTF or GLB file
    GLTFSwiftUIView(url: gltfUrl)
}

Supported glTF Features

  • Unsupported features are planned to be added in future updates.

File Formats

Format Supported
glTF Binary (.glb)
glTF JSON (.gltf)

Buffer Formats

Format Supported
External .bin file
Embedded (data URI in .gltf)

Image Formats

Format Supported
PNG
JPEG
KTX2

Mesh Compression

Extension Supported
KHR_draco_mesh_compression

PBR Materials (metallic-roughness)

Property Supported
baseColorFactor
baseColorTexture
metallicFactor
roughnessFactor
metallicRoughnessTexture

Additional Material Properties

Property Supported
normalTexture
occlusionTexture
emissiveTexture
emissiveFactor
alphaMode
alphaCutoff
doubleSided
extensions ✅ (see blow)

Supported Extensions for Materials

  • KHR_materials_transmission
  • KHR_materials_volume
  • KHR_materials_ior
  • KHR_materials_clearcoat
  • KHR_materials_specular
  • KHR_materials_sheen
  • KHR_materials_unlit
  • KHR_materials_variants
  • KHR_texture_transform

Vertex Attributes

Attribute Supported
POSITION
NORMAL
TANGENT
TEXCOORD_0
TEXCOORD_1
COLOR_0
JOINTS_0
WEIGHTS_0

Node Hierarchy and Transforms

Feature Supported
Node hierarchy
matrix (4x4 transform matrix)
translation / rotation / scale (TRS)

Animation

Channel Supported
translation
rotation
scale
morph target weights

Scenes

Feature Supported
Multiple scenes

Cameras

Feature Supported
Perspective
Orthographic

Build

Sample Project

  • You can build the sample project by opening SwiftGLTFSample.xcodeproj.

Project Structure

SwiftGLTF

  • Provides GLTFView and GLTFMetalView components to display glTF files in UIKit and SwiftUI views.

SwiftGLTFRenderer

  • A library for rendering glTF files using Metal.

SwiftGLTFParser

  • A library that parses glTF and converts it into MDLAsset for use in Swift.

SwiftGLTFCore

  • A library that defines the core data structures of glTF.

MikkTSpace

About

Project to make 3D models from glTF files available in Swift and renderable in Metal

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors