Debian Codegraph @

examples/space_shooter/code/gametilingbackground.pas

raw
1 {
2 Copyright 2019-2024 Michalis Kamburelis.
3
4 This file is part of "Castle Game Engine".
5
6 "Castle Game Engine" is free software; see the file COPYING.txt,
7 included in this distribution, for details about the copyright.
8
9 "Castle Game Engine" is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 ----------------------------------------------------------------------------
14 }
15
16 { TCastleTransform component that draws a tiling (repeatable) texture. }
17 unit GameTilingBackground;
18
19 interface
20
21 uses Classes,
22 CastleVectors, CastleTransform, CastleScene, X3DNodes, CastleViewport;
23
24 type
25 { Draw a tiling (repeatable) texture.
26
27 This is very efficient -- always draws only 1 quad,
28 uses texture coordinats to repeat the texturein both directions as needed. }
29 TTilingBackground = class(TCastleScene)
30 strict private
31 Coordinate: TCoordinateNode;
32 TextureCoordinate: TTextureCoordinateNode;
33 Texture: TImageTextureNode;
34 public
35 { Image position visible at the viewport's left-bottom corner.
36 Change this to effectively move the image.
37 Always call UpdateCoordinates after changing this. }
38 ImageOrigin: TVector2Double;
39
40 constructor Create(AOwner: TComponent); override;
41
42 { Call this always when ImageOrigin or viewport size changes.
43 Adjusts the coordinates (position of the quad in viewport)
44 and texture coordinates (how the image is mapped onto a surface). }
45 procedure UpdateCoordinates(const ParentViewport: TCastleViewport);
46 end;
47
48 implementation
49
50 uses CastleRectangles;
51
52 constructor TTilingBackground.Create(AOwner: TComponent);
53
54 { Create nodes graph with a textured rectangle. }
55 function BuildRootNode: TX3DRootNode;
56 var
57 Shape: TShapeNode;
58 Geometry: TIndexedFaceSetNode;
59 begin
60 { Create ImageTexture node (represents the texture from file) }
61 Texture := TImageTextureNode.Create;
62 Texture.SetUrl(['castle-data:/space/goldSpace.png']);
63 // Set RepeatS, RepeatT to true, to repeat the texture.
64 Texture.RepeatS := true;
65 Texture.RepeatT := true;
66
67 { Create Coordinate node (position of quad in 3D) }
68 Coordinate := TCoordinateNode.Create;
69 Coordinate.SetPoint([
70 { Actual value of these vectors doesn't mater, they will be updated
71 in each UpdateCoordinates.
72 It is only important to provide 4 points here, to avoid warnings
73 at creation: SetCoordIndex below will refer to these points. }
74 Vector3(0 , 0, 0),
75 Vector3(100, 0, 0),
76 Vector3(100, 100, 0),
77 Vector3(0 , 100, 0)
78 ]);
79
80 { Create TextureCoordinate node (how the image is mapped onto a surface) }
81 TextureCoordinate := TTextureCoordinateNode.Create;
82 TextureCoordinate.SetPoint([
83 { Actual value of these vectors doesn't mater, they will be updated
84 in each UpdateCoordinates.
85 It is only important to provide 4 points here, to avoid warnings
86 at creation: SetCoordIndex below will refer to these points. }
87 Vector2(0, 0),
88 Vector2(10, 0),
89 Vector2(10, 10),
90 Vector2(0, 10)
91 ]);
92
93 { Create Shape and IndexedFaceSet node (mesh with coordinates, texture coordinates) }
94 Geometry := TIndexedFaceSetNode.Create;
95 Geometry.Coord := Coordinate;
96 Geometry.TexCoord := TextureCoordinate;
97 Geometry.SetCoordIndex([0, 1, 2, 3]);
98
99 Shape := TShapeNode.Create;
100
101 { Create Appearance (refers to a texture, connects the Texture to Shape) }
102 Shape.Appearance := TAppearanceNode.Create;
103 Shape.Appearance.Texture := Texture;
104
105 Shape.Geometry := Geometry;
106
107 Result := TX3DRootNode.Create;
108 Result.AddChildren(Shape);
109 end;
110
111 begin
112 inherited;
113 Load(BuildRootNode, true);
114 end;
115
116 procedure TTilingBackground.UpdateCoordinates(const ParentViewport: TCastleViewport);
117 var
118 TextureSizeX, TextureSizeY: Single;
119 VisibleRect: TFloatRectangle;
120 ImageOriginFrac: TVector2;
121 begin
122 VisibleRect := ParentViewport.Camera.Orthographic.EffectiveRect;
123 { update Coordinate to make the shape fill entire viewport exactly }
124 Coordinate.SetPoint([
125 Vector3(VisibleRect.Left , VisibleRect.Bottom, 0),
126 Vector3(VisibleRect.Right, VisibleRect.Bottom, 0),
127 Vector3(VisibleRect.Right, VisibleRect.Top , 0),
128 Vector3(VisibleRect.Left , VisibleRect.Top , 0)
129 ]);
130
131 { Texture coordinates are expressed in the range 0..1 }
132 TextureSizeX := VisibleRect.Width / Texture.TextureImage.Width;
133 TextureSizeY := VisibleRect.Height / Texture.TextureImage.Height;
134
135 { Pass to rendering only the fractional part of ImageOrigin.
136 Only the fractional part is relevant,
137 and this way we avoid passing huge floating point values (after
138 a few minutes of gameplay) to GPU and losing precision, resulting
139 in background "stuttering" visually. }
140 ImageOriginFrac.X := Frac(ImageOrigin.X);
141 ImageOriginFrac.Y := Frac(ImageOrigin.Y);
142
143 { update TextureCoordinate to adjust the amount of "repeat" based on control size
144 on the screen (VisibleRect.Width/Height), and apply ImageOrigin. }
145 TextureCoordinate.SetPoint([
146 ImageOriginFrac + Vector2( 0, 0),
147 ImageOriginFrac + Vector2(TextureSizeX, 0),
148 ImageOriginFrac + Vector2(TextureSizeX, TextureSizeY),
149 ImageOriginFrac + Vector2( 0, TextureSizeY)
150 ]);
151 end;
152
153 end.