| 1 |
{%MainUnit x3dnodes.pas} |
| 2 |
{ |
| 3 |
Copyright 2002-2023 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 |
const |
| 20 |
DefaultMaterial_1AmbientColor: TVector3 = (X: 0.2; Y: 0.2; Z: 0.2) deprecated 'use TPhongMaterialInfo.DefaultAmbientColor'; |
| 21 |
DefaultMaterialAmbientIntensity = 0.2 deprecated 'use TPhongMaterialInfo.DefaultAmbientIntensity'; |
| 22 |
DefaultMaterialDiffuseColor: TVector3 = (X: 0.8; Y: 0.8; Z: 0.8) deprecated 'use TPhongMaterialInfo.DefaultDiffuseColor'; |
| 23 |
DefaultMaterialSpecularColor: TVector3 = (X: 0; Y: 0; Z: 0) deprecated 'use TPhongMaterialInfo.DefaultSpecularColor'; |
| 24 |
DefaultMaterialEmissiveColor: TVector3 = (X: 0; Y: 0; Z: 0) deprecated 'use TPhongMaterialInfo.DefaultEmissiveColor'; |
| 25 |
DefaultMaterialShininess = 0.2 deprecated 'use TPhongMaterialInfo.DefaultShininess'; |
| 26 |
DefaultMaterialTransparency = 0.0 deprecated 'use TPhongMaterialInfo.DefaultTransparency'; |
| 27 |
DefaultMaterialMirror = 0.0 deprecated 'use TPhongMaterialInfo.DefaultMirror'; |
| 28 |
DefaultMaterialReflSpecularExp = 1000000 deprecated 'use TPhongMaterialInfo.DefaultReflSpecularExp'; |
| 29 |
DefaultMaterialTransSpecularExp = 1000000 deprecated 'use TPhongMaterialInfo.DefaultTransSpecularExp'; |
| 30 |
|
| 31 |
{ Default value of TAppearanceNode.AlphaCutoff. } |
| 32 |
DefaultAlphaCutoff = 0.5; |
| 33 |
|
| 34 |
{ Constants for |
| 35 |
@link(TMaterialBindingNode_1.FdValue).Value and |
| 36 |
@link(TNormalBindingNode_1.FdValue).Value. |
| 37 |
@groupBegin } |
| 38 |
BIND_DEFAULT = 0 deprecated 'use X3D instead of VRML 1 nodes'; |
| 39 |
BIND_OVERALL = 1 deprecated 'use X3D instead of VRML 1 nodes'; |
| 40 |
BIND_PER_PART = 2 deprecated 'use X3D instead of VRML 1 nodes'; |
| 41 |
BIND_PER_PART_INDEXED = 3 deprecated 'use X3D instead of VRML 1 nodes'; |
| 42 |
BIND_PER_FACE = 4 deprecated 'use X3D instead of VRML 1 nodes'; |
| 43 |
BIND_PER_FACE_INDEXED = 5 deprecated 'use X3D instead of VRML 1 nodes'; |
| 44 |
BIND_PER_VERTEX = 6 deprecated 'use X3D instead of VRML 1 nodes'; |
| 45 |
BIND_PER_VERTEX_INDEXED = 7 deprecated 'use X3D instead of VRML 1 nodes'; |
| 46 |
{ @groupEnd } |
| 47 |
|
| 48 |
{ Constants for @link(TShapeHintsNode_1.FdVertexOrdering).Value. |
| 49 |
@groupBegin } |
| 50 |
VERTORDER_UNKNOWN = 0 deprecated 'use X3D instead of VRML 1 nodes'; |
| 51 |
VERTORDER_CLOCKWISE = 1 deprecated 'use X3D instead of VRML 1 nodes'; |
| 52 |
VERTORDER_COUNTERCLOCKWISE = 2 deprecated 'use X3D instead of VRML 1 nodes'; |
| 53 |
{ @groupEnd } |
| 54 |
|
| 55 |
{ Constants for @link(TShapeHintsNode_1.FdShapeType).Value. |
| 56 |
@groupBegin } |
| 57 |
SHTYPE_UNKNOWN = 0 deprecated 'use X3D instead of VRML 1 nodes'; |
| 58 |
SHTYPE_SOLID = 1 deprecated 'use X3D instead of VRML 1 nodes'; |
| 59 |
{ @groupEnd } |
| 60 |
|
| 61 |
{ Constants for @link(TShapeHintsNode_1.FdFaceType).Value. |
| 62 |
@groupBegin } |
| 63 |
FACETYPE_UNKNOWN = 0 deprecated 'use X3D instead of VRML 1 nodes'; |
| 64 |
FACETYPE_CONVEX = 1 deprecated 'use X3D instead of VRML 1 nodes'; |
| 65 |
{ @groupEnd } |
| 66 |
|
| 67 |
{ Constants for @link(TFontStyleNode.FdFamily).Value. |
| 68 |
@groupBegin } |
| 69 |
FSFAMILY_SERIF = 0 deprecated 'use ffSerif (TX3DFontFamily an enumerated type) with the properties like TFontStyleNode.Family'; |
| 70 |
FSFAMILY_SANS = 1 deprecated 'use ffSans (TX3DFontFamily an enumerated type) with the properties like TFontStyleNode.Family'; |
| 71 |
FSFAMILY_TYPEWRITER = 2 deprecated 'use ffTypeWriter (TX3DFontFamily an enumerated type) with the properties like TFontStyleNode.Family'; |
| 72 |
{ @groupEnd } |
| 73 |
|
| 74 |
{ Constants for VRML 1.0 @link(TFontStyleNode_1.FdStyle) flags. |
| 75 |
@groupBegin } |
| 76 |
FSSTYLE_BOLD = 0 deprecated 'use TFontStyleNode.Bold as a simple boolean'; |
| 77 |
FSSTYLE_ITALIC = 1 deprecated 'use TFontStyleNode.Italic as a simple boolean'; |
| 78 |
{ @groupEnd } |
| 79 |
|
| 80 |
{ Constants for TConeNode.FdParts.Flags. |
| 81 |
@groupBegin } |
| 82 |
CONE_PARTS_SIDES = 0 deprecated 'use X3D instead of VRML 1 nodes'; |
| 83 |
CONE_PARTS_BOTTOM = 1 deprecated 'use X3D instead of VRML 1 nodes'; |
| 84 |
{ @groupEnd } |
| 85 |
|
| 86 |
{ Constants for TCylinderNode.FdParts.Flags. |
| 87 |
@groupBegin } |
| 88 |
CYLINDER_PARTS_SIDES = 0 deprecated 'use X3D instead of VRML 1 nodes'; |
| 89 |
CYLINDER_PARTS_TOP = 1 deprecated 'use X3D instead of VRML 1 nodes'; |
| 90 |
CYLINDER_PARTS_BOTTOM = 2 deprecated 'use X3D instead of VRML 1 nodes'; |
| 91 |
{ @groupEnd } |
| 92 |
|
| 93 |
DefaultHeightMapScale = 0.01; |
| 94 |
DefaultVRML1CreaseAngle = 0.5; |
| 95 |
|
| 96 |
DefaultViewpointFieldOfView = TCastlePerspective.DefaultFieldOfView; |
| 97 |
DefaultNavigationInfoHeadlight = true; |
| 98 |
|
| 99 |
DefaultRenderedTextureWidth = 128; |
| 100 |
DefaultRenderedTextureHeight = 128; |
| 101 |
|
| 102 |
VRML1Version: TX3DVersion = (Major: 1; Minor: 0); |
| 103 |
VRML2Version: TX3DVersion = (Major: 2; Minor: 0); |
| 104 |
{ Latest X3D version supported. } |
| 105 |
X3DVersion: TX3DVersion = (Major: 4; Minor: 0); |
| 106 |
|
| 107 |
xeClassic = CastleInternalX3DLexer.xeClassic; |
| 108 |
xeXML = CastleInternalX3DLexer.xeXML; |
| 109 |
|
| 110 |
{ Minimal values for |
| 111 |
@link(DefaultTriangulationSlices), |
| 112 |
@link(DefaultTriangulationStacks). |
| 113 |
|
| 114 |
Note that MinTriangulationSlices can be lower (2), it works, |
| 115 |
but the result isn't really sensible. |
| 116 |
@groupBegin } |
| 117 |
MinTriangulationSlices: Cardinal = 3; |
| 118 |
MinTriangulationStacks: Cardinal = 1; |
| 119 |
{ @groupEnd } |
| 120 |
|
| 121 |
{ Classes corresponding to nodes on TVRML1State. } |
| 122 |
VRML1StateClasses: array [TVRML1StateNode] of TX3DNodeClass = ( |
| 123 |
TCoordinate3Node_1, |
| 124 |
TShapeHintsNode_1, |
| 125 |
TFontStyleNode_1, |
| 126 |
TMaterialNode_1, |
| 127 |
TMaterialBindingNode_1, |
| 128 |
TNormalNode, |
| 129 |
TNormalBindingNode_1, |
| 130 |
TTexture2Node_1, |
| 131 |
TTextureCoordinate2Node_1 |
| 132 |
{ additions here must be synchronized with additions to TVRML1State } |
| 133 |
) deprecated 'use X3D instead of VRML 1 nodes'; |
| 134 |
|
| 135 |
{ Convert Boolean meaning "repeat" into TBoundaryMode. |
| 136 |
Useful to convert e.g. TImageTextureNode.RepeatS value |
| 137 |
into TTexturePropertiesNode.BoundaryModeS value. } |
| 138 |
BoolRepeatToBoundaryMode: array [Boolean] of TBoundaryMode = ( |
| 139 |
bmClampToEdge, |
| 140 |
bmRepeat |
| 141 |
); |
| 142 |
|
| 143 |
ftExp = ftExponential deprecated 'use ftExponential'; |
| 144 |
|
| 145 |
var |
| 146 |
{ How much to subdivide various objects (like sphere, cylinder) into triangles. |
| 147 |
|
| 148 |
"Slices" divide the circumference of the circle, like the slices of a pizza. |
| 149 |
Must be always >= @link(MinTriangulationSlices). |
| 150 |
|
| 151 |
See @url(https://castle-engine.io/x3d_implementation_geometry3d_extensions.php#section_triangulation |
| 152 |
more about triangulation). } |
| 153 |
DefaultTriangulationSlices: Cardinal = 30; |
| 154 |
|
| 155 |
{ How much to subdivide various objects (like sphere, cylinder) into triangles. |
| 156 |
|
| 157 |
"Stacks" divide the height of the object, like the stacks of a cake or tower. |
| 158 |
Must be always >= @link(MinTriangulationStacks). |
| 159 |
|
| 160 |
See @url(https://castle-engine.io/x3d_implementation_geometry3d_extensions.php#section_triangulation |
| 161 |
more about triangulation). } |
| 162 |
DefaultTriangulationStacks: Cardinal = 20; |
| 163 |
|
| 164 |
const |
| 165 |
{ Standard prefix name for a time sensor to be recognized as a "named animation" |
| 166 |
for TCastleSceneCore.PlayAnimation and friends. } |
| 167 |
DefaultAnimationPrefix = ''; |
| 168 |
|
| 169 |
AllAccessTypes = [atInputOnly, atOutputOnly, atInitializeOnly, atInputOutput]; |
| 170 |
RestrictedAccessTypes = [atInputOnly, atOutputOnly, atInitializeOnly]; |
| 171 |
|
| 172 |
var |
| 173 |
{ Functions registered here will be called when any TX3DNode descendant |
| 174 |
will be destroyed. } |
| 175 |
AnyNodeDestructionNotifications: TNodeDestructionNotificationList; |
| 176 |
|
| 177 |
{ Cache, for all the resources not tied with renderer context. } |
| 178 |
X3DCache: TX3DFontTexturesCache; |
| 179 |
|
| 180 |
{ Should we emit a warning when loading data from |
| 181 |
an URI with an absolute filename, like @code(file:///c:/blah/myimage.png). |
| 182 |
The warning is emitted using WritelnWarning. |
| 183 |
This is quite useful, as usually you want to avoid using such URIs, |
| 184 |
as they will probably not work on any other system than your own |
| 185 |
(and they prevent you from easily moving the files to some other system/location). |
| 186 |
|
| 187 |
In your data (X3D, VRML, Collada, OBJ materials...), |
| 188 |
you should always use relative paths. } |
| 189 |
WarnAboutAbsoluteFilenames: boolean = true; |
| 190 |
|
| 191 |
(*Whether to resolve relative URLs within X3D prototypes with respect |
| 192 |
to the file that instantiated the prototype (false, default, compatible with Instant Player) |
| 193 |
or to the file that defined the prototype (true, compatible with BS Contact). |
| 194 |
|
| 195 |
This is unfortunately ambiguous in the X3D specification, and various |
| 196 |
other X3D browsers behave differently. |
| 197 |
|
| 198 |
Consider you have an X3D file like this: |
| 199 |
|
| 200 |
@preformatted( |
| 201 |
EXTENRPROTO MyObject [ field MFString urlTexture ] "textures/library.wrl#MyObject" |
| 202 |
MyObject { urlTexture "textures/myfile.png" } |
| 203 |
MyObject { urlTexture "myfile.png" } |
| 204 |
) |
| 205 |
|
| 206 |
And next to this file, in a subfolder, you have these files: |
| 207 |
|
| 208 |
@preformatted( |
| 209 |
textures/library.wrl |
| 210 |
textures/myfile.png |
| 211 |
) |
| 212 |
|
| 213 |
Which declaration of @code(MyObject) is correct? |
| 214 |
|
| 215 |
@unorderedList( |
| 216 |
@item(When this is false, you should write @code(MyObject { urlTexture "myfile.png" }). |
| 217 |
The URL is resolved with respect to the file that contains |
| 218 |
the ImageTexture node, which is "textures/library.wrl".) |
| 219 |
|
| 220 |
@item(When this is true, you should write @code(MyObject { urlTexture "textures/myfile.png" }). |
| 221 |
The URL is resolved with respect to the file that instantiates |
| 222 |
the prototype. |
| 223 |
|
| 224 |
Note that our implementation of this case is a bit incomplete. |
| 225 |
It works for most nodes (ImageTexture / AudioClip / ShaderPart / ...), |
| 226 |
but it does not work for the Inline node, |
| 227 |
and for URLs in recursive EXTERNPROTO. |
| 228 |
So URLs in Inline nodes, or in EXTERNPROTO, are always resolved |
| 229 |
like RebaseRelativeUrlsInPrototypes = false. |
| 230 |
) |
| 231 |
) |
| 232 |
|
| 233 |
See the X3D specification |
| 234 |
http://www.web3d.org/documents/specifications/19775-1/V3.3/Part01/components/networking.html#RelativeURLs |
| 235 |
(unfortunately, it does not clarify this case definitively). |
| 236 |
See our testcase in |
| 237 |
https://github.com/castle-engine/demo-models/tree/master/prototypes/relative_urls_test |
| 238 |
*) |
| 239 |
RebaseRelativeUrlsInPrototypes: boolean = false; |
| 240 |
|
| 241 |
{ Default fonts used by Text and FontStyle nodes. } |
| 242 |
DefaultFontData: array [ |
| 243 |
TX3DFontFamily, |
| 244 |
{ bold } Boolean, |
| 245 |
{ italic } Boolean |
| 246 |
] of TTextureFontData; |
| 247 |
|
| 248 |
{ Free all unused X3D nodes on the list, then free and @nil the list itself. |
| 249 |
Ignores @nil items within the list. } |
| 250 |
procedure X3DNodeList_FreeUnusedAndNil(var List: TX3DNodeList); |
| 251 |
|
| 252 |
{ Find a range within "key" field corresponding to given Fraction. |
| 253 |
Returns the index of @bold(right) range delimiter. |
| 254 |
So for normal ranges (between two values of "key" field) it's |
| 255 |
always between 1 and Key.Count - 1. Result 0 indicates we're |
| 256 |
before the left limit, and result equal Key.Count indicates |
| 257 |
we're after right limit. |
| 258 |
|
| 259 |
Result is always between 0 and Key.Count. |
| 260 |
|
| 261 |
Output T is the value between 0..1 indicating where within |
| 262 |
the range we are. It's undefined when Result is 0 or Key.Count |
| 263 |
(indicating we're outside limits). |
| 264 |
|
| 265 |
Call this only when Key.Count > 0. |
| 266 |
|
| 267 |
This is useful to interpreting TAbstractInterpolatorNode.KeyRange |
| 268 |
and such fields. } |
| 269 |
function KeyRange(Key: TSingleList; |
| 270 |
const Fraction: Single; out T: Single): Integer; |
| 271 |
|
| 272 |
{ Free TX3DNode if it is unused (see TX3DNode.FreeIfUnused), |
| 273 |
setting reference to @nil. Analogous to standard FreeAndNil, |
| 274 |
but checks if node is used first. } |
| 275 |
procedure FreeIfUnusedAndNil(var Obj); |
| 276 |
|
| 277 |
{$endif read_interface} |
| 278 |
|
| 279 |
{$ifdef read_implementation} |
| 280 |
|
| 281 |
procedure X3DNodeList_FreeUnusedAndNil(var List: TX3DNodeList); |
| 282 |
var |
| 283 |
I: Integer; |
| 284 |
begin |
| 285 |
if List <> nil then |
| 286 |
begin |
| 287 |
for I := 0 to List.Count - 1 do |
| 288 |
if (List.Items[I] <> nil) and |
| 289 |
(List.Items[I].VRML1ParentsCount + List.Items[I].ParentFieldsCount = 0) then |
| 290 |
List.Items[I].Free; |
| 291 |
FreeAndNil(List); |
| 292 |
end; |
| 293 |
end; |
| 294 |
|
| 295 |
function KeyRange(Key: TSingleList; |
| 296 |
const Fraction: Single; out T: Single): Integer; |
| 297 |
var |
| 298 |
A, B: Integer; |
| 299 |
begin |
| 300 |
Assert(Key.Count > 0); |
| 301 |
Assert(not IsNan(Fraction)); |
| 302 |
|
| 303 |
if Fraction <= Key.First then |
| 304 |
Result := 0 else |
| 305 |
if Fraction >= Key.Last then |
| 306 |
Result := Key.Count else |
| 307 |
begin |
| 308 |
{ Then for sure we're between two Key values. |
| 309 |
Note that we know that Key.Count > 1 (otherwise, Key.First = Key.Last |
| 310 |
so one of <= or >= comparisons above would occur; we check |
| 311 |
IsNan(Fraction) at the beginning to eliminate Fraction=NaN case). } |
| 312 |
Assert(Key.Count > 1); |
| 313 |
|
| 314 |
{ Always A < B. |
| 315 |
We're considering range from Key[A] to Key[B]. |
| 316 |
|
| 317 |
Remember that we cannot actually depend on the fact that |
| 318 |
Key values are non-decreasing. They should be non-decreasing, |
| 319 |
and we have to output correct result only when they are non-decreasing, |
| 320 |
but we also have to terminate (with any result) in any situation. |
| 321 |
Reason: Key values are supplied in X3D file, so they may be broken |
| 322 |
in every possible way. } |
| 323 |
|
| 324 |
A := 0; |
| 325 |
B := Key.Count - 1; |
| 326 |
while B - A > 1 do |
| 327 |
begin |
| 328 |
Result := (A + B) div 2; |
| 329 |
{ A < B => (A + B) < 2B => (A + B) div 2 < B => Result < B. |
| 330 |
Also, Result > A (the only way how Result could be = A |
| 331 |
would be when B = A + 1, but we eliminated this case by "while" |
| 332 |
condition". |
| 333 |
|
| 334 |
This is good, it means A < Result < B, so Result is good candidate |
| 335 |
for next A or B, it will for sure shorten the distance |
| 336 |
between A and B. } |
| 337 |
Assert(A < Result); |
| 338 |
Assert(Result < B); |
| 339 |
{ Using "<" instead of "<=" below: when the Fraction = Key[Result], |
| 340 |
we want to go to right part, not left. See |
| 341 |
- X3D specification |
| 342 |
http://www.web3d.org/documents/specifications/19775-1/V3.3/Part01/components/utils.html#SequencingEvents |
| 343 |
It says that f(t) = vn, if tn <= t < tn-1 |
| 344 |
- Testcase on https://github.com/castle-engine/castle-model-viewer/issues/2 |
| 345 |
} |
| 346 |
if Fraction < Key[Result] then |
| 347 |
B := Result else |
| 348 |
A := Result; |
| 349 |
end; |
| 350 |
Result := B; |
| 351 |
|
| 352 |
if Key[B] > Key[A] then |
| 353 |
T := (Fraction - Key[A]) / (Key[B] - Key[A]) |
| 354 |
else |
| 355 |
T := 0; |
| 356 |
end; |
| 357 |
end; |
| 358 |
|
| 359 |
procedure FreeIfUnusedAndNil(var Obj); |
| 360 |
var |
| 361 |
Temp: TX3DNode; |
| 362 |
begin |
| 363 |
Temp := TX3DNode(Obj); |
| 364 |
Pointer(Obj) := nil; |
| 365 |
Temp.FreeIfUnused; |
| 366 |
end; |
| 367 |
|
| 368 |
{$endif read_implementation} |