2014-05-19 05:49:16 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2014 Chukong Technologies Inc.
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "3d/CCSprite3D.h"
|
|
|
|
#include "3d/CCObjLoader.h"
|
2014-06-04 18:17:09 +08:00
|
|
|
#include "3d/CCMeshSkin.h"
|
2014-06-05 16:36:01 +08:00
|
|
|
#include "3d/CCBundle3D.h"
|
2014-06-16 20:58:13 +08:00
|
|
|
#include "3d/CCSprite3DMaterial.h"
|
2014-07-29 10:49:06 +08:00
|
|
|
#include "3d/CCAttachNode.h"
|
2014-08-29 15:39:52 +08:00
|
|
|
#include "3d/CCMesh.h"
|
2014-05-19 05:49:16 +08:00
|
|
|
|
|
|
|
#include "base/CCDirector.h"
|
2014-12-03 17:40:27 +08:00
|
|
|
#include "base/CCAsyncTaskPool.h"
|
2014-10-20 16:25:24 +08:00
|
|
|
#include "2d/CCLight.h"
|
|
|
|
#include "2d/CCCamera.h"
|
2014-05-19 05:49:16 +08:00
|
|
|
#include "base/ccMacros.h"
|
2014-09-28 16:02:12 +08:00
|
|
|
#include "platform/CCPlatformMacros.h"
|
2014-05-19 05:49:16 +08:00
|
|
|
#include "platform/CCFileUtils.h"
|
|
|
|
#include "renderer/CCTextureCache.h"
|
|
|
|
#include "renderer/CCRenderer.h"
|
|
|
|
#include "renderer/CCGLProgramState.h"
|
|
|
|
#include "renderer/CCGLProgramCache.h"
|
2015-05-06 04:07:32 +08:00
|
|
|
#include "renderer/CCMaterial.h"
|
|
|
|
#include "renderer/CCTechnique.h"
|
|
|
|
#include "renderer/CCPass.h"
|
2014-05-19 05:49:16 +08:00
|
|
|
|
|
|
|
#include "deprecated/CCString.h" // For StringUtils::format
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2015-05-12 12:31:33 +08:00
|
|
|
static GLProgramState* getGLProgramStateForAttribs(MeshVertexData* meshVertexData, bool usesLight);
|
2014-05-19 05:49:16 +08:00
|
|
|
|
2015-01-14 08:41:24 +08:00
|
|
|
Sprite3D* Sprite3D::create()
|
|
|
|
{
|
|
|
|
//
|
|
|
|
auto sprite = new (std::nothrow) Sprite3D();
|
|
|
|
if (sprite && sprite->init())
|
|
|
|
{
|
|
|
|
sprite->autorelease();
|
|
|
|
return sprite;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(sprite);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-05-12 12:31:33 +08:00
|
|
|
Sprite3D* Sprite3D::create(const std::string& modelPath)
|
2014-05-19 05:49:16 +08:00
|
|
|
{
|
2015-05-06 04:07:32 +08:00
|
|
|
CCASSERT(modelPath.length() >= 4, "invalid filename for Sprite3D");
|
2014-05-19 05:49:16 +08:00
|
|
|
|
2014-08-28 07:31:57 +08:00
|
|
|
auto sprite = new (std::nothrow) Sprite3D();
|
2014-05-19 05:49:16 +08:00
|
|
|
if (sprite && sprite->initWithFile(modelPath))
|
|
|
|
{
|
2014-09-09 17:47:34 +08:00
|
|
|
sprite->_contentSize = sprite->getBoundingBox().size;
|
2014-05-19 05:49:16 +08:00
|
|
|
sprite->autorelease();
|
|
|
|
return sprite;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(sprite);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-05-12 12:31:33 +08:00
|
|
|
Sprite3D* Sprite3D::create(const std::string& modelPath, const std::string& texturePath)
|
2014-05-19 05:49:16 +08:00
|
|
|
{
|
|
|
|
auto sprite = create(modelPath);
|
|
|
|
if (sprite)
|
|
|
|
{
|
|
|
|
sprite->setTexture(texturePath);
|
|
|
|
}
|
|
|
|
|
|
|
|
return sprite;
|
|
|
|
}
|
|
|
|
|
2015-05-12 12:31:33 +08:00
|
|
|
void Sprite3D::createAsync(const std::string& modelPath, const std::function<void(Sprite3D*, void*)>& callback, void* callbackparam)
|
2014-12-03 17:40:27 +08:00
|
|
|
{
|
2014-12-05 15:01:30 +08:00
|
|
|
createAsync(modelPath, "", callback, callbackparam);
|
2014-12-03 17:40:27 +08:00
|
|
|
}
|
|
|
|
|
2015-05-12 12:31:33 +08:00
|
|
|
void Sprite3D::createAsync(const std::string& modelPath, const std::string& texturePath, const std::function<void(Sprite3D*, void*)>& callback, void* callbackparam)
|
2014-12-03 17:40:27 +08:00
|
|
|
{
|
|
|
|
Sprite3D *sprite = new (std::nothrow) Sprite3D();
|
|
|
|
if (sprite->loadFromCache(modelPath))
|
|
|
|
{
|
|
|
|
sprite->autorelease();
|
|
|
|
if (!texturePath.empty())
|
|
|
|
sprite->setTexture(texturePath);
|
2014-12-05 15:01:30 +08:00
|
|
|
callback(sprite, callbackparam);
|
2014-12-03 17:40:27 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sprite->_asyncLoadParam.afterLoadCallback = callback;
|
2014-12-04 18:31:10 +08:00
|
|
|
sprite->_asyncLoadParam.texPath = texturePath;
|
2014-12-22 17:40:02 +08:00
|
|
|
sprite->_asyncLoadParam.modlePath = modelPath;
|
2014-12-05 15:01:30 +08:00
|
|
|
sprite->_asyncLoadParam.callbackParam = callbackparam;
|
2014-12-04 18:31:10 +08:00
|
|
|
sprite->_asyncLoadParam.materialdatas = new (std::nothrow) MaterialDatas();
|
|
|
|
sprite->_asyncLoadParam.meshdatas = new (std::nothrow) MeshDatas();
|
|
|
|
sprite->_asyncLoadParam.nodeDatas = new (std::nothrow) NodeDatas();
|
2014-12-05 15:48:46 +08:00
|
|
|
AsyncTaskPool::getInstance()->enqueue(AsyncTaskPool::TaskType::TASK_IO, CC_CALLBACK_1(Sprite3D::afterAsyncLoad, sprite), (void*)(&sprite->_asyncLoadParam), [sprite]()
|
2014-12-03 17:40:27 +08:00
|
|
|
{
|
2014-12-22 17:40:02 +08:00
|
|
|
sprite->_asyncLoadParam.result = sprite->loadFromFile(sprite->_asyncLoadParam.modlePath, sprite->_asyncLoadParam.nodeDatas, sprite->_asyncLoadParam.meshdatas, sprite->_asyncLoadParam.materialdatas);
|
2014-12-05 15:48:46 +08:00
|
|
|
});
|
2014-12-03 17:40:27 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-12-04 18:31:10 +08:00
|
|
|
void Sprite3D::afterAsyncLoad(void* param)
|
2014-12-03 17:40:27 +08:00
|
|
|
{
|
2014-12-04 18:31:10 +08:00
|
|
|
Sprite3D::AsyncLoadParam* asyncParam = (Sprite3D::AsyncLoadParam*)param;
|
2014-12-05 15:01:30 +08:00
|
|
|
autorelease();
|
2014-12-04 18:31:10 +08:00
|
|
|
if (asyncParam)
|
2014-12-03 17:40:27 +08:00
|
|
|
{
|
2014-12-04 18:31:10 +08:00
|
|
|
if (asyncParam->result)
|
2014-12-03 17:40:27 +08:00
|
|
|
{
|
|
|
|
_meshes.clear();
|
|
|
|
_meshVertexDatas.clear();
|
|
|
|
CC_SAFE_RELEASE_NULL(_skeleton);
|
|
|
|
removeAllAttachNode();
|
|
|
|
|
|
|
|
//create in the main thread
|
2014-12-04 18:31:10 +08:00
|
|
|
auto& meshdatas = asyncParam->meshdatas;
|
|
|
|
auto& materialdatas = asyncParam->materialdatas;
|
|
|
|
auto& nodeDatas = asyncParam->nodeDatas;
|
2014-12-03 17:40:27 +08:00
|
|
|
if (initFrom(*nodeDatas, *meshdatas, *materialdatas))
|
|
|
|
{
|
2014-12-22 17:40:02 +08:00
|
|
|
auto spritedata = Sprite3DCache::getInstance()->getSpriteData(asyncParam->modlePath);
|
2014-12-05 15:01:30 +08:00
|
|
|
if (spritedata == nullptr)
|
|
|
|
{
|
|
|
|
//add to cache
|
|
|
|
auto data = new (std::nothrow) Sprite3DCache::Sprite3DData();
|
|
|
|
data->materialdatas = materialdatas;
|
|
|
|
data->nodedatas = nodeDatas;
|
|
|
|
data->meshVertexDatas = _meshVertexDatas;
|
|
|
|
for (const auto mesh : _meshes) {
|
|
|
|
data->glProgramStates.pushBack(mesh->getGLProgramState());
|
|
|
|
}
|
|
|
|
|
2014-12-22 17:40:02 +08:00
|
|
|
Sprite3DCache::getInstance()->addSprite3DData(asyncParam->modlePath, data);
|
2015-03-24 10:22:55 +08:00
|
|
|
|
|
|
|
CC_SAFE_DELETE(meshdatas);
|
2014-12-05 15:01:30 +08:00
|
|
|
materialdatas = nullptr;
|
|
|
|
nodeDatas = nullptr;
|
2014-12-03 17:40:27 +08:00
|
|
|
}
|
|
|
|
}
|
2015-03-24 10:22:55 +08:00
|
|
|
CC_SAFE_DELETE(meshdatas);
|
|
|
|
CC_SAFE_DELETE(materialdatas);
|
|
|
|
CC_SAFE_DELETE(nodeDatas);
|
2014-12-04 18:31:10 +08:00
|
|
|
|
|
|
|
if (asyncParam->texPath != "")
|
|
|
|
{
|
|
|
|
setTexture(asyncParam->texPath);
|
|
|
|
}
|
2014-12-03 17:40:27 +08:00
|
|
|
}
|
2014-12-25 17:40:26 +08:00
|
|
|
else
|
|
|
|
{
|
2014-12-25 17:42:58 +08:00
|
|
|
CCLOG("file load failed: %s ", asyncParam->modlePath.c_str());
|
2014-12-25 17:40:26 +08:00
|
|
|
}
|
2014-12-05 15:01:30 +08:00
|
|
|
asyncParam->afterLoadCallback(this, asyncParam->callbackParam);
|
2014-12-03 17:40:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-29 10:49:06 +08:00
|
|
|
bool Sprite3D::loadFromCache(const std::string& path)
|
2014-05-19 05:49:16 +08:00
|
|
|
{
|
2014-08-19 11:56:09 +08:00
|
|
|
auto spritedata = Sprite3DCache::getInstance()->getSpriteData(path);
|
|
|
|
if (spritedata)
|
|
|
|
{
|
2014-08-22 13:25:26 +08:00
|
|
|
for (auto it : spritedata->meshVertexDatas) {
|
|
|
|
_meshVertexDatas.pushBack(it);
|
2014-08-19 11:56:09 +08:00
|
|
|
}
|
|
|
|
_skeleton = Skeleton3D::create(spritedata->nodedatas->skeleton);
|
|
|
|
CC_SAFE_RETAIN(_skeleton);
|
|
|
|
|
|
|
|
for(const auto& it : spritedata->nodedatas->nodes)
|
|
|
|
{
|
|
|
|
if(it)
|
|
|
|
{
|
|
|
|
createNode(it, this, *(spritedata->materialdatas), spritedata->nodedatas->nodes.size() == 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-24 15:52:24 +08:00
|
|
|
for(const auto& it : spritedata->nodedatas->skeleton)
|
|
|
|
{
|
|
|
|
if(it)
|
|
|
|
{
|
|
|
|
createAttachSprite3DNode(it,*(spritedata->materialdatas));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-17 13:42:08 +08:00
|
|
|
for (ssize_t i = 0; i < _meshes.size(); i++) {
|
2015-05-06 04:07:32 +08:00
|
|
|
// cloning is needed in order to have one state per sprite
|
|
|
|
auto glstate = spritedata->glProgramStates.at(i);
|
|
|
|
_meshes.at(i)->setGLProgramState(glstate->clone());
|
2014-09-17 13:42:08 +08:00
|
|
|
}
|
2014-08-19 11:56:09 +08:00
|
|
|
return true;
|
|
|
|
}
|
2014-06-16 20:58:13 +08:00
|
|
|
|
2014-07-29 10:49:06 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-12-03 17:40:27 +08:00
|
|
|
bool Sprite3D::loadFromFile(const std::string& path, NodeDatas* nodedatas, MeshDatas* meshdatas, MaterialDatas* materialdatas)
|
2014-07-29 10:49:06 +08:00
|
|
|
{
|
|
|
|
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(path);
|
|
|
|
|
2014-12-03 17:40:27 +08:00
|
|
|
std::string ext = path.substr(path.length() - 4, 4);
|
|
|
|
std::transform(ext.begin(), ext.end(), ext.begin(), tolower);
|
|
|
|
if (ext == ".obj")
|
2014-05-19 05:49:16 +08:00
|
|
|
{
|
2014-12-03 17:40:27 +08:00
|
|
|
return Bundle3D::loadObj(*meshdatas, *materialdatas, *nodedatas, fullPath);
|
2014-05-19 05:49:16 +08:00
|
|
|
}
|
2014-12-03 17:40:27 +08:00
|
|
|
else if (ext == ".c3b" || ext == ".c3t")
|
2014-08-15 17:46:21 +08:00
|
|
|
{
|
2014-12-03 17:40:27 +08:00
|
|
|
//load from .c3b or .c3t
|
2014-12-19 10:21:30 +08:00
|
|
|
auto bundle = Bundle3D::createBundle();
|
2014-12-03 17:40:27 +08:00
|
|
|
if (!bundle->load(fullPath))
|
2014-12-19 10:21:30 +08:00
|
|
|
{
|
|
|
|
Bundle3D::destroyBundle(bundle);
|
2014-12-03 17:40:27 +08:00
|
|
|
return false;
|
2014-09-17 13:42:08 +08:00
|
|
|
}
|
2014-12-03 17:40:27 +08:00
|
|
|
|
2014-12-19 10:21:30 +08:00
|
|
|
auto ret = bundle->loadMeshDatas(*meshdatas)
|
2014-12-03 17:40:27 +08:00
|
|
|
&& bundle->loadMaterials(*materialdatas) && bundle->loadNodes(*nodedatas);
|
2014-12-19 10:21:30 +08:00
|
|
|
Bundle3D::destroyBundle(bundle);
|
|
|
|
|
|
|
|
return ret;
|
2014-08-15 17:46:21 +08:00
|
|
|
}
|
2014-08-18 14:16:34 +08:00
|
|
|
return false;
|
2014-06-04 18:17:09 +08:00
|
|
|
}
|
|
|
|
|
2014-05-19 05:49:16 +08:00
|
|
|
Sprite3D::Sprite3D()
|
2014-08-18 11:13:08 +08:00
|
|
|
: _skeleton(nullptr)
|
2014-05-19 05:49:16 +08:00
|
|
|
, _blend(BlendFunc::ALPHA_NON_PREMULTIPLIED)
|
2015-04-02 09:33:48 +08:00
|
|
|
, _aabbDirty(true)
|
2014-09-19 14:50:20 +08:00
|
|
|
, _lightMask(-1)
|
2014-09-25 17:13:46 +08:00
|
|
|
, _shaderUsingLight(false)
|
2015-04-01 16:40:40 +08:00
|
|
|
, _forceDepthWrite(false)
|
2015-05-12 12:31:33 +08:00
|
|
|
, _usingAutogeneratedGLProgram(true)
|
2014-05-19 05:49:16 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Sprite3D::~Sprite3D()
|
|
|
|
{
|
2014-08-15 20:52:45 +08:00
|
|
|
_meshes.clear();
|
2014-08-22 13:25:26 +08:00
|
|
|
_meshVertexDatas.clear();
|
2014-07-30 18:32:34 +08:00
|
|
|
CC_SAFE_RELEASE_NULL(_skeleton);
|
2014-07-29 10:49:06 +08:00
|
|
|
removeAllAttachNode();
|
2014-05-19 05:49:16 +08:00
|
|
|
}
|
|
|
|
|
2015-01-14 08:41:24 +08:00
|
|
|
bool Sprite3D::init()
|
|
|
|
{
|
|
|
|
if(Node::init())
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-05-12 12:31:33 +08:00
|
|
|
bool Sprite3D::initWithFile(const std::string& path)
|
2014-05-19 05:49:16 +08:00
|
|
|
{
|
2014-08-18 14:16:34 +08:00
|
|
|
_meshes.clear();
|
2014-08-22 13:25:26 +08:00
|
|
|
_meshVertexDatas.clear();
|
2014-07-30 18:32:34 +08:00
|
|
|
CC_SAFE_RELEASE_NULL(_skeleton);
|
2014-08-18 14:16:34 +08:00
|
|
|
removeAllAttachNode();
|
2014-07-29 10:49:06 +08:00
|
|
|
|
|
|
|
if (loadFromCache(path))
|
|
|
|
return true;
|
2014-05-19 05:49:16 +08:00
|
|
|
|
2014-12-03 17:40:27 +08:00
|
|
|
MeshDatas* meshdatas = new (std::nothrow) MeshDatas();
|
|
|
|
MaterialDatas* materialdatas = new (std::nothrow) MaterialDatas();
|
2015-05-06 04:07:32 +08:00
|
|
|
NodeDatas* nodeDatas = new (std::nothrow) NodeDatas();
|
2014-12-03 17:40:27 +08:00
|
|
|
if (loadFromFile(path, nodeDatas, meshdatas, materialdatas))
|
2014-05-19 05:49:16 +08:00
|
|
|
{
|
2014-12-03 17:40:27 +08:00
|
|
|
if (initFrom(*nodeDatas, *meshdatas, *materialdatas))
|
|
|
|
{
|
|
|
|
//add to cache
|
|
|
|
auto data = new (std::nothrow) Sprite3DCache::Sprite3DData();
|
|
|
|
data->materialdatas = materialdatas;
|
|
|
|
data->nodedatas = nodeDatas;
|
|
|
|
data->meshVertexDatas = _meshVertexDatas;
|
|
|
|
for (const auto mesh : _meshes) {
|
|
|
|
data->glProgramStates.pushBack(mesh->getGLProgramState());
|
|
|
|
}
|
|
|
|
|
|
|
|
Sprite3DCache::getInstance()->addSprite3DData(path, data);
|
2015-03-25 14:33:53 +08:00
|
|
|
CC_SAFE_DELETE(meshdatas);
|
2015-05-08 15:49:33 +08:00
|
|
|
_contentSize = getBoundingBox().size;
|
2014-12-03 17:40:27 +08:00
|
|
|
return true;
|
|
|
|
}
|
2014-05-19 05:49:16 +08:00
|
|
|
}
|
2015-03-25 14:33:53 +08:00
|
|
|
CC_SAFE_DELETE(meshdatas);
|
|
|
|
CC_SAFE_DELETE(materialdatas);
|
|
|
|
CC_SAFE_DELETE(nodeDatas);
|
2014-06-16 20:58:13 +08:00
|
|
|
|
|
|
|
return false;
|
2014-05-19 05:49:16 +08:00
|
|
|
}
|
|
|
|
|
2014-08-18 14:16:34 +08:00
|
|
|
bool Sprite3D::initFrom(const NodeDatas& nodeDatas, const MeshDatas& meshdatas, const MaterialDatas& materialdatas)
|
|
|
|
{
|
|
|
|
for(const auto& it : meshdatas.meshDatas)
|
|
|
|
{
|
|
|
|
if(it)
|
|
|
|
{
|
2014-08-22 13:25:26 +08:00
|
|
|
// Mesh* mesh = Mesh::create(*it);
|
|
|
|
// _meshes.pushBack(mesh);
|
|
|
|
auto meshvertex = MeshVertexData::create(*it);
|
|
|
|
_meshVertexDatas.pushBack(meshvertex);
|
2014-08-18 14:16:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_skeleton = Skeleton3D::create(nodeDatas.skeleton);
|
|
|
|
CC_SAFE_RETAIN(_skeleton);
|
|
|
|
|
|
|
|
for(const auto& it : nodeDatas.nodes)
|
|
|
|
{
|
|
|
|
if(it)
|
|
|
|
{
|
|
|
|
createNode(it, this, materialdatas, nodeDatas.nodes.size() == 1);
|
|
|
|
}
|
|
|
|
}
|
2014-08-21 11:23:31 +08:00
|
|
|
for(const auto& it : nodeDatas.skeleton)
|
|
|
|
{
|
|
|
|
if(it)
|
|
|
|
{
|
|
|
|
createAttachSprite3DNode(it,materialdatas);
|
|
|
|
}
|
|
|
|
}
|
2014-08-18 14:16:34 +08:00
|
|
|
genGLProgramState();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2015-05-06 04:07:32 +08:00
|
|
|
Sprite3D* Sprite3D::createSprite3DNode(NodeData* nodedata,ModelData* modeldata,const MaterialDatas& materialdatas)
|
2014-08-21 11:23:31 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
auto sprite = new (std::nothrow) Sprite3D();
|
2014-08-21 11:23:31 +08:00
|
|
|
if (sprite)
|
|
|
|
{
|
2014-09-03 15:55:11 +08:00
|
|
|
sprite->setName(nodedata->id);
|
2014-08-22 13:25:26 +08:00
|
|
|
auto mesh = Mesh::create(nodedata->id, getMeshIndexData(modeldata->subMeshId));
|
2015-05-06 04:07:32 +08:00
|
|
|
if (modeldata->matrialId == "" && materialdatas.materials.size())
|
2014-08-21 11:23:31 +08:00
|
|
|
{
|
2015-05-06 04:07:32 +08:00
|
|
|
const NTextureData* textureData = materialdatas.materials[0].getTextureData(NTextureData::Usage::Diffuse);
|
2014-12-23 15:52:45 +08:00
|
|
|
if (!textureData->filename.empty())
|
|
|
|
mesh->setTexture(textureData->filename);
|
2014-08-21 11:23:31 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-05-06 04:07:32 +08:00
|
|
|
const NMaterialData* materialData=materialdatas.getMaterialData(modeldata->matrialId);
|
2014-08-21 11:23:31 +08:00
|
|
|
if(materialData)
|
|
|
|
{
|
|
|
|
const NTextureData* textureData = materialData->getTextureData(NTextureData::Usage::Diffuse);
|
2014-12-23 15:52:45 +08:00
|
|
|
if(textureData && !textureData->filename.empty())
|
2014-08-21 11:23:31 +08:00
|
|
|
{
|
|
|
|
auto tex = Director::getInstance()->getTextureCache()->addImage(textureData->filename);
|
|
|
|
if(tex)
|
|
|
|
{
|
2015-05-12 12:31:33 +08:00
|
|
|
Texture2D::TexParams texParams;
|
2014-08-21 11:23:31 +08:00
|
|
|
texParams.minFilter = GL_LINEAR;
|
|
|
|
texParams.magFilter = GL_LINEAR;
|
|
|
|
texParams.wrapS = textureData->wrapS;
|
|
|
|
texParams.wrapT = textureData->wrapT;
|
|
|
|
tex->setTexParameters(texParams);
|
2014-08-22 13:25:26 +08:00
|
|
|
mesh->setTexture(tex);
|
2014-09-12 17:57:54 +08:00
|
|
|
mesh->_isTransparent = (materialData->getTextureData(NTextureData::Usage::Transparency) != nullptr);
|
2014-08-21 11:23:31 +08:00
|
|
|
}
|
2014-08-18 14:16:34 +08:00
|
|
|
|
2014-08-21 11:23:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-03-17 13:13:03 +08:00
|
|
|
|
|
|
|
// set locale transform
|
2015-03-17 12:15:03 +08:00
|
|
|
Vec3 pos;
|
|
|
|
Quaternion qua;
|
|
|
|
Vec3 scale;
|
2015-03-17 15:17:23 +08:00
|
|
|
nodedata->transform.decompose(&scale, &qua, &pos);
|
2015-03-17 12:15:03 +08:00
|
|
|
sprite->setPosition3D(pos);
|
|
|
|
sprite->setRotationQuat(qua);
|
|
|
|
sprite->setScaleX(scale.x);
|
|
|
|
sprite->setScaleY(scale.y);
|
|
|
|
sprite->setScaleZ(scale.z);
|
|
|
|
|
2014-08-22 13:25:26 +08:00
|
|
|
sprite->addMesh(mesh);
|
2014-08-21 11:23:31 +08:00
|
|
|
sprite->autorelease();
|
|
|
|
sprite->genGLProgramState();
|
|
|
|
}
|
|
|
|
return sprite;
|
|
|
|
}
|
2015-05-12 12:31:33 +08:00
|
|
|
void Sprite3D::createAttachSprite3DNode(NodeData* nodedata, const MaterialDatas& materialdatas)
|
2014-08-21 11:23:31 +08:00
|
|
|
{
|
|
|
|
for(const auto& it : nodedata->modelNodeDatas)
|
|
|
|
{
|
|
|
|
if(it && getAttachNode(nodedata->id))
|
|
|
|
{
|
2015-05-06 04:07:32 +08:00
|
|
|
auto sprite = createSprite3DNode(nodedata,it,materialdatas);
|
2014-08-21 11:23:31 +08:00
|
|
|
if (sprite)
|
|
|
|
{
|
|
|
|
getAttachNode(nodedata->id)->addChild(sprite);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for(const auto& it : nodedata->children)
|
|
|
|
{
|
2015-05-06 04:07:32 +08:00
|
|
|
createAttachSprite3DNode(it,materialdatas);
|
2014-08-21 11:23:31 +08:00
|
|
|
}
|
|
|
|
}
|
2015-05-06 04:07:32 +08:00
|
|
|
|
|
|
|
void Sprite3D::setMaterial(Material *material)
|
|
|
|
{
|
2015-06-03 07:56:59 +08:00
|
|
|
setMaterial(material, -1);
|
2015-05-06 04:07:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Sprite3D::setMaterial(Material *material, int meshIndex)
|
|
|
|
{
|
|
|
|
CCASSERT(material, "Invalid Material");
|
|
|
|
CCASSERT(meshIndex == -1 || (meshIndex >=0 && meshIndex < _meshes.size()), "Invalid meshIndex");
|
|
|
|
|
2015-05-12 12:31:33 +08:00
|
|
|
|
2015-05-06 04:07:32 +08:00
|
|
|
if (meshIndex == -1)
|
|
|
|
{
|
2015-05-12 12:31:33 +08:00
|
|
|
for (auto mesh: _meshes)
|
2015-05-06 04:07:32 +08:00
|
|
|
{
|
|
|
|
mesh->setMaterial(material);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-05-12 12:31:33 +08:00
|
|
|
auto mesh = _meshes.at(meshIndex);
|
|
|
|
mesh->setMaterial(material);
|
2015-05-06 04:07:32 +08:00
|
|
|
}
|
2015-05-12 12:31:33 +08:00
|
|
|
|
|
|
|
_usingAutogeneratedGLProgram = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Material* Sprite3D::getMaterial(int meshIndex) const
|
|
|
|
{
|
|
|
|
CCASSERT(meshIndex >=0 && meshIndex < _meshes.size(), "Invalid meshIndex");
|
|
|
|
return _meshes.at(meshIndex)->getMaterial();
|
2015-05-06 04:07:32 +08:00
|
|
|
}
|
|
|
|
|
2015-05-12 12:31:33 +08:00
|
|
|
|
2014-10-13 15:17:22 +08:00
|
|
|
void Sprite3D::genGLProgramState(bool useLight)
|
2014-05-19 05:49:16 +08:00
|
|
|
{
|
2014-10-13 15:17:22 +08:00
|
|
|
_shaderUsingLight = useLight;
|
2015-05-12 12:31:33 +08:00
|
|
|
|
2014-08-22 13:25:26 +08:00
|
|
|
std::unordered_map<const MeshVertexData*, GLProgramState*> glProgramestates;
|
2015-05-12 12:31:33 +08:00
|
|
|
for(auto meshVertexData : _meshVertexDatas)
|
2014-05-19 05:49:16 +08:00
|
|
|
{
|
2015-05-12 12:31:33 +08:00
|
|
|
auto glprogramstate = getGLProgramStateForAttribs(meshVertexData, useLight);
|
2015-05-06 04:07:32 +08:00
|
|
|
glProgramestates[meshVertexData] = glprogramstate;
|
2014-05-19 05:49:16 +08:00
|
|
|
}
|
2014-08-20 18:20:19 +08:00
|
|
|
|
2015-05-12 12:31:33 +08:00
|
|
|
for (auto& mesh: _meshes)
|
|
|
|
{
|
2015-05-06 04:07:32 +08:00
|
|
|
auto glProgramState = glProgramestates[mesh->getMeshIndexData()->getMeshVertexData()];
|
|
|
|
|
|
|
|
// hack to prevent cloning the very first time
|
|
|
|
if (glProgramState->getReferenceCount() == 1)
|
|
|
|
mesh->setGLProgramState(glProgramState);
|
|
|
|
else
|
|
|
|
mesh->setGLProgramState(glProgramState->clone());
|
2014-05-19 05:49:16 +08:00
|
|
|
}
|
|
|
|
}
|
2014-08-20 18:20:19 +08:00
|
|
|
|
2015-05-06 04:07:32 +08:00
|
|
|
void Sprite3D::createNode(NodeData* nodedata, Node* root, const MaterialDatas& materialdatas, bool singleSprite)
|
2014-08-15 17:46:21 +08:00
|
|
|
{
|
|
|
|
Node* node=nullptr;
|
2014-08-20 11:40:04 +08:00
|
|
|
for(const auto& it : nodedata->modelNodeDatas)
|
2014-08-15 17:46:21 +08:00
|
|
|
{
|
2014-08-20 11:40:04 +08:00
|
|
|
if(it)
|
2014-08-15 17:46:21 +08:00
|
|
|
{
|
2014-08-20 11:40:04 +08:00
|
|
|
if(it->bones.size() > 0 || singleSprite)
|
2014-08-17 22:49:53 +08:00
|
|
|
{
|
2015-05-06 14:38:38 +08:00
|
|
|
if(singleSprite && root!=nullptr)
|
2015-04-27 19:12:44 +08:00
|
|
|
root->setName(nodedata->id);
|
2014-08-22 13:25:26 +08:00
|
|
|
auto mesh = Mesh::create(nodedata->id, getMeshIndexData(it->subMeshId));
|
|
|
|
if(mesh)
|
2014-08-18 10:32:23 +08:00
|
|
|
{
|
2014-08-22 13:25:26 +08:00
|
|
|
_meshes.pushBack(mesh);
|
2014-08-20 11:40:04 +08:00
|
|
|
if (_skeleton && it->bones.size())
|
|
|
|
{
|
|
|
|
auto skin = MeshSkin::create(_skeleton, it->bones, it->invBindPose);
|
2014-08-22 13:25:26 +08:00
|
|
|
mesh->setSkin(skin);
|
2014-08-20 11:40:04 +08:00
|
|
|
}
|
2014-08-22 13:25:26 +08:00
|
|
|
mesh->_visibleChanged = std::bind(&Sprite3D::onAABBDirty, this);
|
2014-08-18 10:32:23 +08:00
|
|
|
|
2015-05-06 04:07:32 +08:00
|
|
|
if (it->matrialId == "" && materialdatas.materials.size())
|
2014-08-18 10:32:23 +08:00
|
|
|
{
|
2015-05-06 04:07:32 +08:00
|
|
|
const NTextureData* textureData = materialdatas.materials[0].getTextureData(NTextureData::Usage::Diffuse);
|
2014-08-22 13:25:26 +08:00
|
|
|
mesh->setTexture(textureData->filename);
|
2014-08-20 11:40:04 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-05-06 04:07:32 +08:00
|
|
|
const NMaterialData* materialData=materialdatas.getMaterialData(it->matrialId);
|
2014-08-20 11:40:04 +08:00
|
|
|
if(materialData)
|
2014-08-18 10:32:23 +08:00
|
|
|
{
|
2014-08-20 11:40:04 +08:00
|
|
|
const NTextureData* textureData = materialData->getTextureData(NTextureData::Usage::Diffuse);
|
2014-12-23 15:52:45 +08:00
|
|
|
if(textureData && !textureData->filename.empty())
|
2014-08-18 10:32:23 +08:00
|
|
|
{
|
2014-08-20 11:40:04 +08:00
|
|
|
auto tex = Director::getInstance()->getTextureCache()->addImage(textureData->filename);
|
|
|
|
if(tex)
|
|
|
|
{
|
2015-05-06 04:07:32 +08:00
|
|
|
Texture2D::TexParams texParams;
|
2014-08-20 11:40:04 +08:00
|
|
|
texParams.minFilter = GL_LINEAR;
|
|
|
|
texParams.magFilter = GL_LINEAR;
|
|
|
|
texParams.wrapS = textureData->wrapS;
|
|
|
|
texParams.wrapT = textureData->wrapT;
|
|
|
|
tex->setTexParameters(texParams);
|
2014-08-22 13:25:26 +08:00
|
|
|
mesh->setTexture(tex);
|
2014-09-12 17:57:54 +08:00
|
|
|
mesh->_isTransparent = (materialData->getTextureData(NTextureData::Usage::Transparency) != nullptr);
|
2014-08-20 11:40:04 +08:00
|
|
|
}
|
2014-08-18 10:32:23 +08:00
|
|
|
|
2014-08-20 11:40:04 +08:00
|
|
|
}
|
2014-08-18 10:32:23 +08:00
|
|
|
}
|
|
|
|
}
|
2015-03-24 17:59:24 +08:00
|
|
|
|
2015-03-25 10:04:51 +08:00
|
|
|
Vec3 pos;
|
|
|
|
Quaternion qua;
|
|
|
|
Vec3 scale;
|
|
|
|
nodedata->transform.decompose(&scale, &qua, &pos);
|
|
|
|
setPosition3D(pos);
|
|
|
|
setRotationQuat(qua);
|
|
|
|
setScaleX(scale.x);
|
|
|
|
setScaleY(scale.y);
|
|
|
|
setScaleZ(scale.z);
|
|
|
|
|
2014-08-18 10:32:23 +08:00
|
|
|
}
|
2014-08-16 14:49:38 +08:00
|
|
|
}
|
2014-08-20 11:40:04 +08:00
|
|
|
else
|
2014-08-16 10:41:42 +08:00
|
|
|
{
|
2015-05-06 04:07:32 +08:00
|
|
|
auto sprite = createSprite3DNode(nodedata,it,materialdatas);
|
2014-08-20 11:40:04 +08:00
|
|
|
if (sprite)
|
2014-08-18 10:32:23 +08:00
|
|
|
{
|
2014-08-20 11:40:04 +08:00
|
|
|
if(root)
|
|
|
|
{
|
|
|
|
root->addChild(sprite);
|
|
|
|
}
|
2014-08-16 16:56:04 +08:00
|
|
|
}
|
2014-08-20 11:40:04 +08:00
|
|
|
node=sprite;
|
|
|
|
}
|
2014-08-15 17:46:21 +08:00
|
|
|
}
|
|
|
|
}
|
2014-08-20 11:40:04 +08:00
|
|
|
if(nodedata->modelNodeDatas.size() ==0 )
|
2014-08-15 17:46:21 +08:00
|
|
|
{
|
|
|
|
node= Node::create();
|
|
|
|
if(node)
|
|
|
|
{
|
2014-09-03 11:43:42 +08:00
|
|
|
node->setName(nodedata->id);
|
2015-03-17 12:15:03 +08:00
|
|
|
|
2015-03-17 13:13:03 +08:00
|
|
|
// set locale transform
|
2015-03-17 12:15:03 +08:00
|
|
|
Vec3 pos;
|
|
|
|
Quaternion qua;
|
|
|
|
Vec3 scale;
|
2015-03-17 15:17:23 +08:00
|
|
|
nodedata->transform.decompose(&scale, &qua, &pos);
|
2015-03-17 12:15:03 +08:00
|
|
|
node->setPosition3D(pos);
|
|
|
|
node->setRotationQuat(qua);
|
|
|
|
node->setScaleX(scale.x);
|
|
|
|
node->setScaleY(scale.y);
|
|
|
|
node->setScaleZ(scale.z);
|
2015-03-17 13:13:03 +08:00
|
|
|
|
2014-08-15 18:20:37 +08:00
|
|
|
if(root)
|
|
|
|
{
|
|
|
|
root->addChild(node);
|
|
|
|
}
|
2014-08-15 17:46:21 +08:00
|
|
|
}
|
|
|
|
}
|
2014-08-21 11:23:31 +08:00
|
|
|
for(const auto& it : nodedata->children)
|
2014-08-15 17:46:21 +08:00
|
|
|
{
|
2015-05-06 04:07:32 +08:00
|
|
|
createNode(it,node, materialdatas, nodedata->children.size() == 1);
|
2014-08-15 17:46:21 +08:00
|
|
|
}
|
|
|
|
}
|
2014-08-15 20:52:45 +08:00
|
|
|
|
2014-08-22 13:25:26 +08:00
|
|
|
MeshIndexData* Sprite3D::getMeshIndexData(const std::string& indexId) const
|
2014-08-15 20:52:45 +08:00
|
|
|
{
|
2014-08-22 13:25:26 +08:00
|
|
|
for (auto it : _meshVertexDatas) {
|
|
|
|
auto index = it->getMeshIndexDataById(indexId);
|
|
|
|
if (index)
|
|
|
|
return index;
|
2014-08-15 20:52:45 +08:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-08-22 13:25:26 +08:00
|
|
|
void Sprite3D::addMesh(Mesh* mesh)
|
2014-08-18 10:32:23 +08:00
|
|
|
{
|
2014-08-22 13:25:26 +08:00
|
|
|
auto meshVertex = mesh->getMeshIndexData()->_vertexData;
|
|
|
|
_meshVertexDatas.pushBack(meshVertex);
|
|
|
|
_meshes.pushBack(mesh);
|
2014-08-18 10:32:23 +08:00
|
|
|
}
|
2014-07-29 10:49:06 +08:00
|
|
|
|
2014-05-19 05:49:16 +08:00
|
|
|
void Sprite3D::setTexture(const std::string& texFile)
|
|
|
|
{
|
|
|
|
auto tex = Director::getInstance()->getTextureCache()->addImage(texFile);
|
2014-06-23 19:08:26 +08:00
|
|
|
setTexture(tex);
|
2014-05-19 05:49:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Sprite3D::setTexture(Texture2D* texture)
|
|
|
|
{
|
2014-08-22 13:25:26 +08:00
|
|
|
for (auto& state : _meshes) {
|
2014-08-20 19:01:04 +08:00
|
|
|
state->setTexture(texture);
|
|
|
|
}
|
2014-07-29 10:49:06 +08:00
|
|
|
}
|
|
|
|
AttachNode* Sprite3D::getAttachNode(const std::string& boneName)
|
|
|
|
{
|
|
|
|
auto it = _attachments.find(boneName);
|
|
|
|
if (it != _attachments.end())
|
|
|
|
return it->second;
|
|
|
|
|
2014-08-18 11:13:08 +08:00
|
|
|
if (_skeleton)
|
2014-07-29 10:49:06 +08:00
|
|
|
{
|
2014-08-18 11:13:08 +08:00
|
|
|
auto bone = _skeleton->getBoneByName(boneName);
|
2015-02-04 10:24:29 +08:00
|
|
|
if (bone)
|
2015-02-04 10:36:16 +08:00
|
|
|
{
|
2015-02-04 10:24:29 +08:00
|
|
|
auto attachNode = AttachNode::create(bone);
|
|
|
|
addChild(attachNode);
|
|
|
|
_attachments[boneName] = attachNode;
|
|
|
|
return attachNode;
|
|
|
|
}
|
2014-07-29 10:49:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sprite3D::removeAttachNode(const std::string& boneName)
|
|
|
|
{
|
|
|
|
auto it = _attachments.find(boneName);
|
|
|
|
if (it != _attachments.end())
|
|
|
|
{
|
|
|
|
removeChild(it->second);
|
|
|
|
_attachments.erase(it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sprite3D::removeAllAttachNode()
|
|
|
|
{
|
|
|
|
for (auto& it : _attachments) {
|
|
|
|
removeChild(it.second);
|
2014-05-19 05:49:16 +08:00
|
|
|
}
|
2014-07-29 10:49:06 +08:00
|
|
|
_attachments.clear();
|
2014-05-19 05:49:16 +08:00
|
|
|
}
|
2015-04-28 23:55:23 +08:00
|
|
|
|
2015-01-13 12:06:50 +08:00
|
|
|
void Sprite3D::visit(cocos2d::Renderer *renderer, const cocos2d::Mat4 &parentTransform, uint32_t parentFlags)
|
|
|
|
{
|
|
|
|
// quick return if not visible. children won't be drawn.
|
|
|
|
if (!_visible)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t flags = processParentFlags(parentTransform, parentFlags);
|
2015-01-15 08:23:35 +08:00
|
|
|
flags |= FLAGS_RENDER_AS_3D;
|
2015-01-13 12:06:50 +08:00
|
|
|
|
|
|
|
//
|
|
|
|
Director* director = Director::getInstance();
|
|
|
|
director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
|
|
|
|
director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, _modelViewTransform);
|
|
|
|
|
|
|
|
bool visibleByCamera = isVisitableByVisitingCamera();
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
if(!_children.empty())
|
|
|
|
{
|
|
|
|
sortAllChildren();
|
|
|
|
// draw children zOrder < 0
|
|
|
|
for( ; i < _children.size(); i++ )
|
|
|
|
{
|
|
|
|
auto node = _children.at(i);
|
|
|
|
|
|
|
|
if (node && node->getLocalZOrder() < 0)
|
|
|
|
node->visit(renderer, _modelViewTransform, flags);
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// self draw
|
|
|
|
if (visibleByCamera)
|
|
|
|
this->draw(renderer, _modelViewTransform, flags);
|
|
|
|
|
|
|
|
for(auto it=_children.cbegin()+i; it != _children.cend(); ++it)
|
|
|
|
(*it)->visit(renderer, _modelViewTransform, flags);
|
|
|
|
}
|
|
|
|
else if (visibleByCamera)
|
|
|
|
{
|
|
|
|
this->draw(renderer, _modelViewTransform, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
|
|
|
|
}
|
|
|
|
|
2014-05-31 07:42:05 +08:00
|
|
|
void Sprite3D::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
|
2014-05-19 05:49:16 +08:00
|
|
|
{
|
2015-01-14 14:41:10 +08:00
|
|
|
#if CC_USE_CULLING
|
2014-12-18 17:18:22 +08:00
|
|
|
// camera clipping
|
2015-05-19 14:47:28 +08:00
|
|
|
if(Camera::getVisitingCamera() && !Camera::getVisitingCamera()->isVisibleInFrustum(&this->getAABB()))
|
2014-12-18 17:18:22 +08:00
|
|
|
return;
|
2015-01-14 14:41:10 +08:00
|
|
|
#endif
|
2014-12-18 17:18:22 +08:00
|
|
|
|
2014-08-16 14:16:17 +08:00
|
|
|
if (_skeleton)
|
|
|
|
_skeleton->updateBoneMatrix();
|
2014-07-30 18:32:34 +08:00
|
|
|
|
2014-05-19 05:49:16 +08:00
|
|
|
Color4F color(getDisplayedColor());
|
|
|
|
color.a = getDisplayedOpacity() / 255.0f;
|
|
|
|
|
2014-09-25 17:13:46 +08:00
|
|
|
//check light and determine the shader used
|
2015-03-16 18:04:50 +08:00
|
|
|
const auto& scene = Director::getInstance()->getRunningScene();
|
2015-05-12 12:31:33 +08:00
|
|
|
|
|
|
|
// Don't override GLProgramState if using manually set Material
|
|
|
|
if (_usingAutogeneratedGLProgram && scene)
|
2015-03-16 18:04:50 +08:00
|
|
|
{
|
2015-05-14 08:39:39 +08:00
|
|
|
const auto lights = scene->getLights();
|
2015-03-16 18:04:50 +08:00
|
|
|
bool usingLight = false;
|
|
|
|
for (const auto light : lights) {
|
|
|
|
usingLight = ((unsigned int)light->getLightFlag() & _lightMask) > 0;
|
|
|
|
if (usingLight)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (usingLight != _shaderUsingLight)
|
2015-05-12 12:31:33 +08:00
|
|
|
{
|
2015-03-16 18:04:50 +08:00
|
|
|
genGLProgramState(usingLight);
|
2015-05-12 12:31:33 +08:00
|
|
|
}
|
2014-09-25 17:13:46 +08:00
|
|
|
}
|
|
|
|
|
2015-06-03 07:56:59 +08:00
|
|
|
for (auto mesh: _meshes)
|
2015-05-06 04:07:32 +08:00
|
|
|
{
|
|
|
|
mesh->draw(renderer,
|
|
|
|
_globalZOrder,
|
|
|
|
transform,
|
|
|
|
flags,
|
|
|
|
_lightMask,
|
|
|
|
Vec4(color.r, color.g, color.b, color.a),
|
|
|
|
_forceDepthWrite);
|
2014-09-23 17:08:28 +08:00
|
|
|
|
2014-06-05 16:36:01 +08:00
|
|
|
}
|
2014-05-19 05:49:16 +08:00
|
|
|
}
|
|
|
|
|
2015-06-03 07:56:59 +08:00
|
|
|
void Sprite3D::setGLProgramState(GLProgramState* glProgramState)
|
2014-08-20 19:01:04 +08:00
|
|
|
{
|
|
|
|
Node::setGLProgramState(glProgramState);
|
2015-06-03 07:56:59 +08:00
|
|
|
for (auto state : _meshes) {
|
2014-08-20 19:01:04 +08:00
|
|
|
state->setGLProgramState(glProgramState);
|
|
|
|
}
|
|
|
|
}
|
2015-06-03 07:56:59 +08:00
|
|
|
void Sprite3D::setGLProgram(GLProgram* glprogram)
|
2014-08-20 19:01:04 +08:00
|
|
|
{
|
2015-05-13 16:16:45 +08:00
|
|
|
auto glProgramState = GLProgramState::create(glprogram);
|
|
|
|
setGLProgramState(glProgramState);
|
2014-08-20 19:01:04 +08:00
|
|
|
}
|
|
|
|
|
2015-06-03 07:56:59 +08:00
|
|
|
void Sprite3D::setBlendFunc(const BlendFunc& blendFunc)
|
2014-05-19 05:49:16 +08:00
|
|
|
{
|
|
|
|
if(_blend.src != blendFunc.src || _blend.dst != blendFunc.dst)
|
|
|
|
{
|
|
|
|
_blend = blendFunc;
|
2015-06-03 07:56:59 +08:00
|
|
|
for(auto mesh: _meshes)
|
2014-08-20 19:01:04 +08:00
|
|
|
{
|
2015-05-06 04:07:32 +08:00
|
|
|
mesh->setBlendFunc(blendFunc);
|
2014-08-20 19:01:04 +08:00
|
|
|
}
|
2014-05-19 05:49:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const BlendFunc& Sprite3D::getBlendFunc() const
|
|
|
|
{
|
|
|
|
return _blend;
|
|
|
|
}
|
|
|
|
|
2015-04-08 16:40:04 +08:00
|
|
|
AABB Sprite3D::getAABBRecursively()
|
|
|
|
{
|
|
|
|
AABB aabb;
|
2015-06-03 07:56:59 +08:00
|
|
|
const auto children = getChildren();
|
|
|
|
for (const auto iter: children)
|
2015-04-08 16:40:04 +08:00
|
|
|
{
|
|
|
|
Sprite3D* child = dynamic_cast<Sprite3D*>(iter);
|
|
|
|
if(child)
|
|
|
|
{
|
|
|
|
aabb.merge(child->getAABBRecursively());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
aabb.merge(getAABB());
|
|
|
|
return aabb;
|
|
|
|
}
|
|
|
|
|
2014-08-18 17:02:13 +08:00
|
|
|
const AABB& Sprite3D::getAABB() const
|
2014-08-08 17:54:39 +08:00
|
|
|
{
|
2014-08-12 13:45:06 +08:00
|
|
|
Mat4 nodeToWorldTransform(getNodeToWorldTransform());
|
2014-08-08 17:54:39 +08:00
|
|
|
|
2014-08-12 13:45:06 +08:00
|
|
|
// If nodeToWorldTransform matrix isn't changed, we don't need to transform aabb.
|
2014-08-18 17:02:13 +08:00
|
|
|
if (memcmp(_nodeToWorldTransform.m, nodeToWorldTransform.m, sizeof(Mat4)) == 0 && !_aabbDirty)
|
2014-08-08 17:54:39 +08:00
|
|
|
{
|
2014-08-12 13:45:06 +08:00
|
|
|
return _aabb;
|
2014-08-08 17:54:39 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-08-18 17:02:13 +08:00
|
|
|
_aabb.reset();
|
2015-04-08 16:40:04 +08:00
|
|
|
if (_meshes.size())
|
|
|
|
{
|
|
|
|
Mat4 transform(nodeToWorldTransform);
|
|
|
|
for (const auto& it : _meshes) {
|
|
|
|
if (it->isVisible())
|
|
|
|
_aabb.merge(it->getAABB());
|
|
|
|
}
|
|
|
|
|
|
|
|
_aabb.transform(transform);
|
|
|
|
_nodeToWorldTransform = nodeToWorldTransform;
|
|
|
|
_aabbDirty = false;
|
2014-08-18 17:02:13 +08:00
|
|
|
}
|
2014-08-08 17:54:39 +08:00
|
|
|
}
|
|
|
|
|
2014-08-12 13:45:06 +08:00
|
|
|
return _aabb;
|
2014-08-08 17:54:39 +08:00
|
|
|
}
|
|
|
|
|
2015-01-28 09:05:51 +08:00
|
|
|
Action* Sprite3D::runAction(Action *action)
|
|
|
|
{
|
|
|
|
setForceDepthWrite(true);
|
|
|
|
return Node::runAction(action);
|
|
|
|
}
|
|
|
|
|
2014-08-08 17:54:39 +08:00
|
|
|
Rect Sprite3D::getBoundingBox() const
|
|
|
|
{
|
|
|
|
AABB aabb = getAABB();
|
|
|
|
Rect ret(aabb._min.x, aabb._min.y, (aabb._max.x - aabb._min.x), (aabb._max.y - aabb._min.y));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-08-13 17:38:16 +08:00
|
|
|
void Sprite3D::setCullFace(GLenum cullFace)
|
|
|
|
{
|
2014-08-22 13:25:26 +08:00
|
|
|
for (auto& it : _meshes) {
|
2015-05-06 04:07:32 +08:00
|
|
|
it->getMaterial()->getStateBlock()->setCullFaceSide((RenderState::CullFaceSide)cullFace);
|
|
|
|
// it->getMeshCommand().setCullFace(cullFace);
|
2014-08-13 17:38:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sprite3D::setCullFaceEnabled(bool enable)
|
|
|
|
{
|
2014-08-22 13:25:26 +08:00
|
|
|
for (auto& it : _meshes) {
|
2015-05-06 04:07:32 +08:00
|
|
|
it->getMaterial()->getStateBlock()->setCullFace(enable);
|
|
|
|
// it->getMeshCommand().setCullFaceEnabled(enable);
|
2014-08-13 17:38:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-22 13:25:26 +08:00
|
|
|
Mesh* Sprite3D::getMeshByIndex(int index) const
|
2014-08-18 11:13:08 +08:00
|
|
|
{
|
2014-08-22 13:25:26 +08:00
|
|
|
CCASSERT(index < _meshes.size(), "invald index");
|
|
|
|
return _meshes.at(index);
|
2014-08-18 11:13:08 +08:00
|
|
|
}
|
|
|
|
|
2014-10-25 12:27:03 +08:00
|
|
|
/**get Mesh by Name */
|
2014-08-22 13:25:26 +08:00
|
|
|
Mesh* Sprite3D::getMeshByName(const std::string& name) const
|
2014-08-18 11:13:08 +08:00
|
|
|
{
|
2014-08-22 13:25:26 +08:00
|
|
|
for (const auto& it : _meshes) {
|
2014-08-18 11:13:08 +08:00
|
|
|
if (it->getName() == name)
|
|
|
|
return it;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-10-25 12:27:03 +08:00
|
|
|
std::vector<Mesh*> Sprite3D::getMeshArrayByName(const std::string& name) const
|
|
|
|
{
|
|
|
|
std::vector<Mesh*> meshes;
|
|
|
|
for (const auto& it : _meshes) {
|
|
|
|
if (it->getName() == name)
|
|
|
|
meshes.push_back(it);
|
|
|
|
}
|
|
|
|
return meshes;
|
|
|
|
}
|
|
|
|
|
2014-08-18 11:13:08 +08:00
|
|
|
MeshSkin* Sprite3D::getSkin() const
|
|
|
|
{
|
2014-08-22 13:25:26 +08:00
|
|
|
for (const auto& it : _meshes) {
|
2014-08-18 11:13:08 +08:00
|
|
|
if (it->getSkin())
|
|
|
|
return it->getSkin();
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-08-19 11:56:09 +08:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
|
|
|
Sprite3DCache* Sprite3DCache::_cacheInstance = nullptr;
|
|
|
|
Sprite3DCache* Sprite3DCache::getInstance()
|
|
|
|
{
|
|
|
|
if (_cacheInstance == nullptr)
|
2014-08-28 07:31:57 +08:00
|
|
|
_cacheInstance = new (std::nothrow) Sprite3DCache();
|
2014-08-19 11:56:09 +08:00
|
|
|
return _cacheInstance;
|
|
|
|
}
|
|
|
|
void Sprite3DCache::destroyInstance()
|
|
|
|
{
|
|
|
|
if (_cacheInstance)
|
|
|
|
{
|
|
|
|
delete _cacheInstance;
|
|
|
|
_cacheInstance = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Sprite3DCache::Sprite3DData* Sprite3DCache::getSpriteData(const std::string& key) const
|
|
|
|
{
|
|
|
|
auto it = _spriteDatas.find(key);
|
|
|
|
if (it != _spriteDatas.end())
|
|
|
|
return it->second;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Sprite3DCache::addSprite3DData(const std::string& key, Sprite3DCache::Sprite3DData* spritedata)
|
|
|
|
{
|
|
|
|
auto it = _spriteDatas.find(key);
|
|
|
|
if (it == _spriteDatas.end())
|
|
|
|
{
|
|
|
|
_spriteDatas[key] = spritedata;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sprite3DCache::removeSprite3DData(const std::string& key)
|
|
|
|
{
|
|
|
|
auto it = _spriteDatas.find(key);
|
|
|
|
if (it != _spriteDatas.end())
|
|
|
|
{
|
|
|
|
delete it->second;
|
|
|
|
}
|
|
|
|
_spriteDatas.erase(it);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sprite3DCache::removeAllSprite3DData()
|
|
|
|
{
|
|
|
|
for (auto& it : _spriteDatas) {
|
|
|
|
delete it.second;
|
|
|
|
}
|
|
|
|
_spriteDatas.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
Sprite3DCache::Sprite3DCache()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
Sprite3DCache::~Sprite3DCache()
|
|
|
|
{
|
|
|
|
removeAllSprite3DData();
|
|
|
|
}
|
|
|
|
|
2015-05-12 12:31:33 +08:00
|
|
|
//
|
|
|
|
// MARK: Helpers
|
|
|
|
//
|
|
|
|
static GLProgramState* getGLProgramStateForAttribs(MeshVertexData* meshVertexData, bool usesLight)
|
|
|
|
{
|
|
|
|
bool textured = meshVertexData->hasVertexAttrib(GLProgram::VERTEX_ATTRIB_TEX_COORD);
|
|
|
|
bool hasSkin = meshVertexData->hasVertexAttrib(GLProgram::VERTEX_ATTRIB_BLEND_INDEX)
|
|
|
|
&& meshVertexData->hasVertexAttrib(GLProgram::VERTEX_ATTRIB_BLEND_WEIGHT);
|
|
|
|
bool hasNormal = meshVertexData->hasVertexAttrib(GLProgram::VERTEX_ATTRIB_NORMAL);
|
|
|
|
|
|
|
|
const char* shader = nullptr;
|
|
|
|
if(textured)
|
|
|
|
{
|
|
|
|
if (hasSkin)
|
|
|
|
{
|
|
|
|
if (hasNormal && usesLight)
|
|
|
|
shader = GLProgram::SHADER_3D_SKINPOSITION_NORMAL_TEXTURE;
|
|
|
|
else
|
|
|
|
shader = GLProgram::SHADER_3D_SKINPOSITION_TEXTURE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (hasNormal && usesLight)
|
|
|
|
shader = GLProgram::SHADER_3D_POSITION_NORMAL_TEXTURE;
|
|
|
|
else
|
|
|
|
shader = GLProgram::SHADER_3D_POSITION_TEXTURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
shader = GLProgram::SHADER_3D_POSITION;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCASSERT(shader, "Couldn't find shader for sprite");
|
|
|
|
|
|
|
|
auto glProgram = GLProgramCache::getInstance()->getGLProgram(shader);
|
|
|
|
auto glprogramstate = GLProgramState::create(glProgram);
|
|
|
|
|
|
|
|
return glprogramstate;
|
|
|
|
}
|
|
|
|
|
2014-05-19 05:49:16 +08:00
|
|
|
NS_CC_END
|