axmol/core/3d/CCMeshMaterial.cpp

393 lines
14 KiB
C++
Raw Normal View History

2014-06-16 20:58:13 +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
https://axis-project.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.
****************************************************************************/
#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"
#include "base/CCConfiguration.h"
#include "renderer/backend/ProgramState.h"
#include "renderer/ccShaders.h"
#include "renderer/CCPass.h"
2014-06-16 20:58:13 +08:00
NS_AX_BEGIN
2014-06-16 20:58:13 +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;
MeshMaterial* MeshMaterial::_quadTextureMaterial = nullptr;
MeshMaterial* MeshMaterial::_quadColorMaterial = nullptr;
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;
backend::ProgramState* MeshMaterial::_quadTextureMaterialProgState = nullptr;
backend::ProgramState* MeshMaterial::_quadColorMaterialProgState = nullptr;
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);
_unLitMaterialSkin = new MeshMaterial();
if (_unLitMaterialSkin && _unLitMaterialSkin->initWithProgramState(_unLitMaterialSkinProgState))
2015-09-07 13:59:38 +08:00
{
_unLitMaterialSkin->_type = MeshMaterial::MaterialType::UNLIT;
2015-09-07 13:59:38 +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);
_diffuseMaterialSkin = new MeshMaterial();
if (_diffuseMaterialSkin && _diffuseMaterialSkin->initWithProgramState(_diffuseMaterialSkinProgState))
2015-09-07 13:59:38 +08:00
{
_diffuseMaterialSkin->_type = MeshMaterial::MaterialType::DIFFUSE;
2015-09-07 13:59:38 +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);
_diffuseMaterial = new MeshMaterial();
if (_diffuseMaterial && _diffuseMaterial->initWithProgramState(_diffuseMaterialProgState))
2015-09-07 13:59:38 +08:00
{
_diffuseMaterial->_type = MeshMaterial::MaterialType::DIFFUSE;
2015-09-07 13:59:38 +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);
_unLitMaterial = new MeshMaterial();
if (_unLitMaterial && _unLitMaterial->initWithProgramState(_unLitMaterialProgState))
2015-09-07 13:59:38 +08:00
{
_unLitMaterial->_type = MeshMaterial::MaterialType::UNLIT;
2015-09-07 13:59:38 +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);
_unLitNoTexMaterial = new MeshMaterial();
if (_unLitNoTexMaterial && _unLitNoTexMaterial->initWithProgramState(_unLitNoTexMaterialProgState))
2015-09-07 13:59:38 +08:00
{
_unLitNoTexMaterial->_type = MeshMaterial::MaterialType::UNLIT_NOTEX;
2015-09-07 13:59:38 +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);
_diffuseNoTexMaterial = new MeshMaterial();
if (_diffuseNoTexMaterial && _diffuseNoTexMaterial->initWithProgramState(_diffuseNoTexMaterialProgState))
2015-09-07 13:59:38 +08:00
{
_diffuseNoTexMaterial->_type = MeshMaterial::MaterialType::DIFFUSE_NOTEX;
2015-09-07 13:59:38 +08:00
}
2015-11-25 10:45:03 +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);
_bumpedDiffuseMaterial = new MeshMaterial();
if (_bumpedDiffuseMaterial && _bumpedDiffuseMaterial->initWithProgramState(_bumpedDiffuseMaterialProgState))
2015-11-25 10:45:03 +08:00
{
_bumpedDiffuseMaterial->_type = MeshMaterial::MaterialType::BUMPED_DIFFUSE;
2015-11-25 10:45:03 +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);
_bumpedDiffuseMaterialSkin = new MeshMaterial();
2021-12-25 10:04:45 +08:00
if (_bumpedDiffuseMaterialSkin &&
_bumpedDiffuseMaterialSkin->initWithProgramState(_bumpedDiffuseMaterialSkinProgState))
2015-11-25 10:45:03 +08:00
{
_bumpedDiffuseMaterialSkin->_type = MeshMaterial::MaterialType::BUMPED_DIFFUSE;
2015-11-25 10:45:03 +08:00
}
2022-07-03 00:46:41 +08:00
program = backend::Program::getBuiltinProgram(backend::ProgramType::QUAD_TEXTURE_2D);
_quadTextureMaterialProgState = new backend::ProgramState(program);
_quadTextureMaterial = new MeshMaterial();
2022-07-03 00:46:41 +08:00
if (_quadTextureMaterial && _quadTextureMaterial->initWithProgramState(_quadTextureMaterialProgState))
{
_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);
_quadColorMaterial = new MeshMaterial();
2022-07-03 00:46:41 +08:00
if (_quadColorMaterial && _quadColorMaterial->initWithProgramState(_quadColorMaterialProgState))
{
_quadColorMaterial->_type = MeshMaterial::MaterialType::QUAD_COLOR;
}
2015-09-07 13:59:38 +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
}
void MeshMaterial::releaseCachedMaterial()
2015-09-08 15:29:54 +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();
}
Material* MeshMaterial::clone() const
2015-09-07 13:59:38 +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;
}
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
MeshMaterial* material = nullptr;
2021-12-25 10:04:45 +08:00
switch (type)
{
case MeshMaterial::MaterialType::UNLIT:
2021-12-25 10:04:45 +08:00
material = skinned ? _unLitMaterialSkin : _unLitMaterial;
break;
case MeshMaterial::MaterialType::UNLIT_NOTEX:
2021-12-25 10:04:45 +08:00
material = _unLitNoTexMaterial;
break;
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;
case MeshMaterial::MaterialType::DIFFUSE:
2021-12-25 10:04:45 +08:00
material = skinned ? _diffuseMaterialSkin : _diffuseMaterial;
break;
case MeshMaterial::MaterialType::DIFFUSE_NOTEX:
2021-12-25 10:04:45 +08:00
material = _diffuseNoTexMaterial;
break;
case MeshMaterial::MaterialType::BUMPED_DIFFUSE:
2021-12-25 10:04:45 +08:00
material = skinned ? _bumpedDiffuseMaterialSkin : _bumpedDiffuseMaterial;
break;
case MeshMaterial::MaterialType::QUAD_TEXTURE:
2022-07-03 00:46:41 +08:00
material = _quadTextureMaterial;
break;
case MeshMaterial::MaterialType::QUAD_COLOR:
2022-07-03 00:46:41 +08:00
material = _quadColorMaterial;
break;
2021-12-25 10:04:45 +08:00
default:
break;
2015-09-07 13:59:38 +08:00
}
if (material)
return (MeshMaterial*)material->clone();
2015-09-07 13:59:38 +08:00
return nullptr;
}
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())
return (MeshMaterial*)it->second->clone();
2021-12-25 10:04:45 +08:00
auto material = new MeshMaterial();
2015-09-07 13:59:38 +08:00
if (material->initWithFile(path))
{
material->_type = MeshMaterial::MaterialType::CUSTOM;
2015-09-08 15:29:54 +08:00
_materials[validfilename] = material;
2021-12-25 10:04:45 +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;
}
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.");
auto mat = new MeshMaterial();
2021-12-08 00:11:53 +08:00
if (mat->initWithProgramState(programState))
2015-09-07 13:59:38 +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;
}
void MeshMaterial::setTexture(Texture2D* tex, NTextureData::Usage usage)
2015-12-01 10:37:53 +08:00
{
const auto& passes = getTechnique()->getPasses();
for (auto&& pass : passes)
2021-12-25 10:04:45 +08:00
{
pass->setUniformTexture(0, tex->getBackendTexture());
2015-12-01 10:37:53 +08:00
}
}
2015-09-07 13:59:38 +08:00
//////////////////////////////////////////////////////////////////////////////////////////////////////
MeshMaterialCache::MeshMaterialCache() {}
2014-06-16 20:58:13 +08:00
MeshMaterialCache::~MeshMaterialCache()
2014-06-16 20:58:13 +08:00
{
removeAllMeshMaterial();
2014-06-16 20:58:13 +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
{
_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;
}
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
}
}
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;
}
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;
}
void MeshMaterialCache::removeAllMeshMaterial()
2014-06-16 20:58:13 +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();
}
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;
}
}
}
NS_AX_END