mirror of https://github.com/axmolengine/axmol.git
add three samples -- toonshader fakeshadow lightmap
This commit is contained in:
parent
73014e8be3
commit
086bca399f
|
@ -55,6 +55,7 @@ static std::function<Layer*()> createFunctions[] =
|
|||
CL(Sprite3DUVAnimationTest),
|
||||
CL(Sprite3DFakeShadowTest),
|
||||
CL(Sprite3DBasicToonShaderTest),
|
||||
CL(Sprite3DLightMapTest),
|
||||
#endif
|
||||
CL(Sprite3DWithSkinTest),
|
||||
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WP8) && (CC_TARGET_PLATFORM != CC_PLATFORM_WINRT)
|
||||
|
@ -1545,28 +1546,54 @@ void Sprite3DMirrorTest::addNewSpriteWithCoords(Vec2 p)
|
|||
Sprite3DFakeShadowTest::Sprite3DFakeShadowTest()
|
||||
{
|
||||
Size visibleSize = Director::getInstance()->getVisibleSize();
|
||||
|
||||
//create UI
|
||||
TTFConfig ttfConfig("fonts/arial.ttf", 16);
|
||||
auto label1 = Label::createWithTTF(ttfConfig,"move_left");
|
||||
auto menuItem1 = MenuItemLabel::create(label1, CC_CALLBACK_1(Sprite3DFakeShadowTest::Move,this,-1));
|
||||
auto label2 = Label::createWithTTF(ttfConfig,"move_right");
|
||||
auto menuItem2 = MenuItemLabel::create(label2, CC_CALLBACK_1(Sprite3DFakeShadowTest::Move,this,1));
|
||||
auto menu = Menu::create(menuItem1, menuItem2,nullptr);
|
||||
menu->setPosition(Vec2::ZERO);
|
||||
menuItem1->setPosition( Vec2( visibleSize.width-80, visibleSize.height-160) );
|
||||
menuItem2->setPosition( Vec2( visibleSize.width-80, visibleSize.height-190) );
|
||||
|
||||
|
||||
auto layer = Layer::create();
|
||||
addChild(layer,0);
|
||||
addChild(menu, 10);
|
||||
|
||||
//create Camera
|
||||
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));
|
||||
_camera->setPosition3D(Vec3(0,20,25));
|
||||
_camera->setRotation3D(Vec3(-60,0,0));
|
||||
|
||||
//create a plane
|
||||
_plane = Sprite3D::create("Sprite3DTest/plane.c3t");
|
||||
_plane->setRotation3D(Vec3(90,0,0));
|
||||
|
||||
// use custom shader
|
||||
auto shader =GLProgram::createWithFilenames("Sprite3DTest/simple_shadow.vert","Sprite3DTest/simple_shadow.frag");
|
||||
auto state = GLProgramState::create(shader);
|
||||
plane->setGLProgramState(state);
|
||||
_plane->setGLProgramState(state);
|
||||
|
||||
//pass mesh's attribute to shader
|
||||
long offset = 0;
|
||||
auto attributeCount = plane->getMesh()->getMeshVertexAttribCount();
|
||||
auto attributeCount = _plane->getMesh()->getMeshVertexAttribCount();
|
||||
for (auto i = 0; i < attributeCount; i++) {
|
||||
auto meshattribute = plane->getMesh()->getMeshVertexAttribute(i);
|
||||
auto meshattribute = _plane->getMesh()->getMeshVertexAttribute(i);
|
||||
state->setVertexAttribPointer(s_attributeNames[meshattribute.vertexAttrib],
|
||||
meshattribute.size,
|
||||
meshattribute.type,
|
||||
GL_FALSE,
|
||||
plane->getMesh()->getVertexSizeInBytes(),
|
||||
_plane->getMesh()->getVertexSizeInBytes(),
|
||||
(GLvoid*)offset);
|
||||
offset += meshattribute.attribSizeBytes;
|
||||
}
|
||||
state->setUniformMat4("u_model_matrix",plane->getNodeToWorldTransform());
|
||||
}
|
||||
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;
|
||||
|
@ -1575,15 +1602,17 @@ Sprite3DFakeShadowTest::Sprite3DFakeShadowTest()
|
|||
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);
|
||||
layer->addChild(_plane);
|
||||
|
||||
//create the orc
|
||||
_orc = Sprite3D::create("Sprite3DTest/orc.c3b");
|
||||
_orc->setScale(0.2);
|
||||
_orc->setRotation3D(Vec3(0,180,0));
|
||||
_orc->setPosition3D(Vec3(0,0,10));
|
||||
_plane->getGLProgramState()->setUniformVec3("u_target_pos",_orc->getPosition3D());
|
||||
layer->addChild(_orc);
|
||||
layer->addChild(_camera);
|
||||
layer->setCameraMask(2);
|
||||
}
|
||||
|
||||
std::string Sprite3DFakeShadowTest::title() const
|
||||
|
@ -1593,9 +1622,17 @@ std::string Sprite3DFakeShadowTest::title() const
|
|||
|
||||
std::string Sprite3DFakeShadowTest::subtitle() const
|
||||
{
|
||||
return "fake shadow effect";
|
||||
return "touch the label to move it";
|
||||
}
|
||||
|
||||
void Sprite3DFakeShadowTest::Move(cocos2d::Ref* sender,int value)
|
||||
{
|
||||
_orc->setPositionX(_orc->getPositionX()+value);
|
||||
//pass the newest orc position
|
||||
_plane->getGLProgramState()->setUniformVec3("u_target_pos",_orc->getPosition3D());
|
||||
}
|
||||
|
||||
|
||||
//the basic toon shader test
|
||||
Sprite3DBasicToonShaderTest::Sprite3DBasicToonShaderTest()
|
||||
{
|
||||
|
@ -1639,3 +1676,65 @@ std::string Sprite3DBasicToonShaderTest::subtitle() const
|
|||
{
|
||||
return " ";
|
||||
}
|
||||
//basic light map test
|
||||
//the assets are from the OpenVR demo
|
||||
Sprite3DLightMapTest::Sprite3DLightMapTest()
|
||||
{
|
||||
//get the visible size.
|
||||
Size visibleSize = Director::getInstance()->getVisibleSize();
|
||||
_camera = Camera::createPerspective(60,visibleSize.width/visibleSize.height,0.1,200);
|
||||
_camera->setCameraFlag(CameraFlag::USER1);
|
||||
_camera->setPosition3D(Vec3(0,15,15));
|
||||
auto LightMapScene = Sprite3D::create("Sprite3DTest/LightMapScene.c3b");
|
||||
LightMapScene->setScale(0.1);
|
||||
addChild(LightMapScene);
|
||||
addChild(_camera);
|
||||
setCameraMask(2);
|
||||
|
||||
//add a point light
|
||||
auto light = PointLight::create(Vec3(35,75,-20.5),Color3B(255,255,255),700);
|
||||
addChild(light);
|
||||
//set the ambient light
|
||||
auto ambient = AmbientLight::create(Color3B(100,100,100));
|
||||
addChild(ambient);
|
||||
|
||||
//create a listener
|
||||
auto listener = EventListenerTouchAllAtOnce::create();
|
||||
listener->onTouchesMoved = CC_CALLBACK_2(Sprite3DLightMapTest::onTouchesMoved, this);
|
||||
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
||||
}
|
||||
|
||||
std::string Sprite3DLightMapTest::title() const
|
||||
{
|
||||
return "light map test";
|
||||
}
|
||||
|
||||
std::string Sprite3DLightMapTest::subtitle() const
|
||||
{
|
||||
return "drag the screen to move around";
|
||||
}
|
||||
|
||||
void Sprite3DLightMapTest::onTouchesMoved(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event* event)
|
||||
{
|
||||
if(touches.size()==1)
|
||||
{
|
||||
float delta = Director::getInstance()->getDeltaTime();
|
||||
auto touch = touches[0];
|
||||
auto location = touch->getLocation();
|
||||
auto PreviousLocation = touch->getPreviousLocation();
|
||||
Point newPos = PreviousLocation - location;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -89,8 +89,24 @@ public:
|
|||
Sprite3DFakeShadowTest();
|
||||
virtual std::string title() const override;
|
||||
virtual std::string subtitle() const override;
|
||||
void Move(cocos2d::Ref* sender,int value);
|
||||
private:
|
||||
cocos2d::Sprite3D * _plane;
|
||||
cocos2d::Sprite3D * _orc;
|
||||
};
|
||||
|
||||
|
||||
class Sprite3DLightMapTest : public Sprite3DTestDemo
|
||||
{
|
||||
public:
|
||||
CREATE_FUNC(Sprite3DLightMapTest);
|
||||
Sprite3DLightMapTest();
|
||||
virtual std::string title() const override;
|
||||
virtual std::string subtitle() const override;
|
||||
void onTouchesMoved(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event* event);
|
||||
private:
|
||||
Camera * _camera;
|
||||
};
|
||||
class Sprite3DBasicToonShaderTest : public Sprite3DTestDemo
|
||||
{
|
||||
public:
|
||||
|
@ -98,6 +114,7 @@ public:
|
|||
Sprite3DBasicToonShaderTest();
|
||||
virtual std::string title() const override;
|
||||
virtual std::string subtitle() const override;
|
||||
|
||||
};
|
||||
class EffectSprite3D;
|
||||
|
||||
|
|
Binary file not shown.
After Width: | Height: | Size: 73 KiB |
Binary file not shown.
|
@ -10,7 +10,7 @@ uniform float duration;
|
|||
uniform sampler2D caustics;
|
||||
void main(void)
|
||||
{
|
||||
vec4 golden = duration*vec4(0,0.8,0.4,1.0);
|
||||
vec4 color = 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;
|
||||
gl_FragColor = u_color*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*color;
|
||||
}
|
|
@ -0,0 +1,93 @@
|
|||
{
|
||||
"version": "0.3",
|
||||
"id": "",
|
||||
"meshes": [
|
||||
{
|
||||
"attributes": [{
|
||||
"size": 3,
|
||||
"type": "GL_FLOAT",
|
||||
"attribute": "VERTEX_ATTRIB_POSITION"
|
||||
}, {
|
||||
"size": 3,
|
||||
"type": "GL_FLOAT",
|
||||
"attribute": "VERTEX_ATTRIB_NORMAL"
|
||||
}, {
|
||||
"size": 2,
|
||||
"type": "GL_FLOAT",
|
||||
"attribute": "VERTEX_ATTRIB_TEX_COORD"
|
||||
}],
|
||||
"vertices": [
|
||||
50.000000, -50.000000, 0.000000, 0.000000, 0.000000, -1.000000, 0.000000, 0.000000,
|
||||
-50.000000, -50.000000, 0.000000, 0.000000, 0.000000, -1.000000, 1.000000, 0.000000,
|
||||
50.000000, 50.000000, 0.000000, 0.000000, 0.000000, -1.000000, 0.000000, 1.000000,
|
||||
-50.000000, 50.000000, 0.000000, 0.000000, 0.000000, -1.000000, 1.000000, 1.000000,
|
||||
50.000000, 50.000000, 3.000000, 0.000000, 0.000000, 1.000000, 1.000000, 1.000000,
|
||||
-50.000000, 50.000000, 3.000000, 0.000000, 0.000000, 1.000000, 0.000000, 1.000000,
|
||||
-50.000000, -50.000000, 3.000000, 0.000000, 0.000000, 1.000000, 0.000000, 0.000000,
|
||||
50.000000, -50.000000, 3.000000, 0.000000, 0.000000, 1.000000, 1.000000, 0.000000,
|
||||
50.000000, -50.000000, 3.000000, 0.000000, -1.000000, 0.000000, 1.000000, 1.000000,
|
||||
-50.000000, -50.000000, 3.000000, 0.000000, -1.000000, 0.000000, 0.000000, 1.000000,
|
||||
-50.000000, -50.000000, 0.000000, 0.000000, -1.000000, 0.000000, 0.000000, 0.000000,
|
||||
50.000000, -50.000000, 0.000000, 0.000000, -1.000000, 0.000000, 1.000000, 0.000000,
|
||||
50.000000, 50.000000, 3.000000, 1.000000, 0.000000, 0.000000, 1.000000, 1.000000,
|
||||
50.000000, -50.000000, 3.000000, 1.000000, 0.000000, 0.000000, 0.000000, 1.000000,
|
||||
50.000000, -50.000000, 0.000000, 1.000000, 0.000000, 0.000000, 0.000000, 0.000000,
|
||||
50.000000, 50.000000, 0.000000, 1.000000, 0.000000, 0.000000, 1.000000, 0.000000,
|
||||
-50.000000, 50.000000, 3.000000, 0.000000, 1.000000, 0.000000, 1.000000, 1.000000,
|
||||
50.000000, 50.000000, 3.000000, 0.000000, 1.000000, 0.000000, 0.000000, 1.000000,
|
||||
50.000000, 50.000000, 0.000000, 0.000000, 1.000000, 0.000000, 0.000000, 0.000000,
|
||||
-50.000000, 50.000000, 0.000000, 0.000000, 1.000000, 0.000000, 1.000000, 0.000000,
|
||||
-50.000000, -50.000000, 3.000000, -1.000000, 0.000000, 0.000000, 1.000000, 1.000000,
|
||||
-50.000000, 50.000000, 3.000000, -1.000000, 0.000000, 0.000000, 0.000000, 1.000000,
|
||||
-50.000000, 50.000000, 0.000000, -1.000000, 0.000000, 0.000000, 0.000000, 0.000000,
|
||||
-50.000000, -50.000000, 0.000000, -1.000000, 0.000000, 0.000000, 1.000000, 0.000000
|
||||
],
|
||||
"parts": [
|
||||
{
|
||||
"id": "shape1_part1",
|
||||
"type": "TRIANGLES",
|
||||
"indices": [
|
||||
0, 1, 2, 1, 3, 2, 4, 5, 6, 4, 6, 7,
|
||||
8, 9, 10, 8, 10, 11, 12, 13, 14, 12, 14, 15,
|
||||
16, 17, 18, 16, 18, 19, 20, 21, 22, 20, 22, 23
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"materials": [
|
||||
{
|
||||
"id": "01 - Default",
|
||||
"ambient": [ 0.588235, 0.588235, 0.588235],
|
||||
"diffuse": [ 0.588235, 0.588235, 0.588235],
|
||||
"emissive": [ 0.000000, 0.000000, 0.000000],
|
||||
"opacity": 1.000000,
|
||||
"specular": [ 0.900000, 0.900000, 0.900000],
|
||||
"shininess": 2.000000,
|
||||
"textures": [
|
||||
{
|
||||
"id": "Map #1",
|
||||
"filename": "plane.png",
|
||||
"type": "DIFFUSE",
|
||||
"wrapModeU": "REPEAT",
|
||||
"wrapModeV": "REPEAT"
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"nodes": [
|
||||
{
|
||||
"id": "Box001",
|
||||
"skeleton": false,
|
||||
"transform": [ 1.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, -1.000000, 0.000000, 0.000000, 1.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 1.000000],
|
||||
"parts": [
|
||||
{
|
||||
"meshpartid": "shape1_part1",
|
||||
"materialid": "01 - Default",
|
||||
"uvMapping": [[ 0]]
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"animations": []
|
||||
}
|
Binary file not shown.
After Width: | Height: | Size: 28 KiB |
Binary file not shown.
After Width: | Height: | Size: 3.5 KiB |
|
@ -0,0 +1,22 @@
|
|||
#ifdef GL_ES
|
||||
varying mediump vec2 TextureCoordOut;
|
||||
varying mediump vec4 v_position;
|
||||
#else
|
||||
varying vec2 TextureCoordOut;
|
||||
varying vec4 v_position;
|
||||
#endif
|
||||
uniform sampler2D u_shadowTexture;
|
||||
uniform vec3 u_target_pos;
|
||||
uniform vec4 u_color;
|
||||
void main(void)
|
||||
{
|
||||
|
||||
float Radius = 4.0f;//project range
|
||||
vec3 UVector = vec3(1.0f, 0.0f, 0.0f)/(2.0f * Radius);
|
||||
vec3 VVector = vec3(0.0f, 0.0f, -1.0f)/(-2.0f * Radius);
|
||||
vec2 coord;
|
||||
coord.x = dot(v_position.xyz - u_target_pos, UVector) + 0.5f;
|
||||
coord.y = dot(v_position.xyz - u_target_pos, VVector) + 0.5f;
|
||||
|
||||
gl_FragColor = u_color*texture2D(CC_Texture0,TextureCoordOut)*texture2D(u_shadowTexture,coord);
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
attribute vec4 a_position;
|
||||
attribute vec2 a_texCoord;
|
||||
varying vec2 TextureCoordOut;
|
||||
varying vec4 v_position;
|
||||
uniform mat4 u_model_matrix;
|
||||
void main(void)
|
||||
{
|
||||
gl_Position = CC_PMatrix * CC_MVMatrix * a_position;
|
||||
TextureCoordOut = a_texCoord;
|
||||
TextureCoordOut.y = (1.0 - TextureCoordOut.y);
|
||||
v_position = u_model_matrix * a_position;
|
||||
}
|
Binary file not shown.
Binary file not shown.
After Width: | Height: | Size: 141 B |
Binary file not shown.
After Width: | Height: | Size: 177 B |
|
@ -0,0 +1,24 @@
|
|||
#ifdef GL_ES
|
||||
varying mediump vec2 TextureCoordOut;
|
||||
#else
|
||||
varying vec2 TextureCoordOut;
|
||||
#endif
|
||||
uniform vec4 u_color;
|
||||
varying vec3 v_normal;
|
||||
void main(void)
|
||||
{
|
||||
vec3 light_direction = vec3(1,-1,0);
|
||||
light_direction = normalize(light_direction);
|
||||
vec3 light_color = vec3(1,1,1);
|
||||
vec3 normal = normalize(v_normal);
|
||||
float diffuse_factor = dot(normal,-light_direction);
|
||||
vec4 diffuse_color = texture2D(CC_Texture0,TextureCoordOut);
|
||||
|
||||
if (diffuse_factor > 0.95) diffuse_factor=1.0;
|
||||
else if (diffuse_factor > 0.75) diffuse_factor = 0.8;
|
||||
else if (diffuse_factor > 0.50) diffuse_factor = 0.6;
|
||||
else diffuse_factor = 0.4;
|
||||
|
||||
light_color = light_color * diffuse_factor;
|
||||
gl_FragColor = vec4(light_color,1.0) * diffuse_color * u_color;
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
attribute vec4 a_position;
|
||||
attribute vec2 a_texCoord;
|
||||
attribute vec3 a_normal;
|
||||
varying vec2 TextureCoordOut;
|
||||
varying vec3 v_normal;
|
||||
void main(void)
|
||||
{
|
||||
gl_Position = CC_MVPMatrix * a_position;
|
||||
TextureCoordOut = a_texCoord;
|
||||
TextureCoordOut.y = (1.0 - TextureCoordOut.y);
|
||||
v_normal = CC_NormalMatrix *a_normal;
|
||||
}
|
Loading…
Reference in New Issue