context.ts 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  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?: gpu_texture_t;
  19. default_radiance_mipmaps?: gpu_texture_t[];
  20. saved_envmap?: gpu_texture_t;
  21. empty_envmap?: gpu_texture_t;
  22. preview_envmap?: gpu_texture_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?: tool_type_t;
  91. layer_preview_dirty?: bool;
  92. layers_preview_dirty?: bool;
  93. node_preview_dirty?: bool;
  94. node_preview?: gpu_texture_t;
  95. node_previews?: map_t<string, gpu_texture_t>;
  96. node_previews_used?: string[];
  97. node_preview_name?: string;
  98. mask_preview_rgba32?: gpu_texture_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?: tool_type_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?: gpu_texture_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?: gpu_texture_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?: gpu_texture_t;
  161. brush_mask_image_is_alpha?: bool;
  162. brush_stencil_image?: gpu_texture_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. particle_hit_x?: f32;
  225. particle_hit_y?: f32;
  226. particle_hit_z?: f32;
  227. last_particle_hit_x?: f32;
  228. last_particle_hit_y?: f32;
  229. last_particle_hit_z?: f32;
  230. particle_timer?: tween_anim_t;
  231. paint_body?: physics_body_t;
  232. }
  233. let context_raw: context_t;
  234. function context_create(): context_t {
  235. let c: context_t = {};
  236. c.merged_object_is_atlas = false; // Only objects referenced by atlas are merged
  237. c.ddirty = 0; // depth
  238. c.pdirty = 0; // paint
  239. c.rdirty = 0; // render
  240. c.brush_blend_dirty = true;
  241. c.node_preview_socket = 0;
  242. c.split_view = false;
  243. c.view_index = -1;
  244. c.view_index_last = -1;
  245. c.picked_color = make_swatch();
  246. c.envmap_loaded = false;
  247. c.show_envmap = false;
  248. c.show_envmap_handle = ui_handle_create();
  249. c.show_envmap_blur = false;
  250. c.show_envmap_blur_handle = ui_handle_create();
  251. c.envmap_angle = 0.0;
  252. c.light_angle = 0.0;
  253. c.cull_backfaces = true;
  254. c.texture_filter = true;
  255. c.format_type = texture_ldr_format_t.PNG;
  256. c.format_quality = 100.0;
  257. c.layers_destination = export_destination_t.DISK;
  258. c.split_by = split_type_t.OBJECT;
  259. c.parse_transform = true;
  260. c.parse_vcols = false;
  261. c.select_time = 0.0;
  262. c.viewport_mode = config_raw.viewport_mode == 0 ?
  263. viewport_mode_t.LIT :
  264. viewport_mode_t.PATH_TRACE;
  265. ///if (arm_android || arm_ios)
  266. c.render_mode = render_mode_t.FORWARD;
  267. ///else
  268. c.render_mode = render_mode_t.DEFERRED;
  269. ///end
  270. c.hscale_was_changed = false;
  271. c.export_mesh_format = mesh_format_t.OBJ;
  272. c.export_mesh_index = 0;
  273. c.pack_assets_on_export = true;
  274. c.paint_vec = vec4_create();
  275. c.last_paint_x = -1.0;
  276. c.last_paint_y = -1.0;
  277. c.foreground_event = false;
  278. c.painted = 0;
  279. c.brush_time = 0.0;
  280. c.clone_start_x = -1.0;
  281. c.clone_start_y = -1.0;
  282. c.clone_delta_x = 0.0;
  283. c.clone_delta_y = 0.0;
  284. c.show_compass = true;
  285. c.project_type = project_model_t.ROUNDED_CUBE;
  286. c.project_aspect_ratio = 0; // 1:1, 2:1, 1:2
  287. c.last_paint_vec_x = -1.0;
  288. c.last_paint_vec_y = -1.0;
  289. c.prev_paint_vec_x = -1.0;
  290. c.prev_paint_vec_y = -1.0;
  291. c.frame = 0;
  292. c.paint2d_view = false;
  293. c.lock_started_x = -1.0;
  294. c.lock_started_y = -1.0;
  295. c.brush_locked = false;
  296. c.brush_can_lock = false;
  297. c.brush_can_unlock = false;
  298. c.camera_type = camera_type_t.PERSPECTIVE;
  299. c.cam_handle = ui_handle_create();
  300. c.vxao_ext = 1.0;
  301. c.vxao_offset = 1.5;
  302. c.vxao_aperture = 1.2;
  303. c.texture_export_path = "";
  304. c.last_status_position = 0;
  305. c.camera_controls = camera_controls_t.ORBIT;
  306. c.pen_painting_only = false; // Reject painting with finger when using pen
  307. c.layer_preview_dirty = true;
  308. c.layers_preview_dirty = false;
  309. c.node_preview_dirty = false;
  310. c.node_preview_name = "";
  311. c.colorid_picked = false;
  312. c.material_preview = false; // Drawing material previews
  313. c.saved_camera = mat4_identity();
  314. c.materialid_picked = 0;
  315. c.uvx_picked = 0.0;
  316. c.uvy_picked = 0.0;
  317. c.picker_select_material = true;
  318. c.picker_mask_handle = ui_handle_create();
  319. c.pick_pos_nor_tex = false;
  320. c.posx_picked = 0.0;
  321. c.posy_picked = 0.0;
  322. c.posz_picked = 0.0;
  323. c.norx_picked = 0.0;
  324. c.nory_picked = 0.0;
  325. c.norz_picked = 0.0;
  326. c.draw_wireframe = false;
  327. c.wireframe_handle = ui_handle_create();
  328. c.draw_texels = false;
  329. c.texels_handle = ui_handle_create();
  330. c.colorid_handle = ui_handle_create();
  331. c.layers_export = export_mode_t.VISIBLE;
  332. c.decal_preview = false;
  333. c.decal_x = 0.0;
  334. c.decal_y = 0.0;
  335. // c.cache_draws = false;
  336. c.write_icon_on_export = false;
  337. c.particle_hit_x = 0.0;
  338. c.particle_hit_y = 0.0;
  339. c.particle_hit_z = 0.0;
  340. c.last_particle_hit_x = 0.0;
  341. c.last_particle_hit_y = 0.0;
  342. c.last_particle_hit_z = 0.0;
  343. c.layer_filter = 0;
  344. c.gizmo_started = false;
  345. c.gizmo_offset = 0.0;
  346. c.gizmo_drag = 0.0;
  347. c.gizmo_drag_last = 0.0;
  348. c.translate_x = false;
  349. c.translate_y = false;
  350. c.translate_z = false;
  351. c.scale_x = false;
  352. c.scale_y = false;
  353. c.scale_z = false;
  354. c.rotate_x = false;
  355. c.rotate_y = false;
  356. c.rotate_z = false;
  357. c.brush_nodes_radius = 1.0;
  358. c.brush_nodes_opacity = 1.0;
  359. c.brush_mask_image_is_alpha = false;
  360. c.brush_stencil_image_is_alpha = false;
  361. c.brush_stencil_x = 0.02;
  362. c.brush_stencil_y = 0.02;
  363. c.brush_stencil_scale = 0.9;
  364. c.brush_stencil_scaling = false;
  365. c.brush_stencil_angle = 0.0;
  366. c.brush_stencil_rotating = false;
  367. c.brush_nodes_scale = 1.0;
  368. c.brush_nodes_angle = 0.0;
  369. c.brush_nodes_hardness = 1.0;
  370. c.brush_directional = false;
  371. c.brush_radius_handle = ui_handle_create();
  372. c.brush_scale_x = 1.0;
  373. c.brush_decal_mask_radius = 0.5;
  374. c.brush_decal_mask_radius_handle = ui_handle_create();
  375. c.brush_decal_mask_radius_handle.value = 0.5;
  376. c.brush_scale_x_handle = ui_handle_create();
  377. c.brush_scale_x_handle.value = 1.0;
  378. c.brush_blending = blend_type_t.MIX;
  379. c.brush_opacity = 1.0;
  380. c.brush_opacity_handle = ui_handle_create();
  381. c.brush_opacity_handle.value = 1.0;
  382. c.brush_scale = 1.0;
  383. c.brush_angle = 0.0;
  384. c.brush_angle_handle = ui_handle_create();
  385. c.brush_angle_handle.value = 0.0;
  386. c.brush_lazy_radius = 0.0;
  387. c.brush_lazy_step = 0.0;
  388. c.brush_lazy_x = 0.0;
  389. c.brush_lazy_y = 0.0;
  390. c.brush_paint = uv_type_t.UVMAP;
  391. c.brush_angle_reject_dot = 0.5;
  392. c.bake_type = bake_type_t.CURVATURE;
  393. c.bake_axis = bake_axis_t.XYZ;
  394. c.bake_up_axis = bake_up_axis_t.Z;
  395. c.bake_samples = 128;
  396. c.bake_ao_strength = 1.0;
  397. c.bake_ao_radius = 1.0;
  398. c.bake_ao_offset = 1.0;
  399. c.bake_curv_strength = 1.0;
  400. c.bake_curv_radius = 1.0;
  401. c.bake_curv_offset = 0.0;
  402. c.bake_curv_smooth = 1;
  403. c.bake_high_poly = 0;
  404. c.xray = false;
  405. c.sym_x = false;
  406. c.sym_y = false;
  407. c.sym_z = false;
  408. c.fill_type_handle = ui_handle_create();
  409. c.paint2d = false;
  410. c.last_htab0_pos = 0;
  411. c.maximized_sidebar_width = 0;
  412. c.drag_dest = 0;
  413. return c;
  414. }
  415. function context_init() {
  416. context_raw = context_create();
  417. context_ext_init(context_raw);
  418. }
  419. function context_use_deferred(): bool {
  420. return context_raw.render_mode != render_mode_t.FORWARD &&
  421. (context_raw.viewport_mode == viewport_mode_t.LIT || context_raw.viewport_mode == viewport_mode_t.PATH_TRACE) &&
  422. context_raw.tool != tool_type_t.COLORID;
  423. }
  424. function context_select_material(i: i32) {
  425. if (project_materials.length <= i) {
  426. return;
  427. }
  428. context_set_material(project_materials[i]);
  429. }
  430. function context_set_material(m: slot_material_t) {
  431. if (array_index_of(project_materials, m) == -1) {
  432. return;
  433. }
  434. context_raw.material = m;
  435. make_material_parse_paint_material();
  436. ui_base_hwnds[tab_area_t.SIDEBAR1].redraws = 2;
  437. ui_header_handle.redraws = 2;
  438. ui_nodes_hwnd.redraws = 2;
  439. ui_nodes_group_stack = [];
  440. let decal: bool = context_is_decal();
  441. if (decal) {
  442. sys_notify_on_next_frame(util_render_make_decal_preview);
  443. }
  444. }
  445. function context_select_brush(i: i32) {
  446. if (project_brushes.length <= i) {
  447. return;
  448. }
  449. context_set_brush(project_brushes[i]);
  450. }
  451. function context_set_brush(b: slot_brush_t) {
  452. if (array_index_of(project_brushes, b) == -1) {
  453. return;
  454. }
  455. context_raw.brush = b;
  456. make_material_parse_brush();
  457. ui_base_hwnds[tab_area_t.SIDEBAR1].redraws = 2;
  458. ui_nodes_hwnd.redraws = 2;
  459. }
  460. function context_select_font(i: i32) {
  461. if (project_fonts.length <= i) {
  462. return;
  463. }
  464. context_set_font(project_fonts[i]);
  465. }
  466. function context_set_font(f: slot_font_t) {
  467. if (array_index_of(project_fonts, f) == -1) {
  468. return;
  469. }
  470. context_raw.font = f;
  471. util_render_make_text_preview();
  472. util_render_make_decal_preview();
  473. ui_base_hwnds[tab_area_t.STATUS].redraws = 2;
  474. ui_view2d_hwnd.redraws = 2;
  475. }
  476. function context_select_layer(i: i32) {
  477. if (project_layers.length <= i) {
  478. return;
  479. }
  480. context_set_layer(project_layers[i]);
  481. }
  482. function context_set_layer(l: slot_layer_t) {
  483. if (l == context_raw.layer) {
  484. return;
  485. }
  486. context_raw.layer = l;
  487. ui_header_handle.redraws = 2;
  488. let current: gpu_texture_t = _draw_current;
  489. let in_use: bool = gpu_in_use;
  490. if (in_use) draw_end();
  491. layers_set_object_mask();
  492. make_material_parse_mesh_material();
  493. make_material_parse_paint_material();
  494. if (in_use) draw_begin(current);
  495. ui_base_hwnds[tab_area_t.SIDEBAR0].redraws = 2;
  496. ui_view2d_hwnd.redraws = 2;
  497. }
  498. function context_select_tool(i: i32) {
  499. context_raw.tool = i;
  500. make_material_parse_paint_material();
  501. make_material_parse_mesh_material();
  502. context_raw.ddirty = 3;
  503. let _viewport_mode: viewport_mode_t = context_raw.viewport_mode;
  504. context_raw.viewport_mode = viewport_mode_t.MINUS_ONE;
  505. context_set_viewport_mode(_viewport_mode);
  506. context_init_tool();
  507. ui_header_handle.redraws = 2;
  508. ui_toolbar_handle.redraws = 2;
  509. }
  510. function context_init_tool() {
  511. let decal: bool = context_is_decal();
  512. if (decal) {
  513. if (context_raw.tool == tool_type_t.TEXT) {
  514. util_render_make_text_preview();
  515. }
  516. util_render_make_decal_preview();
  517. }
  518. else if (context_raw.tool == tool_type_t.PARTICLE) {
  519. util_particle_init();
  520. }
  521. else if (context_raw.tool == tool_type_t.BAKE) {
  522. // Bake in lit mode for now
  523. if (context_raw.viewport_mode == viewport_mode_t.PATH_TRACE) {
  524. context_raw.viewport_mode = viewport_mode_t.LIT;
  525. }
  526. }
  527. else if (context_raw.tool == tool_type_t.MATERIAL) {
  528. layers_update_fill_layers();
  529. context_main_object().skip_context = null;
  530. }
  531. ///if arm_ios
  532. // No hover on iPad, decals are painted by pen release
  533. config_raw.brush_live = decal;
  534. ///end
  535. }
  536. function context_select_paint_object(o: mesh_object_t) {
  537. context_ext_select_paint_object(o);
  538. }
  539. function context_main_object(): mesh_object_t {
  540. ///if is_lab
  541. return project_paint_objects[0];
  542. ///end
  543. for (let i: i32 = 0; i < project_paint_objects.length; ++i) {
  544. let po: mesh_object_t = project_paint_objects[i];
  545. if (po.base.children.length > 0) {
  546. return po;
  547. }
  548. }
  549. return project_paint_objects[0];
  550. }
  551. function context_layer_filter_used(): bool {
  552. ///if is_lab
  553. return true;
  554. ///end
  555. return context_raw.layer_filter > 0 && context_raw.layer_filter <= project_paint_objects.length;
  556. }
  557. function context_object_mask_used(): bool {
  558. ///if is_lab
  559. return false;
  560. ///end
  561. return slot_layer_get_object_mask(context_raw.layer) > 0 && slot_layer_get_object_mask(context_raw.layer) <= project_paint_objects.length;
  562. }
  563. function context_in_viewport(): bool {
  564. return context_raw.paint_vec.x < 1 && context_raw.paint_vec.x > 0 &&
  565. context_raw.paint_vec.y < 1 && context_raw.paint_vec.y > 0;
  566. }
  567. function context_in_paint_area(): bool {
  568. ///if is_lab
  569. return context_in_viewport();
  570. ///end
  571. let right: i32 = sys_w();
  572. if (ui_view2d_show) {
  573. right += ui_view2d_ww;
  574. }
  575. return mouse_view_x() > 0 && mouse_view_x() < right &&
  576. mouse_view_y() > 0 && mouse_view_y() < sys_h();
  577. }
  578. function context_in_layers(): bool {
  579. let tab: string = ui_hovered_tab_name();
  580. return tab == tr("Layers");
  581. }
  582. function context_in_materials(): bool {
  583. let tab: string = ui_hovered_tab_name();
  584. return tab == tr("Materials");
  585. }
  586. function context_in_2d_view(type: view_2d_type_t = view_2d_type_t.LAYER): bool {
  587. return ui_view2d_show && ui_view2d_type == type &&
  588. mouse_x > ui_view2d_wx && mouse_x < ui_view2d_wx + ui_view2d_ww &&
  589. mouse_y > ui_view2d_wy && mouse_y < ui_view2d_wy + ui_view2d_wh;
  590. }
  591. function context_in_nodes(): bool {
  592. return ui_nodes_show &&
  593. mouse_x > ui_nodes_wx && mouse_x < ui_nodes_wx + ui_nodes_ww &&
  594. mouse_y > ui_nodes_wy && mouse_y < ui_nodes_wy + ui_nodes_wh;
  595. }
  596. function context_in_swatches(): bool {
  597. let tab: string = ui_hovered_tab_name();
  598. return tab == tr("Swatches");
  599. }
  600. function context_in_browser(): bool {
  601. let tab: string = ui_hovered_tab_name();
  602. return tab == tr("Browser");
  603. }
  604. function context_is_picker(): bool {
  605. return context_raw.tool == tool_type_t.PICKER || context_raw.tool == tool_type_t.MATERIAL;
  606. }
  607. function context_is_decal(): bool {
  608. return context_raw.tool == tool_type_t.DECAL || context_raw.tool == tool_type_t.TEXT;
  609. }
  610. function context_is_decal_mask(): bool {
  611. return context_is_decal() && operator_shortcut(map_get(config_keymap, "decal_mask"), shortcut_type_t.DOWN);
  612. }
  613. function context_is_decal_mask_paint(): bool {
  614. return context_is_decal() && operator_shortcut(map_get(config_keymap, "decal_mask") + "+" + map_get(config_keymap, "action_paint"), shortcut_type_t.DOWN);
  615. }
  616. function context_is_floating_toolbar(): bool {
  617. // Header is off -> floating toolbar
  618. return config_raw.layout[layout_size_t.HEADER] == 0;
  619. }
  620. function context_get_area_type(): area_type_t {
  621. if (context_in_viewport()) {
  622. return area_type_t.VIEWPORT;
  623. }
  624. if (context_in_nodes()) {
  625. return area_type_t.NODES;
  626. }
  627. if (context_in_browser()) {
  628. return area_type_t.BROWSER;
  629. }
  630. if (context_in_2d_view()) {
  631. return area_type_t.VIEW2D;
  632. }
  633. if (context_in_layers()) {
  634. return area_type_t.LAYERS;
  635. }
  636. if (context_in_materials()) {
  637. return area_type_t.MATERIALS;
  638. }
  639. return area_type_t.MINUS_ONE;
  640. }
  641. function context_set_viewport_mode(mode: viewport_mode_t) {
  642. if (mode == context_raw.viewport_mode) {
  643. return;
  644. }
  645. context_raw.viewport_mode = mode;
  646. if (context_use_deferred()) {
  647. render_path_commands = render_path_deferred_commands;
  648. }
  649. else {
  650. render_path_commands = render_path_forward_commands;
  651. }
  652. let _workspace: i32 = ui_header_worktab.position;
  653. ui_header_worktab.position = 0;
  654. make_material_parse_mesh_material();
  655. ui_header_worktab.position = _workspace;
  656. }
  657. function context_load_envmap() {
  658. if (!context_raw.envmap_loaded) {
  659. // TODO: Unable to share texture for both radiance and envmap - reload image
  660. context_raw.envmap_loaded = true;
  661. map_delete(data_cached_images, "World_radiance.k");
  662. }
  663. world_data_load_envmap(scene_world);
  664. if (context_raw.saved_envmap == null) {
  665. context_raw.saved_envmap = scene_world._.envmap;
  666. }
  667. }
  668. function context_update_envmap() {
  669. if (context_raw.show_envmap) {
  670. scene_world._.envmap = context_raw.show_envmap_blur ? scene_world._.radiance_mipmaps[0] : context_raw.saved_envmap;
  671. }
  672. else {
  673. scene_world._.envmap = context_raw.empty_envmap;
  674. }
  675. }
  676. function context_set_viewport_shader(viewport_shader: any) { // JSValue * -> (ns: node_shader_t)=>void
  677. context_raw.viewport_shader = viewport_shader;
  678. context_set_render_path();
  679. }
  680. function context_set_render_path() {
  681. if (context_raw.render_mode == render_mode_t.FORWARD || context_raw.viewport_shader != null) {
  682. render_path_commands = render_path_forward_commands;
  683. }
  684. else {
  685. render_path_commands = render_path_deferred_commands;
  686. }
  687. sys_notify_on_next_frame(make_material_parse_mesh_material);
  688. }
  689. function context_enable_import_plugin(file: string): bool {
  690. // Return plugin name suitable for importing the specified file
  691. if (box_preferences_files_plugin == null) {
  692. box_preferences_fetch_plugins();
  693. }
  694. let ext: string = substring(file, string_last_index_of(file, ".") + 1, file.length);
  695. for (let i: i32 = 0; i < box_preferences_files_plugin.length; ++i) {
  696. let f: string = box_preferences_files_plugin[i];
  697. if (starts_with(f, "import_") && string_index_of(f, ext) >= 0) {
  698. config_enable_plugin(f);
  699. console_info(f + " " + tr("plugin enabled"));
  700. return true;
  701. }
  702. }
  703. return false;
  704. }
  705. function context_set_swatch(s: swatch_color_t) {
  706. context_raw.swatch = s;
  707. }