來個果凍
至于調(diào)用,只要放一張
這樣的小圓圖两曼,創(chuàng)建一個精靈-用
auto ac1 = CCLiquid::create(1, CCSizeMake(12,12), 3, 22);
pSprite->runAction(ac1);
看起來是不是有點像果凍秫逝,或者說點擊果凍的時候
這個流體的術(shù)是什么,在我們點擊的時候到底對這張圖片做了什么操作
CCliquid這個函數(shù)的創(chuàng)建放在CCActionGrid3D這里唉铜,liquid的創(chuàng)建傳入了四個參數(shù)
執(zhí)行時間玖院、切割細(xì)致程度菠红,彈動次數(shù),彈動幅度
void CCLiquid::update(float time)
{
int i, j;
for (i = 1; i < m_sGridSize.width; ++i)
{
for (j = 1; j < m_sGridSize.height; ++j)
{
ccVertex3F v = originalVertex(ccp(i, j));
v.x = (v.x + (sinf(time * (float)M_PI * m_nWaves * 2 + v.x * (1.0f - pow(time,4))*0.01) * m_fAmplitude * m_fAmplitudeRate));
v.y = (v.y + (sinf(time * (float)M_PI * m_nWaves * 2 + v.y * (1.0f - pow(time,4))*0.01) * m_fAmplitude * m_fAmplitudeRate));
setVertex(ccp(i, j), v);
}
}
}
這里你和cocos2d該文件下的原函數(shù)做對比可能也發(fā)現(xiàn)了
//原函數(shù)
v.x = (v.x + (sinf(time * (float)M_PI * _waves * 2 + v.x * .01f) * _amplitude * _amplitudeRate));
//不變原效果的小改造
v.x = (v.x + (sinf(time * (float)M_PI * m_nWaves * 2 + v.x * (1.0f - pow(time,4))*0.01) * m_fAmplitude * m_fAmplitudeRate));
這里多了一個 (1.0f - pow(time,4)) 這是因為原函數(shù)將一個圖形的頂點進行拉伸后难菌,到時間結(jié)束试溯,狀態(tài)并沒有將它復(fù)原,動作完成這時候time=1.0f 帶入原函數(shù)
v.x = v.x + sin(v.x0.01) 到結(jié)束的時候X軸還是多出了sin(0.01v.x)
因此我所加的 (1.0f - pow(time,4)) 作用便是在結(jié)束的時候把動畫拉回來郊酒,同期修復(fù)的還有ccwave3d遇绞,CCRipple3D 键袱,CCshake3d,CCwaves 好吧這算一個方法摹闽,當(dāng)然蹄咖,僅僅修改原本存在的小bug這算什么事,那我多嘮兩句吧付鹿!
用wave3d試試
我們讓水波動起來
D.imgp("illustrated/bg.png", 0, 0):anchor(ccp(0, 0)):to(self)
-- 水波紋1
local ripple1 = D.imgp("illustrated/ripple.png", 0, 0):anchor(ccp(0, 0)):to(self)
local waves3D = CCLiquid:create(8, CCSize(10, 10), 4, 10)
local RepeatForever = CCRepeatForever:create(waves3D)
ripple1:runAction(RepeatForever)
簡單的Shader模仿
好吧澜汤,敢用Shader的前提是你所用的框架處理shader的垃圾回收處理做的不錯,不會因為開一小會就卡的不行倘屹,這里我直接放源碼,原理就是像素點的偏移
#ifdef GL_ES
precision mediump float;
#endif
//固定模式傳入長寬,resolution即分辨率 CC_Texture0為自身像素 PosX,PosY為鼠標(biāo)位置
//v_texCoord 圖片全屏?xí)r候等于 vec2( gl_FragCoord.x/width, (1.-gl_FragCoord.y/height) );
uniform float width;
uniform float height;
uniform float PosX;
uniform float PosY;
#define resolution vec2(width,height)
uniform sampler2D CC_Texture0;
varying vec2 v_texCoord;
varying vec4 v_fragmentColor;
//uniform float time;
#define time CC_Time.x
const float PI = 3.1415926535897932;
const float speed = 0.2;
const float speed_x = 0.3;
const float speed_y = 0.3;
const float intensity = 3.0;
const int steps = 8;
const float frequency = 4.0;
const int angle = 7;
const float delta = 20.0;
const float intence = 400.0;
const float emboss = 0.3;
float col(vec2 coord)
{
float delta_theta = 2.0 * PI / float(angle);
float col = 0.0;
float theta = 0.0;
for (int i = 0; i < steps; i++)
{
vec2 adjc = coord;
theta = delta_theta * float(i);
adjc.x += cos(theta)*time*speed + time * speed_x;
adjc.y -= sin(theta)*time*speed - time * speed_y;
col = col + cos((adjc.x*cos(theta) - adjc.y*sin(theta))*frequency)*intensity;
}
return cos(col);
}
void main()
{
vec2 p = (gl_FragCoord.xy) / resolution.xy, c1 = p, c2 = p;
float cc1 = col(c1);
c2.x += resolution.x/delta;
float dx = emboss*(cc1-col(c2))/delta;
c2.x = p.x;
c2.y += resolution.y/delta;
float dy = emboss*(cc1-col(c2))/delta;
c1.x += dx;
c1.y += dy;
float alpha = 1.+dot(dx,dy)*intence;
gl_FragColor = v_fragmentColor*texture2D(CC_Texture0,vec2(c1.x,1.-c1.y))*(alpha*alpha);//*(alpha) *v_color*(alpha);
}
等等...發(fā)生了什么情況,我剛插了一張gif結(jié)果前面寫的東西不見了...這是有長度限制慢叨?那就先到這里吧纽匙!--ByBBChangwei-16-04-04