-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomCloneEffectProcessor.cs
More file actions
376 lines (307 loc) · 15.6 KB
/
CustomCloneEffectProcessor.cs
File metadata and controls
376 lines (307 loc) · 15.6 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
using CustomCloneEffectPlugin;
using Vortice;
using Vortice.Direct2D1;
using Vortice.Direct2D1.Effects;
using YukkuriMovieMaker.Commons;
using YukkuriMovieMaker.Player.Video;
namespace CustumCloneEffectPlugin
{
/// <summary>
/// エフェクトのパラメータ側で大苦戦していて、こちらは全く手を着けていないので、描画時にエラーが発生します。
/// こちらは自分で何とかします。
/// </summary>
internal class CustomCloneEffectProcessor : IVideoEffectProcessor
{
readonly CustomCloneEffect item;
readonly AffineTransform2D transformEffect;
IGraphicsDevicesAndContext devices;
ID2D1CommandList? commandList = null;
ID2D1Image? input;
bool isFirst = true;
public ID2D1Image Output { get; }
/*
readonly List<Crop> cropEffects;
readonly List<AffineTransform2D> transforms;
readonly List<Opacity> opacityEffects;
readonly List<GaussianBlur> blurEffects;
readonly List<ID2D1Image?> outputs;
*/
private List<CloneNode> CloneNodes = [];
private int numOfClones = 0;
RawRectF inputRect;
public CustomCloneEffectProcessor(IGraphicsDevicesAndContext devices, CustomCloneEffect item)
{
this.item = item;
this.devices = devices;
/*
cropEffects = new List<Crop>();
transformEffect = new AffineTransform2D(devices.DeviceContext);//Outputのインスタンスを固定するために、間にエフェクトを挟む
opacityEffects = new List<Opacity>();
blurEffects = new List<GaussianBlur>();
*/
transformEffect = new AffineTransform2D(devices.DeviceContext);//Outputのインスタンスを固定するために、間にエフェクトを挟む
Output = transformEffect.Output;//EffectからgetしたOutputは必ずDisposeする必要がある。Effect側では開放されない。
}
public DrawDescription Update(EffectDescription effectDescription)
{
var frame = effectDescription.ItemPosition.Frame;
var length = effectDescription.ItemDuration.Frame;
var fps = effectDescription.FPS;
if (updateClonesValues(frame, length, fps))
{
commandList?.Dispose();
UpdateParentPaths();
updateOutputs();
setCommandList();
}
isFirst = false;
return effectDescription.DrawDescription;
}
private void UpdateParentPaths()
{
List<CloneNode> independent = [];
List<CloneNode> parents = [];
List<CloneNode> children = [];
int numOfChildren = 0;
foreach(var cloneNode in CloneNodes)
{
cloneNode.ParentPath = [];
if (cloneNode.Parent == string.Empty)
{
if (cloneNode.TagName == string.Empty || cloneNode.TagName == cloneNode.Parent) independent.Add(cloneNode);
else parents.Add(cloneNode);
}
else
{
children.Add(cloneNode);
numOfChildren++;
}
}
while(numOfChildren > 0)
{
for(int i = 0;i < children.Count;)
{
var matched = from x in parents
where (x.TagName == children[i].Parent) select x;
if (matched.Count() > 0)
{
children[i].ParentPath = matched.First().ParentPath.Add(matched.First());
parents.Add(children[i]);
children.RemoveAt(i);
}
else i++;
}
if (numOfChildren == children.Count) break;
numOfChildren = children.Count;
}
/*
foreach(var cloneNode in CloneNodes)
{
double x = 0, y = 0, scale = 1, opacity = 1, rotate = 0;
var path = cloneNode.ParentPath.Add(cloneNode);
foreach (var node in path)
{
double x2 = node.Dst_X + node.Cnt_X, y2 = node.Dst_Y + node.Cnt_Y;
float rotate2 = (float)((rotate % 360) * Math.PI / 180);
x += (float)(scale * (x2 * Math.Cos(rotate2) - y2 * Math.Sin(rotate2)));
y += (float)(scale * (x2 * Math.Sin(rotate2) + y2 * Math.Cos(rotate2)));
opacity = ((node.OpacityDependent) ? opacity : 1) * node.Opacity / 100;
scale = ((node.ScaleDependent) ? scale : 1) * node.Scale / 100;
rotate = ((node.RotateDependent) ? rotate : 0) + node.Rotate;
}
cloneNode.Shift = new Vector2 ((float)x, (float)y);
cloneNode.Opacity2 = opacity;
cloneNode.Scale2 = scale;
cloneNode.Rotate2 = (rotate % 360) * Math.PI / 180;
}
*/
}
private void setCommandList()
{
commandList = devices.DeviceContext.CreateCommandList();
var dc = devices.DeviceContext;
dc.Target = commandList;
dc.BeginDraw();
dc.Clear(null);
for (int i = 0; i < numOfClones; i++)
{
if (CloneNodes[i].Appear)
{
if (CloneNodes[i].Output is ID2D1Image output)
{
var vec2 = CloneNodes[i].Shift;
switch (CloneNodes[i].BlendMode)
{
case BlendModeCCE.SourceOver:
dc.DrawImage(output, vec2, compositeMode: CompositeMode.SourceOver);
break;
case BlendModeCCE.Plus:
dc.DrawImage(output, vec2, compositeMode: CompositeMode.Plus);
break;
case BlendModeCCE.DestinationOver:
dc.DrawImage(output, vec2, compositeMode: CompositeMode.DestinationOver);
break;
case BlendModeCCE.DestinationOut:
dc.DrawImage(output, vec2, compositeMode: CompositeMode.DestinationOut);
break;
case BlendModeCCE.SourceAtop:
dc.DrawImage(output, vec2, compositeMode: CompositeMode.SourceAtop);
break;
case BlendModeCCE.XOR:
dc.DrawImage(output, vec2, compositeMode: CompositeMode.Xor);
break;
case BlendModeCCE.MaskInverseErt:
dc.DrawImage(output, vec2, compositeMode: CompositeMode.MaskInverseErt);
break;
case BlendModeCCE.Multiply:
dc.BlendImage(output, BlendMode.Multiply, vec2, null, InterpolationMode.MultiSampleLinear);
break;
case BlendModeCCE.Screen:
dc.BlendImage(output, BlendMode.Screen, vec2, null, InterpolationMode.MultiSampleLinear);
break;
case BlendModeCCE.Darken:
dc.BlendImage(output, BlendMode.Darken, vec2, null, InterpolationMode.MultiSampleLinear);
break;
case BlendModeCCE.Lighten:
dc.BlendImage(output, BlendMode.Lighten, vec2, null, InterpolationMode.MultiSampleLinear);
break;
case BlendModeCCE.Dissolve:
dc.BlendImage(output, BlendMode.Dissolve, vec2, null, InterpolationMode.MultiSampleLinear);
break;
case BlendModeCCE.ColorBurn:
dc.BlendImage(output, BlendMode.ColorBurn, vec2, null, InterpolationMode.MultiSampleLinear);
break;
case BlendModeCCE.LinearBurn:
dc.BlendImage(output, BlendMode.LinearBurn, vec2, null, InterpolationMode.MultiSampleLinear);
break;
case BlendModeCCE.DarkerColor:
dc.BlendImage(output, BlendMode.DarkerColor, vec2, null, InterpolationMode.MultiSampleLinear);
break;
case BlendModeCCE.LighterColor:
dc.BlendImage(output, BlendMode.LighterColor, vec2, null, InterpolationMode.MultiSampleLinear);
break;
case BlendModeCCE.ColorDodge:
dc.BlendImage(output, BlendMode.ColorDodge, vec2, null, InterpolationMode.MultiSampleLinear);
break;
case BlendModeCCE.LinearDodge:
dc.BlendImage(output, BlendMode.LinearDodge, vec2, null, InterpolationMode.MultiSampleLinear);
break;
case BlendModeCCE.Overlay:
dc.BlendImage(output, BlendMode.Overlay, vec2, null, InterpolationMode.MultiSampleLinear);
break;
case BlendModeCCE.SoftLight:
dc.BlendImage(output, BlendMode.SoftLight, vec2, null, InterpolationMode.MultiSampleLinear);
break;
case BlendModeCCE.HardLight:
dc.BlendImage(output, BlendMode.HardLight, vec2, null, InterpolationMode.MultiSampleLinear);
break;
case BlendModeCCE.VividLight:
dc.BlendImage(output, BlendMode.VividLight, vec2, null, InterpolationMode.MultiSampleLinear);
break;
case BlendModeCCE.LinearLight:
dc.BlendImage(output, BlendMode.LinearLight, vec2, null, InterpolationMode.MultiSampleLinear);
break;
case BlendModeCCE.PinLight:
dc.BlendImage(output, BlendMode.PinLight, vec2, null, InterpolationMode.MultiSampleLinear);
break;
case BlendModeCCE.HardMix:
dc.BlendImage(output, BlendMode.HardMix, vec2, null, InterpolationMode.MultiSampleLinear);
break;
case BlendModeCCE.Difference:
dc.BlendImage(output, BlendMode.Difference, vec2, null, InterpolationMode.MultiSampleLinear);
break;
case BlendModeCCE.Exclusion:
dc.BlendImage(output, BlendMode.Exclusion, vec2, null, InterpolationMode.MultiSampleLinear);
break;
case BlendModeCCE.Hue:
dc.BlendImage(output, BlendMode.Hue, vec2, null, InterpolationMode.MultiSampleLinear);
break;
case BlendModeCCE.Saturation:
dc.BlendImage(output, BlendMode.Saturation, vec2, null, InterpolationMode.MultiSampleLinear);
break;
case BlendModeCCE.Color:
dc.BlendImage(output, BlendMode.Color, vec2, null, InterpolationMode.MultiSampleLinear);
break;
case BlendModeCCE.Luminosity:
dc.BlendImage(output, BlendMode.Luminosity, vec2, null, InterpolationMode.MultiSampleLinear);
break;
case BlendModeCCE.Subtract:
dc.BlendImage(output, BlendMode.Subtract, vec2, null, InterpolationMode.MultiSampleLinear);
break;
case BlendModeCCE.Division:
dc.BlendImage(output, BlendMode.Division, vec2, null, InterpolationMode.MultiSampleLinear);
break;
}
}
}
}
dc.EndDraw();
commandList.Close();//CommandListはEndDraw()の後に必ずClose()を呼んで閉じる必要がある
transformEffect.SetInput(0, commandList, true);
}
private void updateOutputs()
{
foreach (var cloneNode in CloneNodes)
cloneNode.CommitOutput(input);
}
private bool updateClonesValues(long frame, long length, int fps)
{
bool isOld = false;
if (numOfClones != item.Clones.Count || isFirst)
{
isOld = true;
numOfClones = item.Clones.Count;
RemoveNodes(0);
for (int i = 0; i < numOfClones; i++)
CloneNodes.Add(new CloneNode(devices, item.Clones[i], length, frame, fps));
}
else
{
for (int i = 0; i < numOfClones; i++)
{
if(CloneNodes[i].Update(item.Clones[i], length, frame, fps))
{
isOld = true;
}
}
var defRect = devices.DeviceContext.GetImageLocalBounds(input);
if (!defRect.Equals(inputRect))
isOld = true;
inputRect = defRect;
}
return isOld;
}
public void ClearInput()
{
input = null;
transformEffect.SetInput(0, null, true);
commandList?.Dispose();//前回のUpdateで作成したCommandListを破棄する
RemoveNodes(0);
}
public void SetInput(ID2D1Image input)
{
commandList?.Dispose();
this.input = input;
numOfClones = 0;
UpdateParentPaths();
updateOutputs();
setCommandList();
}
public void RemoveNodes(int count)
{
while (CloneNodes.Count > count)
{
CloneNodes[count].Dispose();
CloneNodes.RemoveAt(count);
}
}
public void Dispose()
{
commandList?.Dispose();//最後のUpdateで作成したCommandListを破棄
transformEffect.SetInput(0, null, true);//EffectのInputは必ずnullに戻す。
transformEffect.Dispose();
RemoveNodes(0);
Output.Dispose();//EffectからgetしたOutputは必ずDisposeする必要がある。Effect側では開放されない。
}
}
}