make_mesh.ts 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. let make_mesh_layer_pass_count: i32 = 1;
  2. function make_mesh_run(data: material_t, layer_pass: i32 = 0): node_shader_context_t {
  3. let context_id: string = layer_pass == 0 ? "mesh" : "mesh" + layer_pass;
  4. let props: shader_context_t = {
  5. name: context_id,
  6. depth_write: layer_pass == 0 ? true : false,
  7. compare_mode: layer_pass == 0 ? "less" : "equal",
  8. cull_mode: (context_raw.cull_backfaces || layer_pass > 0) ? "clockwise" : "none",
  9. vertex_elements: [
  10. {
  11. name: "pos",
  12. data: "short4norm"
  13. },
  14. {
  15. name: "nor",
  16. data: "short2norm"
  17. },
  18. {
  19. name: "tex",
  20. data: "short2norm"
  21. }
  22. ],
  23. color_attachments: [
  24. "RGBA64",
  25. "RGBA64",
  26. "RGBA64"
  27. ],
  28. depth_attachment: "D32"
  29. };
  30. let con_mesh: node_shader_context_t = node_shader_context_create(data, props);
  31. let kong: node_shader_t = node_shader_context_make_kong(con_mesh);
  32. node_shader_add_out(kong, "tex_coord: float2");
  33. kong.frag_wvpposition = true;
  34. node_shader_add_constant(kong, "VP: float4x4", "_view_proj_matrix");
  35. kong.frag_wposition = true;
  36. let texture_count: i32 = 0;
  37. let displace_strength: f32 = make_material_get_displace_strength();
  38. if (make_material_height_used && displace_strength > 0.0) {
  39. kong.vert_n = true;
  40. node_shader_write_vert(kong, "var height: float = 0.0;");
  41. let num_layers: i32 = 0;
  42. for (let i: i32 = 0; i < project_layers.length; ++i) {
  43. let l: slot_layer_t = project_layers[i];
  44. if (!slot_layer_is_visible(l) || !l.paint_height || !slot_layer_is_layer(l)) {
  45. continue;
  46. }
  47. if (num_layers > 16) {
  48. break;
  49. }
  50. num_layers++;
  51. texture_count++;
  52. node_shader_add_texture(kong, "texpaint_pack_vert" + l.id, "_texpaint_pack_vert" + l.id);
  53. node_shader_write_vert(kong, "height += sample_lod(texpaint_pack_vert" + l.id + ", sampler_linear, input.tex, 0.0).a;");
  54. let masks: slot_layer_t[] = slot_layer_get_masks(l);
  55. if (masks != null) {
  56. for (let i: i32 = 0; i < masks.length; ++i) {
  57. let m: slot_layer_t = masks[i];
  58. if (!slot_layer_is_visible(m)) {
  59. continue;
  60. }
  61. texture_count++;
  62. node_shader_add_texture(kong, "texpaint_vert" + m.id, "_texpaint_vert" + m.id);
  63. node_shader_write_vert(kong, "height *= sample_lod(texpaint_vert" + m.id + ", sampler_linear, input.tex, 0.0).r;");
  64. }
  65. }
  66. }
  67. node_shader_write_vert(kong, "output.wposition += wnormal * float3(height, height, height) * float3(" + displace_strength + ", " + displace_strength + ", " + displace_strength + ");");
  68. }
  69. node_shader_write_vert(kong, "output.pos = constants.VP * float4(output.wposition.xyz, 1.0);");
  70. node_shader_write_vert(kong, "output.tex_coord = input.tex;");
  71. node_shader_write_attrib_frag(kong, "var tex_coord: float2 = input.tex_coord;");
  72. kong.frag_out = "float4[3]";
  73. kong.frag_n = true;
  74. node_shader_add_function(kong, str_pack_float_int16);
  75. if (context_raw.tool == workspace_tool_t.COLORID) {
  76. texture_count++;
  77. node_shader_add_texture(kong, "texcolorid", "_texcolorid");
  78. node_shader_write_frag(kong, "output[0] = float4(n.xy, 1.0, pack_f32_i16(0.0, uint(0)));");
  79. node_shader_write_frag(kong, "var idcol: float3 = pow3(sample_lod(texcolorid, sampler_linear, tex_coord, 0.0).rgb, float3(2.2, 2.2, 2.2));");
  80. node_shader_write_frag(kong, "output[1] = float4(idcol.rgb, 1.0);"); // occ
  81. }
  82. else {
  83. node_shader_add_function(kong, str_octahedron_wrap);
  84. node_shader_add_function(kong, str_cotangent_frame);
  85. if (layer_pass > 0) {
  86. node_shader_add_texture(kong, "gbuffer0");
  87. node_shader_add_texture(kong, "gbuffer1");
  88. node_shader_add_texture(kong, "gbuffer2");
  89. node_shader_write_frag(kong, "var fragcoord: float2 = float2(input.wvpposition.x / input.wvpposition.w, input.wvpposition.y / input.wvpposition.w) * 0.5 + 0.5;");
  90. node_shader_write_frag(kong, "fragcoord.y = 1.0 - fragcoord.y;");
  91. node_shader_write_frag(kong, "var gbuffer0_sample: float4 = sample_lod(gbuffer0, sampler_linear, fragcoord, 0.0);");
  92. node_shader_write_frag(kong, "var gbuffer1_sample: float4 = sample_lod(gbuffer1, sampler_linear, fragcoord, 0.0);");
  93. node_shader_write_frag(kong, "var gbuffer2_sample: float4 = sample_lod(gbuffer2, sampler_linear, fragcoord, 0.0);");
  94. node_shader_write_frag(kong, "var basecol: float3 = gbuffer0_sample.rgb;");
  95. node_shader_write_frag(kong, "var roughness: float = gbuffer2_sample.g;");
  96. node_shader_write_frag(kong, "var metallic: float = gbuffer2_sample.b;");
  97. node_shader_write_frag(kong, "var occlusion: float = gbuffer2_sample.r;");
  98. node_shader_write_frag(kong, "var opacity: float = 1.0;//gbuffer0_sample.a;");
  99. node_shader_write_frag(kong, "var matid: float = gbuffer1_sample.a;");
  100. node_shader_write_frag(kong, "var ntex: float3 = gbuffer1_sample.rgb;");
  101. node_shader_write_frag(kong, "var height: float = gbuffer2_sample.a;");
  102. }
  103. else {
  104. node_shader_write_frag(kong, "var basecol: float3 = float3(0.0, 0.0, 0.0);");
  105. node_shader_write_frag(kong, "var roughness: float = 0.0;");
  106. node_shader_write_frag(kong, "var metallic: float = 0.0;");
  107. node_shader_write_frag(kong, "var occlusion: float = 1.0;");
  108. node_shader_write_frag(kong, "var opacity: float = 1.0;");
  109. node_shader_write_frag(kong, "var matid: float = 0.0;");
  110. node_shader_write_frag(kong, "var ntex: float3 = float3(0.5, 0.5, 1.0);");
  111. node_shader_write_frag(kong, "var height: float = 0.0;");
  112. }
  113. node_shader_write_frag(kong, "var texpaint_sample: float4 = float4(0.0, 0.0, 0.0, 1.0);");
  114. node_shader_write_frag(kong, "var texpaint_nor_sample: float4;");
  115. node_shader_write_frag(kong, "var texpaint_pack_sample: float4;");
  116. node_shader_write_frag(kong, "var texpaint_opac: float;");
  117. if (make_material_height_used) {
  118. node_shader_write_frag(kong, "var height0: float = 0.0;");
  119. node_shader_write_frag(kong, "var height1: float = 0.0;");
  120. node_shader_write_frag(kong, "var height2: float = 0.0;");
  121. node_shader_write_frag(kong, "var height3: float = 0.0;");
  122. }
  123. if (context_raw.draw_wireframe) {
  124. texture_count++;
  125. node_shader_add_texture(kong, "texuvmap", "_texuvmap");
  126. }
  127. if (context_raw.viewport_mode == viewport_mode_t.MASK && slot_layer_get_masks(context_raw.layer) != null) {
  128. for (let i: i32 = 0; i < slot_layer_get_masks(context_raw.layer).length; ++i) {
  129. let m: slot_layer_t = slot_layer_get_masks(context_raw.layer)[i];
  130. if (!slot_layer_is_visible(m)) {
  131. continue;
  132. }
  133. texture_count++;
  134. let index: i32 = array_index_of(project_layers, m);
  135. node_shader_add_texture(kong, "texpaint_view_mask" + m.id, "_texpaint" + index);
  136. }
  137. }
  138. if (context_raw.viewport_mode == viewport_mode_t.LIT && context_raw.render_mode == render_mode_t.FORWARD) {
  139. texture_count += 7;
  140. node_shader_add_texture(kong, "senvmap_brdf", "$brdf.k");
  141. node_shader_add_texture(kong, "senvmap_radiance", "_envmap_radiance");
  142. node_shader_add_texture(kong, "senvmap_radiance0", "_envmap_radiance0");
  143. node_shader_add_texture(kong, "senvmap_radiance1", "_envmap_radiance1");
  144. node_shader_add_texture(kong, "senvmap_radiance2", "_envmap_radiance2");
  145. node_shader_add_texture(kong, "senvmap_radiance3", "_envmap_radiance3");
  146. node_shader_add_texture(kong, "senvmap_radiance4", "_envmap_radiance4");
  147. }
  148. // Get layers for this pass
  149. make_mesh_layer_pass_count = 1;
  150. let layers: slot_layer_t[] = [];
  151. let start_count: i32 = texture_count;
  152. let is_material_tool: bool = context_raw.tool == workspace_tool_t.MATERIAL;
  153. for (let i: i32 = 0; i < project_layers.length; ++i) {
  154. let l: slot_layer_t = project_layers[i];
  155. if (is_material_tool && l != context_raw.layer) {
  156. continue;
  157. }
  158. if (!slot_layer_is_layer(l) || !slot_layer_is_visible(l)) {
  159. continue;
  160. }
  161. let count: i32 = 3;
  162. let masks: slot_layer_t[] = slot_layer_get_masks(l);
  163. if (masks != null) {
  164. count += masks.length;
  165. }
  166. texture_count += count;
  167. if (texture_count >= GPU_MAX_TEXTURES - 3) {
  168. texture_count = start_count + count + 3; // gbuffer0_copy, gbuffer1_copy, gbuffer2_copy
  169. make_mesh_layer_pass_count++;
  170. }
  171. if (layer_pass == make_mesh_layer_pass_count - 1) {
  172. array_push(layers, l);
  173. }
  174. }
  175. let last_pass: bool = layer_pass == make_mesh_layer_pass_count - 1;
  176. for (let i: i32 = 0; i < layers.length; ++i) {
  177. let l: slot_layer_t = layers[i];
  178. if (slot_layer_get_object_mask(l) > 0) {
  179. node_shader_add_constant(kong, "uid: int", "_uid");
  180. if (slot_layer_get_object_mask(l) > project_paint_objects.length) { // Atlas
  181. let visibles: mesh_object_t[] = project_get_atlas_objects(slot_layer_get_object_mask(l));
  182. node_shader_write_frag(kong, "if (");
  183. for (let i: i32 = 0; i < visibles.length; ++i) {
  184. if (i > 0) {
  185. node_shader_write_frag(kong, " || ");
  186. }
  187. let uid: i32 = visibles[i].base.uid;
  188. node_shader_write_frag(kong, uid + " == constants.uid");
  189. }
  190. node_shader_write_frag(kong, ") {");
  191. }
  192. else { // Object mask
  193. let uid: i32 = project_paint_objects[slot_layer_get_object_mask(l) - 1].base.uid;
  194. node_shader_write_frag(kong, "if (" + uid + " == constants.uid) {");
  195. }
  196. }
  197. node_shader_add_texture(kong, "texpaint" + l.id);
  198. node_shader_write_frag(kong, "texpaint_sample = sample_lod(texpaint" + l.id + ", sampler_linear, tex_coord, 0.0);");
  199. node_shader_write_frag(kong, "texpaint_opac = texpaint_sample.a;");
  200. // ///if (arm_direct3d12 || arm_vulkan)
  201. // if (raw.viewport_mode == viewport_mode_t.LIT) {
  202. // write_frag(kong, "if (texpaint_opac < 0.1) { discard; }");
  203. // }
  204. // ///end
  205. let masks: slot_layer_t[] = slot_layer_get_masks(l);
  206. if (masks != null) {
  207. let has_visible: bool = false;
  208. for (let i: i32 = 0; i < masks.length; ++i) {
  209. let m: slot_layer_t = masks[i];
  210. if (slot_layer_is_visible(m)) {
  211. has_visible = true;
  212. break;
  213. }
  214. }
  215. if (has_visible) {
  216. let texpaint_mask: string = "texpaint_mask" + l.id;
  217. node_shader_write_frag(kong, "var " + texpaint_mask + ": float = 0.0;");
  218. for (let i: i32 = 0; i < masks.length; ++i) {
  219. let m: slot_layer_t = masks[i];
  220. if (!slot_layer_is_visible(m)) {
  221. continue;
  222. }
  223. node_shader_add_texture(kong, "texpaint" + m.id);
  224. node_shader_write_frag(kong, "{"); // Group mask is sampled across multiple layers
  225. node_shader_write_frag(kong, "var texpaint_mask_sample" + m.id + ": float = sample_lod(texpaint" + m.id + ", sampler_linear, tex_coord, 0.0).r;");
  226. let opac: f32 = slot_layer_get_opacity(m);
  227. let mask: string = make_material_blend_mode_mask(m.blending, texpaint_mask, "texpaint_mask_sample" + m.id, "float(" + opac + ")");
  228. node_shader_write_frag(kong, texpaint_mask + " = " + mask + ";");
  229. node_shader_write_frag(kong, "}");
  230. }
  231. node_shader_write_frag(kong, "texpaint_opac *= clamp(" + texpaint_mask + ", 0.0, 1.0);");
  232. }
  233. }
  234. if (slot_layer_get_opacity(l) < 1) {
  235. let opac: f32 = slot_layer_get_opacity(l);
  236. node_shader_write_frag(kong, "texpaint_opac *= " + opac + ";");
  237. }
  238. if (l.paint_base) {
  239. if (l == project_layers[0]) {
  240. node_shader_write_frag(kong, "basecol = texpaint_sample.rgb * texpaint_opac;");
  241. }
  242. else {
  243. node_shader_write_frag(kong, "basecol = " + make_material_blend_mode(kong, l.blending, "basecol", "texpaint_sample.rgb", "texpaint_opac") + ";");
  244. }
  245. }
  246. if (l.paint_nor || make_material_emis_used) {
  247. node_shader_add_texture(kong, "texpaint_nor" + l.id);
  248. node_shader_write_frag(kong, "texpaint_nor_sample = sample_lod(texpaint_nor" + l.id + ", sampler_linear, tex_coord, 0.0);");
  249. if (make_material_emis_used) {
  250. node_shader_write_frag(kong, "if (texpaint_opac > 0.0) {");
  251. node_shader_add_constant(kong, "texpaint_size: float2", "_texpaint_size");
  252. node_shader_write_frag(kong, " var texpaint_nor_raw: float4 = texpaint_nor" + l.id + "[uint2(uint(tex_coord.x * constants.texpaint_size.x), uint(tex_coord.y * constants.texpaint_size.y))];");
  253. node_shader_write_frag(kong, " matid = texpaint_nor_raw.a;");
  254. node_shader_write_frag(kong, "}");
  255. }
  256. if (l.paint_nor) {
  257. if (l.paint_nor_blend) {
  258. // Whiteout blend
  259. node_shader_write_frag(kong, "{");
  260. node_shader_write_frag(kong, "var n1: float3 = ntex * float3(2.0, 2.0, 2.0) - float3(1.0, 1.0, 1.0);");
  261. node_shader_write_frag(kong, "var n2: float3 = lerp3(float3(0.5, 0.5, 1.0), texpaint_nor_sample.rgb, texpaint_opac) * float3(2.0, 2.0, 2.0) - float3(1.0, 1.0, 1.0);");
  262. node_shader_write_frag(kong, "ntex = normalize(float3(n1.xy + n2.xy, n1.z * n2.z)) * float3(0.5, 0.5, 0.5) + float3(0.5, 0.5, 0.5);");
  263. node_shader_write_frag(kong, "}");
  264. }
  265. else {
  266. node_shader_write_frag(kong, "ntex = lerp3(ntex, texpaint_nor_sample.rgb, texpaint_opac);");
  267. }
  268. }
  269. }
  270. if (l.paint_occ || l.paint_rough || l.paint_met || (l.paint_height && make_material_height_used)) {
  271. node_shader_add_texture(kong, "texpaint_pack" + l.id);
  272. node_shader_write_frag(kong, "texpaint_pack_sample = sample_lod(texpaint_pack" + l.id + ", sampler_linear, tex_coord, 0.0);");
  273. if (l.paint_occ) {
  274. node_shader_write_frag(kong, "occlusion = lerp(occlusion, texpaint_pack_sample.r, texpaint_opac);");
  275. }
  276. if (l.paint_rough) {
  277. node_shader_write_frag(kong, "roughness = lerp(roughness, texpaint_pack_sample.g, texpaint_opac);");
  278. }
  279. if (l.paint_met) {
  280. node_shader_write_frag(kong, "metallic = lerp(metallic, texpaint_pack_sample.b, texpaint_opac);");
  281. }
  282. if (l.paint_height && make_material_height_used) {
  283. let assign: string = l.paint_height_blend ? "+=" : "=";
  284. node_shader_write_frag(kong, "height " + assign + " texpaint_pack_sample.a * texpaint_opac;");
  285. node_shader_write_frag(kong, "{");
  286. node_shader_add_constant(kong, "texpaint_size: float2", "_texpaint_size");
  287. node_shader_write_frag(kong, "var tex_step: float = 1.0 / constants.texpaint_size.x;");
  288. node_shader_write_frag(kong, "height0 " + assign + " sample_lod(texpaint_pack" + l.id + ", sampler_linear, float2(tex_coord.x - tex_step, tex_coord.y), 0.0).a * texpaint_opac;");
  289. node_shader_write_frag(kong, "height1 " + assign + " sample_lod(texpaint_pack" + l.id + ", sampler_linear, float2(tex_coord.x + tex_step, tex_coord.y), 0.0).a * texpaint_opac;");
  290. node_shader_write_frag(kong, "height2 " + assign + " sample_lod(texpaint_pack" + l.id + ", sampler_linear, float2(tex_coord.x, tex_coord.y - tex_step), 0.0).a * texpaint_opac;");
  291. node_shader_write_frag(kong, "height3 " + assign + " sample_lod(texpaint_pack" + l.id + ", sampler_linear, float2(tex_coord.x, tex_coord.y + tex_step), 0.0).a * texpaint_opac;");
  292. node_shader_write_frag(kong, "}");
  293. }
  294. }
  295. if (slot_layer_get_object_mask(l) > 0) {
  296. node_shader_write_frag(kong, "}");
  297. }
  298. }
  299. if (last_pass && context_raw.draw_texels) {
  300. node_shader_add_constant(kong, "texpaint_size: float2", "_texpaint_size");
  301. node_shader_write_frag(kong, "var texel0: float2 = tex_coord * constants.texpaint_size * 0.01;");
  302. node_shader_write_frag(kong, "var texel1: float2 = tex_coord * constants.texpaint_size * 0.1;");
  303. node_shader_write_frag(kong, "var texel2: float2 = tex_coord * constants.texpaint_size;");
  304. // node_shader_write_frag(kong, "basecol = basecol * max(float((int(texel0.x) % 2.0) == (int(texel0.y) % 2.0)), 0.9);");
  305. // node_shader_write_frag(kong, "basecol = basecol * max(float((int(texel1.x) % 2.0) == (int(texel1.y) % 2.0)), 0.9);");
  306. // node_shader_write_frag(kong, "basecol = basecol * max(float((int(texel2.x) % 2.0) == (int(texel2.y) % 2.0)), 0.9);");
  307. node_shader_write_frag(kong, "var texel0xmod: float = float(int(texel0.x)) % 2.0;");
  308. node_shader_write_frag(kong, "var texel0ymod: float = float(int(texel0.y)) % 2.0;");
  309. node_shader_write_frag(kong, "var texel1xmod: float = float(int(texel1.x)) % 2.0;");
  310. node_shader_write_frag(kong, "var texel1ymod: float = float(int(texel1.y)) % 2.0;");
  311. node_shader_write_frag(kong, "var texel2xmod: float = float(int(texel2.x)) % 2.0;");
  312. node_shader_write_frag(kong, "var texel2ymod: float = float(int(texel2.y)) % 2.0;");
  313. node_shader_write_frag(kong, "if (texel0xmod == texel0ymod) { basecol = basecol * 0.9; }");
  314. node_shader_write_frag(kong, "if (texel1xmod == texel1ymod) { basecol = basecol * 0.9; }");
  315. node_shader_write_frag(kong, "if (texel2xmod == texel2ymod) { basecol = basecol * 0.9; }");
  316. }
  317. if (last_pass && context_raw.draw_wireframe) {
  318. node_shader_write_frag(kong, "var wireframe: float = sample_lod(texuvmap, sampler_linear, tex_coord, 0.0).a;");
  319. node_shader_write_frag(kong, "basecol = basecol * (1.0 - wireframe * 0.25);");
  320. node_shader_write_frag(kong, "roughness = max(roughness, wireframe);");
  321. }
  322. if (make_material_height_used) {
  323. node_shader_write_frag(kong, "if (height > 0.0) {");
  324. node_shader_write_frag(kong, "var height_dx: float = height0 - height1;");
  325. node_shader_write_frag(kong, "var height_dy: float = height2 - height3;");
  326. // Whiteout blend
  327. node_shader_write_frag(kong, "var n1: float3 = ntex * float3(2.0, 2.0, 2.0) - float3(1.0, 1.0, 1.0);");
  328. node_shader_write_frag(kong, "var n2: float3 = normalize(float3(height_dx * 16.0, height_dy * 16.0, 1.0));");
  329. node_shader_write_frag(kong, "ntex = normalize(float3(n1.xy + n2.xy, n1.z * n2.z)) * float3(0.5, 0.5, 0.5) + float3(0.5, 0.5, 0.5);");
  330. node_shader_write_frag(kong, "}");
  331. }
  332. if (!last_pass) {
  333. node_shader_write_frag(kong, "output[0] = float4(basecol, opacity);");
  334. node_shader_write_frag(kong, "output[1] = float4(ntex, matid);");
  335. node_shader_write_frag(kong, "output[2] = float4(occlusion, roughness, metallic, height);");
  336. parser_material_finalize(con_mesh);
  337. con_mesh.data.shader_from_source = true;
  338. gpu_create_shaders_from_kong(node_shader_get(kong), ADDRESS(con_mesh.data.vertex_shader), ADDRESS(con_mesh.data.fragment_shader), ADDRESS(con_mesh.data._.vertex_shader_size), ADDRESS(con_mesh.data._.fragment_shader_size));
  339. return con_mesh;
  340. }
  341. kong.frag_vvec = true;
  342. node_shader_write_frag(kong, "var TBN: float3x3 = cotangent_frame(n, vvec, tex_coord);");
  343. node_shader_write_frag(kong, "n = ntex * float3(2.0, 2.0, 2.0) - float3(1.0, 1.0, 1.0);");
  344. node_shader_write_frag(kong, "n.y = -n.y;");
  345. node_shader_write_frag(kong, "n = normalize(TBN * n);");
  346. if (context_raw.viewport_mode == viewport_mode_t.LIT || context_raw.viewport_mode == viewport_mode_t.PATH_TRACE) {
  347. node_shader_write_frag(kong, "basecol = pow3(basecol, float3(2.2, 2.2, 2.2));");
  348. if (context_raw.viewport_shader != null) {
  349. node_shader_write_frag(kong, "var output_color: float3;");
  350. js_call_ptr(context_raw.viewport_shader, kong);
  351. node_shader_write_frag(kong, "output[1] = float4(output_color, 1.0);");
  352. }
  353. else if (context_raw.render_mode == render_mode_t.FORWARD && context_raw.viewport_mode != viewport_mode_t.PATH_TRACE) {
  354. node_shader_write_frag(kong, "var albedo: float3 = lerp3(basecol, float3(0.0, 0.0, 0.0), metallic);");
  355. node_shader_write_frag(kong, "var f0: float3 = lerp3(float3(0.04, 0.04, 0.04), basecol, metallic);");
  356. kong.frag_vvec = true;
  357. node_shader_write_frag(kong, "var dotnv: float = max(0.0, dot(n, vvec));");
  358. // node_shader_write_frag(kong, "var env_brdf: float2 = senvmap_brdf[uint2(float2(roughness, 1.0 - dotnv) * 256.0)].xy;");
  359. node_shader_write_frag(kong, "var brdf_coord: float2 = float2(roughness, 1.0 - dotnv) * 256.0;");
  360. node_shader_write_frag(kong, "var env_brdf: float4 = senvmap_brdf[uint2(uint(brdf_coord.x), uint(brdf_coord.y))];");
  361. // node_shader_add_constant(kong, "envmap_num_mipmaps: int", "_envmap_num_mipmaps");
  362. node_shader_add_constant(kong, "envmap_data: float4", "_envmap_data"); // angle, sin(angle), cos(angle), strength
  363. node_shader_write_frag(kong, "var wreflect: float3 = reflect(-vvec, n);");
  364. node_shader_add_function(kong, str_envmap_equirect);
  365. // node_shader_write_frag(kong, "var envlod: float = roughness * float(constants.envmap_num_mipmaps);");
  366. // node_shader_write_frag(kong, "var prefiltered_color: float3 = sample_lod(senvmap_radiance, sampler_linear, envmap_equirect(wreflect, constants.envmap_data.x), envlod).rgb;");
  367. node_shader_add_function(kong, str_envmap_sample);
  368. node_shader_write_frag(kong, "var envlod: float = roughness * 5.0;");
  369. node_shader_write_frag(kong, "var lod0: float = floor(envlod);");
  370. node_shader_write_frag(kong, "var lod1: float = ceil(envlod);");
  371. node_shader_write_frag(kong, "var lodf: float = envlod - lod0;");
  372. node_shader_write_frag(kong, "var envmap_coord: float2 = envmap_equirect(wreflect, constants.envmap_data.x);");
  373. node_shader_write_frag(kong, "var lodc0: float3 = envmap_sample(lod0, envmap_coord);");
  374. node_shader_write_frag(kong, "var lodc1: float3 = envmap_sample(lod1, envmap_coord);");
  375. node_shader_write_frag(kong, "var prefiltered_color: float3 = lerp3(lodc0, lodc1, lodf);");
  376. // node_shader_add_constant(kong, "shirr: float4[7]", "_envmap_irradiance");
  377. node_shader_add_constant(kong, "shirr0: float4", "_envmap_irradiance0");
  378. node_shader_add_constant(kong, "shirr1: float4", "_envmap_irradiance1");
  379. node_shader_add_constant(kong, "shirr2: float4", "_envmap_irradiance2");
  380. node_shader_add_constant(kong, "shirr3: float4", "_envmap_irradiance3");
  381. node_shader_add_constant(kong, "shirr4: float4", "_envmap_irradiance4");
  382. node_shader_add_constant(kong, "shirr5: float4", "_envmap_irradiance5");
  383. node_shader_add_constant(kong, "shirr6: float4", "_envmap_irradiance6");
  384. node_shader_add_function(kong, str_sh_irradiance);
  385. node_shader_write_frag(kong, "var indirect: float3 = albedo * (sh_irradiance(float3(n.x * constants.envmap_data.z - n.y * constants.envmap_data.y, n.x * constants.envmap_data.y + n.y * constants.envmap_data.z, n.z)) / 3.14159265);");
  386. node_shader_write_frag(kong, "indirect = indirect + (prefiltered_color * (f0 * env_brdf.x + env_brdf.y) * 1.5);");
  387. node_shader_write_frag(kong, "indirect = indirect * constants.envmap_data.w * occlusion;");
  388. node_shader_write_frag(kong, "output[1] = float4(indirect, 1.0);");
  389. }
  390. else { // Deferred, Pathtraced
  391. if (make_material_emis_used) {
  392. node_shader_write_frag(kong, "if (float(int(matid * 255.0)) % float(3) == 1.0) { basecol = basecol * 10.0; }"); // Boost for bloom
  393. }
  394. node_shader_write_frag(kong, "output[1] = float4(basecol, occlusion);");
  395. }
  396. }
  397. else if (context_raw.viewport_mode == viewport_mode_t.BASE_COLOR && context_raw.layer.paint_base) {
  398. node_shader_write_frag(kong, "output[1] = float4(basecol, 1.0);");
  399. }
  400. else if (context_raw.viewport_mode == viewport_mode_t.NORMAL_MAP && context_raw.layer.paint_nor) {
  401. node_shader_write_frag(kong, "output[1] = float4(ntex.rgb, 1.0);");
  402. }
  403. else if (context_raw.viewport_mode == viewport_mode_t.OCCLUSION && context_raw.layer.paint_occ) {
  404. node_shader_write_frag(kong, "output[1] = float4(float3(occlusion, occlusion, occlusion), 1.0);");
  405. }
  406. else if (context_raw.viewport_mode == viewport_mode_t.ROUGHNESS && context_raw.layer.paint_rough) {
  407. node_shader_write_frag(kong, "output[1] = float4(float3(roughness, roughness, roughness), 1.0);");
  408. }
  409. else if (context_raw.viewport_mode == viewport_mode_t.METALLIC && context_raw.layer.paint_met) {
  410. node_shader_write_frag(kong, "output[1] = float4(float3(metallic, metallic, metallic), 1.0);");
  411. }
  412. else if (context_raw.viewport_mode == viewport_mode_t.OPACITY && context_raw.layer.paint_opac) {
  413. node_shader_write_frag(kong, "output[1] = float4(float3(texpaint_sample.a, texpaint_sample.a, texpaint_sample.a), 1.0);");
  414. }
  415. else if (context_raw.viewport_mode == viewport_mode_t.HEIGHT && context_raw.layer.paint_height) {
  416. node_shader_write_frag(kong, "output[1] = float4(float3(height, height, height), 1.0);");
  417. }
  418. else if (context_raw.viewport_mode == viewport_mode_t.EMISSION) {
  419. node_shader_write_frag(kong, "var matid_mod: float = float(int(matid * 255.0)) % float(3);");
  420. node_shader_write_frag(kong, "var emis: float = 0.0; if (matid_mod == 1.0) { emis = 1.0; }");
  421. node_shader_write_frag(kong, "output[1] = float4(float3(emis, emis, emis), 1.0);");
  422. }
  423. else if (context_raw.viewport_mode == viewport_mode_t.SUBSURFACE) {
  424. node_shader_write_frag(kong, "var matid_mod: float = float(int(matid * 255.0)) % float(3);");
  425. node_shader_write_frag(kong, "var subs: float = 0.0; if (matid_mod == 2.0) { subs = 1.0; }");
  426. node_shader_write_frag(kong, "output[1] = float4(float3(subs, subs, subs), 1.0);");
  427. }
  428. else if (context_raw.viewport_mode == viewport_mode_t.TEXCOORD) {
  429. node_shader_write_frag(kong, "output[1] = float4(tex_coord, 0.0, 1.0);");
  430. }
  431. else if (context_raw.viewport_mode == viewport_mode_t.OBJECT_NORMAL) {
  432. kong.frag_nattr = true;
  433. node_shader_write_frag(kong, "output[1] = float4(input.nattr, 1.0);");
  434. }
  435. else if (context_raw.viewport_mode == viewport_mode_t.MATERIAL_ID) {
  436. let id: i32 = context_raw.layer.id;
  437. node_shader_add_texture(kong, "texpaint_nor" + id);
  438. node_shader_add_constant(kong, "texpaint_size: float2", "_texpaint_size");
  439. // node_shader_write_frag(kong, "var sample_matid: float = texpaint_nor" + id + "[uint2(tex_coord * constants.texpaint_size)].a + 1.0 / 255.0;");
  440. node_shader_write_frag(kong, "var sample_matid_coord: float2 = tex_coord * constants.texpaint_size;");
  441. node_shader_write_frag(kong, "var sample_matid4: float4 = texpaint_nor" + id + "[uint2(uint(sample_matid_coord.x), uint(sample_matid_coord.y))];");
  442. node_shader_write_frag(kong, "var sample_matid: float = sample_matid4.a + 1.0 / 255.0;");
  443. 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);");
  444. 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);");
  445. 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);");
  446. node_shader_write_frag(kong, "output[1] = float4(matid_r, matid_g, matid_b, 1.0);");
  447. }
  448. else if (context_raw.viewport_mode == viewport_mode_t.OBJECT_ID) {
  449. node_shader_add_constant(kong, "object_id: float", "_object_id");
  450. node_shader_write_frag(kong, "var obid: float = constants.object_id + 1.0 / 255.0;");
  451. node_shader_write_frag(kong, "var id_r: float = frac(sin(dot(float2(obid, obid * 20.0), float2(12.9898, 78.233))) * 43758.5453);");
  452. node_shader_write_frag(kong, "var id_g: float = frac(sin(dot(float2(obid * 20.0, obid), float2(12.9898, 78.233))) * 43758.5453);");
  453. node_shader_write_frag(kong, "var id_b: float = frac(sin(dot(float2(obid, obid * 40.0), float2(12.9898, 78.233))) * 43758.5453);");
  454. node_shader_write_frag(kong, "output[1] = float4(id_r, id_g, id_b, 1.0);");
  455. }
  456. else if (context_raw.viewport_mode == viewport_mode_t.MASK && (slot_layer_get_masks(context_raw.layer) != null || slot_layer_is_mask(context_raw.layer))) {
  457. if (slot_layer_is_mask(context_raw.layer)) {
  458. let id: i32 = context_raw.layer.id;
  459. node_shader_write_frag(kong, "var mask_view: float = sample_lod(texpaint" + id + ", sampler_linear, tex_coord, 0.0).r;");
  460. }
  461. else {
  462. node_shader_write_frag(kong, "var mask_view: float = 0.0;");
  463. for (let i: i32 = 0; i < slot_layer_get_masks(context_raw.layer).length; ++i) {
  464. let m: slot_layer_t = slot_layer_get_masks(context_raw.layer)[i];
  465. if (!slot_layer_is_visible(m)) {
  466. continue;
  467. }
  468. node_shader_write_frag(kong, "var mask_sample" + m.id + ": float = sample_lod(texpaint_view_mask" + m.id + ", sampler_linear, tex_coord, 0.0).r;");
  469. let opac: f32 = slot_layer_get_opacity(m);
  470. let mask: string = make_material_blend_mode_mask(m.blending, "mask_view", "mask_sample" + m.id, "float(" + opac + ")");
  471. node_shader_write_frag(kong, "mask_view = " + mask + ";");
  472. }
  473. }
  474. node_shader_write_frag(kong, "output[1] = float4(mask_view, mask_view, mask_view, 1.0);");
  475. }
  476. else {
  477. node_shader_write_frag(kong, "output[1] = float4(1.0, 0.0, 1.0, 1.0);"); // Pink
  478. }
  479. if (context_raw.viewport_mode != viewport_mode_t.LIT && context_raw.viewport_mode != viewport_mode_t.PATH_TRACE) {
  480. node_shader_write_frag(kong, "output[1].rgb = pow3(output[1].rgb, float3(2.2, 2.2, 2.2));"); ////
  481. }
  482. node_shader_write_frag(kong, "n = n / (abs(n.x) + abs(n.y) + abs(n.z));");
  483. // node_shader_write_frag(kong, "n.xy = n.z >= 0.0 ? n.xy : octahedron_wrap(n.xy);");
  484. node_shader_write_frag(kong, "if (n.z < 0.0) { n.xy = octahedron_wrap(n.xy); }");
  485. node_shader_write_frag(kong, "output[0] = float4(n.xy, roughness, pack_f32_i16(metallic, uint(float(int(matid * 255.0)) % float(3))));");
  486. }
  487. node_shader_write_frag(kong, "output[2] = float4(0.0, 0.0, tex_coord.xy);");
  488. parser_material_finalize(con_mesh);
  489. con_mesh.data.shader_from_source = true;
  490. gpu_create_shaders_from_kong(node_shader_get(kong), ADDRESS(con_mesh.data.vertex_shader), ADDRESS(con_mesh.data.fragment_shader), ADDRESS(con_mesh.data._.vertex_shader_size), ADDRESS(con_mesh.data._.fragment_shader_size));
  491. return con_mesh;
  492. }