GLSL shader 磨砂玻璃效果

 

CCProgram vs %{

precision highp float;

#include <cc-global>

in vec3 a_position;

in vec2 a_uv0;

in vec4 a_color;

out vec2 v_uv0;

out vec4 v_color;

void main () {

gl_Position = cc_matViewProj * vec4(a_position, 1);

v_uv0 = a_uv0;

v_color = a_color;

}

}%

CCProgram fs %{

precision highp float;

in vec2 v_uv0;

in vec4 v_color;

uniform sampler2D texture;

uniform Properties {

vec2 size;

};

// 模糊半径

// for 循环的次数必须为常量

const float RADIUS = 20.0;

// 获取模糊颜色

vec4 getBlurColor (vec2 uv) {

vec4 color = vec4(0); // 初始颜色

float sum = 0.0; // 总权重

// 卷积过程

for (float r = -RADIUS; r <= RADIUS; r++) { // 水平方向

float x = uv.x + r / size.x;

if (x < 0.0 || x > 1.0) continue;

for (float c = -RADIUS; c <= RADIUS; c++) { // 垂直方向

float y = uv.y + c / size.y;

if (y < 0.0 || y > 1.0) continue;

vec2 target = vec2(x, y); // 目标纹理坐标

float weight = (RADIUS - abs(r)) * (RADIUS - abs(c)); // 计算权重

color += texture2D(texture, target) * weight; // 累加颜色

sum += weight; // 累加权重

}

}

color /= sum; // 求出平均值

return color;

}

void main () {

vec4 color = v_color;

color *= texture(texture, v_uv0);

if (color.a != 0.0) {

color = getBlurColor(v_uv0); // 获取模糊后的颜色

}

color.a *= v_color.a; // 还原透明度

gl_FragColor = color;

}

}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现环形波纹效果,可以使用OpenGL的着色器语言GLSL来编写着色器程序。具体的实现步骤如下: 1. 定义顶点着色器程序,将顶点坐标传递给片段着色器。 ``` #version 330 core layout (location = 0) in vec3 aPos; void main() { gl_Position = vec4(aPos, 1.0); } ``` 2. 定义片段着色器程序,根据顶点坐标计算出每个像素点的颜色值。 ``` #version 330 core out vec4 FragColor; uniform float time; uniform vec2 center; // 波纹中心 uniform float radius; // 波纹半径 uniform float strength; // 波纹强度 void main() { vec2 uv = gl_FragCoord.xy / vec2(800, 600); // 屏幕坐标系转换到纹理坐标系 float dist = length(uv - center); if (dist < radius) { float alpha = (radius - dist) * strength; float offset = time * 2.0; float angle = atan(uv.y - center.y, uv.x - center.x) + offset; uv.x += alpha * cos(angle); uv.y += alpha * sin(angle); } FragColor = texture(myTexture, uv); } ``` 在片段着色器中,我们定义了一些常量和变量来控制波纹的中心、半径和强度。根据像素点到波纹中心的距离,可以计算出波纹的强度,然后通过一个偏移量来控制波纹的运动方向和速度。最后,将波纹的偏移量应用到纹理坐标上,从而实现波纹效果。 3. 在主程序中,创建一个帧缓冲对象,并将片段着色器渲染的结果绘制到屏幕上。 ``` // 创建帧缓冲对象 unsigned int framebuffer; glGenFramebuffers(1, &framebuffer); glBindFramebuffer(GL_FRAMEBUFFER, framebuffer); // 创建纹理附件 unsigned int texture; glGenTextures(1, &texture); glBindTexture(GL_TEXTURE_2D, texture); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 800, 600, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0); // 检查帧缓冲是否完整 if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) std::cout << "ERROR::FRAMEBUFFER:: Framebuffer is not complete!" << std::endl; // 渲染循环 while (!glfwWindowShouldClose(window)) { float time = glfwGetTime(); // 渲染到帧缓冲 glBindFramebuffer(GL_FRAMEBUFFER, framebuffer); glViewport(0, 0, 800, 600); glClear(GL_COLOR_BUFFER_BIT); shader.use(); shader.setFloat("time", time); shader.setVec2("center", vec2(0.5, 0.5)); shader.setFloat("radius", 0.3); shader.setFloat("strength", 0.1); glBindVertexArray(VAO); glDrawArrays(GL_TRIANGLES, 0, 6); // 渲染到屏幕 glBindFramebuffer(GL_FRAMEBUFFER, 0); glViewport(0, 0, 800, 600); glClear(GL_COLOR_BUFFER_BIT); screenShader.use(); glBindVertexArray(screenVAO); glBindTexture(GL_TEXTURE_2D, texture); glDrawArrays(GL_TRIANGLES, 0, 6); glfwSwapBuffers(window); glfwPollEvents(); } ``` 在主程序中,我们创建了一个帧缓冲对象,并将片段着色器渲染的结果绘制到帧缓冲对象中。然后再将帧缓冲对象中的纹理绘制到屏幕上。这样就可以实现环形波纹效果了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值