axmol/extensions/Particle3D/PU/PUMaterialManager.cpp

214 lines
6.6 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (C) 2013 Henry van Merode. All rights reserved.
Copyright (c) 2015-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
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.
****************************************************************************/
#include "PUMaterialManager.h"
#include "extensions/Particle3D/PU/PUScriptCompiler.h"
#include "extensions/Particle3D/PU/PUTranslateManager.h"
#include "platform/FileUtils.h"
#include "platform/PlatformMacros.h"
2019-11-23 20:27:39 +08:00
#include "renderer/backend/Types.h"
2022-07-16 10:43:05 +08:00
#if (AX_TARGET_PLATFORM == AX_PLATFORM_WIN32)
2021-12-25 10:04:45 +08:00
# include <io.h>
2022-07-16 10:43:05 +08:00
#elif (AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID)
# include "platform/android/FileUtils-android.h"
2021-12-25 10:04:45 +08:00
# include <android/asset_manager.h>
2022-07-16 10:43:05 +08:00
#elif (AX_TARGET_PLATFORM == AX_PLATFORM_IOS || AX_TARGET_PLATFORM == AX_PLATFORM_MAC)
2021-12-25 10:04:45 +08:00
# include <ftw.h>
2022-07-16 10:43:05 +08:00
#elif (AX_TARGET_PLATFORM == AX_PLATFORM_LINUX)
2021-12-25 10:04:45 +08:00
# include <sys/types.h>
# include <sys/stat.h>
# include <dirent.h>
2019-11-23 20:27:39 +08:00
#endif
NS_AX_BEGIN
2019-11-23 20:27:39 +08:00
PUMaterial::PUMaterial()
2021-12-25 10:04:45 +08:00
: isEnabledLight(true)
, ambientColor(Vec4::ONE)
, diffuseColor(Vec4::ONE)
, specularColor(Vec4::ZERO)
, emissiveColor(Vec4::ZERO)
, shininess(0.0f)
, depthTest(true)
, depthWrite(true)
, wrapMode(backend::SamplerAddressMode::CLAMP_TO_EDGE)
2019-11-23 20:27:39 +08:00
{
blendFunc.src = backend::BlendFactor::ONE;
blendFunc.dst = backend::BlendFactor::ZERO;
}
2021-12-25 10:04:45 +08:00
PUMaterialCache::PUMaterialCache() {}
2019-11-23 20:27:39 +08:00
PUMaterialCache::~PUMaterialCache()
{
for (auto&& iter : _materialMap)
2021-12-25 10:04:45 +08:00
{
2019-11-23 20:27:39 +08:00
iter->release();
}
_materialMap.clear();
}
PUMaterialCache* PUMaterialCache::Instance()
{
static PUMaterialCache pmm;
return &pmm;
}
PUMaterial* PUMaterialCache::getMaterial(std::string_view name)
2019-11-23 20:27:39 +08:00
{
for (auto&& iter : _materialMap)
2021-12-25 10:04:45 +08:00
{
2019-11-23 20:27:39 +08:00
if (iter->name == name)
return iter;
}
return nullptr;
}
bool PUMaterialCache::loadMaterials(std::string_view file)
2019-11-23 20:27:39 +08:00
{
bool isFirstCompile = true;
2021-12-25 10:04:45 +08:00
auto list = PUScriptCompiler::Instance()->compile(file, isFirstCompile);
if (list == nullptr || list->empty())
return false;
if (isFirstCompile)
{
2019-11-23 20:27:39 +08:00
PUTranslateManager::Instance()->translateMaterialSystem(this, list);
}
return true;
}
2021-12-25 10:04:45 +08:00
void PUMaterialCache::addMaterial(PUMaterial* material)
2019-11-23 20:27:39 +08:00
{
for (auto&& iter : _materialMap)
2021-12-25 10:04:45 +08:00
{
if (iter->name == material->name)
{
2022-07-16 10:43:05 +08:00
AXLOG("warning: Material has existed (FilePath: %s, MaterialName: %s)", material->fileName.c_str(),
2021-12-25 10:04:45 +08:00
material->name.c_str());
return;
}
2019-11-23 20:27:39 +08:00
}
material->retain();
_materialMap.emplace_back(material);
2019-11-23 20:27:39 +08:00
}
2022-07-16 10:43:05 +08:00
#if (AX_TARGET_PLATFORM == AX_PLATFORM_IOS || AX_TARGET_PLATFORM == AX_PLATFORM_MAC)
2021-12-25 10:04:45 +08:00
int iterPath(const char* fpath, const struct stat* /*sb*/, int typeflag)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
if (typeflag == FTW_F)
2019-11-23 20:27:39 +08:00
{
if (FileUtils::getInstance()->getFileExtension(fpath) == ".material")
PUMaterialCache::Instance()->loadMaterials(fpath);
}
return 0;
}
#endif
bool PUMaterialCache::loadMaterialsFromSearchPaths(std::string_view fileFolder)
2019-11-23 20:27:39 +08:00
{
bool state = false;
#if defined(_WIN32)
2021-12-25 10:04:45 +08:00
std::string seg("/");
std::string fullPath{fileFolder};
fullPath += seg;
fullPath += std::string("*.material");
WIN32_FIND_DATAA data;
HANDLE handle =
FindFirstFileExA(fullPath.c_str(), FindExInfoStandard, &data, FindExSearchNameMatch, NULL, 0);
if (handle != INVALID_HANDLE_VALUE)
2021-12-25 10:04:45 +08:00
{
state = true;
do
{
fullPath = fileFolder;
fullPath += seg;
fullPath += data.cFileName;
loadMaterials(fullPath);
} while (FindNextFileA(handle, &data));
FindClose(handle);
2021-12-25 10:04:45 +08:00
}
#elif (AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID)
std::string::size_type pos = fileFolder.find("assets/");
std::string_view relativePath = fileFolder;
2021-12-25 10:04:45 +08:00
if (pos != std::string::npos)
{
2019-11-23 20:27:39 +08:00
// "assets/" is at the beginning of the path and we don't want it
relativePath = fileFolder.substr(pos + strlen("assets/"));
}
AAssetDir* dir = AAssetManager_openDir(FileUtilsAndroid::getAssetManager(), relativePath.data());
2021-12-25 10:04:45 +08:00
const char* fileName = nullptr;
std::string_view seg("/", 1);
std::string fullpath;
2019-11-23 20:27:39 +08:00
while ((fileName = AAssetDir_getNextFileName(dir)) != nullptr)
{
if (FileUtils::getInstance()->getFileExtension(fileName) == ".material")
{
fullpath.assign(fileFolder).append(seg).append(fileName);
2019-11-23 20:27:39 +08:00
loadMaterials(fullpath);
}
}
AAssetDir_close(dir);
2022-07-16 10:43:05 +08:00
#elif (AX_TARGET_PLATFORM == AX_PLATFORM_IOS || AX_TARGET_PLATFORM == AX_PLATFORM_MAC)
ftw(fileFolder.data(), iterPath, 500);
#elif (AX_TARGET_PLATFORM == AX_PLATFORM_LINUX)
2021-12-25 10:04:45 +08:00
DIR* d; // dir handle
struct dirent* file; // readdir
2019-11-23 20:27:39 +08:00
struct stat statbuf;
if (!(d = opendir(fileFolder.data())))
2019-11-23 20:27:39 +08:00
{
2022-07-16 10:43:05 +08:00
AXLOG("error opendir %s!!!\n", fileFolder.data());
2019-11-23 20:27:39 +08:00
return false;
}
2021-12-25 10:04:45 +08:00
while ((file = readdir(d)) != NULL)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
if (strncmp(file->d_name, ".", 1) == 0 || (stat(file->d_name, &statbuf) >= 0 && S_ISDIR(statbuf.st_mode)))
2019-11-23 20:27:39 +08:00
{
continue;
}
if (FileUtils::getInstance()->getFileExtension(file->d_name) == ".material")
{
std::string fullpath{fileFolder};
fullpath.append("/"sv).append(file->d_name);
2022-07-16 10:43:05 +08:00
AXLOG("%s", fullpath.c_str());
2019-11-23 20:27:39 +08:00
loadMaterials(fullpath);
state = true;
}
}
closedir(d);
#else
2022-07-16 10:43:05 +08:00
AXASSERT(0, "no implement for this platform");
2019-11-23 20:27:39 +08:00
#endif
return state;
}
NS_AX_END