Path: blob/master/scene/resources/camera_texture.cpp
20909 views
/**************************************************************************/1/* camera_texture.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "camera_texture.h"3132#include "servers/camera/camera_feed.h"33#include "servers/rendering/rendering_server.h"3435void CameraTexture::_bind_methods() {36ClassDB::bind_method(D_METHOD("set_camera_feed_id", "feed_id"), &CameraTexture::set_camera_feed_id);37ClassDB::bind_method(D_METHOD("get_camera_feed_id"), &CameraTexture::get_camera_feed_id);3839ClassDB::bind_method(D_METHOD("set_which_feed", "which_feed"), &CameraTexture::set_which_feed);40ClassDB::bind_method(D_METHOD("get_which_feed"), &CameraTexture::get_which_feed);4142ClassDB::bind_method(D_METHOD("set_camera_active", "active"), &CameraTexture::set_camera_active);43ClassDB::bind_method(D_METHOD("get_camera_active"), &CameraTexture::get_camera_active);4445ADD_PROPERTY(PropertyInfo(Variant::INT, "camera_feed_id"), "set_camera_feed_id", "get_camera_feed_id");46ADD_PROPERTY(PropertyInfo(Variant::INT, "which_feed"), "set_which_feed", "get_which_feed");47ADD_PROPERTY(PropertyInfo(Variant::BOOL, "camera_is_active"), "set_camera_active", "get_camera_active");48ADD_PROPERTY_DEFAULT("camera_is_active", false);49}5051void CameraTexture::_on_format_changed() {52// FIXME: `emit_changed` is more appropriate, but causes errors for some reason.53callable_mp((Resource *)this, &Resource::emit_changed).call_deferred();54}5556int CameraTexture::get_width() const {57Ref<CameraFeed> feed = CameraServer::get_singleton()->get_feed_by_id(camera_feed_id);58if (feed.is_valid()) {59return feed->get_base_width();60} else {61return 0;62}63}6465int CameraTexture::get_height() const {66Ref<CameraFeed> feed = CameraServer::get_singleton()->get_feed_by_id(camera_feed_id);67if (feed.is_valid()) {68return feed->get_base_height();69} else {70return 0;71}72}7374bool CameraTexture::has_alpha() const {75return false;76}7778RID CameraTexture::get_rid() const {79Ref<CameraFeed> feed = CameraServer::get_singleton()->get_feed_by_id(camera_feed_id);80if (feed.is_valid()) {81return feed->get_texture(which_feed);82} else {83if (_texture.is_null()) {84_texture = RenderingServer::get_singleton()->texture_2d_placeholder_create();85}86return _texture;87}88}8990Ref<Image> CameraTexture::get_image() const {91return RenderingServer::get_singleton()->texture_2d_get(get_rid());92}9394void CameraTexture::set_camera_feed_id(int p_new_id) {95Ref<CameraFeed> feed = CameraServer::get_singleton()->get_feed_by_id(camera_feed_id);96if (feed.is_valid()) {97if (feed->is_connected("format_changed", callable_mp(this, &CameraTexture::_on_format_changed))) {98feed->disconnect("format_changed", callable_mp(this, &CameraTexture::_on_format_changed));99}100}101102camera_feed_id = p_new_id;103104feed = CameraServer::get_singleton()->get_feed_by_id(camera_feed_id);105if (feed.is_valid()) {106feed->connect("format_changed", callable_mp(this, &CameraTexture::_on_format_changed));107}108109notify_property_list_changed();110callable_mp((Resource *)this, &Resource::emit_changed).call_deferred();111}112113int CameraTexture::get_camera_feed_id() const {114return camera_feed_id;115}116117void CameraTexture::set_which_feed(CameraServer::FeedImage p_which) {118which_feed = p_which;119notify_property_list_changed();120callable_mp((Resource *)this, &Resource::emit_changed).call_deferred();121}122123CameraServer::FeedImage CameraTexture::get_which_feed() const {124return which_feed;125}126127void CameraTexture::set_camera_active(bool p_active) {128Ref<CameraFeed> feed = CameraServer::get_singleton()->get_feed_by_id(camera_feed_id);129if (feed.is_valid()) {130feed->set_active(p_active);131notify_property_list_changed();132callable_mp((Resource *)this, &Resource::emit_changed).call_deferred();133}134}135136bool CameraTexture::get_camera_active() const {137Ref<CameraFeed> feed = CameraServer::get_singleton()->get_feed_by_id(camera_feed_id);138if (feed.is_valid()) {139return feed->is_active();140} else {141return false;142}143}144145CameraTexture::CameraTexture() {146// Note: When any CameraTexture is created, we need to automatically activate monitoring147// of camera feeds. This may incur a small lag spike, so it may be preferable to148// enable it manually before creating the camera texture.149CameraServer::get_singleton()->set_monitoring_feeds(true);150}151152CameraTexture::~CameraTexture() {153if (_texture.is_valid()) {154ERR_FAIL_NULL(RenderingServer::get_singleton());155RenderingServer::get_singleton()->free_rid(_texture);156}157}158159160