Skip to content

Instantly share code, notes, and snippets.

@otmb
Created November 30, 2018 05:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save otmb/28621781f88dc6a34b35e1edb2740fb2 to your computer and use it in GitHub Desktop.
Save otmb/28621781f88dc6a34b35e1edb2740fb2 to your computer and use it in GitHub Desktop.
iOS ImageTracking and PointCLoud
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.iOS;
public class MyGenerateImageAnchor : MonoBehaviour {
[SerializeField]
private ARReferenceImage referenceImage;
[SerializeField]
private GameObject prefabToGenerate;
private GameObject imageAnchorGO;
// Use this for initialization
void Start () {
UnityARSessionNativeInterface.ARImageAnchorAddedEvent += AddImageAnchor;
UnityARSessionNativeInterface.ARImageAnchorUpdatedEvent += UpdateImageAnchor;
UnityARSessionNativeInterface.ARImageAnchorRemovedEvent += RemoveImageAnchor;
}
void AddImageAnchor(ARImageAnchor arImageAnchor)
{
Debug.LogFormat("image anchor added[{0}] : tracked => {1}", arImageAnchor.identifier, arImageAnchor.isTracked);
if (arImageAnchor.referenceImageName == referenceImage.imageName) {
Vector3 position = UnityARMatrixOps.GetPosition (arImageAnchor.transform);
Quaternion rotation = UnityARMatrixOps.GetRotation (arImageAnchor.transform);
imageAnchorGO = Instantiate<GameObject> (prefabToGenerate, position, rotation);
}
}
void UpdateImageAnchor(ARImageAnchor arImageAnchor)
{
Debug.LogFormat("image anchor updated[{0}] : tracked => {1}", arImageAnchor.identifier, arImageAnchor.isTracked);
if (arImageAnchor.referenceImageName == referenceImage.imageName) {
if (arImageAnchor.isTracked)
{
if (!imageAnchorGO.activeSelf)
{
imageAnchorGO.SetActive(true);
}
imageAnchorGO.transform.position = UnityARMatrixOps.GetPosition(arImageAnchor.transform);
imageAnchorGO.transform.rotation = UnityARMatrixOps.GetRotation(arImageAnchor.transform);
HitTest();
}
else if (imageAnchorGO.activeSelf)
{
imageAnchorGO.SetActive(false);
}
}
}
void RemoveImageAnchor(ARImageAnchor arImageAnchor)
{
Debug.LogFormat("image anchor removed[{0}] : tracked => {1}", arImageAnchor.identifier, arImageAnchor.isTracked);
if (imageAnchorGO) {
GameObject.Destroy (imageAnchorGO);
}
}
void OnDestroy()
{
UnityARSessionNativeInterface.ARImageAnchorAddedEvent -= AddImageAnchor;
UnityARSessionNativeInterface.ARImageAnchorUpdatedEvent -= UpdateImageAnchor;
UnityARSessionNativeInterface.ARImageAnchorRemovedEvent -= RemoveImageAnchor;
}
// Update is called once per frame
void Update () {
}
bool HitTestWithResultType (ARPoint point, ARHitTestResultType resultTypes)
{
List<ARHitTestResult> hitResults = UnityARSessionNativeInterface.GetARSessionNativeInterface ().HitTest (point, resultTypes);
if (hitResults.Count > 0) {
foreach (var hitResult in hitResults) {
Debug.Log ("Got hit!");
imageAnchorGO.transform.position = UnityARMatrixOps.GetPosition (hitResult.worldTransform);
Debug.Log (string.Format ("x:{0:0.######} y:{1:0.######} z:{2:0.######}",
imageAnchorGO.transform.position.x,
imageAnchorGO.transform.position.y,
imageAnchorGO.transform.position.z));
return true;
}
}
return false;
}
void HitTest(){
#if !UNITY_EDITOR
if (imageAnchorGO != null)
{
var screenPosition = Camera.main.WorldToViewportPoint(imageAnchorGO.transform.position);
ARPoint point = new ARPoint {
x = screenPosition.x,
y = screenPosition.y
};
// prioritize reults types
ARHitTestResultType[] resultTypes = {
ARHitTestResultType.ARHitTestResultTypeExistingPlaneUsingExtent,
};
foreach (ARHitTestResultType resultType in resultTypes)
{
if (HitTestWithResultType (point, resultType))
{
return;
}
}
}
#endif
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
[RequireComponent(typeof(MeshRenderer))]
[RequireComponent(typeof(MeshFilter))]
public class PointCloudViewer : MonoBehaviour
{
Mesh mesh;
Vector3[] vertices;
public float pcSize = 1.0f;
int depthWidth = 512;
int depthHeight = 424;
// Use this for initialization
void Start()
{
if (mesh != null)
Destroy(mesh);
mesh = new Mesh()
{
indexFormat = IndexFormat.UInt32,
};
vertices = new Vector3[depthWidth * depthHeight];
var indices = new int[vertices.Length];
for (int i = 0; i < vertices.Length; i++)
indices[i] = i;
mesh.MarkDynamic();
mesh.vertices = vertices;
mesh.SetIndices(indices, MeshTopology.Points, 0, false);
mesh.bounds = new Bounds(Vector3.zero, Vector3.one * 10f);
mesh.UploadMeshData(false);
// shader.
GetComponent<Renderer>().material.SetFloat("_Size", pcSize);
GetComponent<MeshFilter>().sharedMesh = mesh;
}
public void SetMesh(Vector3[] vertices,Color[] colors){
mesh.vertices = vertices;
mesh.colors = colors;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment