Skip to content

Instantly share code, notes, and snippets.

View banjun's full-sized avatar

banjun banjun

View GitHub Profile
@banjun
banjun / IKDirectMeshBufferCapture.swift
Last active November 26, 2026 14:43
Get RealityKit IK (inverse kinematics) results
import Foundation
import RealityKit
import Metal
import Synchronization
// Purpose: get RealityKit IK (inverse kinematics) results
// Current result: got joint transforms affected by the IK
//
// How it works:
// hook -[MTLDebugDevice newBufferWithLength:options:]
@banjun
banjun / IKHookSkeletalPoseBuffer.swift
Created November 26, 2026 07:08
Get RealityKit IK (inverse kinematics) results
import Foundation
import Metal
import ObjectiveC.runtime
import Synchronization
// Purpose: get RealityKit IK (inverse kinematics) results
// Current result: got joint transforms affected by the IK
//
// How it works:
// hook -[MTLDebugDevice newBufferWithBytes:length:options:],
import RealityKit
import Metal
/// store splat data even for Simulator, for any urther use case.
/// for devices, use `GaussianSplatComponent.init(CompatibleGaussianSplatResourceComponent)`
/// ```swift
/// let c = CompatibleGaussianSplatResourceComponent(...buffers...)
/// entity.components.set(c)
/// #if targetEnvironment(simulator)
/// print("skipping for Simulator: `entity.components.set(try GaussianSplatComponent(c))`")
@banjun
banjun / ContentView.swift
Created July 28, 2026 12:43
RealityKit ShaderGraph Node Investigation
import SwiftUI
import RealityKit
struct ContentView: View {
@State private var defs: [ShaderGraph.NodeDefinition] = []
@State private var version: ShaderGraph.NodeLibrary.Version = .default
@State private var searchText: String = ""
@State private var platform: ShaderGraph.NodeDefinition.Platform = .visionOS
@State private var osVersion: Int = 27
@State private var groups: [String] = []
@banjun
banjun / visionOS27ViewAttachmentFix.swift
Created June 29, 2026 08:58
visionOS27ViewAttachmentFix.swift
extension Entity {
/// NOTE: for visionOS 27+, sometimes additional stimulation is needed for the attachment to be valid.
/// it might be needed to be called after re-enabled or delayed being added to a scene, i.e. isEnabled = true or parent.addChild(view).
func visionOS27ViewAttachmentFix() async {
guard #available(visionOS 27, *) else { return }
try? await Task.sleep(for: .seconds(0.1))
let dummy = ViewAttachmentEntity(components: [ViewAttachmentComponent(rootView: EmptyView())])
addChild(dummy)
try? await Task.sleep(for: .seconds(0.1))
dummy.removeFromParent()
@banjun
banjun / RemoteVisionOSARKitLayer.swift
Created July 16, 2025 08:12
Remote visionOS CompositorLayer with required ARKit session enabled for RemoteImmersiveSpace
// copied from Apple Sample Codes: CS_HoverEffect https://developer.apple.com/documentation/compositorservices/rendering_hover_effects_in_metal_immersive_apps
// and modified by banjun
#if os(macOS)
import CompositorServices
import SwiftUI
/// Usage:
/// ```swift
/// RemoteVisionOSARKitLayer { renderer, worldTracker in
@banjun
banjun / RenderTextureScene.swift
Last active December 26, 2025 12:51
RealityKit wrapper for RenderTexture like scene, with supports for multiple cameras
import Foundation
import RealityKit
import Metal
///
/// ```swift
/// renderTextureScene.entities.append(rootEntity.clone(recursive: true))
/// let renderTextureMaterial = UnlitMaterial(texture: renderTextureScene.textureResources[0])
/// ModelEntity(mesh: .generatePlane(width: 1, height: 1), materials: [renderTextureMaterial])
/// ...
@banjun
banjun / SwiftUIObservationLeaks.swift
Created February 4, 2025 04:27
SwiftUI & Observation memory leaks
import SwiftUI
import Observation
// Leaks Malloc 48 Bytes block, 144 Bytes block, 128 Bytes block on macOS 15.3 (24D60), Xcode 16.2 (16C5032a)
// leaks speed is ~ +10MB / 5 mins, with Instruments
@main
struct SwiftUIObservationLeaksApp: App {
@State private var model: Model = .init()
@MainActor @Observable final class Model {
extension ModelEntity {
/// create example model with skeleton: https://openusd.org/dev/api/_usd_skel__schema_overview.html
static func createUsdSkelExample() async throws -> ModelEntity {
var d = MeshDescriptor()
d.positions = .init([
.init(0.5, -0.5, 4), .init(-0.5, -0.5, 4), .init(0.5, 0.5, 4), .init(-0.5, 0.5, 4),
.init(-0.5, -0.5, 0), .init(0.5, -0.5, 0), .init(-0.5, 0.5, 0), .init(0.5, 0.5, 0),
.init(-0.5, 0.5, 2), .init(0.5, 0.5, 2), .init(0.5, -0.5, 2), .init(-0.5, -0.5, 2)])
d.primitives = .trianglesAndQuads(triangles: [], quads: [
2, 3, 1, 0,
import SwiftUI
import RealityKit
@main
struct KibouApp: App {
@Environment(\.scenePhase) private var scenePhase
@State private var transform: Transform = .identity
@GestureState private var dragState: SIMD3<Float>?
var body: some SwiftUI.Scene {