Created
August 15, 2017 18:16
-
-
Save cabbibo/c199dbf78962c36b5643a206e6458124 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.XR.iOS; | |
public class ARKitComputeCloud : MonoBehaviour { | |
public int maxPointsToShow; | |
public int maxNewPoints; | |
// creating an event to be able to use more fluidly! | |
public delegate void NewARFrame(int numNew, Vector3 avePos, Vector3 newAve, Vector3[] pcData , Vector3[] opcData , Vector3[] npcData); | |
public event NewARFrame OnNewARFrame; | |
public Vector3[] PointCloudData; | |
public Vector3[] oPointCloudData; | |
public Vector3[] newPointCloudData; | |
public Vector3 averagePoint; | |
public int totalNewPoints = 0; | |
private bool frameUpdated = false; | |
private float[] values; | |
public ComputeBuffer _buffer; | |
struct Vert{ | |
public float used; | |
public Vector3 pos; | |
public Vector3 debug; | |
}; | |
private int vertStructSize = 1 + 3 + 3; | |
// Use this for initialization | |
void OnEnable() { | |
UnityARSessionNativeInterface.ARFrameUpdatedEvent += ARFrameUpdated; | |
frameUpdated = false; | |
newPointCloudData = new Vector3[ maxNewPoints ]; | |
values = new float[ maxPointsToShow * vertStructSize ]; | |
_buffer = new ComputeBuffer( maxPointsToShow , vertStructSize * sizeof( float )); | |
int index = 0; | |
for( int i = 0; i < maxPointsToShow; i++ ){ | |
// used | |
values[ index++ ] = 0; | |
// positions | |
values[ index++ ] = Random.Range( -.01f , .01f ); | |
values[ index++ ] = Random.Range( -.01f , .01f ); | |
values[ index++ ] = Random.Range( -.01f , .01f ); | |
// Debug | |
values[ index++ ] = 0; | |
values[ index++ ] = 0; | |
values[ index++ ] = 0; | |
} | |
_buffer.SetData(values); | |
} | |
public void OnDisable(){ | |
_buffer.Release(); | |
UnityARSessionNativeInterface.ARFrameUpdatedEvent -= ARFrameUpdated; | |
} | |
public void ARFrameUpdated(UnityARCamera camera){ | |
PointCloudData = camera.pointCloudData; | |
frameUpdated = true; | |
} | |
// Update is called once per frame | |
void Update () { | |
if (frameUpdated) { | |
Vector3 averagePoint = new Vector3(0,0,0); | |
Vector3 averageNew = new Vector3(0,0,0); | |
if (PointCloudData != null && PointCloudData.Length > 0) { | |
// int numParticles = Mathf.Min (PointCloudData.Length, maxPointsToShow); | |
int index = 0; | |
float numPoints = 0; | |
// print( PointCloudData.Length ); | |
foreach (Vector3 currentPoint in PointCloudData) { | |
if( index < vertStructSize * maxPointsToShow ){ | |
values[index+0] = 1; | |
values[index+1] = currentPoint.x; | |
values[index+2] = currentPoint.y; | |
values[index+3] = currentPoint.z; | |
averagePoint += currentPoint; | |
numPoints ++; | |
}else{ | |
values[index+0] = 0; | |
values[index+1] = 0; | |
values[index+2] = 0; | |
values[index+3] = 0; | |
} | |
index += vertStructSize; | |
} | |
averagePoint /= numPoints; | |
_buffer.SetData(values); | |
if( oPointCloudData == null ){ | |
oPointCloudData = PointCloudData; | |
newPointCloudData = PointCloudData; | |
totalNewPoints = PointCloudData.Length; | |
}else{ | |
totalNewPoints = 0; | |
Vector3 p1; | |
Vector3 p2; | |
for( int i = 0; i < PointCloudData.Length; i++ ){ | |
p1 = PointCloudData[i]; | |
if( i >= oPointCloudData.Length ){ | |
if( totalNewPoints < maxNewPoints ){ | |
newPointCloudData[ totalNewPoints ] = p1; | |
averageNew += p1; | |
totalNewPoints ++; | |
}else{ | |
// Debug.Log("NO!"); | |
} | |
} | |
bool toAdd = true; | |
for( int j = 0; j < oPointCloudData.Length; j++){ | |
p2 = oPointCloudData[ j ]; | |
if( p1.x == p2.x && p1.y == p2.y && p1.y == p2.y ){ | |
toAdd = false; | |
} | |
} | |
if( toAdd == true ){ | |
newPointCloudData[ totalNewPoints ] = p1; | |
averageNew += p1; | |
totalNewPoints ++; | |
} | |
} | |
oPointCloudData = PointCloudData; | |
} | |
averageNew /= (float)totalNewPoints; | |
if(OnNewARFrame != null) OnNewARFrame(totalNewPoints, averagePoint,averageNew , PointCloudData , oPointCloudData , newPointCloudData ); | |
// no point cloud! | |
} else { | |
} | |
frameUpdated = false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment