Debian Codegraph @

src/scene/castlescene_imagetransform.inc

raw
1 {%MainUnit castlescene.pas}
2 {
3 Copyright 2022-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 { Image (that you can place within TCastleViewport) with configurable size and repeat.
21 Positioned in XY plane by defalut, so most suitable for 2D games. }
22 TCastleImageTransform = class(TCastleTransform)
23 strict private
24 { scene and X3D nodes }
25 FScene: TCastleScene;
26 FRootNode: TX3DRootNode;
27 FAppearanceNode: TAppearanceNode;
28 FShapeNode: TShapeNode;
29 FMaterialNode: TUnlitMaterialNode;
30 FTextureNode: TImageTextureNode;
31 FTexPropertiesNode: TTexturePropertiesNode;
32 FCoordinateNode: TCoordinateNode;
33 FTexCoordNode: TTextureCoordinateNode;
34 FIndexedTriangleSetNode: TIndexedTriangleSetNode;
35
36 { private fields reflecting public properties }
37 FUrl: String;
38 FPivot, FSize, FRepeatImage, FShift: TVector2;
39 FColor: TCastleColor;
40 FSmoothScaling: Boolean;
41 FAlphaChannel: TAutoAlphaChannel;
42 FMipmaps: Boolean;
43
44 FUrlMonitoring: TUrlMonitoring;
45
46 { Update X3D TCoordinateNode, TTextureCoordinateNode. }
47 procedure UpdateCoordinateNodes;
48 procedure SetPivot(const Value: TVector2);
49 procedure SetSize(const Value: TVector2);
50 procedure SetRepeatImage(const Value: TVector2);
51 procedure SetShift(const Value: TVector2);
52 procedure SetColor(const Value: TCastleColor);
53 procedure SetSmoothScaling(const Value: Boolean);
54 procedure SetAlphaChannel(const Value: TAutoAlphaChannel);
55 procedure SetUrl(const Value: String);
56 procedure SetMipmaps(const Value: Boolean);
57 public
58 constructor Create(AOwner: TComponent); override;
59 destructor Destroy; override;
60 function PropertySections(const PropertyName: String): TPropertySections; override;
61 procedure BeforeDestruction; override;
62
63 { How is the image placed within its own coordinate system.
64 Default value (0.5, 0.5) means that the middle of the image is at (0,0,0).
65 Value (0, 0) means that the left-bottom corner of the image is at (0,0,0). }
66 property Pivot: TVector2 read FPivot write SetPivot;
67
68 { Optionally resize the image.
69 This is equivalent o using TCastleTransform.Scale to scale the image,
70 except you can specify a value in terms of image size (in image pixels).
71
72 @unorderedList(
73 @item(When both X and Y are zero, the image is not resized.)
74
75 @item(When only X is non-zero, the image is resized to given width,
76 preserving the image aspect ratio (proportions).)
77
78 @item(When only Y is non-zero, the image is resized to given height,
79 preserving the image aspect ratio (proportions).)
80
81 @item(When both X and Y are non-zero, the image is resized to given width and height,
82 ignoring the image aspect ratio.)
83 )
84
85 The size determines the size @italic(assuming that @link(RepeatImage) is (1,1)).
86 When @link(RepeatImage) is different -- the size is actually multiplied by the @link(RepeatImage).
87 This makes it easy to create a continuous block of images by just increasing @link(RepeatImage).
88 }
89 property Size: TVector2 read FSize write SetSize;
90
91 { How many times to repeat the image, along X and Y. By default this is (1,1). }
92 property RepeatImage: TVector2 read FRepeatImage write SetRepeatImage;
93
94 { How is the image shifted. This just shifts the texture coordinates, not moving the geometry.
95 By default this is (0,0). }
96 property Shift: TVector2 read FShift write SetShift;
97
98 { Color to multiply by image. By default, opaque white. }
99 property Color: TCastleColor read FColor write SetColor;
100
101 { Load again the image from current URL.
102 This makes sense to be used when underlying file on disk
103 changed, and you want to reload it.
104
105 TODO: In case image is shared with other TCastleImageTransform instances,
106 or TCastleScene (refering to the same image using TImageTextureNode),
107 for now this method will not load new image from disk.
108 That's because the previous image contents will "survive" in the cache. }
109 procedure ReloadUrl;
110 published
111 { How is the image scaled.
112 Determines scaling done both by @link(Size) and TCastleTransform.Scale. }
113 property SmoothScaling: Boolean read FSmoothScaling write SetSmoothScaling default true;
114
115 { How to treat alpha channel of the image.
116 By default, this is acAuto, which means that image contents
117 together with current @link(Color) determine how
118 the alpha of image is treated (opaque, alpha test, alpha blending).
119 Set this property force specific treatment. }
120 property AlphaChannel: TAutoAlphaChannel read FAlphaChannel write SetAlphaChannel default acAuto;
121
122 { URL of the image to be loaded.
123 When empty, no image is loaded and this is invisible. }
124 property Url: String read FUrl write SetUrl;
125
126 { Mipmaps make the image look good when it scaled to be @italic(much)
127 smaller on the screen. Using mipmaps also forces the image to have
128 power-of-2 size (it will be resized to power-of-2 if necessary).
129
130 Mipmaps typically make sense for 3D objects,
131 when the camera may move arbitrarily far from the object.
132 For 2D games, you usually don't want to use mipmaps, as you often want to avoid
133 scaling the image to be power-of-2. }
134 property Mipmaps: Boolean read FMipmaps write SetMipmaps default false;
135
136 {$define read_interface_class}
137 {$I auto_generated_persistent_vectors/tcastleimagetransform_persistent_vectors.inc}
138 {$undef read_interface_class}
139 end;
140
141 {$endif read_interface}
142
143 {$ifdef read_implementation}
144
145 { TODO: Implement CropRectangle at some point.
146 Note that it will force a different implementation of RepeatImage, when crop is used. }
147
148 { TCastleImageTransform ---------------------------------------------------------------- }
149
150 constructor TCastleImageTransform.Create(AOwner: TComponent);
151 begin
152 inherited;
153
154 { set default values }
155 FSmoothScaling := true;
156 FAlphaChannel := acAuto;
157 FPivot := Vector2(0.5, 0.5);
158 FSize := Vector2(0, 0);
159 FRepeatImage := Vector2(1, 1);
160 FShift := Vector2(0, 0);
161 FColor := White;
162
163 { create scene }
164 FScene := TCastleScene.Create(nil);
165 FScene.SetTransient;
166 Add(FScene);
167
168 { create X3D nodes }
169 FTexPropertiesNode := TTexturePropertiesNode.Create;
170 FTexPropertiesNode.MinificationFilter := minDefault; // corresponding to default SmoothScaling = true
171 FTexPropertiesNode.MagnificationFilter := magDefault; // corresponding to default SmoothScaling = true
172 FTexPropertiesNode.BoundaryModeS := bmClampToEdge; // correspoding to default FRepeatImage and FShift, UpdateCoordinateNodes may change it
173 FTexPropertiesNode.BoundaryModeT := bmClampToEdge; // correspoding to default FRepeatImage and FShift, UpdateCoordinateNodes may change it
174 { Do not force "power of 2" size, which may prevent mipmaps.
175 This seems like a better default (otherwise the resizing underneath
176 may cause longer loading time, and loss of quality, if not expected).
177 Consistent with X3DLoadInternalImage. }
178 FTexPropertiesNode.GuiTexture := not FMipmaps;
179
180 FTextureNode := TImageTextureNode.Create;
181 { No point in adjusting RepeatS/T: TextureProperties override it.
182 FTextureNode.RepeatS := false;
183 FTextureNode.RepeatT := false; }
184 FTextureNode.TextureProperties := FTexPropertiesNode;
185
186 FMaterialNode := TUnlitMaterialNode.Create;
187 FMaterialNode.EmissiveColor := FColor.XYZ;
188 FMaterialNode.Transparency := 1 - FColor.W;
189 FMaterialNode.EmissiveTexture := FTextureNode;
190
191 FAppearanceNode := TAppearanceNode.Create;
192 FAppearanceNode.AlphaMode := amAuto; // corresponding to default AlphaChannel = acAuto
193 FAppearanceNode.Material := FMaterialNode;
194
195 FCoordinateNode := TCoordinateNode.Create;
196 { Add some zero vectors, this way renderer can prepare VBO
197 and we avoid X3D warnings about invalid vertex index when FCoordinateNode is empty. }
198 FCoordinateNode.SetPoint([
199 TVector3.Zero,
200 TVector3.Zero,
201 TVector3.Zero,
202 TVector3.Zero
203 ]);
204
205 FTexCoordNode := TTextureCoordinateNode.Create;
206 { Add some zero vectors, this way we avoid warnings that 4 tex coords are expected.
207 Testcase: Add TCastleImageTransform, or any TCastleCamera or TCastlePunctualLight
208 (since they all use TCastleImageTransform for design-time visualization),
209 to a viewport and activate shadow volumes by setting Shadows=true on some light. }
210 FTexCoordNode.SetPoint([
211 TVector2.Zero,
212 TVector2.Zero,
213 TVector2.Zero,
214 TVector2.Zero
215 ]);
216
217 FIndexedTriangleSetNode := TIndexedTriangleSetNode.Create;
218 FIndexedTriangleSetNode.SetIndex([0, 1, 2, 0, 2, 3]);
219 FIndexedTriangleSetNode.Coord := FCoordinateNode;
220 FIndexedTriangleSetNode.TexCoord := FTexCoordNode;
221 FIndexedTriangleSetNode.Solid := false;
222
223 FShapeNode := TShapeNode.Create;
224 FShapeNode.Appearance := FAppearanceNode;
225 FShapeNode.Geometry := FIndexedTriangleSetNode;
226 { Render will change to true once some image is loaded.
227 This way we avoid rendering FCoordinateNode with zero contents. }
228 FShapeNode.Visible := false;
229
230 FRootNode := TX3DRootNode.Create;
231 FRootNode.AddChildren(FShapeNode);
232 FScene.Load(FRootNode, true);
233
234 FUrlMonitoring.Init({$ifdef FPC}@{$endif} ReloadUrl);
235
236 {$define read_implementation_constructor}
237 {$I auto_generated_persistent_vectors/tcastleimagetransform_persistent_vectors.inc}
238 {$undef read_implementation_constructor}
239 end;
240
241 destructor TCastleImageTransform.Destroy;
242 begin
243 {$define read_implementation_destructor}
244 {$I auto_generated_persistent_vectors/tcastleimagetransform_persistent_vectors.inc}
245 {$undef read_implementation_destructor}
246
247 FreeAndNil(FScene);
248 inherited;
249 end;
250
251 procedure TCastleImageTransform.BeforeDestruction;
252 begin
253 FUrlMonitoring.Finish(FUrl);
254 inherited;
255 end;
256
257 function TCastleImageTransform.PropertySections(const PropertyName: String): TPropertySections;
258 begin
259 if (PropertyName = 'PivotPersistent') or
260 (PropertyName = 'SizePersistent') or
261 (PropertyName = 'RepeatImagePersistent') or
262 (PropertyName = 'ShiftPersistent') or
263 (PropertyName = 'ColorPersistent') or
264 (PropertyName = 'SmoothScaling') or
265 (PropertyName = 'AlphaChannel') or
266 (PropertyName = 'Url') then
267 Result := [psBasic]
268 else
269 Result := inherited PropertySections(PropertyName);
270 end;
271
272 procedure TCastleImageTransform.UpdateCoordinateNodes;
273 var
274 X1, Y1, X2, Y2, W, H: Single;
275 begin
276 FTextureNode.IsTextureLoaded := true;
277 { Render is true if and only if FCoordinateNode contains useful content (not just zeros) }
278 FShapeNode.Visible := FTextureNode.IsTextureImage;
279 if FTextureNode.IsTextureImage then
280 begin
281 W := FTextureNode.TextureImage.Width;
282 H := FTextureNode.TextureImage.Height;
283
284 { change W, H according to Size }
285 if not ((Size.X = 0) and (Size.Y = 0)) then
286 begin
287 if (Size.X <> 0) and (Size.Y <> 0) then
288 begin
289 W := Size.X;
290 H := Size.Y;
291 end else
292 if Size.X <> 0 then
293 begin
294 H := H * Size.X / W;
295 W := Size.X;
296 end else
297 begin
298 Assert(Size.Y <> 0);
299 Assert(Size.X = 0);
300 W := W * Size.Y / H;
301 H := Size.Y;
302 end;
303 end;
304
305 { calculate coordinates }
306 X1 := - W * Pivot.X;
307 Y1 := - H * Pivot.Y;
308 X2 := W * (RepeatImage.X - Pivot.X);
309 Y2 := H * (RepeatImage.Y - Pivot.Y);
310 FCoordinateNode.SetPoint([
311 Vector3(X1, Y1, 0),
312 Vector3(X2, Y1, 0),
313 Vector3(X2, Y2, 0),
314 Vector3(X1, Y2, 0)
315 ]);
316
317 { calculate texture coordinates }
318 X1 := FShift.X;
319 Y1 := FShift.Y;
320 X2 := FShift.X + FRepeatImage.X;
321 Y2 := FShift.Y + FRepeatImage.Y;
322
323 FTexCoordNode.SetPoint([
324 Vector2(X1, Y1),
325 Vector2(X2, Y1),
326 Vector2(X2, Y2),
327 Vector2(X1, Y2)
328 ]);
329
330 FTexPropertiesNode.BoundaryModeS := BoolRepeatToBoundaryMode[(FShift.X <> 0) or (FRepeatImage.X > 1)];
331 FTexPropertiesNode.BoundaryModeT := BoolRepeatToBoundaryMode[(FShift.Y <> 0) or (FRepeatImage.Y > 1)];
332 end else
333 begin
334 FCoordinateNode.SetPoint([
335 TVector3.Zero,
336 TVector3.Zero,
337 TVector3.Zero,
338 TVector3.Zero
339 ]);
340 FTexCoordNode.SetPoint([
341 TVector2.Zero,
342 TVector2.Zero,
343 TVector2.Zero,
344 TVector2.Zero
345 ]);
346 end;
347 end;
348
349 procedure TCastleImageTransform.SetUrl(const Value: String);
350 begin
351 if FUrl <> Value then
352 begin
353 FUrlMonitoring.ChangeUrl(FUrl, Value);
354 if Value <> '' then
355 FTextureNode.SetUrl([Value])
356 else
357 FTextureNode.SetUrl([]);
358 UpdateCoordinateNodes;
359 end;
360 end;
361
362 procedure TCastleImageTransform.ReloadUrl;
363 var
364 SavedUrl: String;
365 begin
366 SavedUrl := Url;
367 Url := '';
368 Url := SavedUrl;
369 end;
370
371 procedure TCastleImageTransform.SetPivot(const Value: TVector2);
372 begin
373 if not TVector2.PerfectlyEquals(FPivot, Value) then
374 begin
375 FPivot := Value;
376 UpdateCoordinateNodes;
377 end;
378 end;
379
380 procedure TCastleImageTransform.SetSize(const Value: TVector2);
381 begin
382 if not TVector2.PerfectlyEquals(FSize, Value) then
383 begin
384 FSize := Value;
385 UpdateCoordinateNodes;
386 end;
387 end;
388
389 procedure TCastleImageTransform.SetRepeatImage(const Value: TVector2);
390 begin
391 if not TVector2.PerfectlyEquals(FRepeatImage, Value) then
392 begin
393 FRepeatImage := Value;
394 UpdateCoordinateNodes;
395 end;
396 end;
397
398 procedure TCastleImageTransform.SetShift(const Value: TVector2);
399 begin
400 if not TVector2.PerfectlyEquals(FShift, Value) then
401 begin
402 FShift := Value;
403 UpdateCoordinateNodes;
404 end;
405 end;
406
407 procedure TCastleImageTransform.SetColor(const Value: TCastleColor);
408 begin
409 if not TCastleColor.PerfectlyEquals(FColor, Value) then
410 begin
411 FColor := Value;
412 FMaterialNode.EmissiveColor := FColor.XYZ;
413 FMaterialNode.Transparency := 1 - FColor.W;
414 end;
415 end;
416
417 procedure TCastleImageTransform.SetSmoothScaling(const Value: Boolean);
418 begin
419 if FSmoothScaling <> Value then
420 begin
421 FSmoothScaling := Value;
422 if Value then
423 begin
424 FTexPropertiesNode.MinificationFilter := minDefault;
425 FTexPropertiesNode.MagnificationFilter := magDefault;
426 end else
427 begin
428 FTexPropertiesNode.MinificationFilter := minNearest;
429 FTexPropertiesNode.MagnificationFilter := magNearest;
430 end;
431 FScene.ChangedAll; // TODO ChangedAll should not be needed
432 end;
433 end;
434
435 procedure TCastleImageTransform.SetAlphaChannel(const Value: TAutoAlphaChannel);
436 const
437 AlphaChannelToMode: array [TAutoAlphaChannel] of TAlphaMode = (
438 { acAuto -> } amAuto,
439 { acNone -> } amOpaque,
440 { acTest -> } amMask,
441 { acBlending -> } amBlend
442 );
443 begin
444 if FAlphaChannel <> Value then
445 begin
446 FAlphaChannel := Value;
447 FAppearanceNode.AlphaMode := AlphaChannelToMode[Value];
448 FScene.ChangedAll; // TODO ChangedAll should not be needed
449 end;
450 end;
451
452 procedure TCastleImageTransform.SetMipmaps(const Value: Boolean);
453 begin
454 if FMipmaps <> Value then
455 begin
456 FMipmaps := Value;
457 FTexPropertiesNode.GuiTexture := not FMipmaps;
458 FScene.ChangedAll; // TODO ChangedAll should not be needed
459 end;
460 end;
461
462 {$define read_implementation_methods}
463 {$I auto_generated_persistent_vectors/tcastleimagetransform_persistent_vectors.inc}
464 {$undef read_implementation_methods}
465
466 {$endif read_implementation}