| 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 |
{ Circular arc in 2D. |
| 19 |
@bold(Rendering of this node is not implemented yet.) } |
| 20 |
TArc2DNode = class(TAbstractGeometryNode) |
| 21 |
public |
| 22 |
function LocalBoundingBox(State: TX3DGraphTraverseState; |
| 23 |
ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): TBox3D; override; |
| 24 |
function VerticesCount(State: TX3DGraphTraverseState; |
| 25 |
ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): Cardinal; override; |
| 26 |
function TrianglesCount(State: TX3DGraphTraverseState; |
| 27 |
ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): Cardinal; override; |
| 28 |
|
| 29 |
{$I auto_generated_node_helpers/x3dnodes_arc2d.inc} |
| 30 |
end; |
| 31 |
|
| 32 |
{ Type of @link(TArcClose2DNode.ClosureType). |
| 33 |
See X3D specification about the possible arc closure types: |
| 34 |
https://www.web3d.org/documents/specifications/19775-1/V3.3/Part01/components/geometry2D.html#ArcClose2D } |
| 35 |
TArcClosureType = (acPie, acChord); |
| 36 |
|
| 37 |
{ Closed circular arc in 2D. |
| 38 |
@bold(Rendering of this node is not implemented yet.) } |
| 39 |
TArcClose2DNode = class(TAbstractGeometryNode) |
| 40 |
public |
| 41 |
function LocalBoundingBox(State: TX3DGraphTraverseState; |
| 42 |
ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): TBox3D; override; |
| 43 |
function VerticesCount(State: TX3DGraphTraverseState; |
| 44 |
ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): Cardinal; override; |
| 45 |
function TrianglesCount(State: TX3DGraphTraverseState; |
| 46 |
ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): Cardinal; override; |
| 47 |
function SolidField: TSFBool; override; |
| 48 |
|
| 49 |
{$I auto_generated_node_helpers/x3dnodes_arcclose2d.inc} |
| 50 |
end; |
| 51 |
|
| 52 |
{ Circle in 2D. } |
| 53 |
TCircle2DNode = class(TAbstractGeometryNode) |
| 54 |
strict private |
| 55 |
function CalculateSlices: Cardinal; |
| 56 |
public |
| 57 |
function Proxy(var State: TX3DGraphTraverseState): TAbstractGeometryNode; override; |
| 58 |
|
| 59 |
{$I auto_generated_node_helpers/x3dnodes_circle2d.inc} |
| 60 |
end; |
| 61 |
|
| 62 |
{ Disc (filled circle) in 2D. } |
| 63 |
TDisk2DNode = class(TAbstractGeometryNode) |
| 64 |
strict private |
| 65 |
function CalculateSlices: Cardinal; |
| 66 |
public |
| 67 |
function Proxy(var State: TX3DGraphTraverseState): TAbstractGeometryNode; override; |
| 68 |
function SolidField: TSFBool; override; |
| 69 |
|
| 70 |
{$I auto_generated_node_helpers/x3dnodes_disk2d.inc} |
| 71 |
end; |
| 72 |
|
| 73 |
{ Series of line segments in 2D. } |
| 74 |
TPolyline2DNode = class(TAbstractGeometryNode) |
| 75 |
public |
| 76 |
constructor Create(const AX3DName: string = ''; const ABaseUrl: String = ''); override; |
| 77 |
function Proxy(var State: TX3DGraphTraverseState): TAbstractGeometryNode; override; |
| 78 |
|
| 79 |
{$I auto_generated_node_helpers/x3dnodes_polyline2d.inc} |
| 80 |
end; |
| 81 |
|
| 82 |
{ Set of vertices in 2D. } |
| 83 |
TPolypoint2DNode = class(TAbstractGeometryNode) |
| 84 |
public |
| 85 |
function Proxy(var State: TX3DGraphTraverseState): TAbstractGeometryNode; override; |
| 86 |
|
| 87 |
{$I auto_generated_node_helpers/x3dnodes_polypoint2d.inc} |
| 88 |
end; |
| 89 |
|
| 90 |
{ Rectangle in 2D. } |
| 91 |
TRectangle2DNode = class(TAbstractGeometryNode) |
| 92 |
public |
| 93 |
function Proxy(var State: TX3DGraphTraverseState): TAbstractGeometryNode; override; |
| 94 |
function SolidField: TSFBool; override; |
| 95 |
|
| 96 |
{$I auto_generated_node_helpers/x3dnodes_rectangle2d.inc} |
| 97 |
end; |
| 98 |
|
| 99 |
{ Set of triangles in 2D. } |
| 100 |
TTriangleSet2DNode = class(TAbstractGeometryNode) |
| 101 |
public |
| 102 |
function Proxy(var State: TX3DGraphTraverseState): TAbstractGeometryNode; override; |
| 103 |
function SolidField: TSFBool; override; |
| 104 |
|
| 105 |
{$I auto_generated_node_helpers/x3dnodes_triangleset2d.inc} |
| 106 |
end; |
| 107 |
|
| 108 |
{$endif read_interface} |
| 109 |
|
| 110 |
{$ifdef read_implementation} |
| 111 |
|
| 112 |
const |
| 113 |
ArcClosureTypeNames: array [TArcClosureType] of string = |
| 114 |
('PIE', 'CHORD'); |
| 115 |
|
| 116 |
{ TArc2DNode ----------------------------------------------------------------- } |
| 117 |
|
| 118 |
function TArc2DNode.LocalBoundingBox(State: TX3DGraphTraverseState; |
| 119 |
ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): TBox3D; |
| 120 |
begin |
| 121 |
{ Rendering of TArc2DNode not implemented. } |
| 122 |
Result := TBox3D.Empty; |
| 123 |
end; |
| 124 |
|
| 125 |
function TArc2DNode.VerticesCount(State: TX3DGraphTraverseState; |
| 126 |
ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): Cardinal; |
| 127 |
begin |
| 128 |
{ Rendering of TArc2DNode not implemented. } |
| 129 |
Result := 0; |
| 130 |
end; |
| 131 |
|
| 132 |
function TArc2DNode.TrianglesCount(State: TX3DGraphTraverseState; |
| 133 |
ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): Cardinal; |
| 134 |
begin |
| 135 |
{ Rendering of TArc2DNode not implemented. } |
| 136 |
Result := 0; |
| 137 |
end; |
| 138 |
|
| 139 |
{ TArcClose2DNode ----------------------------------------------------------------- } |
| 140 |
|
| 141 |
function TArcClose2DNode.LocalBoundingBox(State: TX3DGraphTraverseState; |
| 142 |
ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): TBox3D; |
| 143 |
begin |
| 144 |
{ Rendering of TArcClose2DNode not implemented. } |
| 145 |
Result := TBox3D.Empty; |
| 146 |
end; |
| 147 |
|
| 148 |
function TArcClose2DNode.VerticesCount(State: TX3DGraphTraverseState; |
| 149 |
ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): Cardinal; |
| 150 |
begin |
| 151 |
{ Rendering of TArcClose2DNode not implemented. } |
| 152 |
Result := 0; |
| 153 |
end; |
| 154 |
|
| 155 |
function TArcClose2DNode.TrianglesCount(State: TX3DGraphTraverseState; |
| 156 |
ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): Cardinal; |
| 157 |
begin |
| 158 |
{ Rendering of TArcClose2DNode not implemented. } |
| 159 |
Result := 0; |
| 160 |
end; |
| 161 |
|
| 162 |
function TArcClose2DNode.SolidField: TSFBool; |
| 163 |
begin |
| 164 |
Result := FdSolid; |
| 165 |
end; |
| 166 |
|
| 167 |
{ TCircle2DNode -------------------------------------------------------------- } |
| 168 |
|
| 169 |
function TCircle2DNode.CalculateSlices: Cardinal; |
| 170 |
begin |
| 171 |
// use default in case of 0 or invalid value |
| 172 |
if FdSlices.Value < MinTriangulationSlices then |
| 173 |
Result := DefaultTriangulationSlices |
| 174 |
else |
| 175 |
Result := FdSlices.Value; |
| 176 |
end; |
| 177 |
|
| 178 |
function TCircle2DNode.Proxy(var State: TX3DGraphTraverseState): TAbstractGeometryNode; |
| 179 |
var |
| 180 |
SlicesNow: Integer; |
| 181 |
C: TCoordinateNode; |
| 182 |
LineSet: TLineSetNode absolute Result; |
| 183 |
I: Integer; |
| 184 |
AngleSin, AngleCos: Single; |
| 185 |
begin |
| 186 |
LineSet := TLineSetNode.Create(X3DName, BaseUrl); |
| 187 |
try |
| 188 |
SlicesNow := CalculateSlices; |
| 189 |
|
| 190 |
C := TCoordinateNode.Create('', BaseUrl); |
| 191 |
C.FdPoint.Items.Count := SlicesNow + 1; |
| 192 |
for I := 0 to SlicesNow - 1 do |
| 193 |
begin |
| 194 |
SinCos((I / SlicesNow) * 2 * Pi, AngleSin, AngleCos); |
| 195 |
C.FdPoint.Items.L[I] := Vector3( |
| 196 |
AngleSin * FdRadius.Value, |
| 197 |
AngleCos * FdRadius.Value, |
| 198 |
0 |
| 199 |
); |
| 200 |
end; |
| 201 |
{ close the circle } |
| 202 |
C.FdPoint.Items.L[SlicesNow] := C.FdPoint.Items.L[0]; |
| 203 |
|
| 204 |
LineSet.Coord := C; |
| 205 |
LineSet.SetVertexCount([SlicesNow + 1]); |
| 206 |
except FreeAndNil(Result); raise end; |
| 207 |
end; |
| 208 |
|
| 209 |
{ TDisk2DNode ---------------------------------------------------------------- } |
| 210 |
|
| 211 |
function TDisk2DNode.CalculateSlices: Cardinal; |
| 212 |
begin |
| 213 |
// use default in case of 0 or invalid value |
| 214 |
if FdSlices.Value < MinTriangulationSlices then |
| 215 |
Result := DefaultTriangulationSlices |
| 216 |
else |
| 217 |
Result := FdSlices.Value; |
| 218 |
end; |
| 219 |
|
| 220 |
function TDisk2DNode.SolidField: TSFBool; |
| 221 |
begin |
| 222 |
Result := FdSolid; |
| 223 |
end; |
| 224 |
|
| 225 |
function TDisk2DNode.Proxy(var State: TX3DGraphTraverseState): TAbstractGeometryNode; |
| 226 |
var |
| 227 |
SlicesNow: Integer; |
| 228 |
|
| 229 |
{ Make a disc from triangles, no inner radius (no hole inside). } |
| 230 |
procedure ProxyTriangles; |
| 231 |
var |
| 232 |
C: TCoordinateNode; |
| 233 |
Triangles: TTriangleFanSetNode absolute Result; |
| 234 |
I: Integer; |
| 235 |
AngleSin, AngleCos: Float; |
| 236 |
begin |
| 237 |
Triangles := TTriangleFanSetNode.Create(X3DName, BaseUrl); |
| 238 |
try |
| 239 |
C := TCoordinateNode.Create('', BaseUrl); |
| 240 |
C.FdPoint.Items.Count := SlicesNow + 2; |
| 241 |
C.FdPoint.Items.L[0] := Vector3(0, 0, 0); |
| 242 |
for I := 0 to SlicesNow do |
| 243 |
begin |
| 244 |
SinCos((I / SlicesNow) * 2 * Pi, AngleSin, AngleCos); |
| 245 |
C.FdPoint.Items.L[I + 1] := Vector3( |
| 246 |
AngleCos * OuterRadius, |
| 247 |
AngleSin * OuterRadius, |
| 248 |
0 |
| 249 |
); |
| 250 |
end; |
| 251 |
|
| 252 |
Triangles.Coord := C; |
| 253 |
Triangles.SetFanCount([SlicesNow + 2]); |
| 254 |
Triangles.Solid := Solid; |
| 255 |
except FreeAndNil(Result); raise end; |
| 256 |
end; |
| 257 |
|
| 258 |
{ Make a disc from quads, with inner radius (hole inside). } |
| 259 |
procedure ProxyQuads; |
| 260 |
var |
| 261 |
C: TCoordinateNode; |
| 262 |
Quads: TIndexedQuadSetNode absolute Result; |
| 263 |
I: Integer; |
| 264 |
AngleSin, AngleCos: Float; |
| 265 |
Indexes: TInt32List; |
| 266 |
begin |
| 267 |
Quads := TIndexedQuadSetNode.Create(X3DName, BaseUrl); |
| 268 |
try |
| 269 |
C := TCoordinateNode.Create('', BaseUrl); |
| 270 |
C.FdPoint.Items.Count := SlicesNow * 2; |
| 271 |
for I := 0 to SlicesNow - 1 do |
| 272 |
begin |
| 273 |
SinCos((I / SlicesNow) * 2 * Pi, AngleSin, AngleCos); |
| 274 |
C.FdPoint.Items.L[I] := Vector3( |
| 275 |
AngleCos * OuterRadius, |
| 276 |
AngleSin * OuterRadius, |
| 277 |
0 |
| 278 |
); |
| 279 |
C.FdPoint.Items.L[I + SlicesNow] := Vector3( |
| 280 |
AngleCos * InnerRadius, |
| 281 |
AngleSin * InnerRadius, |
| 282 |
0 |
| 283 |
); |
| 284 |
end; |
| 285 |
|
| 286 |
Indexes := TInt32List.Create; |
| 287 |
try |
| 288 |
Indexes.Count := SlicesNow * 4; |
| 289 |
for I := 0 to SlicesNow - 1 do |
| 290 |
begin |
| 291 |
Indexes.L[I * 4 ] := I; |
| 292 |
Indexes.L[I * 4 + 1] := (I + 1) mod SlicesNow; |
| 293 |
Indexes.L[I * 4 + 2] := (I + 1) mod SlicesNow + SlicesNow; |
| 294 |
Indexes.L[I * 4 + 3] := I + SlicesNow; |
| 295 |
end; |
| 296 |
|
| 297 |
Quads.Coord := C; |
| 298 |
Quads.SetIndex(Indexes); |
| 299 |
Quads.Solid := Solid; |
| 300 |
finally FreeAndNil(Indexes); end; |
| 301 |
except FreeAndNil(Result); raise end; |
| 302 |
end; |
| 303 |
|
| 304 |
begin |
| 305 |
SlicesNow := CalculateSlices; |
| 306 |
|
| 307 |
if InnerRadius <= 0 then |
| 308 |
ProxyTriangles |
| 309 |
else |
| 310 |
ProxyQuads; |
| 311 |
end; |
| 312 |
|
| 313 |
{ TPolyline2DNode ------------------------------------------------------------ } |
| 314 |
|
| 315 |
constructor TPolyline2DNode.Create(const AX3DName, ABaseUrl: String); |
| 316 |
begin |
| 317 |
inherited; |
| 318 |
FdLineSegments.AddAlternativeName('point', 2); |
| 319 |
end; |
| 320 |
|
| 321 |
function TPolyline2DNode.Proxy(var State: TX3DGraphTraverseState): TAbstractGeometryNode; |
| 322 |
var |
| 323 |
C: TCoordinateNode; |
| 324 |
LineSet: TLineSetNode absolute Result; |
| 325 |
I, Count: Integer; |
| 326 |
begin |
| 327 |
LineSet := TLineSetNode.Create(X3DName, BaseUrl); |
| 328 |
try |
| 329 |
Count := FdLineSegments.Items.Count; |
| 330 |
|
| 331 |
C := TCoordinateNode.Create('', BaseUrl); |
| 332 |
C.FdPoint.Items.Count := Count; |
| 333 |
for I := 0 to Count - 1 do |
| 334 |
begin |
| 335 |
C.FdPoint.Items.L[I] := Vector3( |
| 336 |
FdLineSegments.Items.L[I].X, |
| 337 |
FdLineSegments.Items.L[I].Y, |
| 338 |
0 |
| 339 |
); |
| 340 |
end; |
| 341 |
|
| 342 |
LineSet.Coord := C; |
| 343 |
LineSet.SetVertexCount([Count]); |
| 344 |
except FreeAndNil(Result); raise end; |
| 345 |
end; |
| 346 |
|
| 347 |
{ TPolypoint2DNode ----------------------------------------------------------- } |
| 348 |
|
| 349 |
function TPolypoint2DNode.Proxy(var State: TX3DGraphTraverseState): TAbstractGeometryNode; |
| 350 |
var |
| 351 |
C: TCoordinateNode; |
| 352 |
PointSet: TPointSetNode absolute Result; |
| 353 |
I, Count: Integer; |
| 354 |
begin |
| 355 |
PointSet := TPointSetNode.Create(X3DName, BaseUrl); |
| 356 |
try |
| 357 |
Count := FdPoint.Items.Count; |
| 358 |
|
| 359 |
C := TCoordinateNode.Create('', BaseUrl); |
| 360 |
C.FdPoint.Items.Count := Count; |
| 361 |
for I := 0 to Count - 1 do |
| 362 |
begin |
| 363 |
C.FdPoint.Items.L[I] := Vector3( |
| 364 |
FdPoint.Items.L[I].X, |
| 365 |
FdPoint.Items.L[I].Y, |
| 366 |
0 |
| 367 |
); |
| 368 |
end; |
| 369 |
|
| 370 |
PointSet.Coord := C; |
| 371 |
except FreeAndNil(Result); raise end; |
| 372 |
end; |
| 373 |
|
| 374 |
{ TRectangle2DNode ----------------------------------------------------------- } |
| 375 |
|
| 376 |
function TRectangle2DNode.SolidField: TSFBool; |
| 377 |
begin |
| 378 |
Result := FdSolid; |
| 379 |
end; |
| 380 |
|
| 381 |
function Rectangle2DProxy(Original: TAbstractGeometryNode; |
| 382 |
const Size: TVector2; const Solid: boolean): TAbstractGeometryNode; |
| 383 |
var |
| 384 |
Coord: TCoordinateNode; |
| 385 |
TexCoord: TTextureCoordinateNode; |
| 386 |
QuadSet: TQuadSetNode absolute Result; |
| 387 |
begin |
| 388 |
QuadSet := TQuadSetNode.Create(Original.X3DName, Original.BaseUrl); |
| 389 |
try |
| 390 |
Coord := TCoordinateNode.Create('', Original.BaseUrl); |
| 391 |
Coord.FdPoint.Items.Count := 4; |
| 392 |
QuadSet.FdCoord.Value := Coord; |
| 393 |
|
| 394 |
TexCoord := TTextureCoordinateNode.Create('', Original.BaseUrl); |
| 395 |
TexCoord.FdPoint.Items.Count := 4; |
| 396 |
QuadSet.FdTexCoord.Value := TexCoord; |
| 397 |
|
| 398 |
{ calculate Coord, TexCoord contents } |
| 399 |
|
| 400 |
Coord.FdPoint.Items.L[0] := Vector3(-Size[0] / 2, -Size[1] / 2, 0); |
| 401 |
TexCoord.FdPoint.Items.L[0] := Vector2(0, 0); |
| 402 |
|
| 403 |
Coord.FdPoint.Items.L[1] := Vector3( Size[0] / 2, -Size[1] / 2, 0); |
| 404 |
TexCoord.FdPoint.Items.L[1] := Vector2(1, 0); |
| 405 |
|
| 406 |
Coord.FdPoint.Items.L[2] := Vector3( Size[0] / 2, Size[1] / 2, 0); |
| 407 |
TexCoord.FdPoint.Items.L[2] := Vector2(1, 1); |
| 408 |
|
| 409 |
Coord.FdPoint.Items.L[3] := Vector3(-Size[0] / 2, Size[1] / 2, 0); |
| 410 |
TexCoord.FdPoint.Items.L[3] := Vector2(0, 1); |
| 411 |
|
| 412 |
QuadSet.Solid := Solid; |
| 413 |
except FreeAndNil(Result); raise end; |
| 414 |
end; |
| 415 |
|
| 416 |
function TRectangle2DNode.Proxy(var State: TX3DGraphTraverseState): TAbstractGeometryNode; |
| 417 |
begin |
| 418 |
Result := Rectangle2DProxy(Self, FdSize.Value, FdSolid.Value); |
| 419 |
end; |
| 420 |
|
| 421 |
{ TTriangleSet2DNode --------------------------------------------------------- } |
| 422 |
|
| 423 |
function TTriangleSet2DNode.SolidField: TSFBool; |
| 424 |
begin |
| 425 |
Result := FdSolid; |
| 426 |
end; |
| 427 |
|
| 428 |
function TTriangleSet2DNode.Proxy(var State: TX3DGraphTraverseState): TAbstractGeometryNode; |
| 429 |
var |
| 430 |
C: TCoordinateNode; |
| 431 |
TriangleSet: TTriangleSetNode absolute Result; |
| 432 |
I, Count: Integer; |
| 433 |
begin |
| 434 |
TriangleSet := TTriangleSetNode.Create(X3DName, BaseUrl); |
| 435 |
try |
| 436 |
Count := FdVertices.Items.Count; |
| 437 |
|
| 438 |
C := TCoordinateNode.Create('', BaseUrl); |
| 439 |
C.FdPoint.Items.Count := Count; |
| 440 |
for I := 0 to Count - 1 do |
| 441 |
begin |
| 442 |
C.FdPoint.Items.L[I] := Vector3( |
| 443 |
FdVertices.Items.L[I].X, |
| 444 |
FdVertices.Items.L[I].Y, |
| 445 |
0 |
| 446 |
); |
| 447 |
end; |
| 448 |
|
| 449 |
TriangleSet.Coord := C; |
| 450 |
TriangleSet.Solid := Solid; |
| 451 |
except FreeAndNil(Result); raise end; |
| 452 |
end; |
| 453 |
|
| 454 |
{ registration --------------------------------------------------------------- } |
| 455 |
|
| 456 |
procedure RegisterGeometry2DNodes; |
| 457 |
begin |
| 458 |
NodesManager.RegisterNodeClasses([ |
| 459 |
TArc2DNode, |
| 460 |
TArcClose2DNode, |
| 461 |
TCircle2DNode, |
| 462 |
TDisk2DNode, |
| 463 |
TPolyline2DNode, |
| 464 |
TPolypoint2DNode, |
| 465 |
TRectangle2DNode, |
| 466 |
TTriangleSet2DNode |
| 467 |
]); |
| 468 |
end; |
| 469 |
|
| 470 |
{$endif read_implementation} |