• 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 
16 #include "Raycast.h"
17 
18 #include <meta/interface/intf_object.h>
19 #include <napi_api.h>
20 #include <scene/interface/component_util.h>
21 #include <scene/interface/intf_raycast.h>
22 
23 #include "BaseObjectJS.h"
24 #include "Vec3Proxy.h"
25 
CreateRaycastResult(NapiApi::StrongRef scene,napi_env env,SCENE_NS::NodeHit hitResult)26 NapiApi::Object CreateRaycastResult(NapiApi::StrongRef scene, napi_env env, SCENE_NS::NodeHit hitResult)
27 {
28     auto result = NapiApi::Object { env };
29 
30     const auto nativeNode = interface_pointer_cast<META_NS::IObject>(hitResult.node);
31     auto jsNode = FetchJsObj(nativeNode);
32     if (!jsNode) {
33         const bool storeStrongRef = false; // Nodes are owned by the scene.
34         napi_value args[] = { scene.GetValue(), NapiApi::Object { env }.ToNapiValue() };
35         jsNode = CreateFromNativeInstance(env, nativeNode, storeStrongRef, BASE_NS::countof(args), args);
36     }
37 
38     auto centerDistance = napi_value {};
39     auto distance = napi_value {};
40     napi_create_double(env, hitResult.distance, &distance);
41     napi_create_double(env, hitResult.distanceToCenter, &centerDistance);
42 
43     result.Set("node", jsNode);
44     result.Set("centerDistance", centerDistance);
45     result.Set("distance", distance);
46     result.Set("hitPosition", Vec3Proxy::ToNapiObject(hitResult.position, env));
47     return result;
48 }
49 
ToNativeOptions(napi_env env,NapiApi::Object raycastParameters)50 SCENE_NS::RayCastOptions ToNativeOptions(napi_env env, NapiApi::Object raycastParameters)
51 {
52     auto layerMask = uint64_t {};
53     auto jsObj = NapiApi::Object { env, raycastParameters.Get("layerMask") };
54     if (auto rootObject = jsObj.Native<TrueRootObject>()) {
55         // Layer masks are served as Node objects to the JS side.
56         if (auto nativeNode = interface_pointer_cast<SCENE_NS::INode>(rootObject->GetNativeObject())) {
57             if (auto layerComponent = SCENE_NS::GetComponent<SCENE_NS::ILayer>(nativeNode)) {
58                 layerMask = layerComponent->LayerMask()->GetValue();
59             }
60         }
61     }
62 
63     auto rootNode = SCENE_NS::INode::ConstPtr {};
64     auto anotherjsObj = NapiApi::Object { env, raycastParameters.Get("rootNode") };
65     if (auto rootObject = anotherjsObj.Native<TrueRootObject>()) {
66         rootNode = interface_pointer_cast<SCENE_NS::INode>(rootObject->GetNativeObject());
67     }
68     return { layerMask, rootNode };
69 }
70