Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/resources/camera_texture.cpp
9902 views
1
/**************************************************************************/
2
/* camera_texture.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
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
#include "camera_texture.h"
32
33
#include "servers/camera/camera_feed.h"
34
35
void CameraTexture::_bind_methods() {
36
ClassDB::bind_method(D_METHOD("set_camera_feed_id", "feed_id"), &CameraTexture::set_camera_feed_id);
37
ClassDB::bind_method(D_METHOD("get_camera_feed_id"), &CameraTexture::get_camera_feed_id);
38
39
ClassDB::bind_method(D_METHOD("set_which_feed", "which_feed"), &CameraTexture::set_which_feed);
40
ClassDB::bind_method(D_METHOD("get_which_feed"), &CameraTexture::get_which_feed);
41
42
ClassDB::bind_method(D_METHOD("set_camera_active", "active"), &CameraTexture::set_camera_active);
43
ClassDB::bind_method(D_METHOD("get_camera_active"), &CameraTexture::get_camera_active);
44
45
ADD_PROPERTY(PropertyInfo(Variant::INT, "camera_feed_id"), "set_camera_feed_id", "get_camera_feed_id");
46
ADD_PROPERTY(PropertyInfo(Variant::INT, "which_feed"), "set_which_feed", "get_which_feed");
47
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "camera_is_active"), "set_camera_active", "get_camera_active");
48
ADD_PROPERTY_DEFAULT("camera_is_active", false);
49
}
50
51
void CameraTexture::_on_format_changed() {
52
// FIXME: `emit_changed` is more appropriate, but causes errors for some reason.
53
callable_mp((Resource *)this, &Resource::emit_changed).call_deferred();
54
}
55
56
int CameraTexture::get_width() const {
57
Ref<CameraFeed> feed = CameraServer::get_singleton()->get_feed_by_id(camera_feed_id);
58
if (feed.is_valid()) {
59
return feed->get_base_width();
60
} else {
61
return 0;
62
}
63
}
64
65
int CameraTexture::get_height() const {
66
Ref<CameraFeed> feed = CameraServer::get_singleton()->get_feed_by_id(camera_feed_id);
67
if (feed.is_valid()) {
68
return feed->get_base_height();
69
} else {
70
return 0;
71
}
72
}
73
74
bool CameraTexture::has_alpha() const {
75
return false;
76
}
77
78
RID CameraTexture::get_rid() const {
79
Ref<CameraFeed> feed = CameraServer::get_singleton()->get_feed_by_id(camera_feed_id);
80
if (feed.is_valid()) {
81
return feed->get_texture(which_feed);
82
} else {
83
if (_texture.is_null()) {
84
_texture = RenderingServer::get_singleton()->texture_2d_placeholder_create();
85
}
86
return _texture;
87
}
88
}
89
90
Ref<Image> CameraTexture::get_image() const {
91
return RenderingServer::get_singleton()->texture_2d_get(get_rid());
92
}
93
94
void CameraTexture::set_camera_feed_id(int p_new_id) {
95
Ref<CameraFeed> feed = CameraServer::get_singleton()->get_feed_by_id(camera_feed_id);
96
if (feed.is_valid()) {
97
if (feed->is_connected("format_changed", callable_mp(this, &CameraTexture::_on_format_changed))) {
98
feed->disconnect("format_changed", callable_mp(this, &CameraTexture::_on_format_changed));
99
}
100
}
101
102
camera_feed_id = p_new_id;
103
104
feed = CameraServer::get_singleton()->get_feed_by_id(camera_feed_id);
105
if (feed.is_valid()) {
106
feed->connect("format_changed", callable_mp(this, &CameraTexture::_on_format_changed));
107
}
108
109
notify_property_list_changed();
110
callable_mp((Resource *)this, &Resource::emit_changed).call_deferred();
111
}
112
113
int CameraTexture::get_camera_feed_id() const {
114
return camera_feed_id;
115
}
116
117
void CameraTexture::set_which_feed(CameraServer::FeedImage p_which) {
118
which_feed = p_which;
119
notify_property_list_changed();
120
callable_mp((Resource *)this, &Resource::emit_changed).call_deferred();
121
}
122
123
CameraServer::FeedImage CameraTexture::get_which_feed() const {
124
return which_feed;
125
}
126
127
void CameraTexture::set_camera_active(bool p_active) {
128
Ref<CameraFeed> feed = CameraServer::get_singleton()->get_feed_by_id(camera_feed_id);
129
if (feed.is_valid()) {
130
feed->set_active(p_active);
131
notify_property_list_changed();
132
callable_mp((Resource *)this, &Resource::emit_changed).call_deferred();
133
}
134
}
135
136
bool CameraTexture::get_camera_active() const {
137
Ref<CameraFeed> feed = CameraServer::get_singleton()->get_feed_by_id(camera_feed_id);
138
if (feed.is_valid()) {
139
return feed->is_active();
140
} else {
141
return false;
142
}
143
}
144
145
CameraTexture::CameraTexture() {
146
// Note: When any CameraTexture is created, we need to automatically activate monitoring
147
// of camera feeds. This may incur a small lag spike, so it may be preferable to
148
// enable it manually before creating the camera texture.
149
CameraServer::get_singleton()->set_monitoring_feeds(true);
150
}
151
152
CameraTexture::~CameraTexture() {
153
if (_texture.is_valid()) {
154
ERR_FAIL_NULL(RenderingServer::get_singleton());
155
RenderingServer::get_singleton()->free(_texture);
156
}
157
}
158
159