make_texcoord.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. function make_texcoord_run(vert: node_shader_t, frag: node_shader_t) {
  2. let fill_layer: bool = context_raw.layer.fill_layer != null;
  3. let uv_type: uv_type_t = fill_layer ? context_raw.layer.uv_type : context_raw.brush_paint;
  4. let decal: bool = context_raw.tool == workspace_tool_t.DECAL || context_raw.tool == workspace_tool_t.TEXT;
  5. let angle: f32 = context_raw.brush_angle + context_raw.brush_nodes_angle;
  6. let uv_angle: f32 = fill_layer ? context_raw.layer.angle : angle;
  7. if (uv_type == uv_type_t.PROJECT || decal) { // TexCoords - project
  8. node_shader_add_uniform(frag, "float brushScale", "_brushScale");
  9. node_shader_write_attrib(frag, "vec2 uvsp = sp.xy;");
  10. if (fill_layer) { // Decal layer
  11. node_shader_write_attrib(frag, "if (uvsp.x < 0.0 || uvsp.y < 0.0 || uvsp.x > 1.0 || uvsp.y > 1.0) discard;");
  12. if (uv_angle != 0.0) {
  13. node_shader_add_uniform(frag, "vec2 brushAngle", "_brushAngle");
  14. node_shader_write_attrib(frag, "uvsp = vec2(uvsp.x * brushAngle.x - uvsp.y * brushAngle.y, uvsp.x * brushAngle.y + uvsp.y * brushAngle.x);");
  15. }
  16. frag.n = true;
  17. node_shader_add_uniform(frag, "vec3 decalLayerNor", "_decalLayerNor");
  18. let dot_angle: f32 = context_raw.brush_angle_reject_dot;
  19. node_shader_write(frag, "if (abs(dot(n, decalLayerNor) - 1.0) > " + dot_angle + ") discard;");
  20. frag.wposition = true;
  21. node_shader_add_uniform(frag, "vec3 decalLayerLoc", "_decalLayerLoc");
  22. node_shader_add_uniform(frag, "float decalLayerDim", "_decalLayerDim");
  23. node_shader_write_attrib(frag, "if (abs(dot(decalLayerNor, decalLayerLoc - wposition)) > decalLayerDim) discard;");
  24. }
  25. else if (decal) {
  26. node_shader_add_uniform(frag, "vec4 decalMask", "_decalMask");
  27. node_shader_write_attrib(frag, "uvsp -= decalMask.xy;");
  28. node_shader_write_attrib(frag, "uvsp.x *= aspectRatio;");
  29. node_shader_write_attrib(frag, "uvsp *= 0.21 / (decalMask.w * 0.9);"); // Decal radius
  30. if (context_raw.brush_directional) {
  31. node_shader_add_uniform(frag, "vec3 brushDirection", "_brushDirection");
  32. node_shader_write_attrib(frag, "if (brushDirection.z == 0.0) discard;");
  33. node_shader_write_attrib(frag, "uvsp = vec2(uvsp.x * brushDirection.x - uvsp.y * brushDirection.y, uvsp.x * brushDirection.y + uvsp.y * brushDirection.x);");
  34. }
  35. if (uv_angle != 0.0) {
  36. node_shader_add_uniform(frag, "vec2 brushAngle", "_brushAngle");
  37. node_shader_write_attrib(frag, "uvsp = vec2(uvsp.x * brushAngle.x - uvsp.y * brushAngle.y, uvsp.x * brushAngle.y + uvsp.y * brushAngle.x);");
  38. }
  39. node_shader_add_uniform(frag, "float brushScaleX", "_brushScaleX");
  40. node_shader_write_attrib(frag, "uvsp.x *= brushScaleX;");
  41. node_shader_write_attrib(frag, "uvsp += vec2(0.5, 0.5);");
  42. node_shader_write_attrib(frag, "if (uvsp.x < 0.0 || uvsp.y < 0.0 || uvsp.x > 1.0 || uvsp.y > 1.0) discard;");
  43. }
  44. else {
  45. node_shader_write_attrib(frag, "uvsp.x *= aspectRatio;");
  46. if (uv_angle != 0.0) {
  47. node_shader_add_uniform(frag, "vec2 brushAngle", "_brushAngle");
  48. node_shader_write_attrib(frag, "uvsp = vec2(uvsp.x * brushAngle.x - uvsp.y * brushAngle.y, uvsp.x * brushAngle.y + uvsp.y * brushAngle.x);");
  49. }
  50. }
  51. node_shader_write_attrib(frag, "vec2 texCoord = uvsp * brushScale;");
  52. }
  53. else if (uv_type == uv_type_t.UVMAP) { // TexCoords - uvmap
  54. node_shader_add_uniform(vert, "float brushScale", "_brushScale");
  55. node_shader_add_out(vert, "vec2 texCoord");
  56. node_shader_write(vert, "texCoord = tex * brushScale;");
  57. if (uv_angle > 0.0) {
  58. node_shader_add_uniform(vert, "vec2 brushAngle", "_brushAngle");
  59. node_shader_write(vert, "texCoord = vec2(texCoord.x * brushAngle.x - texCoord.y * brushAngle.y, texCoord.x * brushAngle.y + texCoord.y * brushAngle.x);");
  60. }
  61. }
  62. else { // TexCoords - triplanar
  63. frag.wposition = true;
  64. frag.n = true;
  65. node_shader_add_uniform(frag, "float brushScale", "_brushScale");
  66. node_shader_write_attrib(frag, "vec3 triWeight = wnormal * wnormal;"); // n * n
  67. node_shader_write_attrib(frag, "float triMax = max(triWeight.x, max(triWeight.y, triWeight.z));");
  68. node_shader_write_attrib(frag, "triWeight = max(triWeight - triMax * 0.75, 0.0);");
  69. node_shader_write_attrib(frag, "vec3 texCoordBlend = triWeight * (1.0 / (triWeight.x + triWeight.y + triWeight.z));");
  70. node_shader_write_attrib(frag, "vec2 texCoord = wposition.yz * brushScale * 0.5;");
  71. node_shader_write_attrib(frag, "vec2 texCoord1 = wposition.xz * brushScale * 0.5;");
  72. node_shader_write_attrib(frag, "vec2 texCoord2 = wposition.xy * brushScale * 0.5;");
  73. if (uv_angle != 0.0) {
  74. node_shader_add_uniform(frag, "vec2 brushAngle", "_brushAngle");
  75. node_shader_write_attrib(frag, "texCoord = vec2(texCoord.x * brushAngle.x - texCoord.y * brushAngle.y, texCoord.x * brushAngle.y + texCoord.y * brushAngle.x);");
  76. node_shader_write_attrib(frag, "texCoord1 = vec2(texCoord1.x * brushAngle.x - texCoord1.y * brushAngle.y, texCoord1.x * brushAngle.y + texCoord1.y * brushAngle.x);");
  77. node_shader_write_attrib(frag, "texCoord2 = vec2(texCoord2.x * brushAngle.x - texCoord2.y * brushAngle.y, texCoord2.x * brushAngle.y + texCoord2.y * brushAngle.x);");
  78. }
  79. }
  80. }