-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBloom.cs
More file actions
130 lines (105 loc) · 4.42 KB
/
Bloom.cs
File metadata and controls
130 lines (105 loc) · 4.42 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
using System;
using System.Numerics;
using R3D_cs;
using Raylib_cs;
using static Raylib_cs.Raylib;
namespace Examples;
public static class Bloom
{
public static int Main()
{
// Initialize window
InitWindow(800, 450, "[r3d] - Bloom example");
SetTargetFPS(60);
// Initialize R3D
R3D.Init(GetScreenWidth(), GetScreenHeight());
R3D.SetEnvironmentEx((ref env) =>
{
// Setup bloom and tonemapping
env.Tonemap.Mode = Tonemap.Aces;
env.Bloom.Mode = R3D_cs.Bloom.Mix;
env.Bloom.Levels = 1.0f;
// Set background
env.Background.Color = Color.Black;
});
// Create cube mesh and material
var cube = R3D.GenMeshCube(1.0f, 1.0f, 1.0f);
var material = R3D.GetDefaultMaterial();
var hueCube = 0.0f;
material.Emission.Color = ColorFromHSV(hueCube, 1.0f, 1.0f);
material.Emission.Energy = 1.0f;
material.Albedo.Color = Color.Black;
// Setup camera
var camera = new Camera3D
{
Position = new Vector3(0, 3.5f, 5),
Target = Vector3.Zero,
Up = Vector3.UnitY,
FovY = 60
};
// Main loop
while (!WindowShouldClose())
{
float delta = GetFrameTime();
UpdateCamera(ref camera, CameraMode.Orbital);
// Change cube color
if (IsKeyDown(KeyboardKey.C))
{
hueCube = Raymath.Wrap(hueCube + 45.0f * delta, 0, 360);
material.Emission.Color = ColorFromHSV(hueCube, 1.0f, 1.0f);
}
// Adjust bloom parameters
float intensity = R3D.GetEnvironmentEx().Bloom.Intensity;
int intensityDir = IsKeyDownDelay(KeyboardKey.Right) - IsKeyDownDelay(KeyboardKey.Left);
AdjustBloomParam(ref intensity, intensityDir, 0.01f, 0.0f, float.PositiveInfinity);
R3D.SetEnvironmentEx((ref env) => env.Bloom.Intensity = intensity);
float radius = R3D.GetEnvironmentEx().Bloom.FilterRadius;
int radiusDir = IsKeyDownDelay(KeyboardKey.Up) - IsKeyDownDelay(KeyboardKey.Down);
AdjustBloomParam(ref radius, radiusDir, 0.1f, 0.0f, float.PositiveInfinity);
R3D.SetEnvironmentEx((ref env) => env.Bloom.FilterRadius = radius);
int levelDir = IsMouseButtonDown(MouseButton.Right) - IsMouseButtonDown(MouseButton.Left);
float levels = R3D.GetEnvironmentEx().Bloom.Levels;
AdjustBloomParam(ref levels, levelDir, 0.01f, 0.0f, 1.0f);
R3D.SetEnvironmentEx((ref env) => env.Bloom.Levels = levels);
// Draw scene
if (IsKeyPressed(KeyboardKey.Space))
R3D.SetEnvironmentEx((ref env) => env.Bloom.Mode = (R3D_cs.Bloom)(((int)R3D.GetEnvironmentEx().Bloom.Mode + 1) % ((int)R3D_cs.Bloom.Screen + 1)));
BeginDrawing();
ClearBackground(Color.RayWhite);
R3D.Begin(camera);
R3D.DrawMesh(cube, material, Vector3.Zero, 1.0f);
R3D.End();
var env = R3D.GetEnvironmentEx();
// Draw bloom info
DrawTextRight($"Mode: {GetBloomModeName()}", 10, 20, Color.Lime);
DrawTextRight($"Intensity: {env.Bloom.Intensity:.00}", 40, 20, Color.Lime);
DrawTextRight($"Filter Radius: {env.Bloom.FilterRadius:.00}", 70, 20, Color.Lime);
DrawTextRight($"Levels: {env.Bloom.Levels:.00}", 100, 20, Color.Lime);
EndDrawing();
}
R3D.UnloadMesh(cube);
R3D.Close();
CloseWindow();
return 0;
}
private static CBool IsKeyDownDelay(KeyboardKey key)
{
return IsKeyPressedRepeat(key) || IsKeyPressed(key);
}
private static string GetBloomModeName()
{
string[] modes = ["Disabled", "Mix", "Additive", "Screen"];
var mode = (int)R3D.GetEnvironmentEx().Bloom.Mode;
return mode is >= 0 and <= (int)R3D_cs.Bloom.Screen ? modes[mode] : "Unknown";
}
private static void DrawTextRight(string text, int y, int fontSize, Color color)
{
int width = MeasureText(text, fontSize);
DrawText(text, GetScreenWidth() - width - 10, y, fontSize, color);
}
private static void AdjustBloomParam(ref float param, int direction, float step, float min, float max)
{
if (direction != 0)
param = Math.Clamp(param + direction * step, min, max);
}
}