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 "AnimationJS.h"
16 #include "CameraJS.h"
17 #include "EnvironmentJS.h"
18 #include "GeometryJS.h"
19 #include "ImageJS.h"
20 #include "LightJS.h"
21 #include "MaterialJS.h"
22 #include "MeshJS.h"
23 #include "NodeJS.h"
24 #include "PostProcJS.h"
25 #include "SceneJS.h"
26 #include "ShaderJS.h"
27 #include "SubMeshJS.h"
28 #include "ToneMapJS.h"
29 
RegisterClasses(napi_env env,napi_value exports)30 void RegisterClasses(napi_env env, napi_value exports)
31 {
32     napi_status status;
33     napi_value zero;
34     napi_value one;
35     NapiApi::MyInstanceState* mis;
36     napi_get_instance_data(env, (void**)&mis);
37 
38     status = napi_create_double(env, 0.0, &zero);
39     status = napi_create_double(env, 1.0, &one);
40 
41     // Declare color class
42     {
43         /// Color
44         auto colorCtor = [](napi_env e, napi_callback_info c) -> napi_value { return {}; };
45 
46         // clang-format off
47         napi_property_descriptor desc4[] = {
48             {"r", nullptr, nullptr, nullptr, nullptr, zero, napi_default_jsproperty, nullptr},
49             {"g", nullptr, nullptr, nullptr, nullptr, zero, napi_default_jsproperty, nullptr},
50             {"b", nullptr, nullptr, nullptr, nullptr, zero, napi_default_jsproperty, nullptr},
51             {"a", nullptr, nullptr, nullptr, nullptr, zero, napi_default_jsproperty, nullptr}
52         };
53         // clang-format on
54         napi_value color_class = nullptr;
55         status = napi_define_class(
56             env, "Color", NAPI_AUTO_LENGTH, colorCtor, nullptr, BASE_NS::countof(desc4), desc4, &color_class);
57         mis->StoreCtor("Color", color_class);
58     }
59     // Declare math classes.. "simply" for now.
60     {
61         /// Vec
62         auto vec3Ctor = [](napi_env e, napi_callback_info c) -> napi_value { return {}; };
63 
64         // clang-format off
65         napi_property_descriptor desc3[] = {
66             {"x", nullptr, nullptr, nullptr, nullptr, zero, napi_default_jsproperty, nullptr},
67             {"y", nullptr, nullptr, nullptr, nullptr, zero, napi_default_jsproperty, nullptr},
68             {"z", nullptr, nullptr, nullptr, nullptr, zero, napi_default_jsproperty, nullptr}
69         };
70         // clang-format on
71         napi_value vec3_class = nullptr;
72         status = napi_define_class(
73             env, "Vec3", NAPI_AUTO_LENGTH, vec3Ctor, nullptr, BASE_NS::countof(desc3), desc3, &vec3_class);
74         mis->StoreCtor("Vec3", vec3_class);
75 
76         /// Vec4
77         auto vec4Ctor = [](napi_env e, napi_callback_info c) -> napi_value { return {}; };
78 
79         // clang-format off
80         napi_property_descriptor desc4[] = {
81             {"x", nullptr, nullptr, nullptr, nullptr, zero, napi_default_jsproperty, nullptr},
82             {"y", nullptr, nullptr, nullptr, nullptr, zero, napi_default_jsproperty, nullptr},
83             {"z", nullptr, nullptr, nullptr, nullptr, zero, napi_default_jsproperty, nullptr},
84             {"w", nullptr, nullptr, nullptr, nullptr, zero, napi_default_jsproperty, nullptr}
85         };
86         // clang-format on
87         napi_value vec4_class = nullptr;
88         status = napi_define_class(
89             env, "Vec4", NAPI_AUTO_LENGTH, vec4Ctor, nullptr, BASE_NS::countof(desc4), desc4, &vec4_class);
90         mis->StoreCtor("Vec4", vec4_class);
91 
92         /// Quaternion
93         auto QuatCtor = [](napi_env e, napi_callback_info c) -> napi_value { return {}; };
94 
95         // clang-format off
96         napi_property_descriptor qdesc[] = {
97             {"x", nullptr, nullptr, nullptr, nullptr, zero, napi_default_jsproperty, nullptr},
98             {"y", nullptr, nullptr, nullptr, nullptr, zero, napi_default_jsproperty, nullptr},
99             {"z", nullptr, nullptr, nullptr, nullptr, zero, napi_default_jsproperty, nullptr},
100             {"w", nullptr, nullptr, nullptr, nullptr, one, napi_default_jsproperty, nullptr}
101         };
102         // clang-format on
103         napi_value quaternion_class = nullptr;
104         status = napi_define_class(
105             env, "Quaternion", NAPI_AUTO_LENGTH, QuatCtor, nullptr, BASE_NS::countof(qdesc), qdesc, &quaternion_class);
106         mis->StoreCtor("Quaternion", quaternion_class);
107     }
108 
109     napi_value scene3dNS = exports;
110     SceneJS::Init(env, scene3dNS);
111     NodeJS::Init(env, scene3dNS);
112     CameraJS::Init(env, scene3dNS);
113     EnvironmentJS::Init(env, scene3dNS);
114     PointLightJS::Init(env, scene3dNS);
115     DirectionalLightJS::Init(env, scene3dNS);
116     SpotLightJS::Init(env, scene3dNS);
117     GeometryJS::Init(env, scene3dNS);
118     MeshJS::Init(env, scene3dNS);
119     SubMeshJS::Init(env, scene3dNS);
120     ShaderMaterialJS::Init(env, scene3dNS);
121     ImageJS::Init(env, scene3dNS);
122     PostProcJS::Init(env, scene3dNS);
123     ToneMapJS::Init(env, scene3dNS);
124     ShaderJS::Init(env, scene3dNS);
125     AnimationJS::Init(env, scene3dNS);
126 
127     BaseLight::RegisterEnums({ env, scene3dNS });
128     NodeImpl::RegisterEnums({ env, scene3dNS });
129     SceneResourceImpl::RegisterEnums({ env, scene3dNS });
130 }