Merge pull request #9101 from super626/v3

Add uv animation test
This commit is contained in:
Ricardo Quesada 2014-11-14 14:18:37 -08:00
commit ac5ffea643
7 changed files with 139 additions and 4 deletions

View File

@ -52,6 +52,7 @@ static std::function<Layer*()> createFunctions[] =
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WP8) && (CC_TARGET_PLATFORM != CC_PLATFORM_WINRT)
// 3DEffect use custom shader which is not supported on WP8/WinRT yet.
CL(Sprite3DEffectTest),
CL(Sprite3DUVAnimationTest),
#endif
CL(Sprite3DWithSkinTest),
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WP8) && (CC_TARGET_PLATFORM != CC_PLATFORM_WINRT)
@ -227,11 +228,106 @@ std::string Sprite3DBasicTest::subtitle() const
//------------------------------------------------------------------
//
// Sprite3DHitTest
// Sprite3DUVAnimationTest
//
//------------------------------------------------------------------
Sprite3DUVAnimationTest::Sprite3DUVAnimationTest()
{
//the offset use to translating texture
_cylinder_texture_offset = 0;
_shining_duraion = 0;
Size visibleSize = Director::getInstance()->getVisibleSize();
//use custom camera
auto camera = Camera::createPerspective(60,visibleSize.width/visibleSize.height,0.1,200);
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");
_state = GLProgramState::create(shader);
cylinder->setGLProgramState(_state);
_state->setUniformFloat("offset",_cylinder_texture_offset);
_state->setUniformFloat("duration",_shining_duraion);
//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);
_state->setVertexAttribPointer(s_attributeNames[meshattribute.vertexAttrib],
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
tRepeatParams.magFilter = GL_LINEAR_MIPMAP_LINEAR;
tRepeatParams.minFilter = GL_LINEAR;
tRepeatParams.wrapS = GL_REPEAT;
tRepeatParams.wrapT = GL_REPEAT;
shining_texture->setTexParameters(tRepeatParams);
//pass the texture sampler to our custom shader
_state->setUniformTexture("caustics",shining_texture);
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));
}
std::string Sprite3DUVAnimationTest::title() const
{
return "Testing UV Animation";
}
std::string Sprite3DUVAnimationTest::subtitle() const
{
return "";
}
void Sprite3DUVAnimationTest::cylinderUpdate(float dt)
{
//callback function to update cylinder's texcoord
static bool fade_in = true;
_cylinder_texture_offset += 0.3*dt;
_cylinder_texture_offset = (_cylinder_texture_offset >1) ? 0 : _cylinder_texture_offset;
if(fade_in)
{
_shining_duraion += 0.5*dt;
if(_shining_duraion>1) fade_in = false;
}
else
{
_shining_duraion -= 0.5*dt;
if(_shining_duraion<0) fade_in = true;
}
//pass the result to shader
_state->setUniformFloat("offset",_cylinder_texture_offset);
_state->setUniformFloat("duration",_shining_duraion);
}
//------------------------------------------------------------------
//
// Sprite3DHitTest
//
//------------------------------------------------------------------
Sprite3DHitTest::Sprite3DHitTest()
{
auto s = Director::getInstance()->getWinSize();

View File

@ -35,6 +35,7 @@ namespace cocos2d {
class Delay;
class Ray;
class DrawNode3D;
class GLProgramState;
}
class Sprite3DTestDemo : public BaseTest
@ -51,9 +52,6 @@ public:
virtual std::string title() const override;
virtual std::string subtitle() const override;
virtual void onEnter() override;
protected:
std::string _title;
};
class Sprite3DBasicTest : public Sprite3DTestDemo
@ -68,6 +66,21 @@ public:
void onTouchesEnded(const std::vector<Touch*>& touches, Event* event);
};
class Sprite3DUVAnimationTest : public Sprite3DTestDemo
{
public:
CREATE_FUNC(Sprite3DUVAnimationTest);
Sprite3DUVAnimationTest();
virtual std::string title() const override;
virtual std::string subtitle() const override;
protected:
void cylinderUpdate(float dt);
float _cylinder_texture_offset;
float _shining_duraion;
GLProgramState * _state;
};
class EffectSprite3D;
class Effect3D : public Ref

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

View File

@ -0,0 +1,16 @@
#ifdef GL_ES
varying mediump vec2 TextureCoordOut;
#else
varying vec2 TextureCoordOut;
#endif
uniform vec4 u_color;
uniform float offset;
uniform float duration;
uniform sampler2D caustics;
void main(void)
{
vec4 golden = duration*vec4(0,0.8,0.4,1.0);
//blend two texture
gl_FragColor = texture2D(CC_Texture0, vec2(TextureCoordOut.x- 2.0 * offset,TextureCoordOut.y)) * vec4(0.3,0.3,0.3,1)+texture2D(caustics,vec2(TextureCoordOut.x-offset,TextureCoordOut.y)).r*golden;
}

View File

@ -0,0 +1,10 @@
attribute vec4 a_position;
attribute vec2 a_texCoord;
uniform float offset;
varying vec2 TextureCoordOut;
void main(void)
{
gl_Position = CC_MVPMatrix * a_position;
TextureCoordOut = a_texCoord;
TextureCoordOut.y = (1.0 - TextureCoordOut.y);
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB