dissolve.effect 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. CCProgram vs %{
  2. //顶点着色器
  3. precision highp float;
  4. #include <cc-global>
  5. in vec3 a_position;
  6. in vec2 a_texCoord;
  7. out vec2 v_uv;
  8. void main () {
  9. vec4 pos = vec4(a_position, 1.0);
  10. v_uv = a_texCoord;
  11. gl_Position = cc_matViewProj * pos;
  12. }
  13. }%
  14. CCProgram fs %{
  15. //片元着色器
  16. precision highp float;
  17. #include <cc-global>
  18. in vec2 v_uv;
  19. layout(location = 0) out vec4 fragColor;
  20. // 修改 objectCenter 为 vec4 类型,避免对齐问题
  21. layout(set = 2, binding = 0) uniform DissolveUniforms {
  22. vec4 objectCenter; // 改为 vec4,避免对齐错误
  23. vec4 edgeColor; // 边缘颜色(火焰灼烧效果)
  24. float dissolveThreshold; // 消融阈值
  25. float edgeWidth; // 边缘宽度
  26. };
  27. layout(set = 2, binding = 1) uniform sampler2D mainTexture;
  28. layout(set = 2, binding = 2) uniform sampler2D dissolveTexture;
  29. void main () {
  30. // 获取主纹理颜色
  31. vec4 color = texture(mainTexture, v_uv);
  32. // 获取消融纹理的值
  33. float dissolveValue = texture(dissolveTexture, v_uv).r;
  34. // 计算当前像素与物体中心的距离(物体中心可以设置为 objectCenter.xy)
  35. vec2 centerUV = v_uv; // 将UV坐标转换为[-1, 1]范围
  36. float distanceFromCenter = length(centerUV); // 计算与中心的距离
  37. // 基于距离的消融效果
  38. // 边缘的透明度(火焰效果逐渐消失的部分)
  39. float dissolveFactor = smoothstep(dissolveThreshold - edgeWidth, dissolveThreshold + edgeWidth, dissolveValue - distanceFromCenter);
  40. // 这里给边缘区域添加一个渐变效果,模拟火焰灼烧的感觉
  41. // 边缘会有一个渐变的颜色,从边缘颜色到透明
  42. vec4 fireEdgeColor = mix(edgeColor, vec4(0.0, 0.0, 0.0, 0.0), dissolveFactor);
  43. // 判断是否超出阈值,应用透明度
  44. if (dissolveValue < dissolveThreshold) {
  45. // 超过阈值时显示边缘颜色
  46. fragColor = mix(fireEdgeColor, vec4(0.0), dissolveFactor);
  47. } else {
  48. // 否则显示原始纹理颜色
  49. fragColor = color;
  50. }
  51. // 边缘部分逐渐消融,模拟火焰燃烧的消融效果
  52. fragColor.a *= (1.0 - dissolveValue); // 基于 dissolveValue 控制透明度
  53. }
  54. }%
  55. CCEffect %{
  56. techniques:
  57. - passes:
  58. - vert: vs
  59. frag: fs
  60. rasterizerState:
  61. cullMode: none
  62. blendState:
  63. blend: true
  64. srcBlend: srcAlpha
  65. dstBlend: oneMinusSrcAlpha
  66. properties:
  67. mainTexture:
  68. value: white
  69. editor:
  70. type: texture2D
  71. dissolveTexture:
  72. value: white
  73. editor:
  74. type: texture2D
  75. dissolveThreshold:
  76. value: 0.5
  77. editor:
  78. type: slider
  79. min: 0
  80. max: 1
  81. edgeColor:
  82. value: [1.0, 0.5, 0.0, 1.0] # 这里设置边缘的颜色,模拟火焰灼烧
  83. editor:
  84. type: color
  85. edgeWidth:
  86. value: 0.1
  87. editor:
  88. type: slider
  89. min: 0
  90. max: 1
  91. objectCenter:
  92. value: [0.0, 0.0, 0.0, 0.0]
  93. editor:
  94. type: vec4
  95. }%