make_texcoord.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. function make_texcoord_run(kong: 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_is_decal();
  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_constant(kong, "brush_scale: float", "_brush_scale");
  9. node_shader_write_attrib_frag(kong, "var uvsp: float2 = sp.xy;");
  10. if (fill_layer) { // Decal layer
  11. node_shader_write_attrib_frag(kong, "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_constant(kong, "brush_angle: float2", "_brush_angle");
  14. node_shader_write_attrib_frag(
  15. kong,
  16. "uvsp = float2(uvsp.x * constants.brush_angle.x - uvsp.y * constants.brush_angle.y, uvsp.x * constants.brush_angle.y + uvsp.y * constants.brush_angle.x);");
  17. }
  18. kong.frag_n = true;
  19. node_shader_add_constant(kong, "decal_layer_nor: float3", "_decal_layer_nor");
  20. let dot_angle: f32 = context_raw.brush_angle_reject_dot;
  21. node_shader_write_frag(kong, "if (abs(dot(n, constants.decal_layer_nor) - 1.0) > " + dot_angle + ") { discard; }");
  22. kong.frag_wposition = true;
  23. node_shader_add_constant(kong, "decal_layer_loc: float3", "_decal_layer_loc");
  24. node_shader_add_constant(kong, "decal_layer_dim: float", "_decal_layer_dim");
  25. node_shader_write_attrib_frag(
  26. kong, "if (abs(dot(constants.decal_layer_nor, constants.decal_layer_loc - input.wposition)) > constants.decal_layer_dim) { discard; }");
  27. }
  28. else if (decal) {
  29. node_shader_add_constant(kong, "decal_mask: float4", "_decal_mask");
  30. node_shader_write_attrib_frag(kong, "uvsp = uvsp - constants.decal_mask.xy;");
  31. node_shader_write_attrib_frag(kong, "uvsp.x *= constants.aspect_ratio;");
  32. node_shader_write_attrib_frag(kong, "uvsp = uvsp * (0.21 / (constants.decal_mask.w * 0.9));"); // Decal radius
  33. if (context_raw.brush_directional) {
  34. node_shader_add_constant(kong, "brush_direction: float3", "_brush_direction");
  35. node_shader_write_attrib_frag(kong, "if (constants.brush_direction.z == 0.0) { discard; }");
  36. node_shader_write_attrib_frag(
  37. kong,
  38. "uvsp = float2(uvsp.x * constants.brush_direction.x - uvsp.y * constants.brush_direction.y, uvsp.x * constants.brush_direction.y + uvsp.y * constants.brush_direction.x);");
  39. }
  40. if (uv_angle != 0.0) {
  41. node_shader_add_constant(kong, "brush_angle: float2", "_brush_angle");
  42. node_shader_write_attrib_frag(
  43. kong,
  44. "uvsp = float2(uvsp.x * constants.brush_angle.x - uvsp.y * constants.brush_angle.y, uvsp.x * constants.brush_angle.y + uvsp.y * constants.brush_angle.x);");
  45. }
  46. node_shader_add_constant(kong, "brush_scale_x: float", "_brush_scale_x");
  47. node_shader_write_attrib_frag(kong, "uvsp.x *= constants.brush_scale_x;");
  48. node_shader_write_attrib_frag(kong, "uvsp += float2(0.5, 0.5);");
  49. node_shader_write_attrib_frag(kong, "if (uvsp.x < 0.0 || uvsp.y < 0.0 || uvsp.x > 1.0 || uvsp.y > 1.0) { discard; }");
  50. }
  51. else {
  52. node_shader_write_attrib_frag(kong, "uvsp.x *= constants.aspect_ratio;");
  53. if (uv_angle != 0.0) {
  54. node_shader_add_constant(kong, "brush_angle: float2", "_brush_angle");
  55. node_shader_write_attrib_frag(
  56. kong,
  57. "uvsp = float2(uvsp.x * constants.brush_angle.x - uvsp.y * constants.brush_angle.y, uvsp.x * constants.brush_angle.y + uvsp.y * constants.brush_angle.x);");
  58. }
  59. }
  60. node_shader_write_attrib_frag(kong, "var tex_coord: float2 = uvsp * constants.brush_scale;");
  61. }
  62. else if (uv_type == uv_type_t.UVMAP) { // TexCoords - uvmap
  63. node_shader_add_constant(kong, "brush_scale: float", "_brush_scale");
  64. node_shader_add_out(kong, "tex_coord: float2");
  65. if (context_raw.layer.uv_map == 1) {
  66. node_shader_write_vert(kong, "output.tex_coord = input.tex1 * constants.brush_scale;");
  67. }
  68. else {
  69. node_shader_write_vert(kong, "output.tex_coord = input.tex * constants.brush_scale;");
  70. }
  71. if (uv_angle > 0.0) {
  72. node_shader_add_constant(kong, "brush_angle: float2", "_brush_angle");
  73. node_shader_write_vert(
  74. kong,
  75. "output.tex_coord = float2(output.tex_coord.x * constants.brush_angle.x - output.tex_coord.y * constants.brush_angle.y, output.tex_coord.x * constants.brush_angle.y + output.tex_coord.y * constants.brush_angle.x);");
  76. }
  77. node_shader_write_attrib_frag(kong, "var tex_coord: float2 = input.tex_coord;");
  78. }
  79. else { // TexCoords - triplanar
  80. kong.frag_wposition = true;
  81. kong.frag_n = true;
  82. node_shader_add_constant(kong, "brush_scale: float", "_brush_scale");
  83. node_shader_write_attrib_frag(kong, "var tri_weight: float3 = input.wnormal * input.wnormal;"); // n * n
  84. node_shader_write_attrib_frag(kong, "var tri_max: float = max(tri_weight.x, max(tri_weight.y, tri_weight.z));");
  85. node_shader_write_attrib_frag(kong, "tri_weight = max3(tri_weight - float3(tri_max * 0.75, tri_max * 0.75, tri_max * 0.75), float3(0.0, 0.0, 0.0));");
  86. node_shader_write_attrib_frag(kong, "var tex_coord_blend: float3 = tri_weight * (1.0 / (tri_weight.x + tri_weight.y + tri_weight.z));");
  87. node_shader_write_attrib_frag(kong, "var tex_coord: float2 = input.wposition.yz * constants.brush_scale * 0.5;");
  88. node_shader_write_attrib_frag(kong, "var tex_coord1: float2 = input.wposition.xz * constants.brush_scale * 0.5;");
  89. node_shader_write_attrib_frag(kong, "var tex_coord2: float2 = input.wposition.xy * constants.brush_scale * 0.5;");
  90. if (uv_angle != 0.0) {
  91. node_shader_add_constant(kong, "brush_angle: float2", "_brush_angle");
  92. node_shader_write_attrib_frag(
  93. kong,
  94. "tex_coord = float2(tex_coord.x * constants.brush_angle.x - tex_coord.y * constants.brush_angle.y, tex_coord.x * constants.brush_angle.y + tex_coord.y * constants.brush_angle.x);");
  95. node_shader_write_attrib_frag(
  96. kong,
  97. "tex_coord1 = float2(tex_coord1.x * constants.brush_angle.x - tex_coord1.y * constants.brush_angle.y, tex_coord1.x * constants.brush_angle.y + tex_coord1.y * constants.brush_angle.x);");
  98. node_shader_write_attrib_frag(
  99. kong,
  100. "tex_coord2 = float2(tex_coord2.x * constants.brush_angle.x - tex_coord2.y * constants.brush_angle.y, tex_coord2.x * constants.brush_angle.y + tex_coord2.y * constants.brush_angle.x);");
  101. }
  102. }
  103. }