| 1 |
{%MainUnit x3dloadinternalmd3.pas} |
| 2 |
{ |
| 3 |
Copyright 2007-2023 Michalis Kamburelis. |
| 4 |
|
| 5 |
This file is part of "Castle Game Engine". |
| 6 |
|
| 7 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 8 |
included in this distribution, for details about the copyright. |
| 9 |
|
| 10 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
|
| 14 |
---------------------------------------------------------------------------- |
| 15 |
} |
| 16 |
|
| 17 |
{ MD3 animation support (TMd3Animations and TMd3Animation classes), |
| 18 |
with animations info parsed from animation.cfg. } |
| 19 |
|
| 20 |
type |
| 21 |
{ MD3 animation information. |
| 22 |
Encoded following MD3 animation.cfg conventions. |
| 23 |
See e.g. https://github.com/edems96/MD3-Model---Animation-Viewer/blob/master/src/md3anim.h |
| 24 |
for sample source code reading MD3 animations. } |
| 25 |
TMd3Animation = class |
| 26 |
Name: String; |
| 27 |
|
| 28 |
{ Number of the first frame. } |
| 29 |
FirstFrame: Cardinal; |
| 30 |
|
| 31 |
{ Duration of animation, in frames. |
| 32 |
Divide by @link(Fps) to get duration in seconds. |
| 33 |
|
| 34 |
May be negative in animation.cfg (likely to indicate playing animation backwards, |
| 35 |
not supported). We ignore it for now, i.e. we take |
| 36 |
Abs(NumFrames)/Fps as just duration in seconds. } |
| 37 |
NumFrames: Integer; |
| 38 |
|
| 39 |
{ We read this from animation.cfg, but don't support now. |
| 40 |
It seems that it is either 0 (when animation is not supposed |
| 41 |
to be used a looped) or equal to NumFrames (when animation is |
| 42 |
supposed to be used as looped). } |
| 43 |
LoopingFrames: Cardinal; |
| 44 |
|
| 45 |
{ Frames per second, controlling the speed of this animation. } |
| 46 |
Fps: Cardinal; |
| 47 |
end; |
| 48 |
|
| 49 |
{ MD3 animations list. } |
| 50 |
TMd3Animations = class({$ifdef FPC}specialize{$endif} TObjectList<TMd3Animation>) |
| 51 |
{ Read animation information from animation.cfg file. } |
| 52 |
procedure ReadAnimationCfg(const AnimationCfgUrl: String); |
| 53 |
|
| 54 |
{ Delete from the list animations that are not suitable for the model with NumFrames. |
| 55 |
|
| 56 |
This is necessary, as animation.cfg is sometimes placed adjacent to multiple MD3 |
| 57 |
files, and it matches some but not all animations there, |
| 58 |
i.e. our current MD3 model may not even have enough frames to perform this animation. |
| 59 |
By removing invalid animations, we let TMd3Converter to process all animations |
| 60 |
without fear that it will cause buffer overflow when reading e.g. Tags array. |
| 61 |
|
| 62 |
We don't remove all animations, as this would remove too much. |
| 63 |
E.g. in Tremulous human player animations, this approach allows to at least |
| 64 |
play attack, gesture etc. of upper.md3. } |
| 65 |
procedure ValidateFrames(const NumFrames: Cardinal); |
| 66 |
end; |
| 67 |
|
| 68 |
procedure TMd3Animations.ReadAnimationCfg(const AnimationCfgUrl: String); |
| 69 |
var |
| 70 |
LegsOffset: Integer; |
| 71 |
|
| 72 |
{ For unknown reason, the first frame numbers for LEGS_xxx animations are shifted |
| 73 |
in at least Tremulous models. If we take them exactly, the legs animations |
| 74 |
will be broken (some LEGS_xxx animations will be nonsense, some will go beyond |
| 75 |
available number of frames). |
| 76 |
|
| 77 |
The solution is to shift the first frame by the difference in frames |
| 78 |
between LEGS_WALKCR and TORSO_GESTURE. |
| 79 |
This is a terrible hack, and I can only guess it is necessary because of some |
| 80 |
late hack in id Tech MD3 handling -- instead of fixing frame numbers in animation.cfg, |
| 81 |
someone decided to specifically shift LEGS_xxx numbers in code. |
| 82 |
And in effect everyone now has to shift LEGS_xxx in code, otherwise they are broken. |
| 83 |
|
| 84 |
The solution credit goes to |
| 85 |
https://github.com/MJL85/MenderD3/ , specifically |
| 86 |
https://github.com/MJL85/MenderD3/blob/4b95d8b0d03e46513d49e12c17ecac23f1ba8dd4/src/md3_parse.c#L702 . } |
| 87 |
procedure FixLegsFirstFrame(const Anim: TMd3Animation); |
| 88 |
|
| 89 |
function FirstFrameOf(const AnimName: String): Integer; |
| 90 |
var |
| 91 |
I: Integer; |
| 92 |
begin |
| 93 |
for I := 0 to Count - 1 do |
| 94 |
if Items[I].Name = AnimName then |
| 95 |
Exit(Items[I].FirstFrame); |
| 96 |
Result := 0; |
| 97 |
end; |
| 98 |
|
| 99 |
var |
| 100 |
LegsOffset1, LegsOffset2: Integer; |
| 101 |
begin |
| 102 |
if IsPrefix('LEGS_', Anim.Name, false) then |
| 103 |
begin |
| 104 |
{ calculate LegsOffset only once. |
| 105 |
Not only for performance, also because LEGS_WALKCR first frame will be changed |
| 106 |
by this offset right after first calculation. } |
| 107 |
if LegsOffset = 0 then |
| 108 |
begin |
| 109 |
LegsOffset1 := FirstFrameOf('LEGS_WALKCR'); |
| 110 |
LegsOffset2 := FirstFrameOf('TORSO_GESTURE'); |
| 111 |
LegsOffset := LegsOffset1 - LegsOffset2; |
| 112 |
{WritelnLog('Shifting MD3 LEGS animations first frame by LEGS_WALKCR - TORSO_GESTURE difference (%d - %d)', [ |
| 113 |
LegsOffset1, |
| 114 |
LegsOffset2 |
| 115 |
]);} |
| 116 |
end; |
| 117 |
Anim.FirstFrame := Anim.FirstFrame - LegsOffset; |
| 118 |
end; |
| 119 |
end; |
| 120 |
|
| 121 |
var |
| 122 |
Reader: TTextReader; |
| 123 |
Line, LineComment, AnimationName: String; |
| 124 |
Anim: TMd3Animation; |
| 125 |
Tokens: TCastleStringList; |
| 126 |
CommentPos: Integer; |
| 127 |
begin |
| 128 |
LegsOffset := 0; |
| 129 |
Reader := TTextReader.Create(AnimationCfgUrl); |
| 130 |
try |
| 131 |
while not Reader.Eof do |
| 132 |
begin |
| 133 |
Line := Reader.ReadLn; |
| 134 |
|
| 135 |
{ Calculate LineComment (after //), and trim Line (prefix before //). |
| 136 |
|
| 137 |
Animation name is the first token after // |
| 138 |
(sometimes separated by whitespace from //, sometimes not). |
| 139 |
So we do not just reject the comment, we keep it for later. |
| 140 |
We have to handle lines like this (examples from Tremulous): |
| 141 |
|
| 142 |
130 6 0 15 // TORSO_ATTACK (MUST NOT CHANGE -- hand animation is synced to this) |
| 143 |
90 40 0 20 // TORSO_GESTURE |
| 144 |
41 20 20 30 //NSPA_STAND |
| 145 |
} |
| 146 |
CommentPos := Pos('//', Line); |
| 147 |
if CommentPos <> 0 then |
| 148 |
begin |
| 149 |
LineComment := SEnding(Line, CommentPos + 2); |
| 150 |
Line := Copy(Line, 1, CommentPos - 1); |
| 151 |
end else |
| 152 |
LineComment := ''; |
| 153 |
|
| 154 |
Tokens := CreateTokens(Line); |
| 155 |
try |
| 156 |
// skip lines that don't interest us |
| 157 |
if Tokens.Count = 0 then |
| 158 |
Continue; // empty line |
| 159 |
if (Tokens[0] = 'sex') or // various ignored information |
| 160 |
(Tokens[0] = 'footsteps') or |
| 161 |
(Tokens[0] = 'headoffset') or // example in upper.md3 submitted by https://forum.castle-engine.io/t/md3-improve-animations-support/775/61?u=michalis |
| 162 |
(Tokens[0] = 'nonsegmented') then |
| 163 |
Continue; |
| 164 |
if Tokens.Count <> 4 then |
| 165 |
begin |
| 166 |
WritelnWarning('Unexpected line format in animation.cfg "%s", not 4 items before comment', [Line]); |
| 167 |
Continue; |
| 168 |
end; |
| 169 |
|
| 170 |
AnimationName := NextTokenOnce(LineComment); |
| 171 |
if AnimationName = '' then |
| 172 |
begin |
| 173 |
WritelnWarning('Empty animation name in animation.cfg'); |
| 174 |
Continue; |
| 175 |
end; |
| 176 |
|
| 177 |
// line defines a animation |
| 178 |
Anim := TMd3Animation.Create; |
| 179 |
Anim.FirstFrame := StrToInt(Tokens[0]); |
| 180 |
Anim.NumFrames := StrToInt(Tokens[1]); |
| 181 |
Anim.LoopingFrames := StrToInt(Tokens[2]); |
| 182 |
Anim.Fps := StrToInt(Tokens[3]); |
| 183 |
{ Some MD3 models have 0 for FPS, |
| 184 |
testcase: tremulous-data/models/buildables/booster/animation.cfg |
| 185 |
(Tremoulous data from https://github.com/wtfbbqhax/tremulous-data/blob/master/data-1.1.0.pk3 ). |
| 186 |
This is not a valid value (and NumFrames/Fps would yield NaN) |
| 187 |
so we replace it with DefaultMd3Fps. } |
| 188 |
if Anim.Fps = 0 then |
| 189 |
Anim.Fps := DefaultMd3Fps; |
| 190 |
Anim.Name := AnimationName; |
| 191 |
Add(Anim); |
| 192 |
|
| 193 |
FixLegsFirstFrame(Anim); |
| 194 |
finally FreeAndNil(Tokens) end; |
| 195 |
end; |
| 196 |
finally FreeAndNil(Reader) end; |
| 197 |
end; |
| 198 |
|
| 199 |
procedure TMd3Animations.ValidateFrames(const NumFrames: Cardinal); |
| 200 |
var |
| 201 |
RequiredNumFrames, I: Integer; |
| 202 |
begin |
| 203 |
for I := Count - 1 downto 0 do |
| 204 |
begin |
| 205 |
RequiredNumFrames := Items[I].FirstFrame + Abs(Items[I].NumFrames); |
| 206 |
if RequiredNumFrames > NumFrames then |
| 207 |
begin |
| 208 |
// This warning would be too spamming, ignore |
| 209 |
{ |
| 210 |
WritelnWarning('Animation "%s" defined in "animation.cfg" file would require more frames (%d) than this MD3 model has (%d), ignoring this animation.', [ |
| 211 |
Items[I].Name, |
| 212 |
RequiredNumFrames, |
| 213 |
NumFrames |
| 214 |
]); |
| 215 |
} |
| 216 |
Delete(I); |
| 217 |
end; |
| 218 |
end; |
| 219 |
end; |