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 MATERIAL_JS_H 16 #define MATERIAL_JS_H 17 #include <meta/interface/intf_object.h> 18 19 #include "BaseObjectJS.h" 20 #include "SceneResourceImpl.h" 21 22 class BaseMaterial : public SceneResourceImpl { 23 public: 24 static constexpr uint32_t ID = 30; 25 enum MaterialType { SHADER = 1, METALLIC_ROUGHNESS = 2 }; 26 enum CullMode { NONE = 0, FRONT = 1, BACK = 2 }; 27 static void Init(const char* name, napi_env env, napi_value exports, napi_callback ctor, 28 BASE_NS::vector<napi_property_descriptor>& props); 29 BaseMaterial(MaterialType lt); 30 ~BaseMaterial() override; 31 32 protected: 33 void* GetInstanceImpl(uint32_t); 34 void DisposeNative(BaseObject*); 35 36 MaterialType materialType_; 37 38 private: 39 napi_value GetMaterialType(NapiApi::FunctionContext<>& ctx); 40 napi_value GetShadowReceiver(NapiApi::FunctionContext<>& ctx); 41 void SetShadowReceiver(NapiApi::FunctionContext<bool>& ctx); 42 napi_value GetCullMode(NapiApi::FunctionContext<>& ctx); 43 void SetCullMode(NapiApi::FunctionContext<uint32_t>& ctx); 44 napi_value GetBlend(NapiApi::FunctionContext<>& ctx); 45 void SetBlend(NapiApi::FunctionContext<NapiApi::Object>& ctx); 46 napi_value GetAlphaCutoff(NapiApi::FunctionContext<>& ctx); 47 void SetAlphaCutoff(NapiApi::FunctionContext<float>& ctx); 48 napi_value GetRenderSort(NapiApi::FunctionContext<>& ctx); 49 void SetRenderSort(NapiApi::FunctionContext<NapiApi::Object>& ctx); 50 }; 51 52 class MaterialJS : BaseObject, BaseMaterial { 53 public: 54 static constexpr uint32_t ID = 31; 55 static void Init(napi_env env, napi_value exports); 56 MaterialJS(napi_env, napi_callback_info); 57 ~MaterialJS() override; 58 59 private: 60 void* GetInstanceImpl(uint32_t) override; 61 void DisposeNative(void*) override; 62 void Finalize(napi_env env) override; 63 64 napi_value GetColorShader(NapiApi::FunctionContext<>& ctx); 65 void SetColorShader(NapiApi::FunctionContext<NapiApi::Object>& ctx); 66 67 template<size_t Index> 68 napi_value GetMaterialProperty(NapiApi::FunctionContext<>& ctx); 69 template<size_t Index> 70 void SetMaterialProperty(NapiApi::FunctionContext<NapiApi::Object>& ctx); 71 72 META_NS::IObject::Ptr shaderBind_; 73 NapiApi::StrongRef shader_; 74 }; 75 76 #endif 77