2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2014-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-10-11 12:15:41 +08:00
|
|
|
Copyright (c) 2021 Bytedance Inc.
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-10-11 12:15:41 +08:00
|
|
|
https://adxe.org
|
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 "3d/CCBundle3D.h"
|
|
|
|
#include "3d/CCObjLoader.h"
|
|
|
|
|
|
|
|
#include "base/ccMacros.h"
|
|
|
|
#include "platform/CCFileUtils.h"
|
|
|
|
#include "3d/CCBundleReader.h"
|
|
|
|
#include "base/CCData.h"
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
#define BUNDLE_TYPE_SCENE 1
|
|
|
|
#define BUNDLE_TYPE_NODE 2
|
|
|
|
#define BUNDLE_TYPE_ANIMATIONS 3
|
|
|
|
#define BUNDLE_TYPE_ANIMATION 4
|
|
|
|
#define BUNDLE_TYPE_ANIMATION_CHANNEL 5
|
|
|
|
#define BUNDLE_TYPE_MODEL 10
|
|
|
|
#define BUNDLE_TYPE_MATERIAL 16
|
|
|
|
#define BUNDLE_TYPE_EFFECT 18
|
|
|
|
#define BUNDLE_TYPE_CAMERA 32
|
|
|
|
#define BUNDLE_TYPE_LIGHT 33
|
|
|
|
#define BUNDLE_TYPE_MESH 34
|
|
|
|
#define BUNDLE_TYPE_MESHPART 35
|
|
|
|
#define BUNDLE_TYPE_MESHSKIN 36
|
|
|
|
|
|
|
|
static const char* VERSION = "version";
|
|
|
|
static const char* ID = "id";
|
|
|
|
static const char* DEFAULTPART = "body";
|
|
|
|
static const char* VERTEXSIZE = "vertexsize";
|
|
|
|
static const char* VERTEX = "vertex";
|
|
|
|
static const char* VERTICES = "vertices";
|
|
|
|
static const char* INDEXNUM = "indexnum";
|
|
|
|
static const char* INDICES = "indices";
|
|
|
|
static const char* SUBMESH = "submesh";
|
|
|
|
static const char* ATTRIBUTES = "attributes";
|
2019-11-23 20:27:39 +08:00
|
|
|
static const char* ATTRIBUTESIZE = "size";
|
2021-12-25 10:04:45 +08:00
|
|
|
static const char* TYPE = "type";
|
|
|
|
static const char* ATTRIBUTE = "attribute";
|
|
|
|
static const char* SKIN = "skin";
|
|
|
|
static const char* BINDSHAPE = "bindshape";
|
|
|
|
static const char* MESH = "mesh";
|
|
|
|
static const char* MESHES = "meshes";
|
|
|
|
static const char* MESHPARTID = "meshpartid";
|
|
|
|
static const char* MATERIALID = "materialid";
|
|
|
|
static const char* NODE = "node";
|
|
|
|
static const char* NODES = "nodes";
|
|
|
|
static const char* CHILDREN = "children";
|
|
|
|
static const char* PARTS = "parts";
|
|
|
|
static const char* BONES = "bones";
|
|
|
|
static const char* SKELETON = "skeleton";
|
|
|
|
static const char* MATERIALS = "materials";
|
|
|
|
static const char* ANIMATIONS = "animations";
|
|
|
|
static const char* TRANSFORM = "transform";
|
|
|
|
static const char* OLDTRANSFORM = "tansform";
|
|
|
|
static const char* ANIMATION = "animation";
|
|
|
|
static const char* MATERIAL = "material";
|
|
|
|
static const char* BASE = "base";
|
|
|
|
static const char* FILENAME = "filename";
|
|
|
|
static const char* TEXTURES = "textures";
|
|
|
|
static const char* LENGTH = "length";
|
|
|
|
static const char* BONEID = "boneId";
|
|
|
|
static const char* KEYFRAMES = "keyframes";
|
|
|
|
static const char* TRANSLATION = "translation";
|
|
|
|
static const char* ROTATION = "rotation";
|
|
|
|
static const char* SCALE = "scale";
|
|
|
|
static const char* KEYTIME = "keytime";
|
|
|
|
static const char* AABBS = "aabb";
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void getChildMap(std::map<int, std::vector<int>>& map, SkinData* skinData, const rapidjson::Value& val)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (!skinData)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// get transform matrix
|
|
|
|
Mat4 transform;
|
|
|
|
const rapidjson::Value& parent_transform = val[OLDTRANSFORM];
|
|
|
|
for (rapidjson::SizeType j = 0, size = parent_transform.Size(); j < size; ++j)
|
|
|
|
{
|
|
|
|
transform.m[j] = parent_transform[j].GetFloat();
|
|
|
|
}
|
|
|
|
|
|
|
|
// set origin matrices
|
|
|
|
std::string parent_name = val[ID].GetString();
|
2021-12-25 10:04:45 +08:00
|
|
|
int parent_name_index = skinData->getSkinBoneNameIndex(parent_name);
|
2019-11-23 20:27:39 +08:00
|
|
|
if (parent_name_index < 0)
|
|
|
|
{
|
|
|
|
skinData->addNodeBoneNames(parent_name);
|
|
|
|
skinData->nodeBoneOriginMatrices.push_back(transform);
|
|
|
|
parent_name_index = skinData->getBoneNameIndex(parent_name);
|
|
|
|
}
|
|
|
|
else if (parent_name_index < static_cast<int>(skinData->skinBoneNames.size()))
|
|
|
|
{
|
|
|
|
skinData->skinBoneOriginMatrices[parent_name_index] = transform;
|
|
|
|
}
|
|
|
|
|
|
|
|
// set root bone index
|
2021-12-25 10:04:45 +08:00
|
|
|
if (skinData->rootBoneIndex < 0)
|
2019-11-23 20:27:39 +08:00
|
|
|
skinData->rootBoneIndex = parent_name_index;
|
|
|
|
|
|
|
|
if (!val.HasMember(CHILDREN))
|
|
|
|
return;
|
|
|
|
|
|
|
|
const rapidjson::Value& children = val[CHILDREN];
|
|
|
|
for (rapidjson::SizeType i = 0, size = children.Size(); i < size; ++i)
|
|
|
|
{
|
|
|
|
// get child bone name
|
|
|
|
const rapidjson::Value& child = children[i];
|
|
|
|
|
|
|
|
std::string child_name = child[ID].GetString();
|
2021-12-25 10:04:45 +08:00
|
|
|
int child_name_index = skinData->getSkinBoneNameIndex(child_name);
|
2019-11-23 20:27:39 +08:00
|
|
|
if (child_name_index < 0)
|
|
|
|
{
|
|
|
|
skinData->addNodeBoneNames(child_name);
|
|
|
|
child_name_index = skinData->getBoneNameIndex(child_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
map[parent_name_index].push_back(child_name_index);
|
|
|
|
|
|
|
|
getChildMap(map, skinData, child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Bundle3D* Bundle3D::createBundle()
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
auto bundle = new Bundle3D();
|
2019-11-23 20:27:39 +08:00
|
|
|
return bundle;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Bundle3D::destroyBundle(Bundle3D* bundle)
|
|
|
|
{
|
|
|
|
delete bundle;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Bundle3D::clear()
|
|
|
|
{
|
|
|
|
if (_isBinary)
|
|
|
|
{
|
|
|
|
_binaryBuffer.clear();
|
|
|
|
CC_SAFE_DELETE_ARRAY(_references);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_jsonBuffer.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Bundle3D::load(const std::string& path)
|
|
|
|
{
|
|
|
|
if (path.empty())
|
|
|
|
return false;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (_path == path)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
getModelRelativePath(path);
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool ret = false;
|
2019-11-23 20:27:39 +08:00
|
|
|
std::string ext = FileUtils::getInstance()->getFileExtension(path);
|
|
|
|
if (ext == ".c3t")
|
|
|
|
{
|
|
|
|
_isBinary = false;
|
2021-12-25 10:04:45 +08:00
|
|
|
ret = loadJson(path);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
else if (ext == ".c3b")
|
|
|
|
{
|
|
|
|
_isBinary = true;
|
2021-12-25 10:04:45 +08:00
|
|
|
ret = loadBinary(path);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
CCLOG("warning: %s is invalid file formate", path.c_str());
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
ret ? (_path = path) : (_path = "");
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool Bundle3D::loadObj(MeshDatas& meshdatas,
|
|
|
|
MaterialDatas& materialdatas,
|
|
|
|
NodeDatas& nodedatas,
|
|
|
|
const std::string& fullPath,
|
|
|
|
const char* mtl_basepath)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
meshdatas.resetData();
|
|
|
|
materialdatas.resetData();
|
|
|
|
nodedatas.resetData();
|
|
|
|
|
|
|
|
std::string mtlPath = "";
|
|
|
|
if (mtl_basepath)
|
|
|
|
mtlPath = mtl_basepath;
|
|
|
|
else
|
|
|
|
mtlPath = fullPath.substr(0, fullPath.find_last_of("\\/") + 1);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
std::vector<tinyobj::shape_t> shapes;
|
|
|
|
std::vector<tinyobj::material_t> materials;
|
|
|
|
auto ret = tinyobj::LoadObj(shapes, materials, fullPath.c_str(), mtlPath.c_str());
|
|
|
|
if (ret.empty())
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// fill data
|
|
|
|
// convert material
|
2019-11-23 20:27:39 +08:00
|
|
|
int i = 0;
|
|
|
|
char str[20];
|
|
|
|
std::string dir = "";
|
2021-12-25 10:04:45 +08:00
|
|
|
auto last = fullPath.rfind('/');
|
2019-11-23 20:27:39 +08:00
|
|
|
if (last != std::string::npos)
|
|
|
|
dir = fullPath.substr(0, last + 1);
|
2021-12-25 10:04:45 +08:00
|
|
|
for (auto& material : materials)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
NMaterialData materialdata;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
NTextureData tex;
|
|
|
|
tex.filename = material.diffuse_texname.empty() ? material.diffuse_texname : dir + material.diffuse_texname;
|
2021-12-25 10:04:45 +08:00
|
|
|
tex.type = NTextureData::Usage::Diffuse;
|
|
|
|
tex.wrapS = backend::SamplerAddressMode::CLAMP_TO_EDGE;
|
|
|
|
tex.wrapT = backend::SamplerAddressMode::CLAMP_TO_EDGE;
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
sprintf(str, "%d", ++i);
|
|
|
|
materialdata.textures.push_back(tex);
|
|
|
|
materialdata.id = str;
|
2021-12-25 10:04:45 +08:00
|
|
|
material.name = str;
|
2019-11-23 20:27:39 +08:00
|
|
|
materialdatas.materials.push_back(materialdata);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
// convert mesh
|
2019-11-23 20:27:39 +08:00
|
|
|
i = 0;
|
2021-12-25 10:04:45 +08:00
|
|
|
for (auto& shape : shapes)
|
|
|
|
{
|
|
|
|
auto mesh = shape.mesh;
|
2021-12-08 00:11:53 +08:00
|
|
|
MeshData* meshdata = new MeshData();
|
2019-11-23 20:27:39 +08:00
|
|
|
MeshVertexAttrib attrib;
|
|
|
|
attrib.type = parseGLDataType("GL_FLOAT", 3);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (mesh.positions.size())
|
|
|
|
{
|
|
|
|
attrib.vertexAttrib = shaderinfos::VertexKey::VERTEX_ATTRIB_POSITION;
|
|
|
|
meshdata->attribs.push_back(attrib);
|
|
|
|
}
|
|
|
|
bool hasnormal = false, hastex = false;
|
|
|
|
if (mesh.normals.size())
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
hasnormal = true;
|
2019-11-23 20:27:39 +08:00
|
|
|
attrib.vertexAttrib = shaderinfos::VertexKey::VERTEX_ATTRIB_NORMAL;
|
|
|
|
meshdata->attribs.push_back(attrib);
|
|
|
|
}
|
|
|
|
if (mesh.texcoords.size())
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
hastex = true;
|
|
|
|
attrib.type = parseGLDataType("GL_FLOAT", 2);
|
2019-11-23 20:27:39 +08:00
|
|
|
attrib.vertexAttrib = shaderinfos::VertexKey::VERTEX_ATTRIB_TEX_COORD;
|
|
|
|
meshdata->attribs.push_back(attrib);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
auto vertexNum = mesh.positions.size() / 3;
|
2021-12-25 10:04:45 +08:00
|
|
|
for (unsigned int k = 0; k < vertexNum; ++k)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
meshdata->vertex.push_back(mesh.positions[k * 3]);
|
|
|
|
meshdata->vertex.push_back(mesh.positions[k * 3 + 1]);
|
|
|
|
meshdata->vertex.push_back(mesh.positions[k * 3 + 2]);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (hasnormal)
|
|
|
|
{
|
|
|
|
meshdata->vertex.push_back(mesh.normals[k * 3]);
|
|
|
|
meshdata->vertex.push_back(mesh.normals[k * 3 + 1]);
|
|
|
|
meshdata->vertex.push_back(mesh.normals[k * 3 + 2]);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (hastex)
|
|
|
|
{
|
|
|
|
meshdata->vertex.push_back(mesh.texcoords[k * 2]);
|
|
|
|
meshdata->vertex.push_back(mesh.texcoords[k * 2 + 1]);
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
// split into submesh according to material
|
|
|
|
std::map<int, std::vector<unsigned short>> subMeshMap;
|
|
|
|
for (size_t k = 0, size = mesh.material_ids.size(); k < size; ++k)
|
|
|
|
{
|
|
|
|
int id = mesh.material_ids[k];
|
2019-11-23 20:27:39 +08:00
|
|
|
size_t idx = k * 3;
|
|
|
|
subMeshMap[id].push_back(mesh.indices[idx]);
|
|
|
|
subMeshMap[id].push_back(mesh.indices[idx + 1]);
|
|
|
|
subMeshMap[id].push_back(mesh.indices[idx + 2]);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2021-12-08 00:11:53 +08:00
|
|
|
auto node = new NodeData();
|
2021-12-25 10:04:45 +08:00
|
|
|
node->id = shape.name;
|
|
|
|
for (auto& submesh : subMeshMap)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
meshdata->subMeshIndices.push_back(submesh.second);
|
2021-12-25 10:04:45 +08:00
|
|
|
meshdata->subMeshAABB.push_back(
|
|
|
|
calculateAABB(meshdata->vertex, meshdata->getPerVertexSize(), submesh.second));
|
2019-11-23 20:27:39 +08:00
|
|
|
sprintf(str, "%d", ++i);
|
|
|
|
meshdata->subMeshIds.push_back(str);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
auto modelnode = new ModelData();
|
2019-11-23 20:27:39 +08:00
|
|
|
modelnode->materialId = submesh.first == -1 ? "" : materials[submesh.first].name;
|
2021-12-25 10:04:45 +08:00
|
|
|
modelnode->subMeshId = str;
|
2019-11-23 20:27:39 +08:00
|
|
|
node->modelNodeDatas.push_back(modelnode);
|
|
|
|
}
|
|
|
|
nodedatas.nodes.push_back(node);
|
|
|
|
meshdatas.meshDatas.push_back(meshdata);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
CCLOG("warning: load %s file error: %s", fullPath.c_str(), ret.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Bundle3D::loadSkinData(const std::string& /*id*/, SkinData* skindata)
|
|
|
|
{
|
|
|
|
skindata->resetData();
|
|
|
|
|
|
|
|
if (_isBinary)
|
|
|
|
{
|
|
|
|
return loadSkinDataBinary(skindata);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return loadSkinDataJson(skindata);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Bundle3D::loadAnimationData(const std::string& id, Animation3DData* animationdata)
|
|
|
|
{
|
|
|
|
animationdata->resetData();
|
|
|
|
|
|
|
|
if (_isBinary)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
return loadAnimationDataBinary(id, animationdata);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
return loadAnimationDataJson(id, animationdata);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
// since 3.3, to support reskin
|
2019-11-23 20:27:39 +08:00
|
|
|
bool Bundle3D::loadMeshDatas(MeshDatas& meshdatas)
|
|
|
|
{
|
|
|
|
meshdatas.resetData();
|
|
|
|
if (_isBinary)
|
|
|
|
{
|
|
|
|
if (_version == "0.1" || _version == "0.2")
|
|
|
|
{
|
|
|
|
return loadMeshDatasBinary_0_1(meshdatas);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return loadMeshDatasBinary(meshdatas);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (_version == "1.2" || _version == "0.2")
|
|
|
|
{
|
|
|
|
return loadMeshDataJson_0_1(meshdatas);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return loadMeshDatasJson(meshdatas);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
bool Bundle3D::loadMeshDatasBinary(MeshDatas& meshdatas)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (!seekToFirstType(BUNDLE_TYPE_MESH))
|
|
|
|
return false;
|
|
|
|
unsigned int meshSize = 0;
|
|
|
|
if (_binaryReader.read(&meshSize, 4, 1) != 1)
|
|
|
|
{
|
|
|
|
CCLOG("warning: Failed to read meshdata: attribCount '%s'.", _path.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
MeshData* meshData = nullptr;
|
|
|
|
for (unsigned int i = 0; i < meshSize; ++i)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
unsigned int attribSize = 0;
|
2019-11-23 20:27:39 +08:00
|
|
|
// read mesh data
|
|
|
|
if (_binaryReader.read(&attribSize, 4, 1) != 1 || attribSize < 1)
|
|
|
|
{
|
|
|
|
CCLOG("warning: Failed to read meshdata: attribCount '%s'.", _path.c_str());
|
|
|
|
goto FAILED;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
meshData = new MeshData();
|
2019-11-23 20:27:39 +08:00
|
|
|
meshData->attribCount = attribSize;
|
|
|
|
meshData->attribs.resize(meshData->attribCount);
|
|
|
|
for (ssize_t j = 0; j < meshData->attribCount; ++j)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
std::string attribute = "";
|
2019-11-23 20:27:39 +08:00
|
|
|
unsigned int vSize;
|
|
|
|
if (_binaryReader.read(&vSize, 4, 1) != 1)
|
|
|
|
{
|
|
|
|
CCLOG("warning: Failed to read meshdata: usage or size '%s'.", _path.c_str());
|
|
|
|
goto FAILED;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
std::string type = _binaryReader.readString();
|
|
|
|
attribute = _binaryReader.readString();
|
|
|
|
meshData->attribs[j].type = parseGLDataType(type, vSize);
|
2019-11-23 20:27:39 +08:00
|
|
|
meshData->attribs[j].vertexAttrib = parseGLProgramAttribute(attribute);
|
|
|
|
}
|
|
|
|
unsigned int vertexSizeInFloat = 0;
|
|
|
|
// Read vertex data
|
|
|
|
if (_binaryReader.read(&vertexSizeInFloat, 4, 1) != 1 || vertexSizeInFloat == 0)
|
|
|
|
{
|
|
|
|
CCLOG("warning: Failed to read meshdata: vertexSizeInFloat '%s'.", _path.c_str());
|
|
|
|
goto FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
meshData->vertex.resize(vertexSizeInFloat);
|
|
|
|
if (_binaryReader.read(&meshData->vertex[0], 4, vertexSizeInFloat) != vertexSizeInFloat)
|
|
|
|
{
|
|
|
|
CCLOG("warning: Failed to read meshdata: vertex element '%s'.", _path.c_str());
|
|
|
|
goto FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read index data
|
|
|
|
unsigned int meshPartCount = 1;
|
|
|
|
_binaryReader.read(&meshPartCount, 4, 1);
|
|
|
|
|
|
|
|
for (unsigned int k = 0; k < meshPartCount; ++k)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
std::vector<unsigned short> indexArray;
|
|
|
|
std::string meshPartid = _binaryReader.readString();
|
2019-11-23 20:27:39 +08:00
|
|
|
meshData->subMeshIds.push_back(meshPartid);
|
|
|
|
unsigned int nIndexCount;
|
|
|
|
if (_binaryReader.read(&nIndexCount, 4, 1) != 1)
|
|
|
|
{
|
|
|
|
CCLOG("warning: Failed to read meshdata: nIndexCount '%s'.", _path.c_str());
|
|
|
|
goto FAILED;
|
|
|
|
}
|
|
|
|
indexArray.resize(nIndexCount);
|
|
|
|
if (_binaryReader.read(&indexArray[0], 2, nIndexCount) != nIndexCount)
|
|
|
|
{
|
|
|
|
CCLOG("warning: Failed to read meshdata: indices '%s'.", _path.c_str());
|
|
|
|
goto FAILED;
|
|
|
|
}
|
|
|
|
meshData->subMeshIndices.push_back(indexArray);
|
|
|
|
meshData->numIndex = (int)meshData->subMeshIndices.size();
|
2021-12-25 10:04:45 +08:00
|
|
|
// meshData->subMeshAABB.push_back(calculateAABB(meshData->vertex, meshData->getPerVertexSize(),
|
|
|
|
// indexArray));
|
2019-11-23 20:27:39 +08:00
|
|
|
if (_version != "0.3" && _version != "0.4" && _version != "0.5")
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// read mesh aabb
|
2019-11-23 20:27:39 +08:00
|
|
|
float aabb[6];
|
|
|
|
if (_binaryReader.read(aabb, 4, 6) != 6)
|
|
|
|
{
|
|
|
|
CCLOG("warning: Failed to read meshdata: aabb '%s'.", _path.c_str());
|
|
|
|
goto FAILED;
|
|
|
|
}
|
|
|
|
meshData->subMeshAABB.push_back(AABB(Vec3(aabb[0], aabb[1], aabb[2]), Vec3(aabb[3], aabb[4], aabb[5])));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
meshData->subMeshAABB.push_back(
|
|
|
|
calculateAABB(meshData->vertex, meshData->getPerVertexSize(), indexArray));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
meshdatas.meshDatas.push_back(meshData);
|
|
|
|
}
|
|
|
|
return true;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
FAILED:
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(meshData);
|
|
|
|
for (auto& meshdata : meshdatas.meshDatas)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
delete meshdata;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
meshdatas.meshDatas.clear();
|
|
|
|
return false;
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
bool Bundle3D::loadMeshDatasBinary_0_1(MeshDatas& meshdatas)
|
|
|
|
{
|
|
|
|
if (!seekToFirstType(BUNDLE_TYPE_MESH))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
meshdatas.resetData();
|
|
|
|
|
2021-12-08 00:11:53 +08:00
|
|
|
MeshData* meshdata = new MeshData();
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
// read mesh data
|
2021-12-25 10:04:45 +08:00
|
|
|
unsigned int attribSize = 0;
|
2019-11-23 20:27:39 +08:00
|
|
|
if (_binaryReader.read(&attribSize, 4, 1) != 1 || attribSize < 1)
|
|
|
|
{
|
|
|
|
CCLOG("warning: Failed to read meshdata: attribCount '%s'.", _path.c_str());
|
|
|
|
CC_SAFE_DELETE(meshdata);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
VERTEX_ATTRIB_POSITION,
|
|
|
|
VERTEX_ATTRIB_COLOR,
|
|
|
|
VERTEX_ATTRIB_TEX_COORD,
|
|
|
|
VERTEX_ATTRIB_NORMAL,
|
|
|
|
VERTEX_ATTRIB_BLEND_WEIGHT,
|
|
|
|
VERTEX_ATTRIB_BLEND_INDEX,
|
|
|
|
|
|
|
|
VERTEX_ATTRIB_MAX,
|
|
|
|
|
|
|
|
// backward compatibility
|
|
|
|
VERTEX_ATTRIB_TEX_COORDS = VERTEX_ATTRIB_TEX_COORD,
|
|
|
|
};
|
|
|
|
for (unsigned int i = 0; i < attribSize; ++i)
|
|
|
|
{
|
|
|
|
unsigned int vUsage, vSize;
|
|
|
|
shaderinfos::VertexKey usage;
|
|
|
|
if (_binaryReader.read(&vUsage, 4, 1) != 1 || _binaryReader.read(&vSize, 4, 1) != 1)
|
|
|
|
{
|
|
|
|
CCLOG("warning: Failed to read meshdata: usage or size '%s'.", _path.c_str());
|
|
|
|
CC_SAFE_DELETE(meshdata);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
MeshVertexAttrib meshVertexAttribute;
|
|
|
|
meshVertexAttribute.type = parseGLDataType("GL_FLOAT", vSize);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (vUsage == VERTEX_ATTRIB_NORMAL)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
usage = shaderinfos::VertexKey::VERTEX_ATTRIB_NORMAL;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else if (vUsage == VERTEX_ATTRIB_BLEND_WEIGHT)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
usage = shaderinfos::VertexKey::VERTEX_ATTRIB_BLEND_WEIGHT;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else if (vUsage == VERTEX_ATTRIB_BLEND_INDEX)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
usage = shaderinfos::VertexKey::VERTEX_ATTRIB_BLEND_INDEX;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else if (vUsage == VERTEX_ATTRIB_POSITION)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
usage = shaderinfos::VertexKey::VERTEX_ATTRIB_POSITION;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else if (vUsage == VERTEX_ATTRIB_TEX_COORD)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
usage = shaderinfos::VertexKey::VERTEX_ATTRIB_TEX_COORD;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CCASSERT(false, "invalidate usage value");
|
|
|
|
}
|
|
|
|
meshVertexAttribute.vertexAttrib = usage;
|
|
|
|
|
|
|
|
meshdata->attribs.push_back(meshVertexAttribute);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read vertex data
|
|
|
|
if (_binaryReader.read(&meshdata->vertexSizeInFloat, 4, 1) != 1 || meshdata->vertexSizeInFloat == 0)
|
|
|
|
{
|
|
|
|
CCLOG("warning: Failed to read meshdata: vertexSizeInFloat '%s'.", _path.c_str());
|
|
|
|
CC_SAFE_DELETE(meshdata);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
meshdata->vertex.resize(meshdata->vertexSizeInFloat);
|
|
|
|
if (_binaryReader.read(&meshdata->vertex[0], 4, meshdata->vertexSizeInFloat) != meshdata->vertexSizeInFloat)
|
|
|
|
{
|
|
|
|
CCLOG("warning: Failed to read meshdata: vertex element '%s'.", _path.c_str());
|
|
|
|
CC_SAFE_DELETE(meshdata);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read index data
|
|
|
|
unsigned int meshPartCount = 1;
|
|
|
|
for (unsigned int i = 0; i < meshPartCount; ++i)
|
|
|
|
{
|
|
|
|
unsigned int nIndexCount;
|
|
|
|
if (_binaryReader.read(&nIndexCount, 4, 1) != 1)
|
|
|
|
{
|
|
|
|
CCLOG("warning: Failed to read meshdata: nIndexCount '%s'.", _path.c_str());
|
|
|
|
CC_SAFE_DELETE(meshdata);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<unsigned short> indices;
|
|
|
|
indices.resize(nIndexCount);
|
|
|
|
if (_binaryReader.read(&indices[0], 2, nIndexCount) != nIndexCount)
|
|
|
|
{
|
|
|
|
CCLOG("warning: Failed to read meshdata: indices '%s'.", _path.c_str());
|
|
|
|
CC_SAFE_DELETE(meshdata);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
meshdata->subMeshIndices.push_back(indices);
|
|
|
|
meshdata->subMeshAABB.push_back(calculateAABB(meshdata->vertex, meshdata->getPerVertexSize(), indices));
|
|
|
|
}
|
|
|
|
|
|
|
|
meshdatas.meshDatas.push_back(meshdata);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Bundle3D::loadMeshDatasBinary_0_2(MeshDatas& meshdatas)
|
|
|
|
{
|
|
|
|
if (!seekToFirstType(BUNDLE_TYPE_MESH))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
meshdatas.resetData();
|
|
|
|
|
2021-12-08 00:11:53 +08:00
|
|
|
MeshData* meshdata = new MeshData();
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
// read mesh data
|
2021-12-25 10:04:45 +08:00
|
|
|
unsigned int attribSize = 0;
|
2019-11-23 20:27:39 +08:00
|
|
|
if (_binaryReader.read(&attribSize, 4, 1) != 1 || attribSize < 1)
|
|
|
|
{
|
|
|
|
CCLOG("warning: Failed to read meshdata: attribCount '%s'.", _path.c_str());
|
|
|
|
CC_SAFE_DELETE(meshdata);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
VERTEX_ATTRIB_POSITION,
|
|
|
|
VERTEX_ATTRIB_COLOR,
|
|
|
|
VERTEX_ATTRIB_TEX_COORD,
|
|
|
|
VERTEX_ATTRIB_NORMAL,
|
|
|
|
VERTEX_ATTRIB_BLEND_WEIGHT,
|
|
|
|
VERTEX_ATTRIB_BLEND_INDEX,
|
|
|
|
|
|
|
|
VERTEX_ATTRIB_MAX,
|
|
|
|
|
|
|
|
// backward compatibility
|
|
|
|
VERTEX_ATTRIB_TEX_COORDS = VERTEX_ATTRIB_TEX_COORD,
|
|
|
|
};
|
|
|
|
for (unsigned int i = 0; i < attribSize; ++i)
|
|
|
|
{
|
|
|
|
unsigned int vUsage, vSize;
|
|
|
|
shaderinfos::VertexKey usage = shaderinfos::VertexKey::VERTEX_ATTRIB_ERROR;
|
|
|
|
if (_binaryReader.read(&vUsage, 4, 1) != 1 || _binaryReader.read(&vSize, 4, 1) != 1)
|
|
|
|
{
|
|
|
|
CCLOG("warning: Failed to read meshdata: usage or size '%s'.", _path.c_str());
|
|
|
|
CC_SAFE_DELETE(meshdata);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
MeshVertexAttrib meshVertexAttribute;
|
|
|
|
meshVertexAttribute.type = parseGLDataType("GL_FLOAT", vSize);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (vUsage == VERTEX_ATTRIB_NORMAL)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
usage = shaderinfos::VertexKey::VERTEX_ATTRIB_NORMAL;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else if (vUsage == VERTEX_ATTRIB_BLEND_WEIGHT)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
usage = shaderinfos::VertexKey::VERTEX_ATTRIB_BLEND_WEIGHT;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else if (vUsage == VERTEX_ATTRIB_BLEND_INDEX)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
usage = shaderinfos::VertexKey::VERTEX_ATTRIB_BLEND_INDEX;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else if (vUsage == VERTEX_ATTRIB_POSITION)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
usage = shaderinfos::VertexKey::VERTEX_ATTRIB_POSITION;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else if (vUsage == VERTEX_ATTRIB_TEX_COORD)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
usage = shaderinfos::VertexKey::VERTEX_ATTRIB_TEX_COORD;
|
|
|
|
}
|
|
|
|
meshVertexAttribute.vertexAttrib = usage;
|
|
|
|
|
|
|
|
meshdata->attribs.push_back(meshVertexAttribute);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read vertex data
|
|
|
|
if (_binaryReader.read(&meshdata->vertexSizeInFloat, 4, 1) != 1 || meshdata->vertexSizeInFloat == 0)
|
|
|
|
{
|
|
|
|
CCLOG("warning: Failed to read meshdata: vertexSizeInFloat '%s'.", _path.c_str());
|
|
|
|
CC_SAFE_DELETE(meshdata);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
meshdata->vertex.resize(meshdata->vertexSizeInFloat);
|
|
|
|
if (_binaryReader.read(&meshdata->vertex[0], 4, meshdata->vertexSizeInFloat) != meshdata->vertexSizeInFloat)
|
|
|
|
{
|
|
|
|
CCLOG("warning: Failed to read meshdata: vertex element '%s'.", _path.c_str());
|
|
|
|
CC_SAFE_DELETE(meshdata);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// read submesh
|
|
|
|
unsigned int submeshCount;
|
|
|
|
if (_binaryReader.read(&submeshCount, 4, 1) != 1)
|
|
|
|
{
|
|
|
|
CCLOG("warning: Failed to read meshdata: submeshCount '%s'.", _path.c_str());
|
|
|
|
CC_SAFE_DELETE(meshdata);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < submeshCount; ++i)
|
|
|
|
{
|
|
|
|
unsigned int nIndexCount;
|
|
|
|
if (_binaryReader.read(&nIndexCount, 4, 1) != 1)
|
|
|
|
{
|
|
|
|
CCLOG("warning: Failed to read meshdata: nIndexCount '%s'.", _path.c_str());
|
|
|
|
CC_SAFE_DELETE(meshdata);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<unsigned short> indices;
|
|
|
|
indices.resize(nIndexCount);
|
|
|
|
if (_binaryReader.read(&indices[0], 2, nIndexCount) != nIndexCount)
|
|
|
|
{
|
|
|
|
CCLOG("warning: Failed to read meshdata: indices '%s'.", _path.c_str());
|
|
|
|
CC_SAFE_DELETE(meshdata);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
meshdata->subMeshIndices.push_back(indices);
|
|
|
|
meshdata->subMeshAABB.push_back(calculateAABB(meshdata->vertex, meshdata->getPerVertexSize(), indices));
|
|
|
|
}
|
|
|
|
|
|
|
|
meshdatas.meshDatas.push_back(meshdata);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return true;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
bool Bundle3D::loadMeshDatasJson(MeshDatas& meshdatas)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
const rapidjson::Value& mesh_data_array = _jsonReader[MESHES];
|
2021-12-25 10:04:45 +08:00
|
|
|
for (rapidjson::SizeType index = 0, mesh_data_array_size = mesh_data_array.Size(); index < mesh_data_array_size;
|
|
|
|
++index)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
MeshData* meshData = new MeshData();
|
2019-11-23 20:27:39 +08:00
|
|
|
const rapidjson::Value& mesh_data = mesh_data_array[index];
|
|
|
|
// mesh_vertex_attribute
|
|
|
|
const rapidjson::Value& mesh_vertex_attribute = mesh_data[ATTRIBUTES];
|
|
|
|
MeshVertexAttrib tempAttrib;
|
2021-12-25 10:04:45 +08:00
|
|
|
meshData->attribCount = mesh_vertex_attribute.Size();
|
2019-11-23 20:27:39 +08:00
|
|
|
meshData->attribs.resize(meshData->attribCount);
|
2021-12-25 10:04:45 +08:00
|
|
|
for (rapidjson::SizeType i = 0, mesh_vertex_attribute_size = mesh_vertex_attribute.Size();
|
|
|
|
i < mesh_vertex_attribute_size; ++i)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
const rapidjson::Value& mesh_vertex_attribute_val = mesh_vertex_attribute[i];
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
int size = mesh_vertex_attribute_val[ATTRIBUTESIZE].GetInt();
|
|
|
|
std::string type = mesh_vertex_attribute_val[TYPE].GetString();
|
2019-11-23 20:27:39 +08:00
|
|
|
std::string attribute = mesh_vertex_attribute_val[ATTRIBUTE].GetString();
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
tempAttrib.type = parseGLDataType(type, size);
|
2019-11-23 20:27:39 +08:00
|
|
|
tempAttrib.vertexAttrib = parseGLProgramAttribute(attribute);
|
2021-12-25 10:04:45 +08:00
|
|
|
meshData->attribs[i] = tempAttrib;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
// mesh vertices
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
const rapidjson::Value& mesh_data_vertex_array = mesh_data[VERTICES];
|
2021-12-25 10:04:45 +08:00
|
|
|
auto mesh_data_vertex_array_size = mesh_data_vertex_array.Size();
|
|
|
|
meshData->vertexSizeInFloat = mesh_data_vertex_array_size;
|
2019-11-23 20:27:39 +08:00
|
|
|
for (rapidjson::SizeType i = 0; i < mesh_data_vertex_array_size; ++i)
|
|
|
|
{
|
|
|
|
meshData->vertex.push_back(mesh_data_vertex_array[i].GetFloat());
|
|
|
|
}
|
|
|
|
// mesh part
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
const rapidjson::Value& mesh_part_array = mesh_data[PARTS];
|
|
|
|
for (rapidjson::SizeType i = 0, mesh_part_array_size = mesh_part_array.Size(); i < mesh_part_array_size; ++i)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
std::vector<unsigned short> indexArray;
|
2019-11-23 20:27:39 +08:00
|
|
|
const rapidjson::Value& mesh_part = mesh_part_array[i];
|
|
|
|
meshData->subMeshIds.push_back(mesh_part[ID].GetString());
|
|
|
|
// index_number
|
|
|
|
const rapidjson::Value& indices_val_array = mesh_part[INDICES];
|
2021-12-25 10:04:45 +08:00
|
|
|
for (rapidjson::SizeType j = 0, indices_val_array_size = indices_val_array.Size();
|
|
|
|
j < indices_val_array_size; ++j)
|
2019-11-23 20:27:39 +08:00
|
|
|
indexArray.push_back((unsigned short)indices_val_array[j].GetUint());
|
|
|
|
|
|
|
|
meshData->subMeshIndices.push_back(indexArray);
|
|
|
|
meshData->numIndex = (int)meshData->subMeshIndices.size();
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (mesh_data.HasMember(AABBS))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
const rapidjson::Value& mesh_part_aabb = mesh_part[AABBS];
|
|
|
|
if (mesh_part.HasMember(AABBS) && mesh_part_aabb.Size() == 6)
|
|
|
|
{
|
|
|
|
Vec3 min(mesh_part_aabb[(rapidjson::SizeType)0].GetFloat(),
|
2021-12-25 10:04:45 +08:00
|
|
|
mesh_part_aabb[(rapidjson::SizeType)1].GetFloat(),
|
|
|
|
mesh_part_aabb[(rapidjson::SizeType)2].GetFloat());
|
2019-11-23 20:27:39 +08:00
|
|
|
Vec3 max(mesh_part_aabb[(rapidjson::SizeType)3].GetFloat(),
|
2021-12-25 10:04:45 +08:00
|
|
|
mesh_part_aabb[(rapidjson::SizeType)4].GetFloat(),
|
|
|
|
mesh_part_aabb[(rapidjson::SizeType)5].GetFloat());
|
2019-11-23 20:27:39 +08:00
|
|
|
meshData->subMeshAABB.push_back(AABB(min, max));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
meshData->subMeshAABB.push_back(
|
|
|
|
calculateAABB(meshData->vertex, meshData->getPerVertexSize(), indexArray));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
meshData->subMeshAABB.push_back(
|
|
|
|
calculateAABB(meshData->vertex, meshData->getPerVertexSize(), indexArray));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
meshdatas.meshDatas.push_back(meshData);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
bool Bundle3D::loadNodes(NodeDatas& nodedatas)
|
|
|
|
{
|
|
|
|
if (_version == "0.1" || _version == "1.2" || _version == "0.2")
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
SkinData skinData;
|
2019-11-23 20:27:39 +08:00
|
|
|
if (!loadSkinData("", &skinData))
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto node = new NodeData();
|
|
|
|
auto modelnode = new ModelData();
|
2019-11-23 20:27:39 +08:00
|
|
|
modelnode->materialId = "";
|
2021-12-25 10:04:45 +08:00
|
|
|
modelnode->subMeshId = "";
|
2019-11-23 20:27:39 +08:00
|
|
|
node->modelNodeDatas.push_back(modelnode);
|
|
|
|
nodedatas.nodes.push_back(node);
|
|
|
|
return true;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2021-12-08 00:11:53 +08:00
|
|
|
auto nodeDatas = new NodeData*[skinData.skinBoneNames.size() + skinData.nodeBoneNames.size()];
|
2021-12-25 10:04:45 +08:00
|
|
|
int index = 0;
|
2019-11-23 20:27:39 +08:00
|
|
|
size_t i;
|
|
|
|
auto skinBoneSize = skinData.skinBoneNames.size();
|
|
|
|
auto nodeBoneSize = skinData.nodeBoneNames.size();
|
|
|
|
for (i = 0; i < skinBoneSize; ++i)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
nodeDatas[index] = new NodeData();
|
|
|
|
nodeDatas[index]->id = skinData.skinBoneNames[i];
|
2019-11-23 20:27:39 +08:00
|
|
|
nodeDatas[index]->transform = skinData.skinBoneOriginMatrices[i];
|
|
|
|
++index;
|
|
|
|
}
|
|
|
|
for (i = 0; i < nodeBoneSize; ++i)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
nodeDatas[index] = new NodeData();
|
|
|
|
nodeDatas[index]->id = skinData.nodeBoneNames[i];
|
2019-11-23 20:27:39 +08:00
|
|
|
nodeDatas[index]->transform = skinData.nodeBoneOriginMatrices[i];
|
|
|
|
++index;
|
|
|
|
}
|
|
|
|
for (const auto& it : skinData.boneChild)
|
|
|
|
{
|
|
|
|
const auto& children = it.second;
|
2021-12-25 10:04:45 +08:00
|
|
|
auto parent = nodeDatas[it.first];
|
2019-11-23 20:27:39 +08:00
|
|
|
for (const auto& child : children)
|
|
|
|
{
|
|
|
|
parent->children.push_back(nodeDatas[child]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nodedatas.skeleton.push_back(nodeDatas[skinData.rootBoneIndex]);
|
2021-12-25 10:04:45 +08:00
|
|
|
auto node = new NodeData();
|
|
|
|
auto modelnode = new ModelData();
|
|
|
|
modelnode->materialId = "";
|
|
|
|
modelnode->subMeshId = "";
|
|
|
|
modelnode->bones = skinData.skinBoneNames;
|
2019-11-23 20:27:39 +08:00
|
|
|
modelnode->invBindPose = skinData.inverseBindPoseMatrices;
|
|
|
|
node->modelNodeDatas.push_back(modelnode);
|
|
|
|
nodedatas.nodes.push_back(node);
|
|
|
|
delete[] nodeDatas;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (_isBinary)
|
|
|
|
{
|
|
|
|
loadNodesBinary(nodedatas);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
loadNodesJson(nodedatas);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
bool Bundle3D::loadMaterials(MaterialDatas& materialdatas)
|
|
|
|
{
|
|
|
|
materialdatas.resetData();
|
|
|
|
if (_isBinary)
|
|
|
|
{
|
|
|
|
if (_version == "0.1")
|
|
|
|
{
|
|
|
|
return loadMaterialsBinary_0_1(materialdatas);
|
|
|
|
}
|
|
|
|
else if (_version == "0.2")
|
|
|
|
{
|
|
|
|
return loadMaterialsBinary_0_2(materialdatas);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return loadMaterialsBinary(materialdatas);
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (_version == "1.2")
|
|
|
|
{
|
|
|
|
return loadMaterialDataJson_0_1(materialdatas);
|
|
|
|
}
|
|
|
|
else if (_version == "0.2")
|
|
|
|
{
|
|
|
|
return loadMaterialDataJson_0_2(materialdatas);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return loadMaterialsJson(materialdatas);
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
bool Bundle3D::loadMaterialsBinary(MaterialDatas& materialdatas)
|
|
|
|
{
|
|
|
|
if (!seekToFirstType(BUNDLE_TYPE_MATERIAL))
|
|
|
|
return false;
|
|
|
|
unsigned int materialnum = 1;
|
|
|
|
_binaryReader.read(&materialnum, 4, 1);
|
|
|
|
for (unsigned int i = 0; i < materialnum; ++i)
|
|
|
|
{
|
|
|
|
NMaterialData materialData;
|
|
|
|
materialData.id = _binaryReader.readString();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// skip: diffuse(3), ambient(3), emissive(3), opacity(1), specular(3), shininess(1)
|
2021-12-25 10:04:45 +08:00
|
|
|
float data[14];
|
|
|
|
_binaryReader.read(&data, sizeof(float), 14);
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
unsigned int textureNum = 1;
|
|
|
|
_binaryReader.read(&textureNum, 4, 1);
|
|
|
|
for (unsigned int j = 0; j < textureNum; ++j)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
NTextureData textureData;
|
2019-11-23 20:27:39 +08:00
|
|
|
textureData.id = _binaryReader.readString();
|
|
|
|
if (textureData.id.empty())
|
|
|
|
{
|
|
|
|
CCLOG("warning: Failed to read Materialdata: texturePath is empty '%s'.", textureData.id.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
std::string texturePath = _binaryReader.readString();
|
|
|
|
if (texturePath.empty())
|
|
|
|
{
|
|
|
|
CCLOG("warning: Failed to read Materialdata: texturePath is empty '%s'.", _path.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
textureData.filename = texturePath.empty() ? texturePath : _modelPath + texturePath;
|
2021-12-25 10:04:45 +08:00
|
|
|
float uvdata[4];
|
|
|
|
_binaryReader.read(&uvdata, sizeof(float), 4);
|
2019-11-23 20:27:39 +08:00
|
|
|
textureData.type = parseGLTextureType(_binaryReader.readString());
|
2021-12-25 10:04:45 +08:00
|
|
|
textureData.wrapS = parseSamplerAddressMode(_binaryReader.readString());
|
|
|
|
textureData.wrapT = parseSamplerAddressMode(_binaryReader.readString());
|
2019-11-23 20:27:39 +08:00
|
|
|
materialData.textures.push_back(textureData);
|
|
|
|
}
|
|
|
|
materialdatas.materials.push_back(materialData);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
bool Bundle3D::loadMaterialsBinary_0_1(MaterialDatas& materialdatas)
|
|
|
|
{
|
|
|
|
if (!seekToFirstType(BUNDLE_TYPE_MATERIAL))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
NMaterialData materialData;
|
|
|
|
|
|
|
|
std::string texturePath = _binaryReader.readString();
|
|
|
|
if (texturePath.empty())
|
|
|
|
{
|
|
|
|
CCLOG("warning: Failed to read Materialdata: texturePath is empty '%s'.", _path.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
NTextureData textureData;
|
|
|
|
textureData.filename = texturePath.empty() ? texturePath : _modelPath + texturePath;
|
2021-12-25 10:04:45 +08:00
|
|
|
textureData.type = NTextureData::Usage::Diffuse;
|
|
|
|
textureData.id = "";
|
2019-11-23 20:27:39 +08:00
|
|
|
materialData.textures.push_back(textureData);
|
|
|
|
materialdatas.materials.push_back(materialData);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Bundle3D::loadMaterialsBinary_0_2(MaterialDatas& materialdatas)
|
|
|
|
{
|
|
|
|
if (!seekToFirstType(BUNDLE_TYPE_MATERIAL))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
unsigned int materialnum = 1;
|
|
|
|
_binaryReader.read(&materialnum, 4, 1);
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < materialnum; ++i)
|
|
|
|
{
|
|
|
|
NMaterialData materialData;
|
|
|
|
|
|
|
|
std::string texturePath = _binaryReader.readString();
|
|
|
|
if (texturePath.empty())
|
|
|
|
{
|
|
|
|
CCLOG("warning: Failed to read Materialdata: texturePath is empty '%s'.", _path.c_str());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
NTextureData textureData;
|
|
|
|
textureData.filename = texturePath.empty() ? texturePath : _modelPath + texturePath;
|
2021-12-25 10:04:45 +08:00
|
|
|
textureData.type = NTextureData::Usage::Diffuse;
|
|
|
|
textureData.id = "";
|
2019-11-23 20:27:39 +08:00
|
|
|
materialData.textures.push_back(textureData);
|
|
|
|
materialdatas.materials.push_back(materialData);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool loadMeshDataJson(MeshData* /*meshdata*/)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool loadMeshDataJson_0_1(MeshData* /*meshdata*/)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool loadMeshDataJson_0_2(MeshData* /*meshdata*/)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool Bundle3D::loadMaterialsJson(MaterialDatas& materialdatas)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (!_jsonReader.HasMember(MATERIALS))
|
|
|
|
return false;
|
|
|
|
const rapidjson::Value& material_array = _jsonReader[MATERIALS];
|
|
|
|
for (rapidjson::SizeType i = 0; i < material_array.Size(); ++i)
|
|
|
|
{
|
|
|
|
NMaterialData materialData;
|
|
|
|
const rapidjson::Value& material_val = material_array[i];
|
2021-12-25 10:04:45 +08:00
|
|
|
materialData.id = material_val[ID].GetString();
|
2019-11-23 20:27:39 +08:00
|
|
|
if (material_val.HasMember(TEXTURES))
|
|
|
|
{
|
|
|
|
const rapidjson::Value& texture_array = material_val[TEXTURES];
|
|
|
|
for (rapidjson::SizeType j = 0; j < texture_array.Size(); ++j)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
NTextureData textureData;
|
2019-11-23 20:27:39 +08:00
|
|
|
const rapidjson::Value& texture_val = texture_array[j];
|
2021-12-25 10:04:45 +08:00
|
|
|
std::string filename = texture_val[FILENAME].GetString();
|
|
|
|
textureData.filename = filename.empty() ? filename : _modelPath + filename;
|
|
|
|
textureData.type = parseGLTextureType(texture_val["type"].GetString());
|
|
|
|
textureData.wrapS = parseSamplerAddressMode(texture_val["wrapModeU"].GetString());
|
|
|
|
textureData.wrapT = parseSamplerAddressMode(texture_val["wrapModeV"].GetString());
|
2019-11-23 20:27:39 +08:00
|
|
|
materialData.textures.push_back(textureData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
materialdatas.materials.push_back(materialData);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
bool Bundle3D::loadJson(const std::string& path)
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
|
|
|
|
_jsonBuffer = FileUtils::getInstance()->getStringFromFile(path);
|
|
|
|
|
|
|
|
if (_jsonReader.ParseInsitu<0>((char*)_jsonBuffer.c_str()).HasParseError())
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
CCLOG("Parse json failed in Bundle3D::loadJson function");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const rapidjson::Value& mash_data_array = _jsonReader[VERSION];
|
2021-12-25 10:04:45 +08:00
|
|
|
if (mash_data_array.IsArray()) // Compatible with the old version
|
2019-11-23 20:27:39 +08:00
|
|
|
_version = "1.2";
|
|
|
|
else
|
|
|
|
_version = mash_data_array.GetString();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Bundle3D::loadBinary(const std::string& path)
|
|
|
|
{
|
|
|
|
clear();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// get file data
|
|
|
|
_binaryBuffer.clear();
|
|
|
|
_binaryBuffer = FileUtils::getInstance()->getDataFromFile(path);
|
|
|
|
if (_binaryBuffer.isNull())
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
CCLOG("warning: Failed to read file: %s", path.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// Initialise bundle reader
|
2021-12-25 10:04:45 +08:00
|
|
|
_binaryReader.init((char*)_binaryBuffer.getBytes(), _binaryBuffer.getSize());
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// Read identifier info
|
2021-12-25 10:04:45 +08:00
|
|
|
char identifier[] = {'C', '3', 'B', '\0'};
|
2019-11-23 20:27:39 +08:00
|
|
|
char sig[4];
|
|
|
|
if (_binaryReader.read(sig, 1, 4) != 4 || memcmp(sig, identifier, 4) != 0)
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
CCLOG("warning: Invalid identifier: %s", path.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// Read version
|
|
|
|
unsigned char ver[2];
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_binaryReader.read(ver, 1, 2) != 2)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
CCLOG("warning: Failed to read version:");
|
|
|
|
return false;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
char version[20] = {0};
|
|
|
|
sprintf(version, "%d.%d", ver[0], ver[1]);
|
|
|
|
_version = version;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// Read ref table size
|
|
|
|
if (_binaryReader.read(&_referenceCount, 4, 1) != 1)
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
CCLOG("warning: Failed to read ref table size '%s'.", path.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// Read all refs
|
|
|
|
CC_SAFE_DELETE_ARRAY(_references);
|
2021-12-08 00:11:53 +08:00
|
|
|
_references = new Reference[_referenceCount];
|
2019-11-23 20:27:39 +08:00
|
|
|
for (unsigned int i = 0; i < _referenceCount; ++i)
|
|
|
|
{
|
|
|
|
if ((_references[i].id = _binaryReader.readString()).empty() ||
|
|
|
|
_binaryReader.read(&_references[i].type, 4, 1) != 1 ||
|
|
|
|
_binaryReader.read(&_references[i].offset, 4, 1) != 1)
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
CCLOG("warning: Failed to read ref number %u for bundle '%s'.", i, path.c_str());
|
|
|
|
CC_SAFE_DELETE_ARRAY(_references);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Bundle3D::loadMeshDataJson_0_1(MeshDatas& meshdatas)
|
|
|
|
{
|
|
|
|
const rapidjson::Value& mesh_data_array = _jsonReader[MESH];
|
2021-12-25 10:04:45 +08:00
|
|
|
MeshData* meshdata = new MeshData();
|
|
|
|
const rapidjson::Value& mesh_data_val = mesh_data_array[(rapidjson::SizeType)0];
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
const rapidjson::Value& mesh_data_body_array = mesh_data_val[DEFAULTPART];
|
|
|
|
|
|
|
|
const rapidjson::Value& mesh_data_body_array_0 = mesh_data_body_array[(rapidjson::SizeType)0];
|
|
|
|
|
|
|
|
// mesh_vertex_attribute
|
|
|
|
const rapidjson::Value& mesh_vertex_attribute = mesh_data_val[ATTRIBUTES];
|
2021-12-25 10:04:45 +08:00
|
|
|
meshdata->attribCount = mesh_vertex_attribute.Size();
|
2019-11-23 20:27:39 +08:00
|
|
|
meshdata->attribs.resize(meshdata->attribCount);
|
|
|
|
for (rapidjson::SizeType i = 0; i < mesh_vertex_attribute.Size(); ++i)
|
|
|
|
{
|
|
|
|
const rapidjson::Value& mesh_vertex_attribute_val = mesh_vertex_attribute[i];
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
int size = mesh_vertex_attribute_val[ATTRIBUTESIZE].GetUint();
|
|
|
|
meshdata->attribs[i].type = parseGLDataType(mesh_vertex_attribute_val[TYPE].GetString(), size);
|
2019-11-23 20:27:39 +08:00
|
|
|
meshdata->attribs[i].vertexAttrib = parseGLProgramAttribute(mesh_vertex_attribute_val[ATTRIBUTE].GetString());
|
|
|
|
}
|
|
|
|
|
|
|
|
// vertices
|
|
|
|
meshdata->vertexSizeInFloat = mesh_data_body_array_0[VERTEXSIZE].GetInt();
|
|
|
|
meshdata->vertex.resize(meshdata->vertexSizeInFloat);
|
|
|
|
|
|
|
|
const rapidjson::Value& mesh_data_body_vertices = mesh_data_body_array_0[VERTICES];
|
|
|
|
for (rapidjson::SizeType i = 0; i < mesh_data_body_vertices.Size(); ++i)
|
|
|
|
meshdata->vertex[i] = mesh_data_body_vertices[i].GetFloat();
|
|
|
|
|
|
|
|
// index_number
|
|
|
|
unsigned int indexnum = mesh_data_body_array_0[INDEXNUM].GetUint();
|
|
|
|
|
|
|
|
// indices
|
|
|
|
std::vector<unsigned short> indices;
|
|
|
|
indices.resize(indexnum);
|
|
|
|
|
|
|
|
const rapidjson::Value& indices_val_array = mesh_data_body_array_0[INDICES];
|
|
|
|
for (rapidjson::SizeType i = 0; i < indices_val_array.Size(); ++i)
|
|
|
|
indices[i] = (unsigned short)indices_val_array[i].GetUint();
|
|
|
|
|
|
|
|
meshdata->subMeshIndices.push_back(indices);
|
|
|
|
meshdata->subMeshAABB.push_back(calculateAABB(meshdata->vertex, meshdata->getPerVertexSize(), indices));
|
|
|
|
meshdatas.meshDatas.push_back(meshdata);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Bundle3D::loadMeshDataJson_0_2(MeshDatas& meshdatas)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
MeshData* meshdata = new MeshData();
|
|
|
|
const rapidjson::Value& mesh_array = _jsonReader[MESH];
|
2019-11-23 20:27:39 +08:00
|
|
|
const rapidjson::Value& mesh_array_0 = mesh_array[(rapidjson::SizeType)0];
|
|
|
|
|
|
|
|
// mesh_vertex_attribute
|
|
|
|
const rapidjson::Value& mesh_vertex_attribute = mesh_array_0[ATTRIBUTES];
|
2021-12-25 10:04:45 +08:00
|
|
|
meshdata->attribCount = mesh_vertex_attribute.Size();
|
2019-11-23 20:27:39 +08:00
|
|
|
meshdata->attribs.resize(meshdata->attribCount);
|
|
|
|
for (rapidjson::SizeType i = 0; i < mesh_vertex_attribute.Size(); ++i)
|
|
|
|
{
|
|
|
|
const rapidjson::Value& mesh_vertex_attribute_val = mesh_vertex_attribute[i];
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
auto size = mesh_vertex_attribute_val[ATTRIBUTESIZE].GetUint();
|
|
|
|
meshdata->attribs[i].type = parseGLDataType(mesh_vertex_attribute_val[TYPE].GetString(), size);
|
2019-11-23 20:27:39 +08:00
|
|
|
meshdata->attribs[i].vertexAttrib = parseGLProgramAttribute(mesh_vertex_attribute_val[ATTRIBUTE].GetString());
|
|
|
|
}
|
|
|
|
|
|
|
|
// vertices
|
2021-12-25 10:04:45 +08:00
|
|
|
const rapidjson::Value& mesh_data_vertex = mesh_array_0[VERTEX];
|
2019-11-23 20:27:39 +08:00
|
|
|
const rapidjson::Value& mesh_data_vertex_0 = mesh_data_vertex[(rapidjson::SizeType)0];
|
|
|
|
|
|
|
|
meshdata->vertexSizeInFloat = mesh_data_vertex_0[VERTEXSIZE].GetInt();
|
|
|
|
meshdata->vertex.resize(meshdata->vertexSizeInFloat);
|
|
|
|
|
|
|
|
const rapidjson::Value& mesh_data_body_vertices = mesh_data_vertex_0[VERTICES];
|
|
|
|
for (rapidjson::SizeType i = 0; i < mesh_data_body_vertices.Size(); ++i)
|
|
|
|
meshdata->vertex[i] = mesh_data_body_vertices[i].GetFloat();
|
|
|
|
|
|
|
|
// submesh
|
|
|
|
const rapidjson::Value& mesh_submesh_array = mesh_array_0[SUBMESH];
|
|
|
|
for (rapidjson::SizeType i = 0; i < mesh_submesh_array.Size(); ++i)
|
|
|
|
{
|
|
|
|
const rapidjson::Value& mesh_submesh_val = mesh_submesh_array[i];
|
2021-12-25 10:04:45 +08:00
|
|
|
// std::string id = mesh_submesh_val[ID].GetString();
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
// index_number
|
|
|
|
unsigned int indexnum = mesh_submesh_val[INDEXNUM].GetUint();
|
|
|
|
|
|
|
|
// indices
|
|
|
|
std::vector<unsigned short> indices;
|
|
|
|
indices.resize(indexnum);
|
|
|
|
|
|
|
|
const rapidjson::Value& indices_val_array = mesh_submesh_val[INDICES];
|
|
|
|
for (rapidjson::SizeType j = 0; j < indices_val_array.Size(); ++j)
|
|
|
|
indices[j] = (unsigned short)indices_val_array[j].GetUint();
|
|
|
|
|
|
|
|
meshdata->subMeshIndices.push_back(indices);
|
|
|
|
meshdata->subMeshAABB.push_back(calculateAABB(meshdata->vertex, meshdata->getPerVertexSize(), indices));
|
|
|
|
}
|
|
|
|
meshdatas.meshDatas.push_back(meshdata);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Bundle3D::loadSkinDataJson(SkinData* skindata)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!_jsonReader.HasMember(SKIN))
|
|
|
|
return false;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
const rapidjson::Value& skin_data_array = _jsonReader[SKIN];
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
CCASSERT(skin_data_array.IsArray(), "skin data is not an array");
|
|
|
|
const rapidjson::Value& skin_data_array_val_0 = skin_data_array[(rapidjson::SizeType)0];
|
|
|
|
|
|
|
|
if (!skin_data_array_val_0.HasMember(BONES))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const rapidjson::Value& skin_data_bones = skin_data_array_val_0[BONES];
|
|
|
|
for (rapidjson::SizeType i = 0; i < skin_data_bones.Size(); ++i)
|
|
|
|
{
|
|
|
|
const rapidjson::Value& skin_data_bone = skin_data_bones[i];
|
2021-12-25 10:04:45 +08:00
|
|
|
std::string name = skin_data_bone[NODE].GetString();
|
2019-11-23 20:27:39 +08:00
|
|
|
skindata->addSkinBoneNames(name);
|
|
|
|
|
|
|
|
Mat4 mat_bind_pos;
|
|
|
|
const rapidjson::Value& bind_pos = skin_data_bone[BINDSHAPE];
|
|
|
|
for (rapidjson::SizeType j = 0; j < bind_pos.Size(); ++j)
|
|
|
|
{
|
|
|
|
mat_bind_pos.m[j] = bind_pos[j].GetFloat();
|
|
|
|
}
|
|
|
|
skindata->inverseBindPoseMatrices.push_back(mat_bind_pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
// set root bone information
|
|
|
|
const rapidjson::Value& skin_data_1 = skin_data_array[1];
|
|
|
|
|
|
|
|
// parent and child relationship map
|
|
|
|
skindata->skinBoneOriginMatrices.resize(skindata->skinBoneNames.size());
|
|
|
|
getChildMap(skindata->boneChild, skindata, skin_data_1);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Bundle3D::loadSkinDataBinary(SkinData* skindata)
|
|
|
|
{
|
|
|
|
if (!seekToFirstType(BUNDLE_TYPE_MESHSKIN))
|
|
|
|
return false;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
std::string boneName = _binaryReader.readString();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// transform
|
|
|
|
float bindShape[16];
|
|
|
|
if (!_binaryReader.readMatrix(bindShape))
|
|
|
|
{
|
|
|
|
CCLOG("warning: Failed to read SkinData: bindShape matrix '%s'.", _path.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// bone count
|
|
|
|
unsigned int boneNum;
|
|
|
|
if (!_binaryReader.read(&boneNum))
|
|
|
|
{
|
|
|
|
CCLOG("warning: Failed to read SkinData: boneNum '%s'.", _path.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// Fix bug: check if the bone number is 0.
|
|
|
|
if (boneNum == 0)
|
|
|
|
return false;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// bone names and bind pos
|
|
|
|
float bindpos[16];
|
|
|
|
for (unsigned int i = 0; i < boneNum; ++i)
|
|
|
|
{
|
|
|
|
std::string skinBoneName = _binaryReader.readString();
|
|
|
|
skindata->skinBoneNames.push_back(skinBoneName);
|
|
|
|
if (!_binaryReader.readMatrix(bindpos))
|
|
|
|
{
|
|
|
|
CCLOG("warning: Failed to load SkinData: bindpos '%s'.", _path.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
skindata->inverseBindPoseMatrices.push_back(bindpos);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
skindata->skinBoneOriginMatrices.resize(boneNum);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
boneName = _binaryReader.readString();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// bind shape
|
|
|
|
_binaryReader.readMatrix(bindShape);
|
|
|
|
int rootIndex = skindata->getSkinBoneNameIndex(boneName);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (rootIndex < 0)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
skindata->addNodeBoneNames(boneName);
|
|
|
|
rootIndex = skindata->getBoneNameIndex(boneName);
|
|
|
|
skindata->nodeBoneOriginMatrices.push_back(bindShape);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
skindata->skinBoneOriginMatrices[rootIndex] = bindShape;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// set root bone index
|
|
|
|
skindata->rootBoneIndex = rootIndex;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// read parent and child relationship map
|
|
|
|
float transform[16];
|
|
|
|
unsigned int linkNum;
|
|
|
|
_binaryReader.read(&linkNum);
|
|
|
|
for (unsigned int i = 0; i < linkNum; ++i)
|
|
|
|
{
|
|
|
|
std::string id = _binaryReader.readString();
|
2021-12-25 10:04:45 +08:00
|
|
|
int index = skindata->getSkinBoneNameIndex(id);
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
std::string parentid = _binaryReader.readString();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (!_binaryReader.readMatrix(transform))
|
|
|
|
{
|
|
|
|
CCLOG("warning: Failed to load SkinData: transform '%s'.", _path.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
if (index < 0)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
skindata->addNodeBoneNames(id);
|
|
|
|
index = skindata->getBoneNameIndex(id);
|
|
|
|
skindata->nodeBoneOriginMatrices.push_back(transform);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
skindata->skinBoneOriginMatrices[index] = transform;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
int parentIndex = skindata->getSkinBoneNameIndex(parentid);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (parentIndex < 0)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
skindata->addNodeBoneNames(parentid);
|
|
|
|
parentIndex = skindata->getBoneNameIndex(parentid);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
skindata->boneChild[parentIndex].push_back(index);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Bundle3D::loadMaterialDataJson_0_1(MaterialDatas& materialdatas)
|
|
|
|
{
|
|
|
|
if (!_jsonReader.HasMember(MATERIAL))
|
|
|
|
return false;
|
|
|
|
NMaterialData materialData;
|
|
|
|
const rapidjson::Value& material_data_array = _jsonReader[MATERIAL];
|
|
|
|
|
|
|
|
if (material_data_array.Size() > 0)
|
|
|
|
{
|
|
|
|
const rapidjson::Value& material_data_array_0 = material_data_array[(rapidjson::SizeType)0];
|
|
|
|
if (material_data_array_0.HasMember(BASE))
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
const rapidjson::Value& material_data_base_array = material_data_array_0[BASE];
|
2019-11-23 20:27:39 +08:00
|
|
|
const rapidjson::Value& material_data_base_array_0 = material_data_base_array[(rapidjson::SizeType)0];
|
2021-12-25 10:04:45 +08:00
|
|
|
NTextureData textureData;
|
2019-11-23 20:27:39 +08:00
|
|
|
// set texture
|
|
|
|
std::string filename = material_data_base_array_0[FILENAME].GetString();
|
|
|
|
textureData.filename = filename.empty() ? filename : _modelPath + filename;
|
2021-12-25 10:04:45 +08:00
|
|
|
textureData.type = NTextureData::Usage::Diffuse;
|
|
|
|
textureData.id = "";
|
2019-11-23 20:27:39 +08:00
|
|
|
materialData.textures.push_back(textureData);
|
|
|
|
materialdatas.materials.push_back(materialData);
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Bundle3D::loadMaterialDataJson_0_2(MaterialDatas& materialdatas)
|
|
|
|
{
|
|
|
|
if (!_jsonReader.HasMember(MATERIAL))
|
|
|
|
return false;
|
|
|
|
NMaterialData materialData;
|
|
|
|
const rapidjson::Value& material_array = _jsonReader[MATERIAL];
|
|
|
|
|
|
|
|
for (rapidjson::SizeType i = 0; i < material_array.Size(); ++i)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
NTextureData textureData;
|
2019-11-23 20:27:39 +08:00
|
|
|
const rapidjson::Value& material_val = material_array[i];
|
|
|
|
|
|
|
|
// set texture
|
|
|
|
std::string filename = material_val[TEXTURES].GetString();
|
|
|
|
textureData.filename = filename.empty() ? filename : _modelPath + filename;
|
2021-12-25 10:04:45 +08:00
|
|
|
textureData.type = NTextureData::Usage::Diffuse;
|
|
|
|
textureData.id = "";
|
2019-11-23 20:27:39 +08:00
|
|
|
materialData.textures.push_back(textureData);
|
|
|
|
}
|
|
|
|
materialdatas.materials.push_back(materialData);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool loadMaterialDataJson(MaterialData* /*materialdata*/)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool loadMaterialDataJson_0_1(MaterialData* /*materialdata*/)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool loadMaterialDataJson_0_2(MaterialData* /*materialdata*/)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Bundle3D::loadAnimationDataJson(const std::string& id, Animation3DData* animationdata)
|
|
|
|
{
|
|
|
|
std::string anim = "";
|
|
|
|
if (_version == "1.2" || _version == "0.2")
|
|
|
|
anim = ANIMATION;
|
|
|
|
else
|
|
|
|
anim = ANIMATIONS;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!_jsonReader.HasMember(anim.c_str()))
|
|
|
|
return false;
|
|
|
|
int the_index = -1;
|
2019-11-23 20:27:39 +08:00
|
|
|
const rapidjson::Value& animation_data_array = _jsonReader[anim.c_str()];
|
|
|
|
|
2021-10-23 23:27:14 +08:00
|
|
|
if (animation_data_array.Size() == 0)
|
|
|
|
return false;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!id.empty())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
for (rapidjson::SizeType i = 0; i < animation_data_array.Size(); ++i)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (animation_data_array[i][ID].GetString() == id)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
the_index = static_cast<int>(i);
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (the_index < 0)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
the_index = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const rapidjson::Value& animation_data_array_val_0 = animation_data_array[(rapidjson::SizeType)the_index];
|
|
|
|
|
|
|
|
animationdata->_totalTime = animation_data_array_val_0[LENGTH].GetFloat();
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
const rapidjson::Value& bones = animation_data_array_val_0[BONES];
|
2021-10-23 23:27:14 +08:00
|
|
|
for (rapidjson::SizeType i = 0; i < bones.Size(); ++i)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
const rapidjson::Value& bone = bones[i];
|
|
|
|
std::string bone_name = bone[BONEID].GetString();
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (bone.HasMember(KEYFRAMES))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
const rapidjson::Value& bone_keyframes = bone[KEYFRAMES];
|
2021-10-23 23:27:14 +08:00
|
|
|
rapidjson::SizeType keyframe_size = bone_keyframes.Size();
|
2019-11-23 20:27:39 +08:00
|
|
|
animationdata->_rotationKeys[bone_name].reserve(keyframe_size);
|
|
|
|
animationdata->_scaleKeys[bone_name].reserve(keyframe_size);
|
|
|
|
animationdata->_translationKeys[bone_name].reserve(keyframe_size);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
for (rapidjson::SizeType j = 0; j < keyframe_size; ++j)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
const rapidjson::Value& bone_keyframe = bone_keyframes[j];
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (bone_keyframe.HasMember(TRANSLATION))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
const rapidjson::Value& bone_keyframe_translation = bone_keyframe[TRANSLATION];
|
|
|
|
float keytime = bone_keyframe[KEYTIME].GetFloat();
|
|
|
|
Vec3 val(bone_keyframe_translation[(rapidjson::SizeType)0].GetFloat(),
|
|
|
|
bone_keyframe_translation[1].GetFloat(), bone_keyframe_translation[2].GetFloat());
|
|
|
|
animationdata->_translationKeys[bone_name].push_back(Animation3DData::Vec3Key(keytime, val));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (bone_keyframe.HasMember(ROTATION))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
const rapidjson::Value& bone_keyframe_rotation = bone_keyframe[ROTATION];
|
|
|
|
float keytime = bone_keyframe[KEYTIME].GetFloat();
|
|
|
|
Quaternion val = Quaternion(
|
|
|
|
bone_keyframe_rotation[(rapidjson::SizeType)0].GetFloat(), bone_keyframe_rotation[1].GetFloat(),
|
|
|
|
bone_keyframe_rotation[2].GetFloat(), bone_keyframe_rotation[3].GetFloat());
|
|
|
|
animationdata->_rotationKeys[bone_name].push_back(Animation3DData::QuatKey(keytime, val));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (bone_keyframe.HasMember(SCALE))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
const rapidjson::Value& bone_keyframe_scale = bone_keyframe[SCALE];
|
|
|
|
float keytime = bone_keyframe[KEYTIME].GetFloat();
|
|
|
|
Vec3 val(bone_keyframe_scale[(rapidjson::SizeType)0].GetFloat(), bone_keyframe_scale[1].GetFloat(),
|
|
|
|
bone_keyframe_scale[2].GetFloat());
|
|
|
|
animationdata->_scaleKeys[bone_name].push_back(Animation3DData::Vec3Key(keytime, val));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Bundle3D::loadAnimationDataBinary(const std::string& id, Animation3DData* animationdata)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
if (_version == "0.1" || _version == "0.2" || _version == "0.3" || _version == "0.4")
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (!seekToFirstType(BUNDLE_TYPE_ANIMATIONS))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// if id is not a null string, we need to add a suffix of "animation" for seeding.
|
|
|
|
std::string id_ = id;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (id != "")
|
|
|
|
id_ = id + "animation";
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (!seekToFirstType(BUNDLE_TYPE_ANIMATIONS, id_))
|
|
|
|
return false;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
unsigned int animNum = 1;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_version == "0.3" || _version == "0.4")
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (!_binaryReader.read(&animNum))
|
|
|
|
{
|
|
|
|
CCLOG("warning: Failed to read AnimationData: animNum '%s'.", _path.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
bool has_found = false;
|
|
|
|
for (unsigned int k = 0; k < animNum; ++k)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
animationdata->resetData();
|
|
|
|
std::string animId = _binaryReader.readString();
|
|
|
|
|
|
|
|
if (!_binaryReader.read(&animationdata->_totalTime))
|
|
|
|
{
|
|
|
|
CCLOG("warning: Failed to read AnimationData: totalTime '%s'.", _path.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int nodeAnimationNum;
|
|
|
|
if (!_binaryReader.read(&nodeAnimationNum))
|
|
|
|
{
|
|
|
|
CCLOG("warning: Failed to read AnimationData: animNum '%s'.", _path.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (unsigned int i = 0; i < nodeAnimationNum; ++i)
|
|
|
|
{
|
|
|
|
std::string boneName = _binaryReader.readString();
|
|
|
|
unsigned int keyframeNum;
|
|
|
|
if (!_binaryReader.read(&keyframeNum))
|
|
|
|
{
|
|
|
|
CCLOG("warning: Failed to read AnimationData: keyframeNum '%s'.", _path.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
animationdata->_rotationKeys[boneName].reserve(keyframeNum);
|
|
|
|
animationdata->_scaleKeys[boneName].reserve(keyframeNum);
|
|
|
|
animationdata->_translationKeys[boneName].reserve(keyframeNum);
|
|
|
|
|
|
|
|
for (unsigned int j = 0; j < keyframeNum; ++j)
|
|
|
|
{
|
|
|
|
float keytime;
|
|
|
|
if (!_binaryReader.read(&keytime))
|
|
|
|
{
|
|
|
|
CCLOG("warning: Failed to read AnimationData: keytime '%s'.", _path.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// transform flag
|
|
|
|
unsigned char transformFlag(0);
|
|
|
|
if (_version != "0.1" && _version != "0.2" && _version != "0.3")
|
|
|
|
{
|
|
|
|
if (!_binaryReader.read(&transformFlag))
|
|
|
|
{
|
|
|
|
CCLOG("warning: Failed to read AnimationData: transformFlag '%s'.", _path.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// rotation
|
|
|
|
bool hasRotate = true;
|
|
|
|
if (_version != "0.1" && _version != "0.2" && _version != "0.3")
|
|
|
|
hasRotate = transformFlag & 0x01;
|
|
|
|
|
|
|
|
if (hasRotate)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Quaternion rotate;
|
2019-11-23 20:27:39 +08:00
|
|
|
if (_binaryReader.read(&rotate, 4, 4) != 4)
|
|
|
|
{
|
|
|
|
CCLOG("warning: Failed to read AnimationData: rotate '%s'.", _path.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
animationdata->_rotationKeys[boneName].push_back(Animation3DData::QuatKey(keytime, rotate));
|
|
|
|
}
|
|
|
|
|
|
|
|
// scale
|
|
|
|
bool hasScale = true;
|
|
|
|
if (_version != "0.1" && _version != "0.2" && _version != "0.3")
|
|
|
|
hasScale = (transformFlag >> 1) & 0x01;
|
|
|
|
|
|
|
|
if (hasScale)
|
|
|
|
{
|
|
|
|
Vec3 scale;
|
|
|
|
if (_binaryReader.read(&scale, 4, 3) != 3)
|
|
|
|
{
|
|
|
|
CCLOG("warning: Failed to read AnimationData: scale '%s'.", _path.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
animationdata->_scaleKeys[boneName].push_back(Animation3DData::Vec3Key(keytime, scale));
|
|
|
|
}
|
|
|
|
|
|
|
|
// translation
|
|
|
|
bool hasTranslation = true;
|
|
|
|
if (_version != "0.1" && _version != "0.2" && _version != "0.3")
|
|
|
|
hasTranslation = (transformFlag >> 2) & 0x01;
|
|
|
|
|
|
|
|
if (hasTranslation)
|
|
|
|
{
|
|
|
|
Vec3 position;
|
|
|
|
if (_binaryReader.read(&position, 4, 3) != 3)
|
|
|
|
{
|
|
|
|
CCLOG("warning: Failed to read AnimationData: position '%s'.", _path.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
animationdata->_translationKeys[boneName].push_back(Animation3DData::Vec3Key(keytime, position));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (id == animId || id.empty())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
has_found = true;
|
|
|
|
break;
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!has_found)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
animationdata->resetData();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Bundle3D::loadNodesJson(NodeDatas& nodedatas)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!_jsonReader.HasMember(NODES))
|
|
|
|
return false;
|
2019-11-23 20:27:39 +08:00
|
|
|
const rapidjson::Value& nodes = _jsonReader[NODES];
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!nodes.IsArray())
|
|
|
|
return false;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
// traverse the nodes again
|
|
|
|
for (rapidjson::SizeType i = 0; i < nodes.Size(); ++i)
|
|
|
|
{
|
|
|
|
const rapidjson::Value& jnode = nodes[i];
|
2021-12-25 10:04:45 +08:00
|
|
|
std::string id = jnode[ID].GetString();
|
|
|
|
NodeData* nodedata = parseNodesRecursivelyJson(jnode, nodes.Size() == 1);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
bool isSkeleton = jnode[SKELETON].GetBool();
|
|
|
|
if (isSkeleton)
|
|
|
|
nodedatas.skeleton.push_back(nodedata);
|
|
|
|
else
|
|
|
|
nodedatas.nodes.push_back(nodedata);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
NodeData* Bundle3D::parseNodesRecursivelyJson(const rapidjson::Value& jvalue, bool singleSprite)
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
NodeData* nodedata = new NodeData();
|
2019-11-23 20:27:39 +08:00
|
|
|
// id
|
|
|
|
nodedata->id = jvalue[ID].GetString();
|
|
|
|
|
|
|
|
// transform
|
|
|
|
Mat4 transform;
|
|
|
|
const rapidjson::Value& jtransform = jvalue[TRANSFORM];
|
|
|
|
|
|
|
|
for (rapidjson::SizeType j = 0; j < jtransform.Size(); ++j)
|
|
|
|
{
|
|
|
|
transform.m[j] = jtransform[j].GetFloat();
|
|
|
|
}
|
|
|
|
|
|
|
|
nodedata->transform = transform;
|
|
|
|
|
|
|
|
bool isSkin = false;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// parts
|
|
|
|
if (jvalue.HasMember(PARTS))
|
|
|
|
{
|
|
|
|
const rapidjson::Value& parts = jvalue[PARTS];
|
|
|
|
|
|
|
|
for (rapidjson::SizeType i = 0; i < parts.Size(); ++i)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto modelnodedata = new ModelData();
|
2019-11-23 20:27:39 +08:00
|
|
|
const rapidjson::Value& part = parts[i];
|
2021-12-25 10:04:45 +08:00
|
|
|
modelnodedata->subMeshId = part[MESHPARTID].GetString();
|
|
|
|
modelnodedata->materialId = part[MATERIALID].GetString();
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
if (modelnodedata->subMeshId == "" || modelnodedata->materialId == "")
|
|
|
|
{
|
|
|
|
CCLOG("warning: Node %s part is missing meshPartId or materialId", nodedata->id.c_str());
|
|
|
|
CC_SAFE_DELETE(modelnodedata);
|
|
|
|
CC_SAFE_DELETE(nodedata);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (part.HasMember(BONES))
|
|
|
|
{
|
|
|
|
const rapidjson::Value& bones = part[BONES];
|
|
|
|
|
|
|
|
for (rapidjson::SizeType j = 0; j < bones.Size(); ++j)
|
|
|
|
{
|
|
|
|
const rapidjson::Value& bone = bones[j];
|
|
|
|
|
|
|
|
// node
|
|
|
|
if (!bone.HasMember(NODE))
|
|
|
|
{
|
|
|
|
CCLOG("warning: Bone node ID missing");
|
|
|
|
CC_SAFE_DELETE(modelnodedata);
|
|
|
|
CC_SAFE_DELETE(nodedata);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
modelnodedata->bones.push_back(bone[NODE].GetString());
|
|
|
|
|
|
|
|
Mat4 invbindpos;
|
|
|
|
const rapidjson::Value& jinvbindpos = bone[TRANSFORM];
|
|
|
|
|
|
|
|
for (rapidjson::SizeType k = 0; k < jinvbindpos.Size(); ++k)
|
|
|
|
{
|
|
|
|
invbindpos.m[k] = jinvbindpos[k].GetFloat();
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
// invbindpos.inverse();
|
2019-11-23 20:27:39 +08:00
|
|
|
modelnodedata->invBindPose.push_back(invbindpos);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (bones.Size() > 0)
|
|
|
|
isSkin = true;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
nodedata->modelNodeDatas.push_back(modelnodedata);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// set transform
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_version == "0.1" || _version == "0.2" || _version == "0.3" || _version == "0.4" || _version == "0.5" ||
|
|
|
|
_version == "0.6")
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (isSkin || singleSprite)
|
|
|
|
{
|
|
|
|
nodedata->transform = Mat4::IDENTITY;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nodedata->transform = transform;
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
nodedata->transform = transform;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (jvalue.HasMember(CHILDREN))
|
|
|
|
{
|
|
|
|
const rapidjson::Value& children = jvalue[CHILDREN];
|
2021-12-25 10:04:45 +08:00
|
|
|
for (rapidjson::SizeType i = 0; i < children.Size(); ++i)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
const rapidjson::Value& child = children[i];
|
|
|
|
|
|
|
|
NodeData* tempdata = parseNodesRecursivelyJson(child, singleSprite);
|
|
|
|
nodedata->children.push_back(tempdata);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nodedata;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Bundle3D::loadNodesBinary(NodeDatas& nodedatas)
|
|
|
|
{
|
|
|
|
if (!seekToFirstType(BUNDLE_TYPE_NODE))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
unsigned int nodeSize = 0;
|
|
|
|
if (_binaryReader.read(&nodeSize, 4, 1) != 1)
|
|
|
|
{
|
|
|
|
CCLOG("warning: Failed to read nodes");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// traverse the nodes again
|
|
|
|
for (rapidjson::SizeType i = 0; i < nodeSize; ++i)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
bool skeleton = false;
|
2019-11-23 20:27:39 +08:00
|
|
|
NodeData* nodedata = parseNodesRecursivelyBinary(skeleton, nodeSize == 1);
|
|
|
|
|
|
|
|
if (skeleton)
|
|
|
|
nodedatas.skeleton.push_back(nodedata);
|
|
|
|
else
|
|
|
|
nodedatas.nodes.push_back(nodedata);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
NodeData* Bundle3D::parseNodesRecursivelyBinary(bool& skeleton, bool singleSprite)
|
|
|
|
{
|
|
|
|
// id
|
|
|
|
std::string id = _binaryReader.readString();
|
|
|
|
// is skeleton
|
|
|
|
bool skeleton_;
|
|
|
|
if (_binaryReader.read(&skeleton_, 1, 1) != 1)
|
|
|
|
{
|
|
|
|
CCLOG("warning: Failed to read is skeleton");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (skeleton_)
|
|
|
|
skeleton = true;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// transform
|
|
|
|
Mat4 transform;
|
|
|
|
if (!_binaryReader.readMatrix(transform.m))
|
|
|
|
{
|
|
|
|
CCLOG("warning: Failed to read transform matrix");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
// parts
|
|
|
|
unsigned int partsSize = 0;
|
|
|
|
if (_binaryReader.read(&partsSize, 4, 1) != 1)
|
|
|
|
{
|
|
|
|
CCLOG("warning: Failed to read meshdata: attribCount '%s'.", _path.c_str());
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-12-08 00:11:53 +08:00
|
|
|
NodeData* nodedata = new NodeData();
|
2021-12-25 10:04:45 +08:00
|
|
|
nodedata->id = id;
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
bool isSkin = false;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (partsSize > 0)
|
|
|
|
{
|
|
|
|
for (unsigned int i = 0; i < partsSize; ++i)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto modelnodedata = new ModelData();
|
|
|
|
modelnodedata->subMeshId = _binaryReader.readString();
|
2019-11-23 20:27:39 +08:00
|
|
|
modelnodedata->materialId = _binaryReader.readString();
|
|
|
|
|
2021-10-11 12:15:41 +08:00
|
|
|
if (modelnodedata->subMeshId.empty() || modelnodedata->materialId.empty())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
CCLOG("Node %s part is missing meshPartId or materialId", nodedata->id.c_str());
|
|
|
|
CC_SAFE_DELETE(modelnodedata);
|
|
|
|
CC_SAFE_DELETE(nodedata);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// read bone
|
|
|
|
unsigned int bonesSize = 0;
|
|
|
|
if (_binaryReader.read(&bonesSize, 4, 1) != 1)
|
|
|
|
{
|
|
|
|
CCLOG("warning: Failed to read meshdata: attribCount '%s'.", _path.c_str());
|
|
|
|
CC_SAFE_DELETE(modelnodedata);
|
|
|
|
CC_SAFE_DELETE(nodedata);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bonesSize > 0)
|
|
|
|
{
|
|
|
|
for (unsigned int j = 0; j < bonesSize; ++j)
|
|
|
|
{
|
|
|
|
std::string name = _binaryReader.readString();
|
|
|
|
modelnodedata->bones.push_back(name);
|
|
|
|
|
|
|
|
Mat4 invbindpos;
|
|
|
|
if (!_binaryReader.readMatrix(invbindpos.m))
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(modelnodedata);
|
|
|
|
CC_SAFE_DELETE(nodedata);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
modelnodedata->invBindPose.push_back(invbindpos);
|
|
|
|
}
|
|
|
|
isSkin = true;
|
|
|
|
}
|
|
|
|
unsigned int uvMapping = 0;
|
|
|
|
if (_binaryReader.read(&uvMapping, 4, 1) != 1)
|
|
|
|
{
|
|
|
|
CCLOG("warning: Failed to read nodedata: uvMapping '%s'.", _path.c_str());
|
|
|
|
CC_SAFE_DELETE(modelnodedata);
|
|
|
|
CC_SAFE_DELETE(nodedata);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
for (unsigned int j = 0; j < uvMapping; ++j)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
unsigned int textureIndexSize = 0;
|
2019-11-23 20:27:39 +08:00
|
|
|
if (_binaryReader.read(&textureIndexSize, 4, 1) != 1)
|
|
|
|
{
|
|
|
|
CCLOG("warning: Failed to read meshdata: attribCount '%s'.", _path.c_str());
|
|
|
|
CC_SAFE_DELETE(modelnodedata);
|
|
|
|
CC_SAFE_DELETE(nodedata);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
for (unsigned int k = 0; k < textureIndexSize; ++k)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
unsigned int index = 0;
|
2019-11-23 20:27:39 +08:00
|
|
|
if (_binaryReader.read(&index, 4, 1) != 1)
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(modelnodedata);
|
|
|
|
CC_SAFE_DELETE(nodedata);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nodedata->modelNodeDatas.push_back(modelnodedata);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// set transform
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_version == "0.1" || _version == "0.2" || _version == "0.3" || _version == "0.4" || _version == "0.5" ||
|
|
|
|
_version == "0.6")
|
|
|
|
{
|
|
|
|
if (isSkin || singleSprite)
|
|
|
|
{
|
|
|
|
nodedata->transform = Mat4::IDENTITY;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nodedata->transform = transform;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nodedata->transform = transform;
|
|
|
|
}
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
unsigned int childrenSize = 0;
|
|
|
|
if (_binaryReader.read(&childrenSize, 4, 1) != 1)
|
|
|
|
{
|
|
|
|
CCLOG("warning: Failed to read meshdata: attribCount '%s'.", _path.c_str());
|
|
|
|
CC_SAFE_DELETE(nodedata);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (childrenSize > 0)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
for (unsigned int i = 0; i < childrenSize; ++i)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
NodeData* tempdata = parseNodesRecursivelyBinary(skeleton, singleSprite);
|
|
|
|
nodedata->children.push_back(tempdata);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nodedata;
|
|
|
|
}
|
|
|
|
|
|
|
|
backend::VertexFormat Bundle3D::parseGLDataType(const std::string& str, int size)
|
|
|
|
{
|
|
|
|
backend::VertexFormat ret = backend::VertexFormat::INT;
|
|
|
|
if (str == "GL_BYTE")
|
|
|
|
{
|
|
|
|
switch (size)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
case 4:
|
2019-11-23 20:27:39 +08:00
|
|
|
return backend::VertexFormat::UBYTE4;
|
|
|
|
default:
|
|
|
|
CCLOGERROR("parseVertexType GL_BYTE x %d error", size);
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else if (str == "GL_UNSIGNED_BYTE")
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
switch (size)
|
|
|
|
{
|
|
|
|
case 4:
|
|
|
|
return backend::VertexFormat::UBYTE4;
|
|
|
|
default:
|
|
|
|
CCLOGERROR("parseVertexType GL_UNSIGNED_BYTE x %d error", size);
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else if (str == "GL_SHORT")
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
switch (size)
|
|
|
|
{
|
|
|
|
case 2:
|
|
|
|
return backend::VertexFormat::USHORT2;
|
|
|
|
case 4:
|
|
|
|
return backend::VertexFormat::USHORT4;
|
|
|
|
default:
|
|
|
|
CCLOGERROR("parseVertexType GL_SHORT x %d error", size);
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else if (str == "GL_UNSIGNED_SHORT")
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
switch (size)
|
|
|
|
{
|
|
|
|
case 2:
|
|
|
|
return backend::VertexFormat::USHORT2;
|
|
|
|
case 4:
|
|
|
|
return backend::VertexFormat::USHORT4;
|
|
|
|
default:
|
|
|
|
CCLOGERROR("parseVertexType GL_UNSIGNED_SHORT x %d error", size);
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else if (str == "GL_INT")
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
switch (size)
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
return backend::VertexFormat::INT;
|
|
|
|
case 2:
|
|
|
|
return backend::VertexFormat::INT2;
|
|
|
|
case 3:
|
|
|
|
return backend::VertexFormat::INT3;
|
|
|
|
case 4:
|
|
|
|
return backend::VertexFormat::INT4;
|
|
|
|
default:
|
|
|
|
CCLOGERROR("parseVertexType GL_INT x %d error", size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (str == "GL_UNSIGNED_INT")
|
|
|
|
{
|
|
|
|
switch (size)
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
return backend::VertexFormat::INT;
|
|
|
|
case 2:
|
|
|
|
return backend::VertexFormat::INT2;
|
|
|
|
case 3:
|
|
|
|
return backend::VertexFormat::INT3;
|
|
|
|
case 4:
|
|
|
|
return backend::VertexFormat::INT4;
|
|
|
|
default:
|
|
|
|
CCLOGERROR("parseVertexType GL_UNSIGNED_INT x %d error", size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (str == "GL_FLOAT")
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
switch (size)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
return backend::VertexFormat::FLOAT;
|
|
|
|
case 2:
|
|
|
|
return backend::VertexFormat::FLOAT2;
|
|
|
|
case 3:
|
|
|
|
return backend::VertexFormat::FLOAT3;
|
|
|
|
case 4:
|
|
|
|
return backend::VertexFormat::FLOAT4;
|
|
|
|
default:
|
|
|
|
CCLOGERROR("parseVertexType GL_UNSIGNED_INT x %d error", size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CCASSERT(false, "parseVertexType failed!");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
backend::SamplerAddressMode Bundle3D::parseSamplerAddressMode(const std::string& str)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
if (str == "REPEAT")
|
|
|
|
{
|
|
|
|
return backend::SamplerAddressMode::REPEAT;
|
|
|
|
}
|
|
|
|
else if (str == "CLAMP")
|
|
|
|
{
|
|
|
|
return backend::SamplerAddressMode::CLAMP_TO_EDGE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CCASSERT(false, "Invalid GL type");
|
|
|
|
return backend::SamplerAddressMode::REPEAT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NTextureData::Usage Bundle3D::parseGLTextureType(const std::string& str)
|
|
|
|
{
|
|
|
|
if (str == "AMBIENT")
|
|
|
|
{
|
|
|
|
return NTextureData::Usage::Ambient;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else if (str == "BUMP")
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return NTextureData::Usage::Bump;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else if (str == "DIFFUSE")
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return NTextureData::Usage::Diffuse;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else if (str == "EMISSIVE")
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return NTextureData::Usage::Emissive;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else if (str == "NONE")
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return NTextureData::Usage::None;
|
|
|
|
}
|
|
|
|
else if (str == "NORMAL")
|
|
|
|
{
|
|
|
|
return NTextureData::Usage::Normal;
|
|
|
|
}
|
|
|
|
else if (str == "REFLECTION")
|
|
|
|
{
|
|
|
|
return NTextureData::Usage::Reflection;
|
|
|
|
}
|
|
|
|
else if (str == "SHININESS")
|
|
|
|
{
|
|
|
|
return NTextureData::Usage::Shininess;
|
|
|
|
}
|
|
|
|
else if (str == "SPECULAR")
|
|
|
|
{
|
|
|
|
return NTextureData::Usage::Specular;
|
|
|
|
}
|
|
|
|
else if (str == "TRANSPARENCY")
|
|
|
|
{
|
|
|
|
return NTextureData::Usage::Transparency;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CCASSERT(false, "Wrong Texture type");
|
|
|
|
return NTextureData::Usage::Unknown;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
shaderinfos::VertexKey Bundle3D::parseGLProgramAttribute(const std::string& str)
|
|
|
|
{
|
|
|
|
if (str == "VERTEX_ATTRIB_POSITION")
|
|
|
|
{
|
|
|
|
return shaderinfos::VertexKey::VERTEX_ATTRIB_POSITION;
|
|
|
|
}
|
|
|
|
else if (str == "VERTEX_ATTRIB_COLOR")
|
|
|
|
{
|
|
|
|
return shaderinfos::VertexKey::VERTEX_ATTRIB_COLOR;
|
|
|
|
}
|
|
|
|
else if (str == "VERTEX_ATTRIB_TEX_COORD")
|
|
|
|
{
|
|
|
|
return shaderinfos::VertexKey::VERTEX_ATTRIB_TEX_COORD;
|
|
|
|
}
|
|
|
|
else if (str == "VERTEX_ATTRIB_TEX_COORD1")
|
|
|
|
{
|
|
|
|
return shaderinfos::VertexKey::VERTEX_ATTRIB_TEX_COORD1;
|
|
|
|
}
|
|
|
|
else if (str == "VERTEX_ATTRIB_TEX_COORD2")
|
|
|
|
{
|
|
|
|
return shaderinfos::VertexKey::VERTEX_ATTRIB_TEX_COORD2;
|
|
|
|
}
|
|
|
|
else if (str == "VERTEX_ATTRIB_TEX_COORD3")
|
|
|
|
{
|
|
|
|
return shaderinfos::VertexKey::VERTEX_ATTRIB_TEX_COORD3;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
// comment out them
|
|
|
|
// else if (str == "VERTEX_ATTRIB_TEX_COORD4")
|
|
|
|
// {
|
|
|
|
// return shaderinfos::VertexKey::VERTEX_ATTRIB_TEX_COORD4;
|
|
|
|
// }
|
|
|
|
// else if (str == "VERTEX_ATTRIB_TEX_COORD5")
|
|
|
|
// {
|
|
|
|
// return shaderinfos::VertexKey::VERTEX_ATTRIB_TEX_COORD5;
|
|
|
|
// }
|
|
|
|
// else if (str == "VERTEX_ATTRIB_TEX_COORD6")
|
|
|
|
// {
|
|
|
|
// return shaderinfos::VertexKey::VERTEX_ATTRIB_TEX_COORD6;
|
|
|
|
// }
|
|
|
|
// else if (str == "VERTEX_ATTRIB_TEX_COORD7")
|
|
|
|
// {
|
|
|
|
// return shaderinfos::VertexKey::VERTEX_ATTRIB_TEX_COORD7;
|
|
|
|
// }
|
2019-11-23 20:27:39 +08:00
|
|
|
else if (str == "VERTEX_ATTRIB_NORMAL")
|
|
|
|
{
|
|
|
|
return shaderinfos::VertexKey::VERTEX_ATTRIB_NORMAL;
|
|
|
|
}
|
|
|
|
else if (str == "VERTEX_ATTRIB_BLEND_WEIGHT")
|
|
|
|
{
|
|
|
|
return shaderinfos::VertexKey::VERTEX_ATTRIB_BLEND_WEIGHT;
|
|
|
|
}
|
|
|
|
else if (str == "VERTEX_ATTRIB_BLEND_INDEX")
|
|
|
|
{
|
|
|
|
return shaderinfos::VertexKey::VERTEX_ATTRIB_BLEND_INDEX;
|
|
|
|
}
|
|
|
|
else if (str == "VERTEX_ATTRIB_TANGENT")
|
|
|
|
{
|
|
|
|
return shaderinfos::VertexKey::VERTEX_ATTRIB_TANGENT;
|
|
|
|
}
|
|
|
|
else if (str == "VERTEX_ATTRIB_BINORMAL")
|
|
|
|
{
|
|
|
|
return shaderinfos::VertexKey::VERTEX_ATTRIB_BINORMAL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CCASSERT(false, "Wrong Attribute type");
|
|
|
|
return shaderinfos::VertexKey::VERTEX_ATTRIB_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Bundle3D::getModelRelativePath(const std::string& path)
|
|
|
|
{
|
|
|
|
ssize_t index = path.find_last_of('/');
|
|
|
|
std::string fullModelPath;
|
|
|
|
_modelPath = path.substr(0, index + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
Reference* Bundle3D::seekToFirstType(unsigned int type, const std::string& id)
|
|
|
|
{
|
|
|
|
// for each Reference
|
|
|
|
for (unsigned int i = 0; i < _referenceCount; ++i)
|
|
|
|
{
|
|
|
|
Reference* ref = &_references[i];
|
|
|
|
if (ref->type == type)
|
|
|
|
{
|
|
|
|
// if id is not a null string, we also need to check the Reference's id.
|
|
|
|
if (id != "" && id != ref->id)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// Found a match
|
|
|
|
if (_binaryReader.seek(ref->offset, SEEK_SET) == false)
|
|
|
|
{
|
|
|
|
CCLOG("warning: Failed to seek to object '%s' in bundle '%s'.", ref->id.c_str(), _path.c_str());
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return ref;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<Vec3> Bundle3D::getTrianglesList(const std::string& path)
|
|
|
|
{
|
|
|
|
std::vector<Vec3> trianglesList;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (path.length() <= 4)
|
|
|
|
return trianglesList;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
auto bundle = Bundle3D::createBundle();
|
2019-11-23 20:27:39 +08:00
|
|
|
std::string ext = FileUtils::getInstance()->getFileExtension(path);
|
|
|
|
MeshDatas meshs;
|
|
|
|
if (ext == ".obj")
|
|
|
|
{
|
|
|
|
MaterialDatas materials;
|
|
|
|
NodeDatas nodes;
|
|
|
|
if (!Bundle3D::loadObj(meshs, materials, nodes, path))
|
|
|
|
{
|
|
|
|
Bundle3D::destroyBundle(bundle);
|
|
|
|
return trianglesList;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!bundle->load(path))
|
|
|
|
{
|
|
|
|
Bundle3D::destroyBundle(bundle);
|
|
|
|
return trianglesList;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
bundle->loadMeshDatas(meshs);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
Bundle3D::destroyBundle(bundle);
|
2021-12-25 10:04:45 +08:00
|
|
|
for (auto iter : meshs.meshDatas)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
int preVertexSize = iter->getPerVertexSize() / sizeof(float);
|
2021-12-25 10:04:45 +08:00
|
|
|
for (const auto& indexArray : iter->subMeshIndices)
|
|
|
|
{
|
|
|
|
for (auto i : indexArray)
|
|
|
|
{
|
|
|
|
trianglesList.push_back(Vec3(iter->vertex[i * preVertexSize], iter->vertex[i * preVertexSize + 1],
|
|
|
|
iter->vertex[i * preVertexSize + 2]));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return trianglesList;
|
|
|
|
}
|
|
|
|
|
|
|
|
Bundle3D::Bundle3D()
|
2021-12-25 10:04:45 +08:00
|
|
|
: _modelPath(""), _path(""), _version(""), _referenceCount(0), _references(nullptr), _isBinary(false)
|
|
|
|
{}
|
2019-11-23 20:27:39 +08:00
|
|
|
Bundle3D::~Bundle3D()
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
cocos2d::AABB Bundle3D::calculateAABB(const std::vector<float>& vertex,
|
|
|
|
int stride,
|
|
|
|
const std::vector<unsigned short>& index)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
AABB aabb;
|
|
|
|
stride /= 4;
|
|
|
|
for (const auto& it : index)
|
|
|
|
{
|
|
|
|
Vec3 point(vertex[it * stride], vertex[it * stride + 1], vertex[it * stride + 2]);
|
|
|
|
aabb.updateMinMax(&point, 1);
|
|
|
|
}
|
|
|
|
return aabb;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|