uniforms_ext.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. let uniforms_ext_vec: vec4_t = vec4_create();
  2. let uniforms_ext_ortho_p: mat4_t = mat4_ortho(-0.5, 0.5, -0.5, 0.5, -0.5, 0.5);
  3. function uniforms_ext_init() {
  4. uniforms_i32_links = uniforms_ext_i32_link;
  5. uniforms_f32_links = uniforms_ext_f32_link;
  6. uniforms_vec2_links = uniforms_ext_vec2_link;
  7. uniforms_vec3_links = uniforms_ext_vec3_link;
  8. uniforms_vec4_links = uniforms_ext_vec4_link;
  9. uniforms_mat4_links = uniforms_ext_mat4_link;
  10. uniforms_tex_links = uniforms_ext_tex_link;
  11. }
  12. function uniforms_ext_i32_link(object: object_t, mat: material_data_t, link: string): i32 {
  13. if (link == "_bloomCurrentMip") {
  14. return render_path_base_bloom_current_mip;
  15. }
  16. return 0;
  17. }
  18. function uniforms_ext_f32_link(object: object_t, mat: material_data_t, link: string): f32 {
  19. if (link == "_brushRadius") {
  20. ///if (is_paint || is_sculpt)
  21. let decal: bool = context_raw.tool == workspace_tool_t.DECAL || context_raw.tool == workspace_tool_t.TEXT;
  22. let decal_mask: bool = decal && operator_shortcut(map_get(config_keymap, "decal_mask") + "+" + map_get(config_keymap, "action_paint"), shortcut_type_t.DOWN);
  23. let brush_decal_mask_radius: f32 = context_raw.brush_decal_mask_radius;
  24. if (config_raw.brush_3d) {
  25. brush_decal_mask_radius *= context_raw.paint2d ? 0.55 * ui_view2d_pan_scale : 2.0;
  26. }
  27. let radius: f32 = decal_mask ? brush_decal_mask_radius : context_raw.brush_radius;
  28. let val: f32 = (radius * context_raw.brush_nodes_radius) / 15.0;
  29. if (config_raw.pressure_radius && pen_down()) {
  30. val *= pen_pressure * config_raw.pressure_sensitivity;
  31. }
  32. let scale2d: f32 = (900 / base_h()) * config_raw.window_scale;
  33. if (config_raw.brush_3d && !decal) {
  34. val *= context_raw.paint2d ? 0.55 * scale2d * ui_view2d_pan_scale : 2;
  35. }
  36. else {
  37. val *= scale2d; // Projection ratio
  38. }
  39. ///end
  40. ///if is_lab
  41. let radius: f32 = context_raw.brush_radius;
  42. let val: f32 = radius / 15.0;
  43. if (config_raw.pressure_radius && pen_down()) {
  44. val *= pen_pressure * config_raw.pressure_sensitivity;
  45. }
  46. val *= 2;
  47. ///end
  48. return val;
  49. }
  50. else if (link == "_vignetteStrength") {
  51. return config_raw.rp_vignette;
  52. }
  53. else if (link == "_grainStrength") {
  54. return config_raw.rp_grain;
  55. }
  56. else if (link == "_coneOffset") {
  57. return context_raw.vxao_offset;
  58. }
  59. else if (link == "_coneAperture") {
  60. return context_raw.vxao_aperture;
  61. }
  62. else if (link == "_bloomSampleScale") {
  63. return render_path_base_bloom_sample_scale;
  64. }
  65. ///if (is_paint || is_sculpt)
  66. else if (link == "_brushScaleX") {
  67. return 1 / context_raw.brush_scale_x;
  68. }
  69. else if (link == "_brushOpacity") {
  70. let val: f32 = context_raw.brush_opacity * context_raw.brush_nodes_opacity;
  71. if (config_raw.pressure_opacity && pen_down()) {
  72. val *= pen_pressure * config_raw.pressure_sensitivity;
  73. }
  74. return val;
  75. }
  76. else if (link == "_brushHardness") {
  77. let decal_mask: bool = operator_shortcut(map_get(config_keymap, "decal_mask") + "+" + map_get(config_keymap, "action_paint"), shortcut_type_t.DOWN);
  78. if (context_raw.tool != workspace_tool_t.BRUSH && context_raw.tool != workspace_tool_t.ERASER && context_raw.tool != workspace_tool_t.CLONE && !decal_mask) {
  79. return 1.0;
  80. }
  81. let val: f32 = context_raw.brush_hardness * context_raw.brush_nodes_hardness;
  82. if (config_raw.pressure_hardness && pen_down()) {
  83. val *= pen_pressure * config_raw.pressure_sensitivity;
  84. }
  85. if (config_raw.brush_3d) {
  86. if (context_raw.paint2d) {
  87. val *= 1.0 / ui_view2d_pan_scale;
  88. }
  89. else {
  90. val *= val;
  91. }
  92. }
  93. return val;
  94. }
  95. else if (link == "_brushScale") {
  96. let fill: bool = context_raw.layer.fill_layer != null;
  97. let val: f32 = (fill ? context_raw.layer.scale : context_raw.brush_scale) * context_raw.brush_nodes_scale;
  98. return val;
  99. }
  100. else if (link == "_objectId") {
  101. return array_index_of(project_paint_objects, object.ext);
  102. }
  103. ///if is_paint
  104. else if (link == "_dilateRadius") {
  105. return util_uv_dilatemap != null ? config_raw.dilate_radius : 0.0;
  106. }
  107. ///end
  108. else if (link == "_decalLayerDim") {
  109. return mat4_get_scale(context_raw.layer.decal_mat).z * 0.5;
  110. }
  111. else if (link == "_pickerOpacity") {
  112. return context_raw.picked_color.opacity;
  113. }
  114. else if (link == "_pickerOcclusion") {
  115. return context_raw.picked_color.occlusion;
  116. }
  117. else if (link == "_pickerRoughness") {
  118. return context_raw.picked_color.roughness;
  119. }
  120. else if (link == "_pickerMetallic") {
  121. return context_raw.picked_color.metallic;
  122. }
  123. else if (link == "_pickerHeight") {
  124. return context_raw.picked_color.height;
  125. }
  126. ///end
  127. if (parser_material_script_links != null) {
  128. let keys: string[] = map_keys(parser_material_script_links);
  129. for (let i: i32 = 0; i < keys.length; ++i) {
  130. let key: string = keys[i];
  131. let asciprt_links: any = parser_material_script_links;
  132. let script: string = asciprt_links[key];
  133. let result: f32 = 0.0;
  134. if (script != "") {
  135. result = js_eval(script);
  136. }
  137. return result;
  138. }
  139. }
  140. return 0.0;
  141. }
  142. function uniforms_ext_vec2_link(object: object_t, mat: material_data_t, link: string): vec4_t {
  143. if (link == "_gbufferSize") {
  144. vec4_set(uniforms_ext_vec, 0, 0, 0);
  145. let gbuffer2: render_target_t = map_get(render_path_render_targets, "gbuffer2");
  146. vec4_set(uniforms_ext_vec, gbuffer2._image.width, gbuffer2._image.height, 0);
  147. return uniforms_ext_vec;
  148. }
  149. else if (link == "_cloneDelta") {
  150. vec4_set(uniforms_ext_vec, context_raw.clone_delta_x, context_raw.clone_delta_y, 0);
  151. return uniforms_ext_vec;
  152. }
  153. else if (link == "_texpaintSize") {
  154. vec4_set(uniforms_ext_vec, config_get_texture_res_x(), config_get_texture_res_y(), 0);
  155. return uniforms_ext_vec;
  156. }
  157. ///if (is_paint || is_sculpt)
  158. else if (link == "_brushAngle") {
  159. let brush_angle: f32 = context_raw.brush_angle + context_raw.brush_nodes_angle;
  160. let angle: f32 = context_raw.layer.fill_layer != null ? context_raw.layer.angle : brush_angle;
  161. angle *= (math_pi() / 180);
  162. if (config_raw.pressure_angle && pen_down()) {
  163. angle *= pen_pressure * config_raw.pressure_sensitivity;
  164. }
  165. vec4_set(uniforms_ext_vec, math_cos(-angle), math_sin(-angle), 0);
  166. return uniforms_ext_vec;
  167. }
  168. ///end
  169. return null;
  170. }
  171. function uniforms_ext_vec3_link(object: object_t, mat: material_data_t, link: string): vec4_t {
  172. let v: vec4_t = null;
  173. ///if (is_paint || is_sculpt)
  174. if (link == "_brushDirection") {
  175. v = _uniforms_vec;
  176. // Discard first paint for directional brush
  177. let allow_paint: bool = context_raw.prev_paint_vec_x != context_raw.last_paint_vec_x &&
  178. context_raw.prev_paint_vec_y != context_raw.last_paint_vec_y &&
  179. context_raw.prev_paint_vec_x > 0 &&
  180. context_raw.prev_paint_vec_y > 0;
  181. let x: f32 = context_raw.paint_vec.x;
  182. let y: f32 = context_raw.paint_vec.y;
  183. let lastx: f32 = context_raw.prev_paint_vec_x;
  184. let lasty: f32 = context_raw.prev_paint_vec_y;
  185. if (context_raw.paint2d) {
  186. x = vec2d(x);
  187. lastx = vec2d(lastx);
  188. }
  189. let angle: f32 = math_atan2(-y + lasty, x - lastx) - math_pi() / 2;
  190. vec4_set(v, math_cos(angle), math_sin(angle), allow_paint ? 1 : 0);
  191. context_raw.prev_paint_vec_x = context_raw.last_paint_vec_x;
  192. context_raw.prev_paint_vec_y = context_raw.last_paint_vec_y;
  193. return v;
  194. }
  195. else if (link == "_decalLayerLoc") {
  196. v = _uniforms_vec;
  197. vec4_set(v, context_raw.layer.decal_mat.m[12], context_raw.layer.decal_mat.m[13], context_raw.layer.decal_mat.m[14]);
  198. return v;
  199. }
  200. else if (link == "_decalLayerNor") {
  201. v = _uniforms_vec;
  202. vec4_normalize(vec4_set(v, context_raw.layer.decal_mat.m[8], context_raw.layer.decal_mat.m[9], context_raw.layer.decal_mat.m[10]));
  203. return v;
  204. }
  205. else if (link == "_pickerBase") {
  206. v = _uniforms_vec;
  207. vec4_set(v,
  208. color_get_rb(context_raw.picked_color.base) / 255,
  209. color_get_gb(context_raw.picked_color.base) / 255,
  210. color_get_bb(context_raw.picked_color.base) / 255
  211. );
  212. return v;
  213. }
  214. else if (link == "_pickerNormal") {
  215. v = _uniforms_vec;
  216. vec4_set(v,
  217. color_get_rb(context_raw.picked_color.normal) / 255,
  218. color_get_gb(context_raw.picked_color.normal) / 255,
  219. color_get_bb(context_raw.picked_color.normal) / 255
  220. );
  221. return v;
  222. }
  223. ///if arm_physics
  224. else if (link == "_particleHit") {
  225. v = _uniforms_vec;
  226. vec4_set(v, context_raw.particle_hit_x, context_raw.particle_hit_y, context_raw.particle_hit_z);
  227. return v;
  228. }
  229. else if (link == "_particleHitLast") {
  230. v = _uniforms_vec;
  231. vec4_set(v, context_raw.last_particle_hit_x, context_raw.last_particle_hit_y, context_raw.last_particle_hit_z);
  232. return v;
  233. }
  234. ///end
  235. ///end
  236. return v;
  237. }
  238. ///if (is_paint || is_sculpt)
  239. function vec2d(x: f32) {
  240. // Transform from 3d viewport coord to 2d view coord
  241. context_raw.paint2d_view = false;
  242. let res: f32 = (x * base_w() - base_w()) / ui_view2d_ww;
  243. context_raw.paint2d_view = true;
  244. return res;
  245. }
  246. ///end
  247. function uniforms_ext_vec4_link(object: object_t, mat: material_data_t, link: string): vec4_t {
  248. if (link == "_inputBrush") {
  249. let down: bool = mouse_down() || pen_down();
  250. vec4_set(uniforms_ext_vec, context_raw.paint_vec.x, context_raw.paint_vec.y, down ? 1.0 : 0.0, 0.0);
  251. ///if (is_paint || is_sculpt)
  252. if (context_raw.paint2d) {
  253. uniforms_ext_vec.x = vec2d(uniforms_ext_vec.x);
  254. }
  255. ///end
  256. return uniforms_ext_vec;
  257. }
  258. else if (link == "_inputBrushLast") {
  259. let down: bool = mouse_down() || pen_down();
  260. vec4_set(uniforms_ext_vec, context_raw.last_paint_vec_x, context_raw.last_paint_vec_y, down ? 1.0 : 0.0, 0.0);
  261. ///if (is_paint || is_sculpt)
  262. if (context_raw.paint2d) {
  263. uniforms_ext_vec.x = vec2d(uniforms_ext_vec.x);
  264. }
  265. ///end
  266. return uniforms_ext_vec;
  267. }
  268. else if (link == "_envmapData") {
  269. vec4_set(uniforms_ext_vec, context_raw.envmap_angle, math_sin(-context_raw.envmap_angle), math_cos(-context_raw.envmap_angle), scene_world.strength);
  270. return uniforms_ext_vec;
  271. }
  272. else if (link == "_envmapDataWorld") {
  273. vec4_set(uniforms_ext_vec, context_raw.envmap_angle, math_sin(-context_raw.envmap_angle), math_cos(-context_raw.envmap_angle), context_raw.show_envmap ? scene_world.strength : 1.0);
  274. return uniforms_ext_vec;
  275. }
  276. ///if (is_paint || is_sculpt)
  277. else if (link == "_stencilTransform") {
  278. vec4_set(uniforms_ext_vec, context_raw.brush_stencil_x, context_raw.brush_stencil_y, context_raw.brush_stencil_scale, context_raw.brush_stencil_angle);
  279. if (context_raw.paint2d) uniforms_ext_vec.x = vec2d(uniforms_ext_vec.x);
  280. return uniforms_ext_vec;
  281. }
  282. else if (link == "_decalMask") {
  283. let decal_mask: bool = operator_shortcut(map_get(config_keymap, "decal_mask") + "+" + map_get(config_keymap, "action_paint"), shortcut_type_t.DOWN);
  284. let val: f32 = (context_raw.brush_radius * context_raw.brush_nodes_radius) / 15.0;
  285. let scale2d: f32 = (900 / base_h()) * config_raw.window_scale;
  286. val *= scale2d; // Projection ratio
  287. vec4_set(uniforms_ext_vec, context_raw.decal_x, context_raw.decal_y, decal_mask ? 1 : 0, val);
  288. if (context_raw.paint2d) {
  289. uniforms_ext_vec.x = vec2d(uniforms_ext_vec.x);
  290. }
  291. return uniforms_ext_vec;
  292. }
  293. ///end
  294. return null;
  295. }
  296. function uniforms_ext_mat4_link(object: object_t, mat: material_data_t, link: string): mat4_t {
  297. ///if (is_paint || is_sculpt)
  298. if (link == "_decalLayerMatrix") { // Decal layer
  299. let m: mat4_t = _uniforms_mat;
  300. mat4_set_from(m, context_raw.layer.decal_mat);
  301. mat4_get_inv(m, m);
  302. mat4_mult_mat(m, uniforms_ext_ortho_p);
  303. return m;
  304. }
  305. ///end
  306. return null;
  307. }
  308. function uniforms_ext_tex_link(object: object_t, mat: material_data_t, link: string): image_t {
  309. if (link == "_texpaint_undo") {
  310. ///if (is_paint || is_sculpt)
  311. let i: i32 = history_undo_i - 1 < 0 ? config_raw.undo_steps - 1 : history_undo_i - 1;
  312. return map_get(render_path_render_targets, "texpaint_undo" + i)._image;
  313. ///end
  314. ///if is_lab
  315. return null;
  316. ///end
  317. }
  318. else if (link == "_texpaint_nor_undo") {
  319. ///if (is_paint || is_sculpt)
  320. let i: i32 = history_undo_i - 1 < 0 ? config_raw.undo_steps - 1 : history_undo_i - 1;
  321. return map_get(render_path_render_targets, "texpaint_nor_undo" + i)._image;
  322. ///end
  323. ///if is_lab
  324. return null;
  325. ///end
  326. }
  327. else if (link == "_texpaint_pack_undo") {
  328. ///if (is_paint || is_sculpt)
  329. let i: i32 = history_undo_i - 1 < 0 ? config_raw.undo_steps - 1 : history_undo_i - 1;
  330. return map_get(render_path_render_targets, "texpaint_pack_undo" + i)._image;
  331. ///end
  332. ///if is_lab
  333. return null;
  334. ///end
  335. }
  336. else if (link == "_ltcMat") {
  337. if (const_data_ltc_mat_tex == null) {
  338. const_data_init_ltc();
  339. }
  340. return const_data_ltc_mat_tex;
  341. }
  342. else if (link == "_ltcMag") {
  343. if (const_data_ltc_mag_tex == null) {
  344. const_data_init_ltc();
  345. }
  346. return const_data_ltc_mag_tex;
  347. }
  348. ///if (is_paint || is_sculpt)
  349. else if (link == "_texcolorid") {
  350. if (project_assets.length == 0) {
  351. return map_get(render_path_render_targets, "empty_white")._image;
  352. }
  353. else {
  354. return project_get_image(project_assets[context_raw.colorid_handle.position]);
  355. }
  356. }
  357. else if (link == "_textexttool") { // Opacity map for text
  358. return context_raw.text_tool_image;
  359. }
  360. else if (link == "_texbrushmask") {
  361. return context_raw.brush_mask_image;
  362. }
  363. else if (link == "_texbrushstencil") {
  364. return context_raw.brush_stencil_image;
  365. }
  366. else if (link == "_texparticle") {
  367. return map_get(render_path_render_targets, "texparticle")._image;
  368. }
  369. ///end
  370. ///if is_paint
  371. else if (link == "_texuvmap") {
  372. if (!util_uv_uvmap_cached) {
  373. app_notify_on_init(util_uv_cache_uv_map);
  374. }
  375. return util_uv_uvmap;
  376. }
  377. else if (link == "_textrianglemap") {
  378. if (!util_uv_trianglemap_cached) {
  379. app_notify_on_init(util_uv_cache_triangle_map);
  380. }
  381. return util_uv_trianglemap;
  382. }
  383. else if (link == "_texuvislandmap") {
  384. app_notify_on_init(util_uv_cache_uv_island_map);
  385. return util_uv_uvislandmap_cached ? util_uv_uvislandmap :map_get(render_path_render_targets, "empty_black")._image;
  386. }
  387. else if (link == "_texdilatemap") {
  388. return util_uv_dilatemap;
  389. }
  390. ///end
  391. if (starts_with(link, "_texpaint_pack_vert")) {
  392. let tid: string = substring(link, link.length - 1, link.length);
  393. return map_get(render_path_render_targets, "texpaint_pack" + tid)._image;
  394. }
  395. if (starts_with(link, "_texpaint_vert")) {
  396. ///if (is_paint || is_sculpt)
  397. let tid: i32 = Number(substring(link, link.length - 1, link.length));
  398. return tid < project_layers.length ? project_layers[tid].texpaint : null;
  399. ///end
  400. ///if is_lab
  401. return brush_output_node_inst.texpaint;
  402. ///end
  403. }
  404. if (starts_with(link, "_texpaint_nor")) {
  405. ///if is_paint
  406. let tid: i32 = Number(substring(link, link.length - 1, link.length));
  407. return tid < project_layers.length ? project_layers[tid].texpaint_nor : null;
  408. ///end
  409. ///if is_lab
  410. return brush_output_node_inst.texpaint_nor;
  411. ///end
  412. }
  413. if (starts_with(link, "_texpaint_pack")) {
  414. ///if is_paint
  415. let tid: i32 = Number(substring(link, link.length - 1, link.length));
  416. return tid < project_layers.length ? project_layers[tid].texpaint_pack : null;
  417. ///end
  418. ///if is_lab
  419. return brush_output_node_inst.texpaint_pack;
  420. ///end
  421. }
  422. if (starts_with(link, "_texpaint")) {
  423. ///if (is_paint || is_sculpt)
  424. let tid: i32 = Number(substring(link, link.length - 1, link.length));
  425. return tid < project_layers.length ? project_layers[tid].texpaint : null;
  426. ///end
  427. ///if is_lab
  428. return brush_output_node_inst.texpaint;
  429. ///end
  430. }
  431. ///if (is_paint || is_sculpt)
  432. if (starts_with(link, "_texblur_")) {
  433. let id: string = substring(link, 9, link.length);
  434. return context_raw.node_previews != null ? map_get(context_raw.node_previews, id) :map_get(render_path_render_targets, "empty_black")._image;
  435. }
  436. if (starts_with(link, "_texwarp_")) {
  437. let id: string = substring(link, 9, link.length);
  438. return context_raw.node_previews != null ? map_get(context_raw.node_previews, id) :map_get(render_path_render_targets, "empty_black")._image;
  439. }
  440. if (starts_with(link, "_texbake_")) {
  441. let id: string = substring(link, 9, link.length);
  442. return context_raw.node_previews != null ? map_get(context_raw.node_previews, id) :map_get(render_path_render_targets, "empty_black")._image;
  443. }
  444. ///end
  445. return null;
  446. }