This commit is contained in:
tangziwen 2014-12-17 13:31:35 +08:00
parent 32395f8e90
commit 73014e8be3
2 changed files with 350 additions and 226 deletions

View File

@ -1,27 +1,27 @@
/****************************************************************************
Copyright (c) 2012 cocos2d-x.org
Copyright (c) 2013-2014 Chukong Technologies Inc.
Copyright (c) 2012 cocos2d-x.org
Copyright (c) 2013-2014 Chukong Technologies Inc.
http://www.cocos2d-x.org
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:
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 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.
****************************************************************************/
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"
#include "3d/CCAnimation3D.h"
@ -53,6 +53,8 @@ static std::function<Layer*()> createFunctions[] =
// 3DEffect use custom shader which is not supported on WP8/WinRT yet.
CL(Sprite3DEffectTest),
CL(Sprite3DUVAnimationTest),
CL(Sprite3DFakeShadowTest),
CL(Sprite3DBasicToonShaderTest),
#endif
CL(Sprite3DWithSkinTest),
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WP8) && (CC_TARGET_PLATFORM != CC_PLATFORM_WINRT)
@ -100,7 +102,7 @@ static Layer* restartSpriteTestAction()
//------------------------------------------------------------------
Sprite3DTestDemo::Sprite3DTestDemo(void)
: BaseTest()
: BaseTest()
{
}
@ -171,8 +173,8 @@ void Sprite3DBasicTest::addNewSpriteWithCoords(Vec2 p)
//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 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");
@ -270,8 +272,8 @@ Sprite3DUVAnimationTest::Sprite3DUVAnimationTest()
//create the second texture for cylinder
auto shining_texture = Director::getInstance()->getTextureCache()->addImage("Sprite3DTest/caustics.png");
Texture2D::TexParams tRepeatParams;//set texture parameters
tRepeatParams.magFilter = GL_NEAREST;
tRepeatParams.minFilter = GL_NEAREST;
tRepeatParams.magFilter = GL_LINEAR;
tRepeatParams.minFilter = GL_LINEAR;
tRepeatParams.wrapS = GL_REPEAT;
tRepeatParams.wrapT = GL_REPEAT;
shining_texture->setTexParameters(tRepeatParams);
@ -445,7 +447,7 @@ EffectSprite3D* EffectSprite3D::create(const std::string &path)
}
EffectSprite3D::EffectSprite3D()
: _defaultEffect(nullptr)
: _defaultEffect(nullptr)
{
}
@ -532,11 +534,11 @@ bool Effect3DOutline::init()
}
Effect3DOutline::Effect3DOutline()
: _outlineWidth(1.0f)
, _outlineColor(1, 1, 1)
, _sprite(nullptr)
: _outlineWidth(1.0f)
, _outlineColor(1, 1, 1)
, _sprite(nullptr)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
_backToForegroundListener = EventListenerCustom::create(EVENT_RENDERER_RECREATED,
[this](EventCustom*)
{
@ -553,7 +555,7 @@ Effect3DOutline::Effect3DOutline()
Effect3DOutline::~Effect3DOutline()
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
Director::getInstance()->getEventDispatcher()->removeEventListener(_backToForegroundListener);
#endif
}
@ -613,7 +615,6 @@ void Effect3DOutline::setTarget(EffectSprite3D *sprite)
(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));
@ -673,7 +674,6 @@ void EffectSprite3D::draw(cocos2d::Renderer *renderer, const cocos2d::Mat4 &tran
CustomCommand &cc = std::get<2>(effect);
cc.func = CC_CALLBACK_0(Effect3D::draw,std::get<1>(effect),transform);
renderer->addCommand(&cc);
}
if(!_defaultEffect)
@ -900,11 +900,11 @@ void Sprite3DWithSkinOutlineTest::onTouchesEnded(const std::vector<Touch*>& touc
}
Animate3DTest::Animate3DTest()
: _hurt(nullptr)
, _swim(nullptr)
, _sprite(nullptr)
, _moveAction(nullptr)
, _elapseTransTime(0.f)
: _hurt(nullptr)
, _swim(nullptr)
, _sprite(nullptr)
, _moveAction(nullptr)
, _elapseTransTime(0.f)
{
addSprite3D();
@ -1035,8 +1035,8 @@ void Animate3DTest::onTouchesEnded(const std::vector<Touch*>& touches, Event* ev
}
AttachmentTest::AttachmentTest()
: _hasWeapon(false)
, _sprite(nullptr)
: _hasWeapon(false)
, _sprite(nullptr)
{
auto s = Director::getInstance()->getWinSize();
addNewSpriteWithCoords( Vec2(s.width/2, s.height/2) );
@ -1092,7 +1092,7 @@ void AttachmentTest::onTouchesEnded(const std::vector<Touch*>& touches, Event* e
_hasWeapon = !_hasWeapon;
}
Sprite3DReskinTest::Sprite3DReskinTest()
: _sprite(nullptr)
: _sprite(nullptr)
{
auto s = Director::getInstance()->getWinSize();
addNewSpriteWithCoords( Vec2(s.width/2, s.height/2) );
@ -1102,38 +1102,69 @@ Sprite3DReskinTest::Sprite3DReskinTest()
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
TTFConfig ttfConfig("fonts/arial.ttf", 20);
auto label1 = Label::createWithTTF(ttfConfig,"Hair");
auto item1 = MenuItemLabel::create(label1,CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_reSkin,this) );
auto item1 = MenuItemLabel::create(label1,CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_switchHair,this) );
auto label2 = Label::createWithTTF(ttfConfig,"Glasses");
auto item2 = MenuItemLabel::create(label2, CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_reSkin,this) );
auto item2 = MenuItemLabel::create(label2, CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_switchGlasses,this) );
auto label3 = Label::createWithTTF(ttfConfig,"Coat");
auto item3 = MenuItemLabel::create(label3,CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_reSkin,this) );
auto item3 = MenuItemLabel::create(label3,CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_switchCoat,this) );
auto label4 = Label::createWithTTF(ttfConfig,"Pants");
auto item4 = MenuItemLabel::create(label4, CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_reSkin,this) );
auto item4 = MenuItemLabel::create(label4, CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_switchPants,this) );
auto label5 = Label::createWithTTF(ttfConfig,"Shoes");
auto item5 = MenuItemLabel::create(label5,CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_reSkin,this) );
auto item5 = MenuItemLabel::create(label5,CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_switchShoes,this) );
item1->setPosition( Vec2(VisibleRect::left().x+50, VisibleRect::bottom().y+item1->getContentSize().height*4 ) );
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 ) );
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);
auto pMenu1 = CCMenu::create(item1, item2, item3, item4, item5, nullptr);
pMenu1->setPosition(Vec2(0,0));
this->addChild(pMenu1, 10);
}
void Sprite3DReskinTest::menuCallback_reSkin(Ref* sender)
void Sprite3DReskinTest::menuCallback_switchHair(Ref* sender)
{
long index = (long)(((MenuItemLabel*)sender)->getUserData());
if (index < (int)SkinType::MAX_TYPE)
{
_curSkin[index] = (_curSkin[index] + 1) % _skins[index].size();
std::string str = _curSkin[SkinType::HAIR];
if (str == "Girl_Hair01")
_curSkin[SkinType::HAIR] = "Girl_Hair02";
else
_curSkin[SkinType::HAIR] = "Girl_Hair01";
applyCurSkin();
}
void Sprite3DReskinTest::menuCallback_switchGlasses(Ref* sender)
{
std::string str = _curSkin[SkinType::GLASSES];
if (str == "Girl_Glasses01")
_curSkin[SkinType::GLASSES] = "";
else
_curSkin[SkinType::GLASSES] = "Girl_Glasses01";
applyCurSkin();
}
void Sprite3DReskinTest::menuCallback_switchCoat(Ref* sender)
{
std::string str = _curSkin[SkinType::UPPER_BODY];
if (str == "Girl_UpperBody01")
_curSkin[SkinType::UPPER_BODY] = "Girl_UpperBody02";
else
_curSkin[SkinType::UPPER_BODY] = "Girl_UpperBody01";
applyCurSkin();
}
void Sprite3DReskinTest::menuCallback_switchPants(Ref* sender)
{
std::string str = _curSkin[SkinType::PANTS];
if (str == "Girl_LowerBody01")
_curSkin[SkinType::PANTS] = "Girl_LowerBody02";
else
_curSkin[SkinType::PANTS] = "Girl_LowerBody01";
applyCurSkin();
}
void Sprite3DReskinTest::menuCallback_switchShoes(Ref* sender)
{
std::string strShoes = _curSkin[SkinType::SHOES];
if (strShoes == "Girl_Shoes01")
_curSkin[SkinType::SHOES] = "Girl_Shoes02";
else
_curSkin[SkinType::SHOES] = "Girl_Shoes01";
applyCurSkin();
}
}
std::string Sprite3DReskinTest::title() const
@ -1162,36 +1193,13 @@ void Sprite3DReskinTest::addNewSpriteWithCoords(Vec2 p)
}
_sprite = sprite;
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));
_curSkin[SkinType::UPPER_BODY] = "Girl_UpperBody01";
_curSkin[SkinType::PANTS] = "Girl_LowerBody01";
_curSkin[SkinType::SHOES] = "Girl_Shoes01";
_curSkin[SkinType::HAIR] = "Girl_Hair01";
_curSkin[SkinType::FACE] = "Girl_Face01";
_curSkin[SkinType::HAND] = "Girl_Hand01";
_curSkin[SkinType::GLASSES] = "";
applyCurSkin();
}
@ -1204,8 +1212,8 @@ void Sprite3DReskinTest::applyCurSkin()
for (ssize_t i = 0; i < _sprite->getMeshCount(); i++) {
auto mesh = _sprite->getMeshByIndex(static_cast<int>(i));
bool isVisible = false;
for (int j = 0; j < (int)SkinType::MAX_TYPE; j++) {
if (mesh->getName() == _skins[j].at(_curSkin[j]))
for (auto& it : _curSkin) {
if (mesh->getName() == it.second)
{
isVisible = true;
break;
@ -1473,8 +1481,8 @@ void Sprite3DWithOBBPerfromanceTest::calculateRayByLocationInView(Ray* ray, cons
}
Sprite3DMirrorTest::Sprite3DMirrorTest()
: _sprite(nullptr)
, _mirrorSprite(nullptr)
: _sprite(nullptr)
, _mirrorSprite(nullptr)
{
auto s = Director::getInstance()->getWinSize();
addNewSpriteWithCoords( Vec2(s.width/2, s.height/2) );
@ -1533,3 +1541,101 @@ void Sprite3DMirrorTest::addNewSpriteWithCoords(Vec2 p)
}
_mirrorSprite = sprite;
}
Sprite3DFakeShadowTest::Sprite3DFakeShadowTest()
{
Size visibleSize = Director::getInstance()->getVisibleSize();
auto _camera = Camera::createPerspective(60,visibleSize.width/visibleSize.height,0.1,200);
_camera->setCameraFlag(CameraFlag::USER1);
_camera->setPositionY(3);
auto plane = Sprite3D::create("Sprite3DTest/plane.c3t");
plane->setRotation3D(Vec3(90,0,0));
auto shader =GLProgram::createWithFilenames("Sprite3DTest/simple_shadow.vert","Sprite3DTest/simple_shadow.frag");
auto state = GLProgramState::create(shader);
plane->setGLProgramState(state);
//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);
state->setVertexAttribPointer(s_attributeNames[meshattribute.vertexAttrib],
meshattribute.size,
meshattribute.type,
GL_FALSE,
plane->getMesh()->getVertexSizeInBytes(),
(GLvoid*)offset);
offset += meshattribute.attribSizeBytes;
}
state->setUniformMat4("u_model_matrix",plane->getNodeToWorldTransform());
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);
this->addChild(plane);
auto orc = Sprite3D::create("Sprite3DTest/orc.c3b");
orc->setScale(0.2);
orc->setPosition3D(Vec3(0,0,-10));
plane->getGLProgramState()->setUniformVec3("u_target_pos",orc->getPosition3D());
this->addChild(orc);
this->addChild(_camera);
this->setCameraMask(2);
}
std::string Sprite3DFakeShadowTest::title() const
{
return "fake shadow effect";
}
std::string Sprite3DFakeShadowTest::subtitle() const
{
return "fake shadow effect";
}
//the basic toon shader test
Sprite3DBasicToonShaderTest::Sprite3DBasicToonShaderTest()
{
Size visibleSize = Director::getInstance()->getVisibleSize();
auto _camera = Camera::createPerspective(60,visibleSize.width/visibleSize.height,0.1,200);
_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");
auto state = GLProgramState::create(shader);
teapot->setGLProgramState(state);
teapot->setPosition3D(Vec3(0,-5,-20));
teapot->setRotation3D(Vec3(-90,180,0));
auto rotate_action = RotateBy::create(3,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);
state->setVertexAttribPointer(s_attributeNames[meshattribute.vertexAttrib],
meshattribute.size,
meshattribute.type,
GL_FALSE,
teapot->getMesh()->getVertexSizeInBytes(),
(GLvoid*)offset);
offset += meshattribute.attribSizeBytes;
}
addChild(teapot);
addChild(_camera);
setCameraMask(2);
}
std::string Sprite3DBasicToonShaderTest::title() const
{
return "basic toon shader test";
}
std::string Sprite3DBasicToonShaderTest::subtitle() const
{
return " ";
}

View File

@ -81,6 +81,24 @@ protected:
float _shining_duraion;
GLProgramState * _state;
};
class Sprite3DFakeShadowTest : public Sprite3DTestDemo
{
public:
CREATE_FUNC(Sprite3DFakeShadowTest);
Sprite3DFakeShadowTest();
virtual std::string title() const override;
virtual std::string subtitle() const override;
};
class Sprite3DBasicToonShaderTest : public Sprite3DTestDemo
{
public:
CREATE_FUNC(Sprite3DBasicToonShaderTest);
Sprite3DBasicToonShaderTest();
virtual std::string title() const override;
virtual std::string subtitle() const override;
};
class EffectSprite3D;
class Effect3D : public Ref
@ -120,7 +138,7 @@ protected:
float _outlineWidth;
//weak reference
EffectSprite3D* _sprite;
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
EventListenerCustom* _backToForegroundListener;
#endif
@ -268,8 +286,11 @@ public:
virtual std::string subtitle() const override;
void onTouchesEnded(const std::vector<Touch*>& touches, Event* event);
void addNewSpriteWithCoords(Vec2 p);
void menuCallback_reSkin(Ref* sender);
void menuCallback_switchHair(Ref* sender);
void menuCallback_switchGlasses(Ref* sender);
void menuCallback_switchCoat(Ref* sender);
void menuCallback_switchPants(Ref* sender);
void menuCallback_switchShoes(Ref* sender);
protected:
void applyCurSkin();
@ -282,11 +303,8 @@ protected:
FACE,
HAND,
GLASSES,
MAX_TYPE,
};
std::vector<std::string> _skins[(int)SkinType::MAX_TYPE]; //all skins
int _curSkin[(int)SkinType::MAX_TYPE]; //current skin index
std::map<SkinType, std::string> _curSkin;
cocos2d::Sprite3D* _sprite;
};
class Sprite3DWithOBBPerfromanceTest : public Sprite3DTestDemo