transition-swipe.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #include <obs-module.h>
  2. #include <graphics/vec2.h>
  3. #include "easings.h"
  4. struct swipe_info {
  5. obs_source_t *source;
  6. gs_effect_t *effect;
  7. gs_eparam_t *a_param;
  8. gs_eparam_t *b_param;
  9. gs_eparam_t *swipe_param;
  10. struct vec2 dir;
  11. bool swipe_in;
  12. };
  13. #define S_DIRECTION "direction"
  14. #define S_SWIPE_IN "swipe_in"
  15. static const char *swipe_get_name(void *type_data)
  16. {
  17. UNUSED_PARAMETER(type_data);
  18. return obs_module_text("SwipeTransition");
  19. }
  20. static void *swipe_create(obs_data_t *settings, obs_source_t *source)
  21. {
  22. struct swipe_info *swipe;
  23. char *file = obs_module_file("swipe_transition.effect");
  24. gs_effect_t *effect;
  25. obs_enter_graphics();
  26. effect = gs_effect_create_from_file(file, NULL);
  27. obs_leave_graphics();
  28. bfree(file);
  29. if (!effect) {
  30. blog(LOG_ERROR, "Could not find swipe_transition.effect");
  31. return NULL;
  32. }
  33. swipe = bmalloc(sizeof(*swipe));
  34. swipe->source = source;
  35. swipe->effect = effect;
  36. swipe->a_param = gs_effect_get_param_by_name(effect, "tex_a");
  37. swipe->b_param = gs_effect_get_param_by_name(effect, "tex_b");
  38. swipe->swipe_param = gs_effect_get_param_by_name(effect, "swipe_val");
  39. obs_source_update(source, settings);
  40. return swipe;
  41. }
  42. static void swipe_destroy(void *data)
  43. {
  44. struct swipe_info *swipe = data;
  45. bfree(swipe);
  46. }
  47. static void swipe_update(void *data, obs_data_t *settings)
  48. {
  49. struct swipe_info *swipe = data;
  50. const char *dir = obs_data_get_string(settings, S_DIRECTION);
  51. swipe->swipe_in = obs_data_get_bool(settings, S_SWIPE_IN);
  52. if (strcmp(dir, "right") == 0)
  53. swipe->dir = (struct vec2){-1.0f, 0.0f};
  54. else if (strcmp(dir, "up") == 0)
  55. swipe->dir = (struct vec2){0.0f, 1.0f};
  56. else if (strcmp(dir, "down") == 0)
  57. swipe->dir = (struct vec2){0.0f, -1.0f};
  58. else /* left */
  59. swipe->dir = (struct vec2){1.0f, 0.0f};
  60. }
  61. static void swipe_callback(void *data, gs_texture_t *a, gs_texture_t *b, float t, uint32_t cx, uint32_t cy)
  62. {
  63. struct swipe_info *swipe = data;
  64. struct vec2 swipe_val = swipe->dir;
  65. if (swipe->swipe_in)
  66. vec2_neg(&swipe_val, &swipe_val);
  67. t = cubic_ease_in_out(t);
  68. vec2_mulf(&swipe_val, &swipe_val, swipe->swipe_in ? 1.0f - t : t);
  69. const bool previous = gs_framebuffer_srgb_enabled();
  70. gs_enable_framebuffer_srgb(true);
  71. gs_effect_set_texture_srgb(swipe->a_param, swipe->swipe_in ? b : a);
  72. gs_effect_set_texture_srgb(swipe->b_param, swipe->swipe_in ? a : b);
  73. gs_effect_set_vec2(swipe->swipe_param, &swipe_val);
  74. while (gs_effect_loop(swipe->effect, "Swipe"))
  75. gs_draw_sprite(NULL, 0, cx, cy);
  76. gs_enable_framebuffer_srgb(previous);
  77. }
  78. static void swipe_video_render(void *data, gs_effect_t *effect)
  79. {
  80. struct swipe_info *swipe = data;
  81. obs_transition_video_render(swipe->source, swipe_callback);
  82. UNUSED_PARAMETER(effect);
  83. }
  84. static float mix_a(void *data, float t)
  85. {
  86. UNUSED_PARAMETER(data);
  87. return 1.0f - cubic_ease_in_out(t);
  88. }
  89. static float mix_b(void *data, float t)
  90. {
  91. UNUSED_PARAMETER(data);
  92. return cubic_ease_in_out(t);
  93. }
  94. static bool swipe_audio_render(void *data, uint64_t *ts_out, struct obs_source_audio_mix *audio, uint32_t mixers,
  95. size_t channels, size_t sample_rate)
  96. {
  97. struct swipe_info *swipe = data;
  98. return obs_transition_audio_render(swipe->source, ts_out, audio, mixers, channels, sample_rate, mix_a, mix_b);
  99. }
  100. static obs_properties_t *swipe_properties(void *data)
  101. {
  102. obs_properties_t *ppts = obs_properties_create();
  103. obs_property_t *p;
  104. p = obs_properties_add_list(ppts, S_DIRECTION, obs_module_text("Direction"), OBS_COMBO_TYPE_LIST,
  105. OBS_COMBO_FORMAT_STRING);
  106. obs_property_list_add_string(p, obs_module_text("Direction.Left"), "left");
  107. obs_property_list_add_string(p, obs_module_text("Direction.Right"), "right");
  108. obs_property_list_add_string(p, obs_module_text("Direction.Up"), "up");
  109. obs_property_list_add_string(p, obs_module_text("Direction.Down"), "down");
  110. obs_properties_add_bool(ppts, S_SWIPE_IN, obs_module_text("SwipeIn"));
  111. UNUSED_PARAMETER(data);
  112. return ppts;
  113. }
  114. static enum gs_color_space swipe_video_get_color_space(void *data, size_t count,
  115. const enum gs_color_space *preferred_spaces)
  116. {
  117. UNUSED_PARAMETER(count);
  118. UNUSED_PARAMETER(preferred_spaces);
  119. struct swipe_info *const swipe = data;
  120. return obs_transition_video_get_color_space(swipe->source);
  121. }
  122. struct obs_source_info swipe_transition = {
  123. .id = "swipe_transition",
  124. .type = OBS_SOURCE_TYPE_TRANSITION,
  125. .get_name = swipe_get_name,
  126. .create = swipe_create,
  127. .destroy = swipe_destroy,
  128. .update = swipe_update,
  129. .video_render = swipe_video_render,
  130. .audio_render = swipe_audio_render,
  131. .get_properties = swipe_properties,
  132. .video_get_color_space = swipe_video_get_color_space,
  133. };