make_material.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. let make_material_default_scon: shader_context_t = null;
  2. let make_material_default_mcon: material_context_t = null;
  3. let make_material_height_used: bool = false;
  4. let make_material_emis_used: bool = false;
  5. let make_material_subs_used: bool = false;
  6. function make_material_get_mout(): bool {
  7. for (let i: i32 = 0; i < ui_nodes_get_canvas_material().nodes.length; ++i) {
  8. let n: ui_node_t = ui_nodes_get_canvas_material().nodes[i];
  9. if (n.type == "OUTPUT_MATERIAL_PBR") {
  10. return true;
  11. }
  12. }
  13. return false;
  14. }
  15. function make_material_parse_mesh_material() {
  16. let m: material_data_t = project_materials[0].data;
  17. for (let i: i32 = 0; i < m._.shader.contexts.length; ++i) {
  18. let c: shader_context_t = m._.shader.contexts[i];
  19. if (c.name == "mesh") {
  20. array_remove(m._.shader.contexts, c);
  21. make_material_delete_context(c);
  22. break;
  23. }
  24. }
  25. if (make_mesh_layer_pass_count > 1) {
  26. let i: i32 = 0;
  27. while (i < m._.shader.contexts.length) {
  28. let c: shader_context_t = m._.shader.contexts[i];
  29. for (let j: i32 = 1; j < make_mesh_layer_pass_count; ++j) {
  30. let name: string = "mesh" + j;
  31. if (c.name == name) {
  32. array_remove(m._.shader.contexts, c);
  33. make_material_delete_context(c);
  34. i--;
  35. break;
  36. }
  37. }
  38. i++;
  39. }
  40. i = 0;
  41. while (i < m.contexts.length) {
  42. let c: material_context_t = m.contexts[i];
  43. for (let j: i32 = 1; j < make_mesh_layer_pass_count; ++j) {
  44. let name: string = "mesh" + j;
  45. if (c.name == name) {
  46. array_remove(m.contexts, c);
  47. i--;
  48. break;
  49. }
  50. }
  51. i++;
  52. }
  53. }
  54. let mm: material_t = {
  55. name: "Material",
  56. canvas: null
  57. };
  58. let con: node_shader_context_t = make_mesh_run(mm);
  59. shader_context_load(con.data);
  60. array_push(m._.shader.contexts, con.data);
  61. for (let i: i32 = 1; i < make_mesh_layer_pass_count; ++i) {
  62. let mm: material_t = {
  63. name: "Material",
  64. canvas: null
  65. };
  66. let con: node_shader_context_t = make_mesh_run(mm, i);
  67. shader_context_load(con.data);
  68. array_push(m._.shader.contexts, con.data);
  69. let mcon: material_context_t = {
  70. name: "mesh" + i,
  71. bind_textures: []
  72. };
  73. material_context_load(mcon);
  74. array_push(m.contexts, mcon);
  75. }
  76. context_raw.ddirty = 2;
  77. render_path_raytrace_dirty = 1;
  78. }
  79. function make_material_parse_mesh_preview_material(md: material_data_t = null) {
  80. if (!make_material_get_mout()) {
  81. return;
  82. }
  83. let m: material_data_t = md == null ? project_materials[0].data : md;
  84. let scon: shader_context_t = null;
  85. for (let i: i32 = 0; i < m._.shader.contexts.length; ++i) {
  86. let c: shader_context_t = m._.shader.contexts[i];
  87. if (c.name == "mesh") {
  88. scon = c;
  89. break;
  90. }
  91. }
  92. array_remove(m._.shader.contexts, scon);
  93. let mcon: material_context_t = {
  94. name: "mesh",
  95. bind_textures: []
  96. };
  97. let sd: material_t = {
  98. name: "Material",
  99. canvas: null
  100. };
  101. let con: node_shader_context_t = make_mesh_preview_run(sd, mcon);
  102. for (let i: i32 = 0; i < m.contexts.length; ++i) {
  103. if (m.contexts[i].name == "mesh") {
  104. material_context_load(mcon);
  105. m.contexts[i] = mcon;
  106. break;
  107. }
  108. }
  109. if (scon != null) {
  110. make_material_delete_context(scon);
  111. }
  112. let compile_error: bool = false;
  113. shader_context_load(con.data);
  114. if (con.data == null) {
  115. compile_error = true;
  116. }
  117. scon = con.data;
  118. if (compile_error) {
  119. return;
  120. }
  121. array_push(m._.shader.contexts, scon);
  122. }
  123. function make_material_parse_paint_material(bake_previews: bool = true) {
  124. if (!make_material_get_mout()) {
  125. return;
  126. }
  127. if (bake_previews) {
  128. let current: gpu_texture_t = _draw_current;
  129. let in_use: bool = gpu_in_use;
  130. if (in_use) draw_end();
  131. make_material_bake_node_previews();
  132. if (in_use) draw_begin(current);
  133. }
  134. let m: material_data_t = project_materials[0].data;
  135. for (let i: i32 = 0; i < m._.shader.contexts.length; ++i) {
  136. let c: shader_context_t = m._.shader.contexts[i];
  137. if (c.name == "paint") {
  138. array_remove(m._.shader.contexts, c);
  139. if (c != make_material_default_scon) {
  140. make_material_delete_context(c);
  141. }
  142. break;
  143. }
  144. }
  145. for (let i: i32 = 0; i < m.contexts.length; ++i) {
  146. let c: material_context_t = m.contexts[i];
  147. if (c.name == "paint") {
  148. array_remove(m.contexts, c);
  149. break;
  150. }
  151. }
  152. let sdata: material_t = {
  153. name: "Material",
  154. canvas: ui_nodes_get_canvas_material()
  155. };
  156. let tmcon: material_context_t = {
  157. name: "paint",
  158. bind_textures: []
  159. };
  160. let con: node_shader_context_t = make_paint_run(sdata, tmcon);
  161. let compile_error: bool = false;
  162. let scon: shader_context_t;
  163. shader_context_load(con.data);
  164. if (con.data == null) {
  165. compile_error = true;
  166. }
  167. scon = con.data;
  168. if (compile_error) {
  169. return;
  170. }
  171. material_context_load(tmcon);
  172. let mcon: material_context_t = tmcon;
  173. array_push(m._.shader.contexts, scon);
  174. array_push(m.contexts, mcon);
  175. if (make_material_default_scon == null) {
  176. make_material_default_scon = scon;
  177. }
  178. if (make_material_default_mcon == null) {
  179. make_material_default_mcon = mcon;
  180. }
  181. }
  182. function make_material_bake_node_previews() {
  183. context_raw.node_previews_used = [];
  184. if (context_raw.node_previews == null) {
  185. context_raw.node_previews = map_create();
  186. }
  187. let empty: ui_node_t[] = [];
  188. make_material_traverse_nodes(ui_nodes_get_canvas_material().nodes, null, empty);
  189. let keys: string[] = map_keys(context_raw.node_previews);
  190. for (let i: i32 = 0; i < keys.length; ++i) {
  191. let key: string = keys[i];
  192. if (array_index_of(context_raw.node_previews_used, key) == -1) {
  193. let image: gpu_texture_t = map_get(context_raw.node_previews, key);
  194. iron_delete_texture(image);
  195. map_delete(context_raw.node_previews, key);
  196. }
  197. }
  198. }
  199. function make_material_traverse_nodes(nodes: ui_node_t[], group: ui_node_canvas_t, parents: ui_node_t[]) {
  200. for (let i: i32 = 0; i < nodes.length; ++i) {
  201. let node: ui_node_t = nodes[i];
  202. make_material_bake_node_preview(node, group, parents);
  203. if (node.type == "GROUP") {
  204. for (let j: i32 = 0; j < project_material_groups.length; ++j) {
  205. let g: node_group_t = project_material_groups[j];
  206. let cname: string = g.canvas.name;
  207. if (cname == node.name) {
  208. array_push(parents, node);
  209. make_material_traverse_nodes(g.canvas.nodes, g.canvas, parents);
  210. array_pop(parents);
  211. break;
  212. }
  213. }
  214. }
  215. }
  216. }
  217. function make_material_bake_node_preview(node: ui_node_t, group: ui_node_canvas_t, parents: ui_node_t[]) {
  218. if (node.type == "BLUR") {
  219. let id: string = parser_material_node_name(node, parents);
  220. let image: gpu_texture_t = map_get(context_raw.node_previews, id);
  221. array_push(context_raw.node_previews_used, id);
  222. let res_x: i32 = math_floor(config_get_texture_res_x() / 4);
  223. let res_y: i32 = math_floor(config_get_texture_res_y() / 4);
  224. if (image == null || image.width != res_x || image.height != res_y) {
  225. if (image != null) {
  226. iron_delete_texture(image);
  227. }
  228. image = gpu_create_render_target(res_x, res_y);
  229. map_set(context_raw.node_previews, id, image);
  230. }
  231. parser_material_blur_passthrough = true;
  232. util_render_make_node_preview(ui_nodes_get_canvas_material(), node, image, group, parents);
  233. parser_material_blur_passthrough = false;
  234. }
  235. else if (node.type == "DIRECT_WARP") {
  236. let id: string = parser_material_node_name(node, parents);
  237. let image: gpu_texture_t = map_get(context_raw.node_previews, id);
  238. array_push(context_raw.node_previews_used, id);
  239. let res_x: i32 = math_floor(config_get_texture_res_x());
  240. let res_y: i32 = math_floor(config_get_texture_res_y());
  241. if (image == null || image.width != res_x || image.height != res_y) {
  242. if (image != null) {
  243. iron_delete_texture(image);
  244. }
  245. image = gpu_create_render_target(res_x, res_y);
  246. map_set(context_raw.node_previews, id, image);
  247. }
  248. parser_material_warp_passthrough = true;
  249. util_render_make_node_preview(ui_nodes_get_canvas_material(), node, image, group, parents);
  250. parser_material_warp_passthrough = false;
  251. }
  252. else if (node.type == "BAKE_CURVATURE") {
  253. let id: string = parser_material_node_name(node, parents);
  254. let image: gpu_texture_t = map_get(context_raw.node_previews, id);
  255. array_push(context_raw.node_previews_used, id);
  256. let res_x: i32 = math_floor(config_get_texture_res_x());
  257. let res_y: i32 = math_floor(config_get_texture_res_y());
  258. if (image == null || image.width != res_x || image.height != res_y) {
  259. if (image != null) {
  260. iron_delete_texture(image);
  261. }
  262. image = gpu_create_render_target(res_x, res_y, tex_format_t.R8);
  263. map_set(context_raw.node_previews, id, image);
  264. }
  265. if (render_path_paint_live_layer == null) {
  266. render_path_paint_live_layer = slot_layer_create("_live");
  267. }
  268. let _space: i32 = ui_header_worktab.position;
  269. let _tool: tool_type_t = context_raw.tool;
  270. let _bake_type: bake_type_t = context_raw.bake_type;
  271. ui_header_worktab.position = space_type_t.SPACE3D;
  272. context_raw.tool = tool_type_t.BAKE;
  273. context_raw.bake_type = bake_type_t.CURVATURE;
  274. parser_material_bake_passthrough = true;
  275. parser_material_start_node = node;
  276. parser_material_start_group = group;
  277. parser_material_start_parents = parents;
  278. make_material_parse_paint_material(false);
  279. parser_material_bake_passthrough = false;
  280. parser_material_start_node = null;
  281. parser_material_start_group = null;
  282. parser_material_start_parents = null;
  283. context_raw.pdirty = 1;
  284. render_path_paint_use_live_layer(true);
  285. render_path_paint_commands_paint(false);
  286. render_path_paint_dilate(true, false);
  287. render_path_paint_use_live_layer(false);
  288. context_raw.pdirty = 0;
  289. ui_header_worktab.position = _space;
  290. context_raw.tool = _tool;
  291. context_raw.bake_type = _bake_type;
  292. make_material_parse_paint_material(false);
  293. let rts: map_t<string, render_target_t> = render_path_render_targets;
  294. let texpaint_live: render_target_t = map_get(rts, "texpaint_live");
  295. draw_begin(image);
  296. draw_image(texpaint_live._image, 0, 0);
  297. draw_end();
  298. }
  299. }
  300. type parse_node_preview_result_t = {
  301. scon: shader_context_t;
  302. mcon: material_context_t;
  303. };
  304. function make_material_parse_node_preview_material(node: ui_node_t, group: ui_node_canvas_t = null, parents: ui_node_t[] = null): parse_node_preview_result_t {
  305. if (node.outputs.length == 0) {
  306. return null;
  307. }
  308. let sdata: material_t = {
  309. name: "Material",
  310. canvas: ui_nodes_get_canvas_material()
  311. };
  312. let mcon_raw: material_context_t = {
  313. name: "mesh",
  314. bind_textures: []
  315. };
  316. let con: node_shader_context_t = make_node_preview_run(sdata, mcon_raw, node, group, parents);
  317. let compile_error: bool = false;
  318. let scon: shader_context_t;
  319. shader_context_load(con.data);
  320. if (con.data == null) {
  321. compile_error = true;
  322. }
  323. scon = con.data;
  324. if (compile_error) {
  325. return null;
  326. }
  327. material_context_load(mcon_raw);
  328. let mcon: material_context_t = mcon_raw;
  329. let result: parse_node_preview_result_t = {
  330. scon: scon,
  331. mcon: mcon
  332. };
  333. return result;
  334. }
  335. function make_material_parse_brush() {
  336. parser_logic_parse(context_raw.brush.canvas);
  337. }
  338. function make_material_blend_mode(kong: node_shader_t, blending: i32, cola: string, colb: string, opac: string): string {
  339. if (blending == blend_type_t.MIX) {
  340. return "lerp3(" + cola + ", " + colb + ", " + opac + ")";
  341. }
  342. else if (blending == blend_type_t.DARKEN) {
  343. return "lerp3(" + cola + ", min3(" + cola + ", " + colb + "), " + opac + ")";
  344. }
  345. else if (blending == blend_type_t.MULTIPLY) {
  346. return "lerp3(" + cola + ", " + cola + " * " + colb + ", " + opac + ")";
  347. }
  348. else if (blending == blend_type_t.BURN) {
  349. return "lerp3(" + cola + ", float3(1.0, 1.0, 1.0) - (float3(1.0, 1.0, 1.0) - " + cola + ") / " + colb + ", " + opac + ")";
  350. }
  351. else if (blending == blend_type_t.LIGHTEN) {
  352. return "max3(" + cola + ", " + colb + " * " + opac + ")";
  353. }
  354. else if (blending == blend_type_t.SCREEN) {
  355. return "(float3(1.0, 1.0, 1.0) - (float3(1.0 - " + opac + ", 1.0 - " + opac + ", 1.0 - " + opac + ") + " + opac + " * (float3(1.0, 1.0, 1.0) - " + colb + ")) * (float3(1.0, 1.0, 1.0) - " + cola + "))";
  356. }
  357. else if (blending == blend_type_t.DODGE) {
  358. return "lerp3(" + cola + ", " + cola + " / (float3(1.0, 1.0, 1.0) - " + colb + "), " + opac + ")";
  359. }
  360. else if (blending == blend_type_t.ADD) {
  361. return "lerp3(" + cola + ", " + cola + " + " + colb + ", " + opac + ")";
  362. }
  363. else if (blending == blend_type_t.OVERLAY) {
  364. return "lerp3(" + cola + ", float3( \
  365. " + cola + ".r < 0.5 ? 2.0 * " + cola + ".r * " + colb + ".r : 1.0 - 2.0 * (1.0 - " + cola + ".r) * (1.0 - " + colb + ".r), \
  366. " + cola + ".g < 0.5 ? 2.0 * " + cola + ".g * " + colb + ".g : 1.0 - 2.0 * (1.0 - " + cola + ".g) * (1.0 - " + colb + ".g), \
  367. " + cola + ".b < 0.5 ? 2.0 * " + cola + ".b * " + colb + ".b : 1.0 - 2.0 * (1.0 - " + cola + ".b) * (1.0 - " + colb + ".b) \
  368. ), " + opac + ")";
  369. }
  370. else if (blending == blend_type_t.SOFT_LIGHT) {
  371. return "((1.0 - " + opac + ") * " + cola + " + " + opac + " * ((float3(1.0, 1.0, 1.0) - " + cola + ") * " + colb + " * " + cola + " + " + cola + " * (float3(1.0, 1.0, 1.0) - (float3(1.0, 1.0, 1.0) - " + colb + ") * (float3(1.0, 1.0, 1.0) - " + cola + "))))";
  372. }
  373. else if (blending == blend_type_t.LINEAR_LIGHT) {
  374. return "(" + cola + " + " + opac + " * (float3(2.0, 2.0, 2.0) * (" + colb + " - float3(0.5, 0.5, 0.5))))";
  375. }
  376. else if (blending == blend_type_t.DIFFERENCE) {
  377. return "lerp3(" + cola + ", abs3(" + cola + " - " + colb + "), " + opac + ")";
  378. }
  379. else if (blending == blend_type_t.SUBTRACT) {
  380. return "lerp3(" + cola + ", " + cola + " - " + colb + ", " + opac + ")";
  381. }
  382. else if (blending == blend_type_t.DIVIDE) {
  383. return "float3(1.0 - " + opac + ", 1.0 - " + opac + ", 1.0 - " + opac + ") * " + cola + " + float3(" + opac + ", " + opac + ", " + opac + ") * " + cola + " / " + colb + "";
  384. }
  385. else if (blending == blend_type_t.HUE) {
  386. node_shader_add_function(kong, str_hue_sat);
  387. return "lerp3(" + cola + ", hsv_to_rgb(float3(rgb_to_hsv(" + colb + ").r, rgb_to_hsv(" + cola + ").g, rgb_to_hsv(" + cola + ").b)), " + opac + ")";
  388. }
  389. else if (blending == blend_type_t.SATURATION) {
  390. node_shader_add_function(kong, str_hue_sat);
  391. return "lerp3(" + cola + ", hsv_to_rgb(float3(rgb_to_hsv(" + cola + ").r, rgb_to_hsv(" + colb + ").g, rgb_to_hsv(" + cola + ").b)), " + opac + ")";
  392. }
  393. else if (blending == blend_type_t.COLOR) {
  394. node_shader_add_function(kong, str_hue_sat);
  395. return "lerp3(" + cola + ", hsv_to_rgb(float3(rgb_to_hsv(" + colb + ").r, rgb_to_hsv(" + colb + ").g, rgb_to_hsv(" + cola + ").b)), " + opac + ")";
  396. }
  397. else { // BlendValue
  398. node_shader_add_function(kong, str_hue_sat);
  399. return "lerp3(" + cola + ", hsv_to_rgb(float3(rgb_to_hsv(" + cola + ").r, rgb_to_hsv(" + cola + ").g, rgb_to_hsv(" + colb + ").b)), " + opac + ")";
  400. }
  401. }
  402. function make_material_blend_mode_mask(blending: i32, cola: string, colb: string, opac: string): string {
  403. if (blending == blend_type_t.MIX) {
  404. return "lerp(" + cola + ", " + colb + ", " + opac + ")";
  405. }
  406. else if (blending == blend_type_t.DARKEN) {
  407. return "lerp(" + cola + ", min(" + cola + ", " + colb + "), " + opac + ")";
  408. }
  409. else if (blending == blend_type_t.MULTIPLY) {
  410. return "lerp(" + cola + ", " + cola + " * " + colb + ", " + opac + ")";
  411. }
  412. else if (blending == blend_type_t.BURN) {
  413. return "lerp(" + cola + ", 1.0 - (1.0 - " + cola + ") / " + colb + ", " + opac + ")";
  414. }
  415. else if (blending == blend_type_t.LIGHTEN) {
  416. return "max(" + cola + ", " + colb + " * " + opac + ")";
  417. }
  418. else if (blending == blend_type_t.SCREEN) {
  419. return "(1.0 - ((1.0 - " + opac + ") + " + opac + " * (1.0 - " + colb + ")) * (1.0 - " + cola + "))";
  420. }
  421. else if (blending == blend_type_t.DODGE) {
  422. return "lerp(" + cola + ", " + cola + " / (1.0 - " + colb + "), " + opac + ")";
  423. }
  424. else if (blending == blend_type_t.ADD) {
  425. return "lerp(" + cola + ", " + cola + " + " + colb + ", " + opac + ")";
  426. }
  427. else if (blending == blend_type_t.OVERLAY) {
  428. return "lerp(" + cola + ", " + cola + " < 0.5 ? 2.0 * " + cola + " * " + colb + " : 1.0 - 2.0 * (1.0 - " + cola + ") * (1.0 - " + colb + "), " + opac + ")";
  429. }
  430. else if (blending == blend_type_t.SOFT_LIGHT) {
  431. return "((1.0 - " + opac + ") * " + cola + " + " + opac + " * ((1.0 - " + cola + ") * " + colb + " * " + cola + " + " + cola + " * (1.0 - (1.0 - " + colb + ") * (1.0 - " + cola + "))))";
  432. }
  433. else if (blending == blend_type_t.LINEAR_LIGHT) {
  434. return "(" + cola + " + " + opac + " * (2.0 * (" + colb + " - 0.5)))";
  435. }
  436. else if (blending == blend_type_t.DIFFERENCE) {
  437. return "lerp(" + cola + ", abs(" + cola + " - " + colb + "), " + opac + ")";
  438. }
  439. else if (blending == blend_type_t.SUBTRACT) {
  440. return "lerp(" + cola + ", " + cola + " - " + colb + ", " + opac + ")";
  441. }
  442. else if (blending == blend_type_t.DIVIDE) {
  443. return "(1.0 - " + opac + ") * " + cola + " + " + opac + " * " + cola + " / " + colb + "";
  444. }
  445. else { // BlendHue, BlendSaturation, BlendColor, BlendValue
  446. return "lerp(" + cola + ", " + colb + ", " + opac + ")";
  447. }
  448. }
  449. function make_material_get_displace_strength(): f32 {
  450. let sc: vec4_t = context_main_object().base.transform.scale;
  451. return config_raw.displace_strength * 0.02 * sc.x;
  452. }
  453. function make_material_delete_context(c: shader_context_t) {
  454. sys_notify_on_next_frame(function (c: shader_context_t) { // Ensure pipeline is no longer in use
  455. shader_context_delete(c);
  456. }, c);
  457. }