2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2014-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
|
|
|
|
2022-10-01 16:24:52 +08:00
|
|
|
https://axmolengine.github.io/
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <unordered_map>
|
2023-06-11 13:08:08 +08:00
|
|
|
#include "base/Types.h"
|
|
|
|
#include "renderer/Material.h"
|
|
|
|
#include "3d/Bundle3DData.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
#include "renderer/backend/Types.h"
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @addtogroup _3d
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
class Texture2D;
|
|
|
|
|
|
|
|
namespace backend
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
class ProgramState;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-07-05 14:48:46 +08:00
|
|
|
* @brief MeshMaterial: a mesh material for MeshRenderers.
|
2019-11-23 20:27:39 +08:00
|
|
|
*/
|
2022-07-16 10:43:05 +08:00
|
|
|
class AX_DLL MeshMaterial : public Material
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
2022-07-05 14:48:46 +08:00
|
|
|
* Material types, there are mainly two types of materials. Built-in materials and Custom materials.
|
2019-11-23 20:27:39 +08:00
|
|
|
*/
|
|
|
|
enum class MaterialType
|
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
// Built in materials
|
2021-12-25 10:04:45 +08:00
|
|
|
UNLIT, // unlit material
|
2023-08-25 16:57:54 +08:00
|
|
|
UNLIT_INSTANCE, // unlit instance material
|
2022-07-05 14:48:46 +08:00
|
|
|
UNLIT_NOTEX, // unlit material (without texture)
|
2021-12-25 10:04:45 +08:00
|
|
|
VERTEX_LIT, // vertex lit
|
|
|
|
DIFFUSE, // diffuse (pixel lighting)
|
|
|
|
DIFFUSE_NOTEX, // diffuse (without texture)
|
|
|
|
BUMPED_DIFFUSE, // bumped diffuse
|
2022-07-05 15:42:38 +08:00
|
|
|
QUAD_TEXTURE, // textured quad material
|
|
|
|
QUAD_COLOR, // colored quad material (without texture)
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
// Custom material
|
2022-07-05 14:48:46 +08:00
|
|
|
CUSTOM, // Create from a material file
|
2019-11-23 20:27:39 +08:00
|
|
|
};
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2023-08-25 16:57:54 +08:00
|
|
|
/**
|
|
|
|
* Instanced Material types, there are mainly two types of materials. Built-in materials and Custom materials.
|
|
|
|
*/
|
|
|
|
enum class InstanceMaterialType
|
|
|
|
{
|
|
|
|
NO_INSTANCING, // disabled instancing
|
|
|
|
UNLIT_INSTANCE, // unlit instance material
|
|
|
|
|
|
|
|
// Custom material
|
|
|
|
CUSTOM, // Create from a material file
|
|
|
|
};
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Get material type
|
|
|
|
* @return Material type
|
|
|
|
*/
|
|
|
|
MaterialType getMaterialType() const { return _type; }
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Create built in material from material type
|
|
|
|
* @param type Material type
|
2022-07-05 14:48:46 +08:00
|
|
|
* @param skinned Has hardware skinning?
|
|
|
|
* @return An autorelease material object
|
2019-11-23 20:27:39 +08:00
|
|
|
*/
|
2022-07-05 14:48:46 +08:00
|
|
|
static MeshMaterial* createBuiltInMaterial(MaterialType type, bool skinned);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Create material with file name, it creates material from cache if it is previously loaded
|
|
|
|
* @param path Path of material file
|
2022-07-05 14:48:46 +08:00
|
|
|
* @return An autorelease material object
|
2019-11-23 20:27:39 +08:00
|
|
|
*/
|
2022-07-05 14:48:46 +08:00
|
|
|
static MeshMaterial* createWithFilename(std::string_view path);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
2022-07-05 14:48:46 +08:00
|
|
|
* Create material with a ProgramState
|
2019-11-23 20:27:39 +08:00
|
|
|
* @param programState GLProgramState instance
|
2022-07-05 14:48:46 +08:00
|
|
|
* @return An autorelease material object
|
2019-11-23 20:27:39 +08:00
|
|
|
*/
|
2022-07-05 14:48:46 +08:00
|
|
|
static MeshMaterial* createWithProgramState(backend::ProgramState* programState);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
void setTexture(Texture2D* tex, NTextureData::Usage usage);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
2022-07-05 14:48:46 +08:00
|
|
|
* Create all built-in materials
|
2019-11-23 20:27:39 +08:00
|
|
|
*/
|
|
|
|
static void createBuiltInMaterial();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
2022-07-05 14:48:46 +08:00
|
|
|
* Release all built-in materials
|
2019-11-23 20:27:39 +08:00
|
|
|
*/
|
|
|
|
static void releaseBuiltInMaterial();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Release all cached materials
|
|
|
|
*/
|
|
|
|
static void releaseCachedMaterial();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
2022-07-05 14:48:46 +08:00
|
|
|
* Clone this material.
|
2019-11-23 20:27:39 +08:00
|
|
|
*/
|
|
|
|
virtual Material* clone() const override;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
protected:
|
|
|
|
MaterialType _type;
|
2022-07-05 14:48:46 +08:00
|
|
|
static std::unordered_map<std::string, MeshMaterial*> _materials; // cached material
|
|
|
|
static MeshMaterial* _unLitMaterial;
|
2023-08-25 16:57:54 +08:00
|
|
|
static MeshMaterial* _unLitInstanceMaterial;
|
2022-07-05 14:48:46 +08:00
|
|
|
static MeshMaterial* _unLitNoTexMaterial;
|
|
|
|
static MeshMaterial* _vertexLitMaterial;
|
|
|
|
static MeshMaterial* _diffuseMaterial;
|
|
|
|
static MeshMaterial* _diffuseNoTexMaterial;
|
|
|
|
static MeshMaterial* _bumpedDiffuseMaterial;
|
|
|
|
|
|
|
|
static MeshMaterial* _unLitMaterialSkin;
|
|
|
|
static MeshMaterial* _vertexLitMaterialSkin;
|
|
|
|
static MeshMaterial* _diffuseMaterialSkin;
|
|
|
|
static MeshMaterial* _bumpedDiffuseMaterialSkin;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-05 15:42:38 +08:00
|
|
|
static MeshMaterial* _quadTextureMaterial;
|
|
|
|
static MeshMaterial* _quadColorMaterial;
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
static backend::ProgramState* _unLitMaterialProgState;
|
2023-08-25 16:57:54 +08:00
|
|
|
static backend::ProgramState* _unLitInstanceMaterialProgState;
|
2019-11-23 20:27:39 +08:00
|
|
|
static backend::ProgramState* _unLitNoTexMaterialProgState;
|
|
|
|
static backend::ProgramState* _vertexLitMaterialProgState;
|
|
|
|
static backend::ProgramState* _diffuseMaterialProgState;
|
|
|
|
static backend::ProgramState* _diffuseNoTexMaterialProgState;
|
|
|
|
static backend::ProgramState* _bumpedDiffuseMaterialProgState;
|
|
|
|
|
|
|
|
static backend::ProgramState* _unLitMaterialSkinProgState;
|
|
|
|
static backend::ProgramState* _vertexLitMaterialSkinProgState;
|
|
|
|
static backend::ProgramState* _diffuseMaterialSkinProgState;
|
|
|
|
static backend::ProgramState* _bumpedDiffuseMaterialSkinProgState;
|
2022-07-05 15:42:38 +08:00
|
|
|
|
|
|
|
static backend::ProgramState* _quadTextureMaterialProgState;
|
|
|
|
static backend::ProgramState* _quadColorMaterialProgState;
|
2019-11-23 20:27:39 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2022-07-05 14:48:46 +08:00
|
|
|
* @brief MeshMaterialCache: the MeshRenderer material cache, it can only cache textures for now.
|
2019-11-23 20:27:39 +08:00
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2022-07-05 14:48:46 +08:00
|
|
|
class MeshMaterialCache
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
public:
|
2022-07-05 14:48:46 +08:00
|
|
|
static MeshMaterialCache* getInstance();
|
2019-11-23 20:27:39 +08:00
|
|
|
static void destroyInstance();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
/** add a material to cache */
|
|
|
|
bool addMeshMaterial(std::string_view key, Texture2D* tex);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
/** get material from cache */
|
|
|
|
Texture2D* getMeshMaterial(std::string_view key);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
/** remove all cached materials */
|
|
|
|
void removeAllMeshMaterial();
|
|
|
|
/** remove unused cached materials */
|
|
|
|
void removeUnusedMeshMaterial();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
MeshMaterialCache();
|
|
|
|
~MeshMaterialCache();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
protected:
|
2022-07-05 14:48:46 +08:00
|
|
|
static MeshMaterialCache* _cacheInstance; // cache instance
|
|
|
|
hlookup::string_map<Texture2D*> _materials; // cached materials
|
2019-11-23 20:27:39 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// end of 3d group
|
|
|
|
/// @}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_END
|