Watch
2
0
Fork
You've already forked raylib-cs
0

feat: backporting existing raylib-cs examples and new official raylib examples to WASM, adopting raylib's original code style

This commit is contained in:
tiger tiger tiger 2026-07-17 08:22:54 +02:00
commit 732b7aa346
234 changed files with 38999 additions and 10578 deletions

View file

@ -1,13 +1,15 @@
/*******************************************************************************************
*
* raylib [models] example - Draw textured cube
* raylib [models] example - textured cube
*
* Example complexity rating: [] 2/4
*
* Example originally created with raylib 4.5, last time updated with raylib 4.5
*
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
* BSD-like license that allows static linking with closed source software
*
* Copyright (c) 2022-2023 Ramon Santamaria (@raysan5)
* Copyright (c) 2022-2025 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
@ -16,19 +18,22 @@ using static Raylib_cs.Raylib;
namespace Examples.Models;
public class ModelCubeTexture
public partial class ModelCubeTexture : IExample
{
public static int Main()
private const int screenWidth = 800;
private const int screenHeight = 450;
public string Name => "Models / Model Cube Texture";
public string Title => "raylib [models] example - textured cube";
private Camera3D camera;
private Texture2D texture;
public void Init()
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [models] example - draw cube texture");
// Define the camera to look into our 3d world
Camera3D camera;
camera = new();
camera.Position = new Vector3(0.0f, 10.0f, 10.0f);
camera.Target = new Vector3(0.0f, 0.0f, 0.0f);
camera.Up = new Vector3(0.0f, 1.0f, 0.0f);
@ -36,60 +41,48 @@ public class ModelCubeTexture
camera.Projection = CameraProjection.Perspective;
// Load texture to be applied to the cubes sides
Texture2D texture = LoadTexture("resources/cubicmap_atlas.png");
texture = LoadTexture("resources/cubicmap_atlas.png");
}
SetTargetFPS(60);
//--------------------------------------------------------------------------------------
public void Update()
{
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(Color.RayWhite);
// Main game loop
while (!WindowShouldClose())
{
// Update
//----------------------------------------------------------------------------------
//----------------------------------------------------------------------------------
BeginMode3D(camera);
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(Color.RayWhite);
// Draw cube with an applied texture
DrawCubeTexture(texture, new Vector3(-2.0f, 2.0f, 0.0f), 2.0f, 4.0f, 2.0f, Color.White);
BeginMode3D(camera);
// Draw cube with an applied texture, but only a defined rectangle piece of the texture
DrawCubeTextureRec(
texture,
new Rectangle(0, texture.Height / 2, texture.Width / 2, texture.Height / 2),
new Vector3(2.0f, 1.0f, 0.0f),
2.0f,
2.0f,
2.0f,
Color.White
);
// Draw cube with an applied texture
DrawCubeTexture(texture, new Vector3(-2.0f, 2.0f, 0.0f), 2.0f, 4.0f, 2.0f, Color.White);
DrawGrid(10, 1.0f);
// Draw cube with an applied texture, but only a defined rectangle piece of the texture
DrawCubeTextureRec(
texture,
new Rectangle(0, texture.Height / 2, texture.Width / 2, texture.Height / 2),
new Vector3(2.0f, 1.0f, 0.0f),
2.0f,
2.0f,
2.0f,
Color.White
);
EndMode3D();
DrawGrid(10, 1.0f);
EndDrawing();
//----------------------------------------------------------------------------------
}
EndMode3D();
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
public void Unload()
{
UnloadTexture(texture);
CloseWindow();
//--------------------------------------------------------------------------------------
return 0;
}
// Draw cube textured
// NOTE: Cube position is the center position
static void DrawCubeTexture(
private static void DrawCubeTexture(
Texture2D texture,
Vector3 position,
float width,
@ -98,9 +91,9 @@ public class ModelCubeTexture
Color color
)
{
float x = position.X;
float y = position.Y;
float z = position.Z;
var x = position.X;
var y = position.Y;
var z = position.Z;
// Set desired texture to be enabled while drawing following vertex data
Rlgl.SetTexture(texture.Id);
@ -218,7 +211,7 @@ public class ModelCubeTexture
}
// Draw cube with texture piece applied to all faces
static void DrawCubeTextureRec(
private static void DrawCubeTextureRec(
Texture2D texture,
Rectangle source,
Vector3 position,
@ -228,11 +221,11 @@ public class ModelCubeTexture
Color color
)
{
float x = position.X;
float y = position.Y;
float z = position.Z;
float texWidth = (float)texture.Width;
float texHeight = (float)texture.Height;
var x = position.X;
var y = position.Y;
var z = position.Z;
var texWidth = (float)texture.Width;
var texHeight = (float)texture.Height;
// Set desired texture to be enabled while drawing following vertex data
Rlgl.SetTexture(texture.Id);
@ -312,5 +305,32 @@ public class ModelCubeTexture
Rlgl.SetTexture(0);
}
}
public static int Main()
{
// Initialization
//--------------------------------------------------------------------------------------
InitWindow(screenWidth, screenHeight, "raylib [models] example - textured cube");
SetTargetFPS(60);
//--------------------------------------------------------------------------------------
var game = new ModelCubeTexture();
game.Init();
// Main game loop
while (!WindowShouldClose())
{
game.Update();
}
game.Unload();
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow();
//--------------------------------------------------------------------------------------
return 0;
}
}