← ziglang.inraylib-zigGitHub

mesh instancing

★★★★by seanpringle (@seanpringle)

There is no browser build of this example yet.

Is it the same as C?

//! raylib [shaders] example - mesh instancing
//!
//! Zig-API translation of examples/shaders/shaders_mesh_instancing.c (raylib 6.2).
//! Example contributed by seanpringle (@seanpringle) and reviewed by Max (@moliad) and Ramon Santamaria (@raysan5)
//! Example licensed under an unmodified zlib/libpng license.
//! Copyright (c) 2020-2025 seanpringle (@seanpringle), Max (@moliad) and Ramon Santamaria (@raysan5)

const std = @import("std");
const rl = @import("raylib");
const rlights = @import("rlights");

const glsl_version = 330;

const max_instances = 10000;

/// A random value in [min, max], as f32
fn randomFloat(min: i32, max: i32) f32 {
    return @floatFromInt(rl.getRandomValue(min, max));
}

pub fn main() !void {
    // Initialization
    const screen_width = 800;
    const screen_height = 450;

    rl.initWindow(screen_width, screen_height, "raylib [shaders] example - mesh instancing");
    defer rl.closeWindow(); // Close window and OpenGL context

    // Define the camera to look into our 3d world
    var camera: rl.Camera = .{
        .position = .{ .x = -125.0, .y = 125.0, .z = -125.0 }, // Camera position
        .target = .{ .x = 0.0, .y = 0.0, .z = 0.0 }, // Camera looking at point
        .up = .{ .x = 0.0, .y = 1.0, .z = 0.0 }, // Camera up vector (rotation towards target)
        .fovy = 45.0, // Camera field-of-view Y
        .projection = .perspective, // Camera projection type
    };

    // Define mesh to be instanced
    const cube = rl.genMeshCube(1.0, 1.0, 1.0);
    defer rl.unloadMesh(cube); // Unload cube mesh

    // Define transforms to be uploaded to GPU for instances
    const allocator = std.heap.c_allocator;
    const transforms = try allocator.alloc(rl.Matrix, max_instances); // Pre-multiplied transformations passed to rlgl
    defer allocator.free(transforms);

    // Translate and rotate cubes randomly
    for (transforms) |*transform| {
        const tx = randomFloat(-50, 50);
        const ty = randomFloat(-50, 50);
        const tz = randomFloat(-50, 50);
        const translation = rl.math.matrixTranslate(tx, ty, tz);
        const ax = randomFloat(0, 360);
        const ay = randomFloat(0, 360);
        const az = randomFloat(0, 360);
        const axis = rl.math.vector3Normalize(.{ .x = ax, .y = ay, .z = az });
        const angle = randomFloat(0, 180) * rl.deg2rad;
        const rotation = rl.math.matrixRotate(axis, angle);

        transform.* = rl.math.matrixMultiply(rotation, translation);
    }

    // Load lighting shader
    const shader = try rl.loadShader(
        std.fmt.comptimePrint("resources/shaders/glsl{d}/lighting_instancing.vs", .{glsl_version}),
        std.fmt.comptimePrint("resources/shaders/glsl{d}/lighting.fs", .{glsl_version}),
    );
    // Get shader locations
    shader.loc(.matrix_mvp).* = rl.getShaderLocation(shader, "mvp");
    shader.loc(.vector_view).* = rl.getShaderLocation(shader, "viewPos");

    // Set shader value: ambient light level
    const ambient_loc = rl.getShaderLocation(shader, "ambient");
    rl.setShaderValue(shader, ambient_loc, [4]f32{ 0.2, 0.2, 0.2, 1.0 });

    // Create one light
    _ = rlights.createLight(.directional, .{ .x = 50.0, .y = 50.0, .z = 0.0 }, rl.math.vector3Zero(), .white, shader);

    // NOTE: We are assigning the intancing shader to material.shader
    // to be used on mesh drawing with drawMeshInstanced()
    var mat_instances = rl.loadMaterialDefault();
    defer rl.unloadMaterial(mat_instances); // Also unloads the instancing shader
    mat_instances.shader = shader;
    mat_instances.map(.diffuse).color = .red;

    // Load default material (using raylib intenral default shader) for non-instanced mesh drawing
    // WARNING: Default shader enables vertex color attribute BUT genMeshCube() does not generate vertex colors, so,
    // when drawing the color attribute is disabled and a default color value is provided as input for thevertex attribute
    const mat_default = rl.loadMaterialDefault();
    defer rl.unloadMaterial(mat_default); // Unload default material
    mat_default.map(.diffuse).color = .blue;

    rl.setTargetFPS(60); // Set our game to run at 60 frames-per-second

    // Main game loop
    while (!rl.windowShouldClose()) { // Detect window close button or ESC key
        // Update
        rl.updateCamera(&camera, .orbital);

        // Update the light shader with the camera view position
        rl.setShaderValue(shader, shader.loc(.vector_view).*, camera.position);

        // Draw
        rl.beginDrawing();
        defer rl.endDrawing();

        rl.clearBackground(.ray_white);

        {
            rl.beginMode3D(camera);
            defer rl.endMode3D();

            // Draw cube mesh with default material (BLUE)
            rl.drawMesh(cube, mat_default, rl.math.matrixTranslate(-10.0, 0.0, 0.0));

            // Draw meshes instanced using material containing instancing shader (RED + lighting),
            // transforms[] for the instances should be provided, they are dynamically
            // updated in GPU every frame, so we can animate the different mesh instances
            rl.drawMeshInstanced(cube, mat_instances, transforms);

            // Draw cube mesh with default material (BLUE)
            rl.drawMesh(cube, mat_default, rl.math.matrixTranslate(10.0, 0.0, 0.0));
        }

        rl.drawFPS(10, 10);
    }
}
/*******************************************************************************************
*
*   raylib [shaders] example - mesh instancing
*
*   Example complexity rating: [★★★★] 4/4
*
*   Example originally created with raylib 3.7, last time updated with raylib 4.2
*
*   Example contributed by seanpringle (@seanpringle) and reviewed by Max (@moliad) and Ramon Santamaria (@raysan5)
*
*   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) 2020-2025 seanpringle (@seanpringle), Max (@moliad) and Ramon Santamaria (@raysan5)
*
********************************************************************************************/

#include "raylib.h"
#include "raymath.h"

#define RLIGHTS_IMPLEMENTATION
#include "rlights.h"

#include <stdlib.h>         // Required for: calloc(), free()

#if defined(PLATFORM_DESKTOP)
    #define GLSL_VERSION            330
#else   // PLATFORM_ANDROID, PLATFORM_WEB
    #define GLSL_VERSION            100
#endif

#define MAX_INSTANCES  10000

//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
int main(void)
{
    // Initialization
    //--------------------------------------------------------------------------------------
    const int screenWidth = 800;
    const int screenHeight = 450;

    InitWindow(screenWidth, screenHeight, "raylib [shaders] example - mesh instancing");

    // Define the camera to look into our 3d world
    Camera camera = { 0 };
    camera.position = (Vector3){ -125.0f, 125.0f, -125.0f };    // Camera position
    camera.target = (Vector3){ 0.0f, 0.0f, 0.0f };              // Camera looking at point
    camera.up = (Vector3){ 0.0f, 1.0f, 0.0f };                  // Camera up vector (rotation towards target)
    camera.fovy = 45.0f;                                        // Camera field-of-view Y
    camera.projection = CAMERA_PERSPECTIVE;                     // Camera projection type

    // Define mesh to be instanced
    Mesh cube = GenMeshCube(1.0f, 1.0f, 1.0f);

    // Define transforms to be uploaded to GPU for instances
    Matrix *transforms = (Matrix *)RL_CALLOC(MAX_INSTANCES, sizeof(Matrix));   // Pre-multiplied transformations passed to rlgl

    // Translate and rotate cubes randomly
    for (int i = 0; i < MAX_INSTANCES; i++)
    {
        Matrix translation = MatrixTranslate((float)GetRandomValue(-50, 50), (float)GetRandomValue(-50, 50), (float)GetRandomValue(-50, 50));
        Vector3 axis = Vector3Normalize((Vector3){ (float)GetRandomValue(0, 360), (float)GetRandomValue(0, 360), (float)GetRandomValue(0, 360) });
        float angle = (float)GetRandomValue(0, 180)*DEG2RAD;
        Matrix rotation = MatrixRotate(axis, angle);

        transforms[i] = MatrixMultiply(rotation, translation);
    }

    // Load lighting shader
    Shader shader = LoadShader(TextFormat("resources/shaders/glsl%i/lighting_instancing.vs", GLSL_VERSION),
                               TextFormat("resources/shaders/glsl%i/lighting.fs", GLSL_VERSION));
    // Get shader locations
    shader.locs[SHADER_LOC_MATRIX_MVP] = GetShaderLocation(shader, "mvp");
    shader.locs[SHADER_LOC_VECTOR_VIEW] = GetShaderLocation(shader, "viewPos");

    // Set shader value: ambient light level
    int ambientLoc = GetShaderLocation(shader, "ambient");
    SetShaderValue(shader, ambientLoc, (float[4]){ 0.2f, 0.2f, 0.2f, 1.0f }, SHADER_UNIFORM_VEC4);

    // Create one light
    CreateLight(LIGHT_DIRECTIONAL, (Vector3){ 50.0f, 50.0f, 0.0f }, Vector3Zero(), WHITE, shader);

    // NOTE: We are assigning the intancing shader to material.shader
    // to be used on mesh drawing with DrawMeshInstanced()
    Material matInstances = LoadMaterialDefault();
    matInstances.shader = shader;
    matInstances.maps[MATERIAL_MAP_DIFFUSE].color = RED;

    // Load default material (using raylib intenral default shader) for non-instanced mesh drawing
    // WARNING: Default shader enables vertex color attribute BUT GenMeshCube() does not generate vertex colors, so,
    // when drawing the color attribute is disabled and a default color value is provided as input for thevertex attribute
    Material matDefault = LoadMaterialDefault();
    matDefault.maps[MATERIAL_MAP_DIFFUSE].color = BLUE;

    SetTargetFPS(60);                   // Set our game to run at 60 frames-per-second
    //--------------------------------------------------------------------------------------

    // Main game loop
    while (!WindowShouldClose())        // Detect window close button or ESC key
    {
        // Update
        //----------------------------------------------------------------------------------
        UpdateCamera(&camera, CAMERA_ORBITAL);

        // Update the light shader with the camera view position
        float cameraPos[3] = { camera.position.x, camera.position.y, camera.position.z };
        SetShaderValue(shader, shader.locs[SHADER_LOC_VECTOR_VIEW], cameraPos, SHADER_UNIFORM_VEC3);
        //----------------------------------------------------------------------------------

        // Draw
        //----------------------------------------------------------------------------------
        BeginDrawing();

            ClearBackground(RAYWHITE);

            BeginMode3D(camera);

                // Draw cube mesh with default material (BLUE)
                DrawMesh(cube, matDefault, MatrixTranslate(-10.0f, 0.0f, 0.0f));

                // Draw meshes instanced using material containing instancing shader (RED + lighting),
                // transforms[] for the instances should be provided, they are dynamically
                // updated in GPU every frame, so we can animate the different mesh instances
                DrawMeshInstanced(cube, matInstances, transforms, MAX_INSTANCES);

                // Draw cube mesh with default material (BLUE)
                DrawMesh(cube, matDefault, MatrixTranslate(10.0f, 0.0f, 0.0f));

            EndMode3D();

            DrawFPS(10, 10);

        EndDrawing();
        //----------------------------------------------------------------------------------
    }

    // De-Initialization
    //--------------------------------------------------------------------------------------
    RL_FREE(transforms);    // Free transforms
    UnloadMaterial(matInstances);    // Also unloads the instancing shader
    UnloadMaterial(matDefault);      // Unload default material
    UnloadMesh(cube);                // Unload cube mesh

    CloseWindow();          // Close window and OpenGL context
    //--------------------------------------------------------------------------------------

    return 0;
}

From raylib 6.2. The original on GitHub.