Skip to content

Instantly share code, notes, and snippets.

@DamienDabernat
Created May 29, 2022 09:06
Show Gist options
  • Save DamienDabernat/466d1cb754f7c6bd7642d4e786fbee03 to your computer and use it in GitHub Desktop.
Save DamienDabernat/466d1cb754f7c6bd7642d4e786fbee03 to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using AsyncArTool.Scripts;
using AsyncArTool.Scripts.BackendClient.StrapiModels;
using Unity.Mathematics;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.XR.ARFoundation;
using UnityEngine.XR.ARSubsystems;
//Listez les composants nécessaires à votre experience
[RequireComponent(typeof(ARTrackedImageManager))]
[RequireComponent(typeof(ImageTrackingManager))]
[RequireComponent(typeof(AudioSource))]
[RequireComponent(typeof(AudioRecordingManager))]
[RequireComponent(typeof(MachineLearningManager))]
[RequireComponent(typeof(CameraRecordingManager))]
[RequireComponent(typeof(AnchorCreatorManager))]
[RequireComponent(typeof(AccelerometerRecordingManager))]
[RequireComponent(typeof(SpeechRecognizingManager))]
public class GameManager : MonoBehaviour, IAsyncArLaunchable
{
[SerializeField] [Tooltip("The ARCameraManager which will produce frame events.")]
private ARCameraManager cameraManager;
private ARTrackedImageManager _mTrackedImageManager;
private MachineLearningManager _machineLearningManager;
private CameraRecordingManager _cameraRecordingManager;
private AnchorCreatorManager _anchorCreatorManager;
private ImageTrackingManager _imageTrackingManager;
private ARAnchorManager _mAnchorManager;
private AudioSource _mAudioSource;
private AudioRecordingManager _audioRecordingManager;
private AccelerometerRecordingManager _accelerometerRecordingManager;
private SpeechRecognizingManager _speechRecognizingManager;
private ARRaycastManager _mRaycastManager;
private ARPlaneManager _mPlaneManager;
private bool isAnchorCreated = false;
public GameObject detectionManager;
public GameObject goToAddOnScreenCenter;
private static readonly List<ARRaycastHit> s_Hits = new();
private string _mErrorMessage = "";
private int _numberOfCroissant = 1;
private Artwork _currentArtwork;
private void Awake()
{
// _detectionManager = GameObject.Find("DetectionManager");
// print(_detectionManager);
// _detectionManager.SetActive(false);
//_mTrackedImageManager = GetComponent<ARTrackedImageManager>();
//_imageTrackingManager = GetComponent<ImageTrackingManager>();
//_audioRecordingManager = GetComponent<AudioRecordingManager>();
//_accelerometerRecordingManager = GetComponent<AccelerometerRecordingManager>();
_mAudioSource = GetComponent<AudioSource>();
_machineLearningManager = GetComponent<MachineLearningManager>();
_cameraRecordingManager = GetComponent<CameraRecordingManager>();
_anchorCreatorManager = GetComponent<AnchorCreatorManager>();
_speechRecognizingManager = GetComponent<SpeechRecognizingManager>();
_mRaycastManager = GetComponent<ARRaycastManager>();
_mPlaneManager = GetComponent<ARPlaneManager>();
_mAnchorManager = GetComponent<ARAnchorManager>();
}
// Start is called before the first frame update
void Start()
{
_cameraRecordingManager.LaunchCameraStreamRecording(texture2D =>
{
int size = 200;
int centerX = texture2D.width / 2;
int centerY = texture2D.height / 2;
int posX = centerX - size / 2;
int posY = centerY - size / 2;
Color[] pix = texture2D.GetPixels(posX, posY, size, size);
Texture2D croppedTexture2D = new Texture2D(size, size);
croppedTexture2D.SetPixels(pix);
croppedTexture2D.Apply();
texture2D = croppedTexture2D;
_machineLearningManager.Predict("BottleTestKowee", texture2D, mlClasses =>
{
foreach (var mlClass in mlClasses)
{
switch (mlClass.identifier)
{
case "junk" when mlClass.confidence > 0.70f:
Debug.Log("Not a bottle");
break;
case "bottle" when mlClass.confidence > 0.80f:
Debug.Log("THIS IS A BOTTLE");
//if (Camera.main == null) return;
//var ray = Camera.main.ViewportPointToRay(new Vector3(0.5F, 0.5F, 0));
//if (isAnchorCreated == false && _mRaycastManager.Raycast(ray, s_Hits, TrackableType.PlaneWithinPolygon))
//{
_anchorCreatorManager.AddGameObjectOnScreenCenter(_anchorCreatorManager.anchorPrefab);
Debug.Log("Anchor is created");
isAnchorCreated = true;
//}
Debug.Log("Anchor exists");
break;
default :
Debug.Log("This is nothing");
break;
}
}
});
});
}
private void Update()
{
// If there is no tap, then simply do nothing until the next call to Update().
if (_currentArtwork == null)
return;
if (Input.touchCount == 0)
return;
var touch = Input.GetTouch(0);
if (touch.phase != TouchPhase.Began)
return;
var assetToPop = goToAddOnScreenCenter; //_currentArtwork.additionalAssets.FirstOrDefault(asset => asset.caption == "antique_camera");
if (assetToPop != null)
{
Instantiate(assetToPop.gameObject, new Vector3(0, 0, 0), Quaternion.identity);
if (_mRaycastManager.Raycast(touch.position, s_Hits, TrackableType.PlaneWithinPolygon))
{
Debug.Log("Touched");
// Raycast hits are sorted by distance, so the first one
// will be the closest hit.
var hitPose = s_Hits[0].pose;
var hitTrackableId = s_Hits[0].trackableId;
var hitPlane = _mPlaneManager.GetPlane(hitTrackableId);
var anchor = _mAnchorManager.AttachAnchor(hitPlane, hitPose);
Debug.Log("Will create anchor " + anchor.trackableId);
Debug.Log("Will Instantiate ");
Instantiate(assetToPop.gameObject, anchor.transform);
assetToPop.gameObject.SetActive(true);
Debug.Log("Instantiated");
Instantiate(assetToPop.gameObject,
new Vector3(hitPose.position.x, hitPose.position.y + 0.8f, hitPose.position.z),
hitPose.rotation);
}
}
}
private void OnGUI()
{
const int fontSize = 50;
GUI.skin.button.fontSize = fontSize;
GUI.skin.label.fontSize = fontSize;
const float margin = 100;
GUILayout.BeginArea(new Rect(margin, margin, Screen.width - margin * 2, Screen.height - margin * 2));
GUILayout.Label(_mErrorMessage);
/*if (GUILayout.Button("Place object", GUILayout.Height(150)))
{
_anchorCreatorManager.AddGameObjectOnScreenCenter(_anchorCreatorManager.anchorPrefab);
}*/
GUILayout.EndArea();
}
public void QuitButton()
{
SceneManager.LoadScene("MainMenu");
}
private void SetError(string errorMessage)
{
_mErrorMessage = $"Error: {errorMessage}";
}
public void LaunchExperience(Artwork artwork, string locale)
{
print("!!!!!!!!!!!!!!!!!!!!!! LaunchExperience !!!!!!!!!!!!!!!!!!!!!");
// Debug.Log("Received artwork on GameManager: " + artwork.name);
// Debug.Log("Received locale on GameManager: " + locale);
// Debug.Log("Launch experience");
// _currentArtwork = artwork;
//var fondSonore = artwork.additionalAssets.FirstOrDefault(element => element.caption == "fond_sonore_artwork_1");
// var planeFromArtwork =
// artwork.additionalAssets.FirstOrDefault(element => element.caption == "antique_camera");
// Debug.Log("Will launch speechRecognition");
//print(GameObject.Find("DetectionManager"));
/*_detectionManager = GameObject.Find("DetectionManager");
print(_detectionManager);*/
// _detectionManager.SetActive(true);
//Instantiate(detectionManager, new Vector3(0, 0, 0), quaternion.identity);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment