context.ts 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  1. type context_t = {
  2. texture?: asset_t;
  3. paint_object?: mesh_object_t;
  4. merged_object?: mesh_object_t;
  5. merged_object_is_atlas?: bool;
  6. ddirty?: i32;
  7. pdirty?: i32;
  8. rdirty?: i32;
  9. brush_blend_dirty?: bool;
  10. node_preview_socket?: i32;
  11. split_view?: bool;
  12. view_index?: i32;
  13. view_index_last?: i32;
  14. swatch?: swatch_color_t;
  15. picked_color?: swatch_color_t;
  16. color_picker_callback?: (sc: swatch_color_t)=>void;
  17. default_irradiance?: f32_array_t;
  18. default_radiance?: image_t;
  19. default_radiance_mipmaps?: image_t[];
  20. saved_envmap?: image_t;
  21. empty_envmap?: image_t;
  22. preview_envmap?: image_t;
  23. envmap_loaded?: bool;
  24. show_envmap?: bool;
  25. show_envmap_handle?: ui_handle_t;
  26. show_envmap_blur?: bool;
  27. show_envmap_blur_handle?: ui_handle_t;
  28. envmap_angle?: f32;
  29. light_angle?: f32;
  30. cull_backfaces?: bool;
  31. texture_filter?: bool;
  32. format_type?: texture_ldr_format_t;
  33. format_quality?: f32;
  34. layers_destination?: export_destination_t;
  35. split_by?: split_type_t;
  36. parse_transform?: bool;
  37. parse_vcols?: bool;
  38. select_time?: f32;
  39. viewport_mode?: viewport_mode_t;
  40. render_mode?: render_mode_t;
  41. viewport_shader?: any; // JSValue * -> (ns: node_shader_t)=>void;
  42. hscale_was_changed?: bool;
  43. export_mesh_format?: mesh_format_t;
  44. export_mesh_index?: i32;
  45. pack_assets_on_export?: bool;
  46. paint_vec?: vec4_t;
  47. last_paint_x?: f32;
  48. last_paint_y?: f32;
  49. foreground_event?: bool;
  50. painted?: i32;
  51. brush_time?: f32;
  52. clone_start_x?: f32;
  53. clone_start_y?: f32;
  54. clone_delta_x?: f32;
  55. clone_delta_y?: f32;
  56. show_compass?: bool;
  57. project_type?: project_model_t;
  58. project_aspect_ratio?: i32;
  59. project_objects?: mesh_object_t[];
  60. last_paint_vec_x?: f32;
  61. last_paint_vec_y?: f32;
  62. prev_paint_vec_x?: f32;
  63. prev_paint_vec_y?: f32;
  64. frame?: i32;
  65. paint2d_view?: bool;
  66. lock_started_x?: f32;
  67. lock_started_y?: f32;
  68. brush_locked?: bool;
  69. brush_can_lock?: bool;
  70. brush_can_unlock?: bool;
  71. camera_type?: camera_type_t;
  72. cam_handle?: ui_handle_t;
  73. fov_handle?: ui_handle_t;
  74. undo_handle?: ui_handle_t;
  75. hssao?: ui_handle_t;
  76. hbloom?: ui_handle_t;
  77. hsupersample?: ui_handle_t;
  78. hvxao?: ui_handle_t;
  79. vxao_ext?: f32;
  80. vxao_offset?: f32;
  81. vxao_aperture?: f32;
  82. texture_export_path?: string;
  83. last_status_position?: i32;
  84. camera_controls?: camera_controls_t;
  85. pen_painting_only?: bool;
  86. material?: slot_material_t;
  87. layer?: slot_layer_t;
  88. brush?: slot_brush_t;
  89. font?: slot_font_t;
  90. tool?: workspace_tool_t;
  91. layer_preview_dirty?: bool;
  92. layers_preview_dirty?: bool;
  93. node_preview_dirty?: bool;
  94. node_preview?: image_t;
  95. node_previews?: map_t<string, image_t>;
  96. node_previews_used?: string[];
  97. node_preview_name?: string;
  98. mask_preview_rgba32?: image_t;
  99. mask_preview_last?: slot_layer_t;
  100. colorid_picked?: bool;
  101. material_preview?: bool;
  102. saved_camera?: mat4_t;
  103. color_picker_previous_tool?: workspace_tool_t;
  104. materialid_picked?: i32;
  105. uvx_picked?: f32;
  106. uvy_picked?: f32;
  107. picker_select_material?: bool;
  108. picker_mask_handle?: ui_handle_t;
  109. pick_pos_nor_tex?: bool;
  110. posx_picked?: f32;
  111. posy_picked?: f32;
  112. posz_picked?: f32;
  113. norx_picked?: f32;
  114. nory_picked?: f32;
  115. norz_picked?: f32;
  116. draw_wireframe?: bool;
  117. wireframe_handle?: ui_handle_t;
  118. draw_texels?: bool;
  119. texels_handle?: ui_handle_t;
  120. colorid_handle?: ui_handle_t;
  121. layers_export?: export_mode_t;
  122. decal_image?: image_t;
  123. decal_preview?: bool;
  124. decal_x?: f32;
  125. decal_y?: f32;
  126. cache_draws?: bool;
  127. write_icon_on_export?: bool;
  128. text_tool_image?: image_t;
  129. text_tool_text?: string;
  130. particle_material?: material_data_t;
  131. layer_filter?: i32;
  132. brush_output_node_inst?: brush_output_node_t;
  133. run_brush?: (self: any, i: i32)=>void;
  134. parse_brush_inputs?: (self: any)=>void;
  135. gizmo?: object_t;
  136. gizmo_translate_x?: object_t;
  137. gizmo_translate_y?: object_t;
  138. gizmo_translate_z?: object_t;
  139. gizmo_scale_x?: object_t;
  140. gizmo_scale_y?: object_t;
  141. gizmo_scale_z?: object_t;
  142. gizmo_rotate_x?: object_t;
  143. gizmo_rotate_y?: object_t;
  144. gizmo_rotate_z?: object_t;
  145. gizmo_started?: bool;
  146. gizmo_offset?: f32;
  147. gizmo_drag?: f32;
  148. gizmo_drag_last?: f32;
  149. translate_x?: bool;
  150. translate_y?: bool;
  151. translate_z?: bool;
  152. scale_x?: bool;
  153. scale_y?: bool;
  154. scale_z?: bool;
  155. rotate_x?: bool;
  156. rotate_y?: bool;
  157. rotate_z?: bool;
  158. brush_nodes_radius?: f32;
  159. brush_nodes_opacity?: f32;
  160. brush_mask_image?: image_t;
  161. brush_mask_image_is_alpha?: bool;
  162. brush_stencil_image?: image_t;
  163. brush_stencil_image_is_alpha?: bool;
  164. brush_stencil_x?: f32;
  165. brush_stencil_y?: f32;
  166. brush_stencil_scale?: f32;
  167. brush_stencil_scaling?: bool;
  168. brush_stencil_angle?: f32;
  169. brush_stencil_rotating?: bool;
  170. brush_nodes_scale?: f32;
  171. brush_nodes_angle?: f32;
  172. brush_nodes_hardness?: f32;
  173. brush_directional?: bool;
  174. brush_radius?: f32;
  175. brush_radius_handle?: ui_handle_t;
  176. brush_scale_x?: f32;
  177. brush_decal_mask_radius?: f32;
  178. brush_decal_mask_radius_handle?: ui_handle_t;
  179. brush_scale_x_handle?: ui_handle_t;
  180. brush_blending?: blend_type_t;
  181. brush_opacity?: f32;
  182. brush_opacity_handle?: ui_handle_t;
  183. brush_scale?: f32;
  184. brush_angle?: f32;
  185. brush_angle_handle?: ui_handle_t;
  186. brush_hardness?: f32;
  187. brush_lazy_radius?: f32;
  188. brush_lazy_step?: f32;
  189. brush_lazy_x?: f32;
  190. brush_lazy_y?: f32;
  191. brush_paint?: uv_type_t;
  192. brush_angle_reject_dot?: f32;
  193. bake_type?: bake_type_t;
  194. bake_axis?: bake_axis_t;
  195. bake_up_axis?: bake_up_axis_t;
  196. bake_samples?: i32;
  197. bake_ao_strength?: f32;
  198. bake_ao_radius?: f32;
  199. bake_ao_offset?: f32;
  200. bake_curv_strength?: f32;
  201. bake_curv_radius?: f32;
  202. bake_curv_offset?: f32;
  203. bake_curv_smooth?: i32;
  204. bake_high_poly?: i32;
  205. xray?: bool;
  206. sym_x?: bool;
  207. sym_y?: bool;
  208. sym_z?: bool;
  209. fill_type_handle?: ui_handle_t;
  210. paint2d?: bool;
  211. last_htab0_pos?: i32;
  212. maximized_sidebar_width?: i32;
  213. drag_dest?: i32;
  214. coords?: vec4_t;
  215. start_x?: f32;
  216. start_y?: f32;
  217. lock_begin?: bool;
  218. lock_x?: bool;
  219. lock_y?: bool;
  220. lock_start_x?: f32;
  221. lock_start_y?: f32;
  222. registered?: bool;
  223. selected_object?: object_t;
  224. ///if arm_physics
  225. particle_physics?: bool;
  226. particle_hit_x?: f32;
  227. particle_hit_y?: f32;
  228. particle_hit_z?: f32;
  229. last_particle_hit_x?: f32;
  230. last_particle_hit_y?: f32;
  231. last_particle_hit_z?: f32;
  232. particle_timer?: tween_anim_t;
  233. paint_body?: physics_body_t;
  234. ///end
  235. }
  236. let context_raw: context_t;
  237. function context_create(): context_t {
  238. let c: context_t = {};
  239. c.merged_object_is_atlas = false; // Only objects referenced by atlas are merged
  240. c.ddirty = 0; // depth
  241. c.pdirty = 0; // paint
  242. c.rdirty = 0; // render
  243. c.brush_blend_dirty = true;
  244. c.node_preview_socket = 0;
  245. c.split_view = false;
  246. c.view_index = -1;
  247. c.view_index_last = -1;
  248. c.picked_color = make_swatch();
  249. c.envmap_loaded = false;
  250. c.show_envmap = false;
  251. c.show_envmap_handle = ui_handle_create();
  252. c.show_envmap_blur = false;
  253. c.show_envmap_blur_handle = ui_handle_create();
  254. c.envmap_angle = 0.0;
  255. c.light_angle = 0.0;
  256. c.cull_backfaces = true;
  257. c.texture_filter = true;
  258. c.format_type = texture_ldr_format_t.PNG;
  259. c.format_quality = 100.0;
  260. c.layers_destination = export_destination_t.DISK;
  261. c.split_by = split_type_t.OBJECT;
  262. c.parse_transform = true;
  263. c.parse_vcols = false;
  264. c.select_time = 0.0;
  265. ///if (arm_direct3d12 || arm_vulkan)
  266. c.viewport_mode = viewport_mode_t.PATH_TRACE;
  267. ///else
  268. c.viewport_mode = viewport_mode_t.LIT;
  269. ///end
  270. ///if (arm_android || arm_ios)
  271. c.render_mode = render_mode_t.FORWARD;
  272. ///else
  273. c.render_mode = render_mode_t.DEFERRED;
  274. ///end
  275. c.hscale_was_changed = false;
  276. c.export_mesh_format = mesh_format_t.OBJ;
  277. c.export_mesh_index = 0;
  278. c.pack_assets_on_export = true;
  279. c.paint_vec = vec4_create();
  280. c.last_paint_x = -1.0;
  281. c.last_paint_y = -1.0;
  282. c.foreground_event = false;
  283. c.painted = 0;
  284. c.brush_time = 0.0;
  285. c.clone_start_x = -1.0;
  286. c.clone_start_y = -1.0;
  287. c.clone_delta_x = 0.0;
  288. c.clone_delta_y = 0.0;
  289. c.show_compass = true;
  290. c.project_type = project_model_t.ROUNDED_CUBE;
  291. c.project_aspect_ratio = 0; // 1:1, 2:1, 1:2
  292. c.last_paint_vec_x = -1.0;
  293. c.last_paint_vec_y = -1.0;
  294. c.prev_paint_vec_x = -1.0;
  295. c.prev_paint_vec_y = -1.0;
  296. c.frame = 0;
  297. c.paint2d_view = false;
  298. c.lock_started_x = -1.0;
  299. c.lock_started_y = -1.0;
  300. c.brush_locked = false;
  301. c.brush_can_lock = false;
  302. c.brush_can_unlock = false;
  303. c.camera_type = camera_type_t.PERSPECTIVE;
  304. c.cam_handle = ui_handle_create();
  305. c.vxao_ext = 1.0;
  306. c.vxao_offset = 1.5;
  307. c.vxao_aperture = 1.2;
  308. c.texture_export_path = "";
  309. c.last_status_position = 0;
  310. c.camera_controls = camera_controls_t.ORBIT;
  311. c.pen_painting_only = false; // Reject painting with finger when using pen
  312. c.layer_preview_dirty = true;
  313. c.layers_preview_dirty = false;
  314. c.node_preview_dirty = false;
  315. c.node_preview_name = "";
  316. c.colorid_picked = false;
  317. c.material_preview = false; // Drawing material previews
  318. c.saved_camera = mat4_identity();
  319. c.materialid_picked = 0;
  320. c.uvx_picked = 0.0;
  321. c.uvy_picked = 0.0;
  322. c.picker_select_material = true;
  323. c.picker_mask_handle = ui_handle_create();
  324. c.pick_pos_nor_tex = false;
  325. c.posx_picked = 0.0;
  326. c.posy_picked = 0.0;
  327. c.posz_picked = 0.0;
  328. c.norx_picked = 0.0;
  329. c.nory_picked = 0.0;
  330. c.norz_picked = 0.0;
  331. c.draw_wireframe = false;
  332. c.wireframe_handle = ui_handle_create();
  333. c.draw_texels = false;
  334. c.texels_handle = ui_handle_create();
  335. c.colorid_handle = ui_handle_create();
  336. c.layers_export = export_mode_t.VISIBLE;
  337. c.decal_preview = false;
  338. c.decal_x = 0.0;
  339. c.decal_y = 0.0;
  340. c.cache_draws = false;
  341. c.write_icon_on_export = false;
  342. ///if arm_physics
  343. c.particle_physics = false;
  344. c.particle_hit_x = 0.0;
  345. c.particle_hit_y = 0.0;
  346. c.particle_hit_z = 0.0;
  347. c.last_particle_hit_x = 0.0;
  348. c.last_particle_hit_y = 0.0;
  349. c.last_particle_hit_z = 0.0;
  350. ///end
  351. c.layer_filter = 0;
  352. c.gizmo_started = false;
  353. c.gizmo_offset = 0.0;
  354. c.gizmo_drag = 0.0;
  355. c.gizmo_drag_last = 0.0;
  356. c.translate_x = false;
  357. c.translate_y = false;
  358. c.translate_z = false;
  359. c.scale_x = false;
  360. c.scale_y = false;
  361. c.scale_z = false;
  362. c.rotate_x = false;
  363. c.rotate_y = false;
  364. c.rotate_z = false;
  365. c.brush_nodes_radius = 1.0;
  366. c.brush_nodes_opacity = 1.0;
  367. c.brush_mask_image_is_alpha = false;
  368. c.brush_stencil_image_is_alpha = false;
  369. c.brush_stencil_x = 0.02;
  370. c.brush_stencil_y = 0.02;
  371. c.brush_stencil_scale = 0.9;
  372. c.brush_stencil_scaling = false;
  373. c.brush_stencil_angle = 0.0;
  374. c.brush_stencil_rotating = false;
  375. c.brush_nodes_scale = 1.0;
  376. c.brush_nodes_angle = 0.0;
  377. c.brush_nodes_hardness = 1.0;
  378. c.brush_directional = false;
  379. c.brush_radius_handle = ui_handle_create();
  380. c.brush_scale_x = 1.0;
  381. c.brush_decal_mask_radius = 0.5;
  382. c.brush_decal_mask_radius_handle = ui_handle_create();
  383. c.brush_decal_mask_radius_handle.value = 0.5;
  384. c.brush_scale_x_handle = ui_handle_create();
  385. c.brush_scale_x_handle.value = 1.0;
  386. c.brush_blending = blend_type_t.MIX;
  387. c.brush_opacity = 1.0;
  388. c.brush_opacity_handle = ui_handle_create();
  389. c.brush_opacity_handle.value = 1.0;
  390. ///if is_forge
  391. let atlas_w: i32 = config_get_atlas_res();
  392. let item_w: i32 = config_get_layer_res();
  393. let atlas_stride: i32 = atlas_w / item_w;
  394. c.brush_scale = atlas_stride;
  395. ///else
  396. c.brush_scale = 1.0;
  397. ///end
  398. c.brush_angle = 0.0;
  399. c.brush_angle_handle = ui_handle_create();
  400. c.brush_angle_handle.value = 0.0;
  401. c.brush_lazy_radius = 0.0;
  402. c.brush_lazy_step = 0.0;
  403. c.brush_lazy_x = 0.0;
  404. c.brush_lazy_y = 0.0;
  405. c.brush_paint = uv_type_t.UVMAP;
  406. c.brush_angle_reject_dot = 0.5;
  407. c.bake_type = bake_type_t.AO;
  408. c.bake_axis = bake_axis_t.XYZ;
  409. c.bake_up_axis = bake_up_axis_t.Z;
  410. c.bake_samples = 128;
  411. c.bake_ao_strength = 1.0;
  412. c.bake_ao_radius = 1.0;
  413. c.bake_ao_offset = 1.0;
  414. c.bake_curv_strength = 1.0;
  415. c.bake_curv_radius = 1.0;
  416. c.bake_curv_offset = 0.0;
  417. c.bake_curv_smooth = 1;
  418. c.bake_high_poly = 0;
  419. c.xray = false;
  420. c.sym_x = false;
  421. c.sym_y = false;
  422. c.sym_z = false;
  423. c.fill_type_handle = ui_handle_create();
  424. c.paint2d = false;
  425. c.last_htab0_pos = 0;
  426. c.maximized_sidebar_width = 0;
  427. c.drag_dest = 0;
  428. return c;
  429. }
  430. function context_init() {
  431. context_raw = context_create();
  432. context_ext_init(context_raw);
  433. }
  434. function context_use_deferred(): bool {
  435. return context_raw.render_mode != render_mode_t.FORWARD && (context_raw.viewport_mode == viewport_mode_t.LIT || context_raw.viewport_mode == viewport_mode_t.PATH_TRACE) && context_raw.tool != workspace_tool_t.COLORID;
  436. }
  437. function context_select_material(i: i32) {
  438. if (project_materials.length <= i) {
  439. return;
  440. }
  441. context_set_material(project_materials[i]);
  442. }
  443. function context_set_material(m: slot_material_t) {
  444. if (array_index_of(project_materials, m) == -1) {
  445. return;
  446. }
  447. context_raw.material = m;
  448. make_material_parse_paint_material();
  449. ui_base_hwnds[tab_area_t.SIDEBAR1].redraws = 2;
  450. ui_header_handle.redraws = 2;
  451. ui_nodes_hwnd.redraws = 2;
  452. ui_nodes_group_stack = [];
  453. let decal: bool = context_is_decal();
  454. if (decal) {
  455. app_notify_on_next_frame(util_render_make_decal_preview);
  456. }
  457. }
  458. function context_select_brush(i: i32) {
  459. if (project_brushes.length <= i) {
  460. return;
  461. }
  462. context_set_brush(project_brushes[i]);
  463. }
  464. function context_set_brush(b: slot_brush_t) {
  465. if (array_index_of(project_brushes, b) == -1) {
  466. return;
  467. }
  468. context_raw.brush = b;
  469. make_material_parse_brush();
  470. ui_base_hwnds[tab_area_t.SIDEBAR1].redraws = 2;
  471. ui_nodes_hwnd.redraws = 2;
  472. }
  473. function context_select_font(i: i32) {
  474. if (project_fonts.length <= i) {
  475. return;
  476. }
  477. context_set_font(project_fonts[i]);
  478. }
  479. function context_set_font(f: slot_font_t) {
  480. if (array_index_of(project_fonts, f) == -1) {
  481. return;
  482. }
  483. context_raw.font = f;
  484. util_render_make_text_preview();
  485. util_render_make_decal_preview();
  486. ui_base_hwnds[tab_area_t.STATUS].redraws = 2;
  487. ui_view2d_hwnd.redraws = 2;
  488. }
  489. function context_select_layer(i: i32) {
  490. if (project_layers.length <= i) {
  491. return;
  492. }
  493. context_set_layer(project_layers[i]);
  494. }
  495. function context_set_layer(l: slot_layer_t) {
  496. if (l == context_raw.layer) {
  497. return;
  498. }
  499. context_raw.layer = l;
  500. ui_header_handle.redraws = 2;
  501. let current: image_t = _g2_current;
  502. let g2_in_use: bool = _g2_in_use;
  503. if (g2_in_use) g2_end();
  504. layers_set_object_mask();
  505. make_material_parse_mesh_material();
  506. make_material_parse_paint_material();
  507. if (g2_in_use) g2_begin(current);
  508. ui_base_hwnds[tab_area_t.SIDEBAR0].redraws = 2;
  509. ui_view2d_hwnd.redraws = 2;
  510. }
  511. function context_select_tool(i: i32) {
  512. context_raw.tool = i;
  513. make_material_parse_paint_material();
  514. make_material_parse_mesh_material();
  515. context_raw.ddirty = 3;
  516. let _viewport_mode: viewport_mode_t = context_raw.viewport_mode;
  517. context_raw.viewport_mode = viewport_mode_t.MINUS_ONE;
  518. context_set_viewport_mode(_viewport_mode);
  519. context_init_tool();
  520. ui_header_handle.redraws = 2;
  521. ui_toolbar_handle.redraws = 2;
  522. }
  523. function context_init_tool() {
  524. let decal: bool = context_is_decal();
  525. if (decal) {
  526. if (context_raw.tool == workspace_tool_t.TEXT) {
  527. util_render_make_text_preview();
  528. }
  529. util_render_make_decal_preview();
  530. }
  531. else if (context_raw.tool == workspace_tool_t.PARTICLE) {
  532. util_particle_init();
  533. make_material_parse_particle_material();
  534. }
  535. else if (context_raw.tool == workspace_tool_t.BAKE) {
  536. ///if (arm_direct3d12 || arm_vulkan || arm_metal)
  537. // Bake in lit mode for now
  538. if (context_raw.viewport_mode == viewport_mode_t.PATH_TRACE) {
  539. context_raw.viewport_mode = viewport_mode_t.LIT;
  540. }
  541. ///end
  542. }
  543. else if (context_raw.tool == workspace_tool_t.MATERIAL) {
  544. layers_update_fill_layers();
  545. context_main_object().skip_context = null;
  546. }
  547. ///if arm_ios
  548. // No hover on iPad, decals are painted by pen release
  549. config_raw.brush_live = decal;
  550. ///end
  551. }
  552. function context_select_paint_object(o: mesh_object_t) {
  553. context_ext_select_paint_object(o);
  554. }
  555. function context_main_object(): mesh_object_t {
  556. ///if is_lab
  557. return project_paint_objects[0];
  558. ///end
  559. for (let i: i32 = 0; i < project_paint_objects.length; ++i) {
  560. let po: mesh_object_t = project_paint_objects[i];
  561. if (po.base.children.length > 0) {
  562. return po;
  563. }
  564. }
  565. return project_paint_objects[0];
  566. }
  567. function context_layer_filter_used(): bool {
  568. ///if is_lab
  569. return true;
  570. ///end
  571. return context_raw.layer_filter > 0 && context_raw.layer_filter <= project_paint_objects.length;
  572. }
  573. function context_object_mask_used(): bool {
  574. ///if is_lab
  575. return false;
  576. ///end
  577. return slot_layer_get_object_mask(context_raw.layer) > 0 && slot_layer_get_object_mask(context_raw.layer) <= project_paint_objects.length;
  578. }
  579. function context_in_viewport(): bool {
  580. return context_raw.paint_vec.x < 1 && context_raw.paint_vec.x > 0 &&
  581. context_raw.paint_vec.y < 1 && context_raw.paint_vec.y > 0;
  582. }
  583. function context_in_paint_area(): bool {
  584. ///if is_lab
  585. return context_in_viewport();
  586. ///end
  587. let right: i32 = app_w();
  588. if (ui_view2d_show) {
  589. right += ui_view2d_ww;
  590. }
  591. return mouse_view_x() > 0 && mouse_view_x() < right &&
  592. mouse_view_y() > 0 && mouse_view_y() < app_h();
  593. }
  594. function context_in_layers(): bool {
  595. let tab: string = ui_hovered_tab_name();
  596. return tab == tr("Layers");
  597. }
  598. function context_in_materials(): bool {
  599. let tab: string = ui_hovered_tab_name();
  600. return tab == tr("Materials");
  601. }
  602. function context_in_2d_view(type: view_2d_type_t = view_2d_type_t.LAYER): bool {
  603. return ui_view2d_show && ui_view2d_type == type &&
  604. mouse_x > ui_view2d_wx && mouse_x < ui_view2d_wx + ui_view2d_ww &&
  605. mouse_y > ui_view2d_wy && mouse_y < ui_view2d_wy + ui_view2d_wh;
  606. }
  607. function context_in_nodes(): bool {
  608. return ui_nodes_show &&
  609. mouse_x > ui_nodes_wx && mouse_x < ui_nodes_wx + ui_nodes_ww &&
  610. mouse_y > ui_nodes_wy && mouse_y < ui_nodes_wy + ui_nodes_wh;
  611. }
  612. function context_in_swatches(): bool {
  613. let tab: string = ui_hovered_tab_name();
  614. return tab == tr("Swatches");
  615. }
  616. function context_in_browser(): bool {
  617. let tab: string = ui_hovered_tab_name();
  618. return tab == tr("Browser");
  619. }
  620. function context_is_picker(): bool {
  621. return context_raw.tool == workspace_tool_t.PICKER || context_raw.tool == workspace_tool_t.MATERIAL;
  622. }
  623. function context_is_decal(): bool {
  624. return context_raw.tool == workspace_tool_t.DECAL || context_raw.tool == workspace_tool_t.TEXT;
  625. }
  626. function context_is_decal_mask(): bool {
  627. return context_is_decal() && operator_shortcut(map_get(config_keymap, "decal_mask"), shortcut_type_t.DOWN);
  628. }
  629. function context_is_decal_mask_paint(): bool {
  630. return context_is_decal() && operator_shortcut(map_get(config_keymap, "decal_mask") + "+" + map_get(config_keymap, "action_paint"), shortcut_type_t.DOWN);
  631. }
  632. function context_is_floating_toolbar(): bool {
  633. // Header is off -> floating toolbar
  634. return config_raw.layout[layout_size_t.HEADER] == 0;
  635. }
  636. function context_get_area_type(): area_type_t {
  637. if (context_in_viewport()) {
  638. return area_type_t.VIEWPORT;
  639. }
  640. if (context_in_nodes()) {
  641. return area_type_t.NODES;
  642. }
  643. if (context_in_browser()) {
  644. return area_type_t.BROWSER;
  645. }
  646. if (context_in_2d_view()) {
  647. return area_type_t.VIEW2D;
  648. }
  649. if (context_in_layers()) {
  650. return area_type_t.LAYERS;
  651. }
  652. if (context_in_materials()) {
  653. return area_type_t.MATERIALS;
  654. }
  655. return area_type_t.MINUS_ONE;
  656. }
  657. function context_set_viewport_mode(mode: viewport_mode_t) {
  658. if (mode == context_raw.viewport_mode) {
  659. return;
  660. }
  661. context_raw.viewport_mode = mode;
  662. if (context_use_deferred()) {
  663. render_path_commands = render_path_deferred_commands;
  664. }
  665. else {
  666. render_path_commands = render_path_forward_commands;
  667. }
  668. let _workspace: i32 = ui_header_worktab.position;
  669. ui_header_worktab.position = 0;
  670. make_material_parse_mesh_material();
  671. ui_header_worktab.position = _workspace;
  672. }
  673. function context_load_envmap() {
  674. if (!context_raw.envmap_loaded) {
  675. // TODO: Unable to share texture for both radiance and envmap - reload image
  676. context_raw.envmap_loaded = true;
  677. map_delete(data_cached_images, "World_radiance.k");
  678. }
  679. world_data_load_envmap(scene_world);
  680. if (context_raw.saved_envmap == null) {
  681. context_raw.saved_envmap = scene_world._.envmap;
  682. }
  683. }
  684. function context_update_envmap() {
  685. if (context_raw.show_envmap) {
  686. scene_world._.envmap = context_raw.show_envmap_blur ? scene_world._.radiance_mipmaps[0] : context_raw.saved_envmap;
  687. }
  688. else {
  689. scene_world._.envmap = context_raw.empty_envmap;
  690. }
  691. }
  692. function context_set_viewport_shader(viewport_shader: any) { // JSValue * -> (ns: node_shader_t)=>void
  693. context_raw.viewport_shader = viewport_shader;
  694. context_set_render_path();
  695. }
  696. function context_set_render_path() {
  697. if (context_raw.render_mode == render_mode_t.FORWARD || context_raw.viewport_shader != null) {
  698. render_path_commands = render_path_forward_commands;
  699. }
  700. else {
  701. render_path_commands = render_path_deferred_commands;
  702. }
  703. app_notify_on_init(make_material_parse_mesh_material);
  704. }
  705. function context_enable_import_plugin(file: string): bool {
  706. // Return plugin name suitable for importing the specified file
  707. if (box_preferences_files_plugin == null) {
  708. box_preferences_fetch_plugins();
  709. }
  710. let ext: string = substring(file, string_last_index_of(file, ".") + 1, file.length);
  711. for (let i: i32 = 0; i < box_preferences_files_plugin.length; ++i) {
  712. let f: string = box_preferences_files_plugin[i];
  713. if (starts_with(f, "import_") && string_index_of(f, ext) >= 0) {
  714. config_enable_plugin(f);
  715. console_info(f + " " + tr("plugin enabled"));
  716. return true;
  717. }
  718. }
  719. return false;
  720. }
  721. function context_set_swatch(s: swatch_color_t) {
  722. context_raw.swatch = s;
  723. }