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

猜你喜欢

转载自blog.csdn.net/u012871784/article/details/79803286