Skip to content

Instantly share code, notes, and snippets.

@drewolbrich
drewolbrich / TextureResource+LoadFileAsync.swift
Last active March 6, 2024 15:47
A wrapper around iOS Entity/loadAsync(named:in:) and visionOS Entity(named:in:) async that works on both platforms
//
// TextureResource+LoadFileAsync.swift
//
// Created by Drew Olbrich on 12/24/23.
// Copyright © 2023 Lunar Skydiving LLC. All rights reserved.
//
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
@drewolbrich
drewolbrich / ModelEntity+ReverseTriangleIndices
Created December 17, 2023 06:01
ModelEntity extension that reverses triangle indices
extension ModelEntity {
func reverseTriangleIndices() async {
guard let model else {
assertionFailure("model is undefined")
return
}
var updatedContents = MeshResource.Contents()
@drewolbrich
drewolbrich / SceneUpdateContext+EntitiesWhenRendering.swift
Last active March 6, 2024 15:47
A RealityKit SceneUpdateContext extension that wraps entities(matching:updateSystemWhen:) but works on both visionOS and iOS
//
// SceneUpdateContext+EntitiesWhenRendering.swift
//
// Created by Drew Olbrich on 7/27/23.
// Copyright © 2023 Lunar Skydiving LLC. All rights reserved.
//
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
@drewolbrich
drewolbrich / WindowBarGestureModifier.swift
Last active April 17, 2024 15:26
A visionOS RealityView view modifier that handles drag gestures
//
// WindowBarGestureModifier.swift
//
// Created by Drew Olbrich on 10/25/23.
// Copyright © 2023 Lunar Skydiving LLC. All rights reserved.
//
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
@drewolbrich
drewolbrich / View+WindowResizingRestrictions.swift
Last active January 30, 2024 21:49
A view modifier that sets the resizing restrictions for the view's window
// IMPORTANT: The following code doesn't support specifying a window's
// minimum and maximum size when used to constrain a window's aspect ratio.
// Please use this newer view modifier extension instead:
// https://gist.github.com/drewolbrich/03460fc1bb71b9a821fff722f17ec977
//
// View+WindowResizingRestrictions.swift
//
// Created by Drew Olbrich on 11/19/23.
//
@drewolbrich
drewolbrich / ResizingRestrictionsView.swift
Last active January 24, 2024 15:15
A SwiftUI view that sets UIWindowScene.ResizingRestrictions on its associated UIWindowScene
/// UPDATE: You may want to use this view modifier instead:
/// https://gist.github.com/drewolbrich/90f11b7170267674cbdcb4a0d71ce873
import SwiftUI
/// A view that, when included as a leaf node in a view hierarchy, sets
/// `UIWindowScene.ResizingRestrictions` for the view's `UIWindowScene`.
///
/// Example:
///
@drewolbrich
drewolbrich / SIMD+LookAt.swift
Created November 17, 2023 05:30
SIMD look-at functions
import simd
/// Creates a 4x4 matrix that orients the +Z axis in the direction of `at`, and the
/// +Y axis toward `up`.
func simd_look(at: SIMD3<Float>, up: SIMD3<Float> = SIMD3<Float>(0, 1, 0)) -> simd_float4x4 {
let zAxis = normalize(at)
let xAxis = normalize(cross(up, zAxis))
let yAxis = normalize(cross(zAxis, xAxis))
return simd_float4x4(
@drewolbrich
drewolbrich / Entity+Opacity.swift
Last active April 5, 2024 13:58
An Entity extension that supports animated changes the opacity of an entity and its descendants
//
// Entity+Opacity.swift
//
// Created by Drew Olbrich on 10/25/23.
// Copyright © 2023 Lunar Skydiving LLC. All rights reserved.
//
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
@drewolbrich
drewolbrich / OpacityEntity.swift
Last active January 31, 2024 18:22
An entity that supports animated changes in opacity
// Important: You may want to use this newer `Entity` extension instead: https://gist.github.com/drewolbrich/1e9d3da074c8a1d5ca93721124b97596
import Foundation
import RealityKit
extension Entity {
/// The opacity value applied to the entity and its descendants.
///
/// `OpacityComponent` is assigned to the entity if it doesn't already exist.
@drewolbrich
drewolbrich / Entity+GenerateGroundingShadowComponents.swift
Last active March 6, 2024 15:48
Add GroundingShadowComponent to all model entities in a hierarchy
//
// Entity+GenerateGroundingShadowComponents.swift
//
// Created by Drew Olbrich on 10/18/23.
// Copyright © 2023 Lunar Skydiving LLC. All rights reserved.
//
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal