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-12-19 10:22:42 +08:00
|
|
|
#include "base/CCAsyncTaskPool.h"
|
2014-06-09 18:37:58 +08:00
|
|
|
#include "3d/CCAnimation3D.h"
|
|
|
|
#include "3d/CCAnimate3D.h"
|
2014-07-29 10:49:06 +08:00
|
|
|
#include "3d/CCAttachNode.h"
|
2014-08-08 11:16:33 +08:00
|
|
|
#include "3d/CCRay.h"
|
2014-08-17 23:45:56 +08:00
|
|
|
#include "3d/CCSprite3D.h"
|
2014-08-22 13:25:26 +08:00
|
|
|
#include "renderer/CCVertexIndexBuffer.h"
|
2014-08-08 17:54:39 +08:00
|
|
|
#include "DrawNode3D.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-07-29 10:49:06 +08:00
|
|
|
CL(Sprite3DHitTest),
|
2014-12-05 15:01:30 +08:00
|
|
|
CL(AsyncLoadSprite3DTest),
|
2015-01-14 06:33:16 +08:00
|
|
|
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WP8)
|
2014-07-17 10:10:33 +08:00
|
|
|
// 3DEffect use custom shader which is not supported on WP8/WinRT yet.
|
2014-06-06 19:12:08 +08:00
|
|
|
CL(Sprite3DEffectTest),
|
2014-11-12 17:42:45 +08:00
|
|
|
CL(Sprite3DUVAnimationTest),
|
2014-12-17 13:31:35 +08:00
|
|
|
CL(Sprite3DFakeShadowTest),
|
|
|
|
CL(Sprite3DBasicToonShaderTest),
|
2014-12-18 16:09:29 +08:00
|
|
|
CL(Sprite3DLightMapTest),
|
2014-07-17 10:08:49 +08:00
|
|
|
#endif
|
2014-06-26 18:35:12 +08:00
|
|
|
CL(Sprite3DWithSkinTest),
|
2015-01-14 06:33:16 +08:00
|
|
|
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WP8)
|
2014-08-08 18:01:36 +08:00
|
|
|
CL(Sprite3DWithSkinOutlineTest),
|
|
|
|
#endif
|
2014-07-29 10:49:06 +08:00
|
|
|
CL(Animate3DTest),
|
2014-08-08 11:16:33 +08:00
|
|
|
CL(AttachmentTest),
|
2014-08-19 15:25:00 +08:00
|
|
|
CL(Sprite3DReskinTest),
|
2015-01-14 09:36:22 +08:00
|
|
|
CL(Sprite3DWithOBBPerformanceTest),
|
2014-12-29 14:36:25 +08:00
|
|
|
CL(Sprite3DMirrorTest),
|
2015-01-14 08:41:24 +08:00
|
|
|
CL(QuaternionTest),
|
2015-01-23 18:24:15 +08:00
|
|
|
CL(Sprite3DEmptyTest),
|
2015-01-29 16:21:33 +08:00
|
|
|
CL(UseCaseSprite3D),
|
|
|
|
CL(Sprite3DForceDepthTest)
|
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)
|
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
auto s = new (std::nothrow) Sprite3DTestScene();
|
2014-05-19 05:49:16 +08:00
|
|
|
s->addChild(restartSpriteTestAction());
|
|
|
|
|
|
|
|
Director::getInstance()->replaceScene(s);
|
|
|
|
s->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sprite3DTestDemo::nextCallback(Ref* sender)
|
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
auto s = new (std::nothrow) Sprite3DTestScene();
|
2014-05-19 05:49:16 +08:00
|
|
|
s->addChild( nextSpriteTestAction() );
|
|
|
|
Director::getInstance()->replaceScene(s);
|
|
|
|
s->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sprite3DTestDemo::backCallback(Ref* sender)
|
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
auto s = new (std::nothrow) Sprite3DTestScene();
|
2014-05-19 05:49:16 +08:00
|
|
|
s->addChild( backSpriteTestAction() );
|
|
|
|
Director::getInstance()->replaceScene(s);
|
|
|
|
s->release();
|
|
|
|
}
|
|
|
|
|
2015-01-14 08:41:24 +08:00
|
|
|
//------------------------------------------------------------------
|
2015-01-29 16:21:33 +08:00
|
|
|
//
|
|
|
|
// Sprite3DForceDepthTest
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
Sprite3DForceDepthTest::Sprite3DForceDepthTest()
|
|
|
|
{
|
|
|
|
auto orc = Sprite3D::create("Sprite3DTest/orc.c3b");
|
|
|
|
orc->setScale(5);
|
|
|
|
orc->setNormalizedPosition(Vec2(.5,.3));
|
|
|
|
orc->setPositionZ(40);
|
|
|
|
orc->setRotation3D(Vec3(0,180,0));
|
|
|
|
orc->setGlobalZOrder(-1);
|
|
|
|
|
|
|
|
addChild(orc);
|
|
|
|
|
|
|
|
auto ship = Sprite3D::create("Sprite3DTest/boss1.obj");
|
|
|
|
ship->setScale(5);
|
|
|
|
ship->setTexture("Sprite3DTest/boss.png");
|
|
|
|
ship->setNormalizedPosition(Vec2(.5,.5));
|
|
|
|
ship->setRotation3D(Vec3(90,0,0));
|
|
|
|
ship->setForceDepthWrite(true);
|
|
|
|
|
|
|
|
addChild(ship);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Sprite3DForceDepthTest::title() const
|
|
|
|
{
|
2015-01-29 16:24:32 +08:00
|
|
|
return "Force Depth Write Error Test";
|
2015-01-29 16:21:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string Sprite3DForceDepthTest::subtitle() const
|
|
|
|
{
|
|
|
|
return "Ship should always appear behind orc";
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
2015-01-14 08:41:24 +08:00
|
|
|
//
|
|
|
|
// Sprite3DEmptyTest
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
Sprite3DEmptyTest::Sprite3DEmptyTest()
|
|
|
|
{
|
|
|
|
auto s = Sprite3D::create();
|
|
|
|
s->setNormalizedPosition(Vec2(.5,.5));
|
|
|
|
auto l = Label::create();
|
|
|
|
l->setString("Test");
|
|
|
|
s->addChild(l);
|
|
|
|
addChild(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Sprite3DEmptyTest::title() const
|
|
|
|
{
|
|
|
|
return "Testing Sprite3D Container";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Sprite3DEmptyTest::subtitle() const
|
|
|
|
{
|
|
|
|
return "Sprite3D can act as containers for 2D objects";
|
|
|
|
}
|
2014-05-19 05:49:16 +08:00
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// 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";
|
|
|
|
}
|
|
|
|
|
2014-11-12 17:42:45 +08:00
|
|
|
//------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Sprite3DUVAnimationTest
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
|
|
|
|
Sprite3DUVAnimationTest::Sprite3DUVAnimationTest()
|
|
|
|
{
|
|
|
|
//the offset use to translating texture
|
2014-11-13 14:16:38 +08:00
|
|
|
_cylinder_texture_offset = 0;
|
|
|
|
_shining_duraion = 0;
|
2014-11-12 17:42:45 +08:00
|
|
|
Size visibleSize = Director::getInstance()->getVisibleSize();
|
|
|
|
|
|
|
|
//use custom camera
|
2015-01-15 10:07:23 +08:00
|
|
|
auto camera = Camera::createPerspective(60, visibleSize.width/visibleSize.height, 0.1f, 200);
|
2014-11-12 17:42:45 +08:00
|
|
|
camera->setCameraFlag(CameraFlag::USER1);
|
|
|
|
|
|
|
|
//create cylinder
|
|
|
|
auto cylinder = Sprite3D::create("Sprite3DTest/cylinder.c3b");
|
|
|
|
|
|
|
|
//create and set our custom shader
|
|
|
|
auto shader =GLProgram::createWithFilenames("Sprite3DTest/cylinder.vert","Sprite3DTest/cylinder.frag");
|
2014-11-13 14:16:38 +08:00
|
|
|
_state = GLProgramState::create(shader);
|
|
|
|
cylinder->setGLProgramState(_state);
|
2014-11-12 17:42:45 +08:00
|
|
|
|
2014-11-13 14:16:38 +08:00
|
|
|
_state->setUniformFloat("offset",_cylinder_texture_offset);
|
|
|
|
_state->setUniformFloat("duration",_shining_duraion);
|
2014-11-12 17:42:45 +08:00
|
|
|
//pass mesh's attribute to shader
|
|
|
|
long offset = 0;
|
|
|
|
auto attributeCount = cylinder->getMesh()->getMeshVertexAttribCount();
|
|
|
|
for (auto i = 0; i < attributeCount; i++) {
|
|
|
|
auto meshattribute = cylinder->getMesh()->getMeshVertexAttribute(i);
|
2014-11-13 14:16:38 +08:00
|
|
|
_state->setVertexAttribPointer(s_attributeNames[meshattribute.vertexAttrib],
|
2014-11-12 17:42:45 +08:00
|
|
|
meshattribute.size,
|
|
|
|
meshattribute.type,
|
|
|
|
GL_FALSE,
|
|
|
|
cylinder->getMesh()->getVertexSizeInBytes(),
|
|
|
|
(GLvoid*)offset);
|
|
|
|
offset += meshattribute.attribSizeBytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
//create the second texture for cylinder
|
|
|
|
auto shining_texture = Director::getInstance()->getTextureCache()->addImage("Sprite3DTest/caustics.png");
|
|
|
|
Texture2D::TexParams tRepeatParams;//set texture parameters
|
2014-12-12 15:05:55 +08:00
|
|
|
tRepeatParams.magFilter = GL_NEAREST;
|
|
|
|
tRepeatParams.minFilter = GL_NEAREST;
|
2014-11-12 17:42:45 +08:00
|
|
|
tRepeatParams.wrapS = GL_REPEAT;
|
|
|
|
tRepeatParams.wrapT = GL_REPEAT;
|
|
|
|
shining_texture->setTexParameters(tRepeatParams);
|
|
|
|
//pass the texture sampler to our custom shader
|
2014-11-13 14:16:38 +08:00
|
|
|
_state->setUniformTexture("caustics",shining_texture);
|
2014-11-12 17:42:45 +08:00
|
|
|
|
|
|
|
|
|
|
|
this->addChild(cylinder);
|
|
|
|
this->setCameraMask(2);
|
|
|
|
this->addChild(camera);
|
|
|
|
|
|
|
|
//adjust cylinder's position & rotation
|
|
|
|
cylinder->setPosition3D(Vec3(0,-15,-50));
|
|
|
|
cylinder->setRotation3D(Vec3(-90,0,0));
|
|
|
|
|
|
|
|
//the callback function update cylinder's texcoord
|
|
|
|
schedule(schedule_selector(Sprite3DUVAnimationTest::cylinderUpdate));
|
2014-12-30 11:48:39 +08:00
|
|
|
|
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
|
|
|
|
_backToForegroundListener = EventListenerCustom::create(EVENT_RENDERER_RECREATED,
|
|
|
|
[this](EventCustom*)
|
|
|
|
{
|
|
|
|
auto glProgram = _state->getGLProgram();
|
|
|
|
glProgram->reset();
|
|
|
|
glProgram->initWithFilenames("Sprite3DTest/cylinder.vert", "Sprite3DTest/cylinder.frag");
|
|
|
|
glProgram->link();
|
|
|
|
glProgram->updateUniforms();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundListener, -1);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
Sprite3DUVAnimationTest::~Sprite3DUVAnimationTest()
|
|
|
|
{
|
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
|
|
|
|
Director::getInstance()->getEventDispatcher()->removeEventListener(_backToForegroundListener);
|
|
|
|
#endif
|
2014-11-12 17:42:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string Sprite3DUVAnimationTest::title() const
|
|
|
|
{
|
2014-11-13 14:16:38 +08:00
|
|
|
return "Testing UV Animation";
|
2014-11-12 17:42:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string Sprite3DUVAnimationTest::subtitle() const
|
|
|
|
{
|
2014-11-13 14:16:38 +08:00
|
|
|
return "";
|
2014-11-12 17:42:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Sprite3DUVAnimationTest::cylinderUpdate(float dt)
|
|
|
|
{
|
|
|
|
//callback function to update cylinder's texcoord
|
|
|
|
static bool fade_in = true;
|
2014-11-13 14:16:38 +08:00
|
|
|
_cylinder_texture_offset += 0.3*dt;
|
|
|
|
_cylinder_texture_offset = (_cylinder_texture_offset >1) ? 0 : _cylinder_texture_offset;
|
2014-11-12 17:42:45 +08:00
|
|
|
if(fade_in)
|
|
|
|
{
|
2014-11-13 14:16:38 +08:00
|
|
|
_shining_duraion += 0.5*dt;
|
|
|
|
if(_shining_duraion>1) fade_in = false;
|
2014-11-12 17:42:45 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-11-13 14:16:38 +08:00
|
|
|
_shining_duraion -= 0.5*dt;
|
|
|
|
if(_shining_duraion<0) fade_in = true;
|
2014-11-12 17:42:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//pass the result to shader
|
2014-11-13 14:16:38 +08:00
|
|
|
_state->setUniformFloat("offset",_cylinder_texture_offset);
|
|
|
|
_state->setUniformFloat("duration",_shining_duraion);
|
2014-11-12 17:42:45 +08:00
|
|
|
}
|
|
|
|
|
2014-07-29 10:49:06 +08:00
|
|
|
//------------------------------------------------------------------
|
|
|
|
//
|
2014-12-19 17:39:26 +08:00
|
|
|
// Sprite3DFakeShadowTest
|
2014-07-29 10:49:06 +08:00
|
|
|
//
|
|
|
|
//------------------------------------------------------------------
|
2014-12-19 17:39:26 +08:00
|
|
|
Sprite3DFakeShadowTest::Sprite3DFakeShadowTest()
|
2014-07-29 10:49:06 +08:00
|
|
|
{
|
2014-12-19 17:39:26 +08:00
|
|
|
Size visibleSize = Director::getInstance()->getVisibleSize();
|
|
|
|
|
|
|
|
auto listener = EventListenerTouchAllAtOnce::create();
|
|
|
|
listener->onTouchesBegan = CC_CALLBACK_2(Sprite3DFakeShadowTest::onTouchesBegan, this);
|
|
|
|
listener->onTouchesMoved = CC_CALLBACK_2(Sprite3DFakeShadowTest::onTouchesMoved, this);
|
|
|
|
listener->onTouchesEnded = CC_CALLBACK_2(Sprite3DFakeShadowTest::onTouchesEnded, this);
|
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
|
|
|
|
|
|
|
auto layer = Layer::create();
|
|
|
|
addChild(layer,0);
|
|
|
|
//create Camera
|
2015-01-15 10:07:23 +08:00
|
|
|
_camera = Camera::createPerspective(60, visibleSize.width/visibleSize.height, 0.1f, 200);
|
2014-12-19 17:39:26 +08:00
|
|
|
_camera->setCameraFlag(CameraFlag::USER1);
|
|
|
|
_camera->setPosition3D(Vec3(0,20,25));
|
|
|
|
_camera->setRotation3D(Vec3(-60,0,0));
|
2014-12-17 13:31:35 +08:00
|
|
|
|
2014-12-19 17:39:26 +08:00
|
|
|
//create a plane
|
|
|
|
_plane = Sprite3D::create("Sprite3DTest/plane.c3t");
|
|
|
|
_plane->setRotation3D(Vec3(90,0,0));
|
2014-12-17 13:31:35 +08:00
|
|
|
|
2014-12-19 17:39:26 +08:00
|
|
|
// use custom shader
|
|
|
|
auto shader =GLProgram::createWithFilenames("Sprite3DTest/simple_shadow.vert","Sprite3DTest/simple_shadow.frag");
|
2014-12-30 11:48:39 +08:00
|
|
|
_state = GLProgramState::create(shader);
|
|
|
|
_plane->setGLProgramState(_state);
|
2014-12-17 13:31:35 +08:00
|
|
|
|
2014-12-19 17:39:26 +08:00
|
|
|
//pass mesh's attribute to shader
|
|
|
|
long offset = 0;
|
|
|
|
auto attributeCount = _plane->getMesh()->getMeshVertexAttribCount();
|
|
|
|
for (auto i = 0; i < attributeCount; i++) {
|
|
|
|
auto meshattribute = _plane->getMesh()->getMeshVertexAttribute(i);
|
2014-12-30 11:48:39 +08:00
|
|
|
_state->setVertexAttribPointer(s_attributeNames[meshattribute.vertexAttrib],
|
2014-12-19 17:39:26 +08:00
|
|
|
meshattribute.size,
|
|
|
|
meshattribute.type,
|
|
|
|
GL_FALSE,
|
|
|
|
_plane->getMesh()->getVertexSizeInBytes(),
|
|
|
|
(GLvoid*)offset);
|
|
|
|
offset += meshattribute.attribSizeBytes;
|
|
|
|
}
|
2014-12-30 11:48:39 +08:00
|
|
|
_state->setUniformMat4("u_model_matrix",_plane->getNodeToWorldTransform());
|
2014-12-17 13:31:35 +08:00
|
|
|
|
2014-12-19 17:39:26 +08:00
|
|
|
//create shadow texture
|
|
|
|
auto shadowTexture = Director::getInstance()->getTextureCache()->addImage("Sprite3DTest/shadowCircle.png");
|
|
|
|
Texture2D::TexParams tRepeatParams;//set texture parameters
|
|
|
|
tRepeatParams.magFilter = GL_LINEAR;
|
|
|
|
tRepeatParams.minFilter = GL_LINEAR;
|
|
|
|
tRepeatParams.wrapS = GL_CLAMP_TO_EDGE;
|
|
|
|
tRepeatParams.wrapT = GL_CLAMP_TO_EDGE;
|
|
|
|
shadowTexture->setTexParameters(tRepeatParams);
|
2014-12-30 11:48:39 +08:00
|
|
|
_state->setUniformTexture("u_shadowTexture",shadowTexture);
|
2014-12-19 17:39:26 +08:00
|
|
|
layer->addChild(_plane);
|
2014-12-17 13:31:35 +08:00
|
|
|
|
2014-12-19 17:39:26 +08:00
|
|
|
//create the orc
|
|
|
|
_orc = Sprite3D::create("Sprite3DTest/orc.c3b");
|
2015-01-15 10:07:23 +08:00
|
|
|
_orc->setScale(0.2f);
|
2014-12-19 17:39:26 +08:00
|
|
|
_orc->setRotation3D(Vec3(0,180,0));
|
|
|
|
_orc->setPosition3D(Vec3(0,0,10));
|
|
|
|
_targetPos = _orc->getPosition3D();
|
|
|
|
_plane->getGLProgramState()->setUniformVec3("u_target_pos",_orc->getPosition3D());
|
|
|
|
layer->addChild(_orc);
|
|
|
|
layer->addChild(_camera);
|
|
|
|
layer->setCameraMask(2);
|
2014-12-17 13:31:35 +08:00
|
|
|
|
2014-12-19 17:39:26 +08:00
|
|
|
schedule(CC_SCHEDULE_SELECTOR(Sprite3DFakeShadowTest::updateCamera), 0.0f);
|
2014-12-30 11:48:39 +08:00
|
|
|
|
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
|
|
|
|
_backToForegroundListener = EventListenerCustom::create(EVENT_RENDERER_RECREATED,
|
|
|
|
[this](EventCustom*)
|
|
|
|
{
|
|
|
|
auto glProgram = _state->getGLProgram();
|
|
|
|
glProgram->reset();
|
|
|
|
glProgram->initWithFilenames("Sprite3DTest/simple_shadow.vert","Sprite3DTest/simple_shadow.frag");
|
|
|
|
glProgram->link();
|
|
|
|
glProgram->updateUniforms();
|
|
|
|
|
|
|
|
_state->setUniformMat4("u_model_matrix",_plane->getNodeToWorldTransform());
|
|
|
|
|
|
|
|
//create shadow texture
|
|
|
|
auto shadowTexture = Director::getInstance()->getTextureCache()->addImage("Sprite3DTest/shadowCircle.png");
|
|
|
|
Texture2D::TexParams tRepeatParams;//set texture parameters
|
|
|
|
tRepeatParams.magFilter = GL_LINEAR;
|
|
|
|
tRepeatParams.minFilter = GL_LINEAR;
|
|
|
|
tRepeatParams.wrapS = GL_CLAMP_TO_EDGE;
|
|
|
|
tRepeatParams.wrapT = GL_CLAMP_TO_EDGE;
|
|
|
|
shadowTexture->setTexParameters(tRepeatParams);
|
|
|
|
_state->setUniformTexture("u_shadowTexture",shadowTexture);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundListener, -1);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
Sprite3DFakeShadowTest::~Sprite3DFakeShadowTest()
|
|
|
|
{
|
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
|
|
|
|
Director::getInstance()->getEventDispatcher()->removeEventListener(_backToForegroundListener);
|
|
|
|
#endif
|
2014-07-29 10:49:06 +08:00
|
|
|
}
|
|
|
|
|
2014-12-19 17:39:26 +08:00
|
|
|
std::string Sprite3DFakeShadowTest::title() const
|
2014-07-29 10:49:06 +08:00
|
|
|
{
|
2014-12-19 17:39:26 +08:00
|
|
|
return "fake shadow effect";
|
2014-07-29 10:49:06 +08:00
|
|
|
}
|
|
|
|
|
2014-12-19 17:39:26 +08:00
|
|
|
std::string Sprite3DFakeShadowTest::subtitle() const
|
2014-07-29 10:49:06 +08:00
|
|
|
{
|
2014-12-19 17:39:26 +08:00
|
|
|
return "touch the screen to move around";
|
2014-07-29 10:49:06 +08:00
|
|
|
}
|
|
|
|
|
2014-12-19 17:39:26 +08:00
|
|
|
void Sprite3DFakeShadowTest::Move(cocos2d::Ref* sender,int value)
|
2014-05-19 05:49:16 +08:00
|
|
|
{
|
2014-12-19 17:39:26 +08:00
|
|
|
_orc->setPositionX(_orc->getPositionX()+value);
|
2014-12-17 13:31:35 +08:00
|
|
|
|
2014-05-19 05:49:16 +08:00
|
|
|
}
|
|
|
|
|
2014-12-19 17:39:26 +08:00
|
|
|
void Sprite3DFakeShadowTest::updateCamera(float fDelta)
|
2014-09-18 16:38:35 +08:00
|
|
|
{
|
2014-12-19 17:39:26 +08:00
|
|
|
updateState(fDelta);
|
|
|
|
if(isState(_curState,State_Move))
|
|
|
|
{
|
|
|
|
move3D(fDelta);
|
|
|
|
if(isState(_curState,State_Rotate))
|
|
|
|
{
|
|
|
|
Vec3 curPos = _orc->getPosition3D();
|
|
|
|
|
|
|
|
Vec3 newFaceDir = _targetPos - curPos;
|
|
|
|
newFaceDir.y = 0;
|
|
|
|
newFaceDir.normalize();
|
|
|
|
Vec3 up;
|
|
|
|
_orc->getNodeToWorldTransform().getUpVector(&up);
|
|
|
|
up.normalize();
|
|
|
|
Vec3 right;
|
|
|
|
Vec3::cross(-newFaceDir,up,&right);
|
|
|
|
right.normalize();
|
|
|
|
Vec3 pos = Vec3(0,0,0);
|
|
|
|
Mat4 mat;
|
|
|
|
mat.m[0] = right.x;
|
|
|
|
mat.m[1] = right.y;
|
|
|
|
mat.m[2] = right.z;
|
|
|
|
mat.m[3] = 0.0f;
|
|
|
|
|
|
|
|
mat.m[4] = up.x;
|
|
|
|
mat.m[5] = up.y;
|
|
|
|
mat.m[6] = up.z;
|
|
|
|
mat.m[7] = 0.0f;
|
|
|
|
|
|
|
|
mat.m[8] = newFaceDir.x;
|
|
|
|
mat.m[9] = newFaceDir.y;
|
|
|
|
mat.m[10] = newFaceDir.z;
|
|
|
|
mat.m[11] = 0.0f;
|
2014-12-17 13:31:35 +08:00
|
|
|
|
2014-12-19 17:39:26 +08:00
|
|
|
mat.m[12] = pos.x;
|
|
|
|
mat.m[13] = pos.y;
|
|
|
|
mat.m[14] = pos.z;
|
|
|
|
mat.m[15] = 1.0f;
|
|
|
|
_orc->setAdditionalTransform(&mat);
|
|
|
|
}
|
|
|
|
}
|
2014-09-18 16:38:35 +08:00
|
|
|
}
|
|
|
|
|
2014-12-19 17:39:26 +08:00
|
|
|
|
|
|
|
void Sprite3DFakeShadowTest::move3D(float elapsedTime)
|
2014-05-19 05:49:16 +08:00
|
|
|
{
|
2014-12-19 17:39:26 +08:00
|
|
|
Vec3 curPos= _orc->getPosition3D();
|
|
|
|
Vec3 newFaceDir = _targetPos - curPos;
|
|
|
|
newFaceDir.y = 0.0f;
|
|
|
|
newFaceDir.normalize();
|
|
|
|
Vec3 offset = newFaceDir * 25.0f * elapsedTime;
|
|
|
|
curPos+=offset;
|
|
|
|
_orc->setPosition3D(curPos);
|
|
|
|
offset.x=offset.x;
|
|
|
|
offset.z=offset.z;
|
|
|
|
//pass the newest orc position
|
|
|
|
_plane->getGLProgramState()->setUniformVec3("u_target_pos",_orc->getPosition3D());
|
2014-05-19 05:49:16 +08:00
|
|
|
}
|
|
|
|
|
2014-12-19 17:39:26 +08:00
|
|
|
void Sprite3DFakeShadowTest::updateState(float elapsedTime)
|
2014-05-19 05:49:16 +08:00
|
|
|
{
|
2014-12-19 17:39:26 +08:00
|
|
|
Vec3 curPos= _orc->getPosition3D();
|
|
|
|
Vec3 curFaceDir;
|
|
|
|
_orc->getNodeToWorldTransform().getForwardVector(&curFaceDir);
|
|
|
|
curFaceDir=-curFaceDir;
|
|
|
|
curFaceDir.normalize();
|
|
|
|
Vec3 newFaceDir = _targetPos - curPos;
|
|
|
|
newFaceDir.y = 0.0f;
|
|
|
|
newFaceDir.normalize();
|
|
|
|
float cosAngle = std::fabs(Vec3::dot(curFaceDir,newFaceDir) - 1.0f);
|
|
|
|
float dist = curPos.distanceSquared(_targetPos);
|
|
|
|
if(dist<=4.0f)
|
2014-05-19 05:49:16 +08:00
|
|
|
{
|
2014-12-19 17:39:26 +08:00
|
|
|
if(cosAngle<=0.01f)
|
|
|
|
_curState = State_Idle;
|
|
|
|
else
|
|
|
|
_curState = State_Rotate;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(cosAngle>0.01f)
|
|
|
|
_curState = State_Rotate | State_Move;
|
|
|
|
else
|
|
|
|
_curState = State_Move;
|
2014-05-19 05:49:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-19 17:39:26 +08:00
|
|
|
bool Sprite3DFakeShadowTest::isState(unsigned int state,unsigned int bit) const
|
2014-08-12 09:56:25 +08:00
|
|
|
{
|
2014-12-19 17:39:26 +08:00
|
|
|
return (state & bit) == bit;
|
2014-05-19 05:49:16 +08:00
|
|
|
}
|
|
|
|
|
2014-12-19 17:39:26 +08:00
|
|
|
void Sprite3DFakeShadowTest::onTouchesBegan(const std::vector<Touch*>& touches, cocos2d::Event *event)
|
2014-05-19 05:49:16 +08:00
|
|
|
{
|
2014-12-17 13:31:35 +08:00
|
|
|
|
2014-05-19 05:49:16 +08:00
|
|
|
}
|
|
|
|
|
2014-12-19 17:39:26 +08:00
|
|
|
void Sprite3DFakeShadowTest::onTouchesMoved(const std::vector<Touch*>& touches, cocos2d::Event *event)
|
2014-05-19 05:49:16 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-12-19 17:39:26 +08:00
|
|
|
void Sprite3DFakeShadowTest::onTouchesEnded(const std::vector<Touch*>& touches, cocos2d::Event *event)
|
2014-05-19 05:49:16 +08:00
|
|
|
{
|
2014-12-19 17:39:26 +08:00
|
|
|
for ( auto &item: touches )
|
|
|
|
{
|
|
|
|
auto touch = item;
|
|
|
|
auto location = touch->getLocationInView();
|
|
|
|
if(_camera)
|
|
|
|
{
|
|
|
|
if(_orc )
|
|
|
|
{
|
|
|
|
Vec3 nearP(location.x, location.y, -1.0f), farP(location.x, location.y, 1.0f);
|
2014-05-27 14:50:46 +08:00
|
|
|
|
2014-12-19 17:39:26 +08:00
|
|
|
auto size = Director::getInstance()->getWinSize();
|
|
|
|
_camera->unproject(size, &nearP, &nearP);
|
|
|
|
_camera->unproject(size, &farP, &farP);
|
|
|
|
Vec3 dir(farP - nearP);
|
|
|
|
float dist=0.0f;
|
|
|
|
float ndd = Vec3::dot(Vec3(0,1,0),dir);
|
|
|
|
if(ndd == 0)
|
|
|
|
dist=0.0f;
|
|
|
|
float ndo = Vec3::dot(Vec3(0,1,0),nearP);
|
|
|
|
dist= (0 - ndo) / ndd;
|
|
|
|
Vec3 p = nearP + dist * dir;
|
2014-05-19 05:49:16 +08:00
|
|
|
|
2014-12-19 17:39:26 +08:00
|
|
|
if( p.x > 100)
|
|
|
|
p.x = 100;
|
|
|
|
if( p.x < -100)
|
|
|
|
p.x = -100;
|
|
|
|
if( p.z > 100)
|
|
|
|
p.z = 100;
|
|
|
|
if( p.z < -100)
|
|
|
|
p.z = -100;
|
2014-08-08 18:01:36 +08:00
|
|
|
|
2014-12-19 17:39:26 +08:00
|
|
|
_targetPos=p;
|
|
|
|
}
|
2014-08-08 18:01:36 +08:00
|
|
|
}
|
2014-05-19 05:49:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-19 17:39:26 +08:00
|
|
|
//------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Sprite3DBasicToonShaderTest
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
Sprite3DBasicToonShaderTest::Sprite3DBasicToonShaderTest()
|
2014-05-19 05:49:16 +08:00
|
|
|
{
|
2014-12-19 17:39:26 +08:00
|
|
|
Size visibleSize = Director::getInstance()->getVisibleSize();
|
2015-01-15 10:07:23 +08:00
|
|
|
auto _camera = Camera::createPerspective(60, visibleSize.width/visibleSize.height, 0.1f, 200);
|
2014-12-19 17:39:26 +08:00
|
|
|
_camera->setCameraFlag(CameraFlag::USER1);
|
|
|
|
// create a teapot
|
|
|
|
auto teapot = Sprite3D::create("Sprite3DTest/teapot.c3b");
|
|
|
|
//create and set our custom shader
|
|
|
|
auto shader =GLProgram::createWithFilenames("Sprite3DTest/toon.vert","Sprite3DTest/toon.frag");
|
2014-12-30 11:48:39 +08:00
|
|
|
_state = GLProgramState::create(shader);
|
|
|
|
teapot->setGLProgramState(_state);
|
2014-12-19 17:39:26 +08:00
|
|
|
teapot->setPosition3D(Vec3(0,-5,-20));
|
|
|
|
teapot->setRotation3D(Vec3(-90,180,0));
|
|
|
|
auto rotate_action = RotateBy::create(1.5,Vec3(0,30,0));
|
|
|
|
teapot->runAction(RepeatForever::create(rotate_action));
|
|
|
|
//pass mesh's attribute to shader
|
|
|
|
long offset = 0;
|
|
|
|
auto attributeCount = teapot->getMesh()->getMeshVertexAttribCount();
|
|
|
|
for (auto i = 0; i < attributeCount; i++) {
|
|
|
|
auto meshattribute = teapot->getMesh()->getMeshVertexAttribute(i);
|
2014-12-30 11:48:39 +08:00
|
|
|
_state->setVertexAttribPointer(s_attributeNames[meshattribute.vertexAttrib],
|
2014-12-19 17:39:26 +08:00
|
|
|
meshattribute.size,
|
|
|
|
meshattribute.type,
|
|
|
|
GL_FALSE,
|
|
|
|
teapot->getMesh()->getVertexSizeInBytes(),
|
|
|
|
(GLvoid*)offset);
|
|
|
|
offset += meshattribute.attribSizeBytes;
|
2014-05-19 05:49:16 +08:00
|
|
|
}
|
2014-12-19 17:39:26 +08:00
|
|
|
addChild(teapot);
|
|
|
|
addChild(_camera);
|
|
|
|
setCameraMask(2);
|
2014-12-30 11:48:39 +08:00
|
|
|
|
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
|
|
|
|
_backToForegroundListener = EventListenerCustom::create(EVENT_RENDERER_RECREATED,
|
|
|
|
[this](EventCustom*)
|
|
|
|
{
|
|
|
|
auto glProgram = _state->getGLProgram();
|
|
|
|
glProgram->reset();
|
|
|
|
glProgram->initWithFilenames("Sprite3DTest/toon.vert","Sprite3DTest/toon.frag");
|
|
|
|
glProgram->link();
|
|
|
|
glProgram->updateUniforms();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundListener, -1);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
Sprite3DBasicToonShaderTest::~Sprite3DBasicToonShaderTest()
|
|
|
|
{
|
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
|
|
|
|
Director::getInstance()->getEventDispatcher()->removeEventListener(_backToForegroundListener);
|
|
|
|
#endif
|
2014-05-19 05:49:16 +08:00
|
|
|
}
|
|
|
|
|
2014-12-19 17:39:26 +08:00
|
|
|
std::string Sprite3DBasicToonShaderTest::title() const
|
2014-05-19 05:49:16 +08:00
|
|
|
{
|
2014-12-19 17:39:26 +08:00
|
|
|
return "basic toon shader test";
|
2014-05-19 05:49:16 +08:00
|
|
|
}
|
|
|
|
|
2014-12-19 17:39:26 +08:00
|
|
|
std::string Sprite3DBasicToonShaderTest::subtitle() const
|
2014-05-19 05:49:16 +08:00
|
|
|
{
|
2014-12-19 17:39:26 +08:00
|
|
|
return " ";
|
2014-05-19 05:49:16 +08:00
|
|
|
}
|
|
|
|
|
2014-12-19 17:39:26 +08:00
|
|
|
//------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Sprite3DLightMapTest
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
Sprite3DLightMapTest::Sprite3DLightMapTest()
|
2014-05-19 05:49:16 +08:00
|
|
|
{
|
2014-12-19 17:39:26 +08:00
|
|
|
//the assets are from the OpenVR demo
|
|
|
|
//get the visible size.
|
|
|
|
Size visibleSize = Director::getInstance()->getVisibleSize();
|
2015-01-15 10:07:23 +08:00
|
|
|
_camera = Camera::createPerspective(60, visibleSize.width/visibleSize.height, 0.1f, 200);
|
2014-12-19 17:39:26 +08:00
|
|
|
_camera->setCameraFlag(CameraFlag::USER1);
|
|
|
|
_camera->setPosition3D(Vec3(0,25,15));
|
|
|
|
_camera->setRotation3D(Vec3(-35,0,0));
|
|
|
|
auto LightMapScene = Sprite3D::create("Sprite3DTest/LightMapScene.c3b");
|
2015-01-15 10:07:23 +08:00
|
|
|
LightMapScene->setScale(0.1f);
|
2014-12-19 17:39:26 +08:00
|
|
|
addChild(LightMapScene);
|
|
|
|
addChild(_camera);
|
|
|
|
setCameraMask(2);
|
|
|
|
|
|
|
|
//add a point light
|
|
|
|
auto light = PointLight::create(Vec3(35,75,-20.5),Color3B(255,255,255),150);
|
|
|
|
addChild(light);
|
|
|
|
//set the ambient light
|
|
|
|
auto ambient = AmbientLight::create(Color3B(55,55,55));
|
|
|
|
addChild(ambient);
|
|
|
|
|
|
|
|
//create a listener
|
|
|
|
auto listener = EventListenerTouchAllAtOnce::create();
|
|
|
|
listener->onTouchesMoved = CC_CALLBACK_2(Sprite3DLightMapTest::onTouchesMoved, this);
|
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
2014-05-19 05:49:16 +08:00
|
|
|
}
|
2014-12-30 11:48:39 +08:00
|
|
|
Sprite3DLightMapTest::~Sprite3DLightMapTest()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
std::string Sprite3DLightMapTest::title() const
|
2014-05-19 05:49:16 +08:00
|
|
|
{
|
2014-12-19 17:39:26 +08:00
|
|
|
return "light map test";
|
2014-05-19 05:49:16 +08:00
|
|
|
}
|
|
|
|
|
2014-12-19 17:39:26 +08:00
|
|
|
std::string Sprite3DLightMapTest::subtitle() const
|
2014-05-19 05:49:16 +08:00
|
|
|
{
|
2014-12-19 17:39:26 +08:00
|
|
|
return "drag the screen to move around";
|
2014-05-19 05:49:16 +08:00
|
|
|
}
|
|
|
|
|
2014-12-19 17:39:26 +08:00
|
|
|
void Sprite3DLightMapTest::onTouchesMoved(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event* event)
|
2014-05-19 05:49:16 +08:00
|
|
|
{
|
2014-12-19 17:39:26 +08:00
|
|
|
if(touches.size()==1)
|
2014-05-19 05:49:16 +08:00
|
|
|
{
|
2014-12-19 17:39:26 +08:00
|
|
|
float delta = Director::getInstance()->getDeltaTime();
|
|
|
|
auto touch = touches[0];
|
|
|
|
auto location = touch->getLocation();
|
|
|
|
auto PreviousLocation = touch->getPreviousLocation();
|
|
|
|
Point newPos = PreviousLocation - location;
|
2014-12-19 11:22:59 +08:00
|
|
|
|
2014-12-19 17:39:26 +08:00
|
|
|
Vec3 cameraDir;
|
|
|
|
Vec3 cameraRightDir;
|
|
|
|
_camera->getNodeToWorldTransform().getForwardVector(&cameraDir);
|
|
|
|
cameraDir.normalize();
|
|
|
|
cameraDir.y=0;
|
|
|
|
_camera->getNodeToWorldTransform().getRightVector(&cameraRightDir);
|
|
|
|
cameraRightDir.normalize();
|
|
|
|
cameraRightDir.y=0;
|
|
|
|
Vec3 cameraPos= _camera->getPosition3D();
|
|
|
|
cameraPos+=cameraDir*newPos.y*delta;
|
|
|
|
cameraPos+=cameraRightDir*newPos.x*delta;
|
|
|
|
_camera->setPosition3D(cameraPos);
|
2014-05-19 05:49:16 +08:00
|
|
|
}
|
2014-05-27 14:50:46 +08:00
|
|
|
}
|
|
|
|
|
2014-07-29 10:49:06 +08:00
|
|
|
//------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Sprite3DHitTest
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
Sprite3DHitTest::Sprite3DHitTest()
|
|
|
|
{
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
|
|
|
|
|
|
|
auto sprite1 = Sprite3D::create("Sprite3DTest/boss1.obj");
|
|
|
|
|
|
|
|
sprite1->setScale(4.f);
|
|
|
|
sprite1->setTexture("Sprite3DTest/boss.png");
|
|
|
|
sprite1->setPosition( Vec2(s.width/2, s.height/2) );
|
|
|
|
|
|
|
|
//add to scene
|
|
|
|
addChild( sprite1 );
|
|
|
|
sprite1->runAction(RepeatForever::create(RotateBy::create(3, 360)));
|
|
|
|
|
|
|
|
auto sprite2 = Sprite3D::create("Sprite3DTest/boss1.obj");
|
|
|
|
|
|
|
|
sprite2->setScale(4.f);
|
|
|
|
sprite2->setTexture("Sprite3DTest/boss.png");
|
|
|
|
sprite2->setPosition( Vec2(s.width/2, s.height/2) );
|
|
|
|
sprite2->setAnchorPoint(Vec2(0.5, 0.5));
|
|
|
|
|
|
|
|
//add to scene
|
|
|
|
addChild( sprite2 );
|
|
|
|
sprite2->runAction(RepeatForever::create(RotateBy::create(3, -360)));
|
|
|
|
|
|
|
|
|
|
|
|
// Make sprite1 touchable
|
|
|
|
auto listener1 = EventListenerTouchOneByOne::create();
|
|
|
|
listener1->setSwallowTouches(true);
|
|
|
|
|
|
|
|
listener1->onTouchBegan = [](Touch* touch, Event* event){
|
|
|
|
auto target = static_cast<Sprite3D*>(event->getCurrentTarget());
|
2014-09-09 17:05:33 +08:00
|
|
|
|
|
|
|
Rect rect = target->getBoundingBox();
|
|
|
|
if (rect.containsPoint(touch->getLocation()))
|
2014-07-29 10:49:06 +08:00
|
|
|
{
|
2014-09-09 17:05:33 +08:00
|
|
|
log("sprite3d began... x = %f, y = %f", touch->getLocation().x, touch->getLocation().y);
|
2014-07-29 10:49:06 +08:00
|
|
|
target->setOpacity(100);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
listener1->onTouchMoved = [](Touch* touch, Event* event){
|
|
|
|
auto target = static_cast<Sprite3D*>(event->getCurrentTarget());
|
|
|
|
target->setPosition(target->getPosition() + touch->getDelta());
|
|
|
|
};
|
|
|
|
|
|
|
|
listener1->onTouchEnded = [=](Touch* touch, Event* event){
|
|
|
|
auto target = static_cast<Sprite3D*>(event->getCurrentTarget());
|
|
|
|
log("sprite3d onTouchesEnded.. ");
|
|
|
|
target->setOpacity(255);
|
|
|
|
};
|
|
|
|
|
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener1, sprite1);
|
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener1->clone(), sprite2);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Sprite3DHitTest::title() const
|
|
|
|
{
|
|
|
|
return "Testing Sprite3D Touch in 2D";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Sprite3DHitTest::subtitle() const
|
|
|
|
{
|
|
|
|
return "Tap Sprite3D and Drag";
|
|
|
|
}
|
|
|
|
|
2014-05-19 05:49:16 +08:00
|
|
|
void Sprite3DTestScene::runThisTest()
|
|
|
|
{
|
|
|
|
auto layer = nextSpriteTestAction();
|
|
|
|
addChild(layer);
|
|
|
|
|
|
|
|
Director::getInstance()->replaceScene(this);
|
|
|
|
}
|
|
|
|
|
2014-09-18 16:38:35 +08:00
|
|
|
Sprite3DTestScene::Sprite3DTestScene()
|
|
|
|
{
|
2014-09-25 16:27:31 +08:00
|
|
|
|
2014-09-18 16:38:35 +08:00
|
|
|
}
|
|
|
|
|
2014-05-19 05:49:16 +08:00
|
|
|
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)
|
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
auto sprite = new (std::nothrow) EffectSprite3D();
|
2014-05-19 05:49:16 +08:00
|
|
|
if (sprite && sprite->initWithFile(objFilePath))
|
|
|
|
{
|
|
|
|
sprite->autorelease();
|
2014-08-08 18:01:36 +08:00
|
|
|
if(textureFilePath.size() > 0)
|
|
|
|
sprite->setTexture(textureFilePath);
|
2014-05-19 05:49:16 +08:00
|
|
|
return sprite;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(sprite);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-08-12 09:56:25 +08:00
|
|
|
EffectSprite3D* EffectSprite3D::create(const std::string &path)
|
|
|
|
{
|
|
|
|
if (path.length() < 4)
|
|
|
|
CCASSERT(false, "improper name specified when creating Sprite3D");
|
|
|
|
|
2014-08-28 07:31:57 +08:00
|
|
|
auto sprite = new (std::nothrow) EffectSprite3D();
|
2014-08-12 09:56:25 +08:00
|
|
|
if (sprite && sprite->initWithFile(path))
|
|
|
|
{
|
|
|
|
sprite->autorelease();
|
2014-05-19 05:49:16 +08:00
|
|
|
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";
|
2014-08-08 18:01:36 +08:00
|
|
|
|
2014-08-14 10:04:01 +08:00
|
|
|
const std::string Effect3DOutline::_vertSkinnedShaderFile = "Shaders3D/SkinnedOutline.vert";
|
2014-08-08 18:01:36 +08:00
|
|
|
const std::string Effect3DOutline::_fragSkinnedShaderFile = "Shaders3D/OutLine.frag";
|
|
|
|
const std::string Effect3DOutline::_keySkinnedInGLProgramCache = "Effect3DLibrary_Outline";
|
|
|
|
GLProgram* Effect3DOutline::getOrCreateProgram(bool isSkinned /* = false */ )
|
2014-05-19 05:49:16 +08:00
|
|
|
{
|
2014-08-08 18:01:36 +08:00
|
|
|
if(isSkinned)
|
|
|
|
{
|
|
|
|
auto program = GLProgramCache::getInstance()->getGLProgram(_keySkinnedInGLProgramCache);
|
|
|
|
if(program == nullptr)
|
|
|
|
{
|
|
|
|
program = GLProgram::createWithFilenames(_vertSkinnedShaderFile, _fragSkinnedShaderFile);
|
|
|
|
GLProgramCache::getInstance()->addGLProgram(program, _keySkinnedInGLProgramCache);
|
|
|
|
}
|
|
|
|
return program;
|
|
|
|
}
|
|
|
|
else
|
2014-05-19 05:49:16 +08:00
|
|
|
{
|
2014-08-08 18:01:36 +08:00
|
|
|
auto program = GLProgramCache::getInstance()->getGLProgram(_keyInGLProgramCache);
|
|
|
|
if(program == nullptr)
|
|
|
|
{
|
|
|
|
program = GLProgram::createWithFilenames(_vertShaderFile, _fragShaderFile);
|
|
|
|
GLProgramCache::getInstance()->addGLProgram(program, _keyInGLProgramCache);
|
|
|
|
}
|
|
|
|
return program;
|
2014-05-19 05:49:16 +08:00
|
|
|
}
|
2014-08-08 18:01:36 +08:00
|
|
|
|
2014-05-19 05:49:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Effect3DOutline* Effect3DOutline::create()
|
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
Effect3DOutline* effect = new (std::nothrow) Effect3DOutline();
|
2014-05-19 05:49:16 +08:00
|
|
|
if(effect && effect->init())
|
|
|
|
{
|
|
|
|
effect->autorelease();
|
|
|
|
return effect;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(effect);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Effect3DOutline::init()
|
|
|
|
{
|
|
|
|
|
|
|
|
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-11-22 02:30:49 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
|
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-11-22 02:30:49 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
|
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;
|
2014-08-12 09:51:03 +08:00
|
|
|
if(_glProgramState)
|
|
|
|
_glProgramState->setUniformVec3("OutLineColor", _outlineColor);
|
2014-05-19 05:49:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Effect3DOutline::setOutlineWidth(float width)
|
|
|
|
{
|
|
|
|
if(_outlineWidth != width)
|
|
|
|
{
|
|
|
|
_outlineWidth = width;
|
2014-08-12 09:51:03 +08:00
|
|
|
if(_glProgramState)
|
|
|
|
_glProgramState->setUniformFloat("OutlineWidth", _outlineWidth);
|
2014-05-19 05:49:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-08-11 18:11:15 +08:00
|
|
|
GLProgram* glprogram;
|
2014-08-22 13:25:26 +08:00
|
|
|
if(!sprite->getMesh()->getSkin())
|
2014-08-11 18:11:15 +08:00
|
|
|
glprogram = GLProgram::createWithFilenames(_vertShaderFile, _fragShaderFile);
|
|
|
|
else
|
|
|
|
glprogram = GLProgram::createWithFilenames(_vertSkinnedShaderFile, _fragSkinnedShaderFile);
|
|
|
|
|
|
|
|
_glProgramState = GLProgramState::create(glprogram);
|
|
|
|
|
|
|
|
_glProgramState->retain();
|
|
|
|
_glProgramState->setUniformVec3("OutLineColor", _outlineColor);
|
|
|
|
_glProgramState->setUniformFloat("OutlineWidth", _outlineWidth);
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-08-11 18:11:15 +08:00
|
|
|
static void MatrixPalleteCallBack( GLProgram* glProgram, Uniform* uniform, int paletteSize, const float* palette)
|
|
|
|
{
|
|
|
|
glUniform4fv( uniform->location, (GLsizei)paletteSize, (const float*)palette );
|
|
|
|
}
|
|
|
|
|
2014-05-27 14:50:46 +08:00
|
|
|
void Effect3DOutline::draw(const Mat4 &transform)
|
|
|
|
{
|
2014-05-19 05:49:16 +08:00
|
|
|
//draw
|
2014-07-29 10:49:06 +08:00
|
|
|
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-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());
|
2014-08-11 18:11:15 +08:00
|
|
|
|
2014-08-22 13:25:26 +08:00
|
|
|
auto skin = _sprite->getMesh()->getSkin();
|
|
|
|
if(_sprite && skin)
|
2014-08-11 18:11:15 +08:00
|
|
|
{
|
|
|
|
auto function = std::bind(MatrixPalleteCallBack, std::placeholders::_1, std::placeholders::_2,
|
2014-08-22 13:25:26 +08:00
|
|
|
skin->getMatrixPaletteSize(), (float*)skin->getMatrixPalette());
|
2014-08-11 18:11:15 +08:00
|
|
|
_glProgramState->setUniformCallback("u_matrixPalette", function);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(_sprite)
|
|
|
|
_glProgramState->apply(transform);
|
|
|
|
|
2014-08-22 13:25:26 +08:00
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh->getIndexBuffer());
|
2014-09-11 15:46:32 +08:00
|
|
|
glDrawElements(mesh->getPrimitiveType(), (GLsizei)mesh->getIndexCount(), mesh->getIndexFormat(), 0);
|
2014-08-22 13:25:26 +08:00
|
|
|
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, mesh->getIndexCount());
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
{
|
2015-01-16 06:00:49 +08:00
|
|
|
_command.init(_globalZOrder, transform, flags);
|
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();
|
2014-08-11 18:11:15 +08:00
|
|
|
sprite->addEffect(effect, -1);
|
2014-05-19 05:49:16 +08:00
|
|
|
effect->setOutlineColor(Vec3(1,0,0));
|
|
|
|
effect->setOutlineWidth(0.01f);
|
2014-08-11 18:11:15 +08:00
|
|
|
|
2014-05-19 05:49:16 +08:00
|
|
|
Effect3DOutline* effect2 = Effect3DOutline::create();
|
2014-08-11 18:11:15 +08:00
|
|
|
sprite->addEffect(effect2, -2);
|
2014-05-19 05:49:16 +08:00
|
|
|
effect2->setOutlineWidth(0.02f);
|
|
|
|
effect2->setOutlineColor(Vec3(1,1,0));
|
|
|
|
//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
|
|
|
|
2014-12-05 15:01:30 +08:00
|
|
|
AsyncLoadSprite3DTest::AsyncLoadSprite3DTest()
|
|
|
|
{
|
|
|
|
_paths.push_back("Sprite3DTest/boss.obj");
|
|
|
|
_paths.push_back("Sprite3DTest/girl.c3b");
|
|
|
|
_paths.push_back("Sprite3DTest/orc.c3b");
|
|
|
|
_paths.push_back("Sprite3DTest/ReskinGirl.c3b");
|
|
|
|
_paths.push_back("Sprite3DTest/axe.c3b");
|
|
|
|
|
2014-12-05 15:44:09 +08:00
|
|
|
TTFConfig ttfConfig("fonts/arial.ttf", 15);
|
2014-12-05 15:01:30 +08:00
|
|
|
auto label1 = Label::createWithTTF(ttfConfig,"AsyncLoad Sprite3D");
|
|
|
|
auto item1 = MenuItemLabel::create(label1,CC_CALLBACK_1(AsyncLoadSprite3DTest::menuCallback_asyncLoadSprite,this) );
|
|
|
|
|
2014-12-05 15:44:09 +08:00
|
|
|
auto s = Director::getInstance()->getWinSize();
|
|
|
|
item1->setPosition( s.width * .5f, s.height * .8f);
|
2014-12-05 15:01:30 +08:00
|
|
|
|
|
|
|
auto pMenu1 = CCMenu::create(item1, nullptr);
|
|
|
|
pMenu1->setPosition(Vec2(0,0));
|
|
|
|
this->addChild(pMenu1, 10);
|
|
|
|
|
|
|
|
auto node = Node::create();
|
|
|
|
node->setTag(101);
|
|
|
|
this->addChild(node);
|
2014-12-05 15:44:09 +08:00
|
|
|
|
|
|
|
menuCallback_asyncLoadSprite(nullptr);
|
2014-12-05 15:01:30 +08:00
|
|
|
}
|
2014-12-19 10:22:42 +08:00
|
|
|
|
|
|
|
AsyncLoadSprite3DTest::~AsyncLoadSprite3DTest()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-12-05 15:01:30 +08:00
|
|
|
std::string AsyncLoadSprite3DTest::title() const
|
|
|
|
{
|
2014-12-05 16:44:49 +08:00
|
|
|
return "Testing Sprite3D::createAsync";
|
2014-12-05 15:01:30 +08:00
|
|
|
}
|
|
|
|
std::string AsyncLoadSprite3DTest::subtitle() const
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
void AsyncLoadSprite3DTest::menuCallback_asyncLoadSprite(Ref* sender)
|
|
|
|
{
|
2014-12-19 10:22:42 +08:00
|
|
|
//Note that you must stop the tasks before leaving the scene.
|
|
|
|
AsyncTaskPool::getInstance()->stopTasks(AsyncTaskPool::TaskType::TASK_IO);
|
|
|
|
|
2014-12-05 15:01:30 +08:00
|
|
|
auto node = getChildByTag(101);
|
|
|
|
node->removeAllChildren(); //remove all loaded sprite
|
|
|
|
|
|
|
|
//remove cache data
|
|
|
|
Sprite3DCache::getInstance()->removeAllSprite3DData();
|
|
|
|
long index = 0;
|
|
|
|
for (const auto& path : _paths) {
|
|
|
|
Sprite3D::createAsync(path, CC_CALLBACK_2(AsyncLoadSprite3DTest::asyncLoad_Callback, this), (void*)index++);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AsyncLoadSprite3DTest::asyncLoad_Callback(Sprite3D* sprite, void* param)
|
|
|
|
{
|
|
|
|
long index = (long)param;
|
|
|
|
auto node = getChildByTag(101);
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
|
|
|
float width = s.width / _paths.size();
|
|
|
|
Vec2 point(width * (0.5f + index), s.height / 2.f);
|
|
|
|
sprite->setPosition(point);
|
|
|
|
node->addChild(sprite);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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-08-11 18:11:15 +08:00
|
|
|
auto sprite = EffectSprite3D::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
|
|
|
|
2014-08-08 18:01:36 +08:00
|
|
|
Sprite3DWithSkinOutlineTest::Sprite3DWithSkinOutlineTest()
|
|
|
|
{
|
|
|
|
auto listener = EventListenerTouchAllAtOnce::create();
|
|
|
|
listener->onTouchesEnded = CC_CALLBACK_2(Sprite3DWithSkinOutlineTest::onTouchesEnded, this);
|
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
|
|
|
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
|
|
|
addNewSpriteWithCoords( Vec2(s.width/2, s.height/2) );
|
|
|
|
}
|
|
|
|
std::string Sprite3DWithSkinOutlineTest::title() const
|
|
|
|
{
|
|
|
|
return "Testing Sprite3D for skinned outline";
|
|
|
|
}
|
|
|
|
std::string Sprite3DWithSkinOutlineTest::subtitle() const
|
|
|
|
{
|
|
|
|
return "Tap screen to add more sprite3D";
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sprite3DWithSkinOutlineTest::addNewSpriteWithCoords(Vec2 p)
|
|
|
|
{
|
|
|
|
|
|
|
|
std::string fileName = "Sprite3DTest/orc.c3b";
|
2014-08-12 09:56:25 +08:00
|
|
|
auto sprite = EffectSprite3D::create(fileName);
|
2014-08-08 18:01:36 +08:00
|
|
|
|
|
|
|
Effect3DOutline* effect = Effect3DOutline::create();
|
|
|
|
effect->setOutlineColor(Vec3(1,0,0));
|
|
|
|
effect->setOutlineWidth(0.01f);
|
2014-08-12 09:51:03 +08:00
|
|
|
sprite->addEffect(effect, -1);
|
|
|
|
|
2014-08-11 18:11:15 +08:00
|
|
|
|
2014-08-08 18:01:36 +08:00
|
|
|
Effect3DOutline* effect2 = Effect3DOutline::create();
|
|
|
|
effect2->setOutlineWidth(0.02f);
|
|
|
|
effect2->setOutlineColor(Vec3(1,1,0));
|
2014-08-12 09:51:03 +08:00
|
|
|
sprite->addEffect(effect2, -2);
|
|
|
|
|
2014-08-08 18:01:36 +08:00
|
|
|
|
|
|
|
sprite->setScale(3);
|
|
|
|
sprite->setRotation3D(Vec3(0,180,0));
|
|
|
|
addChild(sprite);
|
|
|
|
sprite->setPosition( Vec2( p.x, p.y) );
|
|
|
|
|
|
|
|
auto animation = Animation3D::create(fileName);
|
|
|
|
if (animation)
|
|
|
|
{
|
|
|
|
auto animate = Animate3D::create(animation);
|
|
|
|
bool inverse = (std::rand() % 3 == 0);
|
|
|
|
|
|
|
|
int rand2 = std::rand();
|
|
|
|
float speed = 1.0f;
|
|
|
|
if(rand2 % 3 == 1)
|
|
|
|
{
|
|
|
|
speed = animate->getSpeed() + CCRANDOM_0_1();
|
|
|
|
}
|
|
|
|
else if(rand2 % 3 == 2)
|
|
|
|
{
|
|
|
|
speed = animate->getSpeed() - 0.5 * CCRANDOM_0_1();
|
|
|
|
}
|
|
|
|
animate->setSpeed(inverse ? -speed : speed);
|
|
|
|
|
|
|
|
sprite->runAction(RepeatForever::create(animate));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sprite3DWithSkinOutlineTest::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)
|
|
|
|
, _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;
|
|
|
|
|
2014-08-29 18:25:42 +08:00
|
|
|
if (_elapseTransTime >= Animate3D::getTransitionTime())
|
2014-06-27 16:36:01 +08:00
|
|
|
{
|
|
|
|
_sprite->stopAction(_hurt);
|
|
|
|
_state = State::SWIMMING;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (_state == State::SWIMMING_TO_HURT)
|
|
|
|
{
|
|
|
|
_elapseTransTime += dt;
|
2014-08-29 18:25:42 +08:00
|
|
|
if (_elapseTransTime >= Animate3D::getTransitionTime())
|
2014-06-27 16:36:01 +08:00
|
|
|
{
|
2014-08-29 18:25:42 +08:00
|
|
|
_sprite->stopAction(_swim);
|
2014-06-27 16:36:01 +08:00
|
|
|
_state = State::HURT;
|
|
|
|
}
|
|
|
|
}
|
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);
|
2014-08-29 18:25:42 +08:00
|
|
|
_swim = RepeatForever::create(animate);
|
|
|
|
sprite->runAction(_swim);
|
|
|
|
|
2014-06-27 16:36:01 +08:00
|
|
|
_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-08-29 18:25:42 +08:00
|
|
|
//rerun swim action
|
|
|
|
_sprite->runAction(_swim);
|
2014-06-27 16:36:01 +08:00
|
|
|
_state = State::HURT_TO_SWIMMING;
|
2014-08-29 18:25:42 +08:00
|
|
|
_elapseTransTime = 0.0f;
|
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)
|
|
|
|
{
|
2014-08-29 18:25:42 +08:00
|
|
|
_elapseTransTime = 0.0f;
|
|
|
|
_state = State::SWIMMING_TO_HURT;
|
|
|
|
_sprite->stopAction(_hurt);
|
2014-06-27 16:36:01 +08:00
|
|
|
_sprite->runAction(_hurt);
|
2014-08-29 18:25:42 +08:00
|
|
|
auto delay = DelayTime::create(_hurt->getDuration() - Animate3D::getTransitionTime());
|
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);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2014-06-26 18:35:12 +08:00
|
|
|
}
|
|
|
|
}
|
2014-07-29 10:49:06 +08:00
|
|
|
|
|
|
|
AttachmentTest::AttachmentTest()
|
|
|
|
: _hasWeapon(false)
|
|
|
|
, _sprite(nullptr)
|
|
|
|
{
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
|
|
|
addNewSpriteWithCoords( Vec2(s.width/2, s.height/2) );
|
|
|
|
|
|
|
|
auto listener = EventListenerTouchAllAtOnce::create();
|
|
|
|
listener->onTouchesEnded = CC_CALLBACK_2(AttachmentTest::onTouchesEnded, this);
|
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
|
|
|
}
|
|
|
|
std::string AttachmentTest::title() const
|
|
|
|
{
|
|
|
|
return "Testing Sprite3D Attachment";
|
|
|
|
}
|
|
|
|
std::string AttachmentTest::subtitle() const
|
|
|
|
{
|
|
|
|
return "touch to switch weapon";
|
|
|
|
}
|
|
|
|
|
|
|
|
void AttachmentTest::addNewSpriteWithCoords(Vec2 p)
|
|
|
|
{
|
|
|
|
std::string fileName = "Sprite3DTest/orc.c3b";
|
|
|
|
auto sprite = Sprite3D::create(fileName);
|
|
|
|
sprite->setScale(5);
|
|
|
|
sprite->setRotation3D(Vec3(0,180,0));
|
|
|
|
addChild(sprite);
|
|
|
|
sprite->setPosition( Vec2( p.x, p.y) );
|
|
|
|
|
|
|
|
//test attach
|
|
|
|
auto sp = Sprite3D::create("Sprite3DTest/axe.c3b");
|
|
|
|
sprite->getAttachNode("Bip001 R Hand")->addChild(sp);
|
|
|
|
|
|
|
|
auto animation = Animation3D::create(fileName);
|
|
|
|
if (animation)
|
|
|
|
{
|
|
|
|
auto animate = Animate3D::create(animation);
|
|
|
|
|
|
|
|
sprite->runAction(RepeatForever::create(animate));
|
|
|
|
}
|
|
|
|
_sprite = sprite;
|
|
|
|
_hasWeapon = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AttachmentTest::onTouchesEnded(const std::vector<Touch*>& touches, Event* event)
|
|
|
|
{
|
|
|
|
if (_hasWeapon)
|
|
|
|
{
|
|
|
|
_sprite->removeAllAttachNode();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto sp = Sprite3D::create("Sprite3DTest/axe.c3b");
|
|
|
|
_sprite->getAttachNode("Bip001 R Hand")->addChild(sp);
|
|
|
|
}
|
|
|
|
_hasWeapon = !_hasWeapon;
|
|
|
|
}
|
2014-08-19 15:25:00 +08:00
|
|
|
Sprite3DReskinTest::Sprite3DReskinTest()
|
|
|
|
: _sprite(nullptr)
|
2014-08-19 14:03:41 +08:00
|
|
|
{
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
|
|
|
addNewSpriteWithCoords( Vec2(s.width/2, s.height/2) );
|
|
|
|
|
|
|
|
auto listener = EventListenerTouchAllAtOnce::create();
|
2014-08-19 15:25:00 +08:00
|
|
|
listener->onTouchesEnded = CC_CALLBACK_2(Sprite3DReskinTest::onTouchesEnded, this);
|
2014-08-19 14:03:41 +08:00
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
2014-09-04 16:27:18 +08:00
|
|
|
TTFConfig ttfConfig("fonts/arial.ttf", 20);
|
2014-08-19 14:21:48 +08:00
|
|
|
auto label1 = Label::createWithTTF(ttfConfig,"Hair");
|
2014-11-19 16:50:09 +08:00
|
|
|
auto item1 = MenuItemLabel::create(label1,CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_reSkin,this) );
|
2014-08-19 14:21:48 +08:00
|
|
|
auto label2 = Label::createWithTTF(ttfConfig,"Glasses");
|
2014-11-19 16:50:09 +08:00
|
|
|
auto item2 = MenuItemLabel::create(label2, CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_reSkin,this) );
|
2014-08-19 15:25:00 +08:00
|
|
|
auto label3 = Label::createWithTTF(ttfConfig,"Coat");
|
2014-11-19 16:50:09 +08:00
|
|
|
auto item3 = MenuItemLabel::create(label3,CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_reSkin,this) );
|
2014-08-19 14:21:48 +08:00
|
|
|
auto label4 = Label::createWithTTF(ttfConfig,"Pants");
|
2014-11-19 16:50:09 +08:00
|
|
|
auto item4 = MenuItemLabel::create(label4, CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_reSkin,this) );
|
2014-08-19 14:21:48 +08:00
|
|
|
auto label5 = Label::createWithTTF(ttfConfig,"Shoes");
|
2014-11-19 16:50:09 +08:00
|
|
|
auto item5 = MenuItemLabel::create(label5,CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_reSkin,this) );
|
2014-08-19 14:21:48 +08:00
|
|
|
item1->setPosition( Vec2(VisibleRect::left().x+50, VisibleRect::bottom().y+item1->getContentSize().height*4 ) );
|
2014-08-28 19:22:01 +08:00
|
|
|
item2->setPosition( Vec2(VisibleRect::left().x+50, VisibleRect::bottom().y+item1->getContentSize().height *5 ) );
|
|
|
|
item3->setPosition( Vec2(VisibleRect::left().x+50, VisibleRect::bottom().y+item1->getContentSize().height*6 ) );
|
|
|
|
item4->setPosition( Vec2(VisibleRect::left().x+50, VisibleRect::bottom().y+item1->getContentSize().height *7 ) );
|
|
|
|
item5->setPosition( Vec2(VisibleRect::left().x+50, VisibleRect::bottom().y+item1->getContentSize().height *8 ) );
|
2014-11-19 16:50:09 +08:00
|
|
|
item1->setUserData((void*)SkinType::HAIR);
|
|
|
|
item2->setUserData((void*)SkinType::GLASSES);
|
|
|
|
item3->setUserData((void*)SkinType::UPPER_BODY);
|
|
|
|
item4->setUserData((void*)SkinType::PANTS);
|
|
|
|
item5->setUserData((void*)SkinType::SHOES);
|
2014-10-09 09:55:33 +08:00
|
|
|
auto pMenu1 = CCMenu::create(item1, item2, item3, item4, item5, nullptr);
|
2014-08-19 14:03:41 +08:00
|
|
|
pMenu1->setPosition(Vec2(0,0));
|
|
|
|
this->addChild(pMenu1, 10);
|
|
|
|
|
|
|
|
}
|
2014-11-19 16:50:09 +08:00
|
|
|
void Sprite3DReskinTest::menuCallback_reSkin(Ref* sender)
|
2014-08-19 14:03:41 +08:00
|
|
|
{
|
2014-11-28 14:12:38 +08:00
|
|
|
long index = (long)(((MenuItemLabel*)sender)->getUserData());
|
2014-11-19 16:50:09 +08:00
|
|
|
if (index < (int)SkinType::MAX_TYPE)
|
|
|
|
{
|
|
|
|
_curSkin[index] = (_curSkin[index] + 1) % _skins[index].size();
|
|
|
|
applyCurSkin();
|
|
|
|
}
|
2014-08-19 14:03:41 +08:00
|
|
|
}
|
2014-10-25 14:11:32 +08:00
|
|
|
|
2014-08-19 15:25:00 +08:00
|
|
|
std::string Sprite3DReskinTest::title() const
|
2014-08-19 14:03:41 +08:00
|
|
|
{
|
2014-08-19 15:25:00 +08:00
|
|
|
return "Testing Sprite3D Reskin";
|
2014-08-19 14:03:41 +08:00
|
|
|
}
|
2014-08-19 15:25:00 +08:00
|
|
|
std::string Sprite3DReskinTest::subtitle() const
|
2014-08-19 14:03:41 +08:00
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2014-08-19 15:25:00 +08:00
|
|
|
void Sprite3DReskinTest::addNewSpriteWithCoords(Vec2 p)
|
2014-08-19 14:03:41 +08:00
|
|
|
{
|
2014-08-20 18:20:19 +08:00
|
|
|
std::string fileName = "Sprite3DTest/ReskinGirl.c3b";
|
2014-08-19 14:03:41 +08:00
|
|
|
auto sprite = Sprite3D::create(fileName);
|
|
|
|
sprite->setScale(4);
|
|
|
|
sprite->setRotation3D(Vec3(0,0,0));
|
|
|
|
addChild(sprite);
|
|
|
|
sprite->setPosition( Vec2( p.x, p.y-60) );
|
|
|
|
auto animation = Animation3D::create(fileName);
|
|
|
|
if (animation)
|
|
|
|
{
|
|
|
|
auto animate = Animate3D::create(animation);
|
|
|
|
|
|
|
|
sprite->runAction(RepeatForever::create(animate));
|
|
|
|
}
|
|
|
|
_sprite = sprite;
|
2014-10-25 14:11:32 +08:00
|
|
|
|
2014-11-19 16:50:09 +08:00
|
|
|
auto& body = _skins[(int)SkinType::UPPER_BODY];
|
|
|
|
body.push_back("Girl_UpperBody01");
|
|
|
|
body.push_back("Girl_UpperBody02");
|
|
|
|
|
|
|
|
auto& pants = _skins[(int)SkinType::PANTS];
|
|
|
|
pants.push_back("Girl_LowerBody01");
|
|
|
|
pants.push_back("Girl_LowerBody02");
|
|
|
|
|
|
|
|
auto& shoes = _skins[(int)SkinType::SHOES];
|
|
|
|
shoes.push_back("Girl_Shoes01");
|
|
|
|
shoes.push_back("Girl_Shoes02");
|
|
|
|
|
|
|
|
auto& hair = _skins[(int)SkinType::HAIR];
|
|
|
|
hair.push_back("Girl_Hair01");
|
|
|
|
hair.push_back("Girl_Hair02");
|
|
|
|
|
|
|
|
auto& face = _skins[(int)SkinType::FACE];
|
|
|
|
face.push_back("Girl_Face01");
|
|
|
|
face.push_back("Girl_Face02");
|
|
|
|
|
|
|
|
auto& hand = _skins[(int)SkinType::HAND];
|
|
|
|
hand.push_back("Girl_Hand01");
|
|
|
|
hand.push_back("Girl_Hand02");
|
|
|
|
|
|
|
|
auto& glasses = _skins[(int)SkinType::GLASSES];
|
|
|
|
glasses.push_back("");
|
|
|
|
glasses.push_back("Girl_Glasses01");
|
|
|
|
|
|
|
|
memset(_curSkin, 0, (int)SkinType::MAX_TYPE * sizeof(int));
|
|
|
|
|
2014-10-25 14:11:32 +08:00
|
|
|
applyCurSkin();
|
2014-08-19 14:03:41 +08:00
|
|
|
}
|
2014-08-08 11:16:33 +08:00
|
|
|
|
2014-08-19 15:25:00 +08:00
|
|
|
void Sprite3DReskinTest::onTouchesEnded(const std::vector<Touch*>& touches, Event* event)
|
2014-08-19 14:03:41 +08:00
|
|
|
{
|
|
|
|
}
|
2014-10-25 14:11:32 +08:00
|
|
|
|
|
|
|
void Sprite3DReskinTest::applyCurSkin()
|
|
|
|
{
|
|
|
|
for (ssize_t i = 0; i < _sprite->getMeshCount(); i++) {
|
2014-11-10 16:02:06 +08:00
|
|
|
auto mesh = _sprite->getMeshByIndex(static_cast<int>(i));
|
2014-10-25 14:11:32 +08:00
|
|
|
bool isVisible = false;
|
2014-11-19 16:50:09 +08:00
|
|
|
for (int j = 0; j < (int)SkinType::MAX_TYPE; j++) {
|
|
|
|
if (mesh->getName() == _skins[j].at(_curSkin[j]))
|
2014-10-25 14:11:32 +08:00
|
|
|
{
|
|
|
|
isVisible = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-11-10 16:02:06 +08:00
|
|
|
_sprite->getMeshByIndex(static_cast<int>(i))->setVisible(isVisible);
|
2014-10-25 14:11:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-14 09:36:22 +08:00
|
|
|
Sprite3DWithOBBPerformanceTest::Sprite3DWithOBBPerformanceTest()
|
2014-08-08 11:16:33 +08:00
|
|
|
{
|
|
|
|
auto listener = EventListenerTouchAllAtOnce::create();
|
2015-01-14 09:36:22 +08:00
|
|
|
listener->onTouchesBegan = CC_CALLBACK_2(Sprite3DWithOBBPerformanceTest::onTouchesBegan, this);
|
|
|
|
listener->onTouchesEnded = CC_CALLBACK_2(Sprite3DWithOBBPerformanceTest::onTouchesEnded, this);
|
|
|
|
listener->onTouchesMoved = CC_CALLBACK_2(Sprite3DWithOBBPerformanceTest::onTouchesMoved, this);
|
2014-08-08 11:16:33 +08:00
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
|
|
|
initDrawBox();
|
|
|
|
|
|
|
|
addNewSpriteWithCoords(Vec2(s.width/2, s.height/2));
|
|
|
|
MenuItemFont::setFontName("fonts/arial.ttf");
|
|
|
|
MenuItemFont::setFontSize(65);
|
2015-01-14 09:36:22 +08:00
|
|
|
auto decrease = MenuItemFont::create(" - ", CC_CALLBACK_1(Sprite3DWithOBBPerformanceTest::delOBBCallback, this));
|
2014-08-08 11:16:33 +08:00
|
|
|
decrease->setColor(Color3B(0,200,20));
|
2015-01-14 09:36:22 +08:00
|
|
|
auto increase = MenuItemFont::create(" + ", CC_CALLBACK_1(Sprite3DWithOBBPerformanceTest::addOBBCallback, this));
|
2014-08-08 11:16:33 +08:00
|
|
|
increase->setColor(Color3B(0,200,20));
|
|
|
|
|
|
|
|
auto menu = Menu::create(decrease, increase, nullptr);
|
|
|
|
menu->alignItemsHorizontally();
|
|
|
|
menu->setPosition(Vec2(s.width/2, s.height-65));
|
|
|
|
addChild(menu, 1);
|
|
|
|
|
|
|
|
TTFConfig ttfCount("fonts/Marker Felt.ttf", 30);
|
|
|
|
_labelCubeCount = Label::createWithTTF(ttfCount,"0 cubes");
|
|
|
|
_labelCubeCount->setColor(Color3B(0,200,20));
|
|
|
|
_labelCubeCount->setPosition(Vec2(s.width/2, s.height-90));
|
|
|
|
addChild(_labelCubeCount);
|
|
|
|
_hasCollider = false;
|
2014-09-19 11:22:37 +08:00
|
|
|
addOBBCallback(nullptr);
|
2014-08-08 11:16:33 +08:00
|
|
|
scheduleUpdate();
|
|
|
|
}
|
2015-01-14 09:36:22 +08:00
|
|
|
std::string Sprite3DWithOBBPerformanceTest::title() const
|
2014-08-08 11:16:33 +08:00
|
|
|
{
|
2015-01-14 09:36:22 +08:00
|
|
|
return "OBB Collison Performance Test";
|
2014-08-08 11:16:33 +08:00
|
|
|
}
|
2015-01-14 09:36:22 +08:00
|
|
|
std::string Sprite3DWithOBBPerformanceTest::subtitle() const
|
2014-08-08 11:16:33 +08:00
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
2015-01-14 09:36:22 +08:00
|
|
|
void Sprite3DWithOBBPerformanceTest::addNewOBBWithCoords(Vec2 p)
|
2014-08-08 11:16:33 +08:00
|
|
|
{
|
|
|
|
Vec3 extents = Vec3(10, 10, 10);
|
|
|
|
AABB aabb(-extents, extents);
|
|
|
|
auto obb = OBB(aabb);
|
|
|
|
obb._center = Vec3(p.x,p.y,0);
|
|
|
|
_obb.push_back(obb);
|
|
|
|
}
|
|
|
|
|
2015-01-14 09:36:22 +08:00
|
|
|
void Sprite3DWithOBBPerformanceTest::onTouchesBegan(const std::vector<Touch*>& touches, Event* event)
|
2014-08-08 11:16:33 +08:00
|
|
|
{
|
2014-10-24 12:03:42 +08:00
|
|
|
for (const auto& touch: touches)
|
2014-08-08 11:16:33 +08:00
|
|
|
{
|
|
|
|
auto location = touch->getLocationInView();
|
2014-10-09 17:19:43 +08:00
|
|
|
auto obbSize = _obb.size();
|
|
|
|
if(obbSize)
|
2014-08-08 11:16:33 +08:00
|
|
|
{
|
|
|
|
_intersetList.clear();
|
|
|
|
Ray ray;
|
|
|
|
calculateRayByLocationInView(&ray,location);
|
2014-10-09 17:19:43 +08:00
|
|
|
for(decltype(obbSize) i = 0; i < obbSize; i++)
|
2014-08-08 11:16:33 +08:00
|
|
|
{
|
|
|
|
if(ray.intersects(_obb[i]))
|
|
|
|
{
|
2014-10-21 11:47:28 +08:00
|
|
|
_intersetList.insert((int)i);
|
2014-08-08 11:16:33 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-14 09:36:22 +08:00
|
|
|
void Sprite3DWithOBBPerformanceTest::onTouchesEnded(const std::vector<Touch*>& touches, Event* event)
|
2014-08-08 11:16:33 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-01-14 09:36:22 +08:00
|
|
|
void Sprite3DWithOBBPerformanceTest::onTouchesMoved(const std::vector<Touch*>& touches, Event* event)
|
2014-08-08 11:16:33 +08:00
|
|
|
{
|
2014-10-24 12:03:42 +08:00
|
|
|
for (const auto& touch: touches)
|
2014-08-08 11:16:33 +08:00
|
|
|
{
|
2014-10-09 17:19:43 +08:00
|
|
|
auto location = touch->getLocation();
|
|
|
|
auto obbSize = _obb.size();
|
|
|
|
|
|
|
|
for(decltype(obbSize) i = 0; i < obbSize; i++)
|
2014-08-08 11:16:33 +08:00
|
|
|
{
|
2014-10-21 11:47:28 +08:00
|
|
|
if(_intersetList.find((int)i) != _intersetList.end())
|
2014-08-08 11:16:33 +08:00
|
|
|
_obb[i]._center = Vec3(location.x,location.y,0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-14 09:36:22 +08:00
|
|
|
void Sprite3DWithOBBPerformanceTest::update(float dt)
|
2014-08-08 11:16:33 +08:00
|
|
|
{
|
|
|
|
char szText[16];
|
|
|
|
sprintf(szText,"%lu cubes",_obb.size());
|
|
|
|
_labelCubeCount->setString(szText);
|
|
|
|
|
|
|
|
if (_drawDebug)
|
|
|
|
{
|
|
|
|
_drawDebug->clear();
|
|
|
|
|
|
|
|
Mat4 mat = _sprite->getNodeToWorldTransform();
|
|
|
|
mat.getRightVector(&_obbt._xAxis);
|
|
|
|
_obbt._xAxis.normalize();
|
|
|
|
|
|
|
|
mat.getUpVector(&_obbt._yAxis);
|
|
|
|
_obbt._yAxis.normalize();
|
|
|
|
|
|
|
|
mat.getForwardVector(&_obbt._zAxis);
|
|
|
|
_obbt._zAxis.normalize();
|
|
|
|
|
|
|
|
_obbt._center = _sprite->getPosition3D();
|
|
|
|
|
|
|
|
Vec3 corners[8] = {};
|
|
|
|
_obbt.getCorners(corners);
|
|
|
|
_drawDebug->drawCube(corners, Color4F(0,0,1,1));
|
|
|
|
}
|
|
|
|
if(_obb.size() > 0)
|
|
|
|
{
|
|
|
|
_drawOBB->clear();
|
2014-10-09 17:19:43 +08:00
|
|
|
auto obbSize = _obb.size();
|
|
|
|
for(decltype(obbSize) i =0; i < obbSize; i++)
|
2014-08-08 11:16:33 +08:00
|
|
|
{
|
|
|
|
Vec3 corners[8] = {};
|
|
|
|
_obb[i].getCorners(corners);
|
|
|
|
_drawOBB->drawCube(corners, _obbt.intersects(_obb[i])?Color4F(1,0,0,1):Color4F(0,1,0,1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-14 09:36:22 +08:00
|
|
|
void Sprite3DWithOBBPerformanceTest::initDrawBox()
|
2014-08-08 11:16:33 +08:00
|
|
|
{
|
|
|
|
_drawOBB = DrawNode3D::create();
|
|
|
|
addChild(_drawOBB);
|
|
|
|
}
|
|
|
|
|
2015-01-14 09:36:22 +08:00
|
|
|
void Sprite3DWithOBBPerformanceTest::addNewSpriteWithCoords(Vec2 p)
|
2014-08-08 11:16:33 +08:00
|
|
|
{
|
|
|
|
std::string fileName = "Sprite3DTest/tortoise.c3b";
|
|
|
|
auto sprite = Sprite3D::create(fileName);
|
|
|
|
sprite->setScale(0.1f);
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
|
|
|
sprite->setPosition(Vec2(s.width * 4.f / 5.f, s.height / 2.f));
|
|
|
|
addChild(sprite);
|
|
|
|
_sprite = sprite;
|
|
|
|
auto animation = Animation3D::create(fileName);
|
|
|
|
if (animation)
|
|
|
|
{
|
|
|
|
auto animate = Animate3D::create(animation, 0.f, 1.933f);
|
|
|
|
sprite->runAction(RepeatForever::create(animate));
|
|
|
|
}
|
|
|
|
|
|
|
|
_moveAction = MoveTo::create(4.f, Vec2(s.width / 5.f, s.height / 2.f));
|
|
|
|
_moveAction->retain();
|
2015-01-14 09:36:22 +08:00
|
|
|
auto seq = Sequence::create(_moveAction, CallFunc::create(CC_CALLBACK_0(Sprite3DWithOBBPerformanceTest::reachEndCallBack, this)), nullptr);
|
2014-08-08 11:16:33 +08:00
|
|
|
seq->setTag(100);
|
|
|
|
sprite->runAction(seq);
|
|
|
|
|
2014-08-08 17:54:39 +08:00
|
|
|
AABB aabb = _sprite->getAABB();
|
2014-08-08 11:16:33 +08:00
|
|
|
_obbt = OBB(aabb);
|
|
|
|
|
|
|
|
_drawDebug = DrawNode3D::create();
|
|
|
|
addChild(_drawDebug);
|
|
|
|
}
|
|
|
|
|
2015-01-14 09:36:22 +08:00
|
|
|
void Sprite3DWithOBBPerformanceTest::reachEndCallBack()
|
2014-08-08 11:16:33 +08:00
|
|
|
{
|
|
|
|
_sprite->stopActionByTag(100);
|
|
|
|
auto inverse = (MoveTo*)_moveAction->reverse();
|
|
|
|
inverse->retain();
|
|
|
|
_moveAction->release();
|
|
|
|
_moveAction = inverse;
|
|
|
|
auto rot = RotateBy::create(1.0f, Vec3(0.f, 180.f, 0.f));
|
2015-01-14 09:36:22 +08:00
|
|
|
auto seq = Sequence::create(rot, _moveAction, CallFunc::create(CC_CALLBACK_0(Sprite3DWithOBBPerformanceTest::reachEndCallBack, this)), nullptr);
|
2014-08-08 11:16:33 +08:00
|
|
|
seq->setTag(100);
|
|
|
|
_sprite->runAction(seq);
|
|
|
|
}
|
|
|
|
|
2015-01-14 09:36:22 +08:00
|
|
|
void Sprite3DWithOBBPerformanceTest::addOBBCallback(Ref* sender)
|
2014-08-08 11:16:33 +08:00
|
|
|
{
|
|
|
|
addOBBWithCount(10);
|
|
|
|
}
|
|
|
|
|
2015-01-14 09:36:22 +08:00
|
|
|
void Sprite3DWithOBBPerformanceTest::addOBBWithCount(float value)
|
2014-08-08 11:16:33 +08:00
|
|
|
{
|
|
|
|
for(int i = 0; i < value; i++)
|
|
|
|
{
|
|
|
|
Vec2 randompos = Vec2(CCRANDOM_0_1() * Director::getInstance()->getWinSize().width,CCRANDOM_0_1() * Director::getInstance()->getWinSize().height);
|
|
|
|
Vec3 extents = Vec3(10, 10, 10);
|
|
|
|
AABB aabb(-extents, extents);
|
|
|
|
auto obb = OBB(aabb);
|
|
|
|
obb._center = Vec3(randompos.x,randompos.y,0);
|
|
|
|
_obb.push_back(obb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-14 09:36:22 +08:00
|
|
|
void Sprite3DWithOBBPerformanceTest::delOBBCallback(Ref* sender)
|
2014-08-08 11:16:33 +08:00
|
|
|
{
|
|
|
|
delOBBWithCount(10);
|
|
|
|
}
|
|
|
|
|
2015-01-14 09:36:22 +08:00
|
|
|
void Sprite3DWithOBBPerformanceTest::delOBBWithCount(float value)
|
2014-08-08 11:16:33 +08:00
|
|
|
{
|
|
|
|
if(_obb.size() >= 10)
|
|
|
|
{
|
|
|
|
_obb.erase(_obb.begin(),_obb.begin() + value);
|
|
|
|
_drawOBB->clear();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return;
|
|
|
|
}
|
2015-01-14 09:36:22 +08:00
|
|
|
void Sprite3DWithOBBPerformanceTest::unproject(const Mat4& viewProjection, const Size* viewport, Vec3* src, Vec3* dst)
|
2014-08-08 11:16:33 +08:00
|
|
|
{
|
|
|
|
assert(dst);
|
|
|
|
|
|
|
|
assert(viewport->width != 0.0f && viewport->height != 0.0f);
|
|
|
|
Vec4 screen(src->x / viewport->width, ((viewport->height - src->y)) / viewport->height, src->z, 1.0f);
|
|
|
|
|
|
|
|
screen.x = screen.x * 2.0f - 1.0f;
|
|
|
|
screen.y = screen.y * 2.0f - 1.0f;
|
|
|
|
screen.z = screen.z * 2.0f - 1.0f;
|
|
|
|
|
|
|
|
viewProjection.getInversed().transformVector(screen, &screen);
|
|
|
|
|
|
|
|
if (screen.w != 0.0f)
|
|
|
|
{
|
|
|
|
screen.x /= screen.w;
|
|
|
|
screen.y /= screen.w;
|
|
|
|
screen.z /= screen.w;
|
|
|
|
}
|
|
|
|
|
|
|
|
dst->set(screen.x, screen.y, screen.z);
|
|
|
|
}
|
|
|
|
|
2015-01-14 09:36:22 +08:00
|
|
|
void Sprite3DWithOBBPerformanceTest::calculateRayByLocationInView(Ray* ray, const Vec2& location)
|
2014-08-08 11:16:33 +08:00
|
|
|
{
|
|
|
|
auto dir = Director::getInstance();
|
|
|
|
auto view = dir->getWinSize();
|
2014-11-05 08:46:14 +08:00
|
|
|
auto mat = dir->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
|
2014-08-08 11:16:33 +08:00
|
|
|
|
|
|
|
Vec3 src = Vec3(location.x, location.y, -1);
|
|
|
|
Vec3 nearPoint;
|
|
|
|
unproject(mat, &view, &src, &nearPoint);
|
|
|
|
|
|
|
|
src = Vec3(location.x, location.y, 1);
|
|
|
|
Vec3 farPoint;
|
|
|
|
unproject(mat, &view, &src, &farPoint);
|
|
|
|
|
|
|
|
Vec3 direction;
|
|
|
|
Vec3::subtract(farPoint, nearPoint, &direction);
|
|
|
|
direction.normalize();
|
|
|
|
|
|
|
|
ray->_origin = nearPoint;
|
|
|
|
ray->_direction = direction;
|
|
|
|
}
|
2014-08-14 17:12:39 +08:00
|
|
|
|
2014-08-13 17:38:16 +08:00
|
|
|
Sprite3DMirrorTest::Sprite3DMirrorTest()
|
|
|
|
: _sprite(nullptr)
|
|
|
|
, _mirrorSprite(nullptr)
|
|
|
|
{
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
|
|
|
addNewSpriteWithCoords( Vec2(s.width/2, s.height/2) );
|
|
|
|
}
|
|
|
|
std::string Sprite3DMirrorTest::title() const
|
|
|
|
{
|
|
|
|
return "Sprite3D Mirror Test";
|
|
|
|
}
|
|
|
|
std::string Sprite3DMirrorTest::subtitle() const
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sprite3DMirrorTest::addNewSpriteWithCoords(Vec2 p)
|
|
|
|
{
|
|
|
|
std::string fileName = "Sprite3DTest/orc.c3b";
|
|
|
|
auto sprite = Sprite3D::create(fileName);
|
|
|
|
sprite->setScale(5);
|
|
|
|
sprite->setRotation3D(Vec3(0,180,0));
|
|
|
|
addChild(sprite);
|
|
|
|
sprite->setPosition( Vec2( p.x - 80, p.y) );
|
|
|
|
|
|
|
|
//test attach
|
|
|
|
auto sp = Sprite3D::create("Sprite3DTest/axe.c3b");
|
|
|
|
sprite->getAttachNode("Bip001 R Hand")->addChild(sp);
|
|
|
|
|
|
|
|
auto animation = Animation3D::create(fileName);
|
|
|
|
if (animation)
|
|
|
|
{
|
|
|
|
auto animate = Animate3D::create(animation);
|
|
|
|
|
|
|
|
sprite->runAction(RepeatForever::create(animate));
|
|
|
|
}
|
|
|
|
_sprite = sprite;
|
|
|
|
_hasWeapon = true;
|
|
|
|
|
|
|
|
//create mirror Sprite3D
|
|
|
|
sprite = Sprite3D::create(fileName);
|
|
|
|
sprite->setScale(5);
|
|
|
|
sprite->setScaleX(-5);
|
|
|
|
sprite->setCullFace(GL_FRONT);
|
|
|
|
sprite->setRotation3D(Vec3(0,180,0));
|
|
|
|
addChild(sprite);
|
|
|
|
sprite->setPosition( Vec2( p.x + 80, p.y) );
|
|
|
|
|
|
|
|
//test attach
|
|
|
|
sp = Sprite3D::create("Sprite3DTest/axe.c3b");
|
|
|
|
sprite->getAttachNode("Bip001 R Hand")->addChild(sp);
|
|
|
|
|
|
|
|
animation = Animation3D::create(fileName);
|
|
|
|
if (animation)
|
|
|
|
{
|
|
|
|
auto animate = Animate3D::create(animation);
|
|
|
|
|
|
|
|
sprite->runAction(RepeatForever::create(animate));
|
|
|
|
}
|
|
|
|
_mirrorSprite = sprite;
|
|
|
|
}
|
2014-12-29 14:36:25 +08:00
|
|
|
|
|
|
|
QuaternionTest::QuaternionTest()
|
|
|
|
: _arcSpeed(CC_DEGREES_TO_RADIANS(90))
|
|
|
|
, _radius(100.f)
|
|
|
|
, _accAngle(0.f)
|
|
|
|
{
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
|
|
|
addNewSpriteWithCoords(Vec2(s.width / 2.f, s.height / 2.f));
|
|
|
|
scheduleUpdate();
|
|
|
|
}
|
|
|
|
std::string QuaternionTest::title() const
|
|
|
|
{
|
|
|
|
return "Test Rotation With Quaternion";
|
|
|
|
}
|
|
|
|
std::string QuaternionTest::subtitle() const
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
void QuaternionTest::addNewSpriteWithCoords(Vec2 p)
|
|
|
|
{
|
|
|
|
std::string fileName = "Sprite3DTest/tortoise.c3b";
|
|
|
|
auto sprite = Sprite3D::create(fileName);
|
|
|
|
sprite->setScale(0.1f);
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
|
|
|
sprite->setPosition(Vec2(s.width / 2.f + _radius * cosf(_accAngle), s.height / 2.f + _radius * sinf(_accAngle)));
|
|
|
|
addChild(sprite);
|
|
|
|
_sprite = sprite;
|
|
|
|
auto animation = Animation3D::create(fileName);
|
|
|
|
if (animation)
|
|
|
|
{
|
|
|
|
auto animate = Animate3D::create(animation, 0.f, 1.933f);
|
|
|
|
sprite->runAction(RepeatForever::create(animate));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void QuaternionTest::update(float delta)
|
|
|
|
{
|
|
|
|
_accAngle += delta * _arcSpeed;
|
2014-12-29 16:24:31 +08:00
|
|
|
const float pi = M_PI;
|
2014-12-29 14:36:25 +08:00
|
|
|
if (_accAngle >= 2 * pi)
|
|
|
|
_accAngle -= 2 * pi;
|
|
|
|
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
|
|
|
_sprite->setPosition(Vec2(s.width / 2.f + _radius * cosf(_accAngle), s.height / 2.f + _radius * sinf(_accAngle)));
|
|
|
|
|
|
|
|
Quaternion quat;
|
|
|
|
Quaternion::createFromAxisAngle(Vec3(0.f, 0.f, 1.f), _accAngle - pi * 0.5f, &quat);
|
|
|
|
_sprite->setRotationQuat(quat);
|
|
|
|
}
|
2015-01-23 18:24:15 +08:00
|
|
|
|
|
|
|
UseCaseSprite3D::UseCaseSprite3D()
|
|
|
|
: _caseIdx(0)
|
|
|
|
{
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
|
|
|
|
2015-01-26 14:58:16 +08:00
|
|
|
_useCaseTitles[0] = "transparent 3d sprite and 2d sprite";
|
2015-01-26 18:44:30 +08:00
|
|
|
_useCaseTitles[1] = "ui - 3d - ui";
|
2015-01-23 18:24:15 +08:00
|
|
|
|
|
|
|
auto itemPrev = MenuItemImage::create("Images/b1.png", "Images/b2.png",
|
|
|
|
[&](Ref *sender) {
|
|
|
|
_caseIdx--;
|
|
|
|
if (_caseIdx < 0)
|
2015-01-26 18:44:30 +08:00
|
|
|
_caseIdx = (int)USECASE::MAX_CASE_NUM - 1;
|
2015-01-23 18:24:15 +08:00
|
|
|
this->switchCase();
|
|
|
|
});
|
|
|
|
|
|
|
|
auto itemNext = MenuItemImage::create("Images/f1.png", "Images/f2.png",
|
|
|
|
[&](Ref *sender) {
|
|
|
|
_caseIdx++;
|
|
|
|
if (_caseIdx >= (int)USECASE::MAX_CASE_NUM)
|
2015-01-26 18:44:30 +08:00
|
|
|
_caseIdx = 0;
|
2015-01-23 18:24:15 +08:00
|
|
|
this->switchCase();
|
|
|
|
});
|
|
|
|
|
|
|
|
auto menu = Menu::create(itemPrev, itemNext, nullptr);
|
|
|
|
menu->alignItemsHorizontally();
|
|
|
|
menu->setScale(0.5);
|
|
|
|
menu->setAnchorPoint(Vec2(0,0));
|
|
|
|
menu->setPosition(Vec2(s.width/2,70));
|
|
|
|
|
|
|
|
_label = Label::create();
|
|
|
|
_label->setPosition(s.width * 0.5f, s.height * 0.8f);
|
|
|
|
addChild(_label);
|
|
|
|
|
|
|
|
addChild(menu);
|
|
|
|
|
2015-01-26 14:58:16 +08:00
|
|
|
//setup camera
|
|
|
|
auto camera = Camera::createPerspective(40, s.width / s.height, 0.01f, 1000.f);
|
|
|
|
camera->setCameraFlag(CameraFlag::USER1);
|
|
|
|
camera->setPosition3D(Vec3(0.f, 30.f, 100.f));
|
|
|
|
camera->lookAt(Vec3(0.f, 0.f, 0.f));
|
|
|
|
addChild(camera);
|
|
|
|
|
2015-01-23 18:24:15 +08:00
|
|
|
switchCase();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string UseCaseSprite3D::title() const
|
|
|
|
{
|
|
|
|
return "Use Case For 2D + 3D";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string UseCaseSprite3D::subtitle() const
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
void UseCaseSprite3D::switchCase()
|
|
|
|
{
|
2015-01-27 15:24:15 +08:00
|
|
|
removeChildByTag(101);
|
2015-01-26 14:58:16 +08:00
|
|
|
|
2015-01-23 18:24:15 +08:00
|
|
|
auto s = Director::getInstance()->getWinSize();
|
|
|
|
_label->setString(_useCaseTitles[_caseIdx]);
|
2015-01-27 15:24:15 +08:00
|
|
|
if (_caseIdx == 0) // use case 1, 3d transparent sprite + 2d sprite
|
2015-01-23 18:24:15 +08:00
|
|
|
{
|
2015-01-26 14:58:16 +08:00
|
|
|
std::string filename = "Sprite3DTest/girl.c3b";
|
2015-01-23 18:24:15 +08:00
|
|
|
auto sprite = Sprite3D::create(filename);
|
2015-01-26 14:58:16 +08:00
|
|
|
sprite->setScale(0.15f);
|
2015-01-23 18:24:15 +08:00
|
|
|
auto animation = Animation3D::create(filename);
|
|
|
|
if (animation)
|
|
|
|
{
|
|
|
|
auto animate = Animate3D::create(animation);
|
|
|
|
|
|
|
|
sprite->runAction(RepeatForever::create(animate));
|
|
|
|
}
|
|
|
|
|
2015-01-26 14:58:16 +08:00
|
|
|
auto circleBack = Sprite3D::create();
|
|
|
|
auto circle = Sprite::create("Sprite3DTest/circle.png");
|
|
|
|
circleBack->setScale(0.5f);
|
|
|
|
circleBack->addChild(circle);
|
|
|
|
circle->runAction(RepeatForever::create(RotateBy::create(3, Vec3(0.f, 0.f, 360.f))));
|
|
|
|
|
|
|
|
circleBack->setRotation3D(Vec3(90, 0, 0));
|
|
|
|
|
|
|
|
auto pos = sprite->getPosition3D();
|
|
|
|
circleBack->setPosition3D(Vec3(pos.x, pos.y, pos.z - 1));
|
|
|
|
|
2015-01-27 15:24:15 +08:00
|
|
|
sprite->setOpacity(250);
|
|
|
|
sprite->setCameraMask(2);
|
|
|
|
circleBack->setCameraMask(2);
|
|
|
|
sprite->setTag(3);
|
|
|
|
circleBack->setTag(2);
|
|
|
|
|
|
|
|
auto node = Node::create();
|
|
|
|
node->addChild(sprite);
|
|
|
|
node->addChild(circleBack);
|
|
|
|
node->setTag(101);
|
|
|
|
addChild(node);
|
|
|
|
|
|
|
|
scheduleUpdate();
|
|
|
|
update(0.f);
|
2015-01-23 18:24:15 +08:00
|
|
|
}
|
2015-01-27 15:24:15 +08:00
|
|
|
else if (_caseIdx == 1) // use case 2, ui - 3d - ui, last ui should on the top
|
2015-01-26 18:44:30 +08:00
|
|
|
{
|
2015-01-27 15:24:15 +08:00
|
|
|
auto layer = LayerColor::create(Color4B(0, 0, 100, 255), s.width / 2.f, s.height / 2.f);
|
|
|
|
layer->setPosition(s.width * 0.25f, s.height * 0.25f);
|
2015-01-28 09:24:01 +08:00
|
|
|
layer->setGlobalZOrder(-1);
|
2015-01-27 15:24:15 +08:00
|
|
|
addChild(layer);
|
2015-01-26 18:44:30 +08:00
|
|
|
|
2015-01-27 15:24:15 +08:00
|
|
|
std::string filename = "Sprite3DTest/girl.c3b";
|
|
|
|
auto sprite = Sprite3D::create(filename);
|
|
|
|
sprite->setScale(0.5f);
|
|
|
|
auto animation = Animation3D::create(filename);
|
|
|
|
if (animation)
|
|
|
|
{
|
|
|
|
auto animate = Animate3D::create(animation);
|
|
|
|
sprite->runAction(RepeatForever::create(animate));
|
|
|
|
}
|
|
|
|
sprite->setPosition(s.width * 0.25f, s.height * 0.125f);
|
|
|
|
layer->addChild(sprite);
|
|
|
|
|
|
|
|
TTFConfig ttfConfig("fonts/arial.ttf", 15);
|
|
|
|
auto label1 = Label::createWithTTF(ttfConfig,"Message");
|
|
|
|
auto item1 = MenuItemLabel::create(label1,CC_CALLBACK_1(UseCaseSprite3D::menuCallback_Message,this) );
|
|
|
|
auto label2 = Label::createWithTTF(ttfConfig,"Message");
|
|
|
|
auto item2 = MenuItemLabel::create(label2, CC_CALLBACK_1(UseCaseSprite3D::menuCallback_Message,this) );
|
|
|
|
|
|
|
|
item1->setPosition( Vec2(s.width * 0.5f - item1->getContentSize().width * 0.5f, s.height * 0.5f - item1->getContentSize().height ) );
|
|
|
|
item2->setPosition( Vec2(s.width * 0.5f - item1->getContentSize().width * 0.5f, s.height * 0.5f - item1->getContentSize().height * 2.f ) );
|
|
|
|
|
|
|
|
auto pMenu1 = CCMenu::create(item1, item2, nullptr);
|
|
|
|
pMenu1->setPosition(Vec2(0,0));
|
|
|
|
layer->addChild(pMenu1);
|
|
|
|
|
|
|
|
layer->setTag(101);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void UseCaseSprite3D::menuCallback_Message(Ref* sender)
|
|
|
|
{
|
|
|
|
auto layer = getChildByTag(101);
|
|
|
|
auto message = layer->getChildByTag(102); // message layer
|
|
|
|
if (message)
|
|
|
|
layer->removeChild(message);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// create a new message layer on the top
|
|
|
|
auto s = layer->getContentSize();
|
|
|
|
auto messagelayer = LayerColor::create(Color4B(100, 100, 0, 255));
|
|
|
|
messagelayer->setContentSize(Size(s.width * 0.5f, s.height * 0.5f));
|
|
|
|
messagelayer->setPosition(Vec2(s.width * 0.25f, s.height * 0.25f));
|
|
|
|
auto label = Label::create();
|
|
|
|
label->setString("This Message Layer \n Should Be On Top");
|
|
|
|
label->setPosition(Vec2(s.width * 0.25f, s.height * 0.25f));
|
|
|
|
messagelayer->addChild(label);
|
|
|
|
messagelayer->setTag(102);
|
|
|
|
layer->addChild(messagelayer);
|
2015-01-26 18:44:30 +08:00
|
|
|
}
|
2015-01-26 14:58:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void UseCaseSprite3D::update(float delta)
|
|
|
|
{
|
2015-01-26 18:44:30 +08:00
|
|
|
if (_caseIdx == 0)
|
|
|
|
{
|
|
|
|
static float accAngle = 0.f;
|
|
|
|
accAngle += delta * CC_DEGREES_TO_RADIANS(60);
|
|
|
|
|
|
|
|
float radius = 30.f;
|
|
|
|
float x = cosf(accAngle) * radius, z = sinf(accAngle) * radius;
|
|
|
|
|
2015-01-27 15:24:15 +08:00
|
|
|
auto node = getChildByTag(101);
|
|
|
|
auto sprite3d = node->getChildByTag(3);
|
|
|
|
auto circle = node->getChildByTag(2);
|
|
|
|
|
|
|
|
sprite3d->setPositionX(x);
|
|
|
|
sprite3d->setPositionZ(z);
|
|
|
|
circle->setPositionX(x);
|
|
|
|
circle->setPositionZ(z);
|
2015-01-26 18:44:30 +08:00
|
|
|
}
|
2015-01-23 18:24:15 +08:00
|
|
|
}
|