2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2014-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef __CCSPRITE3D_H__
|
|
|
|
#define __CCSPRITE3D_H__
|
|
|
|
|
|
|
|
#include <unordered_map>
|
|
|
|
|
|
|
|
#include "base/CCVector.h"
|
|
|
|
#include "base/ccTypes.h"
|
|
|
|
#include "base/CCProtocols.h"
|
|
|
|
#include "2d/CCNode.h"
|
|
|
|
#include "renderer/CCMeshCommand.h"
|
2021-12-25 10:04:45 +08:00
|
|
|
#include "3d/CCSkeleton3D.h" // need to include for lua-binding
|
2019-11-23 20:27:39 +08:00
|
|
|
#include "3d/CCAABB.h"
|
|
|
|
#include "3d/CCBundle3DData.h"
|
|
|
|
#include "3d/CCMeshVertexIndexData.h"
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @addtogroup _3d
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
class Mesh;
|
|
|
|
class Texture2D;
|
|
|
|
class MeshSkin;
|
|
|
|
class AttachNode;
|
|
|
|
struct NodeData;
|
|
|
|
/** @brief Sprite3D: A sprite can be loaded from 3D model files, .obj, .c3t, .c3b, then can be drawn as sprite */
|
|
|
|
class CC_DLL Sprite3D : public Node, public BlendProtocol
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Creates an empty sprite3D without 3D model and texture.
|
|
|
|
*
|
|
|
|
* @return An autoreleased sprite3D object.
|
|
|
|
*/
|
|
|
|
static Sprite3D* create();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/** creates a Sprite3D*/
|
2021-12-26 23:26:34 +08:00
|
|
|
static Sprite3D* create(std::string_view modelPath);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// creates a Sprite3D. It only supports one texture, and overrides the internal texture with 'texturePath'
|
2021-12-26 23:26:34 +08:00
|
|
|
static Sprite3D* create(std::string_view modelPath, std::string_view texturePath);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/** create 3d sprite asynchronously
|
|
|
|
* If the 3d model was previously loaded, it will create a new 3d sprite and the callback will be called at once.
|
2021-12-25 10:04:45 +08:00
|
|
|
* Otherwise it will load the model file in a new thread, and when the 3d sprite is loaded, the callback will be
|
|
|
|
* called with the created Sprite3D and a user-defined parameter. The callback will be called from the main thread,
|
|
|
|
* so it is safe to create any cocos2d object from the callback.
|
2019-11-23 20:27:39 +08:00
|
|
|
* @param modelPath model to be loaded
|
|
|
|
* @param callback callback after loading
|
|
|
|
* @param callbackparam user defined parameter for the callback
|
|
|
|
*/
|
2021-12-26 23:26:34 +08:00
|
|
|
static void createAsync(std::string_view modelPath,
|
2021-12-25 10:04:45 +08:00
|
|
|
const std::function<void(Sprite3D*, void*)>& callback,
|
|
|
|
void* callbackparam);
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
static void createAsync(std::string_view modelPath,
|
|
|
|
std::string_view texturePath,
|
2021-12-25 10:04:45 +08:00
|
|
|
const std::function<void(Sprite3D*, void*)>& callback,
|
|
|
|
void* callbackparam);
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**set diffuse texture, set the first if multiple textures exist*/
|
2021-12-26 23:26:34 +08:00
|
|
|
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
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**get Mesh by index*/
|
|
|
|
Mesh* getMeshByIndex(int index) const;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**get Mesh by Name, it returns the first one if there are more than one mesh with the same name */
|
2021-12-26 23:26:34 +08:00
|
|
|
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
|
|
|
|
*/
|
2021-12-26 23:26:34 +08:00
|
|
|
std::vector<Mesh*> getMeshArrayByName(std::string_view name) const;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/**get mesh*/
|
|
|
|
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
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**get AttachNode by bone name, return nullptr if not exist*/
|
2021-12-26 23:26:34 +08:00
|
|
|
AttachNode* getAttachNode(std::string_view boneName);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**remove attach node*/
|
2021-12-26 23:26:34 +08:00
|
|
|
void removeAttachNode(std::string_view boneName);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**remove all attach nodes*/
|
|
|
|
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
|
2020-09-09 13:03:31 +08:00
|
|
|
/** Sets ProgramState, you should bind attributes by yourself */
|
2021-12-25 10:04:45 +08:00
|
|
|
bool setProgramState(backend::ProgramState* programState, bool needsRetain = true) override;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Get AABB
|
|
|
|
* If the sprite has animation, it can't be calculated accurately,
|
|
|
|
* because bone can drive the vertices, we just use the origin vertices
|
|
|
|
* 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 some times we may have an empty Sprite3D Node as parent, but
|
|
|
|
* the Sprite3D don't contain any meshes, so getAABB()
|
|
|
|
* will return a wrong value at that time.
|
|
|
|
*/
|
|
|
|
AABB getAABBRecursively();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
2021-12-25 10:04:45 +08:00
|
|
|
* Executes an action, and returns the action that is executed. For Sprite3D special logic are 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 An Action pointer
|
|
|
|
*/
|
|
|
|
virtual Action* runAction(Action* action) override;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Force to write to depth buffer, this is useful if you want to achieve effects like fading.
|
|
|
|
*/
|
|
|
|
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 2d bounding-box
|
|
|
|
* Note: the bounding-box is just get from the AABB which as Z=0, so that is not very accurate.
|
|
|
|
*/
|
|
|
|
virtual Rect getBoundingBox() const override;
|
|
|
|
|
|
|
|
// set which face is going to cull, CullFaceSide::BACK, CullFaceSide::FRONT and CullFaceSide::NONE.
|
2020-10-07 00:07:53 +08:00
|
|
|
virtual void setCullFace(CullFaceSide side);
|
2019-11-23 20:27:39 +08:00
|
|
|
// set cull face enable or not
|
|
|
|
void setCullFaceEnabled(bool enable);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
/** light mask getter & setter, light works only when _lightmask & light's flag is true, 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
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**draw*/
|
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 the sprite.
|
|
|
|
The Material will be applied to all the meshes that belong to the sprite.
|
|
|
|
Internally it will call `setMaterial(material,-1)`
|
|
|
|
*/
|
|
|
|
void setMaterial(Material* material);
|
|
|
|
|
|
|
|
/** Adds a new material to a particular mesh of the sprite.
|
|
|
|
meshIndex is the mesh that will be applied to.
|
|
|
|
if meshIndex == -1, then it will be applied to all the meshes that belong to the sprite.
|
|
|
|
*/
|
|
|
|
void setMaterial(Material* material, int meshIndex);
|
|
|
|
|
|
|
|
/** Adds a new material to a particular mesh of the sprite.
|
|
|
|
meshIndex is the mesh that will be applied to.
|
|
|
|
if meshIndex == -1, then it will be applied to all the meshes that belong to the sprite.
|
|
|
|
*/
|
|
|
|
Material* getMaterial(int meshIndex) const;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
2021-12-25 10:04:45 +08:00
|
|
|
* force set this Sprite3D to 2D render queue
|
|
|
|
*/
|
2019-11-23 20:27:39 +08:00
|
|
|
void setForce2DQueue(bool force2D);
|
|
|
|
|
|
|
|
/**
|
2021-12-25 10:04:45 +08:00
|
|
|
* Get meshes used in sprite 3d
|
|
|
|
*/
|
2019-11-23 20:27:39 +08:00
|
|
|
const Vector<Mesh*>& getMeshes() const { return _meshes; }
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
CC_CONSTRUCTOR_ACCESS :
|
|
|
|
|
|
|
|
Sprite3D();
|
2019-11-23 20:27:39 +08:00
|
|
|
virtual ~Sprite3D();
|
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
|
|
|
|
2021-12-26 23:26:34 +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
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**load sprite3d from cache, return true if succeed, false otherwise*/
|
2021-12-26 23:26:34 +08:00
|
|
|
bool loadFromCache(std::string_view path);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
/** load file and set it to meshedatas, nodedatas and materialdatas, obj file .mtl file should be at the same
|
|
|
|
* directory if exist */
|
2021-12-26 23:26:34 +08:00
|
|
|
bool loadFromFile(std::string_view path,
|
2021-12-25 10:04:45 +08:00
|
|
|
NodeDatas* nodedatas,
|
|
|
|
MeshDatas* meshdatas,
|
|
|
|
MaterialDatas* materialdatas);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Visits this Sprite3D's children and draw them recursively.
|
|
|
|
* Note: all its children will rendered as 3D objects
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
virtual void visit(Renderer* renderer, const Mat4& parentTransform, uint32_t parentFlags) override;
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**generate default material*/
|
|
|
|
void genMaterial(bool useLight = false);
|
|
|
|
|
|
|
|
void createNode(NodeData* nodedata, Node* root, const MaterialDatas& materialdatas, bool singleSprite);
|
|
|
|
void createAttachSprite3DNode(NodeData* nodedata, const MaterialDatas& materialdatas);
|
|
|
|
Sprite3D* createSprite3DNode(NodeData* nodedata, ModelData* modeldata, const MaterialDatas& materialdatas);
|
|
|
|
|
|
|
|
/**get MeshIndexData by Id*/
|
2021-12-26 23:26:34 +08:00
|
|
|
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:
|
2021-12-25 10:04:45 +08:00
|
|
|
Skeleton3D* _skeleton; // skeleton
|
|
|
|
|
|
|
|
Vector<MeshVertexData*> _meshVertexDatas;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-26 23:26:34 +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 the matrix
|
|
|
|
mutable bool _aabbDirty;
|
|
|
|
unsigned int _lightMask;
|
|
|
|
bool _shaderUsingLight; // is current shader using light ?
|
|
|
|
bool _forceDepthWrite; // Always write to depth buffer
|
|
|
|
bool _usingAutogeneratedGLProgram;
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
struct AsyncLoadParam
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
std::function<void(Sprite3D*, void*)> afterLoadCallback; // callback after load
|
|
|
|
void* callbackParam;
|
|
|
|
bool result; // sprite load result
|
|
|
|
std::string modelPath;
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////
|
|
|
|
/**
|
|
|
|
* Sprite3DCache
|
|
|
|
* @brief the cache data of Sprite3D, use to speed up Sprite3D::create
|
|
|
|
*/
|
|
|
|
class CC_DLL Sprite3DCache
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
struct Sprite3DData
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Vector<MeshVertexData*> meshVertexDatas;
|
|
|
|
Vector<backend::ProgramState*> programStates;
|
|
|
|
NodeDatas* nodedatas;
|
|
|
|
MaterialDatas* materialdatas;
|
2019-11-23 20:27:39 +08:00
|
|
|
~Sprite3DData()
|
|
|
|
{
|
|
|
|
if (nodedatas)
|
|
|
|
delete nodedatas;
|
|
|
|
if (materialdatas)
|
|
|
|
delete materialdatas;
|
|
|
|
meshVertexDatas.clear();
|
|
|
|
programStates.clear();
|
|
|
|
}
|
|
|
|
};
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**get & destroy*/
|
|
|
|
static Sprite3DCache* getInstance();
|
|
|
|
static void destroyInstance();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get the SpriteData struct
|
|
|
|
*
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2021-12-26 23:26:34 +08:00
|
|
|
Sprite3DData* getSpriteData(std::string_view key) const;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* add the SpriteData into Sprite3D by given the specified key
|
|
|
|
*
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2021-12-26 23:26:34 +08:00
|
|
|
bool addSprite3DData(std::string_view key, Sprite3DData* spritedata);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**remove the SpriteData from Sprite3D by given the specified key*/
|
2021-12-26 23:26:34 +08:00
|
|
|
void removeSprite3DData(std::string_view key);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**remove all the SpriteData from Sprite3D*/
|
|
|
|
void removeAllSprite3DData();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
CC_CONSTRUCTOR_ACCESS : Sprite3DCache();
|
2019-11-23 20:27:39 +08:00
|
|
|
~Sprite3DCache();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
protected:
|
2021-12-25 10:04:45 +08:00
|
|
|
static Sprite3DCache* _cacheInstance;
|
2021-12-26 23:26:34 +08:00
|
|
|
hlookup::string_map<Sprite3DData*> _spriteDatas; // cached sprite data
|
2019-11-23 20:27:39 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// end of 3d group
|
|
|
|
/// @}
|
|
|
|
|
|
|
|
NS_CC_END
|
2021-12-25 10:04:45 +08:00
|
|
|
#endif // __SPRITE3D_H_
|