Debian Codegraph @

src/scene/castlescene_abstractprimitive.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 { Primitives that can be easily added and adjusted inside TCastleViewport. }
18
19 {$ifdef read_interface}
20
21 type
22 { Primitive material type, used by @link(TCastleAbstractPrimitive.Material). }
23 TPrimitiveMaterial = (pmPhysical, pmPhong, pmUnlit);
24
25 { Base class to express primitives that can be easily added and
26 adjusted inside TCastleViewport.
27
28 Such primitives can be anything visible that can be expressed using X3D nodes
29 (like boxes or spheres).
30 Particular descendants of this class, like TCastleBox or TCastleSphere,
31 define a particular primitive.
32 Note: for things not directly visible (like lights) descending from this class
33 is not useful. Instead descend from TCastleTransform.
34
35 Using this class is somewhat similar to using TCastleScene,
36 and loading (using @link(TCastleSceneCore.Load)) an X3D nodes graph
37 with appropriate primitives. In fact, that is exactly what happens internally.
38 But using this class is simpler (it hides X3D nodes from you, although it also
39 hides some possibilities) and you can customize such primitives
40 using the CGE editor. }
41 TCastleAbstractPrimitive = class(TCastleTransform)
42 strict private
43 FScene: TCastleScene;
44 FRootNode: TX3DRootNode;
45 FAppearanceNode: TAppearanceNode;
46 FShapeNode: TShapeNode;
47 FColor: TCastleColor;
48 FMaterial: TPrimitiveMaterial;
49 FTexture, FTextureNormalMap: String;
50 FPreciseCollisions: Boolean;
51 procedure SetMaterial(const Value: TPrimitiveMaterial);
52 procedure SetColor(const Value: TCastleColor);
53 procedure SetTexture(const Value: String);
54 procedure SetTextureNormalMap(const Value: String);
55 { Reflect current FColor, FMaterial, FTexture, FTextureNormalMap values in X3D nodes. }
56 procedure UpdateMaterialNode;
57 procedure SetPreciseCollisions(const Value: Boolean);
58 function GetRenderOptions: TCastleRenderOptions;
59 protected
60 { Descendants should add primitive X3D geometry node here. }
61 property ShapeNode: TShapeNode read FShapeNode;
62
63 { Update collider, notifying it that we changed.
64 This updates TCastleCollider with TCastleCollider.AutoSize.
65 This also updates TCastleMeshCollider mesh, if it referred to us. }
66 procedure UpdateCollider;
67 public
68 constructor Create(AOwner: TComponent); override;
69 destructor Destroy; override;
70 function PropertySections(const PropertyName: String): TPropertySections; override;
71 function HasColliderMesh: Boolean; override;
72 procedure ColliderMesh(const TriangleEvent: TTriangleEvent); override;
73
74 { Color of the text. Opaque white by default.
75
76 What exactly it means, depends on @link(Material):
77 @unorderedList(
78 @item(When Material is pmPhysical, then Color determines TPhysicalMaterialNode.BaseColor and TPhysicalMaterialNode.Transparency.)
79 @item(When Material is pmPhong, then Color determines TMaterialNode.DiffuseColor and TMaterialNode.Transparency.)
80 @item(When Material is pmUnlit, then Color determines TUnlitMaterialNode.EmissiveColor and TUnlitMaterialNode.Transparency.)
81 )
82 }
83 property Color: TCastleColor read FColor write SetColor;
84 published
85 { Material type (which determines lighting calculation) for this primitive. }
86 property Material: TPrimitiveMaterial read FMaterial write SetMaterial default pmPhysical;
87
88 { Texture URL.
89
90 The way texture interacts with lighting depends on the @link(Material) type:
91 @unorderedList(
92 @item(When Material is pmPhysical, then Texture determines TPhysicalMaterialNode.BaseTexture.)
93 @item(When Material is pmPhong, then Texture determines TMaterialNode.DiffuseTexture.)
94 @item(When Material is pmUnlit, then Texture determines TUnlitMaterialNode.EmissiveTexture.)
95 )
96
97 The texture mapping is done automatically, following a reasonable algorithm
98 for the given primitive described in the X3D specification.
99 See https://castle-engine.io/x3d_implementation_geometry3d.php for links to X3D specification.
100 For example,
101 https://www.web3d.org/specifications/X3Dv4Draft/ISO-IEC19775-1v4-CD/Part01/components/geometry3D.html#Box
102 describes how texture is applied to the 6 sides of the box. }
103 property Texture: String read FTexture write SetTexture;
104
105 { Normal map texture URL.
106 Providing normal enhances the details visible on the surface because of lighting.
107 The normalmap texture mapping is the same as @link(Texture) mapping. }
108 property TextureNormalMap: String read FTextureNormalMap write SetTextureNormalMap;
109
110 { Resolve collisions precisely with the primitive geometry.
111 When this is @false we will only consider the primitive bounding box for collisions,
112 so e.g. player will not be able to move within sphere's box corners.
113 This sets @link(TCastleSceneCore.Spatial). }
114 property PreciseCollisions: Boolean read FPreciseCollisions write SetPreciseCollisions default false;
115
116 { Rendering options of the scene. }
117 property RenderOptions: TCastleRenderOptions read GetRenderOptions;
118
119 {$define read_interface_class}
120 {$I auto_generated_persistent_vectors/tcastleabstractprimitive_persistent_vectors.inc}
121 {$undef read_interface_class}
122 end;
123
124 {$endif read_interface}
125
126 {$ifdef read_implementation}
127
128 constructor TCastleAbstractPrimitive.Create(AOwner: TComponent);
129 begin
130 inherited;
131
132 { internal stuff create }
133 FScene := TCastleScene.Create(nil);
134 FScene.SetTransient;
135 Add(FScene);
136
137 { build X3D nodes }
138 FAppearanceNode := TAppearanceNode.Create;
139 FShapeNode := TShapeNode.Create;
140 FShapeNode.Appearance := FAppearanceNode;
141 FRootNode := TX3DRootNode.Create;
142 FRootNode.AddChildren(ShapeNode);
143 FScene.Load(FRootNode, true);
144
145 FMaterial := pmPhysical;
146 FColor := White;
147 UpdateMaterialNode;
148
149 {$define read_implementation_constructor}
150 {$I auto_generated_persistent_vectors/tcastleabstractprimitive_persistent_vectors.inc}
151 {$undef read_implementation_constructor}
152 end;
153
154 destructor TCastleAbstractPrimitive.Destroy;
155 begin
156 {$define read_implementation_destructor}
157 {$I auto_generated_persistent_vectors/tcastleabstractprimitive_persistent_vectors.inc}
158 {$undef read_implementation_destructor}
159
160 FreeAndNil(FScene);
161 inherited;
162 end;
163
164 procedure TCastleAbstractPrimitive.UpdateMaterialNode;
165
166 procedure AdjustTexture(const NodeField: TSFNode; const TextureUrl: String);
167 var
168 TexNode: TImageTextureNode;
169 begin
170 if TextureUrl <> '' then
171 begin
172 if NodeField.Value is TImageTextureNode then
173 TexNode := TImageTextureNode(NodeField.Value)
174 else
175 begin
176 TexNode := TImageTextureNode.Create;
177 NodeField.Value := TexNode;
178 NodeField.Changed;
179 end;
180 if (TexNode.FdUrl.Count <> 1) or
181 (TexNode.FdUrl.Items[0] <> TextureUrl) then
182 TexNode.SetUrl([TextureUrl]);
183 end else
184 begin
185 if NodeField.Value <> nil then
186 begin
187 NodeField.Value := nil;
188 { This causes time-consuming ChangedAll, which is why we checked
189 NodeField.Value <> nil above, to not call this from every UpdateMaterialNode,
190 e.g. if you changed something else than TextureUrl. }
191 NodeField.Changed;
192 end;
193 end;
194 end;
195
196 procedure DoPhysical;
197 var
198 PM: TPhysicalMaterialNode;
199 begin
200 if FAppearanceNode.Material is TPhysicalMaterialNode then
201 PM := TPhysicalMaterialNode(FAppearanceNode.Material)
202 else
203 begin
204 PM := TPhysicalMaterialNode.Create;
205 FAppearanceNode.Material := PM;
206 end;
207 PM.BaseColor := Color.XYZ;
208 PM.Transparency := 1 - Color.W;
209 AdjustTexture(PM.FdBaseTexture, Texture);
210 AdjustTexture(PM.FdNormalTexture, TextureNormalMap);
211 end;
212
213 procedure DoPhong;
214 var
215 M: TMaterialNode;
216 begin
217 if FAppearanceNode.Material is TMaterialNode then
218 M := TMaterialNode(FAppearanceNode.Material)
219 else
220 begin
221 M := TMaterialNode.Create;
222 FAppearanceNode.Material := M;
223 end;
224 M.DiffuseColor := Color.XYZ;
225 M.Transparency := 1 - Color.W;
226 AdjustTexture(M.FdDiffuseTexture, Texture);
227 AdjustTexture(M.FdNormalTexture, TextureNormalMap);
228 end;
229
230 procedure DoUnlit;
231 var
232 UM: TUnlitMaterialNode;
233 begin
234 if FAppearanceNode.Material is TUnlitMaterialNode then
235 UM := TUnlitMaterialNode(FAppearanceNode.Material)
236 else
237 begin
238 UM := TUnlitMaterialNode.Create;
239 FAppearanceNode.Material := UM;
240 end;
241 UM.EmissiveColor := Color.XYZ;
242 UM.Transparency := 1 - Color.W;
243 AdjustTexture(UM.FdEmissiveTexture, Texture);
244 AdjustTexture(UM.FdNormalTexture, TextureNormalMap);
245 end;
246
247 begin
248 case FMaterial of
249 pmPhysical: DoPhysical;
250 pmPhong : DoPhong;
251 pmUnlit : DoUnlit;
252 {$ifndef COMPILER_CASE_ANALYSIS}
253 else raise EInternalError.Create('TCastleAbstractPrimitive.UpdateMaterialNode:FMaterial?');
254 {$endif}
255 end;
256 end;
257
258 procedure TCastleAbstractPrimitive.SetMaterial(const Value: TPrimitiveMaterial);
259 begin
260 if FMaterial <> Value then
261 begin
262 FMaterial := Value;
263 UpdateMaterialNode;
264 end;
265 end;
266
267 procedure TCastleAbstractPrimitive.SetColor(const Value: TCastleColor);
268 begin
269 if not TCastleColor.PerfectlyEquals(FColor, Value) then
270 begin
271 FColor := Value;
272 UpdateMaterialNode;
273 end;
274 end;
275
276 procedure TCastleAbstractPrimitive.SetTexture(const Value: String);
277 begin
278 if FTexture <> Value then
279 begin
280 FTexture := Value;
281 UpdateMaterialNode;
282 end;
283 end;
284
285 procedure TCastleAbstractPrimitive.SetTextureNormalMap(const Value: String);
286 begin
287 if FTextureNormalMap <> Value then
288 begin
289 FTextureNormalMap := Value;
290 UpdateMaterialNode;
291 end;
292 end;
293
294 function TCastleAbstractPrimitive.PropertySections(const PropertyName: String): TPropertySections;
295 begin
296 if (PropertyName = 'ColorPersistent') or
297 (PropertyName = 'Material') or
298 (PropertyName = 'TextureNormalMap') or
299 (PropertyName = 'Texture') or
300 (PropertyName = 'PreciseCollisions') or
301 (PropertyName = 'RenderOptions') then
302 Result := [psBasic]
303 else
304 Result := inherited PropertySections(PropertyName);
305 end;
306
307 function TCastleAbstractPrimitive.HasColliderMesh: Boolean;
308 begin
309 Result := true;
310 end;
311
312 procedure TCastleAbstractPrimitive.ColliderMesh(
313 const TriangleEvent: TTriangleEvent);
314 begin
315 FScene.ColliderMesh(TriangleEvent);
316 end;
317
318 procedure TCastleAbstractPrimitive.SetPreciseCollisions(const Value: Boolean);
319 begin
320 if FPreciseCollisions <> Value then
321 begin
322 FPreciseCollisions := Value;
323 {$warnings off} // this uses deprecated Spatial, which should be Internal at some point
324 if Value then
325 FScene.Spatial := [ssDynamicCollisions]
326 else
327 FScene.Spatial := [];
328 {$warnings on}
329 { Note that we don't add ssRendering,
330 would be largely useless as primitives are usually just 1 shape in an internal scene. }
331 end;
332 end;
333
334 function TCastleAbstractPrimitive.GetRenderOptions: TCastleRenderOptions;
335 begin
336 Result := FScene.RenderOptions;
337 end;
338
339 procedure TCastleAbstractPrimitive.UpdateCollider;
340 var
341 C: TCastleCollider;
342 begin
343 C := FindBehavior(TCastleCollider) as TCastleCollider;
344 if C <> nil then
345 C.InternalTransformChanged(Self);
346 end;
347
348 {$define read_implementation_methods}
349 {$I auto_generated_persistent_vectors/tcastleabstractprimitive_persistent_vectors.inc}
350 {$undef read_implementation_methods}
351
352 {$endif read_implementation}