Skip to content

Instantly share code, notes, and snippets.

View Matt54's full-sized avatar

Matt Pfeiffer Matt54

View GitHub Profile
@Matt54
Matt54 / ColorNoiseImageSphere.swift
Created June 27, 2024 10:37
A color noise textured sphere created from a CGImage and a LowLevelMesh
import RealityKit
import SwiftUI
struct ImageSphere: View {
let cgImage: CGImage
@State private var rotationAngle: Float = 0
@State private var previousSize: SIMD3<Float> = .zero
var body: some View {
GeometryReader3D { proxy in
@Matt54
Matt54 / GradientTextureSphereView.swift
Created June 28, 2024 02:57
LowLevelMesh Sphere with programatically generated gradient texture
import RealityKit
import SwiftUI
struct GradientTextureSphereView: View {
let cgImage: CGImage
@State private var rotationAngle: Float = 0
@State private var previousSize: SIMD3<Float> = .zero
var body: some View {
GeometryReader3D { proxy in
@Matt54
Matt54 / OpacityThresholdSphere.swift
Last active June 29, 2024 11:41
Sphere with holes created from opacity threshold and CGImage with alpha component. Uses two sphere to create outside vs. inside appearance
import RealityKit
import SwiftUI
struct OpacityThresholdSphere: View {
let outerCGImage: CGImage
let innerCGImage: CGImage
@State private var outerEntity: Entity?
@State private var innerEntity: Entity?
@State private var rotationAngle: Float = 0
@State private var frameDuration: TimeInterval = 0.0
@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
@Matt54
Matt54 / FlowerPetal.swift
Last active October 12, 2024 21:27
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 / 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 / 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 / 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 / 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 / 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