Modules: update modules to be built for iOS

Using 'available' checks to fix deprecation compilation errors
Additional checks for simulator
This commit is contained in:
Sergey Minakov 2020-07-15 19:03:01 +03:00
parent 33038be5ed
commit 66be375eb0
4 changed files with 276 additions and 214 deletions

View file

@ -60,8 +60,8 @@ private:
float eye_height, z_near, z_far;
Ref<CameraFeed> feed;
int image_width[2];
int image_height[2];
size_t image_width[2];
size_t image_height[2];
Vector<uint8_t> img_data[2];
struct anchor_map {
@ -84,9 +84,9 @@ public:
void start_session();
void stop_session();
bool get_anchor_detection_is_enabled() const;
void set_anchor_detection_is_enabled(bool p_enable);
virtual int get_camera_feed_id();
bool get_anchor_detection_is_enabled() const override;
void set_anchor_detection_is_enabled(bool p_enable) override;
virtual int get_camera_feed_id() override;
bool get_light_estimation_is_enabled() const;
void set_light_estimation_is_enabled(bool p_enable);
@ -97,22 +97,22 @@ public:
/* while Godot has its own raycast logic this takes ARKits camera into account and hits on any ARAnchor */
Array raycast(Vector2 p_screen_coord);
void notification(int p_what);
virtual void notification(int p_what) override;
virtual StringName get_name() const;
virtual int get_capabilities() const;
virtual StringName get_name() const override;
virtual int get_capabilities() const override;
virtual bool is_initialized() const;
virtual bool initialize();
virtual void uninitialize();
virtual bool is_initialized() const override;
virtual bool initialize() override;
virtual void uninitialize() override;
virtual Size2 get_render_targetsize();
virtual bool is_stereo();
virtual Transform get_transform_for_eye(XRInterface::Eyes p_eye, const Transform &p_cam_transform);
virtual CameraMatrix get_projection_for_eye(XRInterface::Eyes p_eye, real_t p_aspect, real_t p_z_near, real_t p_z_far);
virtual void commit_for_eye(XRInterface::Eyes p_eye, RID p_render_target, const Rect2 &p_screen_rect);
virtual Size2 get_render_targetsize() override;
virtual bool is_stereo() override;
virtual Transform get_transform_for_eye(XRInterface::Eyes p_eye, const Transform &p_cam_transform) override;
virtual CameraMatrix get_projection_for_eye(XRInterface::Eyes p_eye, real_t p_aspect, real_t p_z_near, real_t p_z_far) override;
virtual void commit_for_eye(XRInterface::Eyes p_eye, RID p_render_target, const Rect2 &p_screen_rect) override;
virtual void process();
virtual void process() override;
// called by delegate (void * because C++ and Obj-C don't always mix, should really change all platform/iphone/*.cpp files to .mm)
void _add_or_update_anchor(void *p_anchor);

View file

@ -42,7 +42,9 @@
#include "arkit_session_delegate.h"
// just a dirty workaround for now, declare these as globals. I'll probably encapsulate ARSession and associated logic into an mm object and change ARKitInterface to a normal cpp object that consumes it.
API_AVAILABLE(ios(11.0))
ARSession *ar_session;
ARKitSessionDelegate *ar_delegate;
NSTimeInterval last_timestamp;
@ -55,12 +57,17 @@ void ARKitInterface::start_session() {
if (initialized) {
print_line("Starting ARKit session");
if (@available(iOS 11, *)) {
Class ARWorldTrackingConfigurationClass = NSClassFromString(@"ARWorldTrackingConfiguration");
ARWorldTrackingConfiguration *configuration = [ARWorldTrackingConfigurationClass new];
configuration.lightEstimationEnabled = light_estimation_is_enabled;
if (plane_detection_is_enabled) {
if (@available(iOS 11.3, *)) {
configuration.planeDetection = ARPlaneDetectionVertical | ARPlaneDetectionHorizontal;
} else {
configuration.planeDetection = ARPlaneDetectionHorizontal;
}
} else {
configuration.planeDetection = 0;
}
@ -73,6 +80,7 @@ void ARKitInterface::start_session() {
[ar_session runWithConfiguration:configuration];
}
}
}
void ARKitInterface::stop_session() {
session_was_started = false;
@ -84,20 +92,22 @@ void ARKitInterface::stop_session() {
feed->set_active(false);
}
if (@available(iOS 11.0, *)) {
[ar_session pause];
}
}
}
void ARKitInterface::notification(int p_what) {
// TODO, this is not being called, need to find out why, possibly because this is not a node.
// in that case we need to find a way to get these notifications!
switch (p_what) {
case MainLoop::NOTIFICATION_WM_FOCUS_IN: {
case DisplayServer::WINDOW_EVENT_FOCUS_IN: {
print_line("Focus in");
start_session();
}; break;
case MainLoop::NOTIFICATION_WM_FOCUS_OUT: {
case DisplayServer::WINDOW_EVENT_FOCUS_OUT: {
print_line("Focus out");
stop_session();
@ -162,14 +172,16 @@ int ARKitInterface::get_capabilities() const {
}
Array ARKitInterface::raycast(Vector2 p_screen_coord) {
if (@available(iOS 11, *)) {
Array arr;
Size2 screen_size = OS::get_singleton()->get_window_size();
Size2 screen_size = DisplayServer::get_singleton()->screen_get_size();
CGPoint point;
point.x = p_screen_coord.x / screen_size.x;
point.y = p_screen_coord.y / screen_size.y;
///@TODO maybe give more options here, for now we're taking just ARAchors into account that were found during plane detection keeping their size into account
NSArray<ARHitTestResult *> *results = [ar_session.currentFrame hittest:point types:ARHitTestResultTypeExistingPlaneUsingExtent];
NSArray<ARHitTestResult *> *results = [ar_session.currentFrame hitTest:point types:ARHitTestResultTypeExistingPlaneUsingExtent];
for (ARHitTestResult *result in results) {
Transform transform;
@ -193,6 +205,9 @@ Array ARKitInterface::raycast(Vector2 p_screen_coord) {
}
return arr;
} else {
return Array();
}
}
void ARKitInterface::_bind_methods() {
@ -221,6 +236,7 @@ bool ARKitInterface::initialize() {
XRServer *xr_server = XRServer::get_singleton();
ERR_FAIL_NULL_V(xr_server, false);
if (@available(iOS 11, *)) {
if (!initialized) {
print_line("initializing ARKit");
@ -266,6 +282,9 @@ bool ARKitInterface::initialize() {
}
return true;
} else {
return false;
}
}
void ARKitInterface::uninitialize() {
@ -286,9 +305,12 @@ void ARKitInterface::uninitialize() {
remove_all_anchors();
if (@available(iOS 11.0, *)) {
[ar_session release];
[ar_delegate release];
ar_session = NULL;
}
[ar_delegate release];
ar_delegate = NULL;
initialized = false;
session_was_started = false;
@ -296,15 +318,15 @@ void ARKitInterface::uninitialize() {
}
Size2 ARKitInterface::get_render_targetsize() {
_THREAD_SAFE_METHOD_
// _THREAD_SAFE_METHOD_
Size2 target_size = OS::get_singleton()->get_window_size();
Size2 target_size = DisplayServer::get_singleton()->screen_get_size();
return target_size;
}
Transform ARKitInterface::get_transform_for_eye(XRInterface::Eyes p_eye, const Transform &p_cam_transform) {
_THREAD_SAFE_METHOD_
// _THREAD_SAFE_METHOD_
Transform transform_for_eye;
@ -336,7 +358,7 @@ CameraMatrix ARKitInterface::get_projection_for_eye(XRInterface::Eyes p_eye, rea
}
void ARKitInterface::commit_for_eye(XRInterface::Eyes p_eye, RID p_render_target, const Rect2 &p_screen_rect) {
_THREAD_SAFE_METHOD_
// _THREAD_SAFE_METHOD_
// We must have a valid render target
ERR_FAIL_COND(!p_render_target.is_valid());
@ -345,15 +367,15 @@ void ARKitInterface::commit_for_eye(XRInterface::Eyes p_eye, RID p_render_target
ERR_FAIL_COND(p_screen_rect == Rect2());
// get the size of our screen
Rect2 screen_rect = p_screen_rect;
// Rect2 screen_rect = p_screen_rect;
// screen_rect.position.x += screen_rect.size.x;
// screen_rect.size.x = -screen_rect.size.x;
// screen_rect.position.y += screen_rect.size.y;
// screen_rect.size.y = -screen_rect.size.y;
VSG::rasterizer->set_current_render_target(RID());
VSG::rasterizer->blit_render_target_to_screen(p_render_target, screen_rect, 0);
// VSG::rasterizer->set_current_render_target(RID());
// VSG::rasterizer->blit_render_target_to_screen(p_render_target, screen_rect, 0);
}
XRPositionalTracker *ARKitInterface::get_anchor_for_uuid(const unsigned char *p_uuid) {
@ -432,7 +454,7 @@ void ARKitInterface::remove_all_anchors() {
}
void ARKitInterface::process() {
_THREAD_SAFE_METHOD_
// _THREAD_SAFE_METHOD_
if (@available(iOS 11.0, *)) {
if (initialized) {
@ -443,8 +465,16 @@ void ARKitInterface::process() {
last_timestamp = current_frame.timestamp;
// get some info about our screen and orientation
Size2 screen_size = OS::get_singleton()->get_window_size();
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
Size2 screen_size = DisplayServer::get_singleton()->screen_get_size();
UIInterfaceOrientation orientation = UIInterfaceOrientationUnknown;
if (@available(iOS 13, *)) {
orientation = [UIApplication sharedApplication].delegate.window.windowScene.interfaceOrientation;
#if !defined(TARGET_OS_SIMULATOR) || !TARGET_OS_SIMULATOR
} else {
orientation = [[UIApplication sharedApplication] statusBarOrientation];
#endif
}
// Grab our camera image for our backbuffer
CVPixelBufferRef pixelBuffer = current_frame.capturedImage;
@ -475,27 +505,27 @@ void ARKitInterface::process() {
{
// do Y
int new_width = CVPixelBufferGetWidthOfPlane(pixelBuffer, 0);
int new_height = CVPixelBufferGetHeightOfPlane(pixelBuffer, 0);
int bytes_per_row = CVPixelBufferGetBytesPerRowOfPlane(pixelBuffer, 0);
size_t new_width = CVPixelBufferGetWidthOfPlane(pixelBuffer, 0);
size_t new_height = CVPixelBufferGetHeightOfPlane(pixelBuffer, 0);
size_t bytes_per_row = CVPixelBufferGetBytesPerRowOfPlane(pixelBuffer, 0);
if ((image_width[0] != new_width) || (image_height[0] != new_height)) {
printf("- Camera padding l:%lu r:%lu t:%lu b:%lu\n", extraLeft, extraRight, extraTop, extraBottom);
printf("- Camera Y plane size: %i, %i - %i\n", new_width, new_height, bytes_per_row);
printf("- Camera Y plane size: %lu, %lu - %lu\n", new_width, new_height, bytes_per_row);
image_width[0] = new_width;
image_height[0] = new_height;
img_data[0].resize(new_width * new_height);
}
uint8_t *w = img_data[0].write();
uint8_t *w = img_data[0].ptrw();
if (new_width == bytes_per_row) {
memcpy(w.ptr(), dataY, new_width * new_height);
memcpy(w, dataY, new_width * new_height);
} else {
int offset_a = 0;
int offset_b = extraLeft + (extraTop * bytes_per_row);
for (int r = 0; r < new_height; r++) {
memcpy(w.ptr() + offset_a, dataY + offset_b, new_width);
size_t offset_a = 0;
size_t offset_b = extraLeft + (extraTop * bytes_per_row);
for (size_t r = 0; r < new_height; r++) {
memcpy(w + offset_a, dataY + offset_b, new_width);
offset_a += new_width;
offset_b += bytes_per_row;
}
@ -507,26 +537,26 @@ void ARKitInterface::process() {
{
// do CbCr
int new_width = CVPixelBufferGetWidthOfPlane(pixelBuffer, 1);
int new_height = CVPixelBufferGetHeightOfPlane(pixelBuffer, 1);
int bytes_per_row = CVPixelBufferGetBytesPerRowOfPlane(pixelBuffer, 0);
size_t new_width = CVPixelBufferGetWidthOfPlane(pixelBuffer, 1);
size_t new_height = CVPixelBufferGetHeightOfPlane(pixelBuffer, 1);
size_t bytes_per_row = CVPixelBufferGetBytesPerRowOfPlane(pixelBuffer, 0);
if ((image_width[1] != new_width) || (image_height[1] != new_height)) {
printf("- Camera CbCr plane size: %i, %i - %i\n", new_width, new_height, bytes_per_row);
printf("- Camera CbCr plane size: %lu, %lu - %lu\n", new_width, new_height, bytes_per_row);
image_width[1] = new_width;
image_height[1] = new_height;
img_data[1].resize(2 * new_width * new_height);
}
uint8_t *w = img_data[1].write();
uint8_t *w = img_data[1].ptrw();
if ((2 * new_width) == bytes_per_row) {
memcpy(w.ptr(), dataCbCr, 2 * new_width * new_height);
memcpy(w, dataCbCr, 2 * new_width * new_height);
} else {
int offset_a = 0;
int offset_b = extraLeft + (extraTop * bytes_per_row);
for (int r = 0; r < new_height; r++) {
memcpy(w.ptr() + offset_a, dataCbCr + offset_b, 2 * new_width);
size_t offset_a = 0;
size_t offset_b = extraLeft + (extraTop * bytes_per_row);
for (size_t r = 0; r < new_height; r++) {
memcpy(w + offset_a, dataCbCr + offset_b, 2 * new_width);
offset_a += 2 * new_width;
offset_b += bytes_per_row;
}
@ -658,8 +688,9 @@ void ARKitInterface::process() {
}
void ARKitInterface::_add_or_update_anchor(void *p_anchor) {
_THREAD_SAFE_METHOD_
// _THREAD_SAFE_METHOD_
if (@available(iOS 11.0, *)) {
ARAnchor *anchor = (ARAnchor *)p_anchor;
unsigned char uuid[16];
@ -673,6 +704,7 @@ void ARKitInterface::_add_or_update_anchor(void *p_anchor) {
// can we safely cast this?
ARPlaneAnchor *planeAnchor = (ARPlaneAnchor *)anchor;
if (@available(iOS 11.3, *)) {
if (planeAnchor.geometry.triangleCount > 0) {
Ref<SurfaceTool> surftool;
surftool.instance();
@ -693,6 +725,10 @@ void ARKitInterface::_add_or_update_anchor(void *p_anchor) {
Ref<Mesh> nomesh;
tracker->set_mesh(nomesh);
}
} else {
Ref<Mesh> nomesh;
tracker->set_mesh(nomesh);
}
// Note, this also contains a scale factor which gives us an idea of the size of the anchor
// We may extract that in our XRAnchor class
@ -711,10 +747,12 @@ void ARKitInterface::_add_or_update_anchor(void *p_anchor) {
tracker->set_rw_position(Vector3(m44.columns[3][0], m44.columns[3][1], m44.columns[3][2]));
}
}
}
void ARKitInterface::_remove_anchor(void *p_anchor) {
_THREAD_SAFE_METHOD_
// _THREAD_SAFE_METHOD_
if (@available(iOS 11.0, *)) {
ARAnchor *anchor = (ARAnchor *)p_anchor;
unsigned char uuid[16];
@ -722,13 +760,16 @@ void ARKitInterface::_remove_anchor(void *p_anchor) {
remove_anchor_for_uuid(uuid);
}
}
ARKitInterface::ARKitInterface() {
initialized = false;
session_was_started = false;
plane_detection_is_enabled = false;
light_estimation_is_enabled = false;
if (@available(iOS 11.0, *)) {
ar_session = NULL;
}
z_near = 0.01;
z_far = 1000.0;
projection.set_perspective(60.0, 1.0, z_near, z_far, false);

View file

@ -42,9 +42,9 @@ class ARKitInterface;
@property(nonatomic) ARKitInterface *arkit_interface;
- (void)session:(ARSession *)session didAddAnchors:(NSArray<ARAnchor *> *)anchors;
- (void)session:(ARSession *)session didRemoveAnchors:(NSArray<ARAnchor *> *)anchors;
- (void)session:(ARSession *)session didUpdateAnchors:(NSArray<ARAnchor *> *)anchors;
- (void)session:(ARSession *)session didAddAnchors:(NSArray<ARAnchor *> *)anchors API_AVAILABLE(ios(11.0));
- (void)session:(ARSession *)session didRemoveAnchors:(NSArray<ARAnchor *> *)anchors API_AVAILABLE(ios(11.0));
- (void)session:(ARSession *)session didUpdateAnchors:(NSArray<ARAnchor *> *)anchors API_AVAILABLE(ios(11.0));
@end
#endif /* !ARKIT_SESSION_DELEGATE_H */

View file

@ -158,25 +158,31 @@
} else if (dataCbCr == NULL) {
print_line("Couldn't access CbCr pixel buffer data");
} else {
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
UIInterfaceOrientation orientation = UIInterfaceOrientationUnknown;
if (@available(iOS 13, *)) {
orientation = [UIApplication sharedApplication].delegate.window.windowScene.interfaceOrientation;
#if !defined(TARGET_OS_SIMULATOR) || !TARGET_OS_SIMULATOR
} else {
orientation = [[UIApplication sharedApplication] statusBarOrientation];
#endif
}
Ref<Image> img[2];
{
// do Y
int new_width = CVPixelBufferGetWidthOfPlane(pixelBuffer, 0);
int new_height = CVPixelBufferGetHeightOfPlane(pixelBuffer, 0);
int _bytes_per_row = CVPixelBufferGetBytesPerRowOfPlane(pixelBuffer, 0);
size_t new_width = CVPixelBufferGetWidthOfPlane(pixelBuffer, 0);
size_t new_height = CVPixelBufferGetHeightOfPlane(pixelBuffer, 0);
if ((width[0] != new_width) || (height[0] != new_height)) {
// printf("Camera Y plane %i, %i - %i\n", new_width, new_height, bytes_per_row);
width[0] = new_width;
height[0] = new_height;
img_data[0].resize(new_width * new_height);
}
uint8_t *w = img_data[0].ptrw();
memcpy(w.ptr(), dataY, new_width * new_height);
memcpy(w, dataY, new_width * new_height);
img[0].instance();
img[0]->create(new_width, new_height, 0, Image::FORMAT_R8, img_data[0]);
@ -184,20 +190,17 @@
{
// do CbCr
int new_width = CVPixelBufferGetWidthOfPlane(pixelBuffer, 1);
int new_height = CVPixelBufferGetHeightOfPlane(pixelBuffer, 1);
int bytes_per_row = CVPixelBufferGetBytesPerRowOfPlane(pixelBuffer, 1);
size_t new_width = CVPixelBufferGetWidthOfPlane(pixelBuffer, 1);
size_t new_height = CVPixelBufferGetHeightOfPlane(pixelBuffer, 1);
if ((width[1] != new_width) || (height[1] != new_height)) {
// printf("Camera CbCr plane %i, %i - %i\n", new_width, new_height, bytes_per_row);
width[1] = new_width;
height[1] = new_height;
img_data[1].resize(2 * new_width * new_height);
}
uint8_t *w = img_data[1].ptrw();
memcpy(w.ptr(), dataCbCr, 2 * new_width * new_height);
memcpy(w, dataCbCr, 2 * new_width * new_height);
///TODO GLES2 doesn't support FORMAT_RG8, need to do some form of conversion
img[1].instance();
@ -359,7 +362,24 @@ void CameraIOS::update_feeds() {
// this way of doing things is deprecated but still works,
// rewrite to using AVCaptureDeviceDiscoverySession
AVCaptureDeviceDiscoverySession *session = [AVCaptureDeviceDiscoverySession discoverySessionWithDeviceTypes:[NSArray arrayWithObjects:AVCaptureDeviceTypeBuiltInTelephotoCamera, AVCaptureDeviceTypeBuiltInDualCamera, AVCaptureDeviceTypeBuiltInTrueDepthCamera, AVCaptureDeviceTypeBuiltInWideAngleCamera, nil] mediaType:AVMediaTypeVideo position:AVCaptureDevicePositionUnspecified];
NSMutableArray *deviceTypes = [NSMutableArray array];
if (@available(iOS 10, *)) {
[deviceTypes addObject:AVCaptureDeviceTypeBuiltInWideAngleCamera];
[deviceTypes addObject:AVCaptureDeviceTypeBuiltInTelephotoCamera];
if (@available(iOS 10.2, *)) {
[deviceTypes addObject:AVCaptureDeviceTypeBuiltInDualCamera];
}
if (@available(iOS 11.1, *)) {
[deviceTypes addObject:AVCaptureDeviceTypeBuiltInTrueDepthCamera];
}
AVCaptureDeviceDiscoverySession *session = [AVCaptureDeviceDiscoverySession
discoverySessionWithDeviceTypes:deviceTypes
mediaType:AVMediaTypeVideo
position:AVCaptureDevicePositionUnspecified];
// remove devices that are gone..
for (int i = feeds.size() - 1; i >= 0; i--) {
@ -394,6 +414,7 @@ void CameraIOS::update_feeds() {
add_feed(newfeed);
};
};
}
};
CameraIOS::CameraIOS() {