|
| 1 | +/*************************************************************************/ |
| 2 | +/* arkit_interface.h */ |
| 3 | +/*************************************************************************/ |
| 4 | +/* This file is part of: */ |
| 5 | +/* GODOT ENGINE */ |
| 6 | +/* https://godotengine.org */ |
| 7 | +/*************************************************************************/ |
| 8 | +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ |
| 9 | +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ |
| 10 | +/* */ |
| 11 | +/* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | +/* a copy of this software and associated documentation files (the */ |
| 13 | +/* "Software"), to deal in the Software without restriction, including */ |
| 14 | +/* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | +/* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | +/* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | +/* the following conditions: */ |
| 18 | +/* */ |
| 19 | +/* The above copyright notice and this permission notice shall be */ |
| 20 | +/* included in all copies or substantial portions of the Software. */ |
| 21 | +/* */ |
| 22 | +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ |
| 25 | +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | +/*************************************************************************/ |
| 30 | + |
| 31 | +#ifndef ARKIT_INTERFACE_H |
| 32 | +#define ARKIT_INTERFACE_H |
| 33 | + |
| 34 | +#include "servers/camera/camera_feed.h" |
| 35 | +#include "servers/xr/xr_interface.h" |
| 36 | +#include "servers/xr/xr_positional_tracker.h" |
| 37 | + |
| 38 | +/** |
| 39 | + @author Bastiaan Olij <[email protected]> |
| 40 | +
|
| 41 | + ARKit interface between iPhone and Godot |
| 42 | +*/ |
| 43 | + |
| 44 | +// forward declaration for some needed objects |
| 45 | +class ARKitShader; |
| 46 | + |
| 47 | +#ifdef __OBJC__ |
| 48 | + |
| 49 | +typedef ARAnchor GodotARAnchor; |
| 50 | + |
| 51 | +#else |
| 52 | + |
| 53 | +typedef void GodotARAnchor; |
| 54 | +#endif |
| 55 | + |
| 56 | +class ARKitInterface : public XRInterface { |
| 57 | + GDCLASS(ARKitInterface, XRInterface); |
| 58 | + |
| 59 | +private: |
| 60 | + bool initialized; |
| 61 | + bool session_was_started; |
| 62 | + bool plane_detection_is_enabled; |
| 63 | + bool light_estimation_is_enabled; |
| 64 | + real_t ambient_intensity; |
| 65 | + real_t ambient_color_temperature; |
| 66 | + |
| 67 | + Transform transform; |
| 68 | + CameraMatrix projection; |
| 69 | + float eye_height, z_near, z_far; |
| 70 | + |
| 71 | + Ref<CameraFeed> feed; |
| 72 | + size_t image_width[2]; |
| 73 | + size_t image_height[2]; |
| 74 | + Vector<uint8_t> img_data[2]; |
| 75 | + |
| 76 | + struct anchor_map { |
| 77 | + XRPositionalTracker *tracker; |
| 78 | + unsigned char uuid[16]; |
| 79 | + }; |
| 80 | + |
| 81 | + ///@TODO should use memory map object from Godot? |
| 82 | + unsigned int num_anchors; |
| 83 | + unsigned int max_anchors; |
| 84 | + anchor_map *anchors; |
| 85 | + XRPositionalTracker *get_anchor_for_uuid(const unsigned char *p_uuid); |
| 86 | + void remove_anchor_for_uuid(const unsigned char *p_uuid); |
| 87 | + void remove_all_anchors(); |
| 88 | + |
| 89 | +protected: |
| 90 | + static void _bind_methods(); |
| 91 | + |
| 92 | +public: |
| 93 | + void start_session(); |
| 94 | + void stop_session(); |
| 95 | + |
| 96 | + bool get_anchor_detection_is_enabled() const override; |
| 97 | + void set_anchor_detection_is_enabled(bool p_enable) override; |
| 98 | + virtual int get_camera_feed_id() override; |
| 99 | + |
| 100 | + bool get_light_estimation_is_enabled() const; |
| 101 | + void set_light_estimation_is_enabled(bool p_enable); |
| 102 | + |
| 103 | + real_t get_ambient_intensity() const; |
| 104 | + real_t get_ambient_color_temperature() const; |
| 105 | + |
| 106 | + /* while Godot has its own raycast logic this takes ARKits camera into account and hits on any ARAnchor */ |
| 107 | + Array raycast(Vector2 p_screen_coord); |
| 108 | + |
| 109 | + virtual void notification(int p_what) override; |
| 110 | + |
| 111 | + virtual StringName get_name() const override; |
| 112 | + virtual int get_capabilities() const override; |
| 113 | + |
| 114 | + virtual bool is_initialized() const override; |
| 115 | + virtual bool initialize() override; |
| 116 | + virtual void uninitialize() override; |
| 117 | + |
| 118 | + virtual Size2 get_render_targetsize() override; |
| 119 | + virtual bool is_stereo() override; |
| 120 | + virtual Transform get_transform_for_eye(XRInterface::Eyes p_eye, const Transform &p_cam_transform) override; |
| 121 | + 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; |
| 122 | + virtual void commit_for_eye(XRInterface::Eyes p_eye, RID p_render_target, const Rect2 &p_screen_rect) override; |
| 123 | + |
| 124 | + virtual void process() override; |
| 125 | + |
| 126 | + // called by delegate (void * because C++ and Obj-C don't always mix, should really change all platform/iphone/*.cpp files to .mm) |
| 127 | + void _add_or_update_anchor(GodotARAnchor *p_anchor); |
| 128 | + void _remove_anchor(GodotARAnchor *p_anchor); |
| 129 | + |
| 130 | + ARKitInterface(); |
| 131 | + ~ARKitInterface(); |
| 132 | +}; |
| 133 | + |
| 134 | +#endif /* !ARKIT_INTERFACE_H */ |
0 commit comments