make_material.ts 17 KB

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