2014-05-19 05:49:16 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2013-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 "Sprite3DTest.h"
|
2014-06-09 18:37:58 +08:00
|
|
|
#include "3d/CCAnimation3D.h"
|
|
|
|
#include "3d/CCAnimate3D.h"
|
2014-05-19 05:49:16 +08:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include "../testResource.h"
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
IDC_NEXT = 100,
|
|
|
|
IDC_BACK,
|
|
|
|
IDC_RESTART
|
|
|
|
};
|
|
|
|
|
|
|
|
static int sceneIdx = -1;
|
|
|
|
|
|
|
|
|
|
|
|
static std::function<Layer*()> createFunctions[] =
|
|
|
|
{
|
|
|
|
CL(Sprite3DBasicTest),
|
2014-06-06 19:12:08 +08:00
|
|
|
CL(Sprite3DEffectTest),
|
2014-06-26 18:35:12 +08:00
|
|
|
CL(Sprite3DWithSkinTest),
|
|
|
|
CL(Animate3DTest)
|
2014-05-19 05:49:16 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
#define MAX_LAYER (sizeof(createFunctions) / sizeof(createFunctions[0]))
|
|
|
|
|
|
|
|
static Layer* nextSpriteTestAction()
|
|
|
|
{
|
|
|
|
sceneIdx++;
|
|
|
|
sceneIdx = sceneIdx % MAX_LAYER;
|
|
|
|
|
|
|
|
auto layer = (createFunctions[sceneIdx])();
|
|
|
|
return layer;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Layer* backSpriteTestAction()
|
|
|
|
{
|
|
|
|
sceneIdx--;
|
|
|
|
int total = MAX_LAYER;
|
|
|
|
if( sceneIdx < 0 )
|
|
|
|
sceneIdx += total;
|
|
|
|
|
|
|
|
auto layer = (createFunctions[sceneIdx])();
|
|
|
|
return layer;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Layer* restartSpriteTestAction()
|
|
|
|
{
|
|
|
|
auto layer = (createFunctions[sceneIdx])();
|
|
|
|
return layer;
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// SpriteTestDemo
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
|
|
|
|
Sprite3DTestDemo::Sprite3DTestDemo(void)
|
|
|
|
: BaseTest()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Sprite3DTestDemo::~Sprite3DTestDemo(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Sprite3DTestDemo::title() const
|
|
|
|
{
|
|
|
|
return "No title";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Sprite3DTestDemo::subtitle() const
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sprite3DTestDemo::onEnter()
|
|
|
|
{
|
|
|
|
BaseTest::onEnter();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sprite3DTestDemo::restartCallback(Ref* sender)
|
|
|
|
{
|
|
|
|
auto s = new Sprite3DTestScene();
|
|
|
|
s->addChild(restartSpriteTestAction());
|
|
|
|
|
|
|
|
Director::getInstance()->replaceScene(s);
|
|
|
|
s->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sprite3DTestDemo::nextCallback(Ref* sender)
|
|
|
|
{
|
|
|
|
auto s = new Sprite3DTestScene();
|
|
|
|
s->addChild( nextSpriteTestAction() );
|
|
|
|
Director::getInstance()->replaceScene(s);
|
|
|
|
s->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sprite3DTestDemo::backCallback(Ref* sender)
|
|
|
|
{
|
|
|
|
auto s = new Sprite3DTestScene();
|
|
|
|
s->addChild( backSpriteTestAction() );
|
|
|
|
Director::getInstance()->replaceScene(s);
|
|
|
|
s->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Sprite3DBasicTest
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
|
|
|
|
Sprite3DBasicTest::Sprite3DBasicTest()
|
|
|
|
{
|
|
|
|
auto listener = EventListenerTouchAllAtOnce::create();
|
|
|
|
listener->onTouchesEnded = CC_CALLBACK_2(Sprite3DBasicTest::onTouchesEnded, this);
|
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
|
|
|
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
|
|
|
addNewSpriteWithCoords( Vec2(s.width/2, s.height/2) );
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sprite3DBasicTest::addNewSpriteWithCoords(Vec2 p)
|
|
|
|
{
|
|
|
|
//int idx = (int)(CCRANDOM_0_1() * 1400.0f / 100.0f);
|
|
|
|
//int x = (idx%5) * 85;
|
|
|
|
//int y = (idx/5) * 121;
|
|
|
|
|
|
|
|
// //option 1: load a obj that contain the texture in it
|
|
|
|
// auto sprite = Sprite3D::create("sprite3dTest/scene01.obj");
|
|
|
|
|
|
|
|
//option 2: load obj and assign the texture
|
|
|
|
auto sprite = Sprite3D::create("Sprite3DTest/boss1.obj");
|
|
|
|
sprite->setScale(3.f);
|
|
|
|
sprite->setTexture("Sprite3DTest/boss.png");
|
|
|
|
|
|
|
|
//
|
|
|
|
//sprite->setEffect(cocos2d::EFFECT_OUTLINE);
|
|
|
|
|
|
|
|
//add to scene
|
|
|
|
addChild( sprite );
|
|
|
|
|
|
|
|
sprite->setPosition( Vec2( p.x, p.y) );
|
|
|
|
|
|
|
|
ActionInterval* action;
|
|
|
|
float random = CCRANDOM_0_1();
|
|
|
|
|
|
|
|
if( random < 0.20 )
|
|
|
|
action = ScaleBy::create(3, 2);
|
|
|
|
else if(random < 0.40)
|
|
|
|
action = RotateBy::create(3, 360);
|
|
|
|
else if( random < 0.60)
|
|
|
|
action = Blink::create(1, 3);
|
|
|
|
else if( random < 0.8 )
|
|
|
|
action = TintBy::create(2, 0, -255, -255);
|
|
|
|
else
|
|
|
|
action = FadeOut::create(2);
|
|
|
|
auto action_back = action->reverse();
|
2014-07-10 00:45:27 +08:00
|
|
|
auto seq = Sequence::create( action, action_back, nullptr );
|
2014-05-19 05:49:16 +08:00
|
|
|
|
|
|
|
sprite->runAction( RepeatForever::create(seq) );
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sprite3DBasicTest::onTouchesEnded(const std::vector<Touch*>& touches, Event* event)
|
|
|
|
{
|
|
|
|
for (auto touch: touches)
|
|
|
|
{
|
|
|
|
auto location = touch->getLocation();
|
|
|
|
|
|
|
|
addNewSpriteWithCoords( location );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Sprite3DBasicTest::title() const
|
|
|
|
{
|
|
|
|
return "Testing Sprite3D";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Sprite3DBasicTest::subtitle() const
|
|
|
|
{
|
|
|
|
return "Tap screen to add more sprites";
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sprite3DTestScene::runThisTest()
|
|
|
|
{
|
|
|
|
auto layer = nextSpriteTestAction();
|
|
|
|
addChild(layer);
|
|
|
|
|
|
|
|
Director::getInstance()->replaceScene(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int tuple_sort( const std::tuple<ssize_t,Effect3D*,CustomCommand> &tuple1, const std::tuple<ssize_t,Effect3D*,CustomCommand> &tuple2 )
|
|
|
|
{
|
|
|
|
return std::get<0>(tuple1) < std::get<0>(tuple2);
|
|
|
|
}
|
|
|
|
|
|
|
|
EffectSprite3D* EffectSprite3D::createFromObjFileAndTexture(const std::string &objFilePath, const std::string &textureFilePath)
|
|
|
|
{
|
|
|
|
auto sprite = new EffectSprite3D();
|
|
|
|
if (sprite && sprite->initWithFile(objFilePath))
|
|
|
|
{
|
|
|
|
sprite->autorelease();
|
|
|
|
sprite->setTexture(textureFilePath);
|
|
|
|
return sprite;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(sprite);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
EffectSprite3D::EffectSprite3D()
|
|
|
|
: _defaultEffect(nullptr)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
EffectSprite3D::~EffectSprite3D()
|
|
|
|
{
|
|
|
|
for(auto effect : _effects)
|
|
|
|
{
|
|
|
|
CC_SAFE_RELEASE_NULL(std::get<1>(effect));
|
|
|
|
}
|
|
|
|
CC_SAFE_RELEASE(_defaultEffect);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EffectSprite3D::setEffect3D(Effect3D *effect)
|
|
|
|
{
|
|
|
|
if(_defaultEffect == effect) return;
|
|
|
|
CC_SAFE_RETAIN(effect);
|
|
|
|
CC_SAFE_RELEASE(_defaultEffect);
|
|
|
|
_defaultEffect = effect;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EffectSprite3D::addEffect(Effect3DOutline* effect, ssize_t order)
|
|
|
|
{
|
|
|
|
if(nullptr == effect) return;
|
|
|
|
effect->retain();
|
2014-05-27 14:50:46 +08:00
|
|
|
effect->setTarget(this);
|
2014-05-19 05:49:16 +08:00
|
|
|
|
|
|
|
_effects.push_back(std::make_tuple(order,effect,CustomCommand()));
|
2014-05-27 14:50:46 +08:00
|
|
|
|
2014-05-19 05:49:16 +08:00
|
|
|
std::sort(std::begin(_effects), std::end(_effects), tuple_sort);
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string Effect3DOutline::_vertShaderFile = "Shaders3D/OutLine.vert";
|
|
|
|
const std::string Effect3DOutline::_fragShaderFile = "Shaders3D/OutLine.frag";
|
|
|
|
const std::string Effect3DOutline::_keyInGLProgramCache = "Effect3DLibrary_Outline";
|
|
|
|
GLProgram* Effect3DOutline::getOrCreateProgram()
|
|
|
|
{
|
|
|
|
auto program = GLProgramCache::getInstance()->getGLProgram(_keyInGLProgramCache);
|
|
|
|
if(program == nullptr)
|
|
|
|
{
|
|
|
|
program = GLProgram::createWithFilenames(_vertShaderFile, _fragShaderFile);
|
|
|
|
GLProgramCache::getInstance()->addGLProgram(program, _keyInGLProgramCache);
|
|
|
|
}
|
|
|
|
return program;
|
|
|
|
}
|
|
|
|
|
|
|
|
Effect3DOutline* Effect3DOutline::create()
|
|
|
|
{
|
|
|
|
Effect3DOutline* effect = new Effect3DOutline();
|
|
|
|
if(effect && effect->init())
|
|
|
|
{
|
|
|
|
effect->autorelease();
|
|
|
|
return effect;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(effect);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Effect3DOutline::init()
|
|
|
|
{
|
|
|
|
|
2014-05-28 21:07:12 +08:00
|
|
|
GLProgram* glprogram = GLProgram::createWithFilenames(_vertShaderFile, _fragShaderFile);
|
2014-05-19 05:49:16 +08:00
|
|
|
if(nullptr == glprogram)
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(glprogram);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
_glProgramState = GLProgramState::create(glprogram);
|
|
|
|
if(nullptr == _glProgramState)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
_glProgramState->retain();
|
|
|
|
_glProgramState->setUniformVec3("OutLineColor", _outlineColor);
|
|
|
|
_glProgramState->setUniformFloat("OutlineWidth", _outlineWidth);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Effect3DOutline::Effect3DOutline()
|
|
|
|
: _outlineWidth(1.0f)
|
|
|
|
, _outlineColor(1, 1, 1)
|
2014-05-27 14:50:46 +08:00
|
|
|
, _sprite(nullptr)
|
2014-05-19 05:49:16 +08:00
|
|
|
{
|
2014-05-28 21:07:12 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
|
2014-07-09 23:03:04 +08:00
|
|
|
_backToForegroundListener = EventListenerCustom::create(EVENT_RENDERER_RECREATED,
|
2014-05-28 21:07:12 +08:00
|
|
|
[this](EventCustom*)
|
|
|
|
{
|
|
|
|
auto glProgram = _glProgramState->getGLProgram();
|
|
|
|
glProgram->reset();
|
|
|
|
glProgram->initWithFilenames(_vertShaderFile, _fragShaderFile);
|
|
|
|
glProgram->link();
|
|
|
|
glProgram->updateUniforms();
|
|
|
|
}
|
|
|
|
);
|
2014-05-30 11:13:53 +08:00
|
|
|
Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundListener, -1);
|
2014-05-28 21:07:12 +08:00
|
|
|
#endif
|
2014-05-19 05:49:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Effect3DOutline::~Effect3DOutline()
|
|
|
|
{
|
2014-05-28 21:07:12 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
|
2014-05-30 11:13:53 +08:00
|
|
|
Director::getInstance()->getEventDispatcher()->removeEventListener(_backToForegroundListener);
|
2014-05-28 21:07:12 +08:00
|
|
|
#endif
|
2014-05-19 05:49:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Effect3DOutline::setOutlineColor(const Vec3& color)
|
|
|
|
{
|
|
|
|
if(_outlineColor != color)
|
|
|
|
{
|
|
|
|
_outlineColor = color;
|
|
|
|
_glProgramState->setUniformVec3("OutLineColor", _outlineColor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Effect3DOutline::setOutlineWidth(float width)
|
|
|
|
{
|
|
|
|
if(_outlineWidth != width)
|
|
|
|
{
|
|
|
|
_outlineWidth = width;
|
|
|
|
_glProgramState->setUniformFloat("OutlineWidth", _outlineWidth);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-27 14:50:46 +08:00
|
|
|
void Effect3DOutline::setTarget(EffectSprite3D *sprite)
|
2014-05-19 05:49:16 +08:00
|
|
|
{
|
2014-05-27 15:08:38 +08:00
|
|
|
CCASSERT(nullptr != sprite && nullptr != sprite->getMesh(),"Error: Setting a null pointer or a null mesh EffectSprite3D to Effect3D");
|
2014-05-27 14:50:46 +08:00
|
|
|
|
|
|
|
if(sprite != _sprite)
|
2014-05-19 05:49:16 +08:00
|
|
|
{
|
2014-05-27 14:50:46 +08:00
|
|
|
_sprite = sprite;
|
2014-05-19 05:49:16 +08:00
|
|
|
|
2014-05-27 14:50:46 +08:00
|
|
|
auto mesh = sprite->getMesh();
|
|
|
|
long offset = 0;
|
|
|
|
for (auto i = 0; i < mesh->getMeshVertexAttribCount(); i++)
|
|
|
|
{
|
|
|
|
auto meshvertexattrib = mesh->getMeshVertexAttribute(i);
|
|
|
|
|
|
|
|
_glProgramState->setVertexAttribPointer(s_attributeNames[meshvertexattrib.vertexAttrib],
|
|
|
|
meshvertexattrib.size,
|
|
|
|
meshvertexattrib.type,
|
|
|
|
GL_FALSE,
|
|
|
|
mesh->getVertexSizeInBytes(),
|
|
|
|
(void*)offset);
|
|
|
|
offset += meshvertexattrib.attribSizeBytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
Color4F color(_sprite->getDisplayedColor());
|
|
|
|
color.a = _sprite->getDisplayedOpacity() / 255.0f;
|
|
|
|
_glProgramState->setUniformVec4("u_color", Vec4(color.r, color.g, color.b, color.a));
|
2014-05-19 05:49:16 +08:00
|
|
|
}
|
2014-05-27 14:50:46 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void Effect3DOutline::draw(const Mat4 &transform)
|
|
|
|
{
|
2014-05-19 05:49:16 +08:00
|
|
|
//draw
|
2014-05-27 14:50:46 +08:00
|
|
|
if(_sprite && _sprite->getMesh())
|
2014-05-19 05:49:16 +08:00
|
|
|
{
|
|
|
|
glEnable(GL_CULL_FACE);
|
|
|
|
glCullFace(GL_FRONT);
|
|
|
|
glEnable(GL_DEPTH_TEST);
|
|
|
|
|
2014-05-27 14:50:46 +08:00
|
|
|
auto mesh = _sprite->getMesh();
|
2014-05-19 05:49:16 +08:00
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, mesh->getVertexBuffer());
|
|
|
|
_glProgramState->apply(transform);
|
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh->getIndexBuffer());
|
2014-05-23 03:34:16 +08:00
|
|
|
glDrawElements((GLenum)mesh->getPrimitiveType(), (GLsizei)mesh->getIndexCount(), (GLenum)mesh->getIndexFormat(), 0);
|
2014-05-19 05:49:16 +08:00
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
|
|
glDisable(GL_DEPTH_TEST);
|
|
|
|
glCullFace(GL_BACK);
|
|
|
|
glDisable(GL_CULL_FACE);
|
|
|
|
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, mesh->getIndexCount());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-31 07:42:05 +08:00
|
|
|
void EffectSprite3D::draw(cocos2d::Renderer *renderer, const cocos2d::Mat4 &transform, uint32_t flags)
|
2014-05-19 05:49:16 +08:00
|
|
|
{
|
|
|
|
for(auto &effect : _effects)
|
|
|
|
{
|
|
|
|
if(std::get<0>(effect) >=0)
|
|
|
|
break;
|
|
|
|
CustomCommand &cc = std::get<2>(effect);
|
2014-05-27 14:50:46 +08:00
|
|
|
cc.func = CC_CALLBACK_0(Effect3D::draw,std::get<1>(effect),transform);
|
2014-05-19 05:49:16 +08:00
|
|
|
renderer->addCommand(&cc);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!_defaultEffect)
|
|
|
|
{
|
2014-05-31 07:42:05 +08:00
|
|
|
Sprite3D::draw(renderer, transform, flags);
|
2014-05-19 05:49:16 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_command.init(_globalZOrder);
|
2014-05-27 14:50:46 +08:00
|
|
|
_command.func = CC_CALLBACK_0(Effect3D::draw, _defaultEffect, transform);
|
2014-05-19 05:49:16 +08:00
|
|
|
renderer->addCommand(&_command);
|
|
|
|
}
|
|
|
|
|
|
|
|
for(auto &effect : _effects)
|
|
|
|
{
|
|
|
|
if(std::get<0>(effect) <=0)
|
|
|
|
continue;
|
|
|
|
CustomCommand &cc = std::get<2>(effect);
|
2014-05-27 14:50:46 +08:00
|
|
|
cc.func = CC_CALLBACK_0(Effect3D::draw,std::get<1>(effect),transform);
|
2014-05-19 05:49:16 +08:00
|
|
|
renderer->addCommand(&cc);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Sprite3DEffectTest::Sprite3DEffectTest()
|
|
|
|
{
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
|
|
|
addNewSpriteWithCoords( Vec2(s.width/2, s.height/2) );
|
|
|
|
|
|
|
|
auto listener = EventListenerTouchAllAtOnce::create();
|
|
|
|
listener->onTouchesEnded = CC_CALLBACK_2(Sprite3DEffectTest::onTouchesEnded, this);
|
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Sprite3DEffectTest::title() const
|
|
|
|
{
|
|
|
|
return "Testing Sprite3D";
|
|
|
|
}
|
|
|
|
std::string Sprite3DEffectTest::subtitle() const
|
|
|
|
{
|
|
|
|
return "Sprite3d with effects";
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sprite3DEffectTest::addNewSpriteWithCoords(Vec2 p)
|
|
|
|
{
|
|
|
|
//option 2: load obj and assign the texture
|
|
|
|
auto sprite = EffectSprite3D::createFromObjFileAndTexture("Sprite3DTest/boss1.obj", "Sprite3DTest/boss.png");
|
|
|
|
Effect3DOutline* effect = Effect3DOutline::create();
|
|
|
|
effect->setOutlineColor(Vec3(1,0,0));
|
|
|
|
effect->setOutlineWidth(0.01f);
|
|
|
|
sprite->addEffect(effect, -1);
|
|
|
|
Effect3DOutline* effect2 = Effect3DOutline::create();
|
|
|
|
effect2->setOutlineWidth(0.02f);
|
|
|
|
effect2->setOutlineColor(Vec3(1,1,0));
|
|
|
|
sprite->addEffect(effect2, -2);
|
|
|
|
//sprite->setEffect3D(effect);
|
|
|
|
sprite->setScale(6.f);
|
|
|
|
|
|
|
|
//add to scene
|
|
|
|
addChild( sprite );
|
|
|
|
|
|
|
|
sprite->setPosition( Vec2( p.x, p.y) );
|
|
|
|
|
|
|
|
ActionInterval* action;
|
|
|
|
float random = CCRANDOM_0_1();
|
|
|
|
|
|
|
|
if( random < 0.20 )
|
|
|
|
action = ScaleBy::create(3, 2);
|
|
|
|
else if(random < 0.40)
|
|
|
|
action = RotateBy::create(3, 360);
|
|
|
|
else if( random < 0.60)
|
|
|
|
action = Blink::create(1, 3);
|
|
|
|
else if( random < 0.8 )
|
|
|
|
action = TintBy::create(2, 0, -255, -255);
|
|
|
|
else
|
|
|
|
action = FadeOut::create(2);
|
|
|
|
auto action_back = action->reverse();
|
2014-07-10 00:45:27 +08:00
|
|
|
auto seq = Sequence::create( action, action_back, nullptr );
|
2014-05-19 05:49:16 +08:00
|
|
|
|
|
|
|
sprite->runAction( RepeatForever::create(seq) );
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sprite3DEffectTest::onTouchesEnded(const std::vector<Touch*>& touches, Event* event)
|
|
|
|
{
|
|
|
|
for (auto touch: touches)
|
|
|
|
{
|
|
|
|
auto location = touch->getLocation();
|
|
|
|
|
|
|
|
addNewSpriteWithCoords( location );
|
|
|
|
}
|
|
|
|
}
|
2014-06-06 19:12:08 +08:00
|
|
|
|
|
|
|
Sprite3DWithSkinTest::Sprite3DWithSkinTest()
|
|
|
|
{
|
|
|
|
auto listener = EventListenerTouchAllAtOnce::create();
|
|
|
|
listener->onTouchesEnded = CC_CALLBACK_2(Sprite3DWithSkinTest::onTouchesEnded, this);
|
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
|
|
|
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
|
|
|
addNewSpriteWithCoords( Vec2(s.width/2, s.height/2) );
|
|
|
|
}
|
|
|
|
std::string Sprite3DWithSkinTest::title() const
|
|
|
|
{
|
2014-06-26 18:35:12 +08:00
|
|
|
return "Testing Sprite3D";
|
2014-06-06 19:12:08 +08:00
|
|
|
}
|
|
|
|
std::string Sprite3DWithSkinTest::subtitle() const
|
|
|
|
{
|
2014-06-11 15:07:37 +08:00
|
|
|
return "Tap screen to add more sprite3D";
|
2014-06-06 19:12:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Sprite3DWithSkinTest::addNewSpriteWithCoords(Vec2 p)
|
|
|
|
{
|
2014-06-27 11:44:44 +08:00
|
|
|
std::string fileName = "Sprite3DTest/orc.c3b";
|
2014-06-19 15:43:02 +08:00
|
|
|
auto sprite = Sprite3D::create(fileName);
|
2014-06-27 11:44:44 +08:00
|
|
|
sprite->setScale(3);
|
|
|
|
sprite->setRotation3D(Vec3(0,180,0));
|
2014-06-06 19:12:08 +08:00
|
|
|
addChild(sprite);
|
2014-06-12 18:26:42 +08:00
|
|
|
sprite->setPosition( Vec2( p.x, p.y) );
|
2014-06-13 19:26:46 +08:00
|
|
|
|
2014-07-09 09:53:15 +08:00
|
|
|
auto animation = Animation3D::create(fileName);
|
2014-06-24 14:43:20 +08:00
|
|
|
if (animation)
|
|
|
|
{
|
|
|
|
auto animate = Animate3D::create(animation);
|
2014-07-09 09:53:15 +08:00
|
|
|
bool inverse = (std::rand() % 3 == 0);
|
2014-06-24 14:43:20 +08:00
|
|
|
|
|
|
|
int rand2 = std::rand();
|
2014-07-09 09:53:15 +08:00
|
|
|
float speed = 1.0f;
|
2014-06-24 14:43:20 +08:00
|
|
|
if(rand2 % 3 == 1)
|
|
|
|
{
|
2014-07-09 09:53:15 +08:00
|
|
|
speed = animate->getSpeed() + CCRANDOM_0_1();
|
2014-06-24 14:43:20 +08:00
|
|
|
}
|
|
|
|
else if(rand2 % 3 == 2)
|
|
|
|
{
|
2014-07-09 09:53:15 +08:00
|
|
|
speed = animate->getSpeed() - 0.5 * CCRANDOM_0_1();
|
2014-06-24 14:43:20 +08:00
|
|
|
}
|
2014-07-09 09:53:15 +08:00
|
|
|
animate->setSpeed(inverse ? -speed : speed);
|
2014-06-24 14:43:20 +08:00
|
|
|
|
|
|
|
sprite->runAction(RepeatForever::create(animate));
|
|
|
|
}
|
2014-06-06 19:12:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Sprite3DWithSkinTest::onTouchesEnded(const std::vector<Touch*>& touches, Event* event)
|
|
|
|
{
|
|
|
|
for (auto touch: touches)
|
|
|
|
{
|
|
|
|
auto location = touch->getLocation();
|
|
|
|
|
|
|
|
addNewSpriteWithCoords( location );
|
|
|
|
}
|
|
|
|
}
|
2014-06-26 18:35:12 +08:00
|
|
|
|
|
|
|
Animate3DTest::Animate3DTest()
|
2014-06-27 16:36:01 +08:00
|
|
|
: _hurt(nullptr)
|
|
|
|
, _swim(nullptr)
|
|
|
|
, _sprite(nullptr)
|
|
|
|
, _moveAction(nullptr)
|
|
|
|
, _transTime(0.1f)
|
|
|
|
, _elapseTransTime(0.f)
|
2014-06-26 18:35:12 +08:00
|
|
|
{
|
|
|
|
addSprite3D();
|
2014-06-27 16:36:01 +08:00
|
|
|
|
|
|
|
auto listener = EventListenerTouchAllAtOnce::create();
|
|
|
|
listener->onTouchesEnded = CC_CALLBACK_2(Animate3DTest::onTouchesEnded, this);
|
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
|
|
|
|
|
|
|
scheduleUpdate();
|
|
|
|
}
|
|
|
|
|
|
|
|
Animate3DTest::~Animate3DTest()
|
|
|
|
{
|
|
|
|
CC_SAFE_RELEASE(_moveAction);
|
|
|
|
CC_SAFE_RELEASE(_hurt);
|
|
|
|
CC_SAFE_RELEASE(_swim);
|
2014-06-26 18:35:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string Animate3DTest::title() const
|
|
|
|
{
|
|
|
|
return "Testing Animate3D";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Animate3DTest::subtitle() const
|
|
|
|
{
|
2014-06-27 16:36:01 +08:00
|
|
|
return "Touch to beat the tortoise";
|
|
|
|
}
|
|
|
|
|
|
|
|
void Animate3DTest::update(float dt)
|
|
|
|
{
|
|
|
|
if (_state == State::HURT_TO_SWIMMING)
|
|
|
|
{
|
|
|
|
_elapseTransTime += dt;
|
|
|
|
float t = _elapseTransTime / _transTime;
|
|
|
|
|
|
|
|
if (t >= 1.f)
|
|
|
|
{
|
|
|
|
t = 1.f;
|
|
|
|
_sprite->stopAction(_hurt);
|
|
|
|
_state = State::SWIMMING;
|
|
|
|
}
|
|
|
|
_swim->setWeight(t);
|
|
|
|
_hurt->setWeight(1.f - t);
|
|
|
|
}
|
|
|
|
else if (_state == State::SWIMMING_TO_HURT)
|
|
|
|
{
|
|
|
|
_elapseTransTime += dt;
|
|
|
|
float t = _elapseTransTime / _transTime;
|
|
|
|
if (t >= 1.f)
|
|
|
|
{
|
|
|
|
t = 1.f;
|
|
|
|
_state = State::HURT;
|
|
|
|
}
|
|
|
|
_swim->setWeight(1.f - t);
|
|
|
|
_hurt->setWeight(t);
|
|
|
|
}
|
2014-06-26 18:35:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Animate3DTest::addSprite3D()
|
|
|
|
{
|
2014-06-27 17:02:08 +08:00
|
|
|
std::string fileName = "Sprite3DTest/tortoise.c3b";
|
2014-06-26 18:35:12 +08:00
|
|
|
auto sprite = Sprite3D::create(fileName);
|
|
|
|
sprite->setScale(0.1f);
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2014-06-27 16:36:01 +08:00
|
|
|
sprite->setPosition(Vec2(s.width * 4.f / 5.f, s.height / 2.f));
|
2014-06-27 17:02:08 +08:00
|
|
|
addChild(sprite);
|
2014-06-27 16:36:01 +08:00
|
|
|
_sprite = sprite;
|
2014-07-09 09:53:15 +08:00
|
|
|
auto animation = Animation3D::create(fileName);
|
2014-06-26 18:35:12 +08:00
|
|
|
if (animation)
|
|
|
|
{
|
2014-06-27 16:36:01 +08:00
|
|
|
auto animate = Animate3D::create(animation, 0.f, 1.933f);
|
|
|
|
sprite->runAction(RepeatForever::create(animate));
|
|
|
|
_swim = animate;
|
|
|
|
_swim->retain();
|
|
|
|
_hurt = Animate3D::create(animation, 1.933f, 2.8f);
|
|
|
|
_hurt->retain();
|
|
|
|
_state = State::SWIMMING;
|
2014-06-26 18:35:12 +08:00
|
|
|
}
|
|
|
|
|
2014-06-27 16:36:01 +08:00
|
|
|
_moveAction = MoveTo::create(4.f, Vec2(s.width / 5.f, s.height / 2.f));
|
|
|
|
_moveAction->retain();
|
|
|
|
auto seq = Sequence::create(_moveAction, CallFunc::create(CC_CALLBACK_0(Animate3DTest::reachEndCallBack, this)), nullptr);
|
|
|
|
seq->setTag(100);
|
|
|
|
sprite->runAction(seq);
|
2014-06-26 18:35:12 +08:00
|
|
|
}
|
|
|
|
|
2014-06-27 16:36:01 +08:00
|
|
|
void Animate3DTest::reachEndCallBack()
|
2014-06-26 18:35:12 +08:00
|
|
|
{
|
2014-06-27 16:36:01 +08:00
|
|
|
_sprite->stopActionByTag(100);
|
|
|
|
auto inverse = (MoveTo*)_moveAction->reverse();
|
|
|
|
inverse->retain();
|
|
|
|
_moveAction->release();
|
|
|
|
_moveAction = inverse;
|
|
|
|
auto rot = RotateBy::create(1.f, Vec3(0.f, 180.f, 0.f));
|
|
|
|
auto seq = Sequence::create(rot, _moveAction, CallFunc::create(CC_CALLBACK_0(Animate3DTest::reachEndCallBack, this)), nullptr);
|
|
|
|
seq->setTag(100);
|
|
|
|
_sprite->runAction(seq);
|
2014-06-26 18:35:12 +08:00
|
|
|
}
|
|
|
|
|
2014-06-27 16:36:01 +08:00
|
|
|
void Animate3DTest::renewCallBack()
|
2014-06-26 18:35:12 +08:00
|
|
|
{
|
2014-06-27 16:36:01 +08:00
|
|
|
_sprite->stopActionByTag(101);
|
|
|
|
_state = State::HURT_TO_SWIMMING;
|
2014-06-26 18:35:12 +08:00
|
|
|
}
|
|
|
|
|
2014-06-27 16:36:01 +08:00
|
|
|
void Animate3DTest::onTouchesEnded(const std::vector<Touch*>& touches, Event* event)
|
2014-06-26 18:35:12 +08:00
|
|
|
{
|
2014-06-27 16:36:01 +08:00
|
|
|
for (auto touch: touches)
|
2014-06-26 18:35:12 +08:00
|
|
|
{
|
2014-06-27 16:36:01 +08:00
|
|
|
auto location = touch->getLocation();
|
2014-06-26 18:35:12 +08:00
|
|
|
|
2014-06-27 16:36:01 +08:00
|
|
|
if (_sprite)
|
|
|
|
{
|
|
|
|
float len = (_sprite->getPosition() - location).length();
|
|
|
|
if (len < 40)
|
|
|
|
{
|
|
|
|
//hurt the tortoise
|
|
|
|
if (_state == State::SWIMMING)
|
|
|
|
{
|
|
|
|
_sprite->runAction(_hurt);
|
|
|
|
auto delay = DelayTime::create(_hurt->getDuration() - 0.1f);
|
2014-07-10 00:45:27 +08:00
|
|
|
auto seq = Sequence::create(delay, CallFunc::create(CC_CALLBACK_0(Animate3DTest::renewCallBack, this)), nullptr);
|
2014-06-27 16:36:01 +08:00
|
|
|
seq->setTag(101);
|
|
|
|
_sprite->runAction(seq);
|
|
|
|
_state = State::SWIMMING_TO_HURT;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2014-06-26 18:35:12 +08:00
|
|
|
}
|
|
|
|
}
|