|
| 1 | +/******************************************************************************************* |
| 2 | +* |
| 3 | +* raylib [models] example - Load 3d model with animations and play them |
| 4 | +* |
| 5 | +* Example originally created with raylib 2.5, last time updated with raylib 3.5 |
| 6 | +* |
| 7 | +* Example contributed by Culacant (@culacant) and reviewed by Ramon Santamaria (@raysan5) |
| 8 | +* |
| 9 | +* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified, |
| 10 | +* BSD-like license that allows static linking with closed source software |
| 11 | +* |
| 12 | +* Copyright (c) 2019-2023 Culacant (@culacant) and Ramon Santamaria (@raysan5) |
| 13 | +* |
| 14 | +******************************************************************************************** |
| 15 | +* |
| 16 | +* NOTE: To export a model from blender, make sure it is not posed, the vertices need to be |
| 17 | +* in the same position as they would be in edit mode and the scale of your models is |
| 18 | +* set to 0. Scaling can be done from the export menu. |
| 19 | +* |
| 20 | +********************************************************************************************/ |
| 21 | + |
| 22 | +const r = require('raylib') |
| 23 | +const { join } = require('path') |
| 24 | + |
| 25 | +// Initialization |
| 26 | +// -------------------------------------------------------------------------------------- |
| 27 | +const screenWidth = 800 |
| 28 | +const screenHeight = 450 |
| 29 | + |
| 30 | +r.InitWindow(screenWidth, screenHeight, 'raylib [models] example - model animation') |
| 31 | + |
| 32 | +// Define the camera to look into our 3d world |
| 33 | +const camera = { |
| 34 | + position: { x: 10, y: 10, z: 10 }, // Camera position |
| 35 | + target: { x: 0, y: 0, z: 0 }, // Camera looking at point |
| 36 | + up: { x: 0, y: 1, z: 0 }, // Camera up vector (rotation towards target) |
| 37 | + fovy: 45, // Camera field-of-view Y |
| 38 | + projection: r.CAMERA_PERSPECTIVE // Camera mode type |
| 39 | +} |
| 40 | + |
| 41 | +const model = r.LoadModel(join(__dirname, 'resources', 'guy', 'guy.iqm')) // Load the animated model mesh and basic data |
| 42 | +const texture = r.LoadTexture(join(__dirname, 'resources', 'guy', 'guytex.png')) // Load model texture and set material |
| 43 | + |
| 44 | +// this will fail as it can't return an array of materials |
| 45 | +const materials = r.LoadMaterials(join(__dirname, 'resources', 'guy', 'guy.iqm'), 1) |
| 46 | +console.log('materials: ', materials) |
| 47 | + |
| 48 | +r.SetMaterialTexture(model.materials[0], r.MATERIAL_MAP_DIFFUSE, texture) // Set model material map texture |
| 49 | + |
| 50 | +const position = { x: 0, y: 0, z: 0 } // Set model position |
| 51 | + |
| 52 | +// Load animation data |
| 53 | +let animsCount = 0 |
| 54 | +const anims = r.LoadModelAnimations('resources/models/iqm/guyanim.iqm', animsCount) |
| 55 | +animsCount = anims.length |
| 56 | +let animFrameCounter = 0 |
| 57 | + |
| 58 | +r.DisableCursor() // Catch cursor |
| 59 | +r.SetTargetFPS(60) // Set our game to run at 60 frames-per-second |
| 60 | +// -------------------------------------------------------------------------------------- |
| 61 | + |
| 62 | +// Main game loop |
| 63 | +while (!r.WindowShouldClose()) { // Detect window close button or ESC key |
| 64 | + // Update |
| 65 | + // ---------------------------------------------------------------------------------- |
| 66 | + r.UpdateCamera(camera, r.CAMERA_FIRST_PERSON) |
| 67 | + |
| 68 | + // Play animation when spacebar is held down |
| 69 | + if (r.IsKeyDown(r.KEY_SPACE)) { |
| 70 | + animFrameCounter++ |
| 71 | + r.UpdateModelAnimation(model, anims[0], animFrameCounter) |
| 72 | + if (animFrameCounter >= anims[0].frameCount) animFrameCounter = 0 |
| 73 | + } |
| 74 | + // ---------------------------------------------------------------------------------- |
| 75 | + |
| 76 | + // Draw |
| 77 | + // ---------------------------------------------------------------------------------- |
| 78 | + r.BeginDrawing() |
| 79 | + |
| 80 | + r.ClearBackground(r.RAYWHITE) |
| 81 | + |
| 82 | + r.BeginMode3D(camera) |
| 83 | + |
| 84 | + r.DrawModelEx(model, position, { x: 0, y: 0, z: 0 }, -90, { x: 1, y: 1, z: 1 }, r.WHITE) |
| 85 | + |
| 86 | + for (let i = 0; i < model.boneCount; i++) { |
| 87 | + r.DrawCube(anims[0].framePoses[animFrameCounter][i].translation, 0.2, 0.2, 0.2, r.RED) |
| 88 | + } |
| 89 | + |
| 90 | + r.DrawGrid(10, 1) // Draw a grid |
| 91 | + |
| 92 | + r.EndMode3D() |
| 93 | + |
| 94 | + r.DrawText('PRESS SPACE to PLAY MODEL ANIMATION', 10, 10, 20, r.MAROON) |
| 95 | + r.DrawText('(c) Guy IQM 3D model by @culacant', screenWidth - 200, screenHeight - 20, 10, r.GRAY) |
| 96 | + |
| 97 | + r.EndDrawing() |
| 98 | + // ---------------------------------------------------------------------------------- |
| 99 | +} |
| 100 | + |
| 101 | +// De-Initialization |
| 102 | +// -------------------------------------------------------------------------------------- |
| 103 | +r.UnloadTexture(texture) // Unload texture |
| 104 | +r.UnloadModelAnimations(anims, animsCount) // Unload model animations data |
| 105 | +r.UnloadModel(model) // Unload model |
| 106 | + |
| 107 | +r.CloseWindow() // Close window and OpenGL context |
| 108 | +// -------------------------------------------------------------------------------------- |
0 commit comments