Skip to content

Instantly share code, notes, and snippets.

# If you're using SwiftLint, add this to your swiftlint.yml to make Xcode
# generate warnings when you create new Swift files and Xcode inserts the
# filename where the package name should be.
custom_rules:
# This calls out an Xcode 13 bug where the header comments of newly created
# files include the file name where the package name should be.
xcode_13_header_comment_bug:
regex: ' *\/\/ *([A-Za-z0-9_]*?).swift *\n *\/\/ *\1 *\n'
message: "Incorrect header comment package name"
@drewolbrich
drewolbrich / UIColor+Blended.swift
Last active December 12, 2021 18:46
Blends together two UIColors, returning a dynamic color
import UIKit
extension UIColor {
/// Creates a dynamic color from the receiver blended with another color.
///
/// If `weight` is 0, the color of `self` is returned. If `weight` is 1, the color
/// of `otherColor` is returned.
///
/// This method works correctly with light and dark mode. A dynamic color is
@drewolbrich
drewolbrich / UIColor+Attenuated.swift
Created December 12, 2021 18:48
Attenuates a UIColor, returning a dynamic color
import UIKit
extension UIColor {
/// Creates a dynamic color from the receiver with attenuated brightness.
///
/// This method works correctly with light and dark mode. A dynamic color is
/// returned, so if the user switches to dark mode, the attenuated dark mode version of `self`
/// will be displayed.
func attenuated(by attenuation: CGFloat) -> UIColor {
@drewolbrich
drewolbrich / CommonProfile.metal
Created April 18, 2022 21:05 — forked from warrenm/CommonProfile.metal
SceneKit's CommonProfile Shader v2 (macOS 10.15)
////////////////////////////////////////////////
// CommonProfile Shader v2
#import <metal_stdlib>
using namespace metal;
#ifndef __SCNMetalDefines__
#define __SCNMetalDefines__
@drewolbrich
drewolbrich / Entity+EnumerateHierarchy.swift
Last active December 9, 2023 02:37
Entity/enumerateHierarchy
//
// Entity+EnumerateHierarchy.swift
//
// Created by Drew Olbrich on 7/18/23.
//
import RealityKit
extension Entity {
@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 / Entity+Move1.swift
Created January 16, 2024 01:54
Failed attempt #1 at Entity/move with a callback
import Foundation
import RealityKit
import Combine
private var playbackCompletedSubscriptions: Set<AnyCancellable> = .init()
extension Entity {
/// Animated `Entity/move` with a completion handler.
///
@drewolbrich
drewolbrich / Entity+Move2.swift
Created January 16, 2024 01:55
Failed attempt #2 at Entity/move with a callback
import Foundation
import RealityKit
import Combine
private var playbackCompletedSubscriptions: Set<AnyCancellable> = .init()
extension Entity {
/// Animated `Entity/move` with a completion handler.
///
@drewolbrich
drewolbrich / Entity+Move3.swift
Created January 16, 2024 04:05
Failed attempt #3 at Entity/move with a callback
extension Entity {
/// Animated `Entity/move` with a completion handler.
///
/// This implementation works but for some reason `timingFunction` appears to be
/// ignored and the timing function of the resulting animation is always linear.
func move(to target: Transform, relativeTo referenceEntity: Entity?, duration: TimeInterval, delay: TimeInterval = 0, timingFunction: AnimationTimingFunction = .default, completion: @escaping () -> Void) {
guard let scene else {
completion()
return
@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:
///