make_bake.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. function make_bake_run(con: node_shader_context_t, kong: node_shader_t) {
  2. if (context_raw.bake_type == bake_type_t.CURVATURE) {
  3. let pass: bool = parser_material_bake_passthrough;
  4. let strength: string = pass ? parser_material_bake_passthrough_strength : context_raw.bake_curv_strength + "";
  5. let radius: string = pass ? parser_material_bake_passthrough_radius : context_raw.bake_curv_radius + "";
  6. let offset: string = pass ? parser_material_bake_passthrough_offset : context_raw.bake_curv_offset + "";
  7. strength = "float(" + strength + ")";
  8. radius = "float(" + radius + ")";
  9. offset = "float(" + offset + ")";
  10. kong.frag_n = true;
  11. node_shader_write_frag(kong, "var dx: float3 = ddx3(n);");
  12. node_shader_write_frag(kong, "var dy: float3 = ddy3(n);");
  13. node_shader_write_frag(kong, "var curvature: float = max(dot(dx, dx), dot(dy, dy));");
  14. node_shader_write_frag(kong, "curvature = clamp(pow(curvature, (1.0 / " + radius + ") * 0.25) * " + strength + " * 2.0 + " + offset + " / 10.0, 0.0, 1.0);");
  15. if (context_raw.bake_axis != bake_axis_t.XYZ) {
  16. let axis: string = make_bake_axis_string(context_raw.bake_axis);
  17. node_shader_write_frag(kong, "curvature *= dot(n, " + axis + ");");
  18. }
  19. node_shader_write_frag(kong, "output[0] = float4(curvature, curvature, curvature, 1.0);");
  20. }
  21. else if (context_raw.bake_type == bake_type_t.NORMAL) { // Tangent
  22. kong.frag_n = true;
  23. node_shader_add_texture(kong, "texpaint_undo", "_texpaint_undo"); // Baked high-poly normals
  24. node_shader_write_frag(kong, "var n0: float3 = sample_lod(texpaint_undo, sampler_linear, tex_coord, 0.0).rgb * float3(2.0, 2.0, 2.0) - float3(1.0, 1.0, 1.0);");
  25. node_shader_add_function(kong, str_cotangent_frame);
  26. node_shader_write_frag(kong, "var invTBN: float3x3 = transpose(cotangent_frame(n, n, tex_coord));");
  27. node_shader_write_frag(kong, "var res: float3 = normalize(invTBN * n0) * float3(0.5, 0.5, 0.5) + float3(0.5, 0.5, 0.5);");
  28. node_shader_write_frag(kong, "output[0] = float4(res, 1.0);");
  29. }
  30. else if (context_raw.bake_type == bake_type_t.NORMAL_OBJECT) {
  31. kong.frag_n = true;
  32. node_shader_write_frag(kong, "output[0] = float4(n * float3(0.5, 0.5, 0.5) + float3(0.5, 0.5, 0.5), 1.0);");
  33. if (context_raw.bake_up_axis == bake_up_axis_t.Y) {
  34. node_shader_write_frag(kong, "output[0].rgb = float3(output[0].r, output[0].b, 1.0 - output[0].g);");
  35. }
  36. }
  37. else if (context_raw.bake_type == bake_type_t.HEIGHT) {
  38. kong.frag_wposition = true;
  39. node_shader_add_texture(kong, "texpaint_undo", "_texpaint_undo"); // Baked high-poly positions
  40. node_shader_write_frag(kong, "var wpos0: float3 = sample_lod(texpaint_undo, sampler_linear, tex_coord, 0.0).rgb * float3(2.0, 2.0, 2.0) - float3(1.0, 1.0, 1.0);");
  41. node_shader_write_frag(kong, "var res: float = distance(wpos0, input.wposition) * 10.0;");
  42. node_shader_write_frag(kong, "output[0] = float4(res, res, res, 1.0);");
  43. }
  44. else if (context_raw.bake_type == bake_type_t.DERIVATIVE) {
  45. node_shader_add_texture(kong, "texpaint_undo", "_texpaint_undo"); // Baked height
  46. node_shader_write_frag(kong, "var tex_dx: float2 = ddx2(tex_coord);");
  47. node_shader_write_frag(kong, "var tex_dy: float2 = ddy2(tex_coord);");
  48. node_shader_write_frag(kong, "var h0: float = sample_lod(texpaint_undo, sampler_linear, tex_coord, 0.0).r * 100.0;");
  49. node_shader_write_frag(kong, "var h1: float = sample_lod(texpaint_undo, sampler_linear, tex_coord + tex_dx, 0.0).r * 100.0;");
  50. node_shader_write_frag(kong, "var h2: float = sample_lod(texpaint_undo, sampler_linear, tex_coord + tex_dy, 0.0).r * 100.0;");
  51. node_shader_write_frag(kong, "output[0] = float4((h1 - h0) * 0.5 + 0.5, (h2 - h0) * 0.5 + 0.5, 0.0, 1.0);");
  52. }
  53. else if (context_raw.bake_type == bake_type_t.POSITION) {
  54. kong.frag_wposition = true;
  55. node_shader_write_frag(kong, "output[0] = float4(input.wposition * float3(0.5, 0.5, 0.5) + float3(0.5, 0.5, 0.5), 1.0);");
  56. if (context_raw.bake_up_axis == bake_up_axis_t.Y) {
  57. node_shader_write_frag(kong, "output[0].rgb = float3(output[0].r, output[0].b, 1.0 - output[0].g);");
  58. }
  59. }
  60. else if (context_raw.bake_type == bake_type_t.TEXCOORD) {
  61. node_shader_write_frag(kong, "output[0] = float4(tex_coord.xy, 0.0, 1.0);");
  62. }
  63. else if (context_raw.bake_type == bake_type_t.MATERIALID) {
  64. node_shader_add_texture(kong, "texpaint_nor_undo", "_texpaint_nor_undo");
  65. node_shader_write_frag(kong, "var sample_matid: float = sample_lod(texpaint_nor_undo, sampler_linear, tex_coord, 0.0).a + 1.0 / 255.0;");
  66. node_shader_write_frag(kong, "var matid_r: float = frac(sin(dot(float2(sample_matid, sample_matid * 20.0), float2(12.9898, 78.233))) * 43758.5453);");
  67. node_shader_write_frag(kong, "var matid_g: float = frac(sin(dot(float2(sample_matid * 20.0, sample_matid), float2(12.9898, 78.233))) * 43758.5453);");
  68. node_shader_write_frag(kong, "var matid_b: float = frac(sin(dot(float2(sample_matid, sample_matid * 40.0), float2(12.9898, 78.233))) * 43758.5453);");
  69. node_shader_write_frag(kong, "output[0] = float4(matid_r, matid_g, matid_b, 1.0);");
  70. }
  71. else if (context_raw.bake_type == bake_type_t.OBJECTID) {
  72. node_shader_add_constant(kong, "object_id: float", "_object_id");
  73. node_shader_write_frag(kong, "var obid: float = constants.object_id + 1.0 / 255.0;");
  74. node_shader_write_frag(kong, "var id_r: float = frac(sin(dot(float2(obid, obid * 20.0), float2(12.9898, 78.233))) * 43758.5453);");
  75. node_shader_write_frag(kong, "var id_g: float = frac(sin(dot(float2(obid * 20.0, obid), float2(12.9898, 78.233))) * 43758.5453);");
  76. node_shader_write_frag(kong, "var id_b: float = frac(sin(dot(float2(obid, obid * 40.0), float2(12.9898, 78.233))) * 43758.5453);");
  77. node_shader_write_frag(kong, "output[0] = float4(id_r, id_g, id_b, 1.0);");
  78. }
  79. else if (context_raw.bake_type == bake_type_t.VERTEX_COLOR) {
  80. if (con.allow_vcols) {
  81. node_shader_context_add_elem(con, "col", "short4norm");
  82. node_shader_write_frag(kong, "output[0] = float4(vcolor.r, vcolor.g, vcolor.b, 1.0);");
  83. }
  84. else {
  85. node_shader_write_frag(kong, "output[0] = float4(1.0, 1.0, 1.0, 1.0);");
  86. }
  87. }
  88. }
  89. function make_bake_position_normal(kong: node_shader_t) {
  90. node_shader_add_out(kong, "position: float3");
  91. node_shader_add_out(kong, "normal: float3");
  92. node_shader_add_constant(kong, "W: float4x4", "_world_matrix");
  93. node_shader_write_vert(kong, "output.position = (constants.W * float4(input.pos.xyz, 1.0)).xyz;");
  94. node_shader_write_vert(kong, "output.normal = float3(input.nor.xy, input.pos.w);");
  95. node_shader_write_vert(kong, "var tpos: float2 = float2(input.tex.x * 2.0 - 1.0, (1.0 - input.tex.y) * 2.0 - 1.0);");
  96. node_shader_write_vert(kong, "output.pos = float4(tpos, 0.0, 1.0);");
  97. kong.frag_out = "float4[2]";
  98. node_shader_write_frag(kong, "output[0] = float4(input.position, 1.0);");
  99. node_shader_write_frag(kong, "output[1] = float4(input.normal, 1.0);");
  100. }
  101. function make_bake_set_color_writes(con_paint: node_shader_context_t) {
  102. // Bake into base color, disable other slots
  103. con_paint.data.color_writes_red[1] = false;
  104. con_paint.data.color_writes_green[1] = false;
  105. con_paint.data.color_writes_blue[1] = false;
  106. con_paint.data.color_writes_alpha[1] = false;
  107. con_paint.data.color_writes_red[2] = false;
  108. con_paint.data.color_writes_green[2] = false;
  109. con_paint.data.color_writes_blue[2] = false;
  110. con_paint.data.color_writes_alpha[2] = false;
  111. }
  112. function make_bake_axis_string(i: i32): string {
  113. return i == bake_axis_t.X ? "float3(1,0,0)" :
  114. i == bake_axis_t.Y ? "float3(0,1,0)" :
  115. i == bake_axis_t.Z ? "float3(0,0,1)" :
  116. i == bake_axis_t.MX ? "float3(-1,0,0)" :
  117. i == bake_axis_t.MY ? "float3(0,-1,0)" :
  118. "float3(0,0,-1)";
  119. }