transition-swipe.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. UNUSED_PARAMETER(settings);
  41. return swipe;
  42. }
  43. static void swipe_destroy(void *data)
  44. {
  45. struct swipe_info *swipe = data;
  46. bfree(swipe);
  47. }
  48. static void swipe_update(void *data, obs_data_t *settings)
  49. {
  50. struct swipe_info *swipe = data;
  51. const char *dir = obs_data_get_string(settings, S_DIRECTION);
  52. swipe->swipe_in = obs_data_get_bool(settings, S_SWIPE_IN);
  53. if (strcmp(dir, "right") == 0)
  54. swipe->dir = (struct vec2){-1.0f, 0.0f};
  55. else if (strcmp(dir, "up") == 0)
  56. swipe->dir = (struct vec2){0.0f, 1.0f};
  57. else if (strcmp(dir, "down") == 0)
  58. swipe->dir = (struct vec2){0.0f, -1.0f};
  59. else /* left */
  60. swipe->dir = (struct vec2){1.0f, 0.0f};
  61. }
  62. static void swipe_callback(void *data, gs_texture_t *a, gs_texture_t *b,
  63. float t, uint32_t cx, uint32_t cy)
  64. {
  65. struct swipe_info *swipe = data;
  66. struct vec2 swipe_val = swipe->dir;
  67. if (swipe->swipe_in)
  68. vec2_neg(&swipe_val, &swipe_val);
  69. t = cubic_ease_in_out(t);
  70. vec2_mulf(&swipe_val, &swipe_val, swipe->swipe_in ? 1.0f - t : t);
  71. const bool previous = gs_framebuffer_srgb_enabled();
  72. gs_enable_framebuffer_srgb(true);
  73. gs_effect_set_texture_srgb(swipe->a_param, swipe->swipe_in ? b : a);
  74. gs_effect_set_texture_srgb(swipe->b_param, swipe->swipe_in ? a : b);
  75. gs_effect_set_vec2(swipe->swipe_param, &swipe_val);
  76. while (gs_effect_loop(swipe->effect, "Swipe"))
  77. gs_draw_sprite(NULL, 0, cx, cy);
  78. gs_enable_framebuffer_srgb(previous);
  79. }
  80. static void swipe_video_render(void *data, gs_effect_t *effect)
  81. {
  82. struct swipe_info *swipe = data;
  83. obs_transition_video_render(swipe->source, swipe_callback);
  84. UNUSED_PARAMETER(effect);
  85. }
  86. static float mix_a(void *data, float t)
  87. {
  88. UNUSED_PARAMETER(data);
  89. return 1.0f - cubic_ease_in_out(t);
  90. }
  91. static float mix_b(void *data, float t)
  92. {
  93. UNUSED_PARAMETER(data);
  94. return cubic_ease_in_out(t);
  95. }
  96. static bool swipe_audio_render(void *data, uint64_t *ts_out,
  97. struct obs_source_audio_mix *audio,
  98. uint32_t mixers, size_t channels,
  99. size_t sample_rate)
  100. {
  101. struct swipe_info *swipe = data;
  102. return obs_transition_audio_render(swipe->source, ts_out, audio, mixers,
  103. channels, sample_rate, mix_a, mix_b);
  104. }
  105. static obs_properties_t *swipe_properties(void *data)
  106. {
  107. obs_properties_t *ppts = obs_properties_create();
  108. obs_property_t *p;
  109. p = obs_properties_add_list(ppts, S_DIRECTION,
  110. obs_module_text("Direction"),
  111. OBS_COMBO_TYPE_LIST,
  112. OBS_COMBO_FORMAT_STRING);
  113. obs_property_list_add_string(p, obs_module_text("Direction.Left"),
  114. "left");
  115. obs_property_list_add_string(p, obs_module_text("Direction.Right"),
  116. "right");
  117. obs_property_list_add_string(p, obs_module_text("Direction.Up"), "up");
  118. obs_property_list_add_string(p, obs_module_text("Direction.Down"),
  119. "down");
  120. obs_properties_add_bool(ppts, S_SWIPE_IN, obs_module_text("SwipeIn"));
  121. UNUSED_PARAMETER(data);
  122. return ppts;
  123. }
  124. static enum gs_color_space
  125. swipe_video_get_color_space(void *data, size_t count,
  126. const enum gs_color_space *preferred_spaces)
  127. {
  128. UNUSED_PARAMETER(count);
  129. UNUSED_PARAMETER(preferred_spaces);
  130. struct swipe_info *const swipe = data;
  131. return obs_transition_video_get_color_space(swipe->source);
  132. }
  133. struct obs_source_info swipe_transition = {
  134. .id = "swipe_transition",
  135. .type = OBS_SOURCE_TYPE_TRANSITION,
  136. .get_name = swipe_get_name,
  137. .create = swipe_create,
  138. .destroy = swipe_destroy,
  139. .update = swipe_update,
  140. .video_render = swipe_video_render,
  141. .audio_render = swipe_audio_render,
  142. .get_properties = swipe_properties,
  143. .video_get_color_space = swipe_video_get_color_space,
  144. };