Skip to content

Instantly share code, notes, and snippets.

@t-takasaka
Last active September 11, 2015 08:06
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save t-takasaka/99af38d930b2284a499c to your computer and use it in GitHub Desktop.
Save t-takasaka/99af38d930b2284a499c to your computer and use it in GitHub Desktop.
shader test
bool HelloWorld::init(){
if(!Layer::init()){ return false; }
//ブラーの幅等を設定
const int maxRadius = 64;
int step = 1, radius = 40;
int radiusWithStep = radius / step;
Vec2 *weights = new Vec2[maxRadius];
calculateGaussianWeights(radiusWithStep, weights);
//スプライトを作成
Size visibleSize = Director::getInstance()->getVisibleSize();
Sprite *sprite1 = Sprite::create("HelloWorld.png");
Size spriteSize = sprite1->getContentSize();
Size textureSize = sprite1->getTexture()->getContentSizeInPixels();
sprite1->setPosition(Point(spriteSize.width / 2, spriteSize.height / 2));
sprite1->retain();
Size pixelSize = Size(float(step) / textureSize.width, float(step) / textureSize.height);
// this->addChild(sprite1);
//シェーダをスプライトに紐付け(横方向にブラー)
GLProgram *shader1 = new GLProgram();
sprite1->setShaderProgram(shader1);
shader1->initWithFilenames("shaders/blur.vsh", "shaders/blur.fsh");
shader1->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION);
shader1->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS);
shader1->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR);
shader1->link();
shader1->updateUniforms();
shader1->setUniformLocationWith2f(shader1->getUniformLocationForName("pixelSize"), pixelSize.width, pixelSize.height);
shader1->setUniformLocationWith2f(shader1->getUniformLocationForName("direction"), 1.0f, 0.0f);
shader1->setUniformLocationWith1i(shader1->getUniformLocationForName("radius"), radius);
shader1->setUniformLocationWith2fv(shader1->getUniformLocationForName("weights"), (GLfloat*)weights, maxRadius);
// this->addChild(sprite1);
//スプライトをテクスチャに描画
RenderTexture *texture1 = RenderTexture::create(spriteSize.width, spriteSize.height);
texture1->setPosition(Point(visibleSize.width / 2, visibleSize.height / 2));
texture1->retain();
texture1->beginWithClear(0, 0, 0, 1);
sprite1->visit();
texture1->end();
//ブラーのかかったテクスチャを元にスプライトを作成
Sprite *sprite2 = Sprite::createWithTexture(texture1->getSprite()->getTexture());
sprite2->setPosition(Point(visibleSize.width / 2, visibleSize.height / 2));
sprite2->setScale(1.5);
sprite2->setFlippedY(true);
//シェーダをスプライトに紐付け(縦方向にブラー)
GLProgram *shader2 = new GLProgram();
sprite2->setShaderProgram(shader2);
shader2->initWithFilenames("shaders/blur.vsh", "shaders/blur.fsh");
shader2->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION);
shader2->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS);
shader2->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR);
shader2->link();
shader2->updateUniforms();
shader2->setUniformLocationWith2f(shader2->getUniformLocationForName("pixelSize"), pixelSize.width, pixelSize.height);
shader2->setUniformLocationWith2f(shader2->getUniformLocationForName("direction"), 0.0f, 1.0f);
shader2->setUniformLocationWith1i(shader2->getUniformLocationForName("radius"), radius);
shader2->setUniformLocationWith2fv(shader2->getUniformLocationForName("weights"), (GLfloat*)weights, maxRadius);
//スプライトをレイヤーに描画
this->addChild(sprite2);
return true;
}
void HelloWorld::calculateGaussianWeights(const int points, Vec2 *weights){
float dx = 1.0f / float(points-1);
float sigma = 1.0f / 3.0f;
float norm = 1.0f / (sqrtf(2.0f * M_PI) * sigma * points);
float divsigma2 = 0.5f / (sigma * sigma);
weights[0].x = 1.0f;
weights[0].y = 0.0f;
for (int i = 1; i < points; i++){
float x = float(i) * dx;
weights[i].x = norm * expf(-x * x * divsigma2);
weights[i].y = 0.0f;
weights[0].x -= 2.0f * weights[i].x;
}
}
attribute vec4 a_position;
attribute vec2 a_texCoord;
attribute vec4 a_color;
varying vec4 v_fragmentColor;
varying vec2 v_texCoord;
void main() {
gl_Position = CC_PMatrix * a_position;
v_fragmentColor = a_color;
v_texCoord = a_texCoord;
}
#ifdef GL_ES
precision mediump float;
#endif
varying vec2 v_texCoord;
uniform sampler2D u_texture;
uniform vec2 u_texSize;
uniform int u_mosaicLevel;
varying lowp vec4 v_fragmentColor;
uniform vec2 pixelSize;
uniform vec2 direction;
uniform int radius;
uniform vec2 weights[64];
void main() {
vec4 sum = texture2D(u_texture, vec2(v_texCoord.x, v_texCoord.y)) * weights[0].x;
int rad = 40;
for(int i = 1; i < rad; i++){
vec2 offset = vec2(float(i) * pixelSize.x * direction.x, float(i) * pixelSize.y * direction.y);
sum += texture2D(u_texture, v_texCoord + offset) * weights[i].x;
sum += texture2D(u_texture, v_texCoord - offset) * weights[i].x;
}
gl_FragColor = sum * v_fragmentColor;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment