transition-swipe.c 4.6 KB

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