-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodels_animation.php
143 lines (117 loc) · 4.54 KB
/
models_animation.php
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
declare(strict_types=1);
use Nawarian\Raylib\Raylib;
use Nawarian\Raylib\Types\{Camera3D, Color, Vector3};
use function Nawarian\Raylib\{
BeginDrawing,
BeginMode3D,
ClearBackground,
CloseWindow,
DrawCube,
DrawGrid,
DrawModelEx,
DrawText,
EndDrawing,
EndMode3D,
InitWindow,
IsKeyDown,
LoadModel,
LoadModelAnimations,
LoadTexture,
SetCameraMode,
SetMaterialTexture,
SetTargetFPS,
UnloadModel,
UnloadModelAnimation,
UnloadTexture,
UpdateCamera,
UpdateModelAnimation,
WindowShouldClose
};
require_once __DIR__ . '/../../vendor/autoload.php';
// Initialization
//--------------------------------------------------------------------------------------
$screenWidth = 800;
$screenHeight = 450;
InitWindow($screenWidth, $screenHeight, 'raylib [models] example - model animation');
// Define the camera to look into our 3d world
$camera = new Camera3D(
new Vector3(10, 10, 10),
new Vector3(0, 0, 0),
new Vector3(0, 1, 0),
45,
Camera3D::PROJECTION_PERSPECTIVE,
);
$model = LoadModel(__DIR__ . '/resources/guy/guy.iqm'); // Load the animated model mesh and basic data
$texture = LoadTexture(__DIR__ . '/resources/guy/guytex.png'); // Load model texture and set material
/**
* @psalm-suppress UndefinedMethod
* @psalm-suppress MixedArgument
*/
SetMaterialTexture($model->materials[0], Raylib::MAP_DIFFUSE, $texture); // Set model material map texture
$position = new Vector3(0, 0, 0); // Set model position
// Load animation data
$animsCount = 0;
/** @var \FFI\CData&\ArrayAccess<int, \FFI\CData> $anims */
$anims = LoadModelAnimations(__DIR__ . '/resources/guy/guyanim.iqm', $animsCount);
$animFrameCounter = 0;
SetCameraMode($camera, Camera3D::MODE_FREE); // Set free camera mode
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);
// Play animation when spacebar is held down
if (IsKeyDown(Raylib::KEY_SPACE)) {
$animFrameCounter++;
UpdateModelAnimation($model, $anims[0], $animFrameCounter);
/** @psalm-suppress UndefinedPropertyFetch */
if ($animFrameCounter >= $anims[0]->frameCount) {
$animFrameCounter = 0;
}
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(Color::rayWhite());
// phpcs:disable Generic.WhiteSpace.ScopeIndent.IncorrectExact
BeginMode3D($camera);
DrawModelEx($model, $position, new Vector3(1, 0, 0), -90, new Vector3(1, 1, 1), Color::white());
for ($i = 0; $i < $model->boneCount; $i++) {
/**
* @var stdClass $ffiPosition
* @psalm-suppress UndefinedPropertyFetch
* @psalm-suppress MixedArrayAccess
* @psalm-suppress MixedPropertyFetch
*/
$ffiPosition = $anims[0]->framePoses[$animFrameCounter][$i]->translation;
DrawCube(
new Vector3((float) $ffiPosition->x, (float) $ffiPosition->y, (float) $ffiPosition->z),
0.2,
0.2,
0.2,
Color::red()
);
}
DrawGrid(10, 1.0); // Draw a grid
EndMode3D();
DrawText('PRESS SPACE to PLAY MODEL ANIMATION', 10, 10, 20, Color::maroon());
DrawText('(c) Guy IQM 3D model by @culacant', $screenWidth - 200, $screenHeight - 20, 10, Color::gray());
// phpcs:enable Generic.WhiteSpace.ScopeIndent.IncorrectExact
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadTexture($texture); // Unload texture
// Unload model animations data
for ($i = 0; $i < $animsCount; $i++) {
UnloadModelAnimation($anims[$i]);
}
FFI::free($anims);
UnloadModel($model); // Unload model
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------