make_material.ts 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  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. let override_context: _shader_override_t = {};
  64. if (con.frag.shared_samplers.length > 0) {
  65. let sampler: string = con.frag.shared_samplers[0];
  66. override_context.shared_sampler = substring(sampler, string_last_index_of(sampler, " ") + 1, sampler.length);
  67. }
  68. if (!context_raw.texture_filter) {
  69. override_context.filter = "point";
  70. }
  71. scon._.override_context = override_context;
  72. array_push(m._.shader.contexts, scon);
  73. array_push(m._.shader._.contexts, scon);
  74. for (let i: i32 = 1; i < make_mesh_layer_pass_count; ++i) {
  75. let mm: material_t = {
  76. name: "Material",
  77. canvas: null
  78. };
  79. let con: node_shader_context_t = make_mesh_run(mm, i);
  80. let scon: shader_context_t = shader_context_create(con.data);
  81. let override_context: _shader_override_t = {};
  82. if (con.frag.shared_samplers.length > 0) {
  83. let sampler: string = con.frag.shared_samplers[0];
  84. override_context.shared_sampler = substring(sampler, string_last_index_of(sampler, " ") + 1, sampler.length);
  85. }
  86. if (!context_raw.texture_filter) {
  87. override_context.filter = "point";
  88. }
  89. scon._.override_context = override_context;
  90. array_push(m._.shader.contexts, scon);
  91. array_push(m._.shader._.contexts, scon);
  92. let mcon: material_context_t;
  93. let mmcon: material_context_t = {
  94. name: "mesh" + i,
  95. bind_textures: []
  96. };
  97. mcon = material_context_create(mmcon);
  98. array_push(m.contexts, mcon);
  99. array_push(m._.contexts, mcon);
  100. }
  101. context_raw.ddirty = 2;
  102. ///if arm_voxels
  103. make_material_make_voxel(m);
  104. ///end
  105. ///if (arm_direct3d12 || arm_vulkan || arm_metal)
  106. render_path_raytrace_dirty = 1;
  107. ///end
  108. }
  109. function make_material_parse_particle_material() {
  110. let m: material_data_t = context_raw.particle_material;
  111. let sc: shader_context_t = null;
  112. for (let i: i32 = 0; i < m._.shader._.contexts.length; ++i) {
  113. let c: shader_context_t = m._.shader._.contexts[i];
  114. if (c.name == "mesh") {
  115. sc = c;
  116. break;
  117. }
  118. }
  119. if (sc != null) {
  120. array_remove(m._.shader.contexts, sc);
  121. array_remove(m._.shader._.contexts, sc);
  122. }
  123. let mm: material_t = { name: "MaterialParticle", canvas: null };
  124. let con: node_shader_context_t = make_particle_run(mm);
  125. if (sc != null) {
  126. make_material_delete_context(sc);
  127. }
  128. sc = shader_context_create(con.data);
  129. array_push(m._.shader.contexts, sc);
  130. array_push(m._.shader._.contexts, sc);
  131. }
  132. function make_material_parse_mesh_preview_material(md: material_data_t = null) {
  133. if (!make_material_get_mout()) {
  134. return;
  135. }
  136. let m: material_data_t = md == null ? project_materials[0].data : md;
  137. let scon: shader_context_t = null;
  138. for (let i: i32 = 0; i < m._.shader._.contexts.length; ++i) {
  139. let c: shader_context_t = m._.shader._.contexts[i];
  140. if (c.name == "mesh") {
  141. scon = c;
  142. break;
  143. }
  144. }
  145. array_remove(m._.shader.contexts, scon);
  146. array_remove(m._.shader._.contexts, scon);
  147. let mcon: material_context_t = {
  148. name: "mesh",
  149. bind_textures: []
  150. };
  151. let sd: material_t = {
  152. name: "Material",
  153. canvas: null
  154. };
  155. let con: node_shader_context_t = make_mesh_preview_run(sd, mcon);
  156. for (let i: i32 = 0; i < m._.contexts.length; ++i) {
  157. if (m._.contexts[i].name == "mesh") {
  158. m._.contexts[i] = material_context_create(mcon);
  159. break;
  160. }
  161. }
  162. if (scon != null) {
  163. make_material_delete_context(scon);
  164. }
  165. let compile_error: bool = false;
  166. let _scon: shader_context_t = shader_context_create(con.data);
  167. if (_scon == null) {
  168. compile_error = true;
  169. }
  170. scon = _scon;
  171. if (compile_error) {
  172. return;
  173. }
  174. array_push(m._.shader.contexts, scon);
  175. array_push(m._.shader._.contexts, scon);
  176. }
  177. ///if arm_voxels
  178. function make_material_make_voxel(m: material_data_t) {
  179. let rebuild: bool = make_material_height_used;
  180. if (config_raw.rp_gi != false && rebuild) {
  181. let scon: shader_context_t = null;
  182. for (let i: i32 = 0; i < m._.shader._.contexts.length; ++i) {
  183. let c: shader_context_t = m._.shader._.contexts[i];
  184. if (c.name == "voxel") {
  185. scon = c;
  186. break;
  187. }
  188. }
  189. if (scon != null) {
  190. make_voxel_run(scon);
  191. }
  192. }
  193. }
  194. ///end
  195. function make_material_parse_paint_material(bake_previews: bool = true) {
  196. if (!make_material_get_mout()) {
  197. return;
  198. }
  199. if (bake_previews) {
  200. let current: image_t = _g2_current;
  201. let g2_in_use: bool = _g2_in_use;
  202. if (g2_in_use) g2_end();
  203. make_material_bake_node_previews();
  204. if (g2_in_use) g2_begin(current);
  205. }
  206. let m: material_data_t = project_materials[0].data;
  207. // let scon: TShaderContext = null;
  208. // let mcon: TMaterialContext = null;
  209. for (let i: i32 = 0; i < m._.shader._.contexts.length; ++i) {
  210. let c: shader_context_t = m._.shader._.contexts[i];
  211. if (c.name == "paint") {
  212. array_remove(m._.shader.contexts, c);
  213. array_remove(m._.shader._.contexts, c);
  214. if (c != make_material_default_scon) {
  215. make_material_delete_context(c);
  216. }
  217. break;
  218. }
  219. }
  220. for (let i: i32 = 0; i < m._.contexts.length; ++i) {
  221. let c: material_context_t = m._.contexts[i];
  222. if (c.name == "paint") {
  223. array_remove(m.contexts, c);
  224. array_remove(m._.contexts, c);
  225. break;
  226. }
  227. }
  228. let sdata: material_t = {
  229. name: "Material",
  230. canvas: ui_nodes_get_canvas_material()
  231. };
  232. let tmcon: material_context_t = {
  233. name: "paint",
  234. bind_textures: []
  235. };
  236. let con: node_shader_context_t = make_paint_run(sdata, tmcon);
  237. let compile_error: bool = false;
  238. let scon: shader_context_t;
  239. let _scon: shader_context_t = shader_context_create(con.data);
  240. if (_scon == null) {
  241. compile_error = true;
  242. }
  243. scon = _scon;
  244. if (compile_error) {
  245. return;
  246. }
  247. let override_context: _shader_override_t = {};
  248. override_context.addressing = "repeat";
  249. scon._.override_context = override_context;
  250. let mcon: material_context_t = material_context_create(tmcon);
  251. array_push(m._.shader.contexts, scon);
  252. array_push(m._.shader._.contexts, scon);
  253. array_push(m.contexts, mcon);
  254. array_push(m._.contexts, mcon);
  255. if (make_material_default_scon == null) {
  256. make_material_default_scon = scon;
  257. }
  258. if (make_material_default_mcon == null) {
  259. make_material_default_mcon = mcon;
  260. }
  261. }
  262. function make_material_bake_node_previews() {
  263. context_raw.node_previews_used = [];
  264. if (context_raw.node_previews == null) {
  265. context_raw.node_previews = map_create();
  266. }
  267. let empty: ui_node_t[] = [];
  268. make_material_traverse_nodes(ui_nodes_get_canvas_material().nodes, null, empty);
  269. let keys: string[] = map_keys(context_raw.node_previews);
  270. for (let i: i32 = 0; i < keys.length; ++i) {
  271. let key: string = keys[i];
  272. if (array_index_of(context_raw.node_previews_used, key) == -1) {
  273. let image: image_t = map_get(context_raw.node_previews, key);
  274. app_notify_on_next_frame(function (image: image_t) {
  275. image_unload(image);
  276. }, image);
  277. map_delete(context_raw.node_previews, key);
  278. }
  279. }
  280. }
  281. function make_material_traverse_nodes(nodes: ui_node_t[], group: ui_node_canvas_t, parents: ui_node_t[]) {
  282. for (let i: i32 = 0; i < nodes.length; ++i) {
  283. let node: ui_node_t = nodes[i];
  284. make_material_bake_node_preview(node, group, parents);
  285. if (node.type == "GROUP") {
  286. for (let j: i32 = 0; j < project_material_groups.length; ++j) {
  287. let g: node_group_t = project_material_groups[j];
  288. let cname: string = g.canvas.name;
  289. if (cname == node.name) {
  290. array_push(parents, node);
  291. make_material_traverse_nodes(g.canvas.nodes, g.canvas, parents);
  292. array_pop(parents);
  293. break;
  294. }
  295. }
  296. }
  297. }
  298. }
  299. function make_material_bake_node_preview(node: ui_node_t, group: ui_node_canvas_t, parents: ui_node_t[]) {
  300. if (node.type == "BLUR") {
  301. let id: string = parser_material_node_name(node, parents);
  302. let image: image_t = map_get(context_raw.node_previews, id);
  303. array_push(context_raw.node_previews_used, id);
  304. let res_x: i32 = math_floor(config_get_texture_res_x() / 4);
  305. let res_y: i32 = math_floor(config_get_texture_res_y() / 4);
  306. if (image == null || image.width != res_x || image.height != res_y) {
  307. if (image != null) {
  308. image_unload(image);
  309. }
  310. image = image_create_render_target(res_x, res_y);
  311. map_set(context_raw.node_previews, id, image);
  312. }
  313. parser_material_blur_passthrough = true;
  314. util_render_make_node_preview(ui_nodes_get_canvas_material(), node, image, group, parents);
  315. parser_material_blur_passthrough = false;
  316. }
  317. else if (node.type == "DIRECT_WARP") {
  318. let id: string = parser_material_node_name(node, parents);
  319. let image: image_t = map_get(context_raw.node_previews, id);
  320. array_push(context_raw.node_previews_used, id);
  321. let res_x: i32 = math_floor(config_get_texture_res_x());
  322. let res_y: i32 = math_floor(config_get_texture_res_y());
  323. if (image == null || image.width != res_x || image.height != res_y) {
  324. if (image != null) image_unload(image);
  325. image = image_create_render_target(res_x, res_y);
  326. map_set(context_raw.node_previews, id, image);
  327. }
  328. parser_material_warp_passthrough = true;
  329. util_render_make_node_preview(ui_nodes_get_canvas_material(), node, image, group, parents);
  330. parser_material_warp_passthrough = false;
  331. }
  332. else if (node.type == "BAKE_CURVATURE") {
  333. let id: string = parser_material_node_name(node, parents);
  334. let image: image_t = map_get(context_raw.node_previews, id);
  335. array_push(context_raw.node_previews_used, id);
  336. let res_x: i32 = math_floor(config_get_texture_res_x());
  337. let res_y: i32 = math_floor(config_get_texture_res_y());
  338. if (image == null || image.width != res_x || image.height != res_y) {
  339. if (image != null) {
  340. image_unload(image);
  341. }
  342. image = image_create_render_target(res_x, res_y, tex_format_t.R8);
  343. map_set(context_raw.node_previews, id, image);
  344. }
  345. if (render_path_paint_live_layer == null) {
  346. render_path_paint_live_layer = slot_layer_create("_live");
  347. }
  348. let _space: i32 = ui_header_worktab.position;
  349. let _tool: workspace_tool_t = context_raw.tool;
  350. let _bake_type: bake_type_t = context_raw.bake_type;
  351. ui_header_worktab.position = space_type_t.SPACE3D;
  352. context_raw.tool = workspace_tool_t.BAKE;
  353. context_raw.bake_type = bake_type_t.CURVATURE;
  354. parser_material_bake_passthrough = true;
  355. parser_material_start_node = node;
  356. parser_material_start_group = group;
  357. parser_material_start_parents = parents;
  358. make_material_parse_paint_material(false);
  359. parser_material_bake_passthrough = false;
  360. parser_material_start_node = null;
  361. parser_material_start_group = null;
  362. parser_material_start_parents = null;
  363. context_raw.pdirty = 1;
  364. render_path_paint_use_live_layer(true);
  365. render_path_paint_commands_paint(false);
  366. render_path_paint_dilate(true, false);
  367. render_path_paint_use_live_layer(false);
  368. context_raw.pdirty = 0;
  369. ui_header_worktab.position = _space;
  370. context_raw.tool = _tool;
  371. context_raw.bake_type = _bake_type;
  372. make_material_parse_paint_material(false);
  373. let rts: map_t<string, render_target_t> = render_path_render_targets;
  374. let texpaint_live: render_target_t = map_get(rts, "texpaint_live");
  375. g2_begin(image);
  376. g2_draw_image(texpaint_live._image, 0, 0);
  377. g2_end();
  378. }
  379. }
  380. type parse_node_preview_result_t = {
  381. scon: shader_context_t;
  382. mcon: material_context_t;
  383. };
  384. 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 {
  385. if (node.outputs.length == 0) {
  386. return null;
  387. }
  388. let sdata: material_t = {
  389. name: "Material",
  390. canvas: ui_nodes_get_canvas_material()
  391. };
  392. let mcon_raw: material_context_t = {
  393. name: "mesh",
  394. bind_textures: []
  395. };
  396. let con: node_shader_context_t = make_node_preview_run(sdata, mcon_raw, node, group, parents);
  397. let compile_error: bool = false;
  398. let scon: shader_context_t;
  399. let _scon: shader_context_t = shader_context_create(con.data);
  400. if (_scon == null) {
  401. compile_error = true;
  402. }
  403. scon = _scon;
  404. if (compile_error) {
  405. return null;
  406. }
  407. let mcon: material_context_t = material_context_create(mcon_raw);
  408. let result: parse_node_preview_result_t = {
  409. scon: scon,
  410. mcon: mcon
  411. };
  412. return result;
  413. }
  414. function make_material_parse_brush() {
  415. parser_logic_parse(context_raw.brush.canvas);
  416. }
  417. function make_material_blend_mode(frag: node_shader_t, blending: i32, cola: string, colb: string, opac: string): string {
  418. if (blending == blend_type_t.MIX) {
  419. return "mix(" + cola + ", " + colb + ", " + opac + ")";
  420. }
  421. else if (blending == blend_type_t.DARKEN) {
  422. return "mix(" + cola + ", min(" + cola + ", " + colb + "), " + opac + ")";
  423. }
  424. else if (blending == blend_type_t.MULTIPLY) {
  425. return "mix(" + cola + ", " + cola + " * " + colb + ", " + opac + ")";
  426. }
  427. else if (blending == blend_type_t.BURN) {
  428. return "mix(" + cola + ", vec3(1.0, 1.0, 1.0) - (vec3(1.0, 1.0, 1.0) - " + cola + ") / " + colb + ", " + opac + ")";
  429. }
  430. else if (blending == blend_type_t.LIGHTEN) {
  431. return "max(" + cola + ", " + colb + " * " + opac + ")";
  432. }
  433. else if (blending == blend_type_t.SCREEN) {
  434. return "(vec3(1.0, 1.0, 1.0) - (vec3(1.0 - " + opac + ", 1.0 - " + opac + ", 1.0 - " + opac + ") + " + opac + " * (vec3(1.0, 1.0, 1.0) - " + colb + ")) * (vec3(1.0, 1.0, 1.0) - " + cola + "))";
  435. }
  436. else if (blending == blend_type_t.DODGE) {
  437. return "mix(" + cola + ", " + cola + " / (vec3(1.0, 1.0, 1.0) - " + colb + "), " + opac + ")";
  438. }
  439. else if (blending == blend_type_t.ADD) {
  440. return "mix(" + cola + ", " + cola + " + " + colb + ", " + opac + ")";
  441. }
  442. else if (blending == blend_type_t.OVERLAY) {
  443. return "mix(" + cola + ", vec3( \
  444. " + cola + ".r < 0.5 ? 2.0 * " + cola + ".r * " + colb + ".r : 1.0 - 2.0 * (1.0 - " + cola + ".r) * (1.0 - " + colb + ".r), \
  445. " + cola + ".g < 0.5 ? 2.0 * " + cola + ".g * " + colb + ".g : 1.0 - 2.0 * (1.0 - " + cola + ".g) * (1.0 - " + colb + ".g), \
  446. " + cola + ".b < 0.5 ? 2.0 * " + cola + ".b * " + colb + ".b : 1.0 - 2.0 * (1.0 - " + cola + ".b) * (1.0 - " + colb + ".b) \
  447. ), " + opac + ")";
  448. }
  449. else if (blending == blend_type_t.SOFT_LIGHT) {
  450. return "((1.0 - " + opac + ") * " + cola + " + " + opac + " * ((vec3(1.0, 1.0, 1.0) - " + cola + ") * " + colb + " * " + cola + " + " + cola + " * (vec3(1.0, 1.0, 1.0) - (vec3(1.0, 1.0, 1.0) - " + colb + ") * (vec3(1.0, 1.0, 1.0) - " + cola + "))))";
  451. }
  452. else if (blending == blend_type_t.LINEAR_LIGHT) {
  453. return "(" + cola + " + " + opac + " * (vec3(2.0, 2.0, 2.0) * (" + colb + " - vec3(0.5, 0.5, 0.5))))";
  454. }
  455. else if (blending == blend_type_t.DIFFERENCE) {
  456. return "mix(" + cola + ", abs(" + cola + " - " + colb + "), " + opac + ")";
  457. }
  458. else if (blending == blend_type_t.SUBTRACT) {
  459. return "mix(" + cola + ", " + cola + " - " + colb + ", " + opac + ")";
  460. }
  461. else if (blending == blend_type_t.DIVIDE) {
  462. return "vec3(1.0 - " + opac + ", 1.0 - " + opac + ", 1.0 - " + opac + ") * " + cola + " + vec3(" + opac + ", " + opac + ", " + opac + ") * " + cola + " / " + colb + "";
  463. }
  464. else if (blending == blend_type_t.HUE) {
  465. node_shader_add_function(frag, str_hue_sat);
  466. return "mix(" + cola + ", hsv_to_rgb(vec3(rgb_to_hsv(" + colb + ").r, rgb_to_hsv(" + cola + ").g, rgb_to_hsv(" + cola + ").b)), " + opac + ")";
  467. }
  468. else if (blending == blend_type_t.SATURATION) {
  469. node_shader_add_function(frag, str_hue_sat);
  470. return "mix(" + cola + ", hsv_to_rgb(vec3(rgb_to_hsv(" + cola + ").r, rgb_to_hsv(" + colb + ").g, rgb_to_hsv(" + cola + ").b)), " + opac + ")";
  471. }
  472. else if (blending == blend_type_t.COLOR) {
  473. node_shader_add_function(frag, str_hue_sat);
  474. return "mix(" + cola + ", hsv_to_rgb(vec3(rgb_to_hsv(" + colb + ").r, rgb_to_hsv(" + colb + ").g, rgb_to_hsv(" + cola + ").b)), " + opac + ")";
  475. }
  476. else { // BlendValue
  477. node_shader_add_function(frag, str_hue_sat);
  478. return "mix(" + cola + ", hsv_to_rgb(vec3(rgb_to_hsv(" + cola + ").r, rgb_to_hsv(" + cola + ").g, rgb_to_hsv(" + colb + ").b)), " + opac + ")";
  479. }
  480. }
  481. function make_material_blend_mode_mask(frag: node_shader_t, blending: i32, cola: string, colb: string, opac: string): string {
  482. if (blending == blend_type_t.MIX) {
  483. return "mix(" + cola + ", " + colb + ", " + opac + ")";
  484. }
  485. else if (blending == blend_type_t.DARKEN) {
  486. return "mix(" + cola + ", min(" + cola + ", " + colb + "), " + opac + ")";
  487. }
  488. else if (blending == blend_type_t.MULTIPLY) {
  489. return "mix(" + cola + ", " + cola + " * " + colb + ", " + opac + ")";
  490. }
  491. else if (blending == blend_type_t.BURN) {
  492. return "mix(" + cola + ", 1.0 - (1.0 - " + cola + ") / " + colb + ", " + opac + ")";
  493. }
  494. else if (blending == blend_type_t.LIGHTEN) {
  495. return "max(" + cola + ", " + colb + " * " + opac + ")";
  496. }
  497. else if (blending == blend_type_t.SCREEN) {
  498. return "(1.0 - ((1.0 - " + opac + ") + " + opac + " * (1.0 - " + colb + ")) * (1.0 - " + cola + "))";
  499. }
  500. else if (blending == blend_type_t.DODGE) {
  501. return "mix(" + cola + ", " + cola + " / (1.0 - " + colb + "), " + opac + ")";
  502. }
  503. else if (blending == blend_type_t.ADD) {
  504. return "mix(" + cola + ", " + cola + " + " + colb + ", " + opac + ")";
  505. }
  506. else if (blending == blend_type_t.OVERLAY) {
  507. return "mix(" + cola + ", " + cola + " < 0.5 ? 2.0 * " + cola + " * " + colb + " : 1.0 - 2.0 * (1.0 - " + cola + ") * (1.0 - " + colb + "), " + opac + ")";
  508. }
  509. else if (blending == blend_type_t.SOFT_LIGHT) {
  510. return "((1.0 - " + opac + ") * " + cola + " + " + opac + " * ((1.0 - " + cola + ") * " + colb + " * " + cola + " + " + cola + " * (1.0 - (1.0 - " + colb + ") * (1.0 - " + cola + "))))";
  511. }
  512. else if (blending == blend_type_t.LINEAR_LIGHT) {
  513. return "(" + cola + " + " + opac + " * (2.0 * (" + colb + " - 0.5)))";
  514. }
  515. else if (blending == blend_type_t.DIFFERENCE) {
  516. return "mix(" + cola + ", abs(" + cola + " - " + colb + "), " + opac + ")";
  517. }
  518. else if (blending == blend_type_t.SUBTRACT) {
  519. return "mix(" + cola + ", " + cola + " - " + colb + ", " + opac + ")";
  520. }
  521. else if (blending == blend_type_t.DIVIDE) {
  522. return "(1.0 - " + opac + ") * " + cola + " + " + opac + " * " + cola + " / " + colb + "";
  523. }
  524. else { // BlendHue, BlendSaturation, BlendColor, BlendValue
  525. return "mix(" + cola + ", " + colb + ", " + opac + ")";
  526. }
  527. }
  528. function make_material_get_displace_strength(): f32 {
  529. let sc: vec4_t = context_main_object().base.transform.scale;
  530. return config_raw.displace_strength * 0.02 * sc.x;
  531. }
  532. function make_material_voxelgi_half_extents(): string {
  533. let ext: f32 = context_raw.vxao_ext;
  534. return "const vec3 voxelgi_half_extents = vec3(" + ext + ", " + ext + ", " + ext + ");";
  535. }
  536. function make_material_delete_context(c: shader_context_t) {
  537. app_notify_on_next_frame(function (c: shader_context_t) { // Ensure pipeline is no longer in use
  538. shader_context_delete(c);
  539. }, c);
  540. }