|
| 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 | +} |
0 commit comments