Debian Codegraph @

src/scene/x3d/x3dnodes_utils_materials.inc

raw
1 {%MainUnit x3dnodes.pas}
2 {
3 Copyright 2002-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 { Extra types related to materials. }
18
19 {$ifdef read_interface}
20
21 { Abstract class for material information,
22 that define material properties for various lighting equations.
23 Particular public descendants define different lighting equations.
24 Particular private descendants map X3D nodes' fields to these parameters. }
25 TMaterialInfo = class abstract
26 strict private
27 FNode: TX3DNode;
28 protected
29 function GetMainColor: TVector3; virtual; abstract;
30 procedure SetMainColor(const Value: TVector3); virtual; abstract;
31 function GetEmissiveColor: TVector3; virtual; abstract;
32 procedure SetEmissiveColor(const Value: TVector3); virtual; abstract;
33 public
34 const
35 DefaultTransparency = 0.0;
36
37 constructor Create(ANode: TX3DNode);
38
39 function Transparency: Single; virtual; abstract;
40 { Opacity is just @code(1 - Transparency). }
41 function Opacity: Single;
42
43 { Associated material node. }
44 property Node: TX3DNode read FNode;
45
46 { Single color that is the best approximation of this material look.
47
48 For Phong material, it's DiffuseColor. For unlit material, it's EmissiveColor.
49 For physical material, it's BaseColor.
50
51 This is useful for debug purposes,
52 for example when we force everything to be unlit by Attributes.Lighting=false.
53 It doesn't follow any X3D official equations, it's only for debugging. }
54 property MainColor: TVector3 read GetMainColor write SetMainColor;
55
56 { Emissive color represents the color of the material
57 that is not affected by any light sources.
58 All material types (unlit, Phong, physical) have this color. }
59 property EmissiveColor: TVector3 read GetEmissiveColor write SetEmissiveColor;
60 end;
61
62 { Material information that defines parameters for Phong lighting equations.
63 This hides differences between various material nodes,
64 like TMaterialNode and TCommonSurfaceShaderNode. }
65 TPhongMaterialInfo = class(TMaterialInfo)
66 protected
67 function GetDiffuseColor: TVector3; virtual; abstract;
68 procedure SetDiffuseColor(const Value: TVector3); virtual; abstract;
69 function GetSpecularColor: TVector3; virtual; abstract;
70 procedure SetSpecularColor(const Value: TVector3); virtual; abstract;
71 { Main color of Phong material is @link(DiffuseColor). }
72 function GetMainColor: TVector3; override;
73 procedure SetMainColor(const Value: TVector3); override;
74 public
75 const
76 { Default material parameters.
77
78 They luckily match between all the material-like nodes:
79 @unorderedList(
80 @itemSpacing Compact
81 @item X3D and VRML 2.0: TMaterialNode, TTwoSidedMaterialNode
82 @item VRML 1.0: TMaterialNode_1
83 @item Extension: TCommonSurfaceShaderNode
84 )
85
86 The AmbientColor is an exception.
87 In X3D the effective AmbientColor is an AmbientIntensity * DiffuseColor,
88 thus it has a default value of
89 @code(DefaultAmbientIntensity * DefaultiffuseColor = (0.2 * 0.8, 0.2 * 0.8, 0.2 * 0.8)).
90 }
91 DefaultAmbientColor: TVector3 = (X: 0.2; Y: 0.2; Z: 0.2);
92 DefaultAmbientIntensity = 0.2;
93 DefaultDiffuseColor: TVector3 = (X: 0.8; Y: 0.8; Z: 0.8);
94 DefaultSpecularColor: TVector3 = (X: 0; Y: 0; Z: 0);
95 DefaultEmissiveColor: TVector3 = (X: 0; Y: 0; Z: 0);
96 DefaultShininess = 0.2;
97 DefaultShininessExp = DefaultShininess * 128;
98 DefaultReflectionColor: TVector3 = (X: 0; Y: 0; Z: 0);
99 DefaultTransmissionColor: TVector3 = (X: 0; Y: 0; Z: 0);
100 DefaultReflSpecularExp = 1000000;
101 DefaultTransSpecularExp = 1000000;
102
103 function AmbientColor: TVector3; virtual; abstract;
104 property DiffuseColor: TVector3 read GetDiffuseColor write SetDiffuseColor;
105 property SpecularColor: TVector3 read GetSpecularColor write SetSpecularColor;
106
107 { Only the emissiveColor is not black (zero),
108 which means that the material behaves like unlit.
109
110 This checks that ambient and diffuse and specular colors are all zero.
111 It's an important information about the material sometimes.
112 We can optimize this case when rendering. }
113 function PureEmissive: boolean;
114
115 function Shininess: Single; virtual; abstract;
116
117 { Shininess exponent for Phong lighting equations.
118
119 Remember that the X3D @link(Shininess) field is "normalized",
120 which means that it has to be multiplied by 128.0 to get
121 actual exponent for lighting equations.
122 This function returns the real exponent
123 (already multiplied by 128.0, if necessary). }
124 function ShininessExp: Single;
125
126 function ReflectionColor: TVector3; virtual; abstract;
127 function TransmissionColor: TVector3; virtual;
128
129 { Used by path-tracer.
130 See https://castle-engine.io/x3d_extensions.php#section_ext_material_phong_brdf_fields .
131 Not useful for normal CGE rendering.
132 @deprecated }
133 function ReflSpecular: TVector3; virtual;
134 function ReflDiffuse: TVector3; virtual;
135 function TransSpecular: TVector3; virtual;
136 function TransDiffuse: TVector3; virtual;
137 function ReflSpecularExp: Single; virtual;
138 function TransSpecularExp: Single; virtual;
139 end;
140
141 TX3DMaterialInfoAbstract = TMaterialInfo deprecated 'use TMaterialInfo';
142
143 { Material information that defines parameters for unlit lighting equations. }
144 TUnlitMaterialInfo = class(TMaterialInfo)
145 protected
146 { Main color of unlit material is @link(EmissiveColor). }
147 function GetMainColor: TVector3; override;
148 procedure SetMainColor(const Value: TVector3); override;
149 end;
150
151 { Material information that defines parameters for physical lighting equations. }
152 TPhysicalMaterialInfo = class(TMaterialInfo)
153 protected
154 { Main color of PBR material is @link(BaseColor). }
155 function GetMainColor: TVector3; override;
156 procedure SetMainColor(const Value: TVector3); override;
157 function GetBaseColor: TVector3; virtual; abstract;
158 procedure SetBaseColor(const Value: TVector3); virtual; abstract;
159 public
160 { The most important color of the physical material. }
161 property BaseColor: TVector3 read GetBaseColor write SetBaseColor;
162 function Metallic: Single; virtual; abstract;
163 function Roughness: Single; virtual; abstract;
164 end;
165
166 {$endif read_interface}
167
168 {$ifdef read_implementation}
169
170 { TMaterialInfo ---------------------------------------------------------- }
171
172 constructor TMaterialInfo.Create(ANode: TX3DNode);
173 begin
174 inherited Create;
175 FNode := ANode;
176 end;
177
178 function TMaterialInfo.Opacity: Single;
179 begin
180 Result := 1 - Transparency;
181 end;
182
183 { TPhongMaterialInfo --------------------------------------------------------- }
184
185 function TPhongMaterialInfo.ShininessExp: Single;
186 begin
187 { According to VRML / X3D specification, shininess must be within 0..1 range,
188 so it maps nicely to 0..128 range for OpenGL's exponent.
189 We do clamp to 0..128 since using values outside this range makes
190 OpenGL errors in fixed-function pipeline. }
191
192 Result := Clamped(Shininess * 128.0, 0.0, 128.0);
193 end;
194
195 function TPhongMaterialInfo.TransmissionColor: TVector3;
196 begin
197 { default TransmisionColor implementation }
198 Result := Vector3(Transparency, Transparency, Transparency);
199 end;
200
201 function TPhongMaterialInfo.PureEmissive: boolean;
202 begin
203 Result :=
204 AmbientColor.IsPerfectlyZero and
205 DiffuseColor.IsPerfectlyZero and
206 SpecularColor.IsPerfectlyZero;
207 end;
208
209 function TPhongMaterialInfo.ReflSpecular: TVector3;
210 begin
211 { Default implementation calculates something sensible looking at other fields. }
212 Result := ReflectionColor;
213 end;
214
215 function TPhongMaterialInfo.ReflDiffuse: TVector3;
216 begin
217 { Default implementation calculates something sensible looking at other fields. }
218 Result := DiffuseColor;
219 end;
220
221 function TPhongMaterialInfo.TransSpecular: TVector3;
222 begin
223 { Default implementation calculates something sensible looking at other fields. }
224 Result := TransmissionColor;
225 end;
226
227 function TPhongMaterialInfo.TransDiffuse: TVector3;
228 begin
229 { Default implementation calculates something sensible looking at other fields. }
230 Result := DiffuseColor * TransmissionColor;
231 end;
232
233 function TPhongMaterialInfo.ReflSpecularExp: Single;
234 begin
235 Result := DefaultReflSpecularExp;
236 end;
237
238 function TPhongMaterialInfo.TransSpecularExp: Single;
239 begin
240 Result := DefaultTransSpecularExp;
241 end;
242
243 function TPhongMaterialInfo.GetMainColor: TVector3;
244 begin
245 Result := DiffuseColor;
246 end;
247
248 procedure TPhongMaterialInfo.SetMainColor(const Value: TVector3);
249 begin
250 DiffuseColor := Value;
251 end;
252
253 { TUnlitMaterialInfo --------------------------------------------------------- }
254
255 function TUnlitMaterialInfo.GetMainColor: TVector3;
256 begin
257 Result := EmissiveColor;
258 end;
259
260 procedure TUnlitMaterialInfo.SetMainColor(const Value: TVector3);
261 begin
262 EmissiveColor := Value;
263 end;
264
265 { TPhysicalMaterialInfo --------------------------------------------------------- }
266
267 function TPhysicalMaterialInfo.GetMainColor: TVector3;
268 begin
269 Result := BaseColor;
270 end;
271
272 procedure TPhysicalMaterialInfo.SetMainColor(const Value: TVector3);
273 begin
274 BaseColor := Value;
275 end;
276
277 {$endif read_implementation}