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