Skip to content

Commit 822dddf

Browse files
authored
Merge pull request #4 from JohnLudlow/shaders-customuniform
Add custom uniform shader example
2 parents 243a6b0 + f0897fe commit 822dddf

4 files changed

Lines changed: 182 additions & 3 deletions

File tree

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
/*******************************************************************************************
2+
*
3+
* raylib [shaders] example - Apply a postprocessing shader and connect a custom uniform variable
4+
*
5+
* NOTE: This example requires raylib OpenGL 3.3 or ES2 versions for shaders support,
6+
* OpenGL 1.1 does not support shaders, recompile raylib to OpenGL 3.3 version.
7+
*
8+
* NOTE: Shaders used in this example are #version 330 (OpenGL 3.3), to test this example
9+
* on OpenGL ES 2.0 platforms (Android, Raspberry Pi, HTML5), use #version 100 shaders
10+
* raylib comes with shaders ready for both versions, check raylib/shaders install folder
11+
*
12+
* This example has been created using raylib 1.3 (www.raylib.com)
13+
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
14+
*
15+
* Copyright (c) 2015 Ramon Santamaria (@raysan5)
16+
*
17+
********************************************************************************************/
18+
19+
using System.Diagnostics;
20+
using System.Numerics;
21+
using Raylib_cs;
22+
23+
namespace RaylibCsExamples.Community.Shaders.CustomUniform;
24+
25+
public class CustomUniform
26+
{
27+
public static int Main()
28+
{
29+
// Initialization
30+
//--------------------------------------------------------------------------------------
31+
const int screenWidth = 1600;
32+
const int screenHeight = 900;
33+
34+
// Enable Multi Sampling Anti Aliasing 4x (if available)
35+
Raylib.SetConfigFlags(ConfigFlags.Msaa4xHint);
36+
Raylib.InitWindow(screenWidth, screenHeight, "raylib [shaders] example - custom uniform variable");
37+
{
38+
// Define the camera to look into our 3d world
39+
Camera3D camera = new()
40+
{
41+
Position = new Vector3(8.0f, 8.0f, 8.0f),
42+
Target = new Vector3(0.0f, 1.5f, 0.0f),
43+
Up = new Vector3(0.0f, 1.0f, 0.0f),
44+
FovY = 45.0f,
45+
Projection = CameraProjection.Perspective
46+
};
47+
48+
var model = Raylib.LoadModel("resources/models/obj/barracks.obj");
49+
var texture = Raylib.LoadTexture("resources/models/obj/barracks_diffuse.png");
50+
51+
// Set model diffuse texture
52+
Raylib.SetMaterialTexture(ref model, 0, MaterialMapIndex.Albedo, ref texture);
53+
54+
Vector3 position = new(0.0f, 0.0f, 0.0f);
55+
56+
// Load postpro shader
57+
var shader = Raylib.LoadShader(
58+
"resources/shaders/glsl330/base.vs",
59+
"resources/shaders/glsl330/swirl.fs"
60+
);
61+
62+
// Get variable (uniform) location on the shader to connect with the program
63+
// NOTE: If uniform variable could not be found in the shader, function returns -1
64+
var swirlCenterLoc = Raylib.GetShaderLocation(shader, "center");
65+
var screenSizeLoc = Raylib.GetShaderLocation(shader, "texSize");
66+
67+
var swirlCenter = new Vector2((float)screenWidth / 2, (float)screenHeight / 2);
68+
69+
// Create a RenderTexture2D to be used for render to texture
70+
var target = Raylib.LoadRenderTexture(screenWidth, screenHeight);
71+
72+
Raylib.SetTargetFPS(60);
73+
//--------------------------------------------------------------------------------------
74+
75+
// Main game loop
76+
while (!Raylib.WindowShouldClose())
77+
{
78+
// Update
79+
//----------------------------------------------------------------------------------
80+
var mousePosition = Raylib.GetMousePosition();
81+
82+
swirlCenter.X = mousePosition.X;
83+
swirlCenter.Y = screenHeight - mousePosition.Y;
84+
85+
// Send new value to the shader to be used on drawing
86+
Raylib.SetShaderValue(shader, swirlCenterLoc, swirlCenter, ShaderUniformDataType.Vec2);
87+
Raylib.SetShaderValue(shader, screenSizeLoc, new Vector2(screenWidth, screenHeight), ShaderUniformDataType.Vec2);
88+
89+
Raylib.UpdateCamera(ref camera, CameraMode.Orbital);
90+
//----------------------------------------------------------------------------------
91+
92+
// Draw
93+
//----------------------------------------------------------------------------------
94+
Raylib.BeginDrawing();
95+
{
96+
Raylib.ClearBackground(Color.RayWhite);
97+
98+
// Enable drawing to texture
99+
Raylib.BeginTextureMode(target);
100+
{
101+
Raylib.ClearBackground(Color.RayWhite);
102+
103+
Raylib.BeginMode3D(camera);
104+
{
105+
Raylib.DrawModel(model, position, 0.5f, Color.White);
106+
Raylib.DrawGrid(10, 1.0f);
107+
}
108+
Raylib.EndMode3D();
109+
110+
Raylib.DrawText("TEXT DRAWN IN RENDER TEXTURE", 200, 10, 30, Color.Red);
111+
112+
}
113+
// End drawing to texture (now we have a texture available for next passes)
114+
Raylib.EndTextureMode();
115+
116+
Raylib.BeginShaderMode(shader);
117+
{
118+
// NOTE: Render texture must be y-flipped due to default OpenGL coordinates (left-bottom)
119+
Raylib.DrawTextureRec(
120+
target.Texture,
121+
new Rectangle(0, 0, target.Texture.Width, -target.Texture.Height),
122+
new Vector2(0, 0),
123+
Color.White
124+
);
125+
}
126+
Raylib.EndShaderMode();
127+
128+
Raylib.DrawText(
129+
"(c) Barracks 3D model by Alberto Cano",
130+
screenWidth - 220,
131+
screenHeight - 20,
132+
10,
133+
Color.Gray
134+
);
135+
136+
Raylib.DrawFPS(10, 10);
137+
}
138+
Raylib.EndDrawing();
139+
//----------------------------------------------------------------------------------
140+
}
141+
142+
// De-Initialization
143+
//--------------------------------------------------------------------------------------
144+
Raylib.UnloadShader(shader);
145+
Raylib.UnloadTexture(texture);
146+
Raylib.UnloadModel(model);
147+
Raylib.UnloadRenderTexture(target);
148+
}
149+
Raylib.CloseWindow();
150+
//--------------------------------------------------------------------------------------
151+
152+
return 0;
153+
}
154+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net9.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
<ItemGroup>
10+
<Content Include="../../resources/models/obj/barracks.obj" Link="resources/models/obj/barracks.obj">
11+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
12+
</Content>
13+
14+
<Content Include="../../resources/models/obj/barracks_diffuse.png" Link="resources/models/obj/barracks_diffuse.png">
15+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
16+
</Content>
17+
18+
<Content Include="../../resources/shaders/glsl330/base.vs" Link="resources/shaders/glsl330/base.vs">
19+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
20+
</Content>
21+
22+
<Content Include="../../resources/shaders/glsl330/swirl.fs" Link="resources/shaders/glsl330/swirl.fs">
23+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
24+
</Content>
25+
</ItemGroup>
26+
</Project>

Community/resources/shaders/glsl330/swirl.fs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ out vec4 finalColor;
1414
// NOTE: Add here your custom variables
1515

1616
// NOTE: Render size values should be passed from code
17-
const float renderWidth = 800;
18-
const float renderHeight = 450;
17+
uniform vec2 texSize = vec2(1600, 900);
1918

2019
float radius = 250.0;
2120
float angle = 0.8;
@@ -24,7 +23,6 @@ uniform vec2 center = vec2(200.0, 200.0);
2423

2524
void main()
2625
{
27-
vec2 texSize = vec2(renderWidth, renderHeight);
2826
vec2 tc = fragTexCoord*texSize;
2927
tc -= center;
3028

RaylibCsExamples.slnx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<Folder Name="/Community/Shaders/">
2222
<Project Path="Community/Shaders/RaylibCsExamples.Community.Shaders.BasicLighting/RaylibCsExamples.Community.Shaders.BasicLighting.csproj" />
2323
<Project Path="Community/Shaders/RaylibCsExamples.Community.Shaders.BasicPbr/RaylibCsExamples.Community.Shaders.BasicPbr.csproj" />
24+
<Project Path="Community/Shaders/RaylibCsExamples.Community.Shaders.CustomUniform/RaylibCsExamples.Community.Shaders.CustomUniform.csproj" />
2425
</Folder>
2526
<Folder Name="/Local/" />
2627
<Folder Name="/Local/Objects/">

0 commit comments

Comments
 (0)