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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-06-11 13:08:08 +08:00
|
|
|
#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
|
|
|
|
2023-06-11 13:08:08 +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
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
static MeshMaterial* getMeshRendererMaterialForAttribs(MeshVertexData* meshVertexData, bool usesLight);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
MeshRenderer* MeshRenderer::create()
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
auto mesh = new MeshRenderer();
|
|
|
|
if (mesh->init())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-05 14:48:46 +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;
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
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
|
|
|
|
2022-07-05 14:48:46 +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
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +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);
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
void MeshRenderer::createAsync(std::string_view modelPath,
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string_view texturePath,
|
2022-07-05 14:48:46 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
void MeshRenderer::afterAsyncLoad(void* param)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-05 14:48:46 +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);
|
2022-07-05 14:48:46 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
AABB MeshRenderer::getAABBRecursivelyImp(Node* node)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
AABB aabb;
|
2022-07-21 19:19:08 +08:00
|
|
|
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
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
MeshRenderer* meshRenderer = dynamic_cast<MeshRenderer*>(node);
|
|
|
|
if (meshRenderer)
|
|
|
|
aabb.merge(meshRenderer->getAABB());
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
return aabb;
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
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);
|
2022-07-05 14:48:46 +08:00
|
|
|
if (meshdata)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-21 19:19:08 +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);
|
|
|
|
}
|
2022-07-05 14:48:46 +08:00
|
|
|
_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
|
|
|
|
2022-07-05 14:48:46 +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
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
createNode(it, this, *(meshdata->materialdatas), singleMesh);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-05 14:48:46 +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
|
|
|
{
|
2022-07-05 14:48:46 +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)
|
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
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)
|
2022-08-06 16:17:55 +08:00
|
|
|
, _wireframe(false)
|
2021-12-25 10:04:45 +08:00
|
|
|
, _usingAutogeneratedGLProgram(true)
|
2022-08-06 16:17:55 +08:00
|
|
|
, _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
|
|
|
|
2022-07-05 14:48:46 +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();
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
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
|
|
|
{
|
2022-07-05 14:48:46 +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;
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
MeshRenderer* MeshRenderer::createMeshRendererNode(NodeData* nodedata, ModelData* modeldata, const MaterialDatas& materialdatas)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
auto meshRenderer = new MeshRenderer();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-05 14:48:46 +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);
|
2022-08-06 16:17:55 +08:00
|
|
|
_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);
|
2022-07-05 14:48:46 +08:00
|
|
|
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
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
meshRenderer->addMesh(mesh);
|
|
|
|
meshRenderer->autorelease();
|
|
|
|
meshRenderer->genMaterial();
|
2021-12-08 00:11:53 +08:00
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
return meshRenderer;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2022-07-05 14:48:46 +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
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
auto mesh = createMeshRendererNode(nodedata, it, materialdatas);
|
|
|
|
if (mesh)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-05 14:48:46 +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
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
createAttachMeshRendererNode(it, materialdatas);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +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
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +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;
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
void MeshRenderer::genMaterial(bool useLight)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_shaderUsingLight = useLight;
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
std::unordered_map<const MeshVertexData*, MeshMaterial*> materials;
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& meshVertexData : _meshVertexDatas)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-05 14:48:46 +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
|
|
|
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& mesh : _meshes)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto material = materials[mesh->getMeshIndexData()->getMeshVertexData()];
|
2022-08-06 16:17:55 +08:00
|
|
|
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());
|
2022-10-08 06:15:03 +08:00
|
|
|
material->setTransparent(oldmaterial->isTransparent());
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (material->getReferenceCount() == 1)
|
|
|
|
mesh->setMaterial(material);
|
|
|
|
else
|
|
|
|
mesh->setMaterial(material->clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
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
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
if (!it->bones.empty() || singleMesh)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-05 14:48:46 +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);
|
|
|
|
}
|
2022-07-05 14:48:46 +08:00
|
|
|
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);
|
2022-08-06 16:17:55 +08:00
|
|
|
_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
|
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
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
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
root->addChild(mesh);
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2022-07-05 14:48:46 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
MeshIndexData* MeshRenderer::getMeshIndexData(std::string_view indexId) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-21 19:19:08 +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;
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-31 18:14:46 +08:00
|
|
|
void MeshRenderer::enableInstancing(MeshMaterial::InstanceMaterialType instanceMat, int count)
|
|
|
|
{
|
|
|
|
switch (instanceMat)
|
|
|
|
{
|
|
|
|
case MeshMaterial::InstanceMaterialType::UNLIT_INSTANCE:
|
|
|
|
{
|
|
|
|
auto mat = MeshMaterial::createBuiltInMaterial(MeshMaterial::MaterialType::UNLIT_INSTANCE, false);
|
|
|
|
enableInstancing(mat, count);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MeshRenderer::enableInstancing(MeshMaterial* instanceMat, int count)
|
|
|
|
{
|
|
|
|
for (auto&& mesh : _meshes)
|
|
|
|
{
|
|
|
|
mesh->enableInstancing(true, MAX(1, count));
|
|
|
|
mesh->setMaterial(instanceMat);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MeshRenderer::disableInstancing()
|
|
|
|
{
|
|
|
|
for (auto&& mesh : _meshes)
|
|
|
|
mesh->enableInstancing(false, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MeshRenderer::setDynamicInstancing(bool dynamic)
|
|
|
|
{
|
|
|
|
for (auto&& mesh : _meshes)
|
|
|
|
mesh->setDynamicInstancing(dynamic);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MeshRenderer::addInstanceChild(Node* child, bool active)
|
|
|
|
{
|
|
|
|
for (auto&& mesh : _meshes)
|
|
|
|
{
|
|
|
|
mesh->addInstanceChild(child);
|
|
|
|
if (active)
|
|
|
|
addChild(child);
|
|
|
|
else
|
|
|
|
child->setParent(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MeshRenderer::shrinkToFitInstances()
|
|
|
|
{
|
|
|
|
for (auto&& mesh : _meshes)
|
|
|
|
mesh->shrinkToFitInstances();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MeshRenderer::rebuildInstances()
|
|
|
|
{
|
|
|
|
for (auto&& mesh : _meshes)
|
|
|
|
mesh->rebuildInstances();
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
void MeshRenderer::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);
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
void MeshRenderer::setTexture(Texture2D* texture)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-21 19:19:08 +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);
|
|
|
|
}
|
|
|
|
}
|
2022-07-05 14:48:46 +08:00
|
|
|
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);
|
2021-12-31 12:12:40 +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;
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
void MeshRenderer::removeAllAttachNode()
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-21 19:19:08 +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
|
|
|
//
|
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
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +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
|
|
|
|
2022-07-21 19:19:08 +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),
|
2022-08-06 16:17:55 +08:00
|
|
|
_forceDepthWrite, _wireframe);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-31 18:14:46 +08:00
|
|
|
bool MeshRenderer::setProgramState(backend::ProgramState* programState, bool ownPS/* = false*/)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2023-08-31 18:14:46 +08:00
|
|
|
if (Node::setProgramState(programState, ownPS))
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& state : _meshes)
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +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;
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& mesh : _meshes)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
mesh->setBlendFunc(blendFunc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
const BlendFunc& MeshRenderer::getBlendFunc() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return _blend;
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
AABB MeshRenderer::getAABBRecursively()
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return getAABBRecursivelyImp(this);
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
Action* MeshRenderer::runAction(Action* action)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
setForceDepthWrite(true);
|
|
|
|
return Node::runAction(action);
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
void MeshRenderer::setCullFace(CullFaceSide side)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-21 19:19:08 +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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
void MeshRenderer::setCullFaceEnabled(bool enable)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-21 19:19:08 +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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
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 */
|
2022-07-05 14:48:46 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
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)
|
2022-08-08 13:18:33 +08:00
|
|
|
meshes.emplace_back(it);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
return meshes;
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
2022-07-05 14:48:46 +08:00
|
|
|
MeshRendererCache* MeshRendererCache::_cacheInstance = nullptr;
|
|
|
|
MeshRendererCache* MeshRendererCache::getInstance()
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (_cacheInstance == nullptr)
|
2022-07-05 14:48:46 +08:00
|
|
|
_cacheInstance = new MeshRendererCache();
|
2019-11-23 20:27:39 +08:00
|
|
|
return _cacheInstance;
|
|
|
|
}
|
2022-07-05 14:48:46 +08:00
|
|
|
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
|
|
|
{
|
2022-07-05 14:48:46 +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
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
auto it = _meshDatas.find(key);
|
|
|
|
if (it == _meshDatas.end())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-05 14:48:46 +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
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
auto it = _meshDatas.find(key);
|
|
|
|
if (it != _meshDatas.end())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
delete it->second;
|
2022-07-05 14:48:46 +08:00
|
|
|
_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
|
|
|
{
|
2022-07-21 19:19:08 +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;
|
|
|
|
}
|
2022-07-05 14:48:46 +08:00
|
|
|
_meshDatas.clear();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +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
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +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);
|
2022-07-05 14:48:46 +08:00
|
|
|
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)
|
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
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
|
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
type = hasNormal && usesLight ? MeshMaterial::MaterialType::DIFFUSE
|
|
|
|
: MeshMaterial::MaterialType::UNLIT;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
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
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
return MeshMaterial::createBuiltInMaterial(type, hasSkin);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_END
|