Skip to content

Instantly share code, notes, and snippets.

@saaeiddev
Created January 2, 2026 08:32
Show Gist options
  • Select an option

  • Save saaeiddev/01b9603140269b569fbab7a3ab6acea7 to your computer and use it in GitHub Desktop.

Select an option

Save saaeiddev/01b9603140269b569fbab7a3ab6acea7 to your computer and use it in GitHub Desktop.
AR Brain Education App using ARKit + RealityKit in Swift. Shows 3D brain, labels brain regions, and provides neuroscientific educational info. Conceptually inspired by Brainapse. Developed by Amir Saeid Dehghan
//
// ViewController.swift
// ARBrainEducation
//
// Created by Amir Saeid Dehghan
//
import UIKit
import RealityKit
import ARKit
/*
--------------------------------------------------
This ViewController is the main AR scene controller.
It manages AR session, loads the 3D brain model,
and handles user interaction.
این ViewController کنترل‌کننده اصلی صحنه AR است.
مدیریت سشن واقعیت افزوده، بارگذاری مدل سه‌بعدی مغز
و تعامل کاربر را بر عهده دارد.
--------------------------------------------------
*/
class ViewController: UIViewController {
// ARView is the main container for AR content
// ARView محفظه اصلی نمایش محتوای واقعیت افزوده است
@IBOutlet var arView: ARView!
override func viewDidLoad() {
super.viewDidLoad()
// Step 1: Configure and start AR session
// مرحله ۱: راه‌اندازی و اجرای سشن AR
setupARSession()
// Step 2: Load brain model and labels
// مرحله ۲: بارگذاری مدل مغز و لیبل‌ها
loadBrainModel()
}
// --------------------------------------------------
// AR SESSION CONFIGURATION
// --------------------------------------------------
/*
This function configures ARKit to detect horizontal planes
and enables world tracking.
این تابع ARKit را برای تشخیص سطوح افقی
و ردیابی محیط فعال می‌کند.
*/
func setupARSession() {
let configuration = ARWorldTrackingConfiguration()
configuration.planeDetection = [.horizontal]
arView.session.run(configuration)
}
// --------------------------------------------------
// LOADING 3D BRAIN MODEL
// --------------------------------------------------
/*
This function loads the 3D brain model,
places it on a detected surface,
and adds educational text labels.
این تابع مدل سه‌بعدی مغز را بارگذاری کرده،
روی سطح قرار می‌دهد و متن آموزشی اضافه می‌کند.
*/
func loadBrainModel() {
// Anchor binds virtual content to real-world surface
// Anchor محتوا را به سطح واقعی متصل می‌کند
let anchor = AnchorEntity(plane: .horizontal)
// Load brain 3D model (brain.usdz)
// بارگذاری مدل سه‌بعدی مغز
let brainEntity = try! Entity.load(named: "brain")
brainEntity.scale = SIMD3(0.3, 0.3, 0.3)
// Create 3D text for frontal lobe explanation
// ساخت متن سه‌بعدی برای توضیح لوب پیشانی
let textMesh = MeshResource.generateText(
"Frontal Lobe\nDecision Making",
extrusionDepth: 0.01,
font: .systemFont(ofSize: 0.1),
containerFrame: .zero,
alignment: .center,
lineBreakMode: .byWordWrapping
)
// Text material
// متریال متن
let textMaterial = SimpleMaterial(color: .cyan, isMetallic: false)
let textEntity = ModelEntity(mesh: textMesh, materials: [textMaterial])
// Position text above the brain
// قرار دادن متن بالای مغز
textEntity.position = SIMD3(0, 0.15, 0)
// Add entities to anchor
// اضافه کردن مدل‌ها به انکر
anchor.addChild(brainEntity)
anchor.addChild(textEntity)
// Add anchor to AR scene
// اضافه کردن انکر به صحنه AR
arView.scene.addAnchor(anchor)
}
// --------------------------------------------------
// USER INTERACTION (TOUCH HANDLING)
// --------------------------------------------------
/*
This function detects user taps on AR objects
and shows information about the selected brain region.
این تابع لمس کاربر روی اشیای AR را تشخیص داده
و اطلاعات ناحیه مغزی را نمایش می‌دهد.
*/
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let touch = touches.first else { return }
let location = touch.location(in: arView)
if let entity = arView.entity(at: location) {
showBrainRegionInfo(for: entity)
}
}
// --------------------------------------------------
// EDUCATIONAL INFORMATION DISPLAY
// --------------------------------------------------
/*
This function presents an educational alert
explaining the function of the brain region.
این تابع یک پنجره آموزشی
برای توضیح عملکرد ناحیه مغز نمایش می‌دهد.
*/
func showBrainRegionInfo(for entity: Entity) {
let alert = UIAlertController(
title: "Frontal Lobe",
message: """
Responsible for:
• Decision making
• Planning
• Personality
• Problem solving
مسئول:
• تصمیم‌گیری
• برنامه‌ریزی
• شخصیت
• حل مسئله
""",
preferredStyle: .alert
)
alert.addAction(UIAlertAction(title: "OK", style: .default))
present(alert, animated: true)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment