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