This commit is contained in:
halx99 2022-10-12 12:39:27 +08:00
parent 496962f762
commit ec72ebb697
4 changed files with 89 additions and 56 deletions

View File

@ -1,6 +1,7 @@
/****************************************************************************
Copyright (c) 2014-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
Copyright (c) 2022 Bytedance Inc.
https://axmolengine.github.io/
@ -64,27 +65,23 @@ MeshRenderer* MeshRenderer::create()
MeshRenderer* MeshRenderer::create(std::string_view modelPath)
{
AXASSERT(modelPath.length() >= 4, "Invalid filename.");
auto mesh = new MeshRenderer();
if (mesh->initWithFile(modelPath))
{
mesh->_contentSize = mesh->getBoundingBox().size;
mesh->autorelease();
return mesh;
}
AX_SAFE_DELETE(mesh);
return nullptr;
return create(modelPath, hlookup::empty_sv);
}
MeshRenderer* MeshRenderer::create(std::string_view modelPath, std::string_view texturePath)
{
auto mesh = create(modelPath);
if (mesh)
{
mesh->setTexture(texturePath);
}
AXASSERT(modelPath.length() >= 4, "Invalid filename.");
return mesh;
auto meshRenderer = new MeshRenderer();
if (meshRenderer->initWithFile(modelPath))
{
meshRenderer->setModelTexture(modelPath, texturePath);
meshRenderer->_contentSize = meshRenderer->getBoundingBox().size;
meshRenderer->autorelease();
return meshRenderer;
}
AX_SAFE_DELETE(meshRenderer);
return nullptr;
}
void MeshRenderer::createAsync(std::string_view modelPath,
@ -99,30 +96,29 @@ void MeshRenderer::createAsync(std::string_view modelPath,
const std::function<void(MeshRenderer*, void*)>& callback,
void* callbackparam)
{
MeshRenderer* mesh = new MeshRenderer();
if (mesh->loadFromCache(modelPath))
MeshRenderer* meshRenderer = new MeshRenderer();
if (meshRenderer->loadFromCache(modelPath))
{
mesh->autorelease();
if (!texturePath.empty())
mesh->setTexture(texturePath);
callback(mesh, callbackparam);
meshRenderer->autorelease();
meshRenderer->setModelTexture(modelPath, texturePath);
callback(meshRenderer, callbackparam);
return;
}
mesh->_asyncLoadParam.afterLoadCallback = callback;
mesh->_asyncLoadParam.texPath = texturePath;
mesh->_asyncLoadParam.modelPath = modelPath;
mesh->_asyncLoadParam.modelFullPath = FileUtils::getInstance()->fullPathForFilename(modelPath);
mesh->_asyncLoadParam.callbackParam = callbackparam;
mesh->_asyncLoadParam.materialdatas = new MaterialDatas();
mesh->_asyncLoadParam.meshdatas = new MeshDatas();
mesh->_asyncLoadParam.nodeDatas = new NodeDatas();
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();
AsyncTaskPool::getInstance()->enqueue(
AsyncTaskPool::TaskType::TASK_IO, AX_CALLBACK_1(MeshRenderer::afterAsyncLoad, mesh),
(void*)(&mesh->_asyncLoadParam), [mesh]() {
mesh->_asyncLoadParam.result =
mesh->loadFromFile(mesh->_asyncLoadParam.modelFullPath, mesh->_asyncLoadParam.nodeDatas,
mesh->_asyncLoadParam.meshdatas, mesh->_asyncLoadParam.materialdatas);
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);
});
}
@ -169,10 +165,7 @@ void MeshRenderer::afterAsyncLoad(void* param)
AX_SAFE_DELETE(materialdatas);
AX_SAFE_DELETE(nodeDatas);
if (asyncParam->texPath != "")
{
setTexture(asyncParam->texPath);
}
setModelTexture(asyncParam->modelPath, asyncParam->texPath);
}
else
{
@ -272,13 +265,14 @@ bool MeshRenderer::loadFromFile(std::string_view path,
MeshRenderer::MeshRenderer()
: _skeleton(nullptr)
, _blend(BlendFunc::ALPHA_NON_PREMULTIPLIED)
, _aabbDirty(true)
, _lightMask(-1)
, _aabbDirty(true)
, _shaderUsingLight(false)
, _forceDepthWrite(false)
, _wireframe(false)
, _usingAutogeneratedGLProgram(true)
, _transparentMaterialHint(false)
, _meshTextureHint(0)
{}
MeshRenderer::~MeshRenderer()
@ -381,16 +375,16 @@ MeshRenderer* MeshRenderer::createMeshRendererNode(NodeData* nodedata, ModelData
meshRenderer->setName(nodedata->id);
auto mesh = Mesh::create(nodedata->id, getMeshIndexData(modeldata->subMeshId));
if (_skeleton && modeldata->bones.size())
if (_skeleton && !modeldata->bones.empty())
{
auto skin = MeshSkin::create(_skeleton, modeldata->bones, modeldata->invBindPose);
mesh->setSkin(skin);
}
if (modeldata->materialId == "" && materialdatas.materials.size())
if (modeldata->materialId.empty() && !materialdatas.materials.empty())
{
const NTextureData* textureData = materialdatas.materials[0].getTextureData(NTextureData::Usage::Diffuse);
mesh->setTexture(textureData->filename);
setMeshTexture(mesh, textureData->filename);
}
else
{
@ -400,7 +394,7 @@ MeshRenderer* MeshRenderer::createMeshRendererNode(NodeData* nodedata, ModelData
const NTextureData* textureData = materialData->getTextureData(NTextureData::Usage::Diffuse);
if (textureData)
{
mesh->setTexture(textureData->filename);
setMeshTexture(mesh, textureData->filename);
auto tex = mesh->getTexture();
if (tex)
{
@ -416,7 +410,7 @@ MeshRenderer* MeshRenderer::createMeshRendererNode(NodeData* nodedata, ModelData
textureData = materialData->getTextureData(NTextureData::Usage::Normal);
if (textureData)
{
auto tex = _director->getTextureCache()->addImage(textureData->filename);
auto tex = setMeshTexture(mesh, textureData->filename, NTextureData::Usage::Normal);
if (tex)
{
Texture2D::TexParams texParams;
@ -426,7 +420,6 @@ MeshRenderer* MeshRenderer::createMeshRendererNode(NodeData* nodedata, ModelData
texParams.tAddressMode = textureData->wrapT;
tex->setTexParameters(texParams);
}
mesh->setTexture(tex, NTextureData::Usage::Normal);
}
}
}
@ -556,7 +549,7 @@ void MeshRenderer::createNode(NodeData* nodedata, Node* root, const MaterialData
{
const NTextureData* textureData =
materialdatas.materials[0].getTextureData(NTextureData::Usage::Diffuse);
mesh->setTexture(textureData->filename);
setMeshTexture(mesh, textureData->filename);
}
else
{
@ -567,7 +560,7 @@ void MeshRenderer::createNode(NodeData* nodedata, Node* root, const MaterialData
materialData->getTextureData(NTextureData::Usage::Diffuse);
if (textureData)
{
mesh->setTexture(textureData->filename);
setMeshTexture(mesh, textureData->filename);
auto tex = mesh->getTexture();
if (tex)
{
@ -583,7 +576,7 @@ void MeshRenderer::createNode(NodeData* nodedata, Node* root, const MaterialData
textureData = materialData->getTextureData(NTextureData::Usage::Normal);
if (textureData)
{
auto tex = _director->getTextureCache()->addImage(textureData->filename);
auto tex = setMeshTexture(mesh, textureData->filename, NTextureData::Usage::Normal);
if (tex)
{
Texture2D::TexParams texParams;
@ -593,7 +586,6 @@ void MeshRenderer::createNode(NodeData* nodedata, Node* root, const MaterialData
texParams.tAddressMode = textureData->wrapT;
tex->setTexParameters(texParams);
}
mesh->setTexture(tex, NTextureData::Usage::Normal);
}
}
}
@ -675,6 +667,31 @@ void MeshRenderer::addMesh(Mesh* mesh)
_meshes.pushBack(mesh);
}
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)
{
auto tex = _director->getTextureCache()->addImage(texFile);

View File

@ -1,6 +1,7 @@
/****************************************************************************
Copyright (c) 2014-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
Copyright (c) 2022 Bytedance Inc.
https://axmolengine.github.io/
@ -250,6 +251,16 @@ public:
static AABB getAABBRecursivelyImp(Node* node);
protected:
/** set specific mesh texture, for private use (create mesh stage) only */
Texture2D* setMeshTexture(Mesh* mesh,
std::string_view texPath,
NTextureData::Usage usage = NTextureData::Usage::Diffuse);
/** set model texture from model path when model file not contains texture and texPath is empty
* only for create mesh renderer
*/
void setModelTexture(std::string_view modelPath, std::string_view texPath);
Skeleton3D* _skeleton;
Vector<MeshVertexData*> _meshVertexDatas;
@ -262,13 +273,14 @@ protected:
mutable AABB _aabb; // cache current aabb
mutable Mat4 _nodeToWorldTransform; // cache current matrix
mutable bool _aabbDirty;
unsigned int _lightMask;
mutable bool _aabbDirty;
bool _shaderUsingLight; // Is the current shader using lighting?
bool _forceDepthWrite; // Always write to depth buffer
bool _wireframe; // render in wireframe mode
bool _usingAutogeneratedGLProgram;
bool _transparentMaterialHint; // Generate transparent materials when building from files
unsigned short _meshTextureHint; // Whether model file has texture config
struct AsyncLoadParam
{

View File

@ -1,6 +1,7 @@
/****************************************************************************
Copyright (c) 2015-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
Copyright (c) 2022 Bytedance Inc.
https://axmolengine.github.io/
@ -39,6 +40,7 @@ PhysicsMeshRenderer* PhysicsMeshRenderer::create(std::string_view modelPath,
auto ret = new PhysicsMeshRenderer();
if (ret->initWithFile(modelPath))
{
ret->setModelTexture(modelPath, hlookup::empty_sv);
auto obj = Physics3DRigidBody::create(rigidDes);
ret->_physicsComponent = Physics3DComponent::create(obj, translateInPhysics, rotInPhsyics);
ret->addComponent(ret->_physicsComponent);
@ -58,6 +60,7 @@ PhysicsMeshRenderer* PhysicsMeshRenderer::createWithCollider(std::string_view mo
auto ret = new PhysicsMeshRenderer();
if (ret->initWithFile(modelPath))
{
ret->setModelTexture(modelPath, hlookup::empty_sv);
auto obj = Physics3DCollider::create(colliderDes);
ret->_physicsComponent = Physics3DComponent::create(obj, translateInPhysics, rotInPhsyics);
ret->addComponent(ret->_physicsComponent);

View File

@ -2,6 +2,7 @@
Copyright (c) 2012 cocos2d-x.org
Copyright (c) 2015-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
Copyright (c) 2022 Bytedance Inc.
https://axmolengine.github.io/
@ -33,8 +34,8 @@
USING_NS_AX_EXT;
USING_NS_AX;
std::string boss[] = {"MeshRendererTest/boss.c3b",
"MeshRendererTest/boss.obj"};
static const std::string_view boss[] = {"MeshRendererTest/boss.c3b", "MeshRendererTest/boss.obj"};
static const int boss_count = sizeof(boss) / sizeof(boss[0]);
enum
{
@ -662,7 +663,7 @@ bool Physics3DTerrainDemo::init()
// create mesh
for (int i = 0; i < 2; i++)
for (int i = 0; i < boss_count; i++)
{
std::vector<Vec3> trianglesList = Bundle3D::getTrianglesList(boss[i]);
@ -733,7 +734,7 @@ bool Physics3DCollisionCallbackDemo::init()
return false;
{
for (size_t i = 0; i < 2; i++)
for (size_t i = 0; i < boss_count; i++)
{
Physics3DRigidBodyDes rbDes;