| 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 |
{ TMD3Converter class, that uses other classes in this unit to perform full |
| 18 |
read of MD3 and convertion to X3D. } |
| 19 |
|
| 20 |
type |
| 21 |
TAppearancesDictionary = {$ifdef FPC}specialize{$endif} TDictionary<String, TAppearanceNode>; |
| 22 |
|
| 23 |
{ MD3 (Quake3 engine model format) reader and converter to X3D. } |
| 24 |
TMD3Converter = class |
| 25 |
strict private |
| 26 |
FBaseUrl: String; |
| 27 |
SkinName: String; |
| 28 |
|
| 29 |
{ Material nodes instantiated so far by FrameToX3D. } |
| 30 |
Appearances: TAppearancesDictionary; |
| 31 |
|
| 32 |
{ Read the indicated .skin file and fill the Skin contents, if skin file exists. } |
| 33 |
procedure ReadSkinFile(const SkinUrl: String); |
| 34 |
|
| 35 |
{ Read the stream containing MD3 file. } |
| 36 |
procedure ReadMd3(const Stream: TStream); |
| 37 |
|
| 38 |
{ Convert texture URL found in skin file to a URL, relative to the MD3 file, |
| 39 |
pointing to valid texture file. |
| 40 |
This strips path from URL, always. |
| 41 |
This can also fix (or add) URL extension. } |
| 42 |
class function FixTextureUrl(const TextureUrl, TexturePath: String): String; static; |
| 43 |
|
| 44 |
{ Load a specific animation frame from a given MD3 model. |
| 45 |
@param FrameNumber Frame number to load, must be < Md3.NumFrames. |
| 46 |
@param SceneBox Will be increased by given frame bbox. } |
| 47 |
function FrameToX3D(const FrameNumber: Cardinal; var SceneBox: TBox3D): TGroupNode; |
| 48 |
|
| 49 |
{ Read settings (for now, only SkinName) from anchor inside BaseUrl. } |
| 50 |
procedure SettingsFromAnchor; |
| 51 |
public |
| 52 |
Name: String; |
| 53 |
Surfaces: TMd3SurfaceList; |
| 54 |
Tags: TMd3TagList; |
| 55 |
NumFrames: Integer; |
| 56 |
|
| 57 |
// Always 0 when NumFrames = 0 |
| 58 |
NumTags: Integer; |
| 59 |
|
| 60 |
{ Skin information maps shader names -> texture URLs. |
| 61 |
Empty (but never @nil) if the skin file was not found. } |
| 62 |
Skin: TStringList; |
| 63 |
|
| 64 |
{ Reads MD3 from a stream, with given absolute base URL. |
| 65 |
|
| 66 |
We also read the associated skin file, if it exists. |
| 67 |
We look for file called '<basename>_<SkinName>.skin', |
| 68 |
where |
| 69 |
|
| 70 |
- <basename> is the basename (no path, no extension, no anchor) from BaseUrl |
| 71 |
- <SkinName> may be specified as anchor #skin=xxx at the end of BaseUrl, |
| 72 |
by default 'default'. |
| 73 |
|
| 74 |
So e.g. for 'head.md3' we read 'head_default.skin' by default, |
| 75 |
for 'head.md3#skin:light' we read 'head_light.skin'. |
| 76 |
|
| 77 |
We strip the path from texture URLs found in the skin file, |
| 78 |
because the texture URLs are often given relative to main quake/tremulous/etc. |
| 79 |
data directory, and we don't know this directory. |
| 80 |
So we strip path part, and assume that textures for MD3 models |
| 81 |
are just inside the same directory as the MD3 file. |
| 82 |
|
| 83 |
The Stream must be freely seekable |
| 84 |
(i.e. setting Position to any value must be supported) --- |
| 85 |
if unsure, you can wrap your stream in something like TMemoryStream. |
| 86 |
|
| 87 |
The Stream instance is not owned by this object --- which means |
| 88 |
that if you created it, you should also free it, this class will not |
| 89 |
do it for you. You can free Stream right after constructor finished |
| 90 |
it's work. } |
| 91 |
constructor Create(const Stream: TStream; const ABaseUrl: String); |
| 92 |
destructor Destroy; override; |
| 93 |
|
| 94 |
property BaseUrl: String read FBaseUrl; |
| 95 |
|
| 96 |
function ToX3D: TX3DRootNode; |
| 97 |
end; |
| 98 |
|
| 99 |
constructor TMD3Converter.Create(const Stream: TStream; const ABaseUrl: String); |
| 100 |
var |
| 101 |
SkinUrl: String; |
| 102 |
begin |
| 103 |
inherited Create; |
| 104 |
|
| 105 |
FBaseUrl := ABaseUrl; |
| 106 |
Tags := TMd3TagList.Create; |
| 107 |
Appearances := TAppearancesDictionary.Create; |
| 108 |
|
| 109 |
SkinName := 'default'; // default skin, SettingsFromAnchor may override it |
| 110 |
SettingsFromAnchor; |
| 111 |
// for all following operations, we don't want #anchor in URL anymore |
| 112 |
FBaseUrl := URIDeleteAnchor(FBaseUrl); |
| 113 |
|
| 114 |
Skin := TStringList.Create; |
| 115 |
SkinUrl := DeleteURIExt(BaseUrl) + '_' + SkinName + '.skin'; |
| 116 |
ReadSkinFile(SkinUrl); |
| 117 |
|
| 118 |
ReadMd3(Stream); |
| 119 |
end; |
| 120 |
|
| 121 |
destructor TMD3Converter.Destroy; |
| 122 |
begin |
| 123 |
FreeAndNil(Surfaces); |
| 124 |
FreeAndNil(Skin); |
| 125 |
FreeAndNil(Tags); |
| 126 |
FreeAndNil(Appearances); |
| 127 |
inherited; |
| 128 |
end; |
| 129 |
|
| 130 |
procedure TMD3Converter.SettingsFromAnchor; |
| 131 |
var |
| 132 |
SettingsMap: TStringStringMap; |
| 133 |
Setting: {$ifdef FPC}TStringStringMap.TDictionaryPair{$else}TPair<String, String>{$endif}; |
| 134 |
begin |
| 135 |
SettingsMap := TStringStringMap.Create; |
| 136 |
try |
| 137 |
URIGetSettingsFromAnchor(BaseUrl, SettingsMap); |
| 138 |
for Setting in SettingsMap do |
| 139 |
begin |
| 140 |
if LowerCase(Setting.Key) = 'skin' then |
| 141 |
begin |
| 142 |
SkinName := Setting.Value; |
| 143 |
end else |
| 144 |
WritelnWarning('MD3', 'Unknown setting (%s) in "%s" anchor.', [ |
| 145 |
Setting.Key, |
| 146 |
UriDisplay(BaseUrl) |
| 147 |
]); |
| 148 |
end; |
| 149 |
finally |
| 150 |
FreeAndNil(SettingsMap); |
| 151 |
end; |
| 152 |
end; |
| 153 |
|
| 154 |
class function TMD3Converter.FixTextureUrl(const TextureUrl, TexturePath: String): String; |
| 155 |
begin |
| 156 |
{ Directory recorded in TextureUrl is useless for us, |
| 157 |
it's usually a relative directory inside quake/tremulous/etc. |
| 158 |
data. We just assume that this is inside the same dir as |
| 159 |
md3 model file, so we strip the directory part. } |
| 160 |
Result := ExtractURIName(TextureUrl); |
| 161 |
|
| 162 |
if not URIFileExists(TexturePath + Result) then |
| 163 |
begin |
| 164 |
{ We cannot trust |
| 165 |
the extension of texture given in Result in skin file. |
| 166 |
It's common in Quake3 data files that texture URL is given |
| 167 |
as xxx.tga, while in fact only xxx.jpg exists and should be used. } |
| 168 |
if URIFileExists(TexturePath + ChangeURIExt(Result, '.jpg')) then |
| 169 |
Result := ChangeURIExt(Result, '.jpg') else |
| 170 |
|
| 171 |
{ Also, some files have texture names with missing extension... |
| 172 |
So we have to check also for tga here. |
| 173 |
E.g. tremulous-unpacked-data/models/players/human_base/head } |
| 174 |
if URIFileExists(TexturePath + ChangeURIExt(Result, '.tga')) then |
| 175 |
Result := ChangeURIExt(Result, '.tga'); |
| 176 |
end; |
| 177 |
end; |
| 178 |
|
| 179 |
procedure TMD3Converter.ReadSkinFile(const SkinUrl: String); |
| 180 |
var |
| 181 |
SkinFile: TTextReader; |
| 182 |
S, SkinKey, TextureUrl: String; |
| 183 |
CommaPos: Integer; |
| 184 |
TexturePath: string; |
| 185 |
begin |
| 186 |
if URIFileExists(SkinUrl) then |
| 187 |
begin |
| 188 |
TexturePath := ExtractURIPath(SkinUrl); |
| 189 |
SkinFile := TTextReader.Create(SkinUrl); |
| 190 |
try |
| 191 |
{ Note: We don't just do |
| 192 |
Skin.NameValueSeparator := ','; |
| 193 |
Skin.LoadFromURL(ATextureUrl); |
| 194 |
because |
| 195 |
- MissingNameValueSeparatorAction doesn't seem supported by Delphi |
| 196 |
( https://www.freepascal.org/docs-html/rtl/classes/tstrings.missingnamevalueseparatoraction.html ) |
| 197 |
- we want to process texture URL anyway before we add it. |
| 198 |
} |
| 199 |
|
| 200 |
while not SkinFile.Eof do |
| 201 |
begin |
| 202 |
S := SkinFile.Readln; |
| 203 |
CommaPos := Pos(',', S); |
| 204 |
if CommaPos <> 0 then |
| 205 |
begin |
| 206 |
SkinKey := Trim(Copy(S, 1, CommaPos - 1)); |
| 207 |
TextureUrl := Trim(SEnding(S, CommaPos + 1)); |
| 208 |
{ Tremulous uses 'gfx/null' instead of just 'null', |
| 209 |
see tremulous-data/data-1.1.0/models/players/human_base/upper_light.skin } |
| 210 |
if TextureUrl = 'gfx/null' then |
| 211 |
TextureUrl := 'null'; |
| 212 |
if (TextureUrl <> '') and |
| 213 |
(TextureUrl <> 'null') then |
| 214 |
TextureUrl := FixTextureUrl(TextureUrl, TexturePath); |
| 215 |
Skin.Add(SkinKey + '=' + TextureUrl); |
| 216 |
end else |
| 217 |
WritelnWarning('Unexpected line in MD3 .skin file (no comma): %s', S); |
| 218 |
end; |
| 219 |
finally FreeAndNil(SkinFile) end; |
| 220 |
|
| 221 |
//WritelnLog('MD3 skin file found, with %d pairs (shader name -> texture URL): %s', [Skin.Count, Skin.Text]); |
| 222 |
end; |
| 223 |
end; |
| 224 |
|
| 225 |
procedure TMD3Converter.ReadMd3(const Stream: TStream); |
| 226 |
const |
| 227 |
GoodVersion = 15; |
| 228 |
type |
| 229 |
TMd3Header = record |
| 230 |
Ident: array [0..3] of AnsiChar; |
| 231 |
Version: Int32; |
| 232 |
Name: array [0..Md3MaxQPath - 1] of AnsiChar; |
| 233 |
Flags: Int32; |
| 234 |
NumFrames: Int32; |
| 235 |
NumTags: Int32; |
| 236 |
NumSurfaces: Int32; |
| 237 |
NumSkins: Int32; |
| 238 |
OffsetFrames: Int32; |
| 239 |
OffsetTags: Int32; |
| 240 |
OffsetSurfaces: Int32; |
| 241 |
OffsetEof: Int32; |
| 242 |
end; |
| 243 |
|
| 244 |
(* Unused for now |
| 245 |
TMd3Frame = record |
| 246 |
MinBounds: TVector3; |
| 247 |
MaxBounds: TVector3; |
| 248 |
LocalOrigin: TVector3; |
| 249 |
Radius: Single; |
| 250 |
Name: array [0..15] of AnsiChar; |
| 251 |
end; |
| 252 |
*) |
| 253 |
|
| 254 |
var |
| 255 |
Header: TMd3Header; |
| 256 |
(* Unused for now |
| 257 |
Frame: TMd3Frame; |
| 258 |
Tag: TMd3Tag; |
| 259 |
*) |
| 260 |
I: Integer; |
| 261 |
NewSurface: TMd3Surface; |
| 262 |
begin |
| 263 |
inherited Create; |
| 264 |
|
| 265 |
Stream.ReadBuffer(Header, SizeOf(TMd3Header)); |
| 266 |
|
| 267 |
if not CompareMem(@Header.Ident[0], @GoodIdent[1], Length(GoodIdent)) then |
| 268 |
raise EInvalidMD3.CreateFmt('Identifier of MD3 file must be "%s"', |
| 269 |
[GoodIdent]); |
| 270 |
|
| 271 |
if Header.Version <> GoodVersion then |
| 272 |
raise EInvalidMD3.CreateFmt('Only supported version of MD3 file is "%d"', |
| 273 |
[GoodVersion]); |
| 274 |
|
| 275 |
Name := Header.Name; |
| 276 |
NumFrames := Header.NumFrames; |
| 277 |
NumTags := Header.NumTags; |
| 278 |
|
| 279 |
{ Due to the way MD3 is specified, there's no way to specify tags |
| 280 |
when there are no animation frames. |
| 281 |
Simplify code so that we can rely on it, |
| 282 |
so that we know NumTags<>0 => NumFrames<>0. } |
| 283 |
if NumFrames = 0 then |
| 284 |
NumTags := 0; |
| 285 |
|
| 286 |
(* Unused for now |
| 287 |
if Header.NumFrames <> 0 then |
| 288 |
begin |
| 289 |
Stream.Position := Header.OffsetFrames; |
| 290 |
for I := 0 to Header.NumFrames - 1 do |
| 291 |
begin |
| 292 |
Stream.ReadBuffer(Frame, SizeOf(Frame)); |
| 293 |
end; |
| 294 |
end; |
| 295 |
*) |
| 296 |
|
| 297 |
Tags.Count := NumFrames * NumTags; |
| 298 |
if Tags.Count <> 0 then |
| 299 |
begin |
| 300 |
Stream.Position := Header.OffsetTags; |
| 301 |
Stream.ReadBuffer(Tags.L^, SizeOf(TMd3Tag) * Tags.Count); |
| 302 |
end; |
| 303 |
|
| 304 |
Surfaces := TMd3SurfaceList.Create(true); |
| 305 |
|
| 306 |
if Header.NumSurfaces <> 0 then |
| 307 |
begin |
| 308 |
Stream.Position := Header.OffsetSurfaces; |
| 309 |
for I := 0 to Header.NumSurfaces - 1 do |
| 310 |
begin |
| 311 |
NewSurface := TMd3Surface.Create; |
| 312 |
NewSurface.Read(Stream); |
| 313 |
Surfaces.Add(NewSurface); |
| 314 |
|
| 315 |
if NumFrames <> NewSurface.NumFrames then |
| 316 |
raise EInvalidMD3.CreateFmt('Surface frame count (%d) is different than ' + |
| 317 |
'model''s frame count (%d)', [NumFrames, NewSurface.NumFrames]); |
| 318 |
end; |
| 319 |
end; |
| 320 |
end; |
| 321 |
|
| 322 |
function TMD3Converter.FrameToX3D(const FrameNumber: Cardinal; var SceneBox: TBox3D): TGroupNode; |
| 323 |
|
| 324 |
function MakeCoordinates(Vertexes: TMd3VertexList; |
| 325 |
VertexesInFrameCount: Cardinal): TCoordinateNode; |
| 326 |
const |
| 327 |
Md3XyzScale = 1 / 64; |
| 328 |
var |
| 329 |
I: Integer; |
| 330 |
V: PMd3Vertex; |
| 331 |
begin |
| 332 |
Result := TCoordinateNode.Create('', BaseUrl); |
| 333 |
Result.FdPoint.Items.Count := VertexesInFrameCount; |
| 334 |
V := PMd3Vertex(Vertexes.Ptr(VertexesInFrameCount * FrameNumber)); |
| 335 |
for I := 0 to VertexesInFrameCount - 1 do |
| 336 |
begin |
| 337 |
Result.FdPoint.Items.L[I] := Vector3( |
| 338 |
V^.Position[0] * Md3XyzScale, |
| 339 |
V^.Position[1] * Md3XyzScale, |
| 340 |
V^.Position[2] * Md3XyzScale); |
| 341 |
Inc(V); |
| 342 |
end; |
| 343 |
|
| 344 |
SceneBox.Include(CalculateBoundingBox(Result.FdPoint.Items)); |
| 345 |
end; |
| 346 |
|
| 347 |
function MakeTextureCoordinates( |
| 348 |
TextureCoords: TMd3TexCoordList): TTextureCoordinateNode; |
| 349 |
var |
| 350 |
I: Integer; |
| 351 |
V: PVector2; |
| 352 |
begin |
| 353 |
Result := TTextureCoordinateNode.Create('', BaseUrl); |
| 354 |
Result.FdPoint.Items.Count := TextureCoords.Count; |
| 355 |
V := PVector2(TextureCoords.L); |
| 356 |
for I := 0 to TextureCoords.Count - 1 do |
| 357 |
begin |
| 358 |
Result.FdPoint.Items.L[I] := Vector2(V^[0], 1-V^[1]); |
| 359 |
Inc(V); |
| 360 |
end; |
| 361 |
end; |
| 362 |
|
| 363 |
function MakeIndexes(Triangles: TMd3TriangleList): TIndexedFaceSetNode; |
| 364 |
var |
| 365 |
I: Integer; |
| 366 |
begin |
| 367 |
Result := TIndexedFaceSetNode.Create('', BaseUrl); |
| 368 |
Result.FdCreaseAngle.Value := NiceCreaseAngle; |
| 369 |
Result.FdSolid.Value := false; |
| 370 |
Result.FdCoordIndex.Items.Count := Triangles.Count * 4; |
| 371 |
Result.FdTexCoordIndex.Items.Count := Triangles.Count * 4; |
| 372 |
for I := 0 to Triangles.Count - 1 do |
| 373 |
begin |
| 374 |
Result.FdCoordIndex.Items.L[I*4 + 0] := Triangles.L[I].Indexes[0]; |
| 375 |
Result.FdCoordIndex.Items.L[I*4 + 1] := Triangles.L[I].Indexes[1]; |
| 376 |
Result.FdCoordIndex.Items.L[I*4 + 2] := Triangles.L[I].Indexes[2]; |
| 377 |
Result.FdCoordIndex.Items.L[I*4 + 3] := -1; |
| 378 |
|
| 379 |
Result.FdTexCoordIndex.Items.L[I*4 + 0] := Triangles.L[I].Indexes[0]; |
| 380 |
Result.FdTexCoordIndex.Items.L[I*4 + 1] := Triangles.L[I].Indexes[1]; |
| 381 |
Result.FdTexCoordIndex.Items.L[I*4 + 2] := Triangles.L[I].Indexes[2]; |
| 382 |
Result.FdTexCoordIndex.Items.L[I*4 + 3] := -1; |
| 383 |
end; |
| 384 |
end; |
| 385 |
|
| 386 |
{ First value in TStringList key-value pairs, that is not ''. |
| 387 |
|
| 388 |
Used to get first texture specified in .skin, as a fallback (Tremulous creatures use it). |
| 389 |
|
| 390 |
Moreover, omit cases where texture is '' (Tremulous skins like |
| 391 |
models/players/human_bsuit/upper_default.skin depend on it, as they start with tags: |
| 392 |
|
| 393 |
tag_head, |
| 394 |
tag_weapon, |
| 395 |
u_torso,models/players/human_bsuit/human_bsuit.jpg |
| 396 |
tag_torso, |
| 397 |
|
| 398 |
). } |
| 399 |
function FirstNonEmptyValue(const StrList: TStringList): String; |
| 400 |
var |
| 401 |
I: Integer; |
| 402 |
begin |
| 403 |
for I := 0 to StrList.Count - 1 do |
| 404 |
begin |
| 405 |
Result := StrList.ValueFromIndex[I]; |
| 406 |
if Result <> '' then |
| 407 |
Exit; |
| 408 |
end; |
| 409 |
|
| 410 |
Result := ''; |
| 411 |
end; |
| 412 |
|
| 413 |
function MakeAppearance(const ShaderName: String): TAppearanceNode; |
| 414 |
var |
| 415 |
TextureUrl: String; |
| 416 |
TextureNode: TImageTextureNode; |
| 417 |
Material: TMaterialNode; |
| 418 |
begin |
| 419 |
TextureUrl := Skin.Values[ShaderName]; |
| 420 |
{ If .skin file was not found, or was empty, or ShaderName isn't found there... } |
| 421 |
if TextureUrl = '' then |
| 422 |
TextureUrl := ShaderName; |
| 423 |
{ Use first non-empty texture specified in .skin, as a fallback. } |
| 424 |
if TextureUrl = '' then |
| 425 |
TextureUrl := FirstNonEmptyValue(Skin); |
| 426 |
{ Use basename of MD3 for texture basename. } |
| 427 |
if TextureUrl = '' then |
| 428 |
TextureUrl := DeleteURIExt(ExtractURIName(BaseUrl)); |
| 429 |
|
| 430 |
if not Appearances.TryGetValue(TextureUrl, Result) then |
| 431 |
begin |
| 432 |
Result := TAppearanceNode.Create(TextureUrl, BaseUrl); |
| 433 |
Appearances.Add(TextureUrl, Result); |
| 434 |
|
| 435 |
Material := TMaterialNode.Create(TextureUrl, BaseUrl); |
| 436 |
Result.Material := Material; |
| 437 |
|
| 438 |
if TextureUrl = 'none' then |
| 439 |
begin |
| 440 |
{ In this case, we explicitly don't want to use a texture, |
| 441 |
so leave Material.DiffuseTexture = nil. } |
| 442 |
end else |
| 443 |
begin |
| 444 |
TextureUrl := FixTextureUrl(TextureUrl, ExtractURIPath(BaseUrl)); |
| 445 |
|
| 446 |
TextureNode := TImageTextureNode.Create('', BaseUrl); |
| 447 |
TextureNode.SetUrl([TextureUrl]); |
| 448 |
Material.DiffuseTexture := TextureNode; |
| 449 |
end; |
| 450 |
end; |
| 451 |
end; |
| 452 |
|
| 453 |
function MakeShape(Surface: TMd3Surface): TShapeNode; |
| 454 |
var |
| 455 |
IFS: TIndexedFaceSetNode; |
| 456 |
begin |
| 457 |
IFS := MakeIndexes(Surface.Triangles); |
| 458 |
IFS.Coord := MakeCoordinates(Surface.Vertexes, Surface.VertexesInFrameCount); |
| 459 |
IFS.TexCoord := MakeTextureCoordinates(Surface.TextureCoords); |
| 460 |
|
| 461 |
Result := TShapeNode.Create(Surface.Name, BaseUrl); |
| 462 |
Result.Geometry := IFS; |
| 463 |
Result.Appearance := MakeAppearance(Surface.ShaderName); |
| 464 |
end; |
| 465 |
|
| 466 |
var |
| 467 |
I: Integer; |
| 468 |
begin |
| 469 |
Result := TGroupNode.Create('', BaseUrl); |
| 470 |
|
| 471 |
for I := 0 to Surfaces.Count - 1 do |
| 472 |
Result.AddChildren(MakeShape(Surfaces[I])); |
| 473 |
end; |
| 474 |
|
| 475 |
function TMD3Converter.ToX3D: TX3DRootNode; |
| 476 |
var |
| 477 |
Switch: TSwitchNode; |
| 478 |
TagTransforms: TX3DNodeList; // list of TTransformNode, once for each MD3 tag |
| 479 |
|
| 480 |
{ Create TTransformNode corresponding to tag. |
| 481 |
Add it to Result, set TagTransforms[TagIndex]. } |
| 482 |
procedure AddTag(const TagIndex: Cardinal); |
| 483 |
var |
| 484 |
Tag: PMd3Tag; |
| 485 |
TagTransform: TTransformNode; |
| 486 |
TagName: String; |
| 487 |
{.$define CASTLE_MD3_VISUALIZE_TAG} |
| 488 |
{$ifdef CASTLE_MD3_VISUALIZE_TAG} |
| 489 |
Cone: TConeNode; |
| 490 |
ConeShape: TShapeNode; |
| 491 |
{$endif} |
| 492 |
begin |
| 493 |
// tags do not exist when NumFrames = 0, regardless of NumTags |
| 494 |
Assert(NumFrames <> 0); |
| 495 |
Assert(Tags.Count <> 0); |
| 496 |
|
| 497 |
Tag := PMd3Tag(Tags.Ptr({NumTags * 0 + } TagIndex)); |
| 498 |
TagName := Tag^.Name; |
| 499 |
TagTransform := TTransformNode.Create(TagName); |
| 500 |
TagTransforms.Add(TagTransform); |
| 501 |
Result.AddChildren(TagTransform); |
| 502 |
|
| 503 |
{$ifdef CASTLE_MD3_VISUALIZE_TAG} |
| 504 |
Cone := TConeNode.CreateWithShape(ConeShape); |
| 505 |
TagTransform.AddChildren(ConeShape); |
| 506 |
{$endif} |
| 507 |
end; |
| 508 |
|
| 509 |
procedure AddTagAnimation(const TagIndex: Cardinal; |
| 510 |
const AnimationTimeSensor: TTimeSensorNode; |
| 511 |
const FirstFrame, NumFramesInAnimation: Cardinal); |
| 512 |
var |
| 513 |
FrameIndex, I: Integer; |
| 514 |
TagName: String; |
| 515 |
Tag: PMd3Tag; |
| 516 |
TagTransform: TTransformNode; |
| 517 |
OriginInterpolator: TPositionInterpolatorNode; |
| 518 |
OrientationInterpolator: TOrientationInterpolatorNode; |
| 519 |
begin |
| 520 |
// tags do not exist when NumFrames = 0, regardless of NumTags |
| 521 |
Assert(NumFrames <> 0); |
| 522 |
Assert(Tags.Count <> 0); |
| 523 |
|
| 524 |
TagTransform := TagTransforms[TagIndex] as TTransformNode; |
| 525 |
TagName := TagTransform.X3DName; |
| 526 |
|
| 527 |
OriginInterpolator := TPositionInterpolatorNode.Create( |
| 528 |
TagName + '_' + AnimationTimeSensor.X3DName + '_Origin'); |
| 529 |
OriginInterpolator.FdKey.Count := NumFramesInAnimation; |
| 530 |
OriginInterpolator.FdKeyValue.Count := NumFramesInAnimation; |
| 531 |
Result.AddChildren(OriginInterpolator); |
| 532 |
Result.AddRoute(AnimationTimeSensor.EventFraction_changed, OriginInterpolator.EventSet_fraction); |
| 533 |
Result.AddRoute(OriginInterpolator.EventValue_changed, TagTransform.FdTranslation); |
| 534 |
|
| 535 |
OrientationInterpolator := TOrientationInterpolatorNode.Create( |
| 536 |
TagName + '_' + AnimationTimeSensor.X3DName + '_Orientation'); |
| 537 |
OrientationInterpolator.KeyValueQuaternions := true; |
| 538 |
OrientationInterpolator.FdKey.Count := NumFramesInAnimation; |
| 539 |
OrientationInterpolator.FdKeyValue.Count := NumFramesInAnimation; |
| 540 |
Result.AddChildren(OrientationInterpolator); |
| 541 |
Result.AddRoute(AnimationTimeSensor.EventFraction_changed, OrientationInterpolator.EventSet_fraction); |
| 542 |
Result.AddRoute(OrientationInterpolator.EventValue_changed, TagTransform.FdRotation); |
| 543 |
|
| 544 |
for I := 0 to NumFramesInAnimation - 1 do |
| 545 |
begin |
| 546 |
FrameIndex := FirstFrame + I; |
| 547 |
Tag := PMd3Tag(Tags.Ptr(NumTags * FrameIndex + TagIndex)); |
| 548 |
if TagName <> Tag^.Name then |
| 549 |
WritelnWarning('Inconsistent MD3 tag name, changes between frames: "%s", (frame %d) "%s"', [ |
| 550 |
TagName, |
| 551 |
FrameIndex, |
| 552 |
Tag^.Name |
| 553 |
]); |
| 554 |
|
| 555 |
OriginInterpolator.FdKey.Items[I] := I / NumFramesInAnimation; |
| 556 |
OriginInterpolator.FdKeyValue.Items[I] := Tag^.Origin; |
| 557 |
OrientationInterpolator.FdKey.Items[I] := I / NumFramesInAnimation; |
| 558 |
OrientationInterpolator.FdKeyValue.Items[I] := QuatFromRotationMatrix(Tag^.Orientation).Data.Vector4; |
| 559 |
end; |
| 560 |
end; |
| 561 |
|
| 562 |
{ Add to Result an animation that drives Switch.WhichChoice, |
| 563 |
changing frames from FirstFrame to FirstFrame + NumFramesInAnimation - 1. } |
| 564 |
procedure AddAnimation(const AnimationName: String; |
| 565 |
const FirstFrame, NumFramesInAnimation, Fps: Cardinal); |
| 566 |
var |
| 567 |
TimeSensor: TTimeSensorNode; |
| 568 |
IntSequencer: TIntegerSequencerNode; |
| 569 |
I: Integer; |
| 570 |
begin |
| 571 |
TimeSensor := TTimeSensorNode.Create(AnimationName, BaseUrl); |
| 572 |
TimeSensor.CycleInterval := NumFramesInAnimation / Fps; |
| 573 |
Result.AddChildren(TimeSensor); |
| 574 |
|
| 575 |
IntSequencer := TIntegerSequencerNode.Create(AnimationName + '_IntegerSequencer', BaseUrl); |
| 576 |
{ Using ForceContinuousValue_Changed for the same reason why |
| 577 |
castleinternalnodeinterpolator.pas . |
| 578 |
See https://castle-engine.io/x3d_implementation_eventutilities_extensions.php#section_ext_forceContinuousValue_Changed . |
| 579 |
As multiple IntegerSequencer may affect this Switch (once we add support |
| 580 |
for other animations) this is important. } |
| 581 |
IntSequencer.ForceContinuousValue_Changed := true; |
| 582 |
IntSequencer.FdKey.Count := NumFramesInAnimation; |
| 583 |
IntSequencer.FdKeyValue.Count := NumFramesInAnimation; |
| 584 |
for I := 0 to Integer(NumFramesInAnimation) - 1 do |
| 585 |
begin |
| 586 |
IntSequencer.FdKey.Items[I] := I / NumFramesInAnimation; |
| 587 |
IntSequencer.FdKeyValue.Items[I] := FirstFrame + I; |
| 588 |
end; |
| 589 |
Result.AddChildren(IntSequencer); |
| 590 |
|
| 591 |
Result.AddRoute(TimeSensor.EventFraction_changed, IntSequencer.EventSet_fraction); |
| 592 |
Result.AddRoute(IntSequencer.EventValue_changed, Switch.FdWhichChoice); |
| 593 |
Result.ExportNode(TimeSensor); |
| 594 |
|
| 595 |
for I := 0 to NumTags - 1 do |
| 596 |
AddTagAnimation(I, TimeSensor, FirstFrame, NumFramesInAnimation); |
| 597 |
end; |
| 598 |
|
| 599 |
var |
| 600 |
I: Integer; |
| 601 |
SceneBox: TBox3D; |
| 602 |
Animations: TMd3Animations; |
| 603 |
AnimationCfgUrl: String; |
| 604 |
begin |
| 605 |
Result := TX3DRootNode.Create(Name, BaseUrl); |
| 606 |
Result.HasForceVersion := true; |
| 607 |
Result.ForceVersion := X3DVersion; |
| 608 |
|
| 609 |
SceneBox := TBox3D.Empty; |
| 610 |
|
| 611 |
Switch := TSwitchNode.Create('', BaseUrl); |
| 612 |
Switch.WhichChoice := 0; // show something non-empty on load |
| 613 |
for I := 0 to NumFrames - 1 do |
| 614 |
Switch.AddChildren(FrameToX3D(I, SceneBox)); |
| 615 |
Result.AddChildren(Switch); |
| 616 |
|
| 617 |
TagTransforms := TX3DNodeList.Create(false); |
| 618 |
try |
| 619 |
for I := 0 to NumTags - 1 do |
| 620 |
AddTag(I); |
| 621 |
Assert(TagTransforms.Count = NumTags); |
| 622 |
|
| 623 |
AddAnimation(TNodeInterpolator.DefaultAnimationName, 0, NumFrames, DefaultMd3Fps); |
| 624 |
|
| 625 |
AnimationCfgUrl := ExtractURIPath(BaseUrl) + 'animation.cfg'; |
| 626 |
if URIFileExists(AnimationCfgUrl) then |
| 627 |
begin |
| 628 |
Animations := TMd3Animations.Create; |
| 629 |
try |
| 630 |
Animations.ReadAnimationCfg(AnimationCfgUrl); |
| 631 |
Animations.ValidateFrames(NumFrames); |
| 632 |
for I := 0 to Animations.Count - 1 do |
| 633 |
AddAnimation(Animations[I].Name, Animations[I].FirstFrame, Abs(Animations[I].NumFrames), |
| 634 |
Animations[I].Fps); |
| 635 |
finally FreeAndNil(Animations) end; |
| 636 |
end; |
| 637 |
finally FreeAndNil(TagTransforms) end; |
| 638 |
|
| 639 |
{ MD3 files have no camera. I add camera here, just to force GravityUp |
| 640 |
to be in +Z, since this is the convention used in all MD3 files that |
| 641 |
I saw (so I guess that Quake3 engine generally uses this convention). } |
| 642 |
Result.AddChildren(CameraNodeForWholeScene(cvVrml2_X3d, |
| 643 |
BaseUrl, SceneBox, 0, 2, false, true)); |
| 644 |
end; |