diff --git a/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp b/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp index 71965a0bc3..1bf14a0e57 100644 --- a/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp +++ b/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp @@ -52,6 +52,7 @@ static std::function 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) @@ -225,6 +226,103 @@ std::string Sprite3DBasicTest::subtitle() const return "Tap screen to add more sprites"; } +//------------------------------------------------------------------ +// +// Sprite3DUVAnimationTest +// +//------------------------------------------------------------------ + +Sprite3DUVAnimationTest::Sprite3DUVAnimationTest() +{ + //the offset use to translating texture + this->cylinder_texture_offset = 0; + this->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 "The UV Animation of Sprite3D"; +} + +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 @@ -1445,3 +1543,5 @@ void Sprite3DMirrorTest::addNewSpriteWithCoords(Vec2 p) } _mirrorSprite = sprite; } + + diff --git a/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.h b/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.h index 721d1ee105..f624258c72 100644 --- a/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.h +++ b/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.h @@ -68,6 +68,19 @@ public: void onTouchesEnded(const std::vector& touches, Event* event); }; +class Sprite3DUVAnimationTest : public Sprite3DTestDemo +{ +public: + CREATE_FUNC(Sprite3DUVAnimationTest); + Sprite3DUVAnimationTest(); + virtual std::string title() const override; + virtual std::string subtitle() const override; +private: + void cylinderUpdate(float dt); + float cylinder_texture_offset; + float shining_duraion; + GLProgramState * state; +}; class EffectSprite3D; class Effect3D : public Ref diff --git a/tests/cpp-tests/Resources/Sprite3DTest/caustics.png b/tests/cpp-tests/Resources/Sprite3DTest/caustics.png new file mode 100644 index 0000000000..179f30dbc7 Binary files /dev/null and b/tests/cpp-tests/Resources/Sprite3DTest/caustics.png differ diff --git a/tests/cpp-tests/Resources/Sprite3DTest/cylinder.c3b b/tests/cpp-tests/Resources/Sprite3DTest/cylinder.c3b new file mode 100644 index 0000000000..be07b5b05a Binary files /dev/null and b/tests/cpp-tests/Resources/Sprite3DTest/cylinder.c3b differ diff --git a/tests/cpp-tests/Resources/Sprite3DTest/cylinder.frag b/tests/cpp-tests/Resources/Sprite3DTest/cylinder.frag new file mode 100644 index 0000000000..1cae8301d7 --- /dev/null +++ b/tests/cpp-tests/Resources/Sprite3DTest/cylinder.frag @@ -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; +} \ No newline at end of file diff --git a/tests/cpp-tests/Resources/Sprite3DTest/cylinder.vert b/tests/cpp-tests/Resources/Sprite3DTest/cylinder.vert new file mode 100644 index 0000000000..01da9c33bf --- /dev/null +++ b/tests/cpp-tests/Resources/Sprite3DTest/cylinder.vert @@ -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); +} diff --git a/tests/cpp-tests/Resources/Sprite3DTest/dragon.png b/tests/cpp-tests/Resources/Sprite3DTest/dragon.png new file mode 100644 index 0000000000..0542b67ac3 Binary files /dev/null and b/tests/cpp-tests/Resources/Sprite3DTest/dragon.png differ