make_texcoord.ts 6.0 KB

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