序:
当初看到dnf 中有些 拖影的效果,感觉很炫,所以到后面自己做游戏的时候自然的就想到了 那种炫 的拖尾效果~, 于是 我就想 要将效果 的实现写下来,以后 自己做游戏 也可以 用的了啊~~~~, 希望 对大家 有 的用处 ,哈哈~
正文:
先来一张效果图~。
就是 蓝色 的 影子了,直观的看就是几张尾巴的图片坐标不同,透明度不一样,可能还有旋转的角度,是否翻转了。
一般的做法,可能会想到多建立几个精灵对象,然后跟着目标走,每一个精灵逐渐改变透明度等等,没错这是直接的 实现,虽然可以 达到目的,但是仔细想想 会很麻烦,而且不利于通用到每一个 游戏中。
下面说说实现的另一种方法~~
gl贴图,用4个点表示然后映射纹理坐标,想要几个尾巴就 对应的多建立几个顶点 了。每一组顶点要对应的改变属性,透明度这里用透明通道改变,翻转就是交换左右或者上下顶点的位置,旋转只要跟随目标的 旋转度就行了,基本上计算就不复杂了。
下面就直接贴出完整代码.(3.x版本,2.x需要适当做些修改)
//SmariShadow.h
#ifndef __SMRITISHADOW_H__
#define __SMRITISHADOW_H__
#include "cocos2d.h"
#include "global.h"
USING_NS_CC;
class SmritiShadow:public Node
{
public:
~SmritiShadow();
void draw(Renderer *renderer,const Mat4 &transform,uint32_t flags);
CREATE_FUNC(SmritiShadow)
bool init();
void onDraw();
//设置跟随目标,spr就是跟随的对象,tex尾巴的纹理,如果为null,那么就给出第三个参数精灵帧。
void setTarget(Sprite* spr,Texture2D* tex,SpriteFrame *sf=0);
//因为用到了自定义shader,所以这里写个重置shader程序的函数,以便在程序切换前台的时候恢复。
static void reset();
static GLProgram* pg;
Sprite* target;
Texture2D *texture2d;
Size srcsz;
CustomCommand _customCommand;
Vertex *Vertices;
long pretime;
int arrcount;
GLuint vertexBuffer;
GLuint indexBuffer;
};
#endif
//SmariShadow.cpp
#include "SmritiShadow.h"
GLProgram* SmritiShadow::pg=0;
void SmritiShadow::draw(Renderer *renderer,uint32_t flags)
{
_customCommand.init(_globalZOrder);
_customCommand.func = CC_CALLBACK_0(SmritiShadow::onDraw,this);
renderer->addCommand(&_customCommand);
}
void SmritiShadow::onDraw()
{
struct timeval tv;
gettimeofday(&tv,NULL);
long ct=tv.tv_sec*1000000+tv.tv_usec;
//如果想改变尾巴的长度,请改写下面的50000~
if(ct-pretime>50000)
{
pretime=ct;
arrcount=0;
for(int i=0;i<20;i+=4)
{
Vertices[i].Position[0]=Vertices[i+4].Position[0];
Vertices[i].Position[1]=Vertices[i+4].Position[1];
Vertices[i+1].Position[0]=Vertices[i+5].Position[0];
Vertices[i+1].Position[1]=Vertices[i+5].Position[1];
Vertices[i+2].Position[0]=Vertices[i+6].Position[0];
Vertices[i+2].Position[1]=Vertices[i+6].Position[1];
Vertices[i+3].Position[0]=Vertices[i+7].Position[0];
Vertices[i+3].Position[1]=Vertices[i+7].Position[1];
}
Size sz=srcsz;
Vec2 pos=target->getPosition();
Vec2 midp=pos;
Size sz1=target->getContentSize();
bool bfx=target->isFlippedX();
bool bfy=target->isFlippedY();
Vec2 ap=target->getAnchorPoint();
float rad=CC_DEGREES_TO_RADIANS(target->getRotation());
pos.x-=sz1.width*ap.x;
pos.y-=sz1.height*ap.y;
float s=sin(-rad);
float c=cos(-rad);
Vertices[20].Position[0]=c*pos.x-s*pos.y+(1-c)*midp.x+s*midp.y;
Vertices[20].Position[1]=s*pos.x+c*pos.y-s*midp.x+(1-c)*midp.y;
Vertices[21].Position[0]=c*(pos.x+sz.width)-s*pos.y+(1-c)*midp.x+s*midp.y;
Vertices[21].Position[1]=s*(pos.x+sz.width)+c*pos.y-s*midp.x+(1-c)*midp.y;
Vertices[22].Position[0]=c*pos.x-s*(pos.y+sz.height)+(1-c)*midp.x+s*midp.y;
Vertices[22].Position[1]=s*pos.x+c*(pos.y+sz.height)-s*midp.x+(1-c)*midp.y;
Vertices[23].Position[0]=c*(pos.x+sz.width)-s*(pos.y+sz.height)+(1-c)*midp.x+s*midp.y;
Vertices[23].Position[1]=s*(pos.x+sz.width)+c*(pos.y+sz.height)-s*midp.x+(1-c)*midp.y;
float tempx=0,tempy=0;
if(bfx)
{
tempx=Vertices[20].Position[0];
tempy=Vertices[20].Position[1];
Vertices[20].Position[0]=Vertices[21].Position[0];
Vertices[20].Position[1]=Vertices[21].Position[1];
Vertices[21].Position[0]=tempx;
Vertices[21].Position[1]=tempy;
tempx=Vertices[22].Position[0];
tempy=Vertices[22].Position[1];
Vertices[22].Position[0]=Vertices[23].Position[0];
Vertices[22].Position[1]=Vertices[23].Position[1];
Vertices[23].Position[0]=tempx;
Vertices[23].Position[1]=tempy;
}
if(bfy)
{
tempx=Vertices[20].Position[0];
tempy=Vertices[20].Position[1];
Vertices[20].Position[0]=Vertices[22].Position[0];
Vertices[20].Position[1]=Vertices[22].Position[1];
Vertices[22].Position[0]=tempx;
Vertices[22].Position[1]=tempy;
tempx=Vertices[21].Position[0];
tempy=Vertices[21].Position[1];
Vertices[21].Position[0]=Vertices[23].Position[0];
Vertices[21].Position[1]=Vertices[23].Position[1];
Vertices[23].Position[0]=tempx;
Vertices[23].Position[1]=tempy;
}
for(int i=16;i>=0;i-=4)
{
if(Vertices[i].Position[0]==Vertices[20].Position[0])
{
arrcount+=1;
}
}
}
glEnable(GL_BLEND);
BlendFunc bf=BlendFunc::ALPHA_NON_PREMULTIPLIED;
glBlendFunc(GL_ONE,GL_ONE);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
pg->use();
pg->setUniformsForBuiltins();
GLushort iarr[]={0,1,2,3,
0+4,1+4,2+4,3+4,
0+8,1+8,2+8,3+8,
0+12,1+12,2+12,3+12,
0+16,1+16,2+16,3+16};
glBindBuffer(GL_ARRAY_BUFFER,vertexBuffer);
glBufferData(GL_ARRAY_BUFFER,sizeof(Vertex)*20,Vertices,GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,indexBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(iarr),iarr,GL_STATIC_DRAW);
GLint _positionLocation = glGetAttribLocation(pg->getProgram(),"a_position");
GLint _colorLocation = glGetAttribLocation(pg->getProgram(),"a_color");
GLint _textureLocation = glGetAttribLocation(pg->getProgram(),"a_texCoord");
GLint _textureUniform = glGetUniformLocation(pg->getProgram(),"CC_Texture0");
glEnableVertexAttribArray(_positionLocation);
glEnableVertexAttribArray(_colorLocation);
glEnableVertexAttribArray(_textureLocation);
glVertexAttribPointer(_positionLocation,GL_FLOAT,GL_FALSE,sizeof(Vertex),(GLvoid*)offsetof(Vertex,Position));
glVertexAttribPointer(_colorLocation,4,Color));
glVertexAttribPointer(_textureLocation,
(GLvoid*)offsetof(Vertex,TexCoord));
GL::bindTexture2DN(0,texture2d->getName());
glUniform1i(_textureUniform,0);
glDrawElements(GL_TRIANGLES,sizeof(iarr)/sizeof(GLushort)-arrcount*6,GL_UNSIGNED_SHORT,0);
glDisable(GL_DEPTH_TEST);
glBlendFunc(bf.src,bf.dst);
glEnable(GL_DEPTH_TEST);
glBindBuffer(GL_ARRAY_BUFFER,0);
}
SmritiShadow::~SmritiShadow()
{
glDeleteBuffers(1,&vertexBuffer);
glDeleteBuffers(1,&indexBuffer);
delete[] Vertices;
}
void SmritiShadow::setTarget(Sprite* spr,SpriteFrame *sf)
{
target=spr;
Rect texrc;
if(sf!=0)
{
texture2d=sf->getTexture();
Rect rc=sf->getRectInPixels();
Size dd=texture2d->getContentSize();
texrc=Rect(rc.origin.x/dd.width,rc.origin.y/dd.height,
(rc.origin.x+rc.size.width)/dd.width,(rc.origin.y+rc.size.height)/dd.height);
srcsz=sf->getRectInPixels().size;
}
else
{
texture2d=tex;
texrc=Rect(0,1);
srcsz=texture2d->getContentSize();
}
Size sz=srcsz;
Size sz1=spr->getContentSize();
Vec2 pos=spr->getPosition();
CCPoint ap=target->getAnchorPoint();
pos.x-=sz1.width*ap.x;
pos.y-=sz1.height*ap.y;
for(int i=0;i<24;i+=4)
{
Vertices[i].Position[0]=pos.x;
Vertices[i].Position[1]=pos.y;
Vertices[i].Position[2]=0;
Vertices[i].TexCoord[0]=texrc.origin.x;
Vertices[i].TexCoord[1]=texrc.size.height;
Vertices[i+1].Position[0]=pos.x+sz.width;
Vertices[i+1].Position[1]=pos.y;
Vertices[i+1].Position[2]=0;
Vertices[i+1].TexCoord[0]=texrc.size.width;
Vertices[i+1].TexCoord[1]=texrc.size.height;
Vertices[i+2].Position[0]=pos.x;
Vertices[i+2].Position[1]=pos.y+sz.height;
Vertices[i+2].Position[2]=0;
Vertices[i+2].TexCoord[0]=texrc.origin.x;
Vertices[i+2].TexCoord[1]=texrc.origin.y;
Vertices[i+3].Position[0]=pos.x+sz.width;
Vertices[i+3].Position[1]=pos.y+sz.height;
Vertices[i+3].Position[2]=0;
Vertices[i+3].TexCoord[0]=texrc.size.width;
Vertices[i+3].TexCoord[1]=texrc.origin.y;
}
}
void SmritiShadow::reset()
{
if(pg==0)
{
pg=new CCGLProgram();
}
pg->reset();
pg->initWithVertexShaderByteArray(vertshader,fragshader);
pg->link();
pg->updateUniforms();
}
bool SmritiShadow::init()
{
struct timeval tv;
gettimeofday(&tv,NULL);
long ct=tv.tv_sec*1000000+tv.tv_usec;
pretime=ct;
arrcount=0;
Vertices=new Vertex[4*6];
for(int i=0;i<24;i+=4)
{
Vertices[i].Color[3]=0.2*(i/4+1);
Vertices[i].TexCoord[0]=0;
Vertices[i].TexCoord[1]=1;
Vertices[i+1].Color[3]=0.2*(i/4+1);
Vertices[i+1].TexCoord[0]=1;
Vertices[i+1].TexCoord[1]=1;
Vertices[i+2].Color[3]=0.2*(i/4+1);
Vertices[i+2].TexCoord[0]=0;
Vertices[i+2].TexCoord[1]=0;
Vertices[i+3].Color[3]=0.2*(i/4+1);
Vertices[i+3].TexCoord[0]=1;
Vertices[i+3].TexCoord[1]=0;
}
glGenBuffers( 1,&vertexBuffer );
glGenBuffers( 1,&indexBuffer );
texture2d=0;
target=0;
if(pg==0)
{
pg=new GLProgram();
pg->initWithVertexShaderByteArray(vertshader,fragshader);
pg->link();
pg->updateUniforms();
}
return true;
}
用法:
SmritiShadow* temp=SmritiShadow::create();
temp->setTarget(obj,tex);//obj跟随对象,tex设置的尾巴纹理。
如果 有 什么 不懂的或者 有啥问题 可以 互相 交流交流~~,end。
原文链接:/cocos2dx/343165.html