Shader特效——“翻页”的实现 【GLSL】

2024-08-20 15:58
文章标签 实现 特效 shader 翻页 glsl

本文主要是介绍Shader特效——“翻页”的实现 【GLSL】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

参考自:http://webvfx.rectalogic.com/examples_2transition-shader-pagecurl_8html-example.html

效果图





[cpp] view plain  copy
  1. precision mediump float;  
  2.   
  3. varying vec2 texCoord;  
  4. uniform sampler2D sourceTex;  
  5. uniform sampler2D targetTex;  
  6. uniform float time; // Ranges from 0.0 to 1.0  
  7.   
  8. const float MIN_AMOUNT = -0.16;  
  9. const float MAX_AMOUNT = 1.3;  
  10. float amount = time * (MAX_AMOUNT - MIN_AMOUNT) + MIN_AMOUNT;  
  11.   
  12. const float PI = 3.141592653589793;  
  13.   
  14. const float scale = 512.0;  
  15. const float sharpness = 3.0;  
  16.   
  17. float cylinderCenter = amount;  
  18. // 360 degrees * amount  
  19. float cylinderAngle = 2.0 * PI * amount;  
  20.   
  21. const float cylinderRadius = 1.0 / PI / 2.0;  
  22.   
  23. vec3 hitPoint(float hitAngle, float yc, vec3 point, mat3 rrotation) {  
  24.     float hitPoint = hitAngle / (2.0 * PI);  
  25.     point.y = hitPoint;  
  26.     return rrotation * point;  
  27. }  
  28.   
  29.   
  30. vec4 antiAlias(vec4 color1, vec4 color2, float distance) {  
  31.     distance *= scale;  
  32.     if (distance < 0.0) return color2;  
  33.     if (distance > 2.0) return color1;  
  34.     float dd = pow(1.0 - distance / 2.0, sharpness);  
  35.     return ((color2 - color1) * dd) + color1;  
  36. }  
  37.   
  38. float distanceToEdge(vec3 point) {  
  39.     float dx = abs(point.x > 0.5 ? 1.0 - point.x : point.x);  
  40.     float dy = abs(point.y > 0.5 ? 1.0 - point.y : point.y);  
  41.     if (point.x < 0.0) dx = -point.x;  
  42.     if (point.x > 1.0) dx = point.x - 1.0;  
  43.     if (point.y < 0.0) dy = -point.y;  
  44.     if (point.y > 1.0) dy = point.y - 1.0;  
  45.     if ((point.x < 0.0 || point.x > 1.0) && (point.y < 0.0 || point.y > 1.0)) return sqrt(dx * dx + dy * dy);  
  46.     return min(dx, dy);  
  47. }  
  48.   
  49. vec4 seeThrough(float yc, vec2 p, mat3 rotation, mat3 rrotation) {  
  50.     float hitAngle = PI - (acos(yc / cylinderRadius) - cylinderAngle);  
  51.     vec3 point = hitPoint(hitAngle, yc, rotation * vec3(p, 1.0), rrotation);  
  52.   
  53.     if (yc <= 0.0 && (point.x < 0.0 || point.y < 0.0 || point.x > 1.0 || point.y > 1.0)) {  
  54.         return texture2D(targetTex, texCoord);  
  55.     }  
  56.   
  57.     if (yc > 0.0) return texture2D(sourceTex, p);  
  58.   
  59.     vec4 color = texture2D(sourceTex, point.xy);  
  60.     vec4 tcolor = vec4(0.0);  
  61.   
  62.     return antiAlias(color, tcolor, distanceToEdge(point));  
  63. }  
  64.   
  65. vec4 seeThroughWithShadow(float yc, vec2 p, vec3 point, mat3 rotation, mat3 rrotation) {  
  66.     float shadow = distanceToEdge(point) * 30.0;  
  67.     shadow = (1.0 - shadow) / 3.0;  
  68.     if (shadow < 0.0) shadow = 0.0;  
  69.     else shadow *= amount;  
  70.   
  71.     vec4 shadowColor = seeThrough(yc, p, rotation, rrotation);  
  72.     shadowColor.r -= shadow;  
  73.     shadowColor.g -= shadow;  
  74.     shadowColor.b -= shadow;  
  75.     return shadowColor;  
  76. }  
  77.   
  78. vec4 backside(float yc, vec3 point) {  
  79.     vec4 color = texture2D(sourceTex, point.xy);  
  80.     float gray = (color.r + color.b + color.g) / 15.0;  
  81.     gray += (8.0 / 10.0) * (pow(1.0 - abs(yc / cylinderRadius), 2.0 / 10.0) / 2.0 + (5.0 / 10.0));  
  82.     color.rgb = vec3(gray);  
  83.     return color;  
  84. }  
  85.   
  86. vec4 behindSurface(float yc, vec3 point, mat3 rrotation) {  
  87.     float shado = (1.0 - ((-cylinderRadius - yc) / amount * 7.0)) / 6.0;  
  88.     shado *= 1.0 - abs(point.x - 0.5);  
  89.   
  90.     yc = (-cylinderRadius - cylinderRadius - yc);  
  91.   
  92.     float hitAngle = (acos(yc / cylinderRadius) + cylinderAngle) - PI;  
  93.     point = hitPoint(hitAngle, yc, point, rrotation);  
  94.   
  95.     if (yc < 0.0 && point.x >= 0.0 && point.y >= 0.0 && point.x <= 1.0 && point.y <= 1.0 && (hitAngle < PI || amount > 0.5)){  
  96.         shado = 1.0 - (sqrt(pow(point.x - 0.5, 2.0) + pow(point.y - 0.5, 2.0)) / (71.0 / 100.0));  
  97.         shado *= pow(-yc / cylinderRadius, 3.0);  
  98.         shado *= 0.5;  
  99.     } else  
  100.         shado = 0.0;  
  101.   
  102.     return vec4(texture2D(targetTex, texCoord).rgb - shado, 1.0);  
  103. }  
  104.   
  105. void main(void) {  
  106.     const float angle = 30.0 * PI / 180.0;  
  107.     float c = cos(-angle);  
  108.     float s = sin(-angle);  
  109.   
  110.     mat3 rotation = mat3(  
  111.         c, s, 0,  
  112.         -s, c, 0,  
  113.         0.12, 0.258, 1  
  114.     );  
  115.   
  116.     c = cos(angle);  
  117.     s = sin(angle);  
  118.   
  119.     mat3 rrotation = mat3(  
  120.         c, s, 0,  
  121.         -s, c, 0,  
  122.         0.15, -0.5, 1  
  123.     );  
  124.   
  125.     vec3 point = rotation * vec3(texCoord, 1.0);  
  126.   
  127.     float yc = point.y - cylinderCenter;  
  128.   
  129.     if (yc < -cylinderRadius) {  
  130.         // Behind surface  
  131.         gl_FragColor = behindSurface(yc, point, rrotation);  
  132.         return;  
  133.     }  
  134.   
  135.     if (yc > cylinderRadius) {  
  136.         // Flat surface  
  137.         gl_FragColor = texture2D(sourceTex, texCoord);  
  138.         return;  
  139.     }  
  140.   
  141.     float hitAngle = (acos(yc / cylinderRadius) + cylinderAngle) - PI;  
  142.   
  143.     float hitAngleMod = mod(hitAngle, 2.0 * PI);  
  144.     if ((hitAngleMod > PI && amount < 0.5) || (hitAngleMod > PI/2.0 && amount < 0.0)) {  
  145.         gl_FragColor = seeThrough(yc, texCoord, rotation, rrotation);  
  146.         return;  
  147.     }  
  148.   
  149.     point = hitPoint(hitAngle, yc, point, rrotation);  
  150.   
  151.     if (point.x < 0.0 || point.y < 0.0 || point.x > 1.0 || point.y > 1.0) {  
  152.         gl_FragColor = seeThroughWithShadow(yc, texCoord, point, rotation, rrotation);  
  153.         return;  
  154.     }  
  155.   
  156.     vec4 color = backside(yc, point);  
  157.   
  158.     vec4 otherColor;  
  159.     if (yc < 0.0) {  
  160.         float shado = 1.0 - (sqrt(pow(point.x - 0.5, 2.0) + pow(point.y - 0.5, 2.0)) / 0.71);  
  161.         shado *= pow(-yc / cylinderRadius, 3.0);  
  162.         shado *= 0.5;  
  163.         otherColor = vec4(0.0, 0.0, 0.0, shado);  
  164.     } else {  
  165.         otherColor = texture2D(sourceTex, texCoord);  
  166.     }  
  167.   
  168.     color = antiAlias(color, otherColor, cylinderRadius - abs(yc));  
  169.   
  170.     vec4 cl = seeThroughWithShadow(yc, texCoord, point, rotation, rrotation);  
  171.     float dist = distanceToEdge(point);  
  172.   
  173.     gl_FragColor = antiAlias(color, cl, dist);  
  174. }  


翻页的“分块”结构:









版权声明:涉猎过的知识都像是不断汇入大海的涓涓细流,你怎么知道是哪条汇入的溪流让海洋成为海洋呢【转载请注明出处】 https://blog.csdn.net/panda1234lee/article/details/52282741

这篇关于Shader特效——“翻页”的实现 【GLSL】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1090521

相关文章

MySQL中查找重复值的实现

《MySQL中查找重复值的实现》查找重复值是一项常见需求,比如在数据清理、数据分析、数据质量检查等场景下,我们常常需要找出表中某列或多列的重复值,具有一定的参考价值,感兴趣的可以了解一下... 目录技术背景实现步骤方法一:使用GROUP BY和HAVING子句方法二:仅返回重复值方法三:返回完整记录方法四:

IDEA中新建/切换Git分支的实现步骤

《IDEA中新建/切换Git分支的实现步骤》本文主要介绍了IDEA中新建/切换Git分支的实现步骤,通过菜单创建新分支并选择是否切换,创建后在Git详情或右键Checkout中切换分支,感兴趣的可以了... 前提:项目已被Git托管1、点击上方栏Git->NewBrancjsh...2、输入新的分支的

Python实现对阿里云OSS对象存储的操作详解

《Python实现对阿里云OSS对象存储的操作详解》这篇文章主要为大家详细介绍了Python实现对阿里云OSS对象存储的操作相关知识,包括连接,上传,下载,列举等功能,感兴趣的小伙伴可以了解下... 目录一、直接使用代码二、详细使用1. 环境准备2. 初始化配置3. bucket配置创建4. 文件上传到os

关于集合与数组转换实现方法

《关于集合与数组转换实现方法》:本文主要介绍关于集合与数组转换实现方法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、Arrays.asList()1.1、方法作用1.2、内部实现1.3、修改元素的影响1.4、注意事项2、list.toArray()2.1、方

使用Python实现可恢复式多线程下载器

《使用Python实现可恢复式多线程下载器》在数字时代,大文件下载已成为日常操作,本文将手把手教你用Python打造专业级下载器,实现断点续传,多线程加速,速度限制等功能,感兴趣的小伙伴可以了解下... 目录一、智能续传:从崩溃边缘抢救进度二、多线程加速:榨干网络带宽三、速度控制:做网络的好邻居四、终端交互

java实现docker镜像上传到harbor仓库的方式

《java实现docker镜像上传到harbor仓库的方式》:本文主要介绍java实现docker镜像上传到harbor仓库的方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 前 言2. 编写工具类2.1 引入依赖包2.2 使用当前服务器的docker环境推送镜像2.2

C++20管道运算符的实现示例

《C++20管道运算符的实现示例》本文简要介绍C++20管道运算符的使用与实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录标准库的管道运算符使用自己实现类似的管道运算符我们不打算介绍太多,因为它实际属于c++20最为重要的

Java easyExcel实现导入多sheet的Excel

《JavaeasyExcel实现导入多sheet的Excel》这篇文章主要为大家详细介绍了如何使用JavaeasyExcel实现导入多sheet的Excel,文中的示例代码讲解详细,感兴趣的小伙伴可... 目录1.官网2.Excel样式3.代码1.官网easyExcel官网2.Excel样式3.代码

python实现对数据公钥加密与私钥解密

《python实现对数据公钥加密与私钥解密》这篇文章主要为大家详细介绍了如何使用python实现对数据公钥加密与私钥解密,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录公钥私钥的生成使用公钥加密使用私钥解密公钥私钥的生成这一部分,使用python生成公钥与私钥,然后保存在两个文

浏览器插件cursor实现自动注册、续杯的详细过程

《浏览器插件cursor实现自动注册、续杯的详细过程》Cursor简易注册助手脚本通过自动化邮箱填写和验证码获取流程,大大简化了Cursor的注册过程,它不仅提高了注册效率,还通过友好的用户界面和详细... 目录前言功能概述使用方法安装脚本使用流程邮箱输入页面验证码页面实战演示技术实现核心功能实现1. 随机