forked from katarai/obs-whiteboard-lua
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsource.lua
More file actions
136 lines (103 loc) · 3.53 KB
/
source.lua
File metadata and controls
136 lines (103 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
bit = require("bit")
obs = obslua
local create_renderer = require('renderer').create_renderer
local utils = require('utils')
local M = {}
function M.create_source(whiteboard)
local source = {}
source.id = "whiteboard"
source.output_flags = bit.bor(obs.OBS_SOURCE_VIDEO, obs.OBS_SOURCE_CUSTOM_DRAW)
function ensure_renderer_valid(source)
local video_info = obs.obs_video_info()
if not obs.obs_get_video_info(video_info) then
print "Failed to get video resolution"
return
end
if video_info.base_width == source.width and video_info.base_height == source.height then
return
end
source.width = video_info.base_width
source.height = video_info.base_height
source.renderer = create_renderer(source, whiteboard)
end
function redraw_canvas(graphics, whiteboard)
graphics.render_to_texture(graphics.canvas_texture, function()
graphics.clear()
graphics.draw_lines(whiteboard.lines)
for _, line in ipairs(whiteboard.lines) do
if line.arrow and line.color ~= 0 and #(line.points) > 1 then
graphics.draw_arrow_head(line)
end
end
end)
end
function clear_ui(graphics)
graphics.render_to_texture(graphics.ui_texture, function()
graphics.clear()
end)
end
source.get_name = function()
return "Whiteboard"
end
source.create = function()
local source = {}
source.active = false
ensure_renderer_valid(source)
return source
end
source.destroy = function(source)
source.active = false
source.renderer.destroy()
end
source.video_tick = function(source, dt)
local renderer = source.renderer
ensure_renderer_valid(source)
if not renderer.ready() then
return
end
if not source.active then
return
end
renderer.enter_graphics(function(graphics)
if whiteboard.needs_redraw then
redraw_canvas(graphics, whiteboard)
whiteboard.needs_redraw = false
end
clear_ui(graphics)
whiteboard.update(source, graphics)
end)
end
source.video_render = function(source, effect)
effect = obs.obs_get_base_effect(obs.OBS_EFFECT_DEFAULT)
if effect and source.renderer.ready() then
obs.gs_blend_state_push()
obs.gs_reset_blend_state()
obs.gs_matrix_push()
obs.gs_matrix_identity()
obs.gs_blend_function(obs.GS_BLEND_ONE, obs.GS_BLEND_INVSRCALPHA)
while obs.gs_effect_loop(effect, "Draw") do
obs.obs_source_draw(source.renderer.graphics.canvas_texture.render_target, 0, 0, 0, 0, false);
obs.obs_source_draw(source.renderer.graphics.ui_texture.render_target, 0, 0, 0, 0, false);
end
obs.gs_matrix_pop()
obs.gs_blend_state_pop()
end
end
source.get_width = function(source)
return 0
end
source.get_height = function(source)
return 0
end
source.activate = function(source)
local scene = obs.obs_frontend_get_current_scene()
whiteboard.scene_name = obs.obs_source_get_name(scene)
source.active = true
obs.obs_source_release(scene)
end
source.deactivate = function(source)
source.active = false
end
return source
end
return M