• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include "camera.h"
16 
17 #include <scene/ext/intf_render_resource.h>
18 
19 #include <3d/ecs/components/camera_component.h>
20 #include <3d/ecs/components/render_handle_component.h>
21 #include <render/device/gpu_resource_desc.h>
22 #include <render/device/intf_gpu_resource_manager.h>
23 
24 #include "util.h"
25 
SCENE_BEGIN_NAMESPACE()26 SCENE_BEGIN_NAMESPACE()
27 bool UpdateCameraRenderTarget(const IEcsObject::Ptr& camera, const IRenderTarget::Ptr& target)
28 {
29     if (auto ecs = GetEcs(camera)) {
30         CORE_NS::EntityReference ent;
31         BASE_NS::Math::UVec2 size;
32         if (target) {
33             ent = HandleFromRenderResource(camera->GetScene(), target->GetRenderHandle());
34             if (ent) {
35                 size = target->Size()->GetValue();
36             }
37         }
38         bool changed = false;
39         bool success = false;
40         if (auto c = GetScopedHandle<const CORE3D_NS::CameraComponent>(camera)) {
41             changed = changed || c->renderResolution[0] != size.x;
42             changed = changed || c->renderResolution[1] != size.y;
43             if ((ent && c->customColorTargets.empty()) ||
44                 (!c->customColorTargets.empty() && c->customColorTargets[0] != ent)) {
45                 changed = true;
46             }
47             success = true;
48         }
49         if (changed) {
50             if (auto c = GetScopedHandle<CORE3D_NS::CameraComponent>(camera)) {
51                 if (ent) {
52                     c->renderResolution[0] = static_cast<float>(size.x);
53                     c->renderResolution[1] = static_cast<float>(size.y);
54                     c->customColorTargets = { ent };
55                 } else {
56                     c->renderResolution[0] = 0.0;
57                     c->renderResolution[1] = 0.0;
58                     c->customColorTargets = {};
59                 }
60             }
61         }
62         return success;
63     }
64     CORE_LOG_W("Failed to set camera render target");
65     return false;
66 }
67 SCENE_END_NAMESPACE()
68