Skip to content

Instantly share code, notes, and snippets.

@sGambolati
Last active January 20, 2019 18:18
Show Gist options
  • Save sGambolati/9d5269bff601d46135986a6a4e74620b to your computer and use it in GitHub Desktop.
Save sGambolati/9d5269bff601d46135986a6a4e74620b to your computer and use it in GitHub Desktop.
SpatialDemo's Final ScanManager.cs file.
using System;
using HoloToolkit.Unity;
using HoloToolkit.Unity.InputModule;
using UnityEngine;
public class ScanManager : MonoBehaviour, IInputClickHandler
{
public TextMesh InstructionTextMesh;
public Transform FloorPrefab;
public Transform WallPrefab;
public Transform SurfacePrefag;
// Use this for initialization
void Start()
{
InputManager.Instance.PushFallbackInputHandler(this.gameObject);
SpatialUnderstanding.Instance.RequestBeginScanning();
SpatialUnderstanding.Instance.ScanStateChanged += ScanStateChanged;
}
private void ScanStateChanged()
{
if (SpatialUnderstanding.Instance.ScanState == SpatialUnderstanding.ScanStates.Scanning)
{
LogSurfaceState();
}
else if (SpatialUnderstanding.Instance.ScanState == SpatialUnderstanding.ScanStates.Done)
{
InstanciateObjectOnFloor();
}
}
private void OnDestroy()
{
SpatialUnderstanding.Instance.ScanStateChanged -= ScanStateChanged;
}
// Update is called once per frame
void Update()
{
switch (SpatialUnderstanding.Instance.ScanState)
{
case SpatialUnderstanding.ScanStates.None:
case SpatialUnderstanding.ScanStates.ReadyToScan:
break;
case SpatialUnderstanding.ScanStates.Scanning:
this.LogSurfaceState();
break;
case SpatialUnderstanding.ScanStates.Finishing:
this.InstructionTextMesh.text = "State: Finishing Scan";
break;
case SpatialUnderstanding.ScanStates.Done:
this.InstructionTextMesh.text = "State: Scan Finished";
break;
default:
break;
}
}
private void LogSurfaceState()
{
IntPtr statsPtr = SpatialUnderstanding.Instance.UnderstandingDLL.GetStaticPlayspaceStatsPtr();
if (SpatialUnderstandingDll.Imports.QueryPlayspaceStats(statsPtr) != 0)
{
var stats = SpatialUnderstanding.Instance.UnderstandingDLL.GetStaticPlayspaceStats();
this.InstructionTextMesh.text = string.Format("TotalSurfaceArea: {0:0.##}\nWallSurfaceArea: {1:0.##}\nHorizSurfaceArea: {2:0.##}", stats.TotalSurfaceArea, stats.WallSurfaceArea, stats.HorizSurfaceArea);
}
}
public void OnInputClicked(InputClickedEventData eventData)
{
this.InstructionTextMesh.text = "Requested Finish Scan";
SpatialUnderstanding.Instance.RequestFinishScan();
}
private void InstanciateObjectOnFloor()
{
const int QueryResultMaxCount = 512;
SpatialUnderstandingDllTopology.TopologyResult[] _resultsTopology = new SpatialUnderstandingDllTopology.TopologyResult[QueryResultMaxCount];
var minLengthFloorSpace = 0.25f;
var minWidthFloorSpace = 0.25f;
var resultsTopologyPtr = SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(_resultsTopology);
var locationCount = SpatialUnderstandingDllTopology.QueryTopology_FindPositionsOnFloor(minLengthFloorSpace, minWidthFloorSpace, _resultsTopology.Length, resultsTopologyPtr);
if (locationCount > 0)
{
Instantiate(this.FloorPrefab, _resultsTopology[0].position, Quaternion.LookRotation(_resultsTopology[0].normal, Vector3.up));
this.InstructionTextMesh.text = "Placed the hologram";
}
else
{
this.InstructionTextMesh.text = "I can't found the enough space to place the hologram.";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment