Debian Codegraph @

src/scene/load/spine/x3dloadinternalspine_simpletextureloader.inc

raw
1 {%MainUnit x3dloadinternalspine.pas}
2 {
3 Copyright 2014-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 type
18 { Texture nodes, indexed by attachment name. }
19 TImageTextureNodeMap = {$ifdef FPC}specialize{$endif} TDictionary<string, TImageTextureNode>;
20
21 { Spine simple texture loader (without atlas).
22 Reads each region attachment as a single texture file,
23 from a filename 'images/' + AttachmentName + '.png'. }
24 TSimpleTextureLoader = class(TTextureLoader)
25 strict private
26 Nodes: TImageTextureNodeMap;
27 BaseUrl: String;
28 public
29 constructor Create(const ABaseUrl: String);
30 destructor Destroy; override;
31 function UseNode(const AttachmentName: string;
32 out TexCoord: TQuadTexCoord; out TexRect: TQuadTexRect;
33 out TexRotated: boolean): TImageTextureNode; override;
34 end;
35
36 constructor TSimpleTextureLoader.Create(const ABaseUrl: String);
37 begin
38 inherited Create;
39 BaseUrl := ABaseUrl;
40 Nodes := TImageTextureNodeMap.Create;
41 end;
42
43 destructor TSimpleTextureLoader.Destroy;
44 begin
45 FreeAndNil(Nodes);
46 inherited;
47 end;
48
49 procedure AddSpineTextureProperties(const Node: TImageTextureNode; const IsRepeat: Boolean);
50 var
51 TexProperties: TTexturePropertiesNode;
52 begin
53 { No point in adjusting RepeatS/T: TextureProperties overrides it with BoundaryModeS/T.
54 Node.RepeatS := IsRepeat;
55 Node.RepeatT := IsRepeat;
56 }
57
58 TexProperties := TTexturePropertiesNode.Create;
59 // TexProperties.MagnificationFilter := magDefault; // this is default now
60 // TexProperties.MinificationFilter := minDefault; // this is default now
61 TexProperties.BoundaryModeS := BoolRepeatToBoundaryMode[IsRepeat];
62 TexProperties.BoundaryModeT := BoolRepeatToBoundaryMode[IsRepeat];
63 { Do not force "power of 2" size, which may prevent mipmaps.
64 Not resizing is consistent with X3DLoadInternalImage, LoadCastleSpriteSheet,
65 Tiled maps. Users may use Spine with pixel-art and don't expect images to be resized.
66 See https://github.com/castle-engine/castle-engine/issues/464 . }
67 TexProperties.GuiTexture := true;
68 Node.TextureProperties := TexProperties;
69 end;
70
71 function TSimpleTextureLoader.UseNode(const AttachmentName: string;
72 out TexCoord: TQuadTexCoord; out TexRect: TQuadTexRect;
73 out TexRotated: boolean): TImageTextureNode;
74 begin
75 if not Nodes.TryGetValue(AttachmentName, Result) then
76 begin
77 Result := TImageTextureNode.Create('SimpleTexture_' + AttachmentName, BaseUrl);
78 if not SpineIgnoreTextures then
79 Result.SetUrl(['images/' + AttachmentName + '.png']);
80 AddSpineTextureProperties(Result, false);
81
82 Nodes.AddOrSetValue(AttachmentName, Result);
83 end;
84
85 TexCoord[0] := Vector2(0, 0);
86 TexCoord[1] := Vector2(1, 0);
87 TexCoord[2] := Vector2(1, 1);
88 TexCoord[3] := Vector2(0, 1);
89
90 TexRect[0] := Vector2(0, 0);
91 TexRect[1] := Vector2(1, 1);
92
93 TexRotated := false;
94 end;