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 #ifndef CAMERA_JS_H 16 #define CAMERA_JS_H 17 18 #include <meta/interface/intf_object.h> 19 #include <scene/interface/intf_raycast.h> 20 21 #include <base/containers/unordered_map.h> 22 23 #include "BaseObjectJS.h" 24 #include "ColorProxy.h" 25 #include "NodeImpl.h" 26 27 class CameraJS : public BaseObject<CameraJS>, public NodeImpl { 28 public: 29 static constexpr uint32_t ID = 80; 30 static void Init(napi_env env, napi_value exports); 31 CameraJS(napi_env, napi_callback_info); 32 ~CameraJS() override; 33 virtual void* GetInstanceImpl(uint32_t) override; 34 35 META_NS::IObject::Ptr CreateObject(const META_NS::ClassInfo&); 36 void ReleaseObject(const META_NS::IObject::Ptr&); 37 38 private: 39 void DisposeNative(void*) override; 40 void Finalize(napi_env env) override; 41 napi_value GetFov(NapiApi::FunctionContext<>& ctx); 42 void SetFov(NapiApi::FunctionContext<float>& ctx); 43 44 napi_value GetFar(NapiApi::FunctionContext<>& ctx); 45 void SetFar(NapiApi::FunctionContext<float>& ctx); 46 napi_value GetNear(NapiApi::FunctionContext<>& ctx); 47 void SetNear(NapiApi::FunctionContext<float>& ctx); 48 49 napi_value GetPostProcess(NapiApi::FunctionContext<>& ctx); 50 void SetPostProcess(NapiApi::FunctionContext<NapiApi::Object>& ctx); 51 52 napi_value GetEnabled(NapiApi::FunctionContext<>& ctx); 53 void SetEnabled(NapiApi::FunctionContext<bool>& ctx); 54 55 napi_value GetMSAA(NapiApi::FunctionContext<>& ctx); 56 void SetMSAA(NapiApi::FunctionContext<bool>& ctx); 57 58 napi_value GetColor(NapiApi::FunctionContext<>& ctx); 59 void SetColor(NapiApi::FunctionContext<NapiApi::Object>& ctx); 60 61 napi_value WorldToScreen(NapiApi::FunctionContext<NapiApi::Object>& ctx); 62 napi_value ScreenToWorld(NapiApi::FunctionContext<NapiApi::Object>& ctx); 63 enum class ProjectionDirection { WORLD_TO_SCREEN, SCREEN_TO_WORLD }; 64 template<ProjectionDirection dir> 65 napi_value ProjectCoords(NapiApi::FunctionContext<NapiApi::Object>& ctx); 66 67 napi_value Raycast(NapiApi::FunctionContext<NapiApi::Object, NapiApi::Object>& ctx); 68 napi_value Raycast(napi_env env, NapiApi::Object screenCoordJs, NapiApi::Object options); 69 template<typename CoordType> 70 bool ExtractRaycastStuff(const NapiApi::Object& jsCoord, NapiApi::StrongRef& scene, 71 SCENE_NS::ICameraRayCast::Ptr& raycastSelf, CoordType& nativeCoord); 72 73 BASE_NS::unique_ptr<ColorProxy> clearColor_; 74 NapiApi::StrongRef postProc_; 75 76 BASE_NS::unordered_map<uintptr_t, META_NS::IObject::Ptr> resources_; 77 78 bool msaaEnabled_{false}; 79 bool clearColorEnabled_{false}; 80 }; 81 #endif 82