feat: backporting existing raylib-cs examples and new official raylib examples to WASM, adopting raylib's original code style
This commit is contained in:
parent
768fa93c41
commit
732b7aa346
234 changed files with 38999 additions and 10578 deletions
|
|
@ -2,26 +2,21 @@
|
|||
*
|
||||
* raylib [shaders] example - basic lighting
|
||||
*
|
||||
* Example complexity rating: [★★★★] 4/4
|
||||
*
|
||||
* NOTE: This example requires raylib OpenGL 3.3 or ES2 versions for shaders support,
|
||||
* OpenGL 1.1 does not support shaders, recompile raylib to OpenGL 3.3 version.
|
||||
* OpenGL 1.1 does not support shaders, recompile raylib to OpenGL 3.3 version
|
||||
*
|
||||
* NOTE: Shaders used in this example are #version 330 (OpenGL 3.3).
|
||||
* NOTE: Shaders used in this example are #version 330 (OpenGL 3.3)
|
||||
*
|
||||
* This example has been created using raylib 2.5 (www.raylib.com)
|
||||
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
|
||||
* Example originally created with raylib 3.0, last time updated with raylib 4.2
|
||||
*
|
||||
* Example contributed by Chris Camacho (@codifies) and reviewed by Ramon Santamaria (@raysan5)
|
||||
* Example contributed by Chris Camacho (@chriscamacho) and reviewed by Ramon Santamaria (@raysan5)
|
||||
*
|
||||
* Chris Camacho (@codifies - http://bedroomcoders.co.uk/) notes:
|
||||
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
|
||||
* BSD-like license that allows static linking with closed source software
|
||||
*
|
||||
* This is based on the PBR lighting example, but greatly simplified to aid learning...
|
||||
* actually there is very little of the PBR example left!
|
||||
* When I first looked at the bewildering complexity of the PBR example I feared
|
||||
* I would never understand how I could do simple lighting with raylib however its
|
||||
* a testement to the authors of raylib (including rlights.h) that the example
|
||||
* came together fairly quickly.
|
||||
*
|
||||
* Copyright (c) 2019 Chris Camacho (@codifies) and Ramon Santamaria (@raysan5)
|
||||
* Copyright (c) 2019-2025 Chris Camacho (@chriscamacho) and Ramon Santamaria (@raysan5)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
|
|
@ -31,52 +26,55 @@ using Examples.Shared;
|
|||
|
||||
namespace Examples.Shaders;
|
||||
|
||||
public class BasicLighting
|
||||
public class BasicLighting : IExample
|
||||
{
|
||||
const int GLSL_VERSION = 330;
|
||||
private const int screenWidth = 800;
|
||||
private const int screenHeight = 450;
|
||||
|
||||
public unsafe static int Main()
|
||||
#if BROWSER
|
||||
const int GlslVersion = 100; // WebGL1 needs GLSL ES 100
|
||||
#else
|
||||
private const int GlslVersion = 330;
|
||||
#endif
|
||||
|
||||
public string Name => "Shaders / Basic Lighting";
|
||||
|
||||
public string Title => "raylib [shaders] example - basic lighting";
|
||||
|
||||
public ConfigFlags ConfigFlags => ConfigFlags.Msaa4xHint;
|
||||
|
||||
private Camera3D camera;
|
||||
private Shader shader;
|
||||
private Light[] lights;
|
||||
|
||||
public unsafe void Init()
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
const int screenWidth = 800;
|
||||
const int screenHeight = 450;
|
||||
|
||||
// Enable Multi Sampling Anti Aliasing 4x (if available)
|
||||
SetConfigFlags(ConfigFlags.Msaa4xHint);
|
||||
InitWindow(screenWidth, screenHeight, "raylib [shaders] example - basic lighting");
|
||||
|
||||
// Define the camera to look into our 3d world
|
||||
Camera3D camera = new();
|
||||
camera.Position = new Vector3(2.0f, 4.0f, 6.0f);
|
||||
camera.Target = new Vector3(0.0f, 0.5f, 0.0f);
|
||||
camera.Up = new Vector3(0.0f, 1.0f, 0.0f);
|
||||
camera.FovY = 45.0f;
|
||||
camera.Projection = CameraProjection.Perspective;
|
||||
camera = new();
|
||||
camera.Position = new Vector3(2.0f, 4.0f, 6.0f); // Camera position
|
||||
camera.Target = new Vector3(0.0f, 0.5f, 0.0f); // Camera looking at point
|
||||
camera.Up = new Vector3(0.0f, 1.0f, 0.0f); // Camera up vector (rotation towards target)
|
||||
camera.FovY = 45.0f; // Camera field-of-view Y
|
||||
camera.Projection = CameraProjection.Perspective; // Camera projection type
|
||||
|
||||
// Load plane model from a generated mesh
|
||||
Model model = LoadModelFromMesh(GenMeshPlane(10.0f, 10.0f, 3, 3));
|
||||
Model cube = LoadModelFromMesh(GenMeshCube(2.0f, 4.0f, 2.0f));
|
||||
|
||||
Shader shader = LoadShader(
|
||||
"resources/shaders/glsl330/lighting.vs",
|
||||
"resources/shaders/glsl330/lighting.fs"
|
||||
// Load basic lighting shader
|
||||
shader = LoadShader(
|
||||
$"resources/shaders/glsl{GlslVersion}/lighting.vs",
|
||||
$"resources/shaders/glsl{GlslVersion}/lighting.fs"
|
||||
);
|
||||
|
||||
// Get some required shader loactions
|
||||
// Get some required shader locations
|
||||
shader.Locs[(int)ShaderLocationIndex.VectorView] = GetShaderLocation(shader, "viewPos");
|
||||
// NOTE: "matModel" location name is automatically assigned on shader loading,
|
||||
// no need to get the location again if using that uniform name
|
||||
//shader.Locs[(int)ShaderLocationIndex.MatrixModel] = GetShaderLocation(shader, "matModel");
|
||||
|
||||
// ambient light level
|
||||
int ambientLoc = GetShaderLocation(shader, "ambient");
|
||||
float[] ambient = new[] { 0.1f, 0.1f, 0.1f, 1.0f };
|
||||
// Ambient light level (some basic lighting)
|
||||
var ambientLoc = GetShaderLocation(shader, "ambient");
|
||||
var ambient = new[] { 0.1f, 0.1f, 0.1f, 1.0f };
|
||||
Raylib.SetShaderValue(shader, ambientLoc, ambient, ShaderUniformDataType.Vec4);
|
||||
|
||||
// Assign out lighting shader to model
|
||||
model.Materials[0].Shader = shader;
|
||||
cube.Materials[0].Shader = shader;
|
||||
|
||||
// Using 4 point lights: Color.gold, Color.red, Color.green and Color.blue
|
||||
Light[] lights = new Light[4];
|
||||
// Create lights
|
||||
lights = new Light[4];
|
||||
lights[0] = Rlights.CreateLight(
|
||||
0,
|
||||
LightType.Point,
|
||||
|
|
@ -109,114 +107,116 @@ public class BasicLighting
|
|||
Color.Blue,
|
||||
shader
|
||||
);
|
||||
}
|
||||
|
||||
SetTargetFPS(60);
|
||||
public unsafe void Update()
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
UpdateCamera(ref camera, CameraMode.Orbital);
|
||||
|
||||
// Update the shader with the camera view vector (points towards { 0.0f, 0.0f, 0.0f })
|
||||
Raylib.SetShaderValue(
|
||||
shader,
|
||||
shader.Locs[(int)ShaderLocationIndex.VectorView],
|
||||
camera.Position,
|
||||
ShaderUniformDataType.Vec3
|
||||
);
|
||||
|
||||
// Check key inputs to enable/disable lights
|
||||
if (IsKeyPressed(KeyboardKey.Y))
|
||||
{
|
||||
lights[0].Enabled = !lights[0].Enabled;
|
||||
}
|
||||
if (IsKeyPressed(KeyboardKey.R))
|
||||
{
|
||||
lights[1].Enabled = !lights[1].Enabled;
|
||||
}
|
||||
if (IsKeyPressed(KeyboardKey.G))
|
||||
{
|
||||
lights[2].Enabled = !lights[2].Enabled;
|
||||
}
|
||||
if (IsKeyPressed(KeyboardKey.B))
|
||||
{
|
||||
lights[3].Enabled = !lights[3].Enabled;
|
||||
}
|
||||
|
||||
// Update light values (actually, only enable/disable them)
|
||||
for (var i = 0; i < 4; i++)
|
||||
{
|
||||
Rlights.UpdateLightValues(shader, lights[i]);
|
||||
}
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
|
||||
ClearBackground(Color.RayWhite);
|
||||
|
||||
BeginMode3D(camera);
|
||||
|
||||
BeginShaderMode(shader);
|
||||
|
||||
DrawPlane(Vector3.Zero, new Vector2(10.0f, 10.0f), Color.White);
|
||||
DrawCube(Vector3.Zero, 2.0f, 4.0f, 2.0f, Color.White);
|
||||
|
||||
EndShaderMode();
|
||||
|
||||
// Draw spheres to show where the lights are
|
||||
for (var i = 0; i < 4; i++)
|
||||
{
|
||||
if (lights[i].Enabled)
|
||||
{
|
||||
DrawSphereEx(lights[i].Position, 0.2f, 8, 8, lights[i].Color);
|
||||
}
|
||||
else
|
||||
{
|
||||
DrawSphereWires(lights[i].Position, 0.2f, 8, 8, ColorAlpha(lights[i].Color, 0.3f));
|
||||
}
|
||||
}
|
||||
|
||||
DrawGrid(10, 1.0f);
|
||||
|
||||
EndMode3D();
|
||||
|
||||
DrawFPS(10, 10);
|
||||
|
||||
DrawText("Use keys [Y][R][G][B] to toggle lights", 10, 40, 20, Color.DarkGray);
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
public void Unload()
|
||||
{
|
||||
UnloadShader(shader); // Unload shader
|
||||
}
|
||||
|
||||
public static int Main()
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
SetConfigFlags(ConfigFlags.Msaa4xHint); // Enable Multi Sampling Anti Aliasing 4x (if available)
|
||||
InitWindow(screenWidth, screenHeight, "raylib [shaders] example - basic lighting");
|
||||
|
||||
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
var game = new BasicLighting();
|
||||
game.Init();
|
||||
|
||||
// Main game loop
|
||||
while (!WindowShouldClose())
|
||||
while (!WindowShouldClose()) // Detect window close button or ESC key
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
UpdateCamera(ref camera, CameraMode.Orbital);
|
||||
|
||||
if (IsKeyPressed(KeyboardKey.Y))
|
||||
{
|
||||
lights[0].Enabled = !lights[0].Enabled;
|
||||
}
|
||||
if (IsKeyPressed(KeyboardKey.R))
|
||||
{
|
||||
lights[1].Enabled = !lights[1].Enabled;
|
||||
}
|
||||
if (IsKeyPressed(KeyboardKey.G))
|
||||
{
|
||||
lights[2].Enabled = !lights[2].Enabled;
|
||||
}
|
||||
if (IsKeyPressed(KeyboardKey.B))
|
||||
{
|
||||
lights[3].Enabled = !lights[3].Enabled;
|
||||
}
|
||||
|
||||
// Update light values (actually, only enable/disable them)
|
||||
Rlights.UpdateLightValues(shader, lights[0]);
|
||||
Rlights.UpdateLightValues(shader, lights[1]);
|
||||
Rlights.UpdateLightValues(shader, lights[2]);
|
||||
Rlights.UpdateLightValues(shader, lights[3]);
|
||||
|
||||
// Update the light shader with the camera view position
|
||||
Raylib.SetShaderValue(
|
||||
shader,
|
||||
shader.Locs[(int)ShaderLocationIndex.VectorView],
|
||||
camera.Position,
|
||||
ShaderUniformDataType.Vec3
|
||||
);
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
ClearBackground(Color.RayWhite);
|
||||
|
||||
BeginMode3D(camera);
|
||||
|
||||
DrawModel(model, Vector3.Zero, 1.0f, Color.White);
|
||||
DrawModel(cube, Vector3.Zero, 1.0f, Color.White);
|
||||
|
||||
// Draw markers to show where the lights are
|
||||
if (lights[0].Enabled)
|
||||
{
|
||||
DrawSphereEx(lights[0].Position, 0.2f, 8, 8, Color.Yellow);
|
||||
}
|
||||
else
|
||||
{
|
||||
DrawSphereWires(lights[0].Position, 0.2f, 8, 8, ColorAlpha(Color.Yellow, 0.3f));
|
||||
}
|
||||
|
||||
if (lights[1].Enabled)
|
||||
{
|
||||
DrawSphereEx(lights[1].Position, 0.2f, 8, 8, Color.Red);
|
||||
}
|
||||
else
|
||||
{
|
||||
DrawSphereWires(lights[1].Position, 0.2f, 8, 8, ColorAlpha(Color.Red, 0.3f));
|
||||
}
|
||||
|
||||
if (lights[2].Enabled)
|
||||
{
|
||||
DrawSphereEx(lights[2].Position, 0.2f, 8, 8, Color.Green);
|
||||
}
|
||||
else
|
||||
{
|
||||
DrawSphereWires(lights[2].Position, 0.2f, 8, 8, ColorAlpha(Color.Green, 0.3f));
|
||||
}
|
||||
|
||||
if (lights[3].Enabled)
|
||||
{
|
||||
DrawSphereEx(lights[3].Position, 0.2f, 8, 8, Color.Blue);
|
||||
}
|
||||
else
|
||||
{
|
||||
DrawSphereWires(lights[3].Position, 0.2f, 8, 8, ColorAlpha(Color.Blue, 0.3f));
|
||||
}
|
||||
|
||||
DrawGrid(10, 1.0f);
|
||||
|
||||
EndMode3D();
|
||||
|
||||
DrawFPS(10, 10);
|
||||
DrawText("Use keys [Y][R][G][B] to toggle lights", 10, 40, 20, Color.DarkGray);
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
game.Update();
|
||||
}
|
||||
|
||||
game.Unload();
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
UnloadModel(model);
|
||||
UnloadModel(cube);
|
||||
UnloadShader(shader);
|
||||
|
||||
CloseWindow();
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
|
|
|
|||
Loading…
Reference in a new issue