axmol/core/3d/MeshRenderer.cpp

1062 lines
33 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.
****************************************************************************/
#include "3d/MeshRenderer.h"
#include "3d/ObjLoader.h"
#include "3d/MeshSkin.h"
#include "3d/Bundle3D.h"
#include "3d/MeshMaterial.h"
#include "3d/AttachNode.h"
#include "3d/Mesh.h"
2019-11-23 20:27:39 +08:00
#include "base/Director.h"
#include "base/AsyncTaskPool.h"
#include "base/UTF8.h"
#include "base/Utils.h"
#include "2d/Light.h"
#include "2d/Camera.h"
#include "base/Macros.h"
#include "platform/PlatformMacros.h"
#include "platform/FileUtils.h"
#include "renderer/TextureCache.h"
#include "renderer/Renderer.h"
#include "renderer/Material.h"
#include "renderer/Technique.h"
#include "renderer/Pass.h"
2019-11-23 20:27:39 +08:00
NS_AX_BEGIN
2019-11-23 20:27:39 +08:00
static MeshMaterial* getMeshRendererMaterialForAttribs(MeshVertexData* meshVertexData, bool usesLight);
2019-11-23 20:27:39 +08:00
MeshRenderer* MeshRenderer::create()
2019-11-23 20:27:39 +08:00
{
auto mesh = new MeshRenderer();
if (mesh->init())
2019-11-23 20:27:39 +08:00
{
mesh->autorelease();
return mesh;
2019-11-23 20:27:39 +08:00
}
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(mesh);
2019-11-23 20:27:39 +08:00
return nullptr;
}
MeshRenderer* MeshRenderer::create(std::string_view modelPath)
2019-11-23 20:27:39 +08:00
{
2022-10-12 12:39:27 +08:00
return create(modelPath, hlookup::empty_sv);
2019-11-23 20:27:39 +08:00
}
2022-10-12 12:39:27 +08:00
MeshRenderer* MeshRenderer::create(std::string_view modelPath, std::string_view texturePath)
2019-11-23 20:27:39 +08:00
{
2022-10-12 12:39:27 +08:00
AXASSERT(modelPath.length() >= 4, "Invalid filename.");
auto meshRenderer = new MeshRenderer();
if (meshRenderer->initWithFile(modelPath))
2019-11-23 20:27:39 +08:00
{
2022-10-12 12:39:27 +08:00
meshRenderer->setModelTexture(modelPath, texturePath);
meshRenderer->_contentSize = meshRenderer->getBoundingBox().size;
meshRenderer->autorelease();
return meshRenderer;
2019-11-23 20:27:39 +08:00
}
2022-10-12 12:39:27 +08:00
AX_SAFE_DELETE(meshRenderer);
return nullptr;
2019-11-23 20:27:39 +08:00
}
void MeshRenderer::createAsync(std::string_view modelPath,
const std::function<void(MeshRenderer*, void*)>& callback,
2021-12-25 10:04:45 +08:00
void* callbackparam)
2019-11-23 20:27:39 +08:00
{
createAsync(modelPath, "", callback, callbackparam);
}
void MeshRenderer::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)
2019-11-23 20:27:39 +08:00
{
2022-10-12 12:39:27 +08:00
MeshRenderer* meshRenderer = new MeshRenderer();
if (meshRenderer->loadFromCache(modelPath))
2019-11-23 20:27:39 +08:00
{
2022-10-12 12:39:27 +08:00
meshRenderer->autorelease();
meshRenderer->setModelTexture(modelPath, texturePath);
callback(meshRenderer, callbackparam);
2019-11-23 20:27:39 +08:00
return;
}
2021-12-25 10:04:45 +08:00
2022-10-12 12:39:27 +08:00
meshRenderer->_asyncLoadParam.afterLoadCallback = callback;
meshRenderer->_asyncLoadParam.texPath = texturePath;
meshRenderer->_asyncLoadParam.modelPath = modelPath;
meshRenderer->_asyncLoadParam.modelFullPath = FileUtils::getInstance()->fullPathForFilename(modelPath);
meshRenderer->_asyncLoadParam.callbackParam = callbackparam;
meshRenderer->_asyncLoadParam.materialdatas = new MaterialDatas();
meshRenderer->_asyncLoadParam.meshdatas = new MeshDatas();
meshRenderer->_asyncLoadParam.nodeDatas = new NodeDatas();
2021-12-25 10:04:45 +08:00
AsyncTaskPool::getInstance()->enqueue(
2022-10-12 12:39:27 +08:00
AsyncTaskPool::TaskType::TASK_IO, AX_CALLBACK_1(MeshRenderer::afterAsyncLoad, meshRenderer),
(void*)(&meshRenderer->_asyncLoadParam), [meshRenderer]() {
auto& loadParam = meshRenderer->_asyncLoadParam;
loadParam.result = meshRenderer->loadFromFile(loadParam.modelFullPath, loadParam.nodeDatas,
loadParam.meshdatas, loadParam.materialdatas);
2021-12-25 10:04:45 +08:00
});
2019-11-23 20:27:39 +08:00
}
void MeshRenderer::afterAsyncLoad(void* param)
2019-11-23 20:27:39 +08:00
{
MeshRenderer::AsyncLoadParam* asyncParam = (MeshRenderer::AsyncLoadParam*)param;
2019-11-23 20:27:39 +08:00
autorelease();
if (asyncParam)
{
if (asyncParam->result)
{
_meshes.clear();
_meshVertexDatas.clear();
2022-07-16 10:43:05 +08:00
AX_SAFE_RELEASE_NULL(_skeleton);
2019-11-23 20:27:39 +08:00
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))
{
2022-07-12 21:31:54 +08:00
auto meshdata = MeshRendererCache::getInstance()->getMeshRenderData(asyncParam->modelPath);
if (meshdata == nullptr)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
// add to cache
2022-07-12 21:31:54 +08:00
auto data = new MeshRendererCache::MeshRenderData();
2021-12-25 10:04:45 +08:00
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
2022-07-12 21:31:54 +08:00
MeshRendererCache::getInstance()->addMeshRenderData(asyncParam->modelPath, data);
2021-12-25 10:04:45 +08:00
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(meshdatas);
2019-11-23 20:27:39 +08:00
materialdatas = nullptr;
2021-12-25 10:04:45 +08:00
nodeDatas = nullptr;
2019-11-23 20:27:39 +08:00
}
}
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(meshdatas);
AX_SAFE_DELETE(materialdatas);
AX_SAFE_DELETE(nodeDatas);
2021-12-25 10:04:45 +08:00
2022-10-12 12:39:27 +08:00
setModelTexture(asyncParam->modelPath, asyncParam->texPath);
2019-11-23 20:27:39 +08:00
}
else
{
2022-07-16 10:43:05 +08:00
AXLOG("file load failed: %s\n", asyncParam->modelPath.c_str());
2019-11-23 20:27:39 +08:00
}
asyncParam->afterLoadCallback(this, asyncParam->callbackParam);
}
}
AABB MeshRenderer::getAABBRecursivelyImp(Node* node)
2019-11-23 20:27:39 +08:00
{
AABB aabb;
for (auto&& iter : node->getChildren())
2021-12-25 10:04:45 +08:00
{
2019-11-23 20:27:39 +08:00
aabb.merge(getAABBRecursivelyImp(iter));
}
2021-12-25 10:04:45 +08:00
MeshRenderer* meshRenderer = dynamic_cast<MeshRenderer*>(node);
if (meshRenderer)
aabb.merge(meshRenderer->getAABB());
2019-11-23 20:27:39 +08:00
return aabb;
}
bool MeshRenderer::loadFromCache(std::string_view path)
2019-11-23 20:27:39 +08:00
{
2022-07-12 21:31:54 +08:00
auto meshdata = MeshRendererCache::getInstance()->getMeshRenderData(path);
if (meshdata)
2019-11-23 20:27:39 +08:00
{
for (auto&& it : meshdata->meshVertexDatas)
2021-12-25 10:04:45 +08:00
{
2019-11-23 20:27:39 +08:00
_meshVertexDatas.pushBack(it);
}
_skeleton = Skeleton3D::create(meshdata->nodedatas->skeleton);
2022-07-16 10:43:05 +08:00
AX_SAFE_RETAIN(_skeleton);
2019-11-23 20:27:39 +08:00
const bool singleMesh = (meshdata->nodedatas->nodes.size() == 1);
for (const auto& it : meshdata->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, *(meshdata->materialdatas), singleMesh);
2019-11-23 20:27:39 +08:00
}
}
2021-12-25 10:04:45 +08:00
for (const auto& it : meshdata->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
{
createAttachMeshRendererNode(it, *(meshdata->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)
{
// cloning is needed in order to have one state per mesh
auto glstate = meshdata->programStates.at(i);
2019-11-23 20:27:39 +08:00
_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;
}
bool MeshRenderer::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;
}
MeshRenderer::MeshRenderer()
2021-12-25 10:04:45 +08:00
: _skeleton(nullptr)
, _blend(BlendFunc::ALPHA_NON_PREMULTIPLIED)
, _lightMask(-1)
2022-10-12 12:39:27 +08:00
, _aabbDirty(true)
2021-12-25 10:04:45 +08:00
, _shaderUsingLight(false)
, _forceDepthWrite(false)
, _wireframe(false)
2021-12-25 10:04:45 +08:00
, _usingAutogeneratedGLProgram(true)
, _transparentMaterialHint(false)
2022-10-12 12:39:27 +08:00
, _meshTextureHint(0)
2021-12-25 10:04:45 +08:00
{}
2019-11-23 20:27:39 +08:00
MeshRenderer::~MeshRenderer()
2019-11-23 20:27:39 +08:00
{
_meshes.clear();
_meshVertexDatas.clear();
2022-07-16 10:43:05 +08:00
AX_SAFE_RELEASE_NULL(_skeleton);
2019-11-23 20:27:39 +08:00
removeAllAttachNode();
}
bool MeshRenderer::init()
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
if (Node::init())
2019-11-23 20:27:39 +08:00
{
return true;
}
return false;
}
bool MeshRenderer::initWithFile(std::string_view path)
2019-11-23 20:27:39 +08:00
{
_aabbDirty = true;
_meshes.clear();
_meshVertexDatas.clear();
2022-07-16 10:43:05 +08:00
AX_SAFE_RELEASE_NULL(_skeleton);
2019-11-23 20:27:39 +08:00
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
2022-07-12 21:31:54 +08:00
auto data = new MeshRendererCache::MeshRenderData();
2021-12-25 10:04:45 +08:00
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
2022-07-12 21:31:54 +08:00
MeshRendererCache::getInstance()->addMeshRenderData(path, data);
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(meshdatas);
2019-11-23 20:27:39 +08:00
_contentSize = getBoundingBox().size;
return true;
}
}
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(meshdatas);
AX_SAFE_DELETE(materialdatas);
AX_SAFE_DELETE(nodeDatas);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return false;
}
bool MeshRenderer::initFrom(const NodeDatas& nodeDatas, const MeshDatas& meshdatas, const MaterialDatas& materialdatas)
2019-11-23 20:27:39 +08:00
{
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);
2022-07-16 10:43:05 +08:00
AX_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
{
createAttachMeshRendererNode(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;
}
MeshRenderer* MeshRenderer::createMeshRendererNode(NodeData* nodedata, ModelData* modeldata, const MaterialDatas& materialdatas)
2019-11-23 20:27:39 +08:00
{
auto meshRenderer = new MeshRenderer();
2021-12-25 10:04:45 +08:00
meshRenderer->setName(nodedata->id);
2021-12-08 00:11:53 +08:00
auto mesh = Mesh::create(nodedata->id, getMeshIndexData(modeldata->subMeshId));
2021-12-25 10:04:45 +08:00
2022-10-12 12:39:27 +08:00
if (_skeleton && !modeldata->bones.empty())
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
2022-10-12 12:39:27 +08:00
if (modeldata->materialId.empty() && !materialdatas.materials.empty())
2021-12-08 00:11:53 +08:00
{
const NTextureData* textureData = materialdatas.materials[0].getTextureData(NTextureData::Usage::Diffuse);
2022-10-12 12:39:27 +08:00
setMeshTexture(mesh, textureData->filename);
2021-12-08 00:11:53 +08:00
}
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
{
2022-10-12 12:39:27 +08:00
setMeshTexture(mesh, textureData->filename);
2021-12-08 00:11:53 +08:00
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);
_transparentMaterialHint = 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)
{
2022-10-12 12:39:27 +08:00
auto tex = setMeshTexture(mesh, textureData->filename, NTextureData::Usage::Normal);
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
// set locale transform
Vec3 pos;
Quaternion qua;
Vec3 scale;
nodedata->transform.decompose(&scale, &qua, &pos);
meshRenderer->setPosition3D(pos);
meshRenderer->setRotationQuat(qua);
meshRenderer->setScaleX(scale.x);
meshRenderer->setScaleY(scale.y);
meshRenderer->setScaleZ(scale.z);
2021-12-25 10:04:45 +08:00
meshRenderer->addMesh(mesh);
meshRenderer->autorelease();
meshRenderer->genMaterial();
2021-12-08 00:11:53 +08:00
return meshRenderer;
2019-11-23 20:27:39 +08:00
}
void MeshRenderer::createAttachMeshRendererNode(NodeData* nodedata, const MaterialDatas& materialdatas)
2019-11-23 20:27:39 +08:00
{
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
{
auto mesh = createMeshRendererNode(nodedata, it, materialdatas);
if (mesh)
2019-11-23 20:27:39 +08:00
{
getAttachNode(nodedata->id)->addChild(mesh);
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
{
createAttachMeshRendererNode(it, materialdatas);
2019-11-23 20:27:39 +08:00
}
}
void MeshRenderer::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
}
void MeshRenderer::setMaterial(Material* material, int meshIndex)
2019-11-23 20:27:39 +08:00
{
2022-07-16 10:43:05 +08:00
AXASSERT(material, "Invalid Material");
AXASSERT(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* MeshRenderer::getMaterial(int meshIndex) const
2019-11-23 20:27:39 +08:00
{
2022-07-16 10:43:05 +08:00
AXASSERT(meshIndex >= 0 && meshIndex < _meshes.size(), "Invalid meshIndex.");
2019-11-23 20:27:39 +08:00
return _meshes.at(meshIndex)->getMaterial();
}
void MeshRenderer::genMaterial(bool useLight)
2019-11-23 20:27:39 +08:00
{
_shaderUsingLight = useLight;
std::unordered_map<const MeshVertexData*, MeshMaterial*> materials;
for (auto&& meshVertexData : _meshVertexDatas)
2019-11-23 20:27:39 +08:00
{
auto material = getMeshRendererMaterialForAttribs(meshVertexData, useLight);
2022-07-16 10:43:05 +08:00
AXASSERT(material, "material should cannot be null.");
2019-11-23 20:27:39 +08:00
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()];
material->setTransparent(_transparentMaterialHint);
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());
material->setTransparent(oldmaterial->isTransparent());
2019-11-23 20:27:39 +08:00
}
if (material->getReferenceCount() == 1)
mesh->setMaterial(material);
else
mesh->setMaterial(material->clone());
}
}
void MeshRenderer::createNode(NodeData* nodedata, Node* root, const MaterialDatas& materialdatas, bool singleMesh)
2019-11-23 20:27:39 +08:00
{
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
{
if (!it->bones.empty() || singleMesh)
2019-11-23 20:27:39 +08:00
{
if (singleMesh && 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(&MeshRenderer::onAABBDirty, this);
2019-11-23 20:27:39 +08:00
if (it->materialId == "" && materialdatas.materials.size())
{
2021-12-25 10:04:45 +08:00
const NTextureData* textureData =
materialdatas.materials[0].getTextureData(NTextureData::Usage::Diffuse);
2022-10-12 12:39:27 +08:00
setMeshTexture(mesh, textureData->filename);
2019-11-23 20:27:39 +08:00
}
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
{
2022-10-12 12:39:27 +08:00
setMeshTexture(mesh, textureData->filename);
2019-11-23 20:27:39 +08:00
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);
_transparentMaterialHint = materialData->getTextureData(NTextureData::Usage::Transparency) != nullptr;
2019-11-23 20:27:39 +08:00
}
}
textureData = materialData->getTextureData(NTextureData::Usage::Normal);
if (textureData)
{
2022-10-12 12:39:27 +08:00
auto tex = setMeshTexture(mesh, textureData->filename, NTextureData::Usage::Normal);
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);
}
}
}
}
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
{
auto mesh = createMeshRendererNode(nodedata, it, materialdatas);
if (mesh)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
if (root)
2019-11-23 20:27:39 +08:00
{
root->addChild(mesh);
2021-12-25 10:04:45 +08:00
}
2019-11-23 20:27:39 +08:00
}
node = mesh;
2021-12-25 10:04:45 +08:00
}
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
}
}
MeshIndexData* MeshRenderer::getMeshIndexData(std::string_view indexId) const
2019-11-23 20:27:39 +08:00
{
for (auto&& it : _meshVertexDatas)
2021-12-25 10:04:45 +08:00
{
2019-11-23 20:27:39 +08:00
auto index = it->getMeshIndexDataById(indexId);
if (index)
return index;
}
return nullptr;
}
void MeshRenderer::addMesh(Mesh* mesh)
2019-11-23 20:27:39 +08:00
{
auto meshVertex = mesh->getMeshIndexData()->_vertexData;
_meshVertexDatas.pushBack(meshVertex);
_meshes.pushBack(mesh);
}
2022-10-12 12:39:27 +08:00
Texture2D* MeshRenderer::setMeshTexture(Mesh* mesh, std::string_view texPath, NTextureData::Usage usage)
{
auto tex = _director->getTextureCache()->addImage(texPath);
mesh->setTexture(texPath, usage);
if (tex)
++_meshTextureHint;
return tex;
}
void MeshRenderer::setModelTexture(std::string_view modelPath, std::string_view texturePath) {
if (!texturePath.empty())
setTexture(texturePath);
else if (!_meshTextureHint)
{
auto pos = modelPath.find_last_of('.');
if (pos != std::string_view::npos)
{
std::string modelTexPath{modelPath};
modelTexPath.resize(pos);
modelTexPath.append(".png"sv);
setTexture(modelTexPath);
}
}
}
void MeshRenderer::setTexture(std::string_view texFile)
2019-11-23 20:27:39 +08:00
{
auto tex = _director->getTextureCache()->addImage(texFile);
2019-11-23 20:27:39 +08:00
setTexture(tex);
}
void MeshRenderer::setTexture(Texture2D* texture)
2019-11-23 20:27:39 +08:00
{
for (auto&& mesh : _meshes)
2021-12-25 10:04:45 +08:00
{
2019-11-23 20:27:39 +08:00
mesh->setTexture(texture);
}
}
AttachNode* MeshRenderer::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);
_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;
}
void MeshRenderer::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 MeshRenderer::removeAllAttachNode()
2019-11-23 20:27:39 +08:00
{
for (auto&& it : _attachments)
2021-12-25 10:04:45 +08:00
{
2019-11-23 20:27:39 +08:00
removeChild(it.second);
}
_attachments.clear();
}
2022-08-08 18:02:17 +08:00
void MeshRenderer::visit(ax::Renderer* renderer, const ax::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
//
_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
_director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
2019-11-23 20:27:39 +08:00
}
void MeshRenderer::draw(Renderer* renderer, const Mat4& transform, uint32_t flags)
2019-11-23 20:27:39 +08:00
{
2022-07-16 10:43:05 +08:00
#if AX_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),
_forceDepthWrite, _wireframe);
2019-11-23 20:27:39 +08:00
}
}
2023-08-25 16:57:38 +08:00
bool MeshRenderer::setProgramState(backend::ProgramState* programState, bool needsRetain)
2019-11-23 20:27:39 +08:00
{
2023-08-25 16:57:38 +08:00
if (Node::setProgramState(programState, needsRetain))
2021-12-25 10:04:45 +08:00
{
for (auto&& state : _meshes)
2021-12-25 10:04:45 +08:00
{
state->setProgramState(programState);
}
return true;
2019-11-23 20:27:39 +08:00
}
return false;
2019-11-23 20:27:39 +08:00
}
void MeshRenderer::setBlendFunc(const BlendFunc& blendFunc)
2019-11-23 20:27:39 +08:00
{
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;
for (auto&& mesh : _meshes)
2019-11-23 20:27:39 +08:00
{
mesh->setBlendFunc(blendFunc);
}
}
}
const BlendFunc& MeshRenderer::getBlendFunc() const
2019-11-23 20:27:39 +08:00
{
return _blend;
}
AABB MeshRenderer::getAABBRecursively()
2019-11-23 20:27:39 +08:00
{
return getAABBRecursivelyImp(this);
}
const AABB& MeshRenderer::getAABB() const
2019-11-23 20:27:39 +08:00
{
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;
}
Action* MeshRenderer::runAction(Action* action)
2019-11-23 20:27:39 +08:00
{
setForceDepthWrite(true);
return Node::runAction(action);
}
Rect MeshRenderer::getBoundingBox() const
2019-11-23 20:27:39 +08:00
{
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 MeshRenderer::setCullFace(CullFaceSide side)
2019-11-23 20:27:39 +08:00
{
for (auto&& it : _meshes)
2021-12-25 10:04:45 +08:00
{
2019-11-23 20:27:39 +08:00
it->getMaterial()->getStateBlock().setCullFaceSide(side);
}
}
void MeshRenderer::setCullFaceEnabled(bool enable)
2019-11-23 20:27:39 +08:00
{
for (auto&& it : _meshes)
2021-12-25 10:04:45 +08:00
{
2019-11-23 20:27:39 +08:00
it->getMaterial()->getStateBlock().setCullFace(enable);
}
}
Mesh* MeshRenderer::getMeshByIndex(int index) const
2019-11-23 20:27:39 +08:00
{
2022-07-16 10:43:05 +08:00
AXASSERT(index < _meshes.size(), "Invalid index.");
2019-11-23 20:27:39 +08:00
return _meshes.at(index);
}
/**get Mesh by Name */
Mesh* MeshRenderer::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;
}
std::vector<Mesh*> MeshRenderer::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.emplace_back(it);
2019-11-23 20:27:39 +08:00
}
return meshes;
}
Mesh* MeshRenderer::getMesh() const
2021-12-25 10:04:45 +08:00
{
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
}
///////////////////////////////////////////////////////////////////////////////////
MeshRendererCache* MeshRendererCache::_cacheInstance = nullptr;
MeshRendererCache* MeshRendererCache::getInstance()
2019-11-23 20:27:39 +08:00
{
if (_cacheInstance == nullptr)
_cacheInstance = new MeshRendererCache();
2019-11-23 20:27:39 +08:00
return _cacheInstance;
}
void MeshRendererCache::destroyInstance()
2019-11-23 20:27:39 +08:00
{
if (_cacheInstance)
{
delete _cacheInstance;
_cacheInstance = nullptr;
}
}
2022-07-12 21:31:54 +08:00
MeshRendererCache::MeshRenderData* MeshRendererCache::getMeshRenderData(std::string_view key) const
2019-11-23 20:27:39 +08:00
{
auto it = _meshDatas.find(key);
if (it != _meshDatas.end())
2019-11-23 20:27:39 +08:00
return it->second;
return nullptr;
}
2022-07-12 21:31:54 +08:00
bool MeshRendererCache::addMeshRenderData(std::string_view key, MeshRendererCache::MeshRenderData* meshdata)
2019-11-23 20:27:39 +08:00
{
auto it = _meshDatas.find(key);
if (it == _meshDatas.end())
2019-11-23 20:27:39 +08:00
{
_meshDatas.emplace(key, meshdata);
2019-11-23 20:27:39 +08:00
return true;
}
return false;
}
2022-07-12 21:31:54 +08:00
void MeshRendererCache::removeMeshRenderData(std::string_view key)
2019-11-23 20:27:39 +08:00
{
auto it = _meshDatas.find(key);
if (it != _meshDatas.end())
2019-11-23 20:27:39 +08:00
{
delete it->second;
_meshDatas.erase(it);
2019-11-23 20:27:39 +08:00
}
}
2022-07-12 21:31:54 +08:00
void MeshRendererCache::removeAllMeshRenderData()
2019-11-23 20:27:39 +08:00
{
for (auto&& it : _meshDatas)
2021-12-25 10:04:45 +08:00
{
2019-11-23 20:27:39 +08:00
delete it.second;
}
_meshDatas.clear();
2019-11-23 20:27:39 +08:00
}
MeshRendererCache::MeshRendererCache() {}
MeshRendererCache::~MeshRendererCache()
2019-11-23 20:27:39 +08:00
{
2022-07-12 21:31:54 +08:00
removeAllMeshRenderData();
2019-11-23 20:27:39 +08:00
}
static MeshMaterial* getMeshRendererMaterialForAttribs(MeshVertexData* meshVertexData, bool usesLight)
2019-11-23 20:27:39 +08:00
{
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);
MeshMaterial::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 ? MeshMaterial::MaterialType::BUMPED_DIFFUSE
: MeshMaterial::MaterialType::UNLIT;
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
else
{
type = hasNormal && usesLight ? MeshMaterial::MaterialType::DIFFUSE
: MeshMaterial::MaterialType::UNLIT;
2019-11-23 20:27:39 +08:00
}
}
else
{
type = hasNormal && usesLight ? MeshMaterial::MaterialType::DIFFUSE_NOTEX
: MeshMaterial::MaterialType::UNLIT_NOTEX;
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
return MeshMaterial::createBuiltInMaterial(type, hasSkin);
2019-11-23 20:27:39 +08:00
}
NS_AX_END