base.ts 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433
  1. let base_ui_enabled: bool = true;
  2. let base_is_dragging: bool = false;
  3. let base_is_resizing: bool = false;
  4. let base_drag_asset: asset_t = null;
  5. let base_drag_swatch: swatch_color_t = null;
  6. let base_drag_file: string = null;
  7. let base_drag_file_icon: image_t = null;
  8. let base_drag_tint: i32 = 0xffffffff;
  9. let base_drag_size: i32 = -1;
  10. let base_drag_rect: rect_t = null;
  11. let base_drag_off_x: f32 = 0.0;
  12. let base_drag_off_y: f32 = 0.0;
  13. let base_drag_start: f32 = 0.0;
  14. let base_drop_x: f32 = 0.0;
  15. let base_drop_y: f32 = 0.0;
  16. let base_font: g2_font_t = null;
  17. let base_theme: theme_t;
  18. let base_color_wheel: image_t;
  19. let base_color_wheel_gradient: image_t;
  20. let base_ui_box: zui_t;
  21. let base_ui_menu: zui_t;
  22. let base_default_element_w: i32 = 100;
  23. let base_default_element_h: i32 = 28;
  24. let base_default_font_size: i32 = 13;
  25. let base_res_handle: zui_handle_t = zui_handle_create();
  26. let base_bits_handle: zui_handle_t = zui_handle_create();
  27. let base_drop_paths: string[] = [];
  28. let base_appx: i32 = 0;
  29. let base_appy: i32 = 0;
  30. let base_last_window_width: i32 = 0;
  31. let base_last_window_height: i32 = 0;
  32. ///if (is_paint || is_sculpt)
  33. let base_drag_material: slot_material_t = null;
  34. let base_drag_layer: slot_layer_t = null;
  35. ///end
  36. let base_pipe_copy: pipeline_t;
  37. let base_pipe_copy8: pipeline_t;
  38. let base_pipe_copy128: pipeline_t;
  39. let base_pipe_copy_bgra: pipeline_t;
  40. let base_pipe_copy_rgb: pipeline_t = null;
  41. ///if (is_paint || is_sculpt)
  42. let base_pipe_merge: pipeline_t = null;
  43. let base_pipe_merge_r: pipeline_t = null;
  44. let base_pipe_merge_g: pipeline_t = null;
  45. let base_pipe_merge_b: pipeline_t = null;
  46. let base_pipe_merge_a: pipeline_t = null;
  47. let base_pipe_invert8: pipeline_t;
  48. let base_pipe_apply_mask: pipeline_t;
  49. let base_pipe_merge_mask: pipeline_t;
  50. let base_pipe_colorid_to_mask: pipeline_t;
  51. let base_tex0: kinc_tex_unit_t;
  52. let base_tex1: kinc_tex_unit_t;
  53. let base_texmask: kinc_tex_unit_t;
  54. let base_texa: kinc_tex_unit_t;
  55. let base_opac: kinc_const_loc_t;
  56. let base_blending: kinc_const_loc_t;
  57. let base_tex0_mask: kinc_tex_unit_t;
  58. let base_texa_mask: kinc_tex_unit_t;
  59. let base_tex0_merge_mask: kinc_tex_unit_t;
  60. let base_texa_merge_mask: kinc_tex_unit_t;
  61. let base_tex_colorid: kinc_tex_unit_t;
  62. let base_texpaint_colorid: kinc_tex_unit_t;
  63. let base_opac_merge_mask: kinc_const_loc_t;
  64. let base_blending_merge_mask: kinc_const_loc_t;
  65. let base_temp_mask_image: image_t = null;
  66. ///end
  67. ///if is_lab
  68. let base_pipe_copy_r: pipeline_t;
  69. let base_pipe_copy_g: pipeline_t;
  70. let base_pipe_copy_b: pipeline_t;
  71. let base_pipe_copy_a: pipeline_t;
  72. let base_pipe_copy_a_tex: kinc_tex_unit_t;
  73. let base_pipe_inpaint_preview: pipeline_t;
  74. let base_tex0_inpaint_preview: kinc_tex_unit_t;
  75. let base_texa_inpaint_preview: kinc_tex_unit_t;
  76. ///end
  77. let base_temp_image: image_t = null;
  78. let base_expa: image_t = null;
  79. let base_expb: image_t = null;
  80. let base_expc: image_t = null;
  81. let base_pipe_cursor: pipeline_t;
  82. let base_cursor_vp: kinc_const_loc_t;
  83. let base_cursor_inv_vp: kinc_const_loc_t;
  84. let base_cursor_mouse: kinc_const_loc_t;
  85. let base_cursor_tex_step: kinc_const_loc_t;
  86. let base_cursor_radius: kinc_const_loc_t;
  87. let base_cursor_camera_right: kinc_const_loc_t;
  88. let base_cursor_tint: kinc_const_loc_t;
  89. let base_cursor_tex: kinc_tex_unit_t;
  90. let base_cursor_gbufferd: kinc_tex_unit_t;
  91. ///if (is_paint || is_sculpt)
  92. let base_default_base: f32 = 0.5;
  93. let base_default_rough: f32 = 0.4;
  94. ///if (krom_android || krom_ios)
  95. let base_max_layers: i32 = 18;
  96. ///else
  97. let base_max_layers: i32 = 255;
  98. ///end
  99. ///end
  100. let base_default_fov: f32 = 0.69;
  101. function base_get_default_keymap(): map_t<string, string> {
  102. let keymap: map_t<string, string> = map_create();
  103. map_set(keymap, "action_paint", "left");
  104. map_set(keymap, "action_rotate", "alt+left");
  105. map_set(keymap, "action_pan", "alt+middle");
  106. map_set(keymap, "action_zoom", "alt+right");
  107. map_set(keymap, "rotate_light", "shift+middle");
  108. map_set(keymap, "rotate_envmap", "ctrl+middle");
  109. map_set(keymap, "set_clone_source", "alt");
  110. map_set(keymap, "stencil_transform", "ctrl");
  111. map_set(keymap, "stencil_hide", "z");
  112. map_set(keymap, "brush_radius", "f");
  113. map_set(keymap, "brush_radius_decrease", "[");
  114. map_set(keymap, "brush_radius_increase", "]");
  115. map_set(keymap, "brush_ruler", "shift");
  116. map_set(keymap, "file_new", "ctrl+n");
  117. map_set(keymap, "file_open", "ctrl+o");
  118. map_set(keymap, "file_open_recent", "ctrl+shift+o");
  119. map_set(keymap, "file_save", "ctrl+s");
  120. map_set(keymap, "file_save_as", "ctrl+shift+s");
  121. map_set(keymap, "file_reimport_mesh", "ctrl+r");
  122. map_set(keymap, "file_reimport_textures", "ctrl+shift+r");
  123. map_set(keymap, "file_import_assets", "ctrl+i");
  124. map_set(keymap, "file_export_textures", "ctrl+e");
  125. map_set(keymap, "file_export_textures_as", "ctrl+shift+e");
  126. map_set(keymap, "edit_undo", "ctrl+z");
  127. map_set(keymap, "edit_redo", "ctrl+shift+z");
  128. map_set(keymap, "edit_prefs", "ctrl+k");
  129. map_set(keymap, "view_reset", "0");
  130. map_set(keymap, "view_front", "1");
  131. map_set(keymap, "view_back", "ctrl+1");
  132. map_set(keymap, "view_right", "3");
  133. map_set(keymap, "view_left", "ctrl+3");
  134. map_set(keymap, "view_top", "7");
  135. map_set(keymap, "view_bottom", "ctrl+7");
  136. map_set(keymap, "view_camera_type", "5");
  137. map_set(keymap, "view_orbit_left", "4");
  138. map_set(keymap, "view_orbit_right", "6");
  139. map_set(keymap, "view_orbit_up", "8");
  140. map_set(keymap, "view_orbit_down", "2");
  141. map_set(keymap, "view_orbit_opposite", "9");
  142. map_set(keymap, "view_zoom_in", "");
  143. map_set(keymap, "view_zoom_out", "");
  144. map_set(keymap, "view_distract_free", "f11");
  145. map_set(keymap, "viewport_mode", "ctrl+m");
  146. map_set(keymap, "toggle_node_editor", "tab");
  147. map_set(keymap, "toggle_2d_view", "shift+tab");
  148. map_set(keymap, "toggle_browser", "`");
  149. map_set(keymap, "node_search", "space");
  150. map_set(keymap, "operator_search", "space");
  151. ///if (is_paint || is_sculpt)
  152. map_set(keymap, "decal_mask", "ctrl");
  153. map_set(keymap, "select_material", "shift+number");
  154. map_set(keymap, "select_layer", "alt+number");
  155. map_set(keymap, "brush_opacity", "shift+f");
  156. map_set(keymap, "brush_angle", "alt+f");
  157. map_set(keymap, "tool_brush", "b");
  158. map_set(keymap, "tool_eraser", "e");
  159. map_set(keymap, "tool_fill", "g");
  160. map_set(keymap, "tool_decal", "d");
  161. map_set(keymap, "tool_text", "t");
  162. map_set(keymap, "tool_clone", "l");
  163. map_set(keymap, "tool_blur", "u");
  164. map_set(keymap, "tool_smudge", "m");
  165. map_set(keymap, "tool_particle", "p");
  166. map_set(keymap, "tool_colorid", "c");
  167. map_set(keymap, "tool_picker", "v");
  168. map_set(keymap, "tool_bake", "k");
  169. map_set(keymap, "tool_gizmo", "");
  170. map_set(keymap, "tool_material", "");
  171. map_set(keymap, "swap_brush_eraser", "");
  172. ///end
  173. return keymap;
  174. }
  175. function base_init() {
  176. base_last_window_width = sys_width();
  177. base_last_window_height = sys_height();
  178. sys_notify_on_drop_files(function (drop_path: string) {
  179. ///if krom_linux
  180. drop_path = uri_decode(drop_path);
  181. ///end
  182. drop_path = trim_end(drop_path);
  183. array_push(base_drop_paths, drop_path);
  184. });
  185. sys_notify_on_app_state(
  186. function () { // Foreground
  187. context_raw.foreground_event = true;
  188. context_raw.last_paint_x = -1;
  189. context_raw.last_paint_y = -1;
  190. },
  191. function () {}, // Resume
  192. function () {}, // Pause
  193. function () { // Background
  194. // Release keys after alt-tab / win-tab
  195. keyboard_up_listener(key_code_t.ALT);
  196. keyboard_up_listener(key_code_t.WIN);
  197. },
  198. function () { // Shutdown
  199. ///if (krom_android || krom_ios)
  200. project_save();
  201. ///end
  202. }
  203. );
  204. krom_set_save_and_quit_callback(base_save_and_quit_callback);
  205. let f: g2_font_t = data_get_font("font.ttf");
  206. let image_color_wheel: image_t = data_get_image("color_wheel.k");
  207. let image_color_wheel_gradient: image_t = data_get_image("color_wheel_gradient.k");
  208. base_font = f;
  209. config_load_theme(config_raw.theme, false);
  210. base_default_element_w = base_theme.ELEMENT_W;
  211. base_default_font_size = base_theme.FONT_SIZE;
  212. translator_load_translations(config_raw.locale);
  213. ui_files_filename = tr("untitled");
  214. ///if (krom_android || krom_ios)
  215. sys_title_set(tr("untitled"));
  216. ///end
  217. // Baked font for fast startup
  218. if (config_raw.locale == "en") {
  219. base_font.font_ = krom_g2_font_13(base_font.blob);
  220. base_font.glyphs = _g2_font_glyphs;
  221. }
  222. else {
  223. g2_font_init(base_font);
  224. }
  225. base_color_wheel = image_color_wheel;
  226. base_color_wheel_gradient = image_color_wheel_gradient;
  227. zui_set_enum_texts(base_enum_texts);
  228. zui_tr = tr;
  229. let ops: zui_options_t = {
  230. theme: base_theme,
  231. font: f,
  232. scale_factor: config_raw.window_scale,
  233. color_wheel: base_color_wheel,
  234. black_white_gradient: base_color_wheel_gradient
  235. }
  236. base_ui_box = zui_create(ops);
  237. base_ui_menu = zui_create(ops);
  238. base_default_element_h = base_ui_menu.ops.theme.ELEMENT_H;
  239. // Init plugins
  240. if (config_raw.plugins != null) {
  241. for (let i: i32 = 0; i < config_raw.plugins.length; ++i) {
  242. let plugin: string = config_raw.plugins[i];
  243. plugin_start(plugin);
  244. }
  245. }
  246. args_parse();
  247. camera_init();
  248. ui_base_init();
  249. ui_nodes_init();
  250. ui_view2d_init();
  251. ///if is_lab
  252. random_node_set_seed(math_floor(time_time() * 4294967295));
  253. ///end
  254. app_notify_on_update(base_update);
  255. app_notify_on_render_2d(ui_view2d_render);
  256. app_notify_on_update(ui_view2d_update);
  257. ///if (is_paint || is_sculpt)
  258. app_notify_on_render_2d(ui_base_render_cursor);
  259. ///end
  260. app_notify_on_update(ui_nodes_update);
  261. app_notify_on_render_2d(ui_nodes_render);
  262. app_notify_on_update(ui_base_update);
  263. app_notify_on_render_2d(ui_base_render);
  264. app_notify_on_update(camera_update);
  265. app_notify_on_render_2d(base_render);
  266. ///if (is_paint || is_sculpt)
  267. base_appx = ui_toolbar_w;
  268. ///end
  269. ///if is_lab
  270. base_appx = 0;
  271. ///end
  272. base_appy = ui_header_h;
  273. if (config_raw.layout[layout_size_t.HEADER] == 1) base_appy += ui_header_h;
  274. let cam: camera_object_t = scene_camera;
  275. cam.data.fov = math_floor(cam.data.fov * 100) / 100;
  276. camera_object_build_proj(cam);
  277. args_run();
  278. ///if (krom_android || krom_ios)
  279. let has_projects: bool = config_raw.recent_projects.length > 0;
  280. ///else
  281. let has_projects: bool = true;
  282. ///end
  283. if (config_raw.splash_screen && has_projects) {
  284. box_projects_show();
  285. }
  286. }
  287. function base_save_and_quit_callback(save: bool) {
  288. base_save_window_rect();
  289. if (save) {
  290. project_save(true);
  291. }
  292. else {
  293. sys_stop();
  294. }
  295. }
  296. ///if (is_paint || is_sculpt)
  297. function base_w(): i32 {
  298. // Drawing material preview
  299. if (context_raw.material_preview) {
  300. return util_render_material_preview_size;
  301. }
  302. // Drawing decal preview
  303. if (context_raw.decal_preview) {
  304. return util_render_decal_preview_size;
  305. }
  306. let res: i32 = 0;
  307. if (config_raw.layout == null) {
  308. let sidebarw: i32 = ui_base_default_sidebar_w;
  309. res = sys_width() - sidebarw - ui_toolbar_default_w;
  310. }
  311. else if (ui_nodes_show || ui_view2d_show) {
  312. res = sys_width() - config_raw.layout[layout_size_t.SIDEBAR_W] - config_raw.layout[layout_size_t.NODES_W] - ui_toolbar_w;
  313. }
  314. else if (ui_base_show) {
  315. res = sys_width() - config_raw.layout[layout_size_t.SIDEBAR_W] - ui_toolbar_w;
  316. }
  317. else { // Distract free
  318. res = sys_width();
  319. }
  320. if (context_raw.view_index > -1) {
  321. res = math_floor(res / 2);
  322. }
  323. if (context_raw.paint2d_view) {
  324. res = ui_view2d_ww;
  325. }
  326. return res > 0 ? res : 1; // App was minimized, force render path resize
  327. }
  328. function base_h(): i32 {
  329. // Drawing material preview
  330. if (context_raw.material_preview) {
  331. return util_render_material_preview_size;
  332. }
  333. // Drawing decal preview
  334. if (context_raw.decal_preview) {
  335. return util_render_decal_preview_size;
  336. }
  337. let res: i32 = sys_height();
  338. if (config_raw.layout == null) {
  339. res -= ui_header_default_h * 2 + ui_status_default_status_h;
  340. ///if (krom_android || krom_ios)
  341. let layout_header: i32 = 0;
  342. ///else
  343. let layout_header: i32 = 1;
  344. ///end
  345. if (layout_header == 0) {
  346. res += ui_header_h;
  347. }
  348. }
  349. else if (ui_base_show && res > 0) {
  350. let statush: i32 = config_raw.layout[layout_size_t.STATUS_H];
  351. res -= math_floor(ui_header_default_h * 2 * config_raw.window_scale) + statush;
  352. if (config_raw.layout[layout_size_t.HEADER] == 0) {
  353. res += ui_header_h;
  354. }
  355. }
  356. return res > 0 ? res : 1; // App was minimized, force render path resize
  357. }
  358. ///end
  359. ///if is_lab
  360. function base_w(): i32 {
  361. let res: i32 = 0;
  362. if (ui_nodes_show || ui_view2d_show) {
  363. res = sys_width() - config_raw.layout[layout_size_t.NODES_W];
  364. }
  365. else { // Distract free
  366. res = sys_width();
  367. }
  368. return res > 0 ? res : 1; // App was minimized, force render path resize
  369. }
  370. function base_h(): i32 {
  371. let res: i32 = sys_height();
  372. if (res > 0) {
  373. let statush: i32 = config_raw.layout[layout_size_t.STATUS_H];
  374. res -= math_floor(ui_header_default_h * 2 * config_raw.window_scale) + statush;
  375. }
  376. return res > 0 ? res : 1; // App was minimized, force render path resize
  377. }
  378. ///end
  379. function base_x(): i32 {
  380. ///if (is_paint || is_sculpt)
  381. return context_raw.view_index == 1 ? base_appx + base_w() : base_appx;
  382. ///end
  383. ///if is_lab
  384. return base_appx;
  385. ///end
  386. }
  387. function base_y(): i32 {
  388. return base_appy;
  389. }
  390. function base_on_resize() {
  391. if (sys_width() == 0 || sys_height() == 0) {
  392. return;
  393. }
  394. let ratio_w: f32 = sys_width() / base_last_window_width;
  395. base_last_window_width = sys_width();
  396. let ratio_h: f32 = sys_height() / base_last_window_height;
  397. base_last_window_height = sys_height();
  398. config_raw.layout[layout_size_t.NODES_W] = math_floor(config_raw.layout[layout_size_t.NODES_W] * ratio_w);
  399. ///if (is_paint || is_sculpt)
  400. config_raw.layout[layout_size_t.SIDEBAR_H0] = math_floor(config_raw.layout[layout_size_t.SIDEBAR_H0] * ratio_h);
  401. config_raw.layout[layout_size_t.SIDEBAR_H1] = sys_height() - config_raw.layout[layout_size_t.SIDEBAR_H0];
  402. ///end
  403. base_resize();
  404. ///if (krom_linux || krom_darwin)
  405. base_save_window_rect();
  406. ///end
  407. }
  408. function base_save_window_rect() {
  409. ///if (krom_windows || krom_linux || krom_darwin)
  410. config_raw.window_w = sys_width();
  411. config_raw.window_h = sys_height();
  412. config_raw.window_x = sys_x();
  413. config_raw.window_y = sys_y();
  414. config_save();
  415. ///end
  416. }
  417. function base_resize() {
  418. if (sys_width() == 0 || sys_height() == 0) {
  419. return;
  420. }
  421. let cam: camera_object_t = scene_camera;
  422. if (cam.data.ortho != null) {
  423. cam.data.ortho[2] = -2 * (app_h() / app_w());
  424. cam.data.ortho[3] = 2 * (app_h() / app_w());
  425. }
  426. camera_object_build_proj(cam);
  427. if (context_raw.camera_type == camera_type_t.ORTHOGRAPHIC) {
  428. viewport_update_camera_type(context_raw.camera_type);
  429. }
  430. context_raw.ddirty = 2;
  431. if (ui_base_show) {
  432. ///if (is_paint || is_sculpt)
  433. base_appx = ui_toolbar_w;
  434. ///end
  435. ///if is_lab
  436. base_appx = 0;
  437. ///end
  438. base_appy = ui_header_h * 2;
  439. if (config_raw.layout[layout_size_t.HEADER] == 0) {
  440. base_appy -= ui_header_h;
  441. }
  442. }
  443. else {
  444. base_appx = 0;
  445. base_appy = 0;
  446. }
  447. if (ui_nodes_grid != null) {
  448. app_notify_on_next_frame(function (grid: image_t) {
  449. image_unload(grid);
  450. }, ui_nodes_grid);
  451. ui_nodes_grid = null;
  452. }
  453. base_redraw_ui();
  454. }
  455. function base_redraw_ui() {
  456. ui_header_handle.redraws = 2;
  457. ui_base_hwnds[tab_area_t.STATUS].redraws = 2;
  458. ui_menubar_menu_handle.redraws = 2;
  459. ui_menubar_workspace_handle.redraws = 2;
  460. ui_nodes_hwnd.redraws = 2;
  461. ui_box_hwnd.redraws = 2;
  462. ui_view2d_hwnd.redraws = 2;
  463. if (context_raw.ddirty < 0) {
  464. context_raw.ddirty = 0; // Redraw viewport
  465. }
  466. ///if (is_paint || is_sculpt)
  467. ui_base_hwnds[tab_area_t.SIDEBAR0].redraws = 2;
  468. ui_base_hwnds[tab_area_t.SIDEBAR1].redraws = 2;
  469. ui_toolbar_handle.redraws = 2;
  470. if (context_raw.split_view) {
  471. context_raw.ddirty = 1;
  472. }
  473. ///end
  474. }
  475. function base_update() {
  476. if (mouse_movement_x != 0 || mouse_movement_y != 0) {
  477. krom_set_mouse_cursor(0); // Arrow
  478. }
  479. ///if (is_paint || is_sculpt)
  480. let has_drag: bool = base_drag_asset != null || base_drag_material != null || base_drag_layer != null || base_drag_file != null || base_drag_swatch != null;
  481. ///end
  482. ///if is_lab
  483. let has_drag: bool = base_drag_asset != null || base_drag_file != null || base_drag_swatch != null;
  484. ///end
  485. if (config_raw.touch_ui) {
  486. // Touch and hold to activate dragging
  487. if (base_drag_start < 0.2) {
  488. if (has_drag && mouse_down()) {
  489. base_drag_start += time_real_delta();
  490. }
  491. else {
  492. base_drag_start = 0;
  493. }
  494. has_drag = false;
  495. }
  496. if (mouse_released()) {
  497. base_drag_start = 0;
  498. }
  499. let moved: bool = math_abs(mouse_movement_x) > 1 && math_abs(mouse_movement_y) > 1;
  500. if ((mouse_released() || moved) && !has_drag) {
  501. base_drag_asset = null;
  502. base_drag_swatch = null;
  503. base_drag_file = null;
  504. base_drag_file_icon = null;
  505. base_is_dragging = false;
  506. ///if (is_paint || is_sculpt)
  507. base_drag_material = null;
  508. base_drag_layer = null;
  509. ///end
  510. }
  511. // Disable touch scrolling while dragging is active
  512. zui_set_touch_scroll(!base_is_dragging);
  513. }
  514. if (has_drag && (mouse_movement_x != 0 || mouse_movement_y != 0)) {
  515. base_is_dragging = true;
  516. }
  517. if (mouse_released() && has_drag) {
  518. if (base_drag_asset != null) {
  519. if (context_in_nodes()) { // Create image texture
  520. ui_nodes_accept_asset_drag(array_index_of(project_assets, base_drag_asset));
  521. }
  522. else if (context_in_viewport()) {
  523. if (ends_with(to_lower_case(base_drag_asset.file), ".hdr")) {
  524. let image: image_t = project_get_image(base_drag_asset);
  525. import_envmap_run(base_drag_asset.file, image);
  526. }
  527. }
  528. ///if (is_paint || is_sculpt)
  529. else if (context_in_layers() || context_in_2d_view()) { // Create mask
  530. base_create_image_mask(base_drag_asset);
  531. }
  532. ///end
  533. base_drag_asset = null;
  534. }
  535. else if (base_drag_swatch != null) {
  536. if (context_in_nodes()) { // Create RGB node
  537. ui_nodes_accept_swatch_drag(base_drag_swatch);
  538. }
  539. else if (context_in_swatches()) {
  540. tab_swatches_accept_swatch_drag(base_drag_swatch);
  541. }
  542. ///if (is_paint || is_sculpt)
  543. else if (context_in_materials()) {
  544. tab_materials_accept_swatch_drag(base_drag_swatch);
  545. }
  546. else if (context_in_viewport()) {
  547. let color: i32 = base_drag_swatch.base;
  548. color = color_set_ab(color, base_drag_swatch.opacity * 255);
  549. base_create_color_layer(color, base_drag_swatch.occlusion, base_drag_swatch.roughness, base_drag_swatch.metallic);
  550. }
  551. else if (context_in_layers() && tab_layers_can_drop_new_layer(context_raw.drag_dest)) {
  552. let color: i32 = base_drag_swatch.base;
  553. color = color_set_ab(color, base_drag_swatch.opacity * 255);
  554. base_create_color_layer(color, base_drag_swatch.occlusion, base_drag_swatch.roughness, base_drag_swatch.metallic, context_raw.drag_dest);
  555. }
  556. ///end
  557. base_drag_swatch = null;
  558. }
  559. else if (base_drag_file != null) {
  560. if (!context_in_browser()) {
  561. base_drop_x = mouse_x;
  562. base_drop_y = mouse_y;
  563. ///if (is_paint || is_sculpt)
  564. let material_count: i32 = project_materials.length;
  565. import_asset_run(base_drag_file, base_drop_x, base_drop_y, true, true, function () {
  566. // Asset was material
  567. if (project_materials.length > material_count) {
  568. base_drag_material = context_raw.material;
  569. base_material_dropped();
  570. }
  571. });
  572. ///end
  573. ///if is_lab
  574. import_asset_run(base_drag_file, base_drop_x, base_drop_y);
  575. ///end
  576. }
  577. base_drag_file = null;
  578. base_drag_file_icon = null;
  579. }
  580. ///if (is_paint || is_sculpt)
  581. else if (base_drag_material != null) {
  582. base_material_dropped();
  583. }
  584. else if (base_drag_layer != null) {
  585. if (context_in_nodes()) {
  586. ui_nodes_accept_layer_drag(array_index_of(project_layers, base_drag_layer));
  587. }
  588. else if (context_in_layers() && base_is_dragging) {
  589. slot_layer_move(base_drag_layer, context_raw.drag_dest);
  590. make_material_parse_mesh_material();
  591. }
  592. base_drag_layer = null;
  593. }
  594. ///end
  595. krom_set_mouse_cursor(0); // Arrow
  596. base_is_dragging = false;
  597. }
  598. if (context_raw.color_picker_callback != null && (mouse_released() || mouse_released("right"))) {
  599. context_raw.color_picker_callback = null;
  600. context_select_tool(context_raw.color_picker_previous_tool);
  601. }
  602. base_handle_drop_paths();
  603. ///if (is_paint || is_sculpt)
  604. ///if krom_windows
  605. let is_picker: bool = context_raw.tool == workspace_tool_t.PICKER || context_raw.tool == workspace_tool_t.MATERIAL;
  606. let decal: bool = context_raw.tool == workspace_tool_t.DECAL || context_raw.tool == workspace_tool_t.TEXT;
  607. zui_set_always_redraw_window(!context_raw.cache_draws ||
  608. ui_menu_show ||
  609. ui_box_show ||
  610. base_is_dragging ||
  611. is_picker ||
  612. decal ||
  613. ui_view2d_show ||
  614. !config_raw.brush_3d ||
  615. context_raw.frame < 3);
  616. ///end
  617. ///end
  618. if (zui_always_redraw_window() && context_raw.ddirty < 0) context_raw.ddirty = 0;
  619. }
  620. ///if (is_paint || is_sculpt)
  621. function base_material_dropped() {
  622. // Material drag and dropped onto viewport or layers tab
  623. if (context_in_viewport()) {
  624. let uv_type: uv_type_t = keyboard_down("control") ? uv_type_t.PROJECT : uv_type_t.UVMAP;
  625. let decal_mat: mat4_t = uv_type == uv_type_t.PROJECT ? util_render_get_decal_mat() : null;
  626. base_create_fill_layer(uv_type, decal_mat);
  627. }
  628. if (context_in_layers() && tab_layers_can_drop_new_layer(context_raw.drag_dest)) {
  629. let uv_type: uv_type_t = keyboard_down("control") ? uv_type_t.PROJECT : uv_type_t.UVMAP;
  630. let decal_mat: mat4_t = uv_type == uv_type_t.PROJECT ? util_render_get_decal_mat() : null;
  631. base_create_fill_layer(uv_type, decal_mat, context_raw.drag_dest);
  632. }
  633. else if (context_in_nodes()) {
  634. ui_nodes_accept_material_drag(array_index_of(project_materials, base_drag_material));
  635. }
  636. base_drag_material = null;
  637. }
  638. ///end
  639. function base_handle_drop_paths() {
  640. if (base_drop_paths.length > 0) {
  641. ///if (krom_linux || krom_darwin)
  642. let wait: bool = !mouse_moved; // Mouse coords not updated during drag
  643. ///else
  644. let wait: bool = false;
  645. ///end
  646. if (!wait) {
  647. base_drop_x = mouse_x;
  648. base_drop_y = mouse_y;
  649. let drop_path: string = base_drop_paths.shift();
  650. import_asset_run(drop_path, base_drop_x, base_drop_y);
  651. }
  652. }
  653. }
  654. ///if (is_paint || is_sculpt)
  655. function base_get_drag_background(): rect_t {
  656. let icons: image_t = resource_get("icons.k");
  657. if (base_drag_layer != null && !slot_layer_is_group(base_drag_layer) && base_drag_layer.fill_layer == null) {
  658. return resource_tile50(icons, 4, 1);
  659. }
  660. return null;
  661. }
  662. ///end
  663. function base_get_drag_image(): image_t {
  664. base_drag_tint = 0xffffffff;
  665. base_drag_size = -1;
  666. base_drag_rect = null;
  667. if (base_drag_asset != null) {
  668. return project_get_image(base_drag_asset);
  669. }
  670. if (base_drag_swatch != null) {
  671. base_drag_tint = base_drag_swatch.base;
  672. base_drag_size = 26;
  673. return tab_swatches_empty_get();
  674. }
  675. if (base_drag_file != null) {
  676. if (base_drag_file_icon != null) {
  677. return base_drag_file_icon;
  678. }
  679. let icons: image_t = resource_get("icons.k");
  680. base_drag_rect = string_index_of(base_drag_file, ".") > 0 ? resource_tile50(icons, 3, 1) : resource_tile50(icons, 2, 1);
  681. base_drag_tint = ui_base_ui.ops.theme.HIGHLIGHT_COL;
  682. return icons;
  683. }
  684. ///if is_paint
  685. if (base_drag_material != null) {
  686. return base_drag_material.image_icon;
  687. }
  688. if (base_drag_layer != null && slot_layer_is_group(base_drag_layer)) {
  689. let icons: image_t = resource_get("icons.k");
  690. let folder_closed: rect_t = resource_tile50(icons, 2, 1);
  691. let folder_open: rect_t = resource_tile50(icons, 8, 1);
  692. base_drag_rect = base_drag_layer.show_panel ? folder_open : folder_closed;
  693. base_drag_tint = ui_base_ui.ops.theme.LABEL_COL - 0x00202020;
  694. return icons;
  695. }
  696. if (base_drag_layer != null && slot_layer_is_mask(base_drag_layer) && base_drag_layer.fill_layer == null) {
  697. tab_layers_make_mask_preview_rgba32(base_drag_layer);
  698. return context_raw.mask_preview_rgba32;
  699. }
  700. if (base_drag_layer != null) {
  701. return base_drag_layer.fill_layer != null ? base_drag_layer.fill_layer.image_icon : base_drag_layer.texpaint_preview;
  702. }
  703. ///end
  704. return null;
  705. }
  706. function base_render() {
  707. if (sys_width() == 0 || sys_height() == 0) return;
  708. if (context_raw.frame == 2) {
  709. ///if (is_paint || is_sculpt)
  710. util_render_make_material_preview();
  711. ui_base_hwnds[tab_area_t.SIDEBAR1].redraws = 2;
  712. ///end
  713. make_material_parse_mesh_material();
  714. make_material_parse_paint_material();
  715. context_raw.ddirty = 0;
  716. ///if (is_paint || is_sculpt)
  717. if (history_undo_layers == null) {
  718. history_undo_layers = [];
  719. for (let i: i32 = 0; i < config_raw.undo_steps; ++i) {
  720. let l: slot_layer_t = slot_layer_create("_undo" + history_undo_layers.length);
  721. array_push(history_undo_layers, l);
  722. }
  723. }
  724. ///end
  725. // Default workspace
  726. if (config_raw.workspace != 0) {
  727. ui_header_worktab.position = config_raw.workspace;
  728. ui_menubar_workspace_handle.redraws = 2;
  729. ui_header_worktab.changed = true;
  730. }
  731. // Default camera controls
  732. context_raw.camera_controls = config_raw.camera_controls;
  733. ///if is_lab
  734. app_notify_on_next_frame(function () {
  735. app_notify_on_next_frame(function () {
  736. tab_meshes_set_default_mesh(".Sphere");
  737. });
  738. });
  739. ///end
  740. ///if is_sculpt
  741. app_notify_on_next_frame(function () {
  742. app_notify_on_next_frame(function () {
  743. context_raw.project_type = project_model_t.SPHERE;
  744. project_new();
  745. });
  746. });
  747. ///end
  748. }
  749. else if (context_raw.frame == 3) {
  750. context_raw.ddirty = 3;
  751. }
  752. context_raw.frame++;
  753. if (base_is_dragging) {
  754. krom_set_mouse_cursor(1); // Hand
  755. let img: image_t = base_get_drag_image();
  756. ///if (is_paint || is_sculpt)
  757. let scale_factor: f32 = zui_SCALE(ui_base_ui);
  758. ///end
  759. ///if is_lab
  760. let scale_factor: f32 = zui_SCALE(base_ui_box);
  761. ///end
  762. let size: f32 = (base_drag_size == -1 ? 50 : base_drag_size) * scale_factor;
  763. let ratio: f32 = size / img.width;
  764. let h: f32 = img.height * ratio;
  765. ///if (is_lab || krom_direct3d11 || krom_direct3d12 || krom_metal || krom_vulkan)
  766. let inv: i32 = 0;
  767. ///else
  768. let inv: i32 = (base_drag_material != null || (base_drag_layer != null && base_drag_layer.fill_layer != null)) ? h : 0;
  769. ///end
  770. g2_set_color(base_drag_tint);
  771. ///if (is_paint || is_sculpt)
  772. let bg_rect: rect_t = base_get_drag_background();
  773. if (bg_rect != null) {
  774. g2_draw_scaled_sub_image(resource_get("icons.k"), bg_rect.x, bg_rect.y, bg_rect.w, bg_rect.h, mouse_x + base_drag_off_x, mouse_y + base_drag_off_y + inv, size, h - inv * 2);
  775. }
  776. ///end
  777. base_drag_rect == null ?
  778. g2_draw_scaled_image(img, mouse_x + base_drag_off_x, mouse_y + base_drag_off_y + inv, size, h - inv * 2) :
  779. g2_draw_scaled_sub_image(img, base_drag_rect.x, base_drag_rect.y, base_drag_rect.w, base_drag_rect.h, mouse_x + base_drag_off_x, mouse_y + base_drag_off_y + inv, size, h - inv * 2);
  780. g2_set_color(0xffffffff);
  781. }
  782. let using_menu: bool = ui_menu_show && mouse_y > ui_header_h;
  783. base_ui_enabled = !ui_box_show && !using_menu && !base_is_combo_selected();
  784. if (ui_box_show) {
  785. ui_box_render();
  786. }
  787. if (ui_menu_show) {
  788. ui_menu_render();
  789. }
  790. // Save last pos for continuos paint
  791. context_raw.last_paint_vec_x = context_raw.paint_vec.x;
  792. context_raw.last_paint_vec_y = context_raw.paint_vec.y;
  793. ///if (krom_android || krom_ios)
  794. // No mouse move events for touch, re-init last paint position on touch start
  795. if (!mouse_down()) {
  796. context_raw.last_paint_x = -1;
  797. context_raw.last_paint_y = -1;
  798. }
  799. ///end
  800. }
  801. function base_enum_texts(node_type: string): string[] {
  802. ///if (is_paint || is_sculpt)
  803. if (node_type == "TEX_IMAGE") {
  804. return project_asset_names.length > 0 ? project_asset_names : [""];
  805. }
  806. if (node_type == "LAYER" || node_type == "LAYER_MASK") {
  807. let layer_names: string[] = [];
  808. for (let i: i32 = 0; i < project_layers.length; ++i) {
  809. let l: slot_layer_t = project_layers[i];
  810. array_push(layer_names, l.name);
  811. }
  812. return layer_names;
  813. }
  814. if (node_type == "MATERIAL") {
  815. let material_names: string[] = [];
  816. for (let i: i32 = 0; i < project_materials.length; ++i) {
  817. let m: slot_material_t = project_materials[i];
  818. array_push(material_names, m.canvas.name);
  819. }
  820. return material_names;
  821. }
  822. ///end
  823. ///if is_lab
  824. if (node_type == "ImageTextureNode") {
  825. return project_asset_names.length > 0 ? project_asset_names : [""];
  826. }
  827. ///end
  828. return null;
  829. }
  830. function base_get_asset_index(file_name: string): i32 {
  831. let i: i32 = array_index_of(project_asset_names, file_name);
  832. return i >= 0 ? i : 0;
  833. }
  834. function base_toggle_fullscreen() {
  835. if (sys_mode() == window_mode_t.WINDOWED) {
  836. ///if (krom_windows || krom_linux || krom_darwin)
  837. config_raw.window_w = sys_width();
  838. config_raw.window_h = sys_height();
  839. config_raw.window_x = sys_x();
  840. config_raw.window_y = sys_y();
  841. ///end
  842. sys_mode_set(window_mode_t.FULLSCREEN);
  843. }
  844. else {
  845. sys_mode_set(window_mode_t.WINDOWED);
  846. sys_resize(config_raw.window_w, config_raw.window_h);
  847. sys_move(config_raw.window_x, config_raw.window_y);
  848. }
  849. }
  850. function base_is_scrolling(): bool {
  851. for (let i: i32 = 0; i < base_get_uis().length; ++i) {
  852. let ui: zui_t = base_get_uis()[i];
  853. if (ui.is_scrolling) {
  854. return true;
  855. }
  856. }
  857. return false;
  858. }
  859. function base_is_combo_selected(): bool {
  860. for (let i: i32 = 0; i < base_get_uis().length; ++i) {
  861. let ui: zui_t = base_get_uis()[i];
  862. if (ui.combo_selected_handle_ptr != 0) {
  863. return true;
  864. }
  865. }
  866. return false;
  867. }
  868. function base_get_uis(): zui_t[] {
  869. return [base_ui_box, base_ui_menu, ui_base_ui, ui_nodes_ui, ui_view2d_ui];
  870. }
  871. function base_is_decal_layer(): bool {
  872. ///if is_paint
  873. let is_paint: bool = context_raw.tool != workspace_tool_t.MATERIAL && context_raw.tool != workspace_tool_t.BAKE;
  874. return is_paint && context_raw.layer.fill_layer != null && context_raw.layer.uv_type == uv_type_t.PROJECT;
  875. ///end
  876. ///if (is_sculpt || is_lab)
  877. return false;
  878. ///end
  879. }
  880. function base_redraw_status() {
  881. ui_base_hwnds[tab_area_t.STATUS].redraws = 2;
  882. }
  883. function base_redraw_console() {
  884. let statush: i32 = config_raw.layout[layout_size_t.STATUS_H];
  885. if (ui_base_ui != null && statush > ui_status_default_status_h * zui_SCALE(ui_base_ui)) {
  886. ui_base_hwnds[tab_area_t.STATUS].redraws = 2;
  887. }
  888. }
  889. function base_init_layout() {
  890. let show2d: bool = ui_nodes_show || ui_view2d_show;
  891. let raw: config_t = config_raw;
  892. raw.layout = [];
  893. ///if (is_paint || is_sculpt)
  894. array_push(raw.layout, math_floor(ui_base_default_sidebar_w * raw.window_scale)); // LayoutSidebarW
  895. array_push(raw.layout, math_floor(sys_height() / 2)); // LayoutSidebarH0
  896. array_push(raw.layout, math_floor(sys_height() / 2)); // LayoutSidebarH1
  897. ///end
  898. ///if krom_ios
  899. array_push(raw.layout, show2d ? math_floor((app_w() + raw.layout[layout_size_t.NODES_W]) * 0.473) : math_floor(app_w() * 0.473)); // LayoutNodesW
  900. ///elseif krom_android
  901. array_push(raw.layout, show2d ? math_floor((app_w() + raw.layout[layout_size_t.NODES_W]) * 0.473) : math_floor(app_w() * 0.473));
  902. ///else
  903. array_push(raw.layout, show2d ? math_floor((app_w() + raw.layout[layout_size_t.NODES_W]) * 0.515) : math_floor(app_w() * 0.515)); // Align with ui header controls
  904. ///end
  905. array_push(raw.layout, math_floor(app_h() / 2)); // LayoutNodesH
  906. array_push(raw.layout, math_floor(ui_status_default_status_h * raw.window_scale)); // LayoutStatusH
  907. ///if (krom_android || krom_ios)
  908. array_push(raw.layout, 0); // LayoutHeader
  909. ///else
  910. array_push(raw.layout, 1);
  911. ///end
  912. raw.layout_tabs = [
  913. ///if (is_paint || is_sculpt)
  914. 0,
  915. 0,
  916. ///end
  917. 0
  918. ];
  919. }
  920. function base_init_config() {
  921. let raw: config_t = config_raw;
  922. raw.recent_projects = [];
  923. raw.bookmarks = [];
  924. raw.plugins = [];
  925. ///if (krom_android || krom_ios)
  926. raw.keymap = "touch.json";
  927. ///else
  928. raw.keymap = "default.json";
  929. ///end
  930. raw.theme = "default.json";
  931. raw.server = "https://armorpaint.fra1.digitaloceanspaces.com";
  932. raw.undo_steps = 4;
  933. raw.pressure_radius = true;
  934. raw.pressure_sensitivity = 1.0;
  935. raw.camera_zoom_speed = 1.0;
  936. raw.camera_pan_speed = 1.0;
  937. raw.camera_rotation_speed = 1.0;
  938. raw.zoom_direction = zoom_direction_t.VERTICAL;
  939. ///if (is_paint || is_sculpt)
  940. raw.displace_strength = 0.0;
  941. ///else
  942. raw.displace_strength = 1.0;
  943. ///end
  944. raw.wrap_mouse = false;
  945. ///if is_paint
  946. raw.workspace = space_type_t.SPACE3D;
  947. ///end
  948. ///if is_sculpt
  949. raw.workspace = space_type_t.SPACE3D;
  950. ///end
  951. ///if is_lab
  952. raw.workspace = space_type_t.SPACE2D;
  953. ///end
  954. ///if (krom_android || krom_ios)
  955. raw.camera_controls = camera_controls_t.ROTATE;
  956. ///else
  957. raw.camera_controls = camera_controls_t.ORBIT;
  958. ///end
  959. raw.layer_res = texture_res_t.RES2048;
  960. ///if (krom_android || krom_ios)
  961. raw.touch_ui = true;
  962. raw.splash_screen = true;
  963. ///else
  964. raw.touch_ui = false;
  965. raw.splash_screen = false;
  966. ///end
  967. ///if (is_paint || is_sculpt)
  968. raw.node_preview = true;
  969. ///else
  970. raw.node_preview = false;
  971. ///end
  972. ///if (is_paint || is_sculpt)
  973. raw.pressure_hardness = true;
  974. raw.pressure_angle = false;
  975. raw.pressure_opacity = false;
  976. ///if (krom_vulkan || krom_ios)
  977. raw.material_live = false;
  978. ///else
  979. raw.material_live = true;
  980. ///end
  981. raw.brush_3d = true;
  982. raw.brush_depth_reject = true;
  983. raw.brush_angle_reject = true;
  984. raw.brush_live = false;
  985. raw.show_asset_names = false;
  986. ///end
  987. ///if is_paint
  988. raw.dilate = dilate_type_t.INSTANT;
  989. raw.dilate_radius = 2;
  990. ///end
  991. ///if is_lab
  992. raw.gpu_inference = true;
  993. ///end
  994. }
  995. function base_init_layers() {
  996. ///if (is_paint || is_sculpt)
  997. slot_layer_clear(project_layers[0], color_from_floats(base_default_base, base_default_base, base_default_base, 1.0));
  998. ///end
  999. ///if is_lab
  1000. let texpaint: image_t = map_get(render_path_render_targets, "texpaint")._image;
  1001. let texpaint_nor: image_t = map_get(render_path_render_targets, "texpaint_nor")._image;
  1002. let texpaint_pack: image_t = map_get(render_path_render_targets, "texpaint_pack")._image;
  1003. g2_begin(texpaint);
  1004. g2_draw_scaled_image(resource_get("placeholder.k"), 0, 0, config_get_texture_res_x(), config_get_texture_res_y()); // Base
  1005. g2_end();
  1006. g4_begin(texpaint_nor);
  1007. g4_clear(color_from_floats(0.5, 0.5, 1.0, 0.0)); // Nor
  1008. g4_end();
  1009. g4_begin(texpaint_pack);
  1010. g4_clear(color_from_floats(1.0, 0.4, 0.0, 0.0)); // Occ, rough, met
  1011. g4_end();
  1012. let texpaint_nor_empty: image_t = map_get(render_path_render_targets, "texpaint_nor_empty")._image;
  1013. let texpaint_pack_empty: image_t = map_get(render_path_render_targets, "texpaint_pack_empty")._image;
  1014. g4_begin(texpaint_nor_empty);
  1015. g4_clear(color_from_floats(0.5, 0.5, 1.0, 0.0)); // Nor
  1016. g4_end();
  1017. g4_begin(texpaint_pack_empty);
  1018. g4_clear(color_from_floats(1.0, 0.4, 0.0, 0.0)); // Occ, rough, met
  1019. g4_end();
  1020. ///end
  1021. }
  1022. ///if (is_paint || is_sculpt)
  1023. function base_resize_layers() {
  1024. let conf: config_t = config_raw;
  1025. if (base_res_handle.position >= math_floor(texture_res_t.RES16384)) { // Save memory for >=16k
  1026. conf.undo_steps = 1;
  1027. if (context_raw.undo_handle != null) {
  1028. context_raw.undo_handle.value = conf.undo_steps;
  1029. }
  1030. while (history_undo_layers.length > conf.undo_steps) {
  1031. let l: slot_layer_t = history_undo_layers.pop();
  1032. app_notify_on_next_frame(function (l: slot_layer_t) {
  1033. slot_layer_unload(l);
  1034. }, l);
  1035. }
  1036. }
  1037. for (let i: i32 = 0; i < project_layers.length; ++i) {
  1038. let l: slot_layer_t = project_layers[i];
  1039. slot_layer_resize_and_set_bits(l);
  1040. }
  1041. for (let i: i32 = 0; i < history_undo_layers.length; ++i) {
  1042. let l: slot_layer_t = history_undo_layers[i];
  1043. slot_layer_resize_and_set_bits(l);
  1044. }
  1045. let rts: map_t<string, render_target_t> = render_path_render_targets;
  1046. let _texpaint_blend0: image_t = map_get(rts, "texpaint_blend0")._image;
  1047. app_notify_on_next_frame(function (_texpaint_blend0: image_t) {
  1048. image_unload(_texpaint_blend0);
  1049. }, _texpaint_blend0);
  1050. map_get(rts, "texpaint_blend0").width = config_get_texture_res_x();
  1051. map_get(rts, "texpaint_blend0").height = config_get_texture_res_y();
  1052. map_get(rts, "texpaint_blend0")._image = image_create_render_target(config_get_texture_res_x(), config_get_texture_res_y(), tex_format_t.R8);
  1053. let _texpaint_blend1: image_t = map_get(rts, "texpaint_blend1")._image;
  1054. app_notify_on_next_frame(function (_texpaint_blend1: image_t) {
  1055. image_unload(_texpaint_blend1);
  1056. }, _texpaint_blend1);
  1057. map_get(rts, "texpaint_blend1").width = config_get_texture_res_x();
  1058. map_get(rts, "texpaint_blend1").height = config_get_texture_res_y();
  1059. map_get(rts, "texpaint_blend1")._image = image_create_render_target(config_get_texture_res_x(), config_get_texture_res_y(), tex_format_t.R8);
  1060. context_raw.brush_blend_dirty = true;
  1061. if (map_get(rts, "texpaint_blur") != null) {
  1062. let _texpaint_blur: image_t = map_get(rts, "texpaint_blur")._image;
  1063. app_notify_on_next_frame(function (_texpaint_blur: image_t) {
  1064. image_unload(_texpaint_blur);
  1065. }, _texpaint_blur);
  1066. let size_x: f32 = math_floor(config_get_texture_res_x() * 0.95);
  1067. let size_y: f32 = math_floor(config_get_texture_res_y() * 0.95);
  1068. map_get(rts, "texpaint_blur").width = size_x;
  1069. map_get(rts, "texpaint_blur").height = size_y;
  1070. map_get(rts, "texpaint_blur")._image = image_create_render_target(size_x, size_y);
  1071. }
  1072. if (render_path_paint_live_layer != null) {
  1073. slot_layer_resize_and_set_bits(render_path_paint_live_layer);
  1074. }
  1075. ///if (krom_direct3d12 || krom_vulkan || krom_metal)
  1076. render_path_raytrace_ready = false; // Rebuild baketex
  1077. ///end
  1078. context_raw.ddirty = 2;
  1079. }
  1080. function base_set_layer_bits() {
  1081. for (let i: i32 = 0; i < project_layers.length; ++i) {
  1082. let l: slot_layer_t = project_layers[i];
  1083. slot_layer_resize_and_set_bits(l);
  1084. }
  1085. for (let i: i32 = 0; i < history_undo_layers.length; ++i) {
  1086. let l: slot_layer_t = history_undo_layers[i];
  1087. slot_layer_resize_and_set_bits(l);
  1088. }
  1089. }
  1090. function base_make_merge_pipe(red: bool, green: bool, blue: bool, alpha: bool): pipeline_t {
  1091. let pipe: pipeline_t = g4_pipeline_create();
  1092. pipe.vertex_shader = sys_get_shader("pass.vert");
  1093. pipe.fragment_shader = sys_get_shader("layer_merge.frag");
  1094. let vs: vertex_struct_t = g4_vertex_struct_create();
  1095. g4_vertex_struct_add(vs, "pos", vertex_data_t.F32_2X);
  1096. pipe.input_layout = [vs];
  1097. pipe.color_write_masks_red = [red];
  1098. pipe.color_write_masks_green = [green];
  1099. pipe.color_write_masks_blue = [blue];
  1100. pipe.color_write_masks_alpha = [alpha];
  1101. g4_pipeline_compile(pipe);
  1102. return pipe;
  1103. }
  1104. ///end
  1105. function base_make_pipe() {
  1106. ///if (is_paint || is_sculpt)
  1107. base_pipe_merge = base_make_merge_pipe(true, true, true, true);
  1108. base_pipe_merge_r = base_make_merge_pipe(true, false, false, false);
  1109. base_pipe_merge_g = base_make_merge_pipe(false, true, false, false);
  1110. base_pipe_merge_b = base_make_merge_pipe(false, false, true, false);
  1111. base_pipe_merge_a = base_make_merge_pipe(false, false, false, true);
  1112. base_tex0 =g4_pipeline_get_tex_unit(base_pipe_merge, "tex0"); // Always binding texpaint.a for blending
  1113. base_tex1 =g4_pipeline_get_tex_unit(base_pipe_merge, "tex1");
  1114. base_texmask =g4_pipeline_get_tex_unit(base_pipe_merge, "texmask");
  1115. base_texa =g4_pipeline_get_tex_unit(base_pipe_merge, "texa");
  1116. base_opac =g4_pipeline_get_const_loc(base_pipe_merge, "opac");
  1117. base_blending =g4_pipeline_get_const_loc(base_pipe_merge, "blending");
  1118. ///end
  1119. {
  1120. base_pipe_copy = g4_pipeline_create();
  1121. base_pipe_copy.vertex_shader = sys_get_shader("layer_view.vert");
  1122. base_pipe_copy.fragment_shader = sys_get_shader("layer_copy.frag");
  1123. let vs: vertex_struct_t = g4_vertex_struct_create();
  1124. g4_vertex_struct_add(vs, "pos", vertex_data_t.F32_3X);
  1125. g4_vertex_struct_add(vs, "tex", vertex_data_t.F32_2X);
  1126. g4_vertex_struct_add(vs, "col", vertex_data_t.U8_4X_NORM);
  1127. base_pipe_copy.input_layout = [vs];
  1128. g4_pipeline_compile(base_pipe_copy);
  1129. }
  1130. {
  1131. base_pipe_copy_bgra = g4_pipeline_create();
  1132. base_pipe_copy_bgra.vertex_shader = sys_get_shader("layer_view.vert");
  1133. base_pipe_copy_bgra.fragment_shader = sys_get_shader("layer_copy_bgra.frag");
  1134. let vs: vertex_struct_t = g4_vertex_struct_create();
  1135. g4_vertex_struct_add(vs, "pos", vertex_data_t.F32_3X);
  1136. g4_vertex_struct_add(vs, "tex", vertex_data_t.F32_2X);
  1137. g4_vertex_struct_add(vs, "col", vertex_data_t.U8_4X_NORM);
  1138. base_pipe_copy_bgra.input_layout = [vs];
  1139. g4_pipeline_compile(base_pipe_copy_bgra);
  1140. }
  1141. ///if (krom_metal || krom_vulkan || krom_direct3d12)
  1142. {
  1143. base_pipe_copy8 = g4_pipeline_create();
  1144. base_pipe_copy8.vertex_shader = sys_get_shader("layer_view.vert");
  1145. base_pipe_copy8.fragment_shader = sys_get_shader("layer_copy.frag");
  1146. let vs: vertex_struct_t = g4_vertex_struct_create();
  1147. g4_vertex_struct_add(vs, "pos", vertex_data_t.F32_3X);
  1148. g4_vertex_struct_add(vs, "tex", vertex_data_t.F32_2X);
  1149. g4_vertex_struct_add(vs, "col", vertex_data_t.U8_4X_NORM);
  1150. base_pipe_copy8.input_layout = [vs];
  1151. base_pipe_copy8.color_attachment_count = 1;
  1152. base_pipe_copy8.color_attachments[0] = tex_format_t.R8;
  1153. g4_pipeline_compile(base_pipe_copy8);
  1154. }
  1155. {
  1156. base_pipe_copy128 = g4_pipeline_create();
  1157. base_pipe_copy128.vertex_shader = sys_get_shader("layer_view.vert");
  1158. base_pipe_copy128.fragment_shader = sys_get_shader("layer_copy.frag");
  1159. let vs: vertex_struct_t = g4_vertex_struct_create();
  1160. g4_vertex_struct_add(vs, "pos", vertex_data_t.F32_3X);
  1161. g4_vertex_struct_add(vs, "tex", vertex_data_t.F32_2X);
  1162. g4_vertex_struct_add(vs, "col", vertex_data_t.U8_4X_NORM);
  1163. base_pipe_copy128.input_layout = [vs];
  1164. base_pipe_copy128.color_attachment_count = 1;
  1165. base_pipe_copy128.color_attachments[0] = tex_format_t.RGBA128;
  1166. g4_pipeline_compile(base_pipe_copy128);
  1167. }
  1168. ///else
  1169. base_pipe_copy8 = base_pipe_copy;
  1170. base_pipe_copy128 = base_pipe_copy;
  1171. ///end
  1172. ///if (is_paint || is_sculpt)
  1173. {
  1174. base_pipe_invert8 = g4_pipeline_create();
  1175. base_pipe_invert8.vertex_shader = sys_get_shader("layer_view.vert");
  1176. base_pipe_invert8.fragment_shader = sys_get_shader("layer_invert.frag");
  1177. let vs: vertex_struct_t = g4_vertex_struct_create();
  1178. g4_vertex_struct_add(vs, "pos", vertex_data_t.F32_3X);
  1179. g4_vertex_struct_add(vs, "tex", vertex_data_t.F32_2X);
  1180. g4_vertex_struct_add(vs, "col", vertex_data_t.U8_4X_NORM);
  1181. base_pipe_invert8.input_layout = [vs];
  1182. base_pipe_invert8.color_attachment_count = 1;
  1183. base_pipe_invert8.color_attachments[0] = tex_format_t.R8;
  1184. g4_pipeline_compile(base_pipe_invert8);
  1185. }
  1186. {
  1187. base_pipe_apply_mask = g4_pipeline_create();
  1188. base_pipe_apply_mask.vertex_shader = sys_get_shader("pass.vert");
  1189. base_pipe_apply_mask.fragment_shader = sys_get_shader("mask_apply.frag");
  1190. let vs: vertex_struct_t = g4_vertex_struct_create();
  1191. g4_vertex_struct_add(vs, "pos", vertex_data_t.F32_2X);
  1192. base_pipe_apply_mask.input_layout = [vs];
  1193. g4_pipeline_compile(base_pipe_apply_mask);
  1194. base_tex0_mask = g4_pipeline_get_tex_unit(base_pipe_apply_mask, "tex0");
  1195. base_texa_mask = g4_pipeline_get_tex_unit(base_pipe_apply_mask, "texa");
  1196. }
  1197. {
  1198. base_pipe_merge_mask = g4_pipeline_create();
  1199. base_pipe_merge_mask.vertex_shader = sys_get_shader("pass.vert");
  1200. base_pipe_merge_mask.fragment_shader = sys_get_shader("mask_merge.frag");
  1201. let vs: vertex_struct_t = g4_vertex_struct_create();
  1202. g4_vertex_struct_add(vs, "pos", vertex_data_t.F32_2X);
  1203. base_pipe_merge_mask.input_layout = [vs];
  1204. g4_pipeline_compile(base_pipe_merge_mask);
  1205. base_tex0_merge_mask = g4_pipeline_get_tex_unit(base_pipe_merge_mask, "tex0");
  1206. base_texa_merge_mask = g4_pipeline_get_tex_unit(base_pipe_merge_mask, "texa");
  1207. base_opac_merge_mask = g4_pipeline_get_const_loc(base_pipe_merge_mask, "opac");
  1208. base_blending_merge_mask = g4_pipeline_get_const_loc(base_pipe_merge_mask, "blending");
  1209. }
  1210. {
  1211. base_pipe_colorid_to_mask = g4_pipeline_create();
  1212. base_pipe_colorid_to_mask.vertex_shader = sys_get_shader("pass.vert");
  1213. base_pipe_colorid_to_mask.fragment_shader = sys_get_shader("mask_colorid.frag");
  1214. let vs: vertex_struct_t = g4_vertex_struct_create();
  1215. g4_vertex_struct_add(vs, "pos", vertex_data_t.F32_2X);
  1216. base_pipe_colorid_to_mask.input_layout = [vs];
  1217. g4_pipeline_compile(base_pipe_colorid_to_mask);
  1218. base_texpaint_colorid = g4_pipeline_get_tex_unit(base_pipe_colorid_to_mask, "texpaint_colorid");
  1219. base_tex_colorid = g4_pipeline_get_tex_unit(base_pipe_colorid_to_mask, "texcolorid");
  1220. }
  1221. ///end
  1222. ///if is_lab
  1223. {
  1224. base_pipe_copy_r = g4_pipeline_create();
  1225. base_pipe_copy_r.vertex_shader = sys_get_shader("layer_view.vert");
  1226. base_pipe_copy_r.fragment_shader = sys_get_shader("layer_copy.frag");
  1227. let vs: vertex_struct_t = g4_vertex_struct_create();
  1228. g4_vertex_struct_add(vs, "pos", vertex_data_t.F32_3X);
  1229. g4_vertex_struct_add(vs, "tex", vertex_data_t.F32_2X);
  1230. g4_vertex_struct_add(vs, "col", vertex_data_t.U8_4X_NORM);
  1231. base_pipe_copy_r.input_layout = [vs];
  1232. base_pipe_copy_r.color_write_masks_green = [false];
  1233. base_pipe_copy_r.color_write_masks_blue = [false];
  1234. base_pipe_copy_r.color_write_masks_alpha = [false];
  1235. g4_pipeline_compile(base_pipe_copy_r);
  1236. }
  1237. {
  1238. base_pipe_copy_g = g4_pipeline_create();
  1239. base_pipe_copy_g.vertex_shader = sys_get_shader("layer_view.vert");
  1240. base_pipe_copy_g.fragment_shader = sys_get_shader("layer_copy.frag");
  1241. let vs: vertex_struct_t = g4_vertex_struct_create();
  1242. g4_vertex_struct_add(vs, "pos", vertex_data_t.F32_3X);
  1243. g4_vertex_struct_add(vs, "tex", vertex_data_t.F32_2X);
  1244. g4_vertex_struct_add(vs, "col", vertex_data_t.U8_4X_NORM);
  1245. base_pipe_copy_g.input_layout = [vs];
  1246. base_pipe_copy_g.color_write_masks_red = [false];
  1247. base_pipe_copy_g.color_write_masks_blue = [false];
  1248. base_pipe_copy_g.color_write_masks_alpha = [false];
  1249. g4_pipeline_compile(base_pipe_copy_g);
  1250. }
  1251. {
  1252. base_pipe_copy_b = g4_pipeline_create();
  1253. base_pipe_copy_b.vertex_shader = sys_get_shader("layer_view.vert");
  1254. base_pipe_copy_b.fragment_shader = sys_get_shader("layer_copy.frag");
  1255. let vs: vertex_struct_t = g4_vertex_struct_create();
  1256. g4_vertex_struct_add(vs, "pos", vertex_data_t.F32_3X);
  1257. g4_vertex_struct_add(vs, "tex", vertex_data_t.F32_2X);
  1258. g4_vertex_struct_add(vs, "col", vertex_data_t.U8_4X_NORM);
  1259. base_pipe_copy_b.input_layout = [vs];
  1260. base_pipe_copy_b.color_write_masks_red = [false];
  1261. base_pipe_copy_b.color_write_masks_green = [false];
  1262. base_pipe_copy_b.color_write_masks_alpha = [false];
  1263. g4_pipeline_compile(base_pipe_copy_b);
  1264. }
  1265. {
  1266. base_pipe_inpaint_preview = g4_pipeline_create();
  1267. base_pipe_inpaint_preview.vertex_shader = sys_get_shader("pass.vert");
  1268. base_pipe_inpaint_preview.fragment_shader = sys_get_shader("inpaint_preview.frag");
  1269. let vs: vertex_struct_t = g4_vertex_struct_create();
  1270. g4_vertex_struct_add(vs, "pos", vertex_data_t.F32_2X);
  1271. base_pipe_inpaint_preview.input_layout = [vs];
  1272. g4_pipeline_compile(base_pipe_inpaint_preview);
  1273. base_tex0_inpaint_preview = g4_pipeline_get_tex_unit(base_pipe_inpaint_preview, "tex0");
  1274. base_texa_inpaint_preview = g4_pipeline_get_tex_unit(base_pipe_inpaint_preview, "texa");
  1275. }
  1276. ///end
  1277. }
  1278. function base_make_pipe_copy_rgb() {
  1279. base_pipe_copy_rgb = g4_pipeline_create();
  1280. base_pipe_copy_rgb.vertex_shader = sys_get_shader("layer_view.vert");
  1281. base_pipe_copy_rgb.fragment_shader = sys_get_shader("layer_copy.frag");
  1282. let vs: vertex_struct_t = g4_vertex_struct_create();
  1283. g4_vertex_struct_add(vs, "pos", vertex_data_t.F32_3X);
  1284. g4_vertex_struct_add(vs, "tex", vertex_data_t.F32_2X);
  1285. g4_vertex_struct_add(vs, "col", vertex_data_t.U8_4X_NORM);
  1286. base_pipe_copy_rgb.input_layout = [vs];
  1287. base_pipe_copy_rgb.color_write_masks_alpha = [false];
  1288. g4_pipeline_compile(base_pipe_copy_rgb);
  1289. }
  1290. ///if is_lab
  1291. function base_make_pipe_copy_a() {
  1292. base_pipe_copy_a = g4_pipeline_create();
  1293. base_pipe_copy_a.vertex_shader = sys_get_shader("pass.vert");
  1294. base_pipe_copy_a.fragment_shader = sys_get_shader("layer_copy_rrrr.frag");
  1295. let vs: vertex_struct_t = g4_vertex_struct_create();
  1296. g4_vertex_struct_add(vs, "pos", vertex_data_t.F32_2X);
  1297. base_pipe_copy_a.input_layout = [vs];
  1298. base_pipe_copy_a.color_write_masks_red = [false];
  1299. base_pipe_copy_a.color_write_masks_green = [false];
  1300. base_pipe_copy_a.color_write_masks_blue = [false];
  1301. g4_pipeline_compile(base_pipe_copy_a);
  1302. base_pipe_copy_a_tex = g4_pipeline_get_tex_unit(base_pipe_copy_a, "tex");
  1303. }
  1304. ///end
  1305. function base_make_cursor_pipe() {
  1306. base_pipe_cursor = g4_pipeline_create();
  1307. base_pipe_cursor.vertex_shader = sys_get_shader("cursor.vert");
  1308. base_pipe_cursor.fragment_shader = sys_get_shader("cursor.frag");
  1309. let vs: vertex_struct_t = g4_vertex_struct_create();
  1310. ///if (krom_metal || krom_vulkan)
  1311. g4_vertex_struct_add(vs, "tex", vertex_data_t.I16_2X_NORM);
  1312. ///else
  1313. g4_vertex_struct_add(vs, "pos", vertex_data_t.I16_4X_NORM);
  1314. g4_vertex_struct_add(vs, "nor", vertex_data_t.I16_2X_NORM);
  1315. g4_vertex_struct_add(vs, "tex", vertex_data_t.I16_2X_NORM);
  1316. ///end
  1317. base_pipe_cursor.input_layout = [vs];
  1318. base_pipe_cursor.blend_source = blend_factor_t.SOURCE_ALPHA;
  1319. base_pipe_cursor.blend_dest = blend_factor_t.INV_SOURCE_ALPHA;
  1320. base_pipe_cursor.depth_write = false;
  1321. base_pipe_cursor.depth_mode = compare_mode_t.ALWAYS;
  1322. g4_pipeline_compile(base_pipe_cursor);
  1323. base_cursor_vp = g4_pipeline_get_const_loc(base_pipe_cursor, "VP");
  1324. base_cursor_inv_vp = g4_pipeline_get_const_loc(base_pipe_cursor, "invVP");
  1325. base_cursor_mouse = g4_pipeline_get_const_loc(base_pipe_cursor, "mouse");
  1326. base_cursor_tex_step = g4_pipeline_get_const_loc(base_pipe_cursor, "texStep");
  1327. base_cursor_radius = g4_pipeline_get_const_loc(base_pipe_cursor, "radius");
  1328. base_cursor_camera_right = g4_pipeline_get_const_loc(base_pipe_cursor, "cameraRight");
  1329. base_cursor_tint = g4_pipeline_get_const_loc(base_pipe_cursor, "tint");
  1330. base_cursor_gbufferd = g4_pipeline_get_tex_unit(base_pipe_cursor, "gbufferD");
  1331. base_cursor_tex = g4_pipeline_get_tex_unit(base_pipe_cursor, "tex");
  1332. }
  1333. function base_make_temp_img() {
  1334. ///if (is_paint || is_sculpt)
  1335. let l: slot_layer_t = project_layers[0];
  1336. ///end
  1337. ///if is_lab
  1338. let l: any = brush_output_node_inst;
  1339. ///end
  1340. if (base_temp_image != null && (base_temp_image.width != l.texpaint.width || base_temp_image.height != l.texpaint.height || base_temp_image.format != l.texpaint.format)) {
  1341. let _temptex0: render_target_t = map_get(render_path_render_targets, "temptex0");
  1342. app_notify_on_next_frame(function (_temptex0: render_target_t) {
  1343. render_target_unload(_temptex0);
  1344. }, _temptex0);
  1345. map_delete(render_path_render_targets, "temptex0");
  1346. base_temp_image = null;
  1347. }
  1348. if (base_temp_image == null) {
  1349. ///if (is_paint || is_sculpt)
  1350. let format: string = base_bits_handle.position == texture_bits_t.BITS8 ? "RGBA32" :
  1351. base_bits_handle.position == texture_bits_t.BITS16 ? "RGBA64" :
  1352. "RGBA128";
  1353. ///end
  1354. ///if is_lab
  1355. let format: string = "RGBA32";
  1356. ///end
  1357. let t: render_target_t = render_target_create();
  1358. t.name = "temptex0";
  1359. t.width = l.texpaint.width;
  1360. t.height = l.texpaint.height;
  1361. t.format = format;
  1362. let rt: render_target_t = render_path_create_render_target(t);
  1363. base_temp_image = rt._image;
  1364. }
  1365. }
  1366. ///if (is_paint || is_sculpt)
  1367. function base_make_temp_mask_img() {
  1368. if (base_temp_mask_image != null && (base_temp_mask_image.width != config_get_texture_res_x() || base_temp_mask_image.height != config_get_texture_res_y())) {
  1369. let _temp_mask_image: image_t = base_temp_mask_image;
  1370. app_notify_on_next_frame(function (_temp_mask_image: image_t) {
  1371. image_unload(_temp_mask_image);
  1372. }, _temp_mask_image);
  1373. base_temp_mask_image = null;
  1374. }
  1375. if (base_temp_mask_image == null) {
  1376. base_temp_mask_image = image_create_render_target(config_get_texture_res_x(), config_get_texture_res_y(), tex_format_t.R8);
  1377. }
  1378. }
  1379. ///end
  1380. function base_make_export_img() {
  1381. ///if (is_paint || is_sculpt)
  1382. let l: slot_layer_t = project_layers[0];
  1383. ///end
  1384. ///if is_lab
  1385. let l: any = brush_output_node_inst;
  1386. ///end
  1387. if (base_expa != null && (base_expa.width != l.texpaint.width || base_expa.height != l.texpaint.height || base_expa.format != l.texpaint.format)) {
  1388. let _expa: image_t = base_expa;
  1389. let _expb: image_t = base_expb;
  1390. let _expc: image_t = base_expc;
  1391. app_notify_on_next_frame(function (_expa: image_t) {
  1392. image_unload(_expa);
  1393. }, _expa);
  1394. app_notify_on_next_frame(function (_expb: image_t) {
  1395. image_unload(_expb);
  1396. }, _expb);
  1397. app_notify_on_next_frame(function (_expc: image_t) {
  1398. image_unload(_expc);
  1399. }, _expc);
  1400. base_expa = null;
  1401. base_expb = null;
  1402. base_expc = null;
  1403. map_delete(render_path_render_targets, "expa");
  1404. map_delete(render_path_render_targets, "expb");
  1405. map_delete(render_path_render_targets, "expc");
  1406. }
  1407. if (base_expa == null) {
  1408. ///if (is_paint || is_sculpt)
  1409. let format: string = base_bits_handle.position == texture_bits_t.BITS8 ? "RGBA32" :
  1410. base_bits_handle.position == texture_bits_t.BITS16 ? "RGBA64" :
  1411. "RGBA128";
  1412. ///end
  1413. ///if is_lab
  1414. let format: string = "RGBA32";
  1415. ///end
  1416. {
  1417. let t: render_target_t = render_target_create();
  1418. t.name = "expa";
  1419. t.width = l.texpaint.width;
  1420. t.height = l.texpaint.height;
  1421. t.format = format;
  1422. let rt: render_target_t = render_path_create_render_target(t);
  1423. base_expa = rt._image;
  1424. }
  1425. {
  1426. let t: render_target_t = render_target_create();
  1427. t.name = "expb";
  1428. t.width = l.texpaint.width;
  1429. t.height = l.texpaint.height;
  1430. t.format = format;
  1431. let rt: render_target_t = render_path_create_render_target(t);
  1432. base_expb = rt._image;
  1433. }
  1434. {
  1435. let t: render_target_t = render_target_create();
  1436. t.name = "expc";
  1437. t.width = l.texpaint.width;
  1438. t.height = l.texpaint.height;
  1439. t.format = format;
  1440. let rt: render_target_t = render_path_create_render_target(t);
  1441. base_expc = rt._image;
  1442. }
  1443. }
  1444. }
  1445. ///if (is_paint || is_sculpt)
  1446. function base_duplicate_layer(l: slot_layer_t) {
  1447. if (!slot_layer_is_group(l)) {
  1448. let new_layer: slot_layer_t = slot_layer_duplicate(l);
  1449. context_set_layer(new_layer);
  1450. let masks: slot_layer_t[] = slot_layer_get_masks(l, false);
  1451. if (masks != null) {
  1452. for (let i: i32 = 0; i < masks.length; ++i) {
  1453. let m: slot_layer_t = masks[i];
  1454. m = slot_layer_duplicate(m);
  1455. m.parent = new_layer;
  1456. array_remove(project_layers, m);
  1457. array_insert(project_layers, array_index_of(project_layers, new_layer), m);
  1458. }
  1459. }
  1460. context_set_layer(new_layer);
  1461. }
  1462. else {
  1463. let new_group: slot_layer_t = base_new_group();
  1464. array_remove(project_layers, new_group);
  1465. array_insert(project_layers, array_index_of(project_layers, l) + 1, new_group);
  1466. // group.show_panel = true;
  1467. for (let i: i32 = 0; i < slot_layer_get_children(l).length; ++i) {
  1468. let c: slot_layer_t = slot_layer_get_children(l)[i];
  1469. let masks: slot_layer_t[] = slot_layer_get_masks(c, false);
  1470. let new_layer: slot_layer_t = slot_layer_duplicate(c);
  1471. new_layer.parent = new_group;
  1472. array_remove(project_layers, new_layer);
  1473. array_insert(project_layers, array_index_of(project_layers, new_group), new_layer);
  1474. if (masks != null) {
  1475. for (let i: i32 = 0; i < masks.length; ++i) {
  1476. let m: slot_layer_t = masks[i];
  1477. let new_mask: slot_layer_t = slot_layer_duplicate(m);
  1478. new_mask.parent = new_layer;
  1479. array_remove(project_layers, new_mask);
  1480. array_insert(project_layers, array_index_of(project_layers, new_layer), new_mask);
  1481. }
  1482. }
  1483. }
  1484. let group_masks: slot_layer_t[] = slot_layer_get_masks(l);
  1485. if (group_masks != null) {
  1486. for (let i: i32 = 0; i < group_masks.length; ++i) {
  1487. let m: slot_layer_t = group_masks[i];
  1488. let new_mask: slot_layer_t = slot_layer_duplicate(m);
  1489. new_mask.parent = new_group;
  1490. array_remove(project_layers, new_mask);
  1491. array_insert(project_layers, array_index_of(project_layers, new_group), new_mask);
  1492. }
  1493. }
  1494. context_set_layer(new_group);
  1495. }
  1496. }
  1497. function base_apply_masks(l: slot_layer_t) {
  1498. let masks: slot_layer_t[] = slot_layer_get_masks(l);
  1499. if (masks != null) {
  1500. for (let i: i32 = 0; i < masks.length - 1; ++i) {
  1501. base_merge_layer(masks[i + 1], masks[i]);
  1502. slot_layer_delete(masks[i]);
  1503. }
  1504. slot_layer_apply_mask(masks[masks.length - 1]);
  1505. context_raw.layer_preview_dirty = true;
  1506. }
  1507. }
  1508. function base_merge_down() {
  1509. let l1: slot_layer_t = context_raw.layer;
  1510. if (slot_layer_is_group(l1)) {
  1511. l1 = base_merge_group(l1);
  1512. }
  1513. else if (slot_layer_has_masks(l1)) { // It is a layer
  1514. base_apply_masks(l1);
  1515. context_set_layer(l1);
  1516. }
  1517. let l0: slot_layer_t = project_layers[array_index_of(project_layers, l1) - 1];
  1518. if (slot_layer_is_group(l0)) {
  1519. l0 = base_merge_group(l0);
  1520. }
  1521. else if (slot_layer_has_masks(l0)) { // It is a layer
  1522. base_apply_masks(l0);
  1523. context_set_layer(l0);
  1524. }
  1525. base_merge_layer(l0, l1);
  1526. slot_layer_delete(l1);
  1527. context_set_layer(l0);
  1528. context_raw.layer_preview_dirty = true;
  1529. }
  1530. function base_merge_group(l: slot_layer_t) {
  1531. if (!slot_layer_is_group(l)) {
  1532. return null;
  1533. }
  1534. let children: slot_layer_t[] = slot_layer_get_children(l);
  1535. if (children.length == 1 && slot_layer_has_masks(children[0], false)) {
  1536. base_apply_masks(children[0]);
  1537. }
  1538. for (let i: i32 = 0; i < children.length - 1; ++i) {
  1539. context_set_layer(children[children.length - 1 - i]);
  1540. history_merge_layers();
  1541. base_merge_down();
  1542. }
  1543. // Now apply the group masks
  1544. let masks: slot_layer_t[] = slot_layer_get_masks(l);
  1545. if (masks != null) {
  1546. for (let i: i32 = 0; i < masks.length - 1; ++i) {
  1547. base_merge_layer(masks[i + 1], masks[i]);
  1548. slot_layer_delete(masks[i]);
  1549. }
  1550. base_apply_mask(children[0], masks[masks.length - 1]);
  1551. }
  1552. children[0].parent = null;
  1553. children[0].name = l.name;
  1554. if (children[0].fill_layer != null) {
  1555. slot_layer_to_paint_layer(children[0]);
  1556. }
  1557. slot_layer_delete(l);
  1558. return children[0];
  1559. }
  1560. function base_merge_layer(l0 : slot_layer_t, l1: slot_layer_t, use_mask: bool = false) {
  1561. if (!l1.visible || slot_layer_is_group(l1)) {
  1562. return;
  1563. }
  1564. if (base_pipe_merge == null) {
  1565. base_make_pipe();
  1566. }
  1567. base_make_temp_img();
  1568. if (const_data_screen_aligned_vb == null) {
  1569. const_data_create_screen_aligned_data();
  1570. }
  1571. g2_begin(base_temp_image); // Copy to temp
  1572. g2_set_pipeline(base_pipe_copy);
  1573. g2_draw_image(l0.texpaint, 0, 0);
  1574. g2_set_pipeline(null);
  1575. g2_end();
  1576. let empty: image_t = map_get(render_path_render_targets, "empty_white")._image;
  1577. let mask: image_t = empty;
  1578. let l1masks: slot_layer_t[] = use_mask ? slot_layer_get_masks(l1) : null;
  1579. if (l1masks != null) {
  1580. // for (let i: i32 = 1; i < l1masks.length - 1; ++i) {
  1581. // mergeLayer(l1masks[i + 1], l1masks[i]);
  1582. // }
  1583. mask = l1masks[0].texpaint;
  1584. }
  1585. if (slot_layer_is_mask(l1)) {
  1586. g4_begin(l0.texpaint);
  1587. g4_set_pipeline(base_pipe_merge_mask);
  1588. g4_set_tex(base_tex0_merge_mask, l1.texpaint);
  1589. g4_set_tex(base_texa_merge_mask, base_temp_image);
  1590. g4_set_float(base_opac_merge_mask, slot_layer_get_opacity(l1));
  1591. g4_set_int(base_blending_merge_mask, l1.blending);
  1592. g4_set_vertex_buffer(const_data_screen_aligned_vb);
  1593. g4_set_index_buffer(const_data_screen_aligned_ib);
  1594. g4_draw();
  1595. g4_end();
  1596. }
  1597. if (slot_layer_is_layer(l1)) {
  1598. if (l1.paint_base) {
  1599. g4_begin(l0.texpaint);
  1600. g4_set_pipeline(base_pipe_merge);
  1601. g4_set_tex(base_tex0, l1.texpaint);
  1602. g4_set_tex(base_tex1, empty);
  1603. g4_set_tex(base_texmask, mask);
  1604. g4_set_tex(base_texa, base_temp_image);
  1605. g4_set_float(base_opac, slot_layer_get_opacity(l1));
  1606. g4_set_int(base_blending, l1.blending);
  1607. g4_set_vertex_buffer(const_data_screen_aligned_vb);
  1608. g4_set_index_buffer(const_data_screen_aligned_ib);
  1609. g4_draw();
  1610. g4_end();
  1611. }
  1612. ///if is_paint
  1613. g2_begin(base_temp_image);
  1614. g2_set_pipeline(base_pipe_copy);
  1615. g2_draw_image(l0.texpaint_nor, 0, 0);
  1616. g2_set_pipeline(null);
  1617. g2_end();
  1618. if (l1.paint_nor) {
  1619. g4_begin(l0.texpaint_nor);
  1620. g4_set_pipeline(base_pipe_merge);
  1621. g4_set_tex(base_tex0, l1.texpaint);
  1622. g4_set_tex(base_tex1, l1.texpaint_nor);
  1623. g4_set_tex(base_texmask, mask);
  1624. g4_set_tex(base_texa, base_temp_image);
  1625. g4_set_float(base_opac, slot_layer_get_opacity(l1));
  1626. g4_set_int(base_blending, l1.paint_nor_blend ? -2 : -1);
  1627. g4_set_vertex_buffer(const_data_screen_aligned_vb);
  1628. g4_set_index_buffer(const_data_screen_aligned_ib);
  1629. g4_draw();
  1630. g4_end();
  1631. }
  1632. g2_begin(base_temp_image);
  1633. g2_set_pipeline(base_pipe_copy);
  1634. g2_draw_image(l0.texpaint_pack, 0, 0);
  1635. g2_set_pipeline(null);
  1636. g2_end();
  1637. if (l1.paint_occ || l1.paint_rough || l1.paint_met || l1.paint_height) {
  1638. if (l1.paint_occ && l1.paint_rough && l1.paint_met && l1.paint_height) {
  1639. base_commands_merge_pack(base_pipe_merge, l0.texpaint_pack, l1.texpaint, l1.texpaint_pack, slot_layer_get_opacity(l1), mask, l1.paint_height_blend ? -3 : -1);
  1640. }
  1641. else {
  1642. if (l1.paint_occ) {
  1643. base_commands_merge_pack(base_pipe_merge_r, l0.texpaint_pack, l1.texpaint, l1.texpaint_pack, slot_layer_get_opacity(l1), mask);
  1644. }
  1645. if (l1.paint_rough) {
  1646. base_commands_merge_pack(base_pipe_merge_g, l0.texpaint_pack, l1.texpaint, l1.texpaint_pack, slot_layer_get_opacity(l1), mask);
  1647. }
  1648. if (l1.paint_met) {
  1649. base_commands_merge_pack(base_pipe_merge_b, l0.texpaint_pack, l1.texpaint, l1.texpaint_pack, slot_layer_get_opacity(l1), mask);
  1650. }
  1651. }
  1652. }
  1653. ///end
  1654. }
  1655. }
  1656. function base_flatten(height_to_normal: bool = false, layers: slot_layer_t[] = null): any {
  1657. if (layers == null) {
  1658. layers = project_layers;
  1659. }
  1660. base_make_temp_img();
  1661. base_make_export_img();
  1662. if (base_pipe_merge == null) {
  1663. base_make_pipe();
  1664. }
  1665. if (const_data_screen_aligned_vb == null) {
  1666. const_data_create_screen_aligned_data();
  1667. }
  1668. let empty: image_t = map_get(render_path_render_targets, "empty_white")._image;
  1669. // Clear export layer
  1670. g4_begin(base_expa);
  1671. g4_clear(color_from_floats(0.0, 0.0, 0.0, 0.0));
  1672. g4_end();
  1673. g4_begin(base_expb);
  1674. g4_clear(color_from_floats(0.5, 0.5, 1.0, 0.0));
  1675. g4_end();
  1676. g4_begin(base_expc);
  1677. g4_clear(color_from_floats(1.0, 0.0, 0.0, 0.0));
  1678. g4_end();
  1679. // Flatten layers
  1680. for (let i: i32 = 0; i < layers.length; ++i) {
  1681. let l1: slot_layer_t = layers[i];
  1682. if (!slot_layer_is_visible(l1)) {
  1683. continue;
  1684. }
  1685. if (!slot_layer_is_layer(l1)) {
  1686. continue;
  1687. }
  1688. let mask: image_t = empty;
  1689. let l1masks: slot_layer_t[] = slot_layer_get_masks(l1);
  1690. if (l1masks != null) {
  1691. if (l1masks.length > 1) {
  1692. base_make_temp_mask_img();
  1693. g2_begin(base_temp_mask_image);
  1694. g2_clear(0x00000000);
  1695. g2_end();
  1696. let l1: any = { texpaint: base_temp_mask_image };
  1697. for (let i: i32 = 0; i < l1masks.length; ++i) {
  1698. base_merge_layer(l1, l1masks[i]);
  1699. }
  1700. mask = base_temp_mask_image;
  1701. }
  1702. else {
  1703. mask = l1masks[0].texpaint;
  1704. }
  1705. }
  1706. if (l1.paint_base) {
  1707. g2_begin(base_temp_image); // Copy to temp
  1708. g2_set_pipeline(base_pipe_copy);
  1709. g2_draw_image(base_expa, 0, 0);
  1710. g2_set_pipeline(null);
  1711. g2_end();
  1712. g4_begin(base_expa);
  1713. g4_set_pipeline(base_pipe_merge);
  1714. g4_set_tex(base_tex0, l1.texpaint);
  1715. g4_set_tex(base_tex1, empty);
  1716. g4_set_tex(base_texmask, mask);
  1717. g4_set_tex(base_texa, base_temp_image);
  1718. g4_set_float(base_opac, slot_layer_get_opacity(l1));
  1719. g4_set_int(base_blending, layers.length > 1 ? l1.blending : 0);
  1720. g4_set_vertex_buffer(const_data_screen_aligned_vb);
  1721. g4_set_index_buffer(const_data_screen_aligned_ib);
  1722. g4_draw();
  1723. g4_end();
  1724. }
  1725. ///if is_paint
  1726. if (l1.paint_nor) {
  1727. g2_begin(base_temp_image);
  1728. g2_set_pipeline(base_pipe_copy);
  1729. g2_draw_image(base_expb, 0, 0);
  1730. g2_set_pipeline(null);
  1731. g2_end();
  1732. g4_begin(base_expb);
  1733. g4_set_pipeline(base_pipe_merge);
  1734. g4_set_tex(base_tex0, l1.texpaint);
  1735. g4_set_tex(base_tex1, l1.texpaint_nor);
  1736. g4_set_tex(base_texmask, mask);
  1737. g4_set_tex(base_texa, base_temp_image);
  1738. g4_set_float(base_opac, slot_layer_get_opacity(l1));
  1739. g4_set_int(base_blending, l1.paint_nor_blend ? -2 : -1);
  1740. g4_set_vertex_buffer(const_data_screen_aligned_vb);
  1741. g4_set_index_buffer(const_data_screen_aligned_ib);
  1742. g4_draw();
  1743. g4_end();
  1744. }
  1745. if (l1.paint_occ || l1.paint_rough || l1.paint_met || l1.paint_height) {
  1746. g2_begin(base_temp_image);
  1747. g2_set_pipeline(base_pipe_copy);
  1748. g2_draw_image(base_expc, 0, 0);
  1749. g2_set_pipeline(null);
  1750. g2_end();
  1751. if (l1.paint_occ && l1.paint_rough && l1.paint_met && l1.paint_height) {
  1752. base_commands_merge_pack(base_pipe_merge, base_expc, l1.texpaint, l1.texpaint_pack, slot_layer_get_opacity(l1), mask, l1.paint_height_blend ? -3 : -1);
  1753. }
  1754. else {
  1755. if (l1.paint_occ) {
  1756. base_commands_merge_pack(base_pipe_merge_r, base_expc, l1.texpaint, l1.texpaint_pack, slot_layer_get_opacity(l1), mask);
  1757. }
  1758. if (l1.paint_rough) {
  1759. base_commands_merge_pack(base_pipe_merge_g, base_expc, l1.texpaint, l1.texpaint_pack, slot_layer_get_opacity(l1), mask);
  1760. }
  1761. if (l1.paint_met) {
  1762. base_commands_merge_pack(base_pipe_merge_b, base_expc, l1.texpaint, l1.texpaint_pack, slot_layer_get_opacity(l1), mask);
  1763. }
  1764. }
  1765. }
  1766. ///end
  1767. }
  1768. ///if krom_metal
  1769. // Flush command list
  1770. g2_begin(base_expa);
  1771. g2_end();
  1772. g2_begin(base_expb);
  1773. g2_end();
  1774. g2_begin(base_expc);
  1775. g2_end();
  1776. ///end
  1777. let l0: any = { texpaint: base_expa, texpaint_nor: base_expb, texpaint_pack: base_expc };
  1778. // Merge height map into normal map
  1779. if (height_to_normal && make_material_height_used) {
  1780. g2_begin(base_temp_image);
  1781. g2_set_pipeline(base_pipe_copy);
  1782. g2_draw_image(l0.texpaint_nor, 0, 0);
  1783. g2_set_pipeline(null);
  1784. g2_end();
  1785. g4_begin(l0.texpaint_nor);
  1786. g4_set_pipeline(base_pipe_merge);
  1787. g4_set_tex(base_tex0, base_temp_image);
  1788. g4_set_tex(base_tex1, l0.texpaint_pack);
  1789. g4_set_tex(base_texmask, empty);
  1790. g4_set_tex(base_texa, empty);
  1791. g4_set_float(base_opac, 1.0);
  1792. g4_set_int(base_blending, -4);
  1793. g4_set_vertex_buffer(const_data_screen_aligned_vb);
  1794. g4_set_index_buffer(const_data_screen_aligned_ib);
  1795. g4_draw();
  1796. g4_end();
  1797. }
  1798. return l0;
  1799. }
  1800. function base_apply_mask(l: slot_layer_t, m: slot_layer_t) {
  1801. if (!slot_layer_is_layer(l) || !slot_layer_is_mask(m)) {
  1802. return;
  1803. }
  1804. if (base_pipe_merge == null) {
  1805. base_make_pipe();
  1806. }
  1807. base_make_temp_img();
  1808. // Copy layer to temp
  1809. g2_begin(base_temp_image);
  1810. g2_set_pipeline(base_pipe_copy);
  1811. g2_draw_image(l.texpaint, 0, 0);
  1812. g2_set_pipeline(null);
  1813. g2_end();
  1814. // Apply mask
  1815. if (const_data_screen_aligned_vb == null) {
  1816. const_data_create_screen_aligned_data();
  1817. }
  1818. g4_begin(l.texpaint);
  1819. g4_set_pipeline(base_pipe_apply_mask);
  1820. g4_set_tex(base_tex0_mask, base_temp_image);
  1821. g4_set_tex(base_texa_mask, m.texpaint);
  1822. g4_set_vertex_buffer(const_data_screen_aligned_vb);
  1823. g4_set_index_buffer(const_data_screen_aligned_ib);
  1824. g4_draw();
  1825. g4_end();
  1826. }
  1827. function base_commands_merge_pack(pipe: pipeline_t, i0: image_t, i1: image_t, i1pack: image_t, i1mask_opacity: f32, i1texmask: image_t, i1blending: i32 = -1) {
  1828. g4_begin(i0);
  1829. g4_set_pipeline(pipe);
  1830. g4_set_tex(base_tex0, i1);
  1831. g4_set_tex(base_tex1, i1pack);
  1832. g4_set_tex(base_texmask, i1texmask);
  1833. g4_set_tex(base_texa, base_temp_image);
  1834. g4_set_float(base_opac, i1mask_opacity);
  1835. g4_set_int(base_blending, i1blending);
  1836. g4_set_vertex_buffer(const_data_screen_aligned_vb);
  1837. g4_set_index_buffer(const_data_screen_aligned_ib);
  1838. g4_draw();
  1839. g4_end();
  1840. }
  1841. function base_is_fill_material(): bool {
  1842. ///if is_paint
  1843. if (context_raw.tool == workspace_tool_t.MATERIAL) {
  1844. return true;
  1845. }
  1846. ///end
  1847. let m: slot_material_t = context_raw.material;
  1848. for (let i: i32 = 0; i < project_layers.length; ++i) {
  1849. let l: slot_layer_t = project_layers[i];
  1850. if (l.fill_layer == m) {
  1851. return true;
  1852. }
  1853. }
  1854. return false;
  1855. }
  1856. function base_update_fill_layers() {
  1857. let _layer: slot_layer_t = context_raw.layer;
  1858. let _tool: workspace_tool_t = context_raw.tool;
  1859. let _fill_type: i32 = context_raw.fill_type_handle.position;
  1860. let current: image_t = null;
  1861. ///if is_paint
  1862. if (context_raw.tool == workspace_tool_t.MATERIAL) {
  1863. if (render_path_paint_live_layer == null) {
  1864. render_path_paint_live_layer = slot_layer_create("_live");
  1865. }
  1866. current = _g2_current;
  1867. if (current != null) g2_end();
  1868. context_raw.tool = workspace_tool_t.FILL;
  1869. context_raw.fill_type_handle.position = fill_type_t.OBJECT;
  1870. make_material_parse_paint_material(false);
  1871. context_raw.pdirty = 1;
  1872. render_path_paint_use_live_layer(true);
  1873. render_path_paint_commands_paint(false);
  1874. render_path_paint_dilate(true, true);
  1875. render_path_paint_use_live_layer(false);
  1876. context_raw.tool = _tool;
  1877. context_raw.fill_type_handle.position = _fill_type;
  1878. context_raw.pdirty = 0;
  1879. context_raw.rdirty = 2;
  1880. if (current != null) g2_begin(current);
  1881. return;
  1882. }
  1883. ///end
  1884. let has_fill_layer: bool = false;
  1885. let has_fill_mask: bool = false;
  1886. for (let i: i32 = 0; i < project_layers.length; ++i) {
  1887. let l: slot_layer_t = project_layers[i];
  1888. if (slot_layer_is_layer(l) && l.fill_layer == context_raw.material) {
  1889. has_fill_layer = true;
  1890. }
  1891. }
  1892. for (let i: i32 = 0; i < project_layers.length; ++i) {
  1893. let l: slot_layer_t = project_layers[i];
  1894. if (slot_layer_is_mask(l) && l.fill_layer == context_raw.material) {
  1895. has_fill_mask = true;
  1896. }
  1897. }
  1898. if (has_fill_layer || has_fill_mask) {
  1899. current = _g2_current;
  1900. if (current != null) {
  1901. g2_end();
  1902. }
  1903. context_raw.pdirty = 1;
  1904. context_raw.tool = workspace_tool_t.FILL;
  1905. context_raw.fill_type_handle.position = fill_type_t.OBJECT;
  1906. if (has_fill_layer) {
  1907. let first: bool = true;
  1908. for (let i: i32 = 0; i < project_layers.length; ++i) {
  1909. let l: slot_layer_t = project_layers[i];
  1910. if (slot_layer_is_layer(l) && l.fill_layer == context_raw.material) {
  1911. context_raw.layer = l;
  1912. if (first) {
  1913. first = false;
  1914. make_material_parse_paint_material(false);
  1915. }
  1916. base_set_object_mask();
  1917. slot_layer_clear(l);
  1918. render_path_paint_commands_paint(false);
  1919. render_path_paint_dilate(true, true);
  1920. }
  1921. }
  1922. }
  1923. if (has_fill_mask) {
  1924. let first: bool = true;
  1925. for (let i: i32 = 0; i < project_layers.length; ++i) {
  1926. let l: slot_layer_t = project_layers[i];
  1927. if (slot_layer_is_mask(l) && l.fill_layer == context_raw.material) {
  1928. context_raw.layer = l;
  1929. if (first) {
  1930. first = false;
  1931. make_material_parse_paint_material(false);
  1932. }
  1933. base_set_object_mask();
  1934. slot_layer_clear(l);
  1935. render_path_paint_commands_paint(false);
  1936. render_path_paint_dilate(true, true);
  1937. }
  1938. }
  1939. }
  1940. context_raw.pdirty = 0;
  1941. context_raw.ddirty = 2;
  1942. context_raw.rdirty = 2;
  1943. context_raw.layers_preview_dirty = true; // Repaint all layer previews as multiple layers might have changed.
  1944. if (current != null) g2_begin(current);
  1945. context_raw.layer = _layer;
  1946. base_set_object_mask();
  1947. context_raw.tool = _tool;
  1948. context_raw.fill_type_handle.position = _fill_type;
  1949. make_material_parse_paint_material(false);
  1950. }
  1951. }
  1952. function base_update_fill_layer(parse_paint: bool = true) {
  1953. let current: image_t = _g2_current;
  1954. let g2_in_use: bool = _g2_in_use;
  1955. if (g2_in_use) g2_end();
  1956. let _tool: workspace_tool_t = context_raw.tool;
  1957. let _fill_type: i32 = context_raw.fill_type_handle.position;
  1958. context_raw.tool = workspace_tool_t.FILL;
  1959. context_raw.fill_type_handle.position = fill_type_t.OBJECT;
  1960. context_raw.pdirty = 1;
  1961. slot_layer_clear(context_raw.layer);
  1962. if (parse_paint) {
  1963. make_material_parse_paint_material(false);
  1964. }
  1965. render_path_paint_commands_paint(false);
  1966. render_path_paint_dilate(true, true);
  1967. context_raw.rdirty = 2;
  1968. context_raw.tool = _tool;
  1969. context_raw.fill_type_handle.position = _fill_type;
  1970. if (g2_in_use) g2_begin(current);
  1971. }
  1972. function base_set_object_mask() {
  1973. ///if is_sculpt
  1974. return;
  1975. ///end
  1976. let ar: string[] = [tr("None")];
  1977. for (let i: i32 = 0; i < project_paint_objects.length; ++i) {
  1978. let p: mesh_object_t = project_paint_objects[i];
  1979. array_push(ar, p.base.name);
  1980. }
  1981. let mask: i32 = context_object_mask_used() ? slot_layer_get_object_mask(context_raw.layer) : 0;
  1982. if (context_layer_filter_used()) {
  1983. mask = context_raw.layer_filter;
  1984. }
  1985. if (mask > 0) {
  1986. if (context_raw.merged_object != null) {
  1987. context_raw.merged_object.base.visible = false;
  1988. }
  1989. let o: mesh_object_t = project_paint_objects[0];
  1990. for (let i: i32 = 0; i < project_paint_objects.length; ++i) {
  1991. let p: mesh_object_t = project_paint_objects[i];
  1992. if (p.base.name == ar[mask]) {
  1993. o = p;
  1994. break;
  1995. }
  1996. }
  1997. context_select_paint_object(o);
  1998. }
  1999. else {
  2000. let is_atlas: bool = slot_layer_get_object_mask(context_raw.layer) > 0 && slot_layer_get_object_mask(context_raw.layer) <= project_paint_objects.length;
  2001. if (context_raw.merged_object == null || is_atlas || context_raw.merged_object_is_atlas) {
  2002. let visibles: mesh_object_t[] = is_atlas ? project_get_atlas_objects(slot_layer_get_object_mask(context_raw.layer)) : null;
  2003. util_mesh_merge(visibles);
  2004. }
  2005. context_select_paint_object(context_main_object());
  2006. context_raw.paint_object.skip_context = "paint";
  2007. context_raw.merged_object.base.visible = true;
  2008. }
  2009. util_uv_dilatemap_cached = false;
  2010. }
  2011. function base_new_layer(clear: bool = true, position: i32 = -1): slot_layer_t {
  2012. if (project_layers.length > base_max_layers) {
  2013. return null;
  2014. }
  2015. let l: slot_layer_t = slot_layer_create();
  2016. l.object_mask = context_raw.layer_filter;
  2017. if (position == -1) {
  2018. if (slot_layer_is_mask(context_raw.layer)) context_set_layer(context_raw.layer.parent);
  2019. array_insert(project_layers, array_index_of(project_layers, context_raw.layer) + 1, l);
  2020. }
  2021. else {
  2022. array_insert(project_layers, position, l);
  2023. }
  2024. context_set_layer(l);
  2025. let li: i32 = array_index_of(project_layers, context_raw.layer);
  2026. if (li > 0) {
  2027. let below: slot_layer_t = project_layers[li - 1];
  2028. if (slot_layer_is_layer(below)) {
  2029. context_raw.layer.parent = below.parent;
  2030. }
  2031. }
  2032. if (clear) {
  2033. app_notify_on_init(function (l: slot_layer_t) {
  2034. slot_layer_clear(l);
  2035. }, l);
  2036. }
  2037. context_raw.layer_preview_dirty = true;
  2038. return l;
  2039. }
  2040. function base_new_mask(clear: bool = true, parent: slot_layer_t, position: i32 = -1): slot_layer_t {
  2041. if (project_layers.length > base_max_layers) {
  2042. return null;
  2043. }
  2044. let l: slot_layer_t = slot_layer_create("", layer_slot_type_t.MASK, parent);
  2045. if (position == -1) {
  2046. position = array_index_of(project_layers, parent);
  2047. }
  2048. array_insert(project_layers, position, l);
  2049. context_set_layer(l);
  2050. if (clear) {
  2051. app_notify_on_init(function (l: slot_layer_t) {
  2052. slot_layer_clear(l);
  2053. }, l);
  2054. }
  2055. context_raw.layer_preview_dirty = true;
  2056. return l;
  2057. }
  2058. function base_new_group(): slot_layer_t {
  2059. if (project_layers.length > base_max_layers) {
  2060. return null;
  2061. }
  2062. let l: slot_layer_t = slot_layer_create("", layer_slot_type_t.GROUP);
  2063. array_push(project_layers, l);
  2064. context_set_layer(l);
  2065. return l;
  2066. }
  2067. let _base_uv_type: uv_type_t;
  2068. let _base_decal_mat: mat4_t;
  2069. let _base_position: i32;
  2070. function base_create_fill_layer(uv_type: uv_type_t = uv_type_t.UVMAP, decal_mat: mat4_t = null, position: i32 = -1) {
  2071. _base_uv_type = uv_type;
  2072. _base_decal_mat = decal_mat;
  2073. _base_position = position;
  2074. app_notify_on_init(function () {
  2075. let l: slot_layer_t = base_new_layer(false, _base_position);
  2076. history_new_layer();
  2077. l.uv_type = _base_uv_type;
  2078. if (_base_decal_mat != null) {
  2079. l.decal_mat = _base_decal_mat;
  2080. }
  2081. l.object_mask = context_raw.layer_filter;
  2082. history_to_fill_layer();
  2083. slot_layer_to_fill_layer(l);
  2084. });
  2085. }
  2086. function base_create_image_mask(asset: asset_t) {
  2087. let l: slot_layer_t = context_raw.layer;
  2088. if (slot_layer_is_mask(l) || slot_layer_is_group(l)) {
  2089. return;
  2090. }
  2091. history_new_layer();
  2092. let m: slot_layer_t = base_new_mask(false, l);
  2093. slot_layer_clear(m, 0x00000000, project_get_image(asset));
  2094. context_raw.layer_preview_dirty = true;
  2095. }
  2096. let _base_base_color: i32;
  2097. let _base_occlusion: f32;
  2098. let _base_roughness: f32;
  2099. let _base_metallic: f32;
  2100. function base_create_color_layer(base_color: i32, occlusion: f32 = 1.0, roughness: f32 = base_default_rough, metallic: f32 = 0.0, position: i32 = -1) {
  2101. _base_base_color = base_color;
  2102. _base_occlusion = occlusion;
  2103. _base_roughness = roughness;
  2104. _base_metallic = metallic;
  2105. _base_position = position;
  2106. app_notify_on_init(function () {
  2107. let l: slot_layer_t = base_new_layer(false, _base_position);
  2108. history_new_layer();
  2109. l.uv_type = uv_type_t.UVMAP;
  2110. l.object_mask = context_raw.layer_filter;
  2111. slot_layer_clear(l, _base_base_color, null, _base_occlusion, _base_roughness, _base_metallic);
  2112. });
  2113. }
  2114. function base_on_layers_resized() {
  2115. app_notify_on_init(function () {
  2116. base_resize_layers();
  2117. let _layer: slot_layer_t = context_raw.layer;
  2118. let _material: slot_material_t = context_raw.material;
  2119. for (let i: i32 = 0; i < project_layers.length; ++i) {
  2120. let l: slot_layer_t = project_layers[i];
  2121. if (l.fill_layer != null) {
  2122. context_raw.layer = l;
  2123. context_raw.material = l.fill_layer;
  2124. base_update_fill_layer();
  2125. }
  2126. }
  2127. context_raw.layer = _layer;
  2128. context_raw.material = _material;
  2129. make_material_parse_paint_material();
  2130. });
  2131. util_uv_uvmap = null;
  2132. util_uv_uvmap_cached = false;
  2133. util_uv_trianglemap = null;
  2134. util_uv_trianglemap_cached = false;
  2135. util_uv_dilatemap_cached = false;
  2136. ///if (krom_direct3d12 || krom_vulkan || krom_metal)
  2137. render_path_raytrace_ready = false;
  2138. ///end
  2139. }
  2140. ///end
  2141. ///if is_lab
  2142. function base_flatten(height_to_normal: bool = false): any {
  2143. let texpaint: image_t = brush_output_node_inst.texpaint;
  2144. let texpaint_nor: image_t = brush_output_node_inst.texpaint_nor;
  2145. let texpaint_pack: image_t = brush_output_node_inst.texpaint_pack;
  2146. let nodes: zui_nodes_t = ui_nodes_get_nodes();
  2147. let canvas: zui_node_canvas_t = ui_nodes_get_canvas(true);
  2148. if (nodes.nodes_selected_id.length > 0) {
  2149. let node: zui_node_t = zui_get_node(canvas.nodes, nodes.nodes_selected_id[0]);
  2150. let brush_node: logic_node_t = parser_logic_get_logic_node(node);
  2151. if (brush_node != null && logic_node_get_cached_image(brush_node) != null) {
  2152. texpaint = logic_node_get_cached_image(brush_node);
  2153. texpaint_nor = map_get(render_path_render_targets, "texpaint_nor_empty")._image;
  2154. texpaint_pack = map_get(render_path_render_targets, "texpaint_pack_empty")._image;
  2155. }
  2156. }
  2157. return { texpaint: texpaint, texpaint_nor: texpaint_nor, texpaint_pack: texpaint_pack };
  2158. }
  2159. function base_on_layers_resized() {
  2160. image_unload(brush_output_node_inst.texpaint);
  2161. brush_output_node_inst.texpaint = map_get(render_path_render_targets, "texpaint")._image = image_create_render_target(config_get_texture_res_x(), config_get_texture_res_y());
  2162. image_unload(brush_output_node_inst.texpaint_nor);
  2163. brush_output_node_inst.texpaint_nor = map_get(render_path_render_targets, "texpaint_nor")._image = image_create_render_target(config_get_texture_res_x(), config_get_texture_res_y());
  2164. image_unload(brush_output_node_inst.texpaint_pack);
  2165. brush_output_node_inst.texpaint_pack = map_get(render_path_render_targets, "texpaint_pack")._image = image_create_render_target(config_get_texture_res_x(), config_get_texture_res_y());
  2166. if (inpaint_node_image != null) {
  2167. image_unload(inpaint_node_image);
  2168. inpaint_node_image = null;
  2169. image_unload(inpaint_node_mask);
  2170. inpaint_node_mask = null;
  2171. inpaint_node_init();
  2172. }
  2173. if (photo_to_pbr_node_images != null) {
  2174. for (let i: i32 = 0; i < photo_to_pbr_node_images.length; ++i) {
  2175. let image: image_t = photo_to_pbr_node_images[i];
  2176. image_unload(image);
  2177. }
  2178. photo_to_pbr_node_images = null;
  2179. photo_to_pbr_node_init();
  2180. }
  2181. if (tiling_node_image != null) {
  2182. image_unload(tiling_node_image);
  2183. tiling_node_image = null;
  2184. tiling_node_init();
  2185. }
  2186. image_unload(map_get(render_path_render_targets, "texpaint_blend0")._image);
  2187. map_get(render_path_render_targets, "texpaint_blend0")._image = image_create_render_target(config_get_texture_res_x(), config_get_texture_res_y(), tex_format_t.R8);
  2188. image_unload(map_get(render_path_render_targets, "texpaint_blend1")._image);
  2189. map_get(render_path_render_targets, "texpaint_blend1")._image = image_create_render_target(config_get_texture_res_x(), config_get_texture_res_y(), tex_format_t.R8);
  2190. if (map_get(render_path_render_targets, "texpaint_node") != null) {
  2191. map_delete(render_path_render_targets, "texpaint_node");
  2192. }
  2193. if (map_get(render_path_render_targets, "texpaint_node_target") != null) {
  2194. map_delete(render_path_render_targets, "texpaint_node_target");
  2195. }
  2196. app_notify_on_next_frame(function () {
  2197. base_init_layers();
  2198. });
  2199. ///if (krom_direct3d12 || krom_vulkan || krom_metal)
  2200. render_path_raytrace_ready = false;
  2201. ///end
  2202. }
  2203. ///end