public void UpdateVirtualObjectPosition(VirtualObject virtualObject, SCNVector3 position, bool filterPosition, NMatrix4 cameraTransform) { var cameraWorldPos = cameraTransform.Translation(); var cameraToPosition = position.Subtract(cameraWorldPos); // Limit the distance of the object from the camera to a maximum of 10m if (cameraToPosition.LengthFast > 10) { cameraToPosition = cameraToPosition.Normalized() * 10; } // Compute the average distance of the object from the camera over the last ten // updates. If filterPosition is true, compute a new position for the object // with this average. Notice that the distance is applied to the vector from // the camera to the content, so it only affects the percieved distance of the // object - the averaging does _not_ make the content "lag". var hitTestResultDistance = cameraToPosition.LengthFast; virtualObject.RecentVirtualObjectDistances.Add(hitTestResultDistance); virtualObject.RecentVirtualObjectDistances.KeepLast(10); if (filterPosition) { var averageDistance = virtualObject.RecentVirtualObjectDistances.Average(); var averagedDistancePos = cameraWorldPos + cameraToPosition.Normalized() * averageDistance; virtualObject.Position = averagedDistancePos; } else { virtualObject.Position = cameraWorldPos + cameraToPosition; } }
private void SetNewVirtualObjectPosition(VirtualObject virtualObject, SCNVector3 position, NMatrix4 cameraTransform) { var cameraWorldPos = cameraTransform.Translation(); var cameraToPosition = position.Subtract(cameraWorldPos); // Limit the distance of the object from the camera to a maximum of 10m if (cameraToPosition.LengthFast > 10) { cameraToPosition = cameraToPosition.Normalized() * 10; } virtualObject.Position = cameraWorldPos + cameraToPosition; virtualObject.RecentVirtualObjectDistances.Clear(); }