-
Notifications
You must be signed in to change notification settings - Fork 87
/
Shader.cs
404 lines (348 loc) · 13.7 KB
/
Shader.cs
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
using System;
using SFML.Graphics;
using SFML.System;
using SFML.Window;
namespace shader
{
/// <summary>Base class for effects</summary>
abstract class Effect : Drawable
{
protected Effect(string name)
{
myName = name;
}
public string Name => myName;
public void Update(float time, float x, float y)
{
if (Shader.IsAvailable)
{
OnUpdate(time, x, y);
}
}
public void Draw(RenderTarget target, RenderStates states)
{
if (Shader.IsAvailable)
{
OnDraw(target, states);
}
else
{
var error = new Text("Shader not\nsupported", GetFont())
{
Position = new Vector2f(320, 200),
CharacterSize = 36
};
target.Draw(error, states);
}
}
public static void SetFont(Font font)
{
ourFont = font;
}
protected abstract void OnUpdate(float time, float x, float y);
protected abstract void OnDraw(RenderTarget target, RenderStates states);
protected Font GetFont()
{
return ourFont;
}
private readonly string myName;
private static Font ourFont;
}
/// <summary>"Pixelate" fragment shader</summary>
class Pixelate : Effect
{
public Pixelate() : base("pixelate")
{
// Load the texture and initialize the sprite
myTexture = new Texture("resources/background.jpg");
mySprite = new Sprite(myTexture);
// Load the shader
myShader = new Shader(null, null, "resources/pixelate.frag");
myShader.SetUniform("texture", Shader.CurrentTexture);
}
protected override void OnUpdate(float time, float x, float y)
{
myShader.SetUniform("pixel_threshold", ( x + y ) / 30);
}
protected override void OnDraw(RenderTarget target, RenderStates states)
{
states = new RenderStates(states)
{
Shader = myShader
};
target.Draw(mySprite, states);
}
private readonly Texture myTexture;
private readonly Sprite mySprite;
private Shader myShader;
}
/// <summary>"Wave" vertex shader + "blur" fragment shader</summary>
class WaveBlur : Effect
{
public WaveBlur() : base("wave + blur")
{
// Create the text
myText = new Text
{
DisplayedString = "Praesent suscipit augue in velit pulvinar hendrerit varius purus aliquam.\n" +
"Mauris mi odio, bibendum quis fringilla a, laoreet vel orci. Proin vitae vulputate tortor.\n" +
"Praesent cursus ultrices justo, ut feugiat ante vehicula quis.\n" +
"Donec fringilla scelerisque mauris et viverra.\n" +
"Maecenas adipiscing ornare scelerisque. Nullam at libero elit.\n" +
"Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.\n" +
"Nullam leo urna, tincidunt id semper eget, ultricies sed mi.\n" +
"Morbi mauris massa, commodo id dignissim vel, lobortis et elit.\n" +
"Fusce vel libero sed neque scelerisque venenatis.\n" +
"Integer mattis tincidunt quam vitae iaculis.\n" +
"Vivamus fringilla sem non velit venenatis fermentum.\n" +
"Vivamus varius tincidunt nisi id vehicula.\n" +
"Integer ullamcorper, enim vitae euismod rutrum, massa nisl semper ipsum,\n" +
"vestibulum sodales sem ante in massa.\n" +
"Vestibulum in augue non felis convallis viverra.\n" +
"Mauris ultricies dolor sed massa convallis sed aliquet augue fringilla.\n" +
"Duis erat eros, porta in accumsan in, blandit quis sem.\n" +
"In hac habitasse platea dictumst. Etiam fringilla est id odio dapibus sit amet semper dui laoreet.\n",
Font = GetFont(),
CharacterSize = 22,
Position = new Vector2f(30, 20)
};
// Load the shader
myShader = new Shader("resources/wave.vert", null, "resources/blur.frag");
}
protected override void OnUpdate(float time, float x, float y)
{
myShader.SetUniform("wave_phase", time);
myShader.SetUniform("wave_amplitude", new Vector2f(x * 40, y * 40));
myShader.SetUniform("blur_radius", ( x + y ) * 0.008F);
}
protected override void OnDraw(RenderTarget target, RenderStates states)
{
states = new RenderStates(states)
{
Shader = myShader
};
target.Draw(myText, states);
}
private readonly Text myText;
private Shader myShader;
}
/// <summary>"Storm" vertex shader + "blink" fragment shader</summary>
class StormBlink : Effect
{
public StormBlink() : base("storm + blink")
{
Random random = new Random();
// Create the points
myPoints = new VertexArray(PrimitiveType.Points);
for (int i = 0; i < 40000; ++i)
{
float x = random.Next(0, 800);
float y = random.Next(0, 600);
byte r = (byte)random.Next(0, 255);
byte g = (byte)random.Next(0, 255);
byte b = (byte)random.Next(0, 255);
myPoints.Append(new Vertex(new Vector2f(x, y), new Color(r, g, b)));
}
// Load the shader
myShader = new Shader("resources/storm.vert", null, "resources/blink.frag");
}
protected override void OnUpdate(float time, float x, float y)
{
float radius = 200 + (float)Math.Cos(time) * 150;
myShader.SetUniform("storm_position", new Vector2f(x * 800, y * 600));
myShader.SetUniform("storm_inner_radius", radius / 3);
myShader.SetUniform("storm_total_radius", radius);
myShader.SetUniform("blink_alpha", 0.5F + (float)Math.Cos(time * 3) * 0.25F);
}
protected override void OnDraw(RenderTarget target, RenderStates states)
{
states = new RenderStates(states);
states.Shader = myShader;
target.Draw(myPoints, states);
}
private VertexArray myPoints;
private Shader myShader;
}
/// <summary>"Edge" post-effect fragment shader</summary>
class Edge : Effect
{
public Edge() : base("edge post-effect")
{
// Create the off-screen surface
mySurface = new RenderTexture(800, 600)
{
Smooth = true
};
// Load the textures
myBackgroundTexture = new Texture("resources/sfml.png")
{
Smooth = true
};
myEntityTexture = new Texture("resources/devices.png")
{
Smooth = true
};
// Initialize the background sprite
myBackgroundSprite = new Sprite(myBackgroundTexture);
myBackgroundSprite.Position = new Vector2f(135, 100);
// Load the moving entities
myEntities = new Sprite[6];
for (int i = 0; i < myEntities.Length; ++i)
{
myEntities[i] = new Sprite(myEntityTexture, new IntRect(96 * i, 0, 96, 96));
}
// Load the shader
myShader = new Shader(null, null, "resources/edge.frag");
myShader.SetUniform("texture", Shader.CurrentTexture);
}
protected override void OnUpdate(float time, float x, float y)
{
myShader.SetUniform("edge_threshold", 1 - ( x + y ) / 2);
// Update the position of the moving entities
for (var i = 0; i < myEntities.Length; ++i)
{
float posX = (float)Math.Cos(0.25F * ( time * i + ( myEntities.Length - i ) )) * 300 + 350;
float posY = (float)Math.Sin(0.25F * ( time * ( myEntities.Length - i ) + i )) * 200 + 250;
myEntities[i].Position = new Vector2f(posX, posY);
}
// Render the updated scene to the off-screen surface
mySurface.Clear(Color.White);
mySurface.Draw(myBackgroundSprite);
foreach (Sprite entity in myEntities)
{
mySurface.Draw(entity);
}
mySurface.Display();
}
protected override void OnDraw(RenderTarget target, RenderStates states)
{
states = new RenderStates(states)
{
Shader = myShader
};
target.Draw(new Sprite(mySurface.Texture), states);
}
private RenderTexture mySurface;
private readonly Texture myBackgroundTexture;
private readonly Texture myEntityTexture;
private Sprite myBackgroundSprite;
Sprite[] myEntities;
private Shader myShader;
}
static class Program
{
private static Effect[] effects;
private static int current;
private static Text description;
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
// Create the main window
var window = new RenderWindow(new VideoMode(800, 600), "SFML.Net Shader");
window.SetVerticalSyncEnabled(true);
// Setup event handlers
window.Closed += OnClosed;
window.KeyPressed += OnKeyPressed;
// Load the application font and pass it to the Effect class
var font = new Font("resources/sansation.ttf");
Effect.SetFont(font);
// Create the effects
effects = new Effect[]
{
new Pixelate(),
new WaveBlur(),
new StormBlink(),
new Edge()
};
current = 0;
// Create the messages background
var textBackgroundTexture = new Texture("resources/text-background.png");
var textBackground = new Sprite(textBackgroundTexture)
{
Position = new Vector2f(0, 520),
Color = new Color(255, 255, 255, 200)
};
// Create the description text
description = new Text("Current effect: " + effects[current].Name, font, 20)
{
Position = new Vector2f(10, 530),
FillColor = new Color(80, 80, 80)
};
// Create the instructions text
var instructions = new Text("Press left and right arrows to change the current shader", font, 20)
{
Position = new Vector2f(280, 555),
FillColor = new Color(80, 80, 80)
};
// Start the game loop
var clock = new Clock();
while (window.IsOpen)
{
// Process events
window.DispatchEvents();
// Update the current example
var x = (float)Mouse.GetPosition(window).X / window.Size.X;
var y = (float)Mouse.GetPosition(window).Y / window.Size.Y;
effects[current].Update(clock.ElapsedTime.AsSeconds(), x, y);
// Clear the window
window.Clear(new Color(255, 128, 0));
// Draw the current example
window.Draw(effects[current]);
// Draw the text
window.Draw(textBackground);
window.Draw(instructions);
window.Draw(description);
// Finally, display the rendered frame on screen
window.Display();
}
}
/// <summary>
/// Function called when the window is closed
/// </summary>
static void OnClosed(object sender, EventArgs e)
{
var window = (RenderWindow)sender;
window.Close();
}
/// <summary>
/// Function called when a key is pressed
/// </summary>
static void OnKeyPressed(object sender, KeyEventArgs e)
{
var window = (RenderWindow)sender;
// Escape key : exit
if (e.Code == Keyboard.Key.Escape)
{
window.Close();
}
// Left arrow key: previous shader
if (e.Code == Keyboard.Key.Left)
{
if (current == 0)
{
current = effects.Length - 1;
}
else
{
current--;
}
description.DisplayedString = $"Current effect: {effects[current].Name}";
}
// Right arrow key: next shader
if (e.Code == Keyboard.Key.Right)
{
if (current == effects.Length - 1)
{
current = 0;
}
else
{
current++;
}
description.DisplayedString = $"Current effect: {effects[current].Name}";
}
}
}
}