transition-swipe.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. gs_effect_set_texture(swipe->a_param, swipe->swipe_in ? b : a);
  72. gs_effect_set_texture(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. }
  77. static void swipe_video_render(void *data, gs_effect_t *effect)
  78. {
  79. struct swipe_info *swipe = data;
  80. obs_transition_video_render(swipe->source, swipe_callback);
  81. UNUSED_PARAMETER(effect);
  82. }
  83. static float mix_a(void *data, float t)
  84. {
  85. UNUSED_PARAMETER(data);
  86. return 1.0f - cubic_ease_in_out(t);
  87. }
  88. static float mix_b(void *data, float t)
  89. {
  90. UNUSED_PARAMETER(data);
  91. return cubic_ease_in_out(t);
  92. }
  93. static bool swipe_audio_render(void *data, uint64_t *ts_out,
  94. struct obs_source_audio_mix *audio,
  95. uint32_t mixers, size_t channels,
  96. size_t sample_rate)
  97. {
  98. struct swipe_info *swipe = data;
  99. return obs_transition_audio_render(swipe->source, ts_out, audio, mixers,
  100. channels, sample_rate, mix_a, mix_b);
  101. }
  102. static obs_properties_t *swipe_properties(void *data)
  103. {
  104. obs_properties_t *ppts = obs_properties_create();
  105. obs_property_t *p;
  106. p = obs_properties_add_list(ppts, S_DIRECTION,
  107. obs_module_text("Direction"),
  108. OBS_COMBO_TYPE_LIST,
  109. OBS_COMBO_FORMAT_STRING);
  110. obs_property_list_add_string(p, obs_module_text("Direction.Left"),
  111. "left");
  112. obs_property_list_add_string(p, obs_module_text("Direction.Right"),
  113. "right");
  114. obs_property_list_add_string(p, obs_module_text("Direction.Up"), "up");
  115. obs_property_list_add_string(p, obs_module_text("Direction.Down"),
  116. "down");
  117. obs_properties_add_bool(ppts, S_SWIPE_IN, obs_module_text("SwipeIn"));
  118. UNUSED_PARAMETER(data);
  119. return ppts;
  120. }
  121. struct obs_source_info swipe_transition = {
  122. .id = "swipe_transition",
  123. .type = OBS_SOURCE_TYPE_TRANSITION,
  124. .get_name = swipe_get_name,
  125. .create = swipe_create,
  126. .destroy = swipe_destroy,
  127. .update = swipe_update,
  128. .video_render = swipe_video_render,
  129. .audio_render = swipe_audio_render,
  130. .get_properties = swipe_properties,
  131. };