2014-06-16 20:58:13 +08:00
|
|
|
/****************************************************************************
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2014-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2014-06-16 20:58:13 +08:00
|
|
|
|
2022-08-08 18:02:17 +08:00
|
|
|
https://axys1.github.io/
|
2014-06-16 20:58:13 +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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
#include "3d/CCMeshMaterial.h"
|
2015-12-01 10:37:53 +08:00
|
|
|
#include "3d/CCMesh.h"
|
2015-09-07 13:59:38 +08:00
|
|
|
#include "platform/CCFileUtils.h"
|
2014-08-29 15:39:52 +08:00
|
|
|
#include "renderer/CCTexture2D.h"
|
2015-09-07 13:59:38 +08:00
|
|
|
#include "base/CCDirector.h"
|
|
|
|
#include "base/CCEventType.h"
|
2019-05-16 09:26:00 +08:00
|
|
|
#include "base/CCConfiguration.h"
|
2019-01-30 09:35:17 +08:00
|
|
|
#include "renderer/backend/ProgramState.h"
|
|
|
|
#include "renderer/ccShaders.h"
|
|
|
|
#include "renderer/CCPass.h"
|
2014-06-16 20:58:13 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2014-06-16 20:58:13 +08:00
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
MeshMaterialCache* MeshMaterialCache::_cacheInstance = nullptr;
|
|
|
|
|
|
|
|
std::unordered_map<std::string, MeshMaterial*> MeshMaterial::_materials;
|
|
|
|
MeshMaterial* MeshMaterial::_unLitMaterial = nullptr;
|
|
|
|
MeshMaterial* MeshMaterial::_unLitNoTexMaterial = nullptr;
|
|
|
|
MeshMaterial* MeshMaterial::_vertexLitMaterial = nullptr;
|
|
|
|
MeshMaterial* MeshMaterial::_diffuseMaterial = nullptr;
|
|
|
|
MeshMaterial* MeshMaterial::_diffuseNoTexMaterial = nullptr;
|
|
|
|
MeshMaterial* MeshMaterial::_bumpedDiffuseMaterial = nullptr;
|
|
|
|
|
|
|
|
MeshMaterial* MeshMaterial::_unLitMaterialSkin = nullptr;
|
|
|
|
MeshMaterial* MeshMaterial::_vertexLitMaterialSkin = nullptr;
|
|
|
|
MeshMaterial* MeshMaterial::_diffuseMaterialSkin = nullptr;
|
|
|
|
MeshMaterial* MeshMaterial::_bumpedDiffuseMaterialSkin = nullptr;
|
|
|
|
|
2022-07-05 15:42:38 +08:00
|
|
|
MeshMaterial* MeshMaterial::_quadTextureMaterial = nullptr;
|
|
|
|
MeshMaterial* MeshMaterial::_quadColorMaterial = nullptr;
|
2022-07-02 23:22:31 +08:00
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
backend::ProgramState* MeshMaterial::_unLitMaterialProgState = nullptr;
|
|
|
|
backend::ProgramState* MeshMaterial::_unLitNoTexMaterialProgState = nullptr;
|
|
|
|
backend::ProgramState* MeshMaterial::_vertexLitMaterialProgState = nullptr;
|
|
|
|
backend::ProgramState* MeshMaterial::_diffuseMaterialProgState = nullptr;
|
|
|
|
backend::ProgramState* MeshMaterial::_diffuseNoTexMaterialProgState = nullptr;
|
|
|
|
backend::ProgramState* MeshMaterial::_bumpedDiffuseMaterialProgState = nullptr;
|
|
|
|
|
|
|
|
backend::ProgramState* MeshMaterial::_unLitMaterialSkinProgState = nullptr;
|
|
|
|
backend::ProgramState* MeshMaterial::_vertexLitMaterialSkinProgState = nullptr;
|
|
|
|
backend::ProgramState* MeshMaterial::_diffuseMaterialSkinProgState = nullptr;
|
|
|
|
backend::ProgramState* MeshMaterial::_bumpedDiffuseMaterialSkinProgState = nullptr;
|
|
|
|
|
2022-07-05 15:42:38 +08:00
|
|
|
backend::ProgramState* MeshMaterial::_quadTextureMaterialProgState = nullptr;
|
|
|
|
backend::ProgramState* MeshMaterial::_quadColorMaterialProgState = nullptr;
|
2022-07-02 23:22:31 +08:00
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
void MeshMaterial::createBuiltInMaterial()
|
2015-09-07 13:59:38 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::SKINPOSITION_TEXTURE_3D);
|
2021-12-08 00:11:53 +08:00
|
|
|
_unLitMaterialSkinProgState = new backend::ProgramState(program);
|
2022-07-05 14:48:46 +08:00
|
|
|
_unLitMaterialSkin = new MeshMaterial();
|
2019-01-30 09:35:17 +08:00
|
|
|
if (_unLitMaterialSkin && _unLitMaterialSkin->initWithProgramState(_unLitMaterialSkinProgState))
|
2015-09-07 13:59:38 +08:00
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
_unLitMaterialSkin->_type = MeshMaterial::MaterialType::UNLIT;
|
2015-09-07 13:59:38 +08:00
|
|
|
}
|
2019-01-30 09:35:17 +08:00
|
|
|
|
2019-10-15 09:40:59 +08:00
|
|
|
program = backend::Program::getBuiltinProgram(backend::ProgramType::SKINPOSITION_NORMAL_TEXTURE_3D);
|
2021-12-08 00:11:53 +08:00
|
|
|
_diffuseMaterialSkinProgState = new backend::ProgramState(program);
|
2022-07-05 14:48:46 +08:00
|
|
|
_diffuseMaterialSkin = new MeshMaterial();
|
2019-01-30 09:35:17 +08:00
|
|
|
if (_diffuseMaterialSkin && _diffuseMaterialSkin->initWithProgramState(_diffuseMaterialSkinProgState))
|
2015-09-07 13:59:38 +08:00
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
_diffuseMaterialSkin->_type = MeshMaterial::MaterialType::DIFFUSE;
|
2015-09-07 13:59:38 +08:00
|
|
|
}
|
2019-01-30 09:35:17 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
program = backend::Program::getBuiltinProgram(backend::ProgramType::POSITION_NORMAL_TEXTURE_3D);
|
2021-12-08 00:11:53 +08:00
|
|
|
_diffuseMaterialProgState = new backend::ProgramState(program);
|
2022-07-05 14:48:46 +08:00
|
|
|
_diffuseMaterial = new MeshMaterial();
|
2019-01-30 09:35:17 +08:00
|
|
|
if (_diffuseMaterial && _diffuseMaterial->initWithProgramState(_diffuseMaterialProgState))
|
2015-09-07 13:59:38 +08:00
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
_diffuseMaterial->_type = MeshMaterial::MaterialType::DIFFUSE;
|
2015-09-07 13:59:38 +08:00
|
|
|
}
|
2019-01-30 09:35:17 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
program = backend::Program::getBuiltinProgram(backend::ProgramType::POSITION_TEXTURE_3D);
|
2021-12-08 00:11:53 +08:00
|
|
|
_unLitMaterialProgState = new backend::ProgramState(program);
|
2022-07-05 14:48:46 +08:00
|
|
|
_unLitMaterial = new MeshMaterial();
|
2019-01-30 09:35:17 +08:00
|
|
|
if (_unLitMaterial && _unLitMaterial->initWithProgramState(_unLitMaterialProgState))
|
2015-09-07 13:59:38 +08:00
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
_unLitMaterial->_type = MeshMaterial::MaterialType::UNLIT;
|
2015-09-07 13:59:38 +08:00
|
|
|
}
|
2019-01-30 09:35:17 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
program = backend::Program::getBuiltinProgram(backend::ProgramType::POSITION_3D);
|
2021-12-08 00:11:53 +08:00
|
|
|
_unLitNoTexMaterialProgState = new backend::ProgramState(program);
|
2022-07-05 14:48:46 +08:00
|
|
|
_unLitNoTexMaterial = new MeshMaterial();
|
2019-01-30 09:35:17 +08:00
|
|
|
if (_unLitNoTexMaterial && _unLitNoTexMaterial->initWithProgramState(_unLitNoTexMaterialProgState))
|
2015-09-07 13:59:38 +08:00
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
_unLitNoTexMaterial->_type = MeshMaterial::MaterialType::UNLIT_NOTEX;
|
2015-09-07 13:59:38 +08:00
|
|
|
}
|
2019-01-30 09:35:17 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
program = backend::Program::getBuiltinProgram(backend::ProgramType::POSITION_NORMAL_3D);
|
2021-12-08 00:11:53 +08:00
|
|
|
_diffuseNoTexMaterialProgState = new backend::ProgramState(program);
|
2022-07-05 14:48:46 +08:00
|
|
|
_diffuseNoTexMaterial = new MeshMaterial();
|
2019-01-30 09:35:17 +08:00
|
|
|
if (_diffuseNoTexMaterial && _diffuseNoTexMaterial->initWithProgramState(_diffuseNoTexMaterialProgState))
|
2015-09-07 13:59:38 +08:00
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
_diffuseNoTexMaterial->_type = MeshMaterial::MaterialType::DIFFUSE_NOTEX;
|
2015-09-07 13:59:38 +08:00
|
|
|
}
|
2015-11-25 10:45:03 +08:00
|
|
|
|
2019-10-15 09:40:59 +08:00
|
|
|
program = backend::Program::getBuiltinProgram(backend::ProgramType::POSITION_BUMPEDNORMAL_TEXTURE_3D);
|
2021-12-08 00:11:53 +08:00
|
|
|
_bumpedDiffuseMaterialProgState = new backend::ProgramState(program);
|
2022-07-05 14:48:46 +08:00
|
|
|
_bumpedDiffuseMaterial = new MeshMaterial();
|
2019-01-30 09:35:17 +08:00
|
|
|
if (_bumpedDiffuseMaterial && _bumpedDiffuseMaterial->initWithProgramState(_bumpedDiffuseMaterialProgState))
|
2015-11-25 10:45:03 +08:00
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
_bumpedDiffuseMaterial->_type = MeshMaterial::MaterialType::BUMPED_DIFFUSE;
|
2015-11-25 10:45:03 +08:00
|
|
|
}
|
|
|
|
|
2019-10-15 09:40:59 +08:00
|
|
|
program = backend::Program::getBuiltinProgram(backend::ProgramType::SKINPOSITION_BUMPEDNORMAL_TEXTURE_3D);
|
2021-12-08 00:11:53 +08:00
|
|
|
_bumpedDiffuseMaterialSkinProgState = new backend::ProgramState(program);
|
2022-07-05 14:48:46 +08:00
|
|
|
_bumpedDiffuseMaterialSkin = new MeshMaterial();
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_bumpedDiffuseMaterialSkin &&
|
|
|
|
_bumpedDiffuseMaterialSkin->initWithProgramState(_bumpedDiffuseMaterialSkinProgState))
|
2015-11-25 10:45:03 +08:00
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
_bumpedDiffuseMaterialSkin->_type = MeshMaterial::MaterialType::BUMPED_DIFFUSE;
|
2015-11-25 10:45:03 +08:00
|
|
|
}
|
2022-07-02 23:22:31 +08:00
|
|
|
|
2022-07-03 00:46:41 +08:00
|
|
|
program = backend::Program::getBuiltinProgram(backend::ProgramType::QUAD_TEXTURE_2D);
|
|
|
|
_quadTextureMaterialProgState = new backend::ProgramState(program);
|
2022-07-05 15:42:38 +08:00
|
|
|
_quadTextureMaterial = new MeshMaterial();
|
2022-07-03 00:46:41 +08:00
|
|
|
if (_quadTextureMaterial && _quadTextureMaterial->initWithProgramState(_quadTextureMaterialProgState))
|
2022-07-02 23:22:31 +08:00
|
|
|
{
|
2022-07-05 15:42:38 +08:00
|
|
|
_quadTextureMaterial->_type = MeshMaterial::MaterialType::QUAD_TEXTURE;
|
2022-07-03 00:46:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
program = backend::Program::getBuiltinProgram(backend::ProgramType::QUAD_COLOR_2D);
|
|
|
|
_quadColorMaterialProgState = new backend::ProgramState(program);
|
2022-07-05 15:42:38 +08:00
|
|
|
_quadColorMaterial = new MeshMaterial();
|
2022-07-03 00:46:41 +08:00
|
|
|
if (_quadColorMaterial && _quadColorMaterial->initWithProgramState(_quadColorMaterialProgState))
|
|
|
|
{
|
2022-07-05 15:42:38 +08:00
|
|
|
_quadColorMaterial->_type = MeshMaterial::MaterialType::QUAD_COLOR;
|
2022-07-02 23:22:31 +08:00
|
|
|
}
|
2015-09-07 13:59:38 +08:00
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
void MeshMaterial::releaseBuiltInMaterial()
|
2015-09-07 13:59:38 +08:00
|
|
|
{
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_RELEASE_NULL(_unLitMaterial);
|
|
|
|
AX_SAFE_RELEASE_NULL(_unLitMaterialSkin);
|
|
|
|
|
|
|
|
AX_SAFE_RELEASE_NULL(_unLitNoTexMaterial);
|
|
|
|
AX_SAFE_RELEASE_NULL(_vertexLitMaterial);
|
|
|
|
AX_SAFE_RELEASE_NULL(_diffuseMaterial);
|
|
|
|
AX_SAFE_RELEASE_NULL(_diffuseNoTexMaterial);
|
|
|
|
AX_SAFE_RELEASE_NULL(_bumpedDiffuseMaterial);
|
|
|
|
|
|
|
|
AX_SAFE_RELEASE_NULL(_vertexLitMaterialSkin);
|
|
|
|
AX_SAFE_RELEASE_NULL(_diffuseMaterialSkin);
|
|
|
|
AX_SAFE_RELEASE_NULL(_bumpedDiffuseMaterialSkin);
|
2021-12-25 10:04:45 +08:00
|
|
|
// release program states
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_RELEASE_NULL(_unLitMaterialProgState);
|
|
|
|
AX_SAFE_RELEASE_NULL(_unLitNoTexMaterialProgState);
|
|
|
|
AX_SAFE_RELEASE_NULL(_vertexLitMaterialProgState);
|
|
|
|
AX_SAFE_RELEASE_NULL(_diffuseMaterialProgState);
|
|
|
|
AX_SAFE_RELEASE_NULL(_diffuseNoTexMaterialProgState);
|
|
|
|
AX_SAFE_RELEASE_NULL(_bumpedDiffuseMaterialProgState);
|
|
|
|
|
|
|
|
AX_SAFE_RELEASE_NULL(_unLitMaterialSkinProgState);
|
|
|
|
AX_SAFE_RELEASE_NULL(_vertexLitMaterialSkinProgState);
|
|
|
|
AX_SAFE_RELEASE_NULL(_diffuseMaterialSkinProgState);
|
|
|
|
AX_SAFE_RELEASE_NULL(_bumpedDiffuseMaterialSkinProgState);
|
2015-09-07 13:59:38 +08:00
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
void MeshMaterial::releaseCachedMaterial()
|
2015-09-08 15:29:54 +08:00
|
|
|
{
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& it : _materials)
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
2015-09-08 15:29:54 +08:00
|
|
|
if (it.second)
|
|
|
|
it.second->release();
|
|
|
|
}
|
|
|
|
_materials.clear();
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
Material* MeshMaterial::clone() const
|
2015-09-07 13:59:38 +08:00
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
auto material = new MeshMaterial();
|
2021-12-08 00:11:53 +08:00
|
|
|
|
|
|
|
material->_renderState = _renderState;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
for (const auto& technique : _techniques)
|
2015-09-07 13:59:38 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
auto t = technique->clone();
|
|
|
|
t->setMaterial(material);
|
2021-12-25 10:04:45 +08:00
|
|
|
for (ssize_t i = 0; i < t->getPassCount(); i++)
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
t->getPassByIndex(i)->setTechnique(t);
|
2015-09-07 13:59:38 +08:00
|
|
|
}
|
2021-12-08 00:11:53 +08:00
|
|
|
material->_techniques.pushBack(t);
|
2015-09-07 13:59:38 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2021-12-08 00:11:53 +08:00
|
|
|
// current technique
|
2021-12-25 10:04:45 +08:00
|
|
|
auto name = _currentTechnique->getName();
|
2021-12-08 00:11:53 +08:00
|
|
|
material->_currentTechnique = material->getTechniqueByName(name);
|
2021-12-25 10:04:45 +08:00
|
|
|
material->_type = _type;
|
2021-12-08 00:11:53 +08:00
|
|
|
material->autorelease();
|
|
|
|
|
2015-09-07 13:59:38 +08:00
|
|
|
return material;
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
MeshMaterial* MeshMaterial::createBuiltInMaterial(MaterialType type, bool skinned)
|
2015-09-07 13:59:38 +08:00
|
|
|
{
|
|
|
|
/////
|
|
|
|
if (_diffuseMaterial == nullptr)
|
2015-09-08 15:29:54 +08:00
|
|
|
createBuiltInMaterial();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
MeshMaterial* material = nullptr;
|
2021-12-25 10:04:45 +08:00
|
|
|
switch (type)
|
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
case MeshMaterial::MaterialType::UNLIT:
|
2021-12-25 10:04:45 +08:00
|
|
|
material = skinned ? _unLitMaterialSkin : _unLitMaterial;
|
|
|
|
break;
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
case MeshMaterial::MaterialType::UNLIT_NOTEX:
|
2021-12-25 10:04:45 +08:00
|
|
|
material = _unLitNoTexMaterial;
|
|
|
|
break;
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
case MeshMaterial::MaterialType::VERTEX_LIT:
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(0, "not implemented");
|
2021-12-25 10:04:45 +08:00
|
|
|
break;
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
case MeshMaterial::MaterialType::DIFFUSE:
|
2021-12-25 10:04:45 +08:00
|
|
|
material = skinned ? _diffuseMaterialSkin : _diffuseMaterial;
|
|
|
|
break;
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
case MeshMaterial::MaterialType::DIFFUSE_NOTEX:
|
2021-12-25 10:04:45 +08:00
|
|
|
material = _diffuseNoTexMaterial;
|
|
|
|
break;
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
case MeshMaterial::MaterialType::BUMPED_DIFFUSE:
|
2021-12-25 10:04:45 +08:00
|
|
|
material = skinned ? _bumpedDiffuseMaterialSkin : _bumpedDiffuseMaterial;
|
|
|
|
break;
|
|
|
|
|
2022-07-05 15:42:38 +08:00
|
|
|
case MeshMaterial::MaterialType::QUAD_TEXTURE:
|
2022-07-03 00:46:41 +08:00
|
|
|
material = _quadTextureMaterial;
|
|
|
|
break;
|
|
|
|
|
2022-07-05 15:42:38 +08:00
|
|
|
case MeshMaterial::MaterialType::QUAD_COLOR:
|
2022-07-03 00:46:41 +08:00
|
|
|
material = _quadColorMaterial;
|
2022-07-02 23:22:31 +08:00
|
|
|
break;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
default:
|
|
|
|
break;
|
2015-09-07 13:59:38 +08:00
|
|
|
}
|
|
|
|
if (material)
|
2022-07-05 14:48:46 +08:00
|
|
|
return (MeshMaterial*)material->clone();
|
2019-01-30 09:35:17 +08:00
|
|
|
|
2015-09-07 13:59:38 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
MeshMaterial* MeshMaterial::createWithFilename(std::string_view path)
|
2015-09-07 13:59:38 +08:00
|
|
|
{
|
|
|
|
auto validfilename = FileUtils::getInstance()->fullPathForFilename(path);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!validfilename.empty())
|
|
|
|
{
|
2015-09-08 15:29:54 +08:00
|
|
|
auto it = _materials.find(validfilename);
|
|
|
|
if (it != _materials.end())
|
2022-07-05 14:48:46 +08:00
|
|
|
return (MeshMaterial*)it->second->clone();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
auto material = new MeshMaterial();
|
2015-09-07 13:59:38 +08:00
|
|
|
if (material->initWithFile(path))
|
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
material->_type = MeshMaterial::MaterialType::CUSTOM;
|
2015-09-08 15:29:54 +08:00
|
|
|
_materials[validfilename] = material;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
return (MeshMaterial*)material->clone();
|
2015-09-07 13:59:38 +08:00
|
|
|
}
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_DELETE(material);
|
2015-09-07 13:59:38 +08:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
MeshMaterial* MeshMaterial::createWithProgramState(backend::ProgramState* programState)
|
2015-09-07 13:59:38 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(programState, "Invalid program state.");
|
2019-01-30 09:35:17 +08:00
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
auto mat = new MeshMaterial();
|
2021-12-08 00:11:53 +08:00
|
|
|
if (mat->initWithProgramState(programState))
|
2015-09-07 13:59:38 +08:00
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
mat->_type = MeshMaterial::MaterialType::CUSTOM;
|
2015-09-07 13:59:38 +08:00
|
|
|
mat->autorelease();
|
|
|
|
return mat;
|
|
|
|
}
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_DELETE(mat);
|
2015-09-07 13:59:38 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
void MeshMaterial::setTexture(Texture2D* tex, NTextureData::Usage usage)
|
2015-12-01 10:37:53 +08:00
|
|
|
{
|
|
|
|
const auto& passes = getTechnique()->getPasses();
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& pass : passes)
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
2019-02-15 14:30:10 +08:00
|
|
|
pass->setUniformTexture(0, tex->getBackendTexture());
|
2015-12-01 10:37:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-07 13:59:38 +08:00
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
MeshMaterialCache::MeshMaterialCache() {}
|
2014-06-16 20:58:13 +08:00
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
MeshMaterialCache::~MeshMaterialCache()
|
2014-06-16 20:58:13 +08:00
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
removeAllMeshMaterial();
|
2014-06-16 20:58:13 +08:00
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
MeshMaterialCache* MeshMaterialCache::getInstance()
|
2014-06-16 20:58:13 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!_cacheInstance)
|
2014-06-16 20:58:13 +08:00
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
_cacheInstance = new MeshMaterialCache();
|
2014-06-16 20:58:13 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-06-16 20:58:13 +08:00
|
|
|
return _cacheInstance;
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
void MeshMaterialCache::destroyInstance()
|
2014-06-16 20:58:13 +08:00
|
|
|
{
|
|
|
|
if (_cacheInstance)
|
|
|
|
{
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_DELETE(_cacheInstance);
|
2014-06-16 20:58:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
bool MeshMaterialCache::addMeshMaterial(std::string_view key, Texture2D* texture)
|
2014-06-16 20:58:13 +08:00
|
|
|
{
|
|
|
|
auto itr = _materials.find(key);
|
|
|
|
if (itr == _materials.end())
|
|
|
|
{
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_RETAIN(texture);
|
2021-12-26 23:26:34 +08:00
|
|
|
_materials.emplace(key, texture);
|
2014-06-16 20:58:13 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
Texture2D* MeshMaterialCache::getMeshMaterial(std::string_view key)
|
2014-06-16 20:58:13 +08:00
|
|
|
{
|
|
|
|
auto itr = _materials.find(key);
|
|
|
|
if (itr != _materials.end())
|
|
|
|
{
|
|
|
|
return itr->second;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
void MeshMaterialCache::removeAllMeshMaterial()
|
2014-06-16 20:58:13 +08:00
|
|
|
{
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& itr : _materials)
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_RELEASE_NULL(itr.second);
|
2014-06-16 20:58:13 +08:00
|
|
|
}
|
|
|
|
_materials.clear();
|
|
|
|
}
|
2022-07-05 14:48:46 +08:00
|
|
|
void MeshMaterialCache::removeUnusedMeshMaterial()
|
2014-06-16 20:58:13 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
for (auto it = _materials.cbegin(), itCend = _materials.cend(); it != itCend; /* nothing */)
|
|
|
|
{
|
2014-06-16 20:58:13 +08:00
|
|
|
auto value = it->second;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (value->getReferenceCount() == 1)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("cocos2d: MeshMaterialCache: removing unused mesh renderer materials.");
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-06-16 20:58:13 +08:00
|
|
|
value->release();
|
2016-04-02 08:19:11 +08:00
|
|
|
it = _materials.erase(it);
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-06-16 20:58:13 +08:00
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_END
|