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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "3d/CCSprite3D.h"
|
|
|
|
#include "3d/CCObjLoader.h"
|
|
|
|
#include "3d/CCMeshSkin.h"
|
|
|
|
#include "3d/CCBundle3D.h"
|
|
|
|
#include "3d/CCSprite3DMaterial.h"
|
|
|
|
#include "3d/CCAttachNode.h"
|
|
|
|
#include "3d/CCMesh.h"
|
|
|
|
|
|
|
|
#include "base/CCDirector.h"
|
|
|
|
#include "base/CCAsyncTaskPool.h"
|
|
|
|
#include "base/ccUTF8.h"
|
|
|
|
#include "base/ccUtils.h"
|
|
|
|
#include "2d/CCLight.h"
|
|
|
|
#include "2d/CCCamera.h"
|
|
|
|
#include "base/ccMacros.h"
|
|
|
|
#include "platform/CCPlatformMacros.h"
|
|
|
|
#include "platform/CCFileUtils.h"
|
|
|
|
#include "renderer/CCTextureCache.h"
|
|
|
|
#include "renderer/CCRenderer.h"
|
|
|
|
#include "renderer/CCMaterial.h"
|
|
|
|
#include "renderer/CCTechnique.h"
|
|
|
|
#include "renderer/CCPass.h"
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
static Sprite3DMaterial* getSprite3DMaterialForAttribs(MeshVertexData* meshVertexData, bool usesLight);
|
|
|
|
|
|
|
|
Sprite3D* Sprite3D::create()
|
|
|
|
{
|
|
|
|
//
|
2021-12-08 00:11:53 +08:00
|
|
|
auto sprite = new Sprite3D();
|
|
|
|
if (sprite->init())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
sprite->autorelease();
|
|
|
|
return sprite;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(sprite);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
Sprite3D* Sprite3D::create(std::string_view modelPath)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
CCASSERT(modelPath.length() >= 4, "invalid filename for Sprite3D");
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2021-12-08 00:11:53 +08:00
|
|
|
auto sprite = new Sprite3D();
|
|
|
|
if (sprite->initWithFile(modelPath))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
sprite->_contentSize = sprite->getBoundingBox().size;
|
|
|
|
sprite->autorelease();
|
|
|
|
return sprite;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(sprite);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2021-12-26 23:26:34 +08:00
|
|
|
Sprite3D* Sprite3D::create(std::string_view modelPath, std::string_view texturePath)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto sprite = create(modelPath);
|
|
|
|
if (sprite)
|
|
|
|
{
|
|
|
|
sprite->setTexture(texturePath);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return sprite;
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
void Sprite3D::createAsync(std::string_view modelPath,
|
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
|
|
|
{
|
|
|
|
createAsync(modelPath, "", callback, callbackparam);
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
void Sprite3D::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
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Sprite3D* sprite = new Sprite3D();
|
2019-11-23 20:27:39 +08:00
|
|
|
if (sprite->loadFromCache(modelPath))
|
|
|
|
{
|
|
|
|
sprite->autorelease();
|
|
|
|
if (!texturePath.empty())
|
|
|
|
sprite->setTexture(texturePath);
|
|
|
|
callback(sprite, callbackparam);
|
|
|
|
return;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
sprite->_asyncLoadParam.afterLoadCallback = callback;
|
2021-12-25 10:04:45 +08:00
|
|
|
sprite->_asyncLoadParam.texPath = texturePath;
|
|
|
|
sprite->_asyncLoadParam.modelPath = modelPath;
|
|
|
|
sprite->_asyncLoadParam.callbackParam = callbackparam;
|
|
|
|
sprite->_asyncLoadParam.materialdatas = new MaterialDatas();
|
|
|
|
sprite->_asyncLoadParam.meshdatas = new MeshDatas();
|
|
|
|
sprite->_asyncLoadParam.nodeDatas = new NodeDatas();
|
|
|
|
AsyncTaskPool::getInstance()->enqueue(
|
|
|
|
AsyncTaskPool::TaskType::TASK_IO, CC_CALLBACK_1(Sprite3D::afterAsyncLoad, sprite),
|
|
|
|
(void*)(&sprite->_asyncLoadParam), [sprite]() {
|
|
|
|
sprite->_asyncLoadParam.result =
|
|
|
|
sprite->loadFromFile(sprite->_asyncLoadParam.modelPath, sprite->_asyncLoadParam.nodeDatas,
|
|
|
|
sprite->_asyncLoadParam.meshdatas, sprite->_asyncLoadParam.materialdatas);
|
|
|
|
});
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Sprite3D::afterAsyncLoad(void* param)
|
|
|
|
{
|
|
|
|
Sprite3D::AsyncLoadParam* asyncParam = (Sprite3D::AsyncLoadParam*)param;
|
|
|
|
autorelease();
|
|
|
|
if (asyncParam)
|
|
|
|
{
|
|
|
|
if (asyncParam->result)
|
|
|
|
{
|
|
|
|
_meshes.clear();
|
|
|
|
_meshVertexDatas.clear();
|
|
|
|
CC_SAFE_RELEASE_NULL(_skeleton);
|
|
|
|
removeAllAttachNode();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
// create in the main thread
|
|
|
|
auto& meshdatas = asyncParam->meshdatas;
|
2019-11-23 20:27:39 +08:00
|
|
|
auto& materialdatas = asyncParam->materialdatas;
|
2021-12-25 10:04:45 +08:00
|
|
|
auto& nodeDatas = asyncParam->nodeDatas;
|
2019-11-23 20:27:39 +08:00
|
|
|
if (initFrom(*nodeDatas, *meshdatas, *materialdatas))
|
|
|
|
{
|
|
|
|
auto spritedata = Sprite3DCache::getInstance()->getSpriteData(asyncParam->modelPath);
|
|
|
|
if (spritedata == nullptr)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// add to cache
|
|
|
|
auto data = new Sprite3DCache::Sprite3DData();
|
|
|
|
data->materialdatas = materialdatas;
|
|
|
|
data->nodedatas = nodeDatas;
|
2019-11-23 20:27:39 +08:00
|
|
|
data->meshVertexDatas = _meshVertexDatas;
|
2021-12-25 10:04:45 +08:00
|
|
|
for (const auto mesh : _meshes)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
data->programStates.pushBack(mesh->getProgramState());
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
Sprite3DCache::getInstance()->addSprite3DData(asyncParam->modelPath, data);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
CC_SAFE_DELETE(meshdatas);
|
|
|
|
materialdatas = nullptr;
|
2021-12-25 10:04:45 +08:00
|
|
|
nodeDatas = nullptr;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(meshdatas);
|
|
|
|
CC_SAFE_DELETE(materialdatas);
|
|
|
|
CC_SAFE_DELETE(nodeDatas);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (asyncParam->texPath != "")
|
|
|
|
{
|
|
|
|
setTexture(asyncParam->texPath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CCLOG("file load failed: %s ", asyncParam->modelPath.c_str());
|
|
|
|
}
|
|
|
|
asyncParam->afterLoadCallback(this, asyncParam->callbackParam);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
AABB Sprite3D::getAABBRecursivelyImp(Node* node)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
AABB aabb;
|
2021-12-25 10:04:45 +08:00
|
|
|
for (auto iter : node->getChildren())
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
aabb.merge(getAABBRecursivelyImp(iter));
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
Sprite3D* sprite3d = dynamic_cast<Sprite3D*>(node);
|
2019-11-23 20:27:39 +08:00
|
|
|
if (sprite3d)
|
|
|
|
aabb.merge(sprite3d->getAABB());
|
|
|
|
|
|
|
|
return aabb;
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
bool Sprite3D::loadFromCache(std::string_view path)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto spritedata = Sprite3DCache::getInstance()->getSpriteData(path);
|
|
|
|
if (spritedata)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
for (auto it : spritedata->meshVertexDatas)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
_meshVertexDatas.pushBack(it);
|
|
|
|
}
|
|
|
|
_skeleton = Skeleton3D::create(spritedata->nodedatas->skeleton);
|
|
|
|
CC_SAFE_RETAIN(_skeleton);
|
|
|
|
|
|
|
|
const bool singleSprite = (spritedata->nodedatas->nodes.size() == 1);
|
2021-12-25 10:04:45 +08:00
|
|
|
for (const auto& it : spritedata->nodedatas->nodes)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (it)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
createNode(it, this, *(spritedata->materialdatas), singleSprite);
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
for (const auto& it : spritedata->nodedatas->skeleton)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (it)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
createAttachSprite3DNode(it, *(spritedata->materialdatas));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
for (ssize_t i = 0, size = _meshes.size(); i < size; ++i)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
// cloning is needed in order to have one state per sprite
|
|
|
|
auto glstate = spritedata->programStates.at(i);
|
|
|
|
_meshes.at(i)->setProgramState(glstate->clone());
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
bool Sprite3D::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
|
|
|
{
|
|
|
|
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(path);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
std::string ext = FileUtils::getInstance()->getFileExtension(path);
|
|
|
|
if (ext == ".obj")
|
|
|
|
{
|
|
|
|
return Bundle3D::loadObj(*meshdatas, *materialdatas, *nodedatas, fullPath);
|
|
|
|
}
|
|
|
|
else if (ext == ".c3b" || ext == ".c3t")
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// load from .c3b or .c3t
|
2019-11-23 20:27:39 +08:00
|
|
|
auto bundle = Bundle3D::createBundle();
|
|
|
|
if (!bundle->load(fullPath))
|
|
|
|
{
|
|
|
|
Bundle3D::destroyBundle(bundle);
|
|
|
|
return false;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
auto ret =
|
|
|
|
bundle->loadMeshDatas(*meshdatas) && bundle->loadMaterials(*materialdatas) && bundle->loadNodes(*nodedatas);
|
2019-11-23 20:27:39 +08:00
|
|
|
Bundle3D::destroyBundle(bundle);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Sprite3D::Sprite3D()
|
2021-12-25 10:04:45 +08:00
|
|
|
: _skeleton(nullptr)
|
|
|
|
, _blend(BlendFunc::ALPHA_NON_PREMULTIPLIED)
|
|
|
|
, _aabbDirty(true)
|
|
|
|
, _lightMask(-1)
|
|
|
|
, _shaderUsingLight(false)
|
|
|
|
, _forceDepthWrite(false)
|
|
|
|
, _usingAutogeneratedGLProgram(true)
|
|
|
|
{}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
Sprite3D::~Sprite3D()
|
|
|
|
{
|
|
|
|
_meshes.clear();
|
|
|
|
_meshVertexDatas.clear();
|
|
|
|
CC_SAFE_RELEASE_NULL(_skeleton);
|
|
|
|
removeAllAttachNode();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Sprite3D::init()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (Node::init())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
bool Sprite3D::initWithFile(std::string_view path)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_aabbDirty = true;
|
|
|
|
_meshes.clear();
|
|
|
|
_meshVertexDatas.clear();
|
|
|
|
CC_SAFE_RELEASE_NULL(_skeleton);
|
|
|
|
removeAllAttachNode();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (loadFromCache(path))
|
|
|
|
return true;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
MeshDatas* meshdatas = new MeshDatas();
|
2021-12-08 00:11:53 +08:00
|
|
|
MaterialDatas* materialdatas = new MaterialDatas();
|
2021-12-25 10:04:45 +08:00
|
|
|
NodeDatas* nodeDatas = new NodeDatas();
|
2019-11-23 20:27:39 +08:00
|
|
|
if (loadFromFile(path, nodeDatas, meshdatas, materialdatas))
|
|
|
|
{
|
|
|
|
if (initFrom(*nodeDatas, *meshdatas, *materialdatas))
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// add to cache
|
|
|
|
auto data = new Sprite3DCache::Sprite3DData();
|
|
|
|
data->materialdatas = materialdatas;
|
|
|
|
data->nodedatas = nodeDatas;
|
2019-11-23 20:27:39 +08:00
|
|
|
data->meshVertexDatas = _meshVertexDatas;
|
2021-12-25 10:04:45 +08:00
|
|
|
for (const auto mesh : _meshes)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
data->programStates.pushBack(mesh->getProgramState());
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
Sprite3DCache::getInstance()->addSprite3DData(path, data);
|
|
|
|
CC_SAFE_DELETE(meshdatas);
|
|
|
|
_contentSize = getBoundingBox().size;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(meshdatas);
|
|
|
|
CC_SAFE_DELETE(materialdatas);
|
|
|
|
CC_SAFE_DELETE(nodeDatas);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Sprite3D::initFrom(const NodeDatas& nodeDatas, const MeshDatas& meshdatas, const MaterialDatas& materialdatas)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
for (const auto& it : meshdatas.meshDatas)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (it)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// Mesh* mesh = Mesh::create(*it);
|
|
|
|
// _meshes.pushBack(mesh);
|
2019-11-23 20:27:39 +08:00
|
|
|
auto meshvertex = MeshVertexData::create(*it);
|
|
|
|
_meshVertexDatas.pushBack(meshvertex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_skeleton = Skeleton3D::create(nodeDatas.skeleton);
|
|
|
|
CC_SAFE_RETAIN(_skeleton);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
auto size = nodeDatas.nodes.size();
|
2021-12-25 10:04:45 +08:00
|
|
|
for (const auto& it : nodeDatas.nodes)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (it)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
createNode(it, this, materialdatas, size == 1);
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
for (const auto& it : nodeDatas.skeleton)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (it)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
createAttachSprite3DNode(it, materialdatas);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
genMaterial();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Sprite3D* Sprite3D::createSprite3DNode(NodeData* nodedata, ModelData* modeldata, const MaterialDatas& materialdatas)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
auto sprite = new Sprite3D();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2021-12-08 00:11:53 +08:00
|
|
|
sprite->setName(nodedata->id);
|
|
|
|
auto mesh = Mesh::create(nodedata->id, getMeshIndexData(modeldata->subMeshId));
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2021-12-08 00:11:53 +08:00
|
|
|
if (_skeleton && modeldata->bones.size())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
auto skin = MeshSkin::create(_skeleton, modeldata->bones, modeldata->invBindPose);
|
|
|
|
mesh->setSkin(skin);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2021-12-08 00:11:53 +08:00
|
|
|
if (modeldata->materialId == "" && materialdatas.materials.size())
|
|
|
|
{
|
|
|
|
const NTextureData* textureData = materialdatas.materials[0].getTextureData(NTextureData::Usage::Diffuse);
|
|
|
|
mesh->setTexture(textureData->filename);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const NMaterialData* materialData = materialdatas.getMaterialData(modeldata->materialId);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (materialData)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
const NTextureData* textureData = materialData->getTextureData(NTextureData::Usage::Diffuse);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (textureData)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
mesh->setTexture(textureData->filename);
|
|
|
|
auto tex = mesh->getTexture();
|
2021-12-25 10:04:45 +08:00
|
|
|
if (tex)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
Texture2D::TexParams texParams;
|
2021-12-25 10:04:45 +08:00
|
|
|
texParams.minFilter = backend::SamplerFilter::LINEAR;
|
|
|
|
texParams.magFilter = backend::SamplerFilter::LINEAR;
|
2021-12-08 00:11:53 +08:00
|
|
|
texParams.sAddressMode = textureData->wrapS;
|
|
|
|
texParams.tAddressMode = textureData->wrapT;
|
|
|
|
tex->setTexParameters(texParams);
|
|
|
|
mesh->_isTransparent = (materialData->getTextureData(NTextureData::Usage::Transparency) != nullptr);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-08 00:11:53 +08:00
|
|
|
}
|
|
|
|
textureData = materialData->getTextureData(NTextureData::Usage::Normal);
|
|
|
|
if (textureData)
|
|
|
|
{
|
|
|
|
auto tex = _director->getTextureCache()->addImage(textureData->filename);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (tex)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
Texture2D::TexParams texParams;
|
2021-12-25 10:04:45 +08:00
|
|
|
texParams.minFilter = backend::SamplerFilter::LINEAR;
|
|
|
|
texParams.magFilter = backend::SamplerFilter::LINEAR;
|
2021-12-08 00:11:53 +08:00
|
|
|
texParams.sAddressMode = textureData->wrapS;
|
|
|
|
texParams.tAddressMode = textureData->wrapT;
|
|
|
|
tex->setTexParameters(texParams);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-08 00:11:53 +08:00
|
|
|
mesh->setTexture(tex, NTextureData::Usage::Normal);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-08 00:11:53 +08:00
|
|
|
|
|
|
|
// set locale transform
|
|
|
|
Vec3 pos;
|
|
|
|
Quaternion qua;
|
|
|
|
Vec3 scale;
|
|
|
|
nodedata->transform.decompose(&scale, &qua, &pos);
|
|
|
|
sprite->setPosition3D(pos);
|
|
|
|
sprite->setRotationQuat(qua);
|
|
|
|
sprite->setScaleX(scale.x);
|
|
|
|
sprite->setScaleY(scale.y);
|
|
|
|
sprite->setScaleZ(scale.z);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2021-12-08 00:11:53 +08:00
|
|
|
sprite->addMesh(mesh);
|
|
|
|
sprite->autorelease();
|
|
|
|
sprite->genMaterial();
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
return sprite;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
void Sprite3D::createAttachSprite3DNode(NodeData* nodedata, const MaterialDatas& materialdatas)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
for (const auto& it : nodedata->modelNodeDatas)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (it && getAttachNode(nodedata->id))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto sprite = createSprite3DNode(nodedata, it, materialdatas);
|
2019-11-23 20:27:39 +08:00
|
|
|
if (sprite)
|
|
|
|
{
|
|
|
|
getAttachNode(nodedata->id)->addChild(sprite);
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
for (const auto& it : nodedata->children)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
createAttachSprite3DNode(it, materialdatas);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Sprite3D::setMaterial(Material* material)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
setMaterial(material, -1);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Sprite3D::setMaterial(Material* material, int meshIndex)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
CCASSERT(material, "Invalid Material");
|
2021-12-25 10:04:45 +08:00
|
|
|
CCASSERT(meshIndex == -1 || (meshIndex >= 0 && meshIndex < _meshes.size()), "Invalid meshIndex");
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
if (meshIndex == -1)
|
|
|
|
{
|
|
|
|
for (ssize_t i = 0, size = _meshes.size(); i < size; ++i)
|
|
|
|
{
|
|
|
|
_meshes.at(i)->setMaterial(i == 0 ? material : material->clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto mesh = _meshes.at(meshIndex);
|
|
|
|
mesh->setMaterial(material);
|
|
|
|
}
|
|
|
|
|
|
|
|
_usingAutogeneratedGLProgram = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Material* Sprite3D::getMaterial(int meshIndex) const
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
CCASSERT(meshIndex >= 0 && meshIndex < _meshes.size(), "Invalid meshIndex");
|
2019-11-23 20:27:39 +08:00
|
|
|
return _meshes.at(meshIndex)->getMaterial();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sprite3D::genMaterial(bool useLight)
|
|
|
|
{
|
|
|
|
_shaderUsingLight = useLight;
|
|
|
|
|
|
|
|
std::unordered_map<const MeshVertexData*, Sprite3DMaterial*> materials;
|
2021-12-25 10:04:45 +08:00
|
|
|
for (auto meshVertexData : _meshVertexDatas)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto material = getSprite3DMaterialForAttribs(meshVertexData, useLight);
|
|
|
|
CCASSERT(material, "material should not be null");
|
|
|
|
materials[meshVertexData] = material;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
for (auto& mesh : _meshes)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto material = materials[mesh->getMeshIndexData()->getMeshVertexData()];
|
2021-12-25 10:04:45 +08:00
|
|
|
// keep original state block if exist
|
2019-11-23 20:27:39 +08:00
|
|
|
auto oldmaterial = mesh->getMaterial();
|
|
|
|
if (oldmaterial)
|
|
|
|
{
|
|
|
|
material->setStateBlock(oldmaterial->getStateBlock());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (material->getReferenceCount() == 1)
|
|
|
|
mesh->setMaterial(material);
|
|
|
|
else
|
|
|
|
mesh->setMaterial(material->clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sprite3D::createNode(NodeData* nodedata, Node* root, const MaterialDatas& materialdatas, bool singleSprite)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Node* node = nullptr;
|
|
|
|
for (const auto& it : nodedata->modelNodeDatas)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (it)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!it->bones.empty() || singleSprite)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (singleSprite && root != nullptr)
|
2019-11-23 20:27:39 +08:00
|
|
|
root->setName(nodedata->id);
|
|
|
|
auto mesh = Mesh::create(nodedata->id, getMeshIndexData(it->subMeshId));
|
2021-12-25 10:04:45 +08:00
|
|
|
if (mesh)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_meshes.pushBack(mesh);
|
|
|
|
if (_skeleton && it->bones.size())
|
|
|
|
{
|
|
|
|
auto skin = MeshSkin::create(_skeleton, it->bones, it->invBindPose);
|
|
|
|
mesh->setSkin(skin);
|
|
|
|
}
|
|
|
|
mesh->_visibleChanged = std::bind(&Sprite3D::onAABBDirty, this);
|
|
|
|
|
|
|
|
if (it->materialId == "" && materialdatas.materials.size())
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
const NTextureData* textureData =
|
|
|
|
materialdatas.materials[0].getTextureData(NTextureData::Usage::Diffuse);
|
2019-11-23 20:27:39 +08:00
|
|
|
mesh->setTexture(textureData->filename);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const NMaterialData* materialData = materialdatas.getMaterialData(it->materialId);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (materialData)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
const NTextureData* textureData =
|
|
|
|
materialData->getTextureData(NTextureData::Usage::Diffuse);
|
|
|
|
if (textureData)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
mesh->setTexture(textureData->filename);
|
|
|
|
auto tex = mesh->getTexture();
|
2021-12-25 10:04:45 +08:00
|
|
|
if (tex)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
Texture2D::TexParams texParams;
|
2021-12-25 10:04:45 +08:00
|
|
|
texParams.minFilter = backend::SamplerFilter::LINEAR;
|
|
|
|
texParams.magFilter = backend::SamplerFilter::LINEAR;
|
2019-11-23 20:27:39 +08:00
|
|
|
texParams.sAddressMode = textureData->wrapS;
|
|
|
|
texParams.tAddressMode = textureData->wrapT;
|
|
|
|
tex->setTexParameters(texParams);
|
2021-12-25 10:04:45 +08:00
|
|
|
mesh->_isTransparent =
|
|
|
|
(materialData->getTextureData(NTextureData::Usage::Transparency) != nullptr);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
textureData = materialData->getTextureData(NTextureData::Usage::Normal);
|
|
|
|
if (textureData)
|
|
|
|
{
|
2021-04-22 22:01:47 +08:00
|
|
|
auto tex = _director->getTextureCache()->addImage(textureData->filename);
|
2019-11-23 20:27:39 +08:00
|
|
|
if (tex)
|
|
|
|
{
|
|
|
|
Texture2D::TexParams texParams;
|
2021-12-25 10:04:45 +08:00
|
|
|
texParams.minFilter = backend::SamplerFilter::LINEAR;
|
|
|
|
texParams.magFilter = backend::SamplerFilter::LINEAR;
|
2019-11-23 20:27:39 +08:00
|
|
|
texParams.sAddressMode = textureData->wrapS;
|
|
|
|
texParams.tAddressMode = textureData->wrapT;
|
|
|
|
tex->setTexParameters(texParams);
|
|
|
|
}
|
|
|
|
mesh->setTexture(tex, NTextureData::Usage::Normal);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
Vec3 pos;
|
|
|
|
Quaternion qua;
|
|
|
|
Vec3 scale;
|
|
|
|
nodedata->transform.decompose(&scale, &qua, &pos);
|
|
|
|
setPosition3D(pos);
|
|
|
|
setRotationQuat(qua);
|
|
|
|
setScaleX(scale.x);
|
|
|
|
setScaleY(scale.y);
|
|
|
|
setScaleZ(scale.z);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
node = this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto sprite = createSprite3DNode(nodedata, it, materialdatas);
|
2019-11-23 20:27:39 +08:00
|
|
|
if (sprite)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (root)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
root->addChild(sprite);
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
node = sprite;
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (nodedata->modelNodeDatas.size() == 0)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
node = Node::create();
|
|
|
|
if (node)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
node->setName(nodedata->id);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// set locale transform
|
|
|
|
Vec3 pos;
|
|
|
|
Quaternion qua;
|
|
|
|
Vec3 scale;
|
|
|
|
nodedata->transform.decompose(&scale, &qua, &pos);
|
|
|
|
node->setPosition3D(pos);
|
|
|
|
node->setRotationQuat(qua);
|
|
|
|
node->setScaleX(scale.x);
|
|
|
|
node->setScaleY(scale.y);
|
|
|
|
node->setScaleZ(scale.z);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
if (root)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
root->addChild(node);
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto size = nodedata->children.size();
|
2021-12-25 10:04:45 +08:00
|
|
|
for (const auto& it : nodedata->children)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
createNode(it, node, materialdatas, size == 1);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
MeshIndexData* Sprite3D::getMeshIndexData(std::string_view indexId) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
for (auto it : _meshVertexDatas)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
auto index = it->getMeshIndexDataById(indexId);
|
|
|
|
if (index)
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Sprite3D::addMesh(Mesh* mesh)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto meshVertex = mesh->getMeshIndexData()->_vertexData;
|
|
|
|
_meshVertexDatas.pushBack(meshVertex);
|
|
|
|
_meshes.pushBack(mesh);
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
void Sprite3D::setTexture(std::string_view texFile)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-04-22 22:01:47 +08:00
|
|
|
auto tex = _director->getTextureCache()->addImage(texFile);
|
2019-11-23 20:27:39 +08:00
|
|
|
setTexture(tex);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sprite3D::setTexture(Texture2D* texture)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
for (auto mesh : _meshes)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
mesh->setTexture(texture);
|
|
|
|
}
|
|
|
|
}
|
2021-12-26 23:26:34 +08:00
|
|
|
AttachNode* Sprite3D::getAttachNode(std::string_view boneName)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto it = _attachments.find(boneName);
|
|
|
|
if (it != _attachments.end())
|
|
|
|
return it->second;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (_skeleton)
|
|
|
|
{
|
|
|
|
auto bone = _skeleton->getBoneByName(boneName);
|
|
|
|
if (bone)
|
|
|
|
{
|
|
|
|
auto attachNode = AttachNode::create(bone);
|
|
|
|
addChild(attachNode);
|
2021-12-26 23:26:34 +08:00
|
|
|
_attachments.emplace(boneName, attachNode); // _attachments[boneName] = attachNode;
|
2019-11-23 20:27:39 +08:00
|
|
|
return attachNode;
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
void Sprite3D::removeAttachNode(std::string_view boneName)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto it = _attachments.find(boneName);
|
|
|
|
if (it != _attachments.end())
|
|
|
|
{
|
|
|
|
removeChild(it->second);
|
|
|
|
_attachments.erase(it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sprite3D::removeAllAttachNode()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
for (auto& it : _attachments)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
removeChild(it.second);
|
|
|
|
}
|
|
|
|
_attachments.clear();
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Sprite3D::visit(cocos2d::Renderer* renderer, const cocos2d::Mat4& parentTransform, uint32_t parentFlags)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
// quick return if not visible. children won't be drawn.
|
|
|
|
if (!_visible)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
uint32_t flags = processParentFlags(parentTransform, parentFlags);
|
|
|
|
flags |= FLAGS_RENDER_AS_3D;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
//
|
2021-04-22 22:01:47 +08:00
|
|
|
_director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
|
|
|
|
_director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, _modelViewTransform);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
bool visibleByCamera = isVisitableByVisitingCamera();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
int i = 0;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
if (!_children.empty())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
sortAllChildren();
|
|
|
|
// draw children zOrder < 0
|
2021-12-25 10:04:45 +08:00
|
|
|
for (auto size = _children.size(); i < size; i++)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto node = _children.at(i);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (node && node->getLocalZOrder() < 0)
|
|
|
|
node->visit(renderer, _modelViewTransform, flags);
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// self draw
|
|
|
|
if (visibleByCamera)
|
|
|
|
this->draw(renderer, _modelViewTransform, flags);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
for (auto it = _children.cbegin() + i, itCend = _children.cend(); it != itCend; ++it)
|
2019-11-23 20:27:39 +08:00
|
|
|
(*it)->visit(renderer, _modelViewTransform, flags);
|
|
|
|
}
|
|
|
|
else if (visibleByCamera)
|
|
|
|
{
|
|
|
|
this->draw(renderer, _modelViewTransform, flags);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2021-04-22 22:01:47 +08:00
|
|
|
_director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Sprite3D::draw(Renderer* renderer, const Mat4& transform, uint32_t flags)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
#if CC_USE_CULLING
|
2021-12-25 10:04:45 +08:00
|
|
|
// TODO new-renderer: interface isVisibleInFrustum removal
|
|
|
|
// camera clipping
|
|
|
|
// if(_children.empty() && Camera::getVisitingCamera() &&
|
|
|
|
// !Camera::getVisitingCamera()->isVisibleInFrustum(&getAABB()))
|
2019-11-23 20:27:39 +08:00
|
|
|
// return;
|
|
|
|
#endif
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (_skeleton)
|
|
|
|
_skeleton->updateBoneMatrix();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
Color4F color(getDisplayedColor());
|
|
|
|
color.a = getDisplayedOpacity() / 255.0f;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
// check light and determine the shader used
|
2019-11-23 20:27:39 +08:00
|
|
|
const auto& scene = Director::getInstance()->getRunningScene();
|
|
|
|
|
|
|
|
// Don't override GLProgramState if using manually set Material
|
|
|
|
if (_usingAutogeneratedGLProgram && scene)
|
|
|
|
{
|
|
|
|
const auto lights = scene->getLights();
|
2021-12-25 10:04:45 +08:00
|
|
|
bool usingLight = false;
|
|
|
|
for (const auto light : lights)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
usingLight = light->isEnabled() && ((static_cast<unsigned int>(light->getLightFlag()) & _lightMask) > 0);
|
|
|
|
if (usingLight)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (usingLight != _shaderUsingLight)
|
|
|
|
{
|
|
|
|
genMaterial(usingLight);
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
for (auto mesh : _meshes)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
mesh->draw(renderer, _globalZOrder, transform, flags, _lightMask, Vec4(color.r, color.g, color.b, color.a),
|
2019-11-23 20:27:39 +08:00
|
|
|
_forceDepthWrite);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-16 16:23:14 +08:00
|
|
|
bool Sprite3D::setProgramState(backend::ProgramState* programState, bool needsRetain)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (Node::setProgramState(programState, needsRetain))
|
|
|
|
{
|
|
|
|
for (auto state : _meshes)
|
|
|
|
{
|
2020-10-16 16:23:14 +08:00
|
|
|
state->setProgramState(programState);
|
|
|
|
}
|
|
|
|
return true;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2020-10-16 16:23:14 +08:00
|
|
|
return false;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Sprite3D::setBlendFunc(const BlendFunc& blendFunc)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_blend.src != blendFunc.src || _blend.dst != blendFunc.dst)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_blend = blendFunc;
|
2021-12-25 10:04:45 +08:00
|
|
|
for (auto mesh : _meshes)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
mesh->setBlendFunc(blendFunc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const BlendFunc& Sprite3D::getBlendFunc() const
|
|
|
|
{
|
|
|
|
return _blend;
|
|
|
|
}
|
|
|
|
|
|
|
|
AABB Sprite3D::getAABBRecursively()
|
|
|
|
{
|
|
|
|
return getAABBRecursivelyImp(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
const AABB& Sprite3D::getAABB() const
|
|
|
|
{
|
|
|
|
Mat4 nodeToWorldTransform(getNodeToWorldTransform());
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// If nodeToWorldTransform matrix isn't changed, we don't need to transform aabb.
|
|
|
|
if (memcmp(_nodeToWorldTransform.m, nodeToWorldTransform.m, sizeof(Mat4)) == 0 && !_aabbDirty)
|
|
|
|
{
|
|
|
|
return _aabb;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_aabb.reset();
|
|
|
|
if (_meshes.size())
|
|
|
|
{
|
|
|
|
Mat4 transform(nodeToWorldTransform);
|
2021-12-25 10:04:45 +08:00
|
|
|
for (const auto& it : _meshes)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
if (it->isVisible())
|
|
|
|
_aabb.merge(it->getAABB());
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
_aabb.transform(transform);
|
|
|
|
_nodeToWorldTransform = nodeToWorldTransform;
|
2021-12-25 10:04:45 +08:00
|
|
|
_aabbDirty = false;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return _aabb;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Action* Sprite3D::runAction(Action* action)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
setForceDepthWrite(true);
|
|
|
|
return Node::runAction(action);
|
|
|
|
}
|
|
|
|
|
|
|
|
Rect Sprite3D::getBoundingBox() const
|
|
|
|
{
|
|
|
|
AABB aabb = getAABB();
|
|
|
|
Rect ret(aabb._min.x, aabb._min.y, (aabb._max.x - aabb._min.x), (aabb._max.y - aabb._min.y));
|
2021-12-25 10:04:45 +08:00
|
|
|
return ret;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Sprite3D::setCullFace(CullFaceSide side)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
for (auto& it : _meshes)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
it->getMaterial()->getStateBlock().setCullFaceSide(side);
|
2021-12-25 10:04:45 +08:00
|
|
|
// it->getMeshCommand().setCullFace(cullFace);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sprite3D::setCullFaceEnabled(bool enable)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
for (auto& it : _meshes)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
it->getMaterial()->getStateBlock().setCullFace(enable);
|
2021-12-25 10:04:45 +08:00
|
|
|
// it->getMeshCommand().setCullFaceEnabled(enable);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Mesh* Sprite3D::getMeshByIndex(int index) const
|
|
|
|
{
|
|
|
|
CCASSERT(index < _meshes.size(), "invalid index");
|
|
|
|
return _meshes.at(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**get Mesh by Name */
|
2021-12-26 23:26:34 +08:00
|
|
|
Mesh* Sprite3D::getMeshByName(std::string_view name) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
for (const auto& it : _meshes)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
if (it->getName() == name)
|
|
|
|
return it;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
std::vector<Mesh*> Sprite3D::getMeshArrayByName(std::string_view name) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
std::vector<Mesh*> meshes;
|
2021-12-25 10:04:45 +08:00
|
|
|
for (const auto& it : _meshes)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
if (it->getName() == name)
|
|
|
|
meshes.push_back(it);
|
|
|
|
}
|
|
|
|
return meshes;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Mesh* Sprite3D::getMesh() const
|
|
|
|
{
|
|
|
|
if (_meshes.empty())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
return _meshes.at(0);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Sprite3D::setForce2DQueue(bool force2D)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
for (const auto& mesh : _meshes)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
mesh->setForce2DQueue(force2D);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
|
|
|
Sprite3DCache* Sprite3DCache::_cacheInstance = nullptr;
|
|
|
|
Sprite3DCache* Sprite3DCache::getInstance()
|
|
|
|
{
|
|
|
|
if (_cacheInstance == nullptr)
|
2021-12-08 00:11:53 +08:00
|
|
|
_cacheInstance = new Sprite3DCache();
|
2019-11-23 20:27:39 +08:00
|
|
|
return _cacheInstance;
|
|
|
|
}
|
|
|
|
void Sprite3DCache::destroyInstance()
|
|
|
|
{
|
|
|
|
if (_cacheInstance)
|
|
|
|
{
|
|
|
|
delete _cacheInstance;
|
|
|
|
_cacheInstance = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
Sprite3DCache::Sprite3DData* Sprite3DCache::getSpriteData(std::string_view key) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto it = _spriteDatas.find(key);
|
|
|
|
if (it != _spriteDatas.end())
|
|
|
|
return it->second;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
bool Sprite3DCache::addSprite3DData(std::string_view key, Sprite3DCache::Sprite3DData* spritedata)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto it = _spriteDatas.find(key);
|
|
|
|
if (it == _spriteDatas.end())
|
|
|
|
{
|
2021-12-26 23:26:34 +08:00
|
|
|
_spriteDatas.emplace(key, spritedata); // _spriteDatas[key] = spritedata;
|
2019-11-23 20:27:39 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
void Sprite3DCache::removeSprite3DData(std::string_view key)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto it = _spriteDatas.find(key);
|
|
|
|
if (it != _spriteDatas.end())
|
|
|
|
{
|
|
|
|
delete it->second;
|
|
|
|
_spriteDatas.erase(it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sprite3DCache::removeAllSprite3DData()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
for (auto& it : _spriteDatas)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
delete it.second;
|
|
|
|
}
|
|
|
|
_spriteDatas.clear();
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Sprite3DCache::Sprite3DCache() {}
|
2019-11-23 20:27:39 +08:00
|
|
|
Sprite3DCache::~Sprite3DCache()
|
|
|
|
{
|
|
|
|
removeAllSprite3DData();
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// MARK: Helpers
|
|
|
|
//
|
|
|
|
static Sprite3DMaterial* getSprite3DMaterialForAttribs(MeshVertexData* meshVertexData, bool usesLight)
|
|
|
|
{
|
|
|
|
bool textured = meshVertexData->hasVertexAttrib(shaderinfos::VertexKey::VERTEX_ATTRIB_TEX_COORD);
|
2021-12-25 10:04:45 +08:00
|
|
|
bool hasSkin = meshVertexData->hasVertexAttrib(shaderinfos::VertexKey::VERTEX_ATTRIB_BLEND_INDEX) &&
|
|
|
|
meshVertexData->hasVertexAttrib(shaderinfos::VertexKey::VERTEX_ATTRIB_BLEND_WEIGHT);
|
|
|
|
bool hasNormal = meshVertexData->hasVertexAttrib(shaderinfos::VertexKey::VERTEX_ATTRIB_NORMAL);
|
|
|
|
bool hasTangentSpace = meshVertexData->hasVertexAttrib(shaderinfos::VertexKey::VERTEX_ATTRIB_TANGENT) &&
|
|
|
|
meshVertexData->hasVertexAttrib(shaderinfos::VertexKey::VERTEX_ATTRIB_BINORMAL);
|
2019-11-23 20:27:39 +08:00
|
|
|
Sprite3DMaterial::MaterialType type;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (textured)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (hasTangentSpace)
|
|
|
|
{
|
|
|
|
type = hasNormal && usesLight ? Sprite3DMaterial::MaterialType::BUMPED_DIFFUSE
|
|
|
|
: Sprite3DMaterial::MaterialType::UNLIT;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
type = hasNormal && usesLight ? Sprite3DMaterial::MaterialType::DIFFUSE
|
|
|
|
: Sprite3DMaterial::MaterialType::UNLIT;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
type = hasNormal && usesLight ? Sprite3DMaterial::MaterialType::DIFFUSE_NOTEX
|
|
|
|
: Sprite3DMaterial::MaterialType::UNLIT_NOTEX;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return Sprite3DMaterial::createBuiltInMaterial(type, hasSkin);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|