2015-02-23 00:34:30 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2014 cocos2d-x.org
|
2019-11-25 01:35:26 +08:00
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-09 22:23:34 +08:00
|
|
|
https://axis-project.github.io/
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-02-23 00:34:30 +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:
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-02-23 00:34:30 +08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-02-23 00:34:30 +08:00
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-03-16 14:02:08 +08:00
|
|
|
#include "2d/CCLight.h"
|
2022-07-05 14:48:46 +08:00
|
|
|
#include "3d/CCMeshRenderer.h"
|
2016-03-16 14:02:08 +08:00
|
|
|
#include "3d/CCAnimate3D.h"
|
|
|
|
#include "3d/CCAnimation3D.h"
|
|
|
|
#include "platform/CCFileUtils.h"
|
2022-07-05 14:48:46 +08:00
|
|
|
#include "WidgetReader/MeshReader/MeshReader.h"
|
2015-02-23 00:34:30 +08:00
|
|
|
|
2020-10-21 10:12:00 +08:00
|
|
|
#include "CSParseBinary_generated.h"
|
|
|
|
#include "CSParse3DBinary_generated.h"
|
2015-02-25 06:39:21 +08:00
|
|
|
|
2020-10-21 10:12:00 +08:00
|
|
|
#include "FlatBuffersSerialize.h"
|
|
|
|
#include "WidgetReader/Node3DReader/Node3DReader.h"
|
2015-02-23 00:34:30 +08:00
|
|
|
|
|
|
|
#include "flatbuffers/flatbuffers.h"
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
USING_NS_AX;
|
2015-02-23 00:34:30 +08:00
|
|
|
using namespace flatbuffers;
|
|
|
|
|
|
|
|
namespace cocostudio
|
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
IMPLEMENT_CLASS_NODE_READER_INFO(MeshReader)
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
MeshReader::MeshReader() {}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
MeshReader::~MeshReader() {}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
static MeshReader* _instanceMeshReader = nullptr;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
MeshReader* MeshReader::getInstance()
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
if (!_instanceMeshReader)
|
2015-02-23 00:34:30 +08:00
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
_instanceMeshReader = new MeshReader();
|
2015-02-23 00:34:30 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
return _instanceMeshReader;
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
void MeshReader::purge()
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
CC_SAFE_DELETE(_instanceMeshReader);
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
void MeshReader::destroyInstance()
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
CC_SAFE_DELETE(_instanceMeshReader);
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
Vec2 MeshReader::getVec2Attribute(pugi::xml_attribute attribute) const
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
|
|
|
if (!attribute)
|
|
|
|
return Vec2::ZERO;
|
|
|
|
|
|
|
|
Vec2 ret;
|
|
|
|
std::string attriname;
|
|
|
|
|
|
|
|
while (attribute)
|
2015-02-23 00:34:30 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
attriname = attribute.name();
|
2021-12-31 15:49:45 +08:00
|
|
|
std::string_view value = attribute.value();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
if (attriname == "X")
|
2015-02-23 00:34:30 +08:00
|
|
|
{
|
2021-12-31 15:49:45 +08:00
|
|
|
ret.x = atof(value.data());
|
2015-02-23 00:34:30 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else if (attriname == "Y")
|
2015-02-23 00:34:30 +08:00
|
|
|
{
|
2021-12-31 15:49:45 +08:00
|
|
|
ret.y = atof(value.data());
|
2015-02-23 00:34:30 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
attribute = attribute.next_attribute();
|
2015-02-23 00:34:30 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
Offset<Table> MeshReader::createOptionsWithFlatBuffers(pugi::xml_node objectData,
|
2021-12-25 10:04:45 +08:00
|
|
|
flatbuffers::FlatBufferBuilder* builder)
|
|
|
|
{
|
|
|
|
auto temp = Node3DReader::getInstance()->createOptionsWithFlatBuffers(objectData, builder);
|
|
|
|
auto node3DOptions = *(Offset<Node3DOption>*)(&temp);
|
|
|
|
|
|
|
|
bool runAction = false;
|
|
|
|
std::string path;
|
|
|
|
int resourceType = 0;
|
|
|
|
bool isFlipped = false;
|
|
|
|
int lightFlag = 0;
|
|
|
|
|
|
|
|
std::string attriname;
|
|
|
|
auto attribute = objectData.first_attribute();
|
|
|
|
while (attribute)
|
2015-02-23 00:34:30 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
attriname = attribute.name();
|
2021-12-31 15:49:45 +08:00
|
|
|
std::string_view value = attribute.value();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
if (attriname == "RunAction3D")
|
2015-02-23 00:34:30 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
runAction = value == "True" ? true : false;
|
|
|
|
}
|
|
|
|
else if (attriname == "IsFlipped")
|
|
|
|
{
|
|
|
|
isFlipped = value == "True" ? true : false;
|
|
|
|
}
|
|
|
|
else if (attriname == "LightFlag")
|
|
|
|
{
|
|
|
|
if (value == "LIGHT0")
|
|
|
|
lightFlag = (int)LightFlag::LIGHT0;
|
|
|
|
else if (value == "LIGHT1")
|
|
|
|
lightFlag = (int)LightFlag::LIGHT1;
|
|
|
|
else if (value == "LIGHT2")
|
|
|
|
lightFlag = (int)LightFlag::LIGHT2;
|
|
|
|
else if (value == "LIGHT3")
|
|
|
|
lightFlag = (int)LightFlag::LIGHT3;
|
|
|
|
else if (value == "LIGHT4")
|
|
|
|
lightFlag = (int)LightFlag::LIGHT4;
|
|
|
|
else if (value == "LIGHT5")
|
|
|
|
lightFlag = (int)LightFlag::LIGHT5;
|
|
|
|
else if (value == "LIGHT6")
|
|
|
|
lightFlag = (int)LightFlag::LIGHT6;
|
|
|
|
else if (value == "LIGHT7")
|
|
|
|
lightFlag = (int)LightFlag::LIGHT7;
|
|
|
|
else if (value == "LIGHT8")
|
|
|
|
lightFlag = (int)LightFlag::LIGHT8;
|
|
|
|
else if (value == "LIGHT9")
|
|
|
|
lightFlag = (int)LightFlag::LIGHT9;
|
|
|
|
else if (value == "LIGHT10")
|
|
|
|
lightFlag = (int)LightFlag::LIGHT10;
|
|
|
|
else if (value == "LIGHT11")
|
|
|
|
lightFlag = (int)LightFlag::LIGHT11;
|
|
|
|
else if (value == "LIGHT12")
|
|
|
|
lightFlag = (int)LightFlag::LIGHT12;
|
2015-02-23 00:34:30 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
attribute = attribute.next_attribute();
|
|
|
|
}
|
|
|
|
|
|
|
|
// FileData
|
|
|
|
auto child = objectData.first_child();
|
|
|
|
while (child)
|
|
|
|
{
|
2021-12-31 15:49:45 +08:00
|
|
|
std::string_view name = child.name();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
if (name == "FileData")
|
2015-02-23 00:34:30 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
attribute = child.first_attribute();
|
|
|
|
|
|
|
|
while (attribute)
|
2015-02-23 00:34:30 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
name = attribute.name();
|
2021-12-31 15:49:45 +08:00
|
|
|
std::string_view value = attribute.value();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
if (name == "Path")
|
2015-02-23 00:34:30 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
path = value;
|
|
|
|
}
|
|
|
|
else if (name == "Type")
|
|
|
|
{
|
|
|
|
if (value == "Normal")
|
2015-02-23 00:34:30 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
resourceType = 1;
|
2015-02-23 00:34:30 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else if (value == "Default")
|
2015-02-23 00:34:30 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
resourceType = 0;
|
2015-02-23 00:34:30 +08:00
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
attribute = attribute.next_attribute();
|
2015-02-23 00:34:30 +08:00
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
child = child.next_sibling();
|
2015-02-23 00:34:30 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
auto options = CreateSprite3DOptions(
|
|
|
|
*builder, node3DOptions,
|
|
|
|
CreateResourceData(*builder, builder->CreateString(path), builder->CreateString(""), resourceType), runAction,
|
|
|
|
isFlipped, lightFlag);
|
|
|
|
|
|
|
|
return *(Offset<Table>*)(&options);
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
void MeshReader::setPropsWithFlatBuffers(axis::Node* node, const flatbuffers::Table* sprite3DOptions)
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
MeshRenderer* mesh = static_cast<MeshRenderer*>(node);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
auto options = (Sprite3DOptions*)sprite3DOptions;
|
|
|
|
|
|
|
|
int lightFlag = options->lightFlag();
|
|
|
|
bool runAction = options->runAction() != 0;
|
|
|
|
bool isFlipped = options->isFlipped() != 0;
|
|
|
|
auto fileData = options->fileData();
|
|
|
|
std::string path = fileData->path()->c_str();
|
|
|
|
|
|
|
|
if (runAction && FileUtils::getInstance()->isFileExist(path))
|
2015-02-23 00:34:30 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Animation3D* animation = Animation3D::create(path);
|
|
|
|
if (animation)
|
2015-02-23 00:34:30 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Animate3D* animate = Animate3D::create(animation);
|
|
|
|
Action* action = RepeatForever::create(animate);
|
2022-07-05 14:48:46 +08:00
|
|
|
mesh->runAction(action);
|
2015-08-27 18:23:13 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
2015-09-29 18:33:28 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
auto nodeOptions = options->node3DOption()->nodeOptions();
|
|
|
|
|
|
|
|
uint8_t alpha = (uint8_t)nodeOptions->color()->a();
|
|
|
|
uint8_t red = (uint8_t)nodeOptions->color()->r();
|
|
|
|
uint8_t green = (uint8_t)nodeOptions->color()->g();
|
|
|
|
uint8_t blue = (uint8_t)nodeOptions->color()->b();
|
|
|
|
|
|
|
|
if (alpha != 255)
|
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
mesh->setOpacity(alpha);
|
2015-02-23 00:34:30 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (red != 255 || green != 255 || blue != 255)
|
2015-02-23 00:34:30 +08:00
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
mesh->setColor(Color3B(red, green, blue));
|
2015-02-23 00:34:30 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (isFlipped)
|
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
mesh->setCullFaceEnabled(true);
|
|
|
|
mesh->setCullFace(CullFaceSide::FRONT);
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (lightFlag <= 0)
|
|
|
|
{
|
|
|
|
lightFlag = 1;
|
|
|
|
}
|
2022-07-05 14:48:46 +08:00
|
|
|
mesh->setLightMask(lightFlag);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
auto node3DReader = Node3DReader::getInstance();
|
2022-07-05 14:48:46 +08:00
|
|
|
node3DReader->setPropsWithFlatBuffers(mesh, (Table*)(options->node3DOption()));
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
Node* MeshReader::createNodeWithFlatBuffers(const flatbuffers::Table* sprite3DOptions)
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
|
|
|
auto options = (Sprite3DOptions*)sprite3DOptions;
|
|
|
|
|
|
|
|
auto fileData = options->fileData();
|
|
|
|
std::string path = fileData->path()->c_str();
|
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
MeshRenderer* ret = MeshRenderer::create();
|
2021-12-25 10:04:45 +08:00
|
|
|
if (FileUtils::getInstance()->isFileExist(path))
|
|
|
|
{
|
|
|
|
ret->initWithFile(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
setPropsWithFlatBuffers(ret, sprite3DOptions);
|
|
|
|
|
|
|
|
return ret;
|
2015-02-23 00:34:30 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
} // namespace cocostudio
|