Debian Codegraph @

src/transform/castlebehaviors_billboard.inc

raw
1 {%MainUnit castlebehaviors.pas}
2 {
3 Copyright 2021-2024 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 { Behavior to make parent TCastleTransform a billboard, that always rotates
21 to face the current camera.
22
23 The front of the parent, that always turns to the camera,
24 is determined by the @link(TCastleTransform.Orientation),
25 which in turn by default is taken from @link(TCastleTransform.DefaultOrientation),
26 which by default just means "positive Z axis" (otUpYDirectionZ).
27 The idea is that @link(TCastleTransform.Direction) is updated
28 to point toward the camera.
29
30 The axis around which billboard rotates is determined by @link(AxisOfRotation).
31 When non-zero, this axis is set as @link(TCastleTransform.Up). }
32 TCastleBillboard = class(TCastleBehavior)
33 strict private
34 FAxisOfRotation: TVector3;
35 FMatchCameraDirection: Boolean;
36 protected
37 function CanAttachToParent(const NewParent: TCastleTransform;
38 out ReasonWhyCannot: String): Boolean; override;
39 public
40 constructor Create(AOwner: TComponent); override;
41 destructor Destroy; override;
42 procedure BeforeRender(
43 const CameraView: TViewVectors;
44 const ParentParentWorldTransformation, ParentWorldTransformation: TTransformation); override;
45 function PropertySections(const PropertyName: String): TPropertySections; override;
46
47 { Axis around which the billboard rotates to face the camera.
48 The rotation is free in 3D (with undefined up vector) when this is zero.
49
50 When non-zero, this axis is set as @link(TCastleTransform.Up).
51
52 Similar to X3D @link(TBillboardNode.AxisOfRotation).
53 By default +Y (that is, (0, 1, 0)).
54
55 Note that this axis is expressed in the coordinate system
56 of "parent of our parent" transform.
57 That is, if you have instance X of a TCastleTransform, with TCastleBehavior attached,
58 and it is a child of TCastleTransform instance Y,
59 then you can rotate Y and it effectively rotates also the billboard's
60 axis of rotation. The billboard still works correctly (it will rotate the X
61 to look at camera as much as possible, though it cannot change rotation on Y).
62
63 See @link(MatchCameraDirection) for details how this axis affects
64 object transformation. }
65 property AxisOfRotation: TVector3 read FAxisOfRotation write FAxisOfRotation;
66
67 published
68 { Whether the billboard direction should match the camera direction
69 (@true, default) or direction from camera to the billboard origin (@false).
70
71 The difference between these 2 settings is subtle.
72
73 @unorderedList(
74 @item(
75 When @true, all billboards in the scene have the same orientation
76 (if they have the same @link(AxisOfRotation)).
77 They just follow camera orientation.
78 Billboard orientation is unrelated to billboard position,
79 and unrelated to camera position.
80
81 Strafing (moving camera) left/right doesn't change the billboard orientation.
82 Moving billboard left/right doesn't change the billboard orientation.
83
84 This may look better or worse -- really depends on what you want to achieve.
85
86 The major advantage of this approach (and the reason why it is the default)
87 is that blending sorting for objects with thin layers, like on Spine models,
88 can be made perfectly reliable, even in 3D.
89
90 @unorderedList(
91 @item(
92 When @link(AxisOfRotation) is zero (on all billboards) then
93 @link(TCastleViewport.BlendingSort) set to @link(sort3D)
94 will be perfect. It will work even
95 if camera can make free rotations (downward and upward) in 3D.
96
97 @link(sort3D) is also a good choice if @link(AxisOfRotation) is non-zero,
98 but your camera "up" is locked (you don't let player to rotate downward or upward)
99 to the same direction.
100 )
101
102 @item(
103 When @link(AxisOfRotation) is +Y (0,1,0) (on all billboards) then
104 @link(TCastleViewport.BlendingSort) set to @link(sort3DVerticalBillboards)
105 will be perfect. It will work even
106 if camera can make free rotations (downward and upward) in 3D.
107 )
108
109 @item(
110 Many other possibilities can be accounted for using @link(sortCustom)
111 and @link(TCastleViewport.OnCustomShapeSort) event.
112 If only your @link(AxisOfRotation) is consistent for all billboards,
113 you can implement something similar to @link(sort3DVerticalBillboards)
114 to account for given up vector.
115 )
116 )
117 )
118
119 @item(
120 When @false, billboard orientation adjusts to the direction
121 from camera to the origin of the billboard transformation.
122 This means that the billboard position affects the billboard orientation,
123 e.g. billboards on the edge of the field of view
124 will have a different orientation than in the middle of the view.
125
126 This is consistent with X3D Billboard,
127 https://www.web3d.org/documents/specifications/19775-1/V4.0/Part01/components/navigation.html#Billboard .
128 )
129 )
130 }
131 property MatchCameraDirection: Boolean
132 read FMatchCameraDirection write FMatchCameraDirection default true;
133
134 {$define read_interface_class}
135 {$I auto_generated_persistent_vectors/tcastlebillboard_persistent_vectors.inc}
136 {$undef read_interface_class}
137 end;
138
139 {$endif read_interface}
140
141 {$ifdef read_implementation}
142
143 {$define read_implementation_methods}
144 {$I auto_generated_persistent_vectors/tcastlebillboard_persistent_vectors.inc}
145 {$undef read_implementation_methods}
146
147 constructor TCastleBillboard.Create(AOwner: TComponent);
148 begin
149 inherited;
150
151 FMatchCameraDirection := true;
152 FAxisOfRotation := Vector3(0, 1, 0);
153
154 {$define read_implementation_constructor}
155 {$I auto_generated_persistent_vectors/tcastlebillboard_persistent_vectors.inc}
156 {$undef read_implementation_constructor}
157 end;
158
159 destructor TCastleBillboard.Destroy;
160 begin
161 {$define read_implementation_destructor}
162 {$I auto_generated_persistent_vectors/tcastlebillboard_persistent_vectors.inc}
163 {$undef read_implementation_destructor}
164 inherited;
165 end;
166
167 function TCastleBillboard.PropertySections(const PropertyName: String): TPropertySections;
168 begin
169 if ArrayContainsString(PropertyName, [
170 'AxisOfRotationPersistent', 'MatchCameraDirection'
171 ]) then
172 Result := [psBasic]
173 else
174 Result := inherited PropertySections(PropertyName);
175 end;
176
177 procedure TCastleBillboard.BeforeRender(
178 const CameraView: TViewVectors;
179 const ParentParentWorldTransformation, ParentWorldTransformation: TTransformation);
180
181 { Like Parent.Parent.WorldToLocalDirection, but uses current
182 transformation knowledge. }
183 function ParentParentWorldToLocalDirection(const D: TVector3): TVector3;
184 begin
185 Result := ParentParentWorldTransformation.InverseTransform.MultDirection(D);
186 end;
187
188 { Like Parent.Parent.WorldToLocal, but uses current
189 transformation knowledge. }
190 function ParentParentWorldToLocal(const P: TVector3): TVector3;
191 begin
192 Result := ParentParentWorldTransformation.InverseTransform.MultPoint(P);
193 end;
194
195 var
196 DirectionToCamera: TVector3;
197 begin
198 inherited;
199
200 if MatchCameraDirection then
201 DirectionToCamera := -ParentParentWorldToLocalDirection(CameraView.Direction)
202 else
203 begin
204 { The transformation which interests us is the whole Parent.Parent transformation,
205 and the Parent.Translation.
206 We will modify the rotation.
207
208 WorldInverseTransform := TranslationMatrix(-Parent.Translation) *
209 Parent.Parent.WorldInverseTransform;
210
211 // calculate vector from (0, 0, 0) to camera Position, in local coords
212 DirectionToCamera := WorldInverseTransform.MultPoint(CameraView.Translation);
213
214 More optimized version (just revert translation directly,
215 instead of as a matrix): }
216
217 DirectionToCamera := ParentParentWorldToLocal(CameraView.Translation)
218 - Parent.Translation;
219 end;
220
221 Parent.Center := TVector3.Zero; // no other value makes sense for billboard implementation
222
223 if AxisOfRotation.IsPerfectlyZero then
224 begin
225 Parent.SetView(
226 DirectionToCamera,
227 { Note that here we don't care about Parent.Translation,
228 because we only transform direction. }
229 ParentParentWorldToLocalDirection(CameraView.Up)
230 );
231 end else
232 begin
233 Parent.SetView(
234 DirectionToCamera,
235 AxisOfRotation,
236 { adjust direction, keep up constant } false
237 );
238 end;
239 end;
240
241 function TCastleBillboard.CanAttachToParent(const NewParent: TCastleTransform;
242 out ReasonWhyCannot: String): Boolean;
243 begin
244 Result := inherited;
245 if not Result then Exit;
246
247 if NewParent.FindBehavior(TCastleBillboard) <> nil then
248 begin
249 ReasonWhyCannot := 'Only one TCastleBillboard behavior can be added to a given TCastleTransform';
250 Result := false;
251 end;
252 end;
253
254 {$endif read_implementation}