This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import RealityFoundation | |
/// | |
/// ```swift | |
/// renderTextureScene.entities.append(rootEntity.clone(recursive: true)) | |
/// let renderTextureMaterial = UnlitMaterial(texture: renderTextureScene.textureResources[0]) | |
/// ModelEntity(mesh: .generatePlane(width: 1, height: 1), materials: [renderTextureMaterial]) | |
/// ... | |
/// |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
extension Timer { | |
@MainActor @discardableResult static func scheduledTimerOnMainRunLoop(withTimeInterval: TimeInterval, repeats: Bool, block: @escaping @MainActor (Timer) -> Void) -> Timer { | |
// The doc says: Creates and returns a new NSTimer object initialized with the specified block object and schedules it on the current run loop | |
// But the block is not annotated with @MainActor and we need MainActor.assumeIsolated | |
// Since Timer is non-Sendable, capture by nonisolated(unsafe) to pass from nonisolated context to MainActor. | |
Timer.scheduledTimer(withTimeInterval: withTimeInterval, repeats: repeats) { timer in | |
nonisolated(unsafe) let timer: Timer = timer | |
MainActor.assumeIsolated { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import simd | |
func generatePlaneMesh(lengthInMeter: Float = 1, divisions: Int = 100, name: String = "GeneratedPlane") { | |
let n = divisions | |
let xys: [(Int, Int)] = (0..<n).flatMap { y in (0..<n).map { x in (x, y) }} | |
let points: [SIMD3<Float>] = xys.map { x, y in | |
SIMD3(Float(x) / Float(n - 1) - 0.5, Float(y) / Float(n - 1) - 0.5, 0) * lengthInMeter | |
} | |
let faceVertexIndices: [Int] = xys.flatMap { x, y in | |
guard x < n - 1, y < n - 1 else { return [Int]() } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct MultiColumnText: View { | |
private let storage: NSTextStorage | |
private let layoutManager: NSLayoutManager = .init() | |
private let containers: [NSTextContainer] | |
init(text: NSAttributedString, columns: Int) { | |
storage = .init(attributedString: text) | |
storage.addLayoutManager(layoutManager) | |
containers = (0..<columns).map {_ in .init()} | |
containers.forEach { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import RealityKit | |
extension ShapeResource { | |
/// generate a box with its surfaces facing inward. | |
/// NOTE: size 30 works with interactions, 50 does not work with interactions | |
static func generateInwardBox(size: Float) async throws -> ShapeResource { | |
// An array of vertex positions containing discrete points on the mesh. | |
let meshPositions: [SIMD3<Float>] = [ | |
.init(-1, -1, -1), // 0 LBF | |
.init(+1, -1, -1), // 1 RBF |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
// prerequisites in Info.plist: NSUserActivityTypes contains type, UIApplicationSceneManifest/UIApplicationSupportsMultipleScenes = YES | |
// accepts NSUserActivity.targetContentIdentifier = type | |
// see also: https://developer.apple.com/documentation/swiftui/scene/handlesexternalevents(matching:) | |
struct UserActivityWindowGroup<Content: View, Payload: Codable>: Scene { | |
var type: String | |
@ViewBuilder var content: (Payload) -> Content | |
init(type: String, payloadType: Payload.Type, @ViewBuilder content: @escaping (Payload) -> Content) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/zsh | |
if [[ -z $BOGO_ARM64_TO_SIM ]]; then | |
echo "❌ requires BOGO_ARM64_TO_SIM: set an executable binary path built from https://github.com/bogo/arm64-to-sim" | |
exit 1; | |
fi | |
set -eu | |
# set -x # debug log |
NewerOlder