context.ts 23 KB

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