MakeTexcoord.hx 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package arm.node;
  2. import arm.ui.UITrait;
  3. import arm.node.MaterialShader;
  4. import arm.Tool;
  5. class MakeTexcoord {
  6. public static function run(vert: MaterialShader, frag: MaterialShader) {
  7. var uvType = Context.layer.material_mask != null ? Context.layer.uvType : UITrait.inst.brushPaint;
  8. var decal = Context.tool == ToolDecal || Context.tool == ToolText;
  9. // TexCoords - project
  10. if (uvType == UVProject || decal) {
  11. frag.add_uniform('float brushScale', '_brushScale');
  12. frag.write_attrib('vec2 uvsp = sp.xy;');
  13. if (decal) {
  14. frag.write_attrib('uvsp -= inp.xy;');
  15. frag.write_attrib('uvsp.x *= aspectRatio;');
  16. frag.write_attrib('uvsp *= 0.21 / (brushRadius * 0.9);');
  17. frag.add_uniform('float brushScaleX', '_brushScaleX');
  18. frag.write_attrib('uvsp.x *= brushScaleX;');
  19. frag.write_attrib('uvsp += vec2(0.5, 0.5);');
  20. frag.write_attrib('if (uvsp.x < 0.01 || uvsp.y < 0.01 || uvsp.x > 0.99 || uvsp.y > 0.99) discard;');
  21. }
  22. else {
  23. frag.write_attrib('uvsp.x *= aspectRatio;');
  24. }
  25. frag.write_attrib('vec2 texCoord = fract(uvsp * brushScale);');
  26. var uvRot = Context.layer.material_mask != null ? Context.layer.uvRot : UITrait.inst.brushRot;
  27. if (uvRot > 0.0) {
  28. var a = uvRot * (Math.PI / 180);
  29. frag.write('texCoord = vec2(texCoord.x * ${Math.cos(a)} - texCoord.y * ${Math.sin(a)}, texCoord.x * ${Math.sin(a)} + texCoord.y * ${Math.cos(a)});');
  30. }
  31. }
  32. else if (uvType == UVMap) { // TexCoords - uvmap
  33. vert.add_uniform('float brushScale', '_brushScale');
  34. vert.add_out('vec2 texCoord');
  35. vert.write('texCoord = subtex * brushScale;');
  36. var uvRot = Context.layer.material_mask != null ? Context.layer.uvRot : UITrait.inst.brushRot;
  37. if (uvRot > 0.0) {
  38. var a = uvRot * (Math.PI / 180);
  39. vert.write('texCoord = vec2(texCoord.x * ${Math.cos(a)} - texCoord.y * ${Math.sin(a)}, texCoord.x * ${Math.sin(a)} + texCoord.y * ${Math.cos(a)});');
  40. }
  41. }
  42. else { // Triplanar
  43. frag.wposition = true;
  44. frag.n = true;
  45. frag.add_uniform('float brushScale', '_brushScale');
  46. frag.write_attrib('vec3 triWeight = wnormal * wnormal;'); // n * n
  47. frag.write_attrib('float triMax = max(triWeight.x, max(triWeight.y, triWeight.z));');
  48. frag.write_attrib('triWeight = max(triWeight - triMax * 0.75, 0.0);');
  49. frag.write_attrib('vec3 texCoordBlend = triWeight * (1.0 / (triWeight.x + triWeight.y + triWeight.z));');
  50. frag.write_attrib('vec2 texCoord = fract(wposition.yz * brushScale * 0.5);');
  51. frag.write_attrib('vec2 texCoord1 = fract(wposition.xz * brushScale * 0.5);');
  52. frag.write_attrib('vec2 texCoord2 = fract(wposition.xy * brushScale * 0.5);');
  53. }
  54. }
  55. }