Skip to content

Instantly share code, notes, and snippets.

View Matt54's full-sized avatar

Matt Pfeiffer Matt54

View GitHub Profile
@Matt54
Matt54 / ModelUVTextureRealityView.swift
Created August 25, 2024 13:53
RealityView loading a USDZ model and visualizing the UV coordinates with a LowLevelTexture
import SwiftUI
import RealityKit
import MetalKit
struct ModelUVTextureRealityView: View {
@State var entity: ModelEntity?
@State var texture: LowLevelTexture?
let device: MTLDevice
let commandQueue: MTLCommandQueue
let textureComputePipeline: MTLComputePipelineState
@Matt54
Matt54 / AdjustModelTextureRealityView.swift
Created August 24, 2024 21:00
RealityView loading USDZ file and copying it's TextureResource into a LowLevelTexture for animation using metal
import SwiftUI
import RealityKit
import MetalKit
struct AdjustModelTextureRealityView: View {
@State var entity: ModelEntity?
@State var originalTexture: LowLevelTexture?
@State var fadedTexture: LowLevelTexture?
@State var fadeValue: Float = 0.0
@State var timer: Timer?
@Matt54
Matt54 / DownloadModelRealityView.swift
Created August 23, 2024 00:26
RealityView created by downloading USDZ file to create ModelEntity and then swapping it's material for PhysicallyBasedMaterial
import RealityKit
import SwiftUI
struct DownloadModelRealityView: View {
@State var rotationAngle: Float = 0
@State var timer: Timer?
let url = URL(string: "https://matt54.github.io/Resources/StatueOfBuddha.usdz")!
var body: some View {
RealityView { content in
@Matt54
Matt54 / OuterSpaceCeilingPortalView.swift
Created August 21, 2024 04:51
RealityKit ceiling portal view with outer space skybox
import ARKit
import RealityKit
import SwiftUI
struct OuterSpaceCeilingPortalView: View {
@State var detectionTimer: Timer?
@State var portalEntity: Entity?
@State var maxRadius: Float = 0
@State var portalTransform: simd_float4x4?
@State var animationTimer: Timer?
@Matt54
Matt54 / ImagePlaneView.swift
Created August 16, 2024 04:47
Feathering effect applied to an image on a Window and also a RealityView
import SwiftUI
import RealityKit
#Preview("Window View") {
ImagePlaneView()
}
#Preview("RealityView") {
ImagePlaneRealityView()
}
@Matt54
Matt54 / MorphSphere.metal
Last active September 4, 2024 06:03
RealityView - organic, wave-like sphere morphing using LowLevelMesh and metal
#include <metal_stdlib>
using namespace metal;
struct VertexData {
float3 position;
float3 normal;
float2 uv;
};
struct MorphingSphereParams {
@Matt54
Matt54 / ImmersivePreviewExample.swift
Created August 11, 2024 16:14
Demonstrating full immersive preview
import RealityKit
import SwiftUI
struct ImmersivePreviewExample: View {
var body: some View {
RealityView { content in
let meshResource = MeshResource.generatePlane(width: 2.0, height: 2.0)
let entity = Entity()
let modelComponent = ModelComponent(mesh: meshResource, materials: [SimpleMaterial(color: .yellow, roughness: 1.0, isMetallic: false)])
entity.components.set(modelComponent)
@Matt54
Matt54 / SphereMeshTextureComparisonView.swift
Created August 10, 2024 23:00
RealityView comparing built-in generateSphere with LowLevelMesh implementation - both having and excluding uv data in vertex attributes
import SwiftUI
import RealityKit
struct SphereMeshTextureComparisonView: View {
let latitudeBands = 128
let longitudeBands = 80
let radius: Float = 0.1
// Metal-related properties
let device: MTLDevice
@Matt54
Matt54 / LightBeamSynthView.swift
Last active March 14, 2025 10:01
RealityView of light beam synth with touch gesture interactions
import AVFoundation
import SwiftUI
import RealityKit
#Preview { LightBeamSynthView() }
struct LightBeamSynthView: View {
@Environment(\.physicalMetrics) var physicalMetrics
@State var outerCylinderEntity: Entity?
@State var innerCylinderEntity: Entity?
@State var touchEntity: Entity?
@Matt54
Matt54 / CylinderWiggleMeshView.swift
Last active August 9, 2024 00:26
RealityView LowLevelMesh Cylinder with a wiggling animation
import SwiftUI
import RealityKit
struct CylinderWiggleMeshView: View {
let radialSegments = 16
let heightSegments = 5
let animationStepAmount: Float = 0.05
let radius: Float = 0.005
let height: Float = 0.15
let displacementRange: Float = 0.005