axmol/core/3d/MeshRenderer.h

360 lines
13 KiB
C
Raw Normal View History

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-12 12:39:27 +08:00
Copyright (c) 2022 Bytedance Inc.
2019-11-23 20:27:39 +08:00
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.
****************************************************************************/
2022-07-16 10:43:05 +08:00
#ifndef __AX_MESH_RENDERER_H__
#define __AX_MESH_RENDERER_H__
2019-11-23 20:27:39 +08:00
#include <unordered_map>
#include "base/Vector.h"
#include "base/Types.h"
#include "base/Protocols.h"
#include "2d/Node.h"
#include "renderer/MeshCommand.h"
#include "3d/Skeleton3D.h" // needs to be included for lua-bindings
#include "3d/AABB.h"
#include "3d/Bundle3DData.h"
#include "3d/MeshVertexIndexData.h"
2019-11-23 20:27:39 +08:00
NS_AX_BEGIN
2019-11-23 20:27:39 +08:00
/**
* @addtogroup _3d
* @{
*/
class Mesh;
class Texture2D;
class MeshSkin;
class AttachNode;
struct NodeData;
/** @brief MeshRenderer: A mesh can be loaded from model files, .obj, .c3t, .c3b
*and a mesh renderer renders a list of these loaded meshes with specified materials
*/
2022-07-16 10:43:05 +08:00
class AX_DLL MeshRenderer : public Node, public BlendProtocol
2019-11-23 20:27:39 +08:00
{
public:
/**
* Creates an empty MeshRenderer without a mesh or a texture.
* Can be used to create procedural meshes on runtime.
2019-11-23 20:27:39 +08:00
*
* @return An autoreleased MeshRenderer object.
2019-11-23 20:27:39 +08:00
*/
static MeshRenderer* create();
2021-12-25 10:04:45 +08:00
/** creates a MeshRenderer with a specified path */
static MeshRenderer* create(std::string_view modelPath);
2021-12-25 10:04:45 +08:00
/** creates a MeshRenderer. A mesh can only have one texture, the default texture can be overridden with 'texturePath' */
static MeshRenderer* create(std::string_view modelPath, std::string_view texturePath);
2021-12-25 10:04:45 +08:00
/** create 3d mesh asynchronously
* If the 3d model was previously loaded, it will create a new 3d mesh and the callback will be called once.
* Otherwise it will load the model file in a new thread, and when the 3d mesh is loaded, the callback will be
* called with the created MeshRenderer and a user-defined parameter. The callback will be called from the main thread,
* so it is safe to create any object from the callback.
2019-11-23 20:27:39 +08:00
* @param modelPath model to be loaded
* @param callback callback when loading is finished
* @param callbackparam user-defined parameter for the callback
2019-11-23 20:27:39 +08:00
*/
static void createAsync(std::string_view modelPath,
const std::function<void(MeshRenderer*, void*)>& callback,
2021-12-25 10:04:45 +08:00
void* callbackparam);
static void createAsync(std::string_view modelPath,
std::string_view texturePath,
const std::function<void(MeshRenderer*, void*)>& callback,
2021-12-25 10:04:45 +08:00
void* callbackparam);
/** set diffuse texture, set the first mesh's texture if multiple textures exist */
void setTexture(std::string_view texFile);
2019-11-23 20:27:39 +08:00
void setTexture(Texture2D* texture);
2021-12-25 10:04:45 +08:00
/** get Mesh by index */
2019-11-23 20:27:39 +08:00
Mesh* getMeshByIndex(int index) const;
2021-12-25 10:04:45 +08:00
/** get Mesh by Name, returns the first one if there are more than one mesh with the same name */
Mesh* getMeshByName(std::string_view name) const;
2021-12-25 10:04:45 +08:00
/**
2019-11-23 20:27:39 +08:00
* get mesh array by name, returns all meshes with the given name
*
* @lua NA
*/
std::vector<Mesh*> getMeshArrayByName(std::string_view name) const;
2019-11-23 20:27:39 +08:00
/** get mesh at index 0 which is the default mesh */
2019-11-23 20:27:39 +08:00
Mesh* getMesh() const;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/** get mesh count */
ssize_t getMeshCount() const { return _meshes.size(); }
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
Skeleton3D* getSkeleton() const { return _skeleton; }
2021-12-25 10:04:45 +08:00
/** return an AttachNode by bone name. Otherwise, return nullptr if it doesn't exist */
AttachNode* getAttachNode(std::string_view boneName);
2021-12-25 10:04:45 +08:00
/** remove an attached node */
void removeAttachNode(std::string_view boneName);
2021-12-25 10:04:45 +08:00
/** remove all attached nodes */
2019-11-23 20:27:39 +08:00
void removeAllAttachNode();
// overrides
2021-12-25 10:04:45 +08:00
virtual void setBlendFunc(const BlendFunc& blendFunc) override;
virtual const BlendFunc& getBlendFunc() const override;
2019-11-23 20:27:39 +08:00
// overrides
/** Sets ProgramState, attributes should be bound by the user */
2023-08-31 18:59:12 +08:00
bool setProgramState(backend::ProgramState* programState, bool needsRetain = true) override;
2019-11-23 20:27:39 +08:00
/*
* Get AABB
* If the mesh has animations, it can't be calculated accurately,
* because a bone can transform the vertices, and the untransformed vertices are used
2019-11-23 20:27:39 +08:00
* to calculate the AABB.
*/
const AABB& getAABB() const;
2021-12-25 10:04:45 +08:00
/*
2019-11-23 20:27:39 +08:00
* Get AABB Recursively
* Because sometimes we may have an empty MeshRenderer Node as parent, If
* the MeshRenderer doesn't contain any meshes, then we use getAABB()
2019-11-23 20:27:39 +08:00
*/
AABB getAABBRecursively();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Executes an action, and returns the action that is executed. For the MeshRenderer special logic is needed to take
* care of Fading.
2019-11-23 20:27:39 +08:00
*
* This node becomes the action's target. Refer to Action::getTarget()
* @warning Actions don't retain their target.
*
* @return a pointer to Action
2019-11-23 20:27:39 +08:00
*/
virtual Action* runAction(Action* action) override;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Force depth buffer writing, this is useful if you want to achieve effects like fading.
2019-11-23 20:27:39 +08:00
*/
void setForceDepthWrite(bool value) { _forceDepthWrite = value; }
2021-12-25 10:04:45 +08:00
bool isForceDepthWrite() const { return _forceDepthWrite; };
2019-11-23 20:27:39 +08:00
/**
* Returns a 2d bounding-box
* Note: the bounding-box is taken from the mesh's AABB with Z-axis ignored.
2019-11-23 20:27:39 +08:00
*/
virtual Rect getBoundingBox() const override;
// set face culling side, CullFaceSide::BACK, CullFaceSide::FRONT and CullFaceSide::NONE.
2020-10-07 00:07:53 +08:00
virtual void setCullFace(CullFaceSide side);
// set face culling enabled.
2019-11-23 20:27:39 +08:00
void setCullFaceEnabled(bool enable);
2021-12-25 10:04:45 +08:00
/** light mask getter & setter, lighting only works when _lightmask & light's flag are set to true, the default
value of _lightmask is 0xffff */
2019-11-23 20:27:39 +08:00
void setLightMask(unsigned int mask) { _lightMask = mask; }
unsigned int getLightMask() const { return _lightMask; }
2021-12-25 10:04:45 +08:00
/** enables wireframe rendering mode for this mesh renderer only, this can be very useful for debugging and
understanding generated meshes. */
void setWireframe(bool value) { _wireframe = value; }
bool isWireframe() const { return _wireframe; }
/** render all meshes within this mesh renderer */
2021-12-25 10:04:45 +08:00
virtual void draw(Renderer* renderer, const Mat4& transform, uint32_t flags) override;
2019-11-23 20:27:39 +08:00
/** Adds a new material to this mesh renderer.
The Material will be applied to all the meshes that belong to the mesh renderer.
It will internally call `setMaterial(material,-1)`
2019-11-23 20:27:39 +08:00
*/
void setMaterial(Material* material);
/** Adds a new material to a particular mesh in this mesh renderer.
* if meshIndex == -1, then it will be applied to all the meshes that belong to this mesh renderer.
*
* @param meshIndex Index of the mesh to apply the material to.
2019-11-23 20:27:39 +08:00
*/
void setMaterial(Material* material, int meshIndex);
/** Gets the material of a specific mesh in this mesh renderer.
*
* @param meshIndex Index of the mesh to get the material from. 0 is the default index.
2019-11-23 20:27:39 +08:00
*/
Material* getMaterial(int meshIndex = 0) const;
2019-11-23 20:27:39 +08:00
/** Get list of meshes used in this mesh renderer. */
2019-11-23 20:27:39 +08:00
const Vector<Mesh*>& getMeshes() const { return _meshes; }
MeshRenderer();
virtual ~MeshRenderer();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
virtual bool init() override;
2021-12-25 10:04:45 +08:00
bool initWithFile(std::string_view path);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
bool initFrom(const NodeDatas& nodedatas, const MeshDatas& meshdatas, const MaterialDatas& materialdatas);
2021-12-25 10:04:45 +08:00
/** load a mesh renderer from cache, returns true if succeeded, false otherwise. */
bool loadFromCache(std::string_view path);
2021-12-25 10:04:45 +08:00
/** load a file and feed it's content into meshedatas, nodedatas and materialdatas, obj file and .mtl file
should be in the same directory. */
bool loadFromFile(std::string_view path, NodeDatas* nodedatas, MeshDatas* meshdatas, MaterialDatas* materialdatas);
2019-11-23 20:27:39 +08:00
/**
* Visits this MeshRenderer's children and draws them recursively.
* Note: all children will be rendered in 3D space with depth, this behaviour can be changed using
* setForce2DQueue()
2019-11-23 20:27:39 +08:00
*/
2021-12-25 10:04:45 +08:00
virtual void visit(Renderer* renderer, const Mat4& parentTransform, uint32_t parentFlags) override;
/** generate default material. */
2019-11-23 20:27:39 +08:00
void genMaterial(bool useLight = false);
void createNode(NodeData* nodedata, Node* root, const MaterialDatas& materialdatas, bool singleMesh);
void createAttachMeshRendererNode(NodeData* nodedata, const MaterialDatas& materialdatas);
MeshRenderer* createMeshRendererNode(NodeData* nodedata, ModelData* modeldata, const MaterialDatas& materialdatas);
2019-11-23 20:27:39 +08:00
/** get MeshIndexData by Id */
MeshIndexData* getMeshIndexData(std::string_view indexId) const;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
void addMesh(Mesh* mesh);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
void onAABBDirty() { _aabbDirty = true; }
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
void afterAsyncLoad(void* param);
2021-12-25 10:04:45 +08:00
static AABB getAABBRecursivelyImp(Node* node);
2019-11-23 20:27:39 +08:00
protected:
2022-10-12 12:39:27 +08:00
/** set specific mesh texture, for private use (create mesh stage) only */
Texture2D* setMeshTexture(Mesh* mesh,
std::string_view texPath,
NTextureData::Usage usage = NTextureData::Usage::Diffuse);
/** set model texture from model path when model file not contains texture and texPath is empty
* only for create mesh renderer
*/
void setModelTexture(std::string_view modelPath, std::string_view texPath);
Skeleton3D* _skeleton;
2021-12-25 10:04:45 +08:00
Vector<MeshVertexData*> _meshVertexDatas;
2019-11-23 20:27:39 +08:00
hlookup::string_map<AttachNode*> _attachments;
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
BlendFunc _blend;
Vector<Mesh*> _meshes;
mutable AABB _aabb; // cache current aabb
mutable Mat4 _nodeToWorldTransform; // cache current matrix
2021-12-25 10:04:45 +08:00
unsigned int _lightMask;
2022-10-12 12:39:27 +08:00
mutable bool _aabbDirty;
bool _shaderUsingLight; // Is the current shader using lighting?
2021-12-25 10:04:45 +08:00
bool _forceDepthWrite; // Always write to depth buffer
bool _wireframe; // render in wireframe mode
2021-12-25 10:04:45 +08:00
bool _usingAutogeneratedGLProgram;
bool _transparentMaterialHint; // Generate transparent materials when building from files
2022-10-12 12:39:27 +08:00
unsigned short _meshTextureHint; // Whether model file has texture config
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
struct AsyncLoadParam
{
std::function<void(MeshRenderer*, void*)> afterLoadCallback; // callback after loading is finished
2021-12-25 10:04:45 +08:00
void* callbackParam;
bool result; // mesh renderer loading result
2021-12-25 10:04:45 +08:00
std::string modelPath;
std::string modelFullPath;
std::string texPath;
2019-11-23 20:27:39 +08:00
MeshDatas* meshdatas;
MaterialDatas* materialdatas;
2021-12-25 10:04:45 +08:00
NodeDatas* nodeDatas;
2019-11-23 20:27:39 +08:00
};
2021-12-25 10:04:45 +08:00
AsyncLoadParam _asyncLoadParam;
2019-11-23 20:27:39 +08:00
};
///////////////////////////////////////////////////////
/**
* @brief MeshRendererCache: the cache data of MeshRenderer, used to speed up the creation process of MeshRenderer
2019-11-23 20:27:39 +08:00
*/
2022-07-16 10:43:05 +08:00
class AX_DLL MeshRendererCache
2019-11-23 20:27:39 +08:00
{
public:
2022-07-12 21:31:54 +08:00
struct MeshRenderData
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
Vector<MeshVertexData*> meshVertexDatas;
Vector<backend::ProgramState*> programStates;
NodeDatas* nodedatas;
MaterialDatas* materialdatas;
2022-07-12 21:31:54 +08:00
~MeshRenderData()
2019-11-23 20:27:39 +08:00
{
if (nodedatas)
delete nodedatas;
if (materialdatas)
delete materialdatas;
meshVertexDatas.clear();
programStates.clear();
}
};
2021-12-25 10:04:45 +08:00
static MeshRendererCache* getInstance();
2019-11-23 20:27:39 +08:00
static void destroyInstance();
/**
* get a MeshData object by key
2019-11-23 20:27:39 +08:00
*
* @lua NA
*/
2022-07-12 21:31:54 +08:00
MeshRenderData* getMeshRenderData(std::string_view key) const;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* add a MeshData object into the MeshRenderer with a specified key
2019-11-23 20:27:39 +08:00
*
* @lua NA
*/
2022-07-12 21:31:54 +08:00
bool addMeshRenderData(std::string_view key, MeshRenderData* meshdata);
2021-12-25 10:04:45 +08:00
/** remove a MeshData from the MeshRenderer with a specified key */
2022-07-12 21:31:54 +08:00
void removeMeshRenderData(std::string_view key);
2021-12-25 10:04:45 +08:00
/** remove all the MeshData objects from the MeshRenderer */
2022-07-12 21:31:54 +08:00
void removeAllMeshRenderData();
2021-12-25 10:04:45 +08:00
MeshRendererCache();
~MeshRendererCache();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
protected:
static MeshRendererCache* _cacheInstance;
2022-07-12 21:31:54 +08:00
hlookup::string_map<MeshRenderData*> _meshDatas; // cached mesh data
2019-11-23 20:27:39 +08:00
};
// end of 3d group
/// @}
NS_AX_END
2022-07-16 10:43:05 +08:00
#endif // __AX_MESH_RENDERER_H__