Skip to content

Instantly share code, notes, and snippets.

View ynagatomo's full-sized avatar

Yasuhito Nagatomo ynagatomo

View GitHub Profile
// ARPortalView.swift
import SwiftUI
import RealityKit
import ARKit
struct ARPortalView: UIViewRepresentable {
func makeUIView(context: Context) -> ARView {
let innerSpaceSize: Float = 3.0 // [meters]
let innerSpaceMargin: Float = 0.02 // [meters]
let innerSpaceOcclusion: Float = 0.01 // [meters] for to create a door inside the inner space
@ynagatomo
ynagatomo / ARSpaceView.swift
Created January 23, 2022 05:47
A minimal AR view.
// ARSpaceView.swift
import SwiftUI
import RealityKit
import ARKit
struct ARSpaceView: UIViewRepresentable {
let modelName = "InsideISS.usdz"
let modelPosition = SIMD3<Float>([0, 0, 0]) // [meters]
func makeUIView(context: Context) -> some UIView {
// AREarthVideoView.swift
import SwiftUI
import RealityKit
import ARKit
struct AREarthVideoView: UIViewControllerRepresentable {
typealias UIViewControllerType = ARViewController
func makeCoordinator() -> Coordinator {
return Coordinator(self)
@ynagatomo
ynagatomo / RealityDump.swift
Last active February 20, 2024 15:15
A simple dump function to show the RealityKit Entity, ModelEntity, and AnchorEntity object.
// RealityDump.swift
// Created by Yasuhito NAGATOMO on 2022/02/22.
import Foundation
import RealityKit
#if DEBUG
// swiftlint:disable line_length
private let keywords = [ // (string, indentLevel [1...])
// Type-safe State Machine with Phantom Type May 2022 @AtarayoSD
import Foundation
protocol State {}
// Transitions
protocol TransferableToB {}
protocol TransferableToC {}
protocol TransferableToD {}
// SIMD - Initializers
SIMD3<Int>()
SIMD3<Double>(SIMD3<Double>())
SIMD3<Double>(1, 2, 3)
SIMD3<Int8>(SIMD3<Double>(1.9, -1.9, -1.1), rounding: .towardZero)
SIMD3<Int8>(arrayLiteral: 1, 2, 3)
SIMD3<Int8>(clamping: SIMD3<Int>(200, -200, 100))
SIMD3<UInt8>(clamping: SIMD3<Int>(200, -200, 100))
SIMD3<Int8>(truncatingIfNeeded: SIMD3<Int>(512, 511, 100))
SIMD3<Int8>([1, 2, 3])
import simd
// Create vectors
vector_uchar2(0, 0) // typealias
simd_make_uchar4(255) // (Uint8)
simd_make_uchar4(1, 2, 3, 4) // (Uint8 x4)
vector4(1, 2, 3, 4) // (Uint8 x4)
simd_make_uchar4(simd_make_uchar3(1, 2, 3), 4)
simd_uchar(simd_int4(1, 2, 3, 4))
simd_uchar(simd_int4(255, 256, 257, -1))
import simd
// Initializers
simd_float2x3()
simd_float2x3(1)
simd_float2x2(diagonal: SIMD2<Float>.one * 2)
simd_float2x3([SIMD3<Float>(1, 2, 3), SIMD3<Float>(4, 5, 6)])
simd_float2x3(SIMD3<Float>(1, 2, 3), SIMD3<Float>(4, 5, 6))
simd_float2x3(columns: (simd_float3.zero, simd_float3.one))
simd_float2x3(rows: [SIMD2<Float>.zero, SIMD2<Float>.one, SIMD2<Float>.zero])
import simd
// Initializers
simd_quatf()
simd_quatf(vector: simd_float4(0, 0, 0, 1)) // [0, 1, 2]: img, [3]: real
simd_quatf(simd_float3x3(SIMD3<Float>(cos(Float.pi/2), -sin(Float.pi/2), 0), SIMD3<Float>(sin(Float.pi/2), cos(Float.pi/2), 0), SIMD3<Float>(0, 0, 1))) // from 3x3 rotation matrix, which must be orthogonal and have a determinant of 1
simd_float3x3(SIMD3<Float>(cos(Float.pi/2), -sin(Float.pi/2), 0), SIMD3<Float>(sin(Float.pi/2), cos(Float.pi/2), 0), SIMD3<Float>(0, 0, 1)).determinant
simd_quatf(simd_float4x4(SIMD4<Float>(cos(Float.pi/2), -sin(Float.pi/2), 0, 0), SIMD4<Float>(sin(Float.pi/2), cos(Float.pi/2), 0, 0), SIMD4<Float>(0, 0, 1, 0), SIMD4<Float>(0, 0, 0, 1))) // from 4x4 rotation matrix, whose 3x3 part must be orthogonal and have a determinant of 1
simd_quatf(angle: Float.pi/2, axis: SIMD3<Float>(0, 0, -1)) // rotation about an axis
simd_quatf(from: SIMD3<Float>(0, 1, 0), to: SIMD3<Float>(1, 0, 0)) // rotation between two vectors. They should be normalized.
//
// ContentView.swift
// sfsymbols4sample
//
// Created by Yasuhito Nagatomo on 2022/06/16.
// Updated on 2022/06/17 with the advice of @kingatarthur.
//
import SwiftUI