Skip to content

Instantly share code, notes, and snippets.

View Matt54's full-sized avatar

Matt Pfeiffer Matt54

View GitHub Profile
@Matt54
Matt54 / AddBlendGlitchView.swift
Created July 15, 2024 09:18
RBG spheres with add blend mode materials creating a glitchy offset effect
import RealityKit
import SwiftUI
struct AddBlendGlitchView: View {
@State var rootEntity: Entity?
@State private var offsetTimer: Timer?
@State var isOffset: Bool = false
@State private var jitterTimer: Timer?
@State private var rotationAngles: SIMD3<Float> = [0, 0, 0]
@State private var rotateAndScaleTimer: Timer?
@Matt54
Matt54 / SphereApproximationComparisonView.swift
Created July 14, 2024 18:37
Comparing Icosphere (geodesic polyhedra) to lat/long grid approach for sphere approximation
import SwiftUI
import RealityKit
struct SphereApproximationComparisonView: View {
var icospheres: [Entity] = (MeshQuality.allCases).map { quality in
return try! generateEntity(radius: 0.1, meshQuality: quality, type: .icosphere)
}
var gridSpheres: [Entity] = (MeshQuality.allCases).map { quality in
return try! generateEntity(radius: 0.1, meshQuality: quality, type: .grid)
import RealityKit
import SwiftUI
struct TriangleCountView: View {
let generateSphereEntity = createEntity(resource: .generateSphere(radius: 0.5), material: getMaterial())
let generateSpecificSphereEntity = try! createEntity(resource: .generateSpecificSphere(radius: 0.5, latitudeBands: 15, longitudeBands: 15), material: getMaterial())
@State var numberOfLatitudeBands: Int = 15
@State var numberOfLongitudeBands: Int = 15
@State var sphereRadius: Float = 0.5
@Matt54
Matt54 / ExtrudedTextView.swift
Last active July 12, 2024 17:53
RealityView that creates a MeshResource from an AttributedString
import RealityKit
import SwiftUI
struct ExtrudedTextView: View {
let entity: Entity = try! getEntity()
var body: some View {
RealityView { content in
// hack to align text in volume
entity.transform.translation.x -= 0.21
@Matt54
Matt54 / MeshResource+generateSpecificSphere.swift
Last active July 9, 2024 13:10
Creates a sphere MeshResource with a specified number of latitude and longitude bands (for lower poly sphere)
extension MeshResource {
static func generateSpecificSphere(radius: Float, latitudeBands: Int = 10, longitudeBands: Int = 10) throws -> MeshResource {
let vertexCount = (latitudeBands + 1) * (longitudeBands + 1)
let indexCount = latitudeBands * longitudeBands * 6
var desc = MyVertex.descriptor
desc.vertexCapacity = vertexCount
desc.indexCapacity = indexCount
let mesh = try LowLevelMesh(descriptor: desc)
@Matt54
Matt54 / FibonacciLatticeView.swift
Created July 9, 2024 01:02
RealityKit View using fibonacci lattice to evenly distribute points around a sphere
import RealityKit
import SwiftUI
struct FibonacciLatticeView: View {
@State private var rotationAngles: SIMD3<Float> = [0, 0, 0]
@State private var modulationTimer: Timer?
@State private var time: Double = 0.0
@State private var lastRotationUpdateTime = CACurrentMediaTime()
let overallSphereRadius: Float = 0.0875
@Matt54
Matt54 / AddBlendModeView.swift
Last active July 8, 2024 10:10
RealityKit Material add blend mode example
import RealityKit
import SwiftUI
struct AddBlendModeView: View {
var isSphereOnTop: Bool = true
@State var rootEntity: Entity?
var body: some View {
RealityView { content in
} update: { content in
@Matt54
Matt54 / GlowingLowPolySphere.swift
Last active July 7, 2024 20:07
Sun with rays entity created from many low poly spheres at varying opacity and radius + different rotation animation along each axis
import RealityKit
import SwiftUI
struct GlowingLowPolySphere: View {
@State private var opacity: Float = 1.0
@State private var isForward: Bool = false
@State private var rotationAngles: SIMD3<Float> = [0, 0, 0]
@State private var modulationTimer: Timer?
@State private var time: Double = 0.0
@State private var lastRotationUpdateTime = CACurrentMediaTime()
@Matt54
Matt54 / FlowerPetal.swift
Last active July 2, 2024 00:52
A flower petal / leaf looking RealityKit view created from a LowLevelMesh
import RealityKit
import SwiftUI
struct FlowerPetal: View {
@State private var rotationAngle: Float = 0
var body: some View {
RealityView { content in
let leafEntity = try! leafEntity()
content.add(leafEntity)
@Matt54
Matt54 / GlowingSphere.swift
Created June 30, 2024 12:26
A blend of many spheres with varying opacity and size fading in and out
import RealityKit
import SwiftUI
struct GlowingSphere: View {
@State private var opacity: Float = 1.0
@State private var isForward: Bool = false
var body: some View {
GeometryReader3D { proxy in
RealityView { content in