MakeTexcoord.hx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package arm.node;
  2. import arm.ui.UISidebar;
  3. import arm.shader.NodeShader;
  4. import arm.Enums;
  5. class MakeTexcoord {
  6. public static function run(vert: NodeShader, frag: NodeShader) {
  7. var fillLayer = Context.layer.fill_layer != null;
  8. var uvType = fillLayer ? Context.layer.uvType : Context.brushPaint;
  9. var decal = Context.tool == ToolDecal || Context.tool == ToolText;
  10. var angle = Context.brushAngle + Context.brushNodesAngle;
  11. var uvAngle = fillLayer ? Context.layer.angle : angle;
  12. if (uvType == UVProject || decal) { // TexCoords - project
  13. frag.add_uniform('float brushScale', '_brushScale');
  14. frag.write_attrib('vec2 uvsp = sp.xy;');
  15. if (fillLayer) { // Decal layer
  16. frag.write_attrib('if (uvsp.x < 0.0 || uvsp.y < 0.0 || uvsp.x > 1.0 || uvsp.y > 1.0) discard;');
  17. if (uvAngle != 0.0) {
  18. frag.add_uniform('vec2 brushAngle', '_brushAngle');
  19. frag.write_attrib('uvsp = vec2(uvsp.x * brushAngle.x - uvsp.y * brushAngle.y, uvsp.x * brushAngle.y + uvsp.y * brushAngle.x);');
  20. }
  21. frag.n = true;
  22. frag.add_uniform('vec3 decalLayerNor', '_decalLayerNor');
  23. var dotAngle = Context.brushAngleRejectDot;
  24. frag.write('if (abs(dot(n, decalLayerNor) - 1.0) > $dotAngle) discard;');
  25. frag.wposition = true;
  26. frag.add_uniform('vec3 decalLayerLoc', '_decalLayerLoc');
  27. frag.add_uniform('float decalLayerDim', '_decalLayerDim');
  28. frag.write_attrib('if (abs(dot(decalLayerNor, decalLayerLoc - wposition)) > decalLayerDim) discard;');
  29. }
  30. else if (decal) {
  31. frag.add_uniform('vec4 decalMask', '_decalMask');
  32. frag.write_attrib('vec4 decalMaskLocal = decalMask;'); // TODO: spirv workaround
  33. frag.write_attrib('uvsp -= decalMaskLocal.xy;');
  34. frag.write_attrib('uvsp.x *= aspectRatio;');
  35. frag.write_attrib('uvsp *= 0.21 / (decalMaskLocal.w * 0.9);'); // Decal radius
  36. if (Context.brushDirectional) {
  37. frag.add_uniform('vec3 brushDirection', '_brushDirection');
  38. frag.write_attrib('if (brushDirection.z == 0.0) discard;');
  39. frag.write_attrib('uvsp = vec2(uvsp.x * brushDirection.x - uvsp.y * brushDirection.y, uvsp.x * brushDirection.y + uvsp.y * brushDirection.x);');
  40. }
  41. if (uvAngle != 0.0) {
  42. frag.add_uniform('vec2 brushAngle', '_brushAngle');
  43. frag.write_attrib('uvsp = vec2(uvsp.x * brushAngle.x - uvsp.y * brushAngle.y, uvsp.x * brushAngle.y + uvsp.y * brushAngle.x);');
  44. }
  45. frag.add_uniform('float brushScaleX', '_brushScaleX');
  46. frag.write_attrib('uvsp.x *= brushScaleX;');
  47. frag.write_attrib('uvsp += vec2(0.5, 0.5);');
  48. frag.write_attrib('if (uvsp.x < 0.0 || uvsp.y < 0.0 || uvsp.x > 1.0 || uvsp.y > 1.0) discard;');
  49. }
  50. else {
  51. frag.write_attrib('uvsp.x *= aspectRatio;');
  52. if (uvAngle != 0.0) {
  53. frag.add_uniform('vec2 brushAngle', '_brushAngle');
  54. frag.write_attrib('uvsp = vec2(uvsp.x * brushAngle.x - uvsp.y * brushAngle.y, uvsp.x * brushAngle.y + uvsp.y * brushAngle.x);');
  55. }
  56. }
  57. frag.write_attrib('vec2 texCoord = uvsp * brushScale;');
  58. }
  59. else if (uvType == UVMap) { // TexCoords - uvmap
  60. vert.add_uniform('float brushScale', '_brushScale');
  61. vert.add_out('vec2 texCoord');
  62. vert.write('texCoord = tex * brushScale;');
  63. if (uvAngle > 0.0) {
  64. vert.add_uniform('vec2 brushAngle', '_brushAngle');
  65. vert.write('texCoord = vec2(texCoord.x * brushAngle.x - texCoord.y * brushAngle.y, texCoord.x * brushAngle.y + texCoord.y * brushAngle.x);');
  66. }
  67. }
  68. else { // TexCoords - triplanar
  69. frag.wposition = true;
  70. frag.n = true;
  71. frag.add_uniform('float brushScale', '_brushScale');
  72. frag.write_attrib('vec3 triWeight = wnormal * wnormal;'); // n * n
  73. frag.write_attrib('float triMax = max(triWeight.x, max(triWeight.y, triWeight.z));');
  74. frag.write_attrib('triWeight = max(triWeight - triMax * 0.75, 0.0);');
  75. frag.write_attrib('vec3 texCoordBlend = triWeight * (1.0 / (triWeight.x + triWeight.y + triWeight.z));');
  76. frag.write_attrib('vec2 texCoord = wposition.yz * brushScale * 0.5;');
  77. frag.write_attrib('vec2 texCoord1 = wposition.xz * brushScale * 0.5;');
  78. frag.write_attrib('vec2 texCoord2 = wposition.xy * brushScale * 0.5;');
  79. if (uvAngle != 0.0) {
  80. frag.add_uniform('vec2 brushAngle', '_brushAngle');
  81. frag.write_attrib('texCoord = vec2(texCoord.x * brushAngle.x - texCoord.y * brushAngle.y, texCoord.x * brushAngle.y + texCoord.y * brushAngle.x);');
  82. frag.write_attrib('texCoord1 = vec2(texCoord1.x * brushAngle.x - texCoord1.y * brushAngle.y, texCoord1.x * brushAngle.y + texCoord1.y * brushAngle.x);');
  83. frag.write_attrib('texCoord2 = vec2(texCoord2.x * brushAngle.x - texCoord2.y * brushAngle.y, texCoord2.x * brushAngle.y + texCoord2.y * brushAngle.x);');
  84. }
  85. }
  86. }
  87. }