Debian Codegraph @

src/scene/castlescene_plane.inc

raw
1 {%MainUnit castlescene.pas}
2 {
3 Copyright 2021-2022 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 {$ifdef read_interface}
18
19 type
20 { Plane with configurable size, position and material.
21
22 The plane is actually just a rectangle with specified @link(Size).
23 It is centered around the point (0,0,0).
24
25 The plane is parallel to the given @link(Axis), by default the Axis is 1 (Y)
26 -- which means that the plane acts like a floor in a typical 3D setup.
27 If you want a rectangle visible in typical 2D setup (where X goes to the right,
28 Y goes up), change the @link(Axis) to 2 (Z).
29
30 You can translate, rotate, scale the plane freely, as it is a TCastleTransform,
31 and can be a child of other TCastleTransform instances too. }
32 TCastlePlane = class(TCastleAbstractPrimitive)
33 strict private
34 FQuadSetNode: TQuadSetNode;
35 FCoordinateNode: TCoordinateNode;
36 FSize: TVector2;
37 FAxis: T3DAxis;
38 FDoubleSided: Boolean;
39 procedure SetSize(const Value: TVector2);
40 procedure SetAxis(const Value: T3DAxis);
41 { Update FCoordinateNode based on FSize, FAxis }
42 procedure UpdateCoordinateNode;
43 procedure SetDoubleSided(const Value: Boolean);
44 public
45 constructor Create(AOwner: TComponent); override;
46 destructor Destroy; override;
47 function PropertySections(const PropertyName: String): TPropertySections; override;
48
49 { Size of the plane. By default (2, 2).
50
51 The coordinates affected by this property depend on current @link(Axis) value.
52 That is:
53
54 @unorderedList(
55 @item Axis = 0 means that this property determines the size in Y and Z (in this order).
56 @item Axis = 1 means that this property determines the size in X and Z (in this order).
57 @item Axis = 2 means that this property determines the size in X and Y (in this order).
58 )
59 }
60 property Size: TVector2 read FSize write SetSize;
61 published
62 { Axis where the plane is constant.
63 That is:
64
65 @unorderedList(
66 @item Axis = 0 means the plane is in YZ (at constant X = 0).
67 @item Axis = 1 means the plane is in XZ (at constant Y = 0). In particular useful for floors in 3D games.
68 @item Axis = 2 means the plane is in XY (at constant Z = 0). In particular useful for rectangles in 2D games.
69 )
70 }
71 property Axis: T3DAxis read FAxis write SetAxis default 1;
72
73 { When this is @true, the object is visible (and lit) from both sides. }
74 property DoubleSided: Boolean read FDoubleSided write SetDoubleSided default false;
75
76 {$define read_interface_class}
77 {$I auto_generated_persistent_vectors/tcastleplane_persistent_vectors.inc}
78 {$undef read_interface_class}
79 end;
80
81 {$endif read_interface}
82
83 {$ifdef read_implementation}
84
85 { TCastlePlane ---------------------------------------------------------------- }
86
87 constructor TCastlePlane.Create(AOwner: TComponent);
88 begin
89 inherited;
90
91 { default properties values }
92 FSize := Vector2(2, 2);
93 FAxis := 1;
94
95 FCoordinateNode := TCoordinateNode.Create;
96 UpdateCoordinateNode;
97
98 FQuadSetNode := TQuadSetNode.Create;
99 FQuadSetNode.Coord := FCoordinateNode;
100 ShapeNode.Geometry := FQuadSetNode;
101
102 {$define read_implementation_constructor}
103 {$I auto_generated_persistent_vectors/tcastleplane_persistent_vectors.inc}
104 {$undef read_implementation_constructor}
105 end;
106
107 destructor TCastlePlane.Destroy;
108 begin
109 {$define read_implementation_destructor}
110 {$I auto_generated_persistent_vectors/tcastleplane_persistent_vectors.inc}
111 {$undef read_implementation_destructor}
112
113 inherited;
114 end;
115
116 procedure TCastlePlane.UpdateCoordinateNode;
117 var
118 V: array [0..3] of TVector2;
119 V3D: array [0..3] of TVector3;
120 I: Integer;
121 C1, C2: T3DAxis;
122 Size2: TVector2;
123 begin
124 Size2 := FSize * 0.5;
125 V[0] := Vector2(-Size2.X, -Size2.Y);
126 V[1] := Vector2( Size2.X, -Size2.Y);
127 V[2] := Vector2( Size2.X, Size2.Y);
128 V[3] := Vector2(-Size2.X, Size2.Y);
129
130 RestOf3DCoordsCycle(FAxis, C1, C2);
131 for I := Low(V) to High(V) do
132 begin
133 {$warnings off} // silence FPC warning about V3D uninitialized
134 V3D[I].Data[FAxis] := 0;
135 {$warnings on}
136 V3D[I].Data[C1] := V[I].X;
137 V3D[I].Data[C2] := V[I].Y;
138 end;
139
140 FCoordinateNode.SetPoint(V3D);
141 end;
142
143 function TCastlePlane.PropertySections(const PropertyName: String): TPropertySections;
144 begin
145 if (PropertyName = 'SizePersistent') or
146 (PropertyName = 'Axis') or
147 (PropertyName = 'DoubleSided') then
148 Result := [psBasic]
149 else
150 Result := inherited PropertySections(PropertyName);
151 end;
152
153 procedure TCastlePlane.SetSize(const Value: TVector2);
154 begin
155 if not TVector2.PerfectlyEquals(FSize, Value) then
156 begin
157 FSize := Value;
158 UpdateCoordinateNode;
159 end;
160 end;
161
162 procedure TCastlePlane.SetAxis(const Value: T3DAxis);
163 begin
164 if FAxis <> Value then
165 begin
166 FAxis := Value;
167 UpdateCoordinateNode;
168 UpdateCollider;
169 end;
170 end;
171
172 procedure TCastlePlane.SetDoubleSided(const Value: Boolean);
173 begin
174 if FDoubleSided <> Value then
175 begin
176 FDoubleSided := Value;
177 FQuadSetNode.Solid := not Value;
178 end;
179 end;
180
181 {$define read_implementation_methods}
182 {$I auto_generated_persistent_vectors/tcastleplane_persistent_vectors.inc}
183 {$undef read_implementation_methods}
184
185 {$endif read_implementation}