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
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "3d/CCSprite3DMaterial.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
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
Sprite3DMaterialCache* Sprite3DMaterialCache::_cacheInstance = nullptr;
|
|
|
|
|
2015-09-08 15:29:54 +08:00
|
|
|
std::unordered_map<std::string, Sprite3DMaterial*> Sprite3DMaterial::_materials;
|
2021-12-25 10:04:45 +08:00
|
|
|
Sprite3DMaterial* Sprite3DMaterial::_unLitMaterial = nullptr;
|
|
|
|
Sprite3DMaterial* Sprite3DMaterial::_unLitNoTexMaterial = nullptr;
|
|
|
|
Sprite3DMaterial* Sprite3DMaterial::_vertexLitMaterial = nullptr;
|
|
|
|
Sprite3DMaterial* Sprite3DMaterial::_diffuseMaterial = nullptr;
|
|
|
|
Sprite3DMaterial* Sprite3DMaterial::_diffuseNoTexMaterial = nullptr;
|
2015-09-07 13:59:38 +08:00
|
|
|
Sprite3DMaterial* Sprite3DMaterial::_bumpedDiffuseMaterial = nullptr;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Sprite3DMaterial* Sprite3DMaterial::_unLitMaterialSkin = nullptr;
|
|
|
|
Sprite3DMaterial* Sprite3DMaterial::_vertexLitMaterialSkin = nullptr;
|
|
|
|
Sprite3DMaterial* Sprite3DMaterial::_diffuseMaterialSkin = nullptr;
|
2015-09-07 13:59:38 +08:00
|
|
|
Sprite3DMaterial* Sprite3DMaterial::_bumpedDiffuseMaterialSkin = nullptr;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
backend::ProgramState* Sprite3DMaterial::_unLitMaterialProgState = nullptr;
|
|
|
|
backend::ProgramState* Sprite3DMaterial::_unLitNoTexMaterialProgState = nullptr;
|
|
|
|
backend::ProgramState* Sprite3DMaterial::_vertexLitMaterialProgState = nullptr;
|
|
|
|
backend::ProgramState* Sprite3DMaterial::_diffuseMaterialProgState = nullptr;
|
|
|
|
backend::ProgramState* Sprite3DMaterial::_diffuseNoTexMaterialProgState = nullptr;
|
2019-01-30 09:35:17 +08:00
|
|
|
backend::ProgramState* Sprite3DMaterial::_bumpedDiffuseMaterialProgState = nullptr;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
backend::ProgramState* Sprite3DMaterial::_unLitMaterialSkinProgState = nullptr;
|
|
|
|
backend::ProgramState* Sprite3DMaterial::_vertexLitMaterialSkinProgState = nullptr;
|
|
|
|
backend::ProgramState* Sprite3DMaterial::_diffuseMaterialSkinProgState = nullptr;
|
2019-01-30 09:35:17 +08:00
|
|
|
backend::ProgramState* Sprite3DMaterial::_bumpedDiffuseMaterialSkinProgState = nullptr;
|
|
|
|
|
2015-09-08 15:29:54 +08:00
|
|
|
void Sprite3DMaterial::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);
|
2021-12-25 10:04:45 +08:00
|
|
|
_unLitMaterialSkin = new Sprite3DMaterial();
|
2019-01-30 09:35:17 +08:00
|
|
|
if (_unLitMaterialSkin && _unLitMaterialSkin->initWithProgramState(_unLitMaterialSkinProgState))
|
2015-09-07 13:59:38 +08:00
|
|
|
{
|
|
|
|
_unLitMaterialSkin->_type = Sprite3DMaterial::MaterialType::UNLIT;
|
|
|
|
}
|
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);
|
2021-12-25 10:04:45 +08:00
|
|
|
_diffuseMaterialSkin = new Sprite3DMaterial();
|
2019-01-30 09:35:17 +08:00
|
|
|
if (_diffuseMaterialSkin && _diffuseMaterialSkin->initWithProgramState(_diffuseMaterialSkinProgState))
|
2015-09-07 13:59:38 +08:00
|
|
|
{
|
|
|
|
_diffuseMaterialSkin->_type = Sprite3DMaterial::MaterialType::DIFFUSE;
|
|
|
|
}
|
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);
|
2021-12-25 10:04:45 +08:00
|
|
|
_diffuseMaterial = new Sprite3DMaterial();
|
2019-01-30 09:35:17 +08:00
|
|
|
if (_diffuseMaterial && _diffuseMaterial->initWithProgramState(_diffuseMaterialProgState))
|
2015-09-07 13:59:38 +08:00
|
|
|
{
|
|
|
|
_diffuseMaterial->_type = Sprite3DMaterial::MaterialType::DIFFUSE;
|
|
|
|
}
|
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);
|
2021-12-25 10:04:45 +08:00
|
|
|
_unLitMaterial = new Sprite3DMaterial();
|
2019-01-30 09:35:17 +08:00
|
|
|
if (_unLitMaterial && _unLitMaterial->initWithProgramState(_unLitMaterialProgState))
|
2015-09-07 13:59:38 +08:00
|
|
|
{
|
|
|
|
_unLitMaterial->_type = Sprite3DMaterial::MaterialType::UNLIT;
|
|
|
|
}
|
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);
|
2021-12-25 10:04:45 +08:00
|
|
|
_unLitNoTexMaterial = new Sprite3DMaterial();
|
2019-01-30 09:35:17 +08:00
|
|
|
if (_unLitNoTexMaterial && _unLitNoTexMaterial->initWithProgramState(_unLitNoTexMaterialProgState))
|
2015-09-07 13:59:38 +08:00
|
|
|
{
|
|
|
|
_unLitNoTexMaterial->_type = Sprite3DMaterial::MaterialType::UNLIT_NOTEX;
|
|
|
|
}
|
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);
|
2021-12-25 10:04:45 +08:00
|
|
|
_diffuseNoTexMaterial = new Sprite3DMaterial();
|
2019-01-30 09:35:17 +08:00
|
|
|
if (_diffuseNoTexMaterial && _diffuseNoTexMaterial->initWithProgramState(_diffuseNoTexMaterialProgState))
|
2015-09-07 13:59:38 +08:00
|
|
|
{
|
|
|
|
_diffuseNoTexMaterial->_type = Sprite3DMaterial::MaterialType::DIFFUSE_NOTEX;
|
|
|
|
}
|
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);
|
2021-12-25 10:04:45 +08:00
|
|
|
_bumpedDiffuseMaterial = new Sprite3DMaterial();
|
2019-01-30 09:35:17 +08:00
|
|
|
if (_bumpedDiffuseMaterial && _bumpedDiffuseMaterial->initWithProgramState(_bumpedDiffuseMaterialProgState))
|
2015-11-25 10:45:03 +08:00
|
|
|
{
|
|
|
|
_bumpedDiffuseMaterial->_type = Sprite3DMaterial::MaterialType::BUMPED_DIFFUSE;
|
|
|
|
}
|
|
|
|
|
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);
|
2021-12-25 10:04:45 +08:00
|
|
|
_bumpedDiffuseMaterialSkin = new Sprite3DMaterial();
|
|
|
|
if (_bumpedDiffuseMaterialSkin &&
|
|
|
|
_bumpedDiffuseMaterialSkin->initWithProgramState(_bumpedDiffuseMaterialSkinProgState))
|
2015-11-25 10:45:03 +08:00
|
|
|
{
|
|
|
|
_bumpedDiffuseMaterialSkin->_type = Sprite3DMaterial::MaterialType::BUMPED_DIFFUSE;
|
|
|
|
}
|
2015-09-07 13:59:38 +08:00
|
|
|
}
|
|
|
|
|
2015-09-08 15:29:54 +08:00
|
|
|
void Sprite3DMaterial::releaseBuiltInMaterial()
|
2015-09-07 13:59:38 +08:00
|
|
|
{
|
|
|
|
CC_SAFE_RELEASE_NULL(_unLitMaterial);
|
|
|
|
CC_SAFE_RELEASE_NULL(_unLitMaterialSkin);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-09-07 13:59:38 +08:00
|
|
|
CC_SAFE_RELEASE_NULL(_unLitNoTexMaterial);
|
|
|
|
CC_SAFE_RELEASE_NULL(_vertexLitMaterial);
|
|
|
|
CC_SAFE_RELEASE_NULL(_diffuseMaterial);
|
|
|
|
CC_SAFE_RELEASE_NULL(_diffuseNoTexMaterial);
|
|
|
|
CC_SAFE_RELEASE_NULL(_bumpedDiffuseMaterial);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-09-07 13:59:38 +08:00
|
|
|
CC_SAFE_RELEASE_NULL(_vertexLitMaterialSkin);
|
|
|
|
CC_SAFE_RELEASE_NULL(_diffuseMaterialSkin);
|
|
|
|
CC_SAFE_RELEASE_NULL(_bumpedDiffuseMaterialSkin);
|
2021-12-25 10:04:45 +08:00
|
|
|
// release program states
|
2019-01-30 09:35:17 +08:00
|
|
|
CC_SAFE_RELEASE_NULL(_unLitMaterialProgState);
|
|
|
|
CC_SAFE_RELEASE_NULL(_unLitNoTexMaterialProgState);
|
|
|
|
CC_SAFE_RELEASE_NULL(_vertexLitMaterialProgState);
|
|
|
|
CC_SAFE_RELEASE_NULL(_diffuseMaterialProgState);
|
|
|
|
CC_SAFE_RELEASE_NULL(_diffuseNoTexMaterialProgState);
|
|
|
|
CC_SAFE_RELEASE_NULL(_bumpedDiffuseMaterialProgState);
|
|
|
|
|
|
|
|
CC_SAFE_RELEASE_NULL(_unLitMaterialSkinProgState);
|
|
|
|
CC_SAFE_RELEASE_NULL(_vertexLitMaterialSkinProgState);
|
|
|
|
CC_SAFE_RELEASE_NULL(_diffuseMaterialSkinProgState);
|
|
|
|
CC_SAFE_RELEASE_NULL(_bumpedDiffuseMaterialSkinProgState);
|
2015-09-07 13:59:38 +08:00
|
|
|
}
|
|
|
|
|
2015-09-08 15:29:54 +08:00
|
|
|
void Sprite3DMaterial::releaseCachedMaterial()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
for (auto& it : _materials)
|
|
|
|
{
|
2015-09-08 15:29:54 +08:00
|
|
|
if (it.second)
|
|
|
|
it.second->release();
|
|
|
|
}
|
|
|
|
_materials.clear();
|
|
|
|
}
|
|
|
|
|
2015-09-07 13:59:38 +08:00
|
|
|
Material* Sprite3DMaterial::clone() const
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
auto material = new Sprite3DMaterial();
|
|
|
|
|
|
|
|
// RenderState::cloneInto(material);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
Sprite3DMaterial* Sprite3DMaterial::createBuiltInMaterial(MaterialType type, bool skinned)
|
|
|
|
{
|
|
|
|
/////
|
|
|
|
if (_diffuseMaterial == nullptr)
|
2015-09-08 15:29:54 +08:00
|
|
|
createBuiltInMaterial();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-09-07 13:59:38 +08:00
|
|
|
Sprite3DMaterial* material = nullptr;
|
2021-12-25 10:04:45 +08:00
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case Sprite3DMaterial::MaterialType::UNLIT:
|
|
|
|
material = skinned ? _unLitMaterialSkin : _unLitMaterial;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Sprite3DMaterial::MaterialType::UNLIT_NOTEX:
|
|
|
|
material = _unLitNoTexMaterial;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Sprite3DMaterial::MaterialType::VERTEX_LIT:
|
|
|
|
CCASSERT(0, "not implement");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Sprite3DMaterial::MaterialType::DIFFUSE:
|
|
|
|
material = skinned ? _diffuseMaterialSkin : _diffuseMaterial;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Sprite3DMaterial::MaterialType::DIFFUSE_NOTEX:
|
|
|
|
material = _diffuseNoTexMaterial;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Sprite3DMaterial::MaterialType::BUMPED_DIFFUSE:
|
|
|
|
material = skinned ? _bumpedDiffuseMaterialSkin : _bumpedDiffuseMaterial;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2015-09-07 13:59:38 +08:00
|
|
|
}
|
|
|
|
if (material)
|
|
|
|
return (Sprite3DMaterial*)material->clone();
|
2019-01-30 09:35:17 +08:00
|
|
|
|
2015-09-07 13:59:38 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
Sprite3DMaterial* Sprite3DMaterial::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())
|
|
|
|
return (Sprite3DMaterial*)it->second->clone();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2021-12-08 00:11:53 +08:00
|
|
|
auto material = new Sprite3DMaterial();
|
2015-09-07 13:59:38 +08:00
|
|
|
if (material->initWithFile(path))
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
material->_type = Sprite3DMaterial::MaterialType::CUSTOM;
|
2015-09-08 15:29:54 +08:00
|
|
|
_materials[validfilename] = material;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-09-08 15:29:54 +08:00
|
|
|
return (Sprite3DMaterial*)material->clone();
|
2015-09-07 13:59:38 +08:00
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(material);
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-01-30 09:35:17 +08:00
|
|
|
Sprite3DMaterial* Sprite3DMaterial::createWithProgramState(backend::ProgramState* programState)
|
2015-09-07 13:59:38 +08:00
|
|
|
{
|
|
|
|
CCASSERT(programState, "Invalid GL Program State");
|
2019-01-30 09:35:17 +08:00
|
|
|
|
2021-12-08 00:11:53 +08:00
|
|
|
auto mat = new Sprite3DMaterial();
|
|
|
|
if (mat->initWithProgramState(programState))
|
2015-09-07 13:59:38 +08:00
|
|
|
{
|
|
|
|
mat->_type = Sprite3DMaterial::MaterialType::CUSTOM;
|
|
|
|
mat->autorelease();
|
|
|
|
return mat;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(mat);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-12-01 10:37:53 +08:00
|
|
|
void Sprite3DMaterial::setTexture(Texture2D* tex, NTextureData::Usage usage)
|
|
|
|
{
|
|
|
|
const auto& passes = getTechnique()->getPasses();
|
2021-12-25 10:04:45 +08:00
|
|
|
for (auto& pass : passes)
|
|
|
|
{
|
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
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Sprite3DMaterialCache::Sprite3DMaterialCache() {}
|
2014-06-16 20:58:13 +08:00
|
|
|
|
|
|
|
Sprite3DMaterialCache::~Sprite3DMaterialCache()
|
|
|
|
{
|
|
|
|
removeAllSprite3DMaterial();
|
|
|
|
}
|
|
|
|
|
|
|
|
Sprite3DMaterialCache* Sprite3DMaterialCache::getInstance()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!_cacheInstance)
|
2014-06-16 20:58:13 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
_cacheInstance = new Sprite3DMaterialCache();
|
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;
|
|
|
|
}
|
|
|
|
|
2014-06-17 19:18:56 +08:00
|
|
|
void Sprite3DMaterialCache::destroyInstance()
|
2014-06-16 20:58:13 +08:00
|
|
|
{
|
|
|
|
if (_cacheInstance)
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(_cacheInstance);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
bool Sprite3DMaterialCache::addSprite3DMaterial(std::string_view key, Texture2D* texture)
|
2014-06-16 20:58:13 +08:00
|
|
|
{
|
|
|
|
auto itr = _materials.find(key);
|
|
|
|
if (itr == _materials.end())
|
|
|
|
{
|
|
|
|
CC_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;
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
Texture2D* Sprite3DMaterialCache::getSprite3DMaterial(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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sprite3DMaterialCache::removeAllSprite3DMaterial()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
for (auto& itr : _materials)
|
|
|
|
{
|
2021-12-27 19:18:36 +08:00
|
|
|
CC_SAFE_RELEASE_NULL(itr.second);
|
2014-06-16 20:58:13 +08:00
|
|
|
}
|
|
|
|
_materials.clear();
|
|
|
|
}
|
|
|
|
void Sprite3DMaterialCache::removeUnusedSprite3DMaterial()
|
|
|
|
{
|
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)
|
|
|
|
{
|
2019-01-30 09:35:17 +08:00
|
|
|
CCLOG("cocos2d: Sprite3DMaterialCache: removing unused Sprite3DMaterial");
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|