public static SystemMeshData FromEnvironmentObseration(EnvironmentObservation observation) { var meshData = new SystemMeshData() { Vertices = new Numerics.Vector3[observation.VertexCount], Normals = new Numerics.Vector3[observation.VertexCount], Indices = new uint[observation.TriangleIndexCount] }; #if UNITY_WSA observation.GetVertexPositions(meshData.Vertices); observation.GetVertexNormals(meshData.Normals); observation.GetTriangleIndices(meshData.Indices); #endif return(meshData); }
async void Update() { if (!_updateInProgress && (DateTime.Now - _lastMeshUpdateTime) > TimeSpan.FromSeconds(2)) { _updateInProgress = true; EnvironmentObservation observation = await Query.ComputeLatestEnvironmentObservationAsync(EnvironmentTopology); #if SPATIALCOORDINATESYSTEM_API_PRESENT ObjectAnchorsLocation?observationLocation = await _mesh.LocateAndSetFromEnvironmentObservation(observation); if (observationLocation.HasValue) { transform.SetPositionAndRotation(observationLocation.Value.Position, observationLocation.Value.Orientation); } #endif _lastMeshUpdateTime = DateTime.Now; _updateInProgress = false; } }
// Update is called once per frame async void Update() { if (!_updateInProgress && Query != null && EnvironmentMaterial != null && (!_lastMeshUpdateTime.HasValue || DateTime.Now - _lastMeshUpdateTime.Value > TimeSpan.FromSeconds(2))) { _updateInProgress = true; EnvironmentObservation observation = await Query.ComputeLatestEnvironmentObservationAsync(EnvironmentObservationTopology.PointCloud); MeshLoader.MeshData? meshData = null; ObjectAnchorsLocation?observationLocation = null; await Task.Run(() => { var vertexPositions = new System.Numerics.Vector3[observation.VertexCount]; var vertexNormals = new System.Numerics.Vector3[vertexPositions.Length]; var triangleIndices = new uint[observation.TriangleIndexCount]; observation.GetVertexPositions(vertexPositions); observation.GetVertexNormals(vertexNormals); observation.GetTriangleIndices(triangleIndices); meshData = new MeshLoader.MeshData(vertexPositions, vertexNormals, triangleIndices); #if SPATIALCOORDINATESYSTEM_API_PRESENT observationLocation = observation.Origin.ToSpatialCoordinateSystem().TryGetTransformTo(ObjectAnchorsWorldManager.WorldOrigin)?.ToUnityLocation(); #endif }); if (observationLocation.HasValue && meshData.HasValue) { MeshLoader.LoadMesh(_meshFilter.mesh, meshData.Value); transform.SetPositionAndRotation(observationLocation.Value.Position, observationLocation.Value.Orientation); } _lastMeshUpdateTime = DateTime.Now; _updateInProgress = false; } }
public static async Task <ObjectAnchorsLocation?> LocateAndSetFromEnvironmentObservation(this Mesh mesh, EnvironmentObservation observation) { ObjectAnchorsLocation?observationLocation = null; await mesh.SetFromSystemMeshDataAsync(() => { observationLocation = observation.Origin.ToSpatialCoordinateSystem().TryGetTransformTo(ObjectAnchorsWorldManager.WorldOrigin)?.ToUnityLocation(); return(SystemMeshData.FromEnvironmentObseration(observation)); }); return(observationLocation); }