Skip to content

Commit

Permalink
feat: adding reality kit
Browse files Browse the repository at this point in the history
  • Loading branch information
jacob-i committed Feb 3, 2024
1 parent 1a9e4f5 commit 22eb13e
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 35 deletions.
2 changes: 1 addition & 1 deletion app/ios/Podfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Uncomment this line to define a global platform for your project
# platform :ios, '11.0'
# platform :ios, '12.0'

# CocoaPods analytics sends network stats synchronously affecting flutter build latency.
ENV['COCOAPODS_DISABLE_STATS'] = 'true'
Expand Down
2 changes: 1 addition & 1 deletion app/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,6 @@ SPEC CHECKSUMS:
url_launcher_ios: bf5ce03e0e2088bad9cc378ea97fa0ed5b49673b
webview_flutter_wkwebview: 2e2d318f21a5e036e2c3f26171342e95908bd60a

PODFILE CHECKSUM: ef19549a9bc3046e7bb7d2fab4d021637c0c58a3
PODFILE CHECKSUM: c4c93c5f6502fe2754f48404d3594bf779584011

COCOAPODS: 1.14.3
6 changes: 3 additions & 3 deletions app/ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SUPPORTED_PLATFORMS = iphoneos;
Expand Down Expand Up @@ -445,7 +445,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
Expand Down Expand Up @@ -494,7 +494,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SUPPORTED_PLATFORMS = iphoneos;
Expand Down
42 changes: 25 additions & 17 deletions app/ios/Runner/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,25 @@ import RealityKit

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
// Keep a reference to the ARView or the custom UIView you've created
var arViewContainer: GlowbyARView?

override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {

GeneratedPluginRegistrant.register(with: self)
print("howdy")

GeneratedPluginRegistrant.register(with: self)

let controller : FlutterViewController = window?.rootViewController as! FlutterViewController
guard let controller = window?.rootViewController as? FlutterViewController else {
print("RootViewController is not of type FlutterViewController")
return false
}
let arChannel = FlutterMethodChannel(name: "com.glowbom/ar", binaryMessenger: controller.binaryMessenger)

arChannel.setMethodCallHandler({
[weak self] (call: FlutterMethodCall, result: @escaping FlutterResult) -> Void in
// Identify which method is being called from Flutter
print("Received method call: \(call.method)")
if call.method == "loadARView" {
self?.loadARView(result: result)
} else {
Expand All @@ -30,18 +33,23 @@ import RealityKit
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}

private func loadARView(result: @escaping FlutterResult) {
// Ensure you are running on the main thread
DispatchQueue.main.async {
// If the AR view is not already initialized
if self.arViewContainer == nil {
// Initialize the AR view
self.arViewContainer = GlowbyARView(frame: self.window?.bounds ?? .zero)
// Add the AR view to the view hierarchy
self.window?.rootViewController?.view.addSubview(self.arViewContainer!)
}
// Return success to the Flutter side
result(nil)
private func loadARView(result: @escaping FlutterResult) {
DispatchQueue.main.async {
if self.arViewContainer == nil {
print("ARViewContainer is nil, initializing ARView...")
self.arViewContainer = GlowbyARView(frame: self.window?.bounds ?? .zero)
guard let arViewContainer = self.arViewContainer else {
print("Failed to create ARViewContainer")
result(FlutterError(code: "AR_VIEW_CREATION_FAILED", message: "Failed to create ARViewContainer", details: nil))
return
}
print("Adding ARViewContainer to RootViewController's view...")
self.window?.rootViewController?.view.addSubview(arViewContainer)
print("ARViewContainer added to view hierarchy")
} else {
print("ARViewContainer already initialized")
}
result(nil)
}
}
}
44 changes: 31 additions & 13 deletions app/ios/Runner/Glowby.swift
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
//
// Glowby.swift
// Runner
//
// Created by Jacob Ilin on 2/2/24.
//

import UIKit
import RealityKit
import ARKit

class GlowbyARView: UIView {
class GlowbyARView: UIView, ARSessionDelegate {
var arView: ARView!

override init(frame: CGRect) {
super.init(frame: frame)
arView = ARView(frame: .zero) // Initialize with zero frame
arView.autoresizingMask = [.flexibleWidth, .flexibleHeight] // Use autoresizing instead
print("Initializing GlowbyARView with frame: \(frame)")

let arFrame = CGRect(x: frame.width / 2, y: 0, width: frame.width / 2, height: frame.height) // Only cover half the screen
arView = ARView(frame: arFrame) // Set the frame to the passed value
arView.session.delegate = self // Set the delegate to receive AR session updates
arView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
self.addSubview(arView)

// Start the AR session with a world tracking configuration
let configuration = ARWorldTrackingConfiguration()
configuration.planeDetection = [.horizontal, .vertical] // Add if you want to detect planes
arView.session.run(configuration)

// Setup RealityKit scene here
let anchor = AnchorEntity(world: .zero) // Corrected to use the right initializer
let mesh = MeshResource.generateBox(size: 0.1) // Creates a cube of 10cm x 10cm x 10cm
let anchor = AnchorEntity(world: SIMD3(x: 0.2, y: 0, z: 0)) // Position the cube 20cm to the right of the origin
let mesh = MeshResource.generateBox(size: 0.1) // Create a 10cm cube
let material = SimpleMaterial(color: .blue, isMetallic: true)
let cube = ModelEntity(mesh: mesh, materials: [material])
anchor.addChild(cube)
Expand All @@ -30,5 +33,20 @@ class GlowbyARView: UIView {
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}

// ARSessionDelegate method
func session(_ session: ARSession, didFailWithError error: Error) {
// Handle session failures
print("AR Session Failure: \(error.localizedDescription)")
}

func sessionWasInterrupted(_ session: ARSession) {
// Handle session interruptions
print("AR Session was interrupted")
}

func sessionInterruptionEnded(_ session: ARSession) {
// Handle session interruption ends
print("AR Session interruption ended")
}
}
13 changes: 13 additions & 0 deletions app/lib/views/screens/talk_screen.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:glowby/services/openai_api.dart';
import 'package:glowby/services/pulze_ai_api.dart';
import 'package:glowby/utils/utils.dart';
Expand Down Expand Up @@ -325,9 +326,21 @@ class TalkApp extends StatefulWidget {
}

class _TalkAppState extends State<TalkApp> {
static const platform = MethodChannel('com.glowbom/ar');

Future<void> _loadARView() async {
try {
final result = await platform.invokeMethod('loadARView');
print('AR View loaded: $result');
} on PlatformException catch (e) {
print("Failed to load AR View: '${e.message}'.");
}
}

@override
void initState() {
super.initState();
_loadARView();
keyIndex.addListener(() {
setState(() {});
});
Expand Down

0 comments on commit 22eb13e

Please sign in to comment.