MakeBlur.hx 5.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package arm.node;
  2. import arm.shader.NodeShader;
  3. class MakeBlur {
  4. public static function run(vert: NodeShader, frag: NodeShader) {
  5. frag.write('vec2 gbufferSizeLocal = gbufferSize;'); // TODO: spirv workaround
  6. #if (kha_direct3d11 || kha_direct3d12 || kha_metal || kha_vulkan)
  7. frag.write('vec2 texCoordInp = texelFetch(gbuffer2, ivec2(sp.x * gbufferSizeLocal.x, sp.y * gbufferSizeLocal.y), 0).ba;');
  8. #else
  9. frag.write('vec2 texCoordInp = texelFetch(gbuffer2, ivec2(sp.x * gbufferSizeLocal.x, (1.0 - sp.y) * gbufferSizeLocal.y), 0).ba;');
  10. #end
  11. frag.write('vec3 basecol = vec3(0.0, 0.0, 0.0);');
  12. frag.write('float roughness = 0.0;');
  13. frag.write('float metallic = 0.0;');
  14. frag.write('float occlusion = 0.0;');
  15. frag.write('vec3 nortan = vec3(0.0, 0.0, 0.0);');
  16. frag.write('float height = 0.0;');
  17. frag.write('float mat_opacity = 1.0;');
  18. frag.write('float opacity = 0.0;');
  19. if (Context.material.paintEmis) {
  20. frag.write('float emis = 0.0;');
  21. }
  22. if (Context.material.paintSubs) {
  23. frag.write('float subs = 0.0;');
  24. }
  25. frag.add_uniform('vec2 texpaintSize', '_texpaintSize');
  26. frag.write('vec2 texpaintSizeLocal = texpaintSize;'); // TODO: spirv workaround
  27. frag.write('float blur_step = 1.0 / texpaintSizeLocal.x;');
  28. if (Context.blurDirectional) {
  29. #if (kha_direct3d11 || kha_direct3d12 || kha_metal)
  30. frag.write('const float blur_weight[7] = {1.0 / 28.0, 2.0 / 28.0, 3.0 / 28.0, 4.0 / 28.0, 5.0 / 28.0, 6.0 / 28.0, 7.0 / 28.0};');
  31. #else
  32. frag.write('const float blur_weight[7] = float[](1.0 / 28.0, 2.0 / 28.0, 3.0 / 28.0, 4.0 / 28.0, 5.0 / 28.0, 6.0 / 28.0, 7.0 / 28.0);');
  33. #end
  34. frag.add_uniform('vec3 brushDirection', '_brushDirection');
  35. frag.write('vec2 blur_direction = brushDirection.yx;');
  36. frag.write('for (int i = 0; i < 7; ++i) {');
  37. #if (kha_direct3d11 || kha_direct3d12 || kha_metal || kha_vulkan)
  38. frag.write('vec2 texCoordInp2 = texelFetch(gbuffer2, ivec2((sp.x + blur_direction.x * blur_step * float(i)) * gbufferSizeLocal.x, (sp.y + blur_direction.y * blur_step * float(i)) * gbufferSizeLocal.y), 0).ba;');
  39. #else
  40. frag.write('vec2 texCoordInp2 = texelFetch(gbuffer2, ivec2((sp.x + blur_direction.x * blur_step * float(i)) * gbufferSizeLocal.x, (1.0 - (sp.y + blur_direction.y * blur_step * float(i))) * gbufferSizeLocal.y), 0).ba;');
  41. #end
  42. frag.write('vec4 texpaint_sample = texture(texpaint_undo, texCoordInp2);');
  43. frag.write('opacity += texpaint_sample.a * blur_weight[i];');
  44. frag.write('basecol += texpaint_sample.rgb * blur_weight[i];');
  45. frag.write('vec4 texpaint_pack_sample = texture(texpaint_pack_undo, texCoordInp2) * blur_weight[i];');
  46. frag.write('roughness += texpaint_pack_sample.g;');
  47. frag.write('metallic += texpaint_pack_sample.b;');
  48. frag.write('occlusion += texpaint_pack_sample.r;');
  49. frag.write('height += texpaint_pack_sample.a;');
  50. frag.write('nortan += texture(texpaint_nor_undo, texCoordInp2).rgb * blur_weight[i];');
  51. frag.write('}');
  52. }
  53. else {
  54. #if (kha_direct3d11 || kha_direct3d12 || kha_metal)
  55. frag.write('const float blur_weight[15] = {0.034619 / 2.0, 0.044859 / 2.0, 0.055857 / 2.0, 0.066833 / 2.0, 0.076841 / 2.0, 0.084894 / 2.0, 0.090126 / 2.0, 0.09194 / 2.0, 0.090126 / 2.0, 0.084894 / 2.0, 0.076841 / 2.0, 0.066833 / 2.0, 0.055857 / 2.0, 0.044859 / 2.0, 0.034619 / 2.0};');
  56. #else
  57. frag.write('const float blur_weight[15] = float[](0.034619 / 2.0, 0.044859 / 2.0, 0.055857 / 2.0, 0.066833 / 2.0, 0.076841 / 2.0, 0.084894 / 2.0, 0.090126 / 2.0, 0.09194 / 2.0, 0.090126 / 2.0, 0.084894 / 2.0, 0.076841 / 2.0, 0.066833 / 2.0, 0.055857 / 2.0, 0.044859 / 2.0, 0.034619 / 2.0);');
  58. #end
  59. // X
  60. frag.write('for (int i = -7; i <= 7; ++i) {');
  61. frag.write('vec4 texpaint_sample = texture(texpaint_undo, texCoordInp + vec2(blur_step * float(i), 0.0));');
  62. frag.write('opacity += texpaint_sample.a * blur_weight[i + 7];');
  63. frag.write('basecol += texpaint_sample.rgb * blur_weight[i + 7];');
  64. frag.write('vec4 texpaint_pack_sample = texture(texpaint_pack_undo, texCoordInp + vec2(blur_step * float(i), 0.0)) * blur_weight[i + 7];');
  65. frag.write('roughness += texpaint_pack_sample.g;');
  66. frag.write('metallic += texpaint_pack_sample.b;');
  67. frag.write('occlusion += texpaint_pack_sample.r;');
  68. frag.write('height += texpaint_pack_sample.a;');
  69. frag.write('nortan += texture(texpaint_nor_undo, texCoordInp + vec2(blur_step * float(i), 0.0)).rgb * blur_weight[i + 7];');
  70. frag.write('}');
  71. // Y
  72. frag.write('for (int j = -7; j <= 7; ++j) {');
  73. frag.write('vec4 texpaint_sample = texture(texpaint_undo, texCoordInp + vec2(0.0, blur_step * float(j)));');
  74. frag.write('opacity += texpaint_sample.a * blur_weight[j + 7];');
  75. frag.write('basecol += texpaint_sample.rgb * blur_weight[j + 7];');
  76. frag.write('vec4 texpaint_pack_sample = texture(texpaint_pack_undo, texCoordInp + vec2(0.0, blur_step * float(j))) * blur_weight[j + 7];');
  77. frag.write('roughness += texpaint_pack_sample.g;');
  78. frag.write('metallic += texpaint_pack_sample.b;');
  79. frag.write('occlusion += texpaint_pack_sample.r;');
  80. frag.write('height += texpaint_pack_sample.a;');
  81. frag.write('nortan += texture(texpaint_nor_undo, texCoordInp + vec2(0.0, blur_step * float(j))).rgb * blur_weight[j + 7];');
  82. frag.write('}');
  83. }
  84. frag.write('opacity *= brushOpacity;');
  85. }
  86. }