Skip to content

Commit

Permalink
#3 feat: Unity AR foundation (#28)
Browse files Browse the repository at this point in the history
* feat: Add cross platform camera

* feat: Add level placement logic for AR

* fix: Remove unused code

* feat: New input system

* fix: Move annas test scene to development folder
  • Loading branch information
AdamJonsson authored Nov 17, 2021
1 parent f77ec0f commit 972df03
Show file tree
Hide file tree
Showing 59 changed files with 1,896 additions and 726 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 62 additions & 0 deletions Assets/Managers/ARSession/AR Session.prefab
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!1 &6800535302357286287
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 7223179368835717418}
- component: {fileID: 8108863857534307628}
- component: {fileID: 5474733860047409772}
m_Layer: 0
m_Name: AR Session
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &7223179368835717418
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 6800535302357286287}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &8108863857534307628
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 6800535302357286287}
m_Enabled: 0
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 3859a92a05d4f5d418cb6ca605290e74, type: 3}
m_Name:
m_EditorClassIdentifier:
m_AttemptUpdate: 1
m_MatchFrameRate: 1
m_TrackingMode: 2
--- !u!114 &5474733860047409772
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 6800535302357286287}
m_Enabled: 0
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: fa850fbd5b8aded44846f96e35f1a9f5, type: 3}
m_Name:
m_EditorClassIdentifier:

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ GameObject:
serializedVersion: 6
m_Component:
- component: {fileID: 9061636952727761179}
- component: {fileID: 88678719283795960}
m_Layer: 0
m_Name: CompatibilityChecker
m_Name: Compatibility Checker
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
Expand All @@ -31,3 +32,15 @@ Transform:
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &88678719283795960
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 6226634792145431312}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 47d0980116f884e48b1dcab1883af74d, type: 3}
m_Name:
m_EditorClassIdentifier:
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.ARFoundation;

Expand All @@ -21,26 +20,46 @@ public enum DeviceSupport
void Start()
{
_arSession = FindObjectOfType<ARSession>();
if (_arSession)
if (_arSession == null)
{
throw new ArgumentException("An ARSession needs to exist for the CompatibilityChecker");
}

StartCoroutine(CheckSupportForCurrentDevice());
}

public IEnumerator CheckForARSupport(Action<ARSessionState> OnCheckCompleted) {
public IEnumerator CheckSupportForCurrentDevice() {

// AR
if ((ARSession.state == ARSessionState.None) ||
(ARSession.state == ARSessionState.CheckingAvailability))
{
yield return ARSession.CheckAvailability();
}
OnCheckCompleted(ARSession.state);

// VR
// TODO

if (ARSession.state == ARSessionState.Unsupported)
{
_supportedDevice = DeviceSupport.DESKTOP;
}
else
{
_supportedDevice = DeviceSupport.AR;
}

_listeners.Invoke(_supportedDevice);
}

public static void AddListener(Action<DeviceSupport> OnResult)
{
if (_supportedDevice == CompatibilityChecker.DeviceSupport.UNKNOWN)
_listeners += OnResult;
if (_supportedDevice != CompatibilityChecker.DeviceSupport.UNKNOWN)
{
OnResult.Invoke(_supportedDevice);
return;
}
_listeners += OnResult;
}
}

8 changes: 8 additions & 0 deletions Assets/Managers/LevelPlacementManager.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,46 +1,46 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!1 &3624798292056245312
--- !u!1 &1760304300311812898
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 8109644083082424302}
- component: {fileID: 8133267252169484392}
- component: {fileID: 4719238613831707738}
- component: {fileID: -4828068412382251286}
m_Layer: 0
m_Name: LevelPositionManager
m_Name: Level Placement Manager
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &8109644083082424302
--- !u!4 &4719238613831707738
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 3624798292056245312}
m_GameObject: {fileID: 1760304300311812898}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: -6.0399113, y: -9.504788, z: 15.417434}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &8133267252169484392
--- !u!114 &-4828068412382251286
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 3624798292056245312}
m_GameObject: {fileID: 1760304300311812898}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 05baa64f55a484af7924f141954d03ca, type: 3}
m_Script: {fileID: 11500000, guid: 6f79d4f192ccf4e8aba8e82bd0a46ef9, type: 3}
m_Name:
m_EditorClassIdentifier:

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

89 changes: 89 additions & 0 deletions Assets/Managers/LevelPlacementManager/LevelPlacementManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.XR.ARFoundation;
using UnityEngine.XR.ARSubsystems;
using UnityEngine.InputSystem;

public class LevelPlacementManager : MonoBehaviour
{
[SerializeField] GameObject _levelPlaceholder;

private ARRaycastManager _arRaycastManager;

private List<RaycastHit> _raycastHits;

private LevelPlacementData _levelPlacementData;

private Camera _arCamera;

private Pose _latestPlaneHit;

// Start is called before the first frame update
void Start()
{
CompatibilityChecker.AddListener(delegate (CompatibilityChecker.DeviceSupport supportedDevice) {
if (supportedDevice != CompatibilityChecker.DeviceSupport.AR) {
Destroy(this.gameObject);
return;
}

this._arRaycastManager = FindObjectOfType<ARRaycastManager>();
this._arCamera = FindObjectOfType<Camera>();
this._levelPlacementData = FindObjectOfType<LevelPlacementData>();
});
}

// Update is called once per frame
void Update()
{
if (!SetupManager.IsSetupCompleted) return;
this.LookForPlane();
this.RenderLevelPlacementPlaceholder();
this.CheckForUserInput();
}

private void LookForPlane()
{
Vector2 screenCenter = _arCamera.ViewportToScreenPoint(new Vector3(0.5f, 0.5f));
var hits = new List<ARRaycastHit>();
this._arRaycastManager.Raycast(screenCenter, hits, TrackableType.PlaneEstimated);

if (hits.Count == 0) return;
Vector3 cameraForward = _arCamera.transform.forward;
Vector3 cameraBearing = new Vector3(cameraForward.x, 0, cameraForward.z);
Quaternion hitRotation = Quaternion.LookRotation(cameraBearing);

this._latestPlaneHit = new Pose(hits[0].pose.position, hitRotation);
}

private void RenderLevelPlacementPlaceholder()
{
if (this._latestPlaneHit == null) return;
_levelPlaceholder.transform.SetPositionAndRotation(this._latestPlaneHit.position, this._latestPlaneHit.rotation);
}

private void CheckForUserInput()
{
if (this._latestPlaneHit == null) return;
bool tappingScreenOnceWithOneFinger = Touchscreen.current.IsPressed(0);
if (tappingScreenOnceWithOneFinger)
{
ChoosePositionAndRotationOfLevels(_latestPlaneHit);
}
}

private void ChoosePositionAndRotationOfLevels(Pose pose)
{
_levelPlacementData.SetLevelPose(pose);
Debug.Log("Level Position Set");
MoveToNextScene();
}

private void MoveToNextScene()
{
// TODO, create a level management system
SceneManager.LoadScene("SampleScene");
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Assets/Managers/LevelPlacmentData.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 46 additions & 0 deletions Assets/Managers/LevelPlacmentData/Level Placement Data.prefab
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!1 &4179574480081331153
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 4664305778208492834}
- component: {fileID: 1261725758507793055}
m_Layer: 0
m_Name: Level Placement Data
m_TagString: Untagged
m_Icon: {fileID: 3306451490063965843, guid: 0000000000000000d000000000000000, type: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &4664305778208492834
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 4179574480081331153}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1261725758507793055
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 4179574480081331153}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 2afa58682c2324d6192fba877a40520d, type: 3}
m_Name:
m_EditorClassIdentifier:

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 972df03

Please sign in to comment.