| 1 |
{%MainUnit x3dnodes.pas} |
| 2 |
{ |
| 3 |
Copyright 2008-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 |
{ Abstract class for (most) nodes that are valid product structure children. } |
| 19 |
TAbstractProductStructureChildNode = class(TAbstractChildNode) |
| 20 |
{$I auto_generated_node_helpers/x3dnodes_x3dproductstructurechildnode.inc} |
| 21 |
end; |
| 22 |
|
| 23 |
{ Holds a set of assemblies or parts grouped together. } |
| 24 |
TCADAssemblyNode = class(TAbstractGroupingNode) |
| 25 |
protected |
| 26 |
function DirectEnumerateActive(Func: TEnumerateChildrenFunction): Pointer; override; |
| 27 |
{$I auto_generated_node_helpers/x3dnodes_cadassembly.inc} |
| 28 |
end; |
| 29 |
|
| 30 |
{ Holds the geometry representing a face of a part. } |
| 31 |
TCADFaceNode = class(TAbstractProductStructureChildNode) |
| 32 |
protected |
| 33 |
function DirectEnumerateActive(Func: TEnumerateChildrenFunction): Pointer; override; |
| 34 |
{$I auto_generated_node_helpers/x3dnodes_cadface.inc} |
| 35 |
end; |
| 36 |
|
| 37 |
{ Hierarchy of nodes used for showing layer structure for the CAD model. } |
| 38 |
TCADLayerNode = class(TAbstractGroupingNode) |
| 39 |
protected |
| 40 |
function DirectEnumerateActive(Func: TEnumerateChildrenFunction): Pointer; override; |
| 41 |
{$I auto_generated_node_helpers/x3dnodes_cadlayer.inc} |
| 42 |
end; |
| 43 |
|
| 44 |
{ Represents the location and faces that constitute a part in the CAD model. } |
| 45 |
TCADPartNode = class(TAbstractGroupingNode) |
| 46 |
strict private |
| 47 |
type |
| 48 |
TCADPartNodeTransformFunctionality = class(TTransformFunctionality) |
| 49 |
strict private |
| 50 |
FParent: TCADPartNode; |
| 51 |
public |
| 52 |
constructor Create(const AParent: TCADPartNode); |
| 53 |
property Parent: TCADPartNode read FParent; |
| 54 |
procedure ApplyTransform(var Transformation: TTransformation); override; |
| 55 |
end; |
| 56 |
var |
| 57 |
FTransformFunctionality: TCADPartNodeTransformFunctionality; |
| 58 |
protected |
| 59 |
function DirectEnumerateActive(Func: TEnumerateChildrenFunction): Pointer; override; |
| 60 |
public |
| 61 |
constructor Create(const AX3DName: String = ''; const ABaseUrl: String = ''); override; |
| 62 |
function TransformationChange: TNodeTransformationChange; override; |
| 63 |
{$I auto_generated_node_helpers/x3dnodes_cadpart.inc} |
| 64 |
end; |
| 65 |
|
| 66 |
{ Quads. } |
| 67 |
TIndexedQuadSetNode = class(TAbstractComposedGeometryNode) |
| 68 |
public |
| 69 |
function CoordIndexField: TMFLong; override; |
| 70 |
|
| 71 |
procedure InternalCoordPolygons( |
| 72 |
State: TX3DGraphTraverseState; |
| 73 |
PolygonHandler: TIndexedPolygonHandler); override; |
| 74 |
|
| 75 |
function TrianglesCount(State: TX3DGraphTraverseState; |
| 76 |
ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): Cardinal; override; |
| 77 |
|
| 78 |
{$I auto_generated_node_helpers/x3dnodes_indexedquadset.inc} |
| 79 |
end; |
| 80 |
|
| 81 |
{ Quads, not indexed. } |
| 82 |
TQuadSetNode = class(TAbstractComposedGeometryNode) |
| 83 |
strict private |
| 84 |
FInternalTrianglesCoordIndex: TMFLong; |
| 85 |
public |
| 86 |
constructor Create(const AX3DName: String = ''; const ABaseUrl: String = ''); override; |
| 87 |
destructor Destroy; override; |
| 88 |
|
| 89 |
{ Internal TMFLong field that allows to treat the TQuadSetNode essentially like |
| 90 |
TIndexedTriangleSetNode. That is, these indexes have 3 items for each triangle, |
| 91 |
6 items for each quad. You can use them to index all the vertex coordinates |
| 92 |
and per-vertex attributes, and thus render TQuadSetNode just like you would render |
| 93 |
TIndexedTriangleSetNode. } |
| 94 |
function InternalTrianglesCoordIndex: TMFLong; |
| 95 |
|
| 96 |
procedure InternalCoordPolygons( |
| 97 |
State: TX3DGraphTraverseState; |
| 98 |
PolygonHandler: TIndexedPolygonHandler); override; |
| 99 |
|
| 100 |
function TrianglesCount(State: TX3DGraphTraverseState; |
| 101 |
ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): Cardinal; override; |
| 102 |
|
| 103 |
{$I auto_generated_node_helpers/x3dnodes_quadset.inc} |
| 104 |
end; |
| 105 |
|
| 106 |
{$endif read_interface} |
| 107 |
|
| 108 |
{$ifdef read_implementation} |
| 109 |
function TCADAssemblyNode.DirectEnumerateActive(Func: TEnumerateChildrenFunction): Pointer; |
| 110 |
begin |
| 111 |
Result := FdChildren.Enumerate(Func); |
| 112 |
if Result <> nil then Exit; |
| 113 |
end; |
| 114 |
|
| 115 |
function TCADFaceNode.DirectEnumerateActive(Func: TEnumerateChildrenFunction): Pointer; |
| 116 |
begin |
| 117 |
Result := FdShape.Enumerate(Func); |
| 118 |
if Result <> nil then Exit; |
| 119 |
end; |
| 120 |
|
| 121 |
function TCADLayerNode.DirectEnumerateActive(Func: TEnumerateChildrenFunction): Pointer; |
| 122 |
var |
| 123 |
I: Integer; |
| 124 |
begin |
| 125 |
Result := inherited; |
| 126 |
if Result <> nil then Exit; |
| 127 |
|
| 128 |
for I := 0 to FdChildren.Count - 1 do |
| 129 |
begin |
| 130 |
Result := Func(Self, FdChildren[I]); |
| 131 |
if Result <> nil then Exit; |
| 132 |
end; |
| 133 |
end; |
| 134 |
|
| 135 |
{ TCADPartNode.TCADPartNodeTransformFunctionality ----------------------------------------- } |
| 136 |
|
| 137 |
constructor TCADPartNode.TCADPartNodeTransformFunctionality.Create(const AParent: TCADPartNode); |
| 138 |
begin |
| 139 |
inherited Create(AParent); |
| 140 |
FParent := AParent; |
| 141 |
end; |
| 142 |
|
| 143 |
procedure TCADPartNode.TCADPartNodeTransformFunctionality.ApplyTransform(var Transformation: TTransformation); |
| 144 |
begin |
| 145 |
inherited; |
| 146 |
Transformation.Multiply( |
| 147 |
Parent.Center, |
| 148 |
Parent.Rotation, |
| 149 |
Parent.Scale, |
| 150 |
Parent.ScaleOrientation, |
| 151 |
Parent.Translation); |
| 152 |
end; |
| 153 |
|
| 154 |
{ TCADPartNode --------------------------------------------------------------- } |
| 155 |
|
| 156 |
constructor TCADPartNode.Create(const AX3DName, ABaseUrl: String); |
| 157 |
begin |
| 158 |
inherited; |
| 159 |
FTransformFunctionality := TCADPartNodeTransformFunctionality.Create(Self); |
| 160 |
AddFunctionality(FTransformFunctionality); |
| 161 |
end; |
| 162 |
|
| 163 |
function TCADPartNode.DirectEnumerateActive(Func: TEnumerateChildrenFunction): Pointer; |
| 164 |
begin |
| 165 |
Result := FdChildren.Enumerate(Func); |
| 166 |
if Result <> nil then Exit; |
| 167 |
end; |
| 168 |
|
| 169 |
function TCADPartNode.TransformationChange: TNodeTransformationChange; |
| 170 |
begin |
| 171 |
Result := ntcTransform; |
| 172 |
end; |
| 173 |
|
| 174 |
function TIndexedQuadSetNode.CoordIndexField: TMFLong; |
| 175 |
begin |
| 176 |
Result := FdIndex; |
| 177 |
end; |
| 178 |
|
| 179 |
{ TQuadSetNode --------------------------------------------------------------- } |
| 180 |
|
| 181 |
constructor TQuadSetNode.Create(const AX3DName: String = ''; const ABaseUrl: String = ''); |
| 182 |
begin |
| 183 |
inherited; |
| 184 |
FInternalTrianglesCoordIndex := TMFLong.Create(Self, false, 'internalTrianglesCoordIndex', []); |
| 185 |
end; |
| 186 |
|
| 187 |
destructor TQuadSetNode.Destroy; |
| 188 |
begin |
| 189 |
FreeAndNil(FInternalTrianglesCoordIndex); |
| 190 |
inherited; |
| 191 |
end; |
| 192 |
|
| 193 |
function TQuadSetNode.InternalTrianglesCoordIndex: TMFLong; |
| 194 |
var |
| 195 |
QuadsCount, OldQuadsCount, I: TListSize; |
| 196 |
begin |
| 197 |
if FdCoord.Value is TCoordinateNode then |
| 198 |
QuadsCount := TCoordinateNode(FdCoord.Value).FdPoint.Items.Count div 4 |
| 199 |
else |
| 200 |
QuadsCount := 0; |
| 201 |
|
| 202 |
if FInternalTrianglesCoordIndex.Count <> QuadsCount * 6 then |
| 203 |
begin |
| 204 |
OldQuadsCount := FInternalTrianglesCoordIndex.Count div 6; |
| 205 |
FInternalTrianglesCoordIndex.Count := QuadsCount * 6; |
| 206 |
for I := OldQuadsCount to QuadsCount - 1 do |
| 207 |
begin |
| 208 |
FInternalTrianglesCoordIndex.Items.L[I * 6 ] := I * 4; |
| 209 |
FInternalTrianglesCoordIndex.Items.L[I * 6 + 1] := I * 4 + 1; |
| 210 |
FInternalTrianglesCoordIndex.Items.L[I * 6 + 2] := I * 4 + 2; |
| 211 |
|
| 212 |
FInternalTrianglesCoordIndex.Items.L[I * 6 + 3] := I * 4; |
| 213 |
FInternalTrianglesCoordIndex.Items.L[I * 6 + 4] := I * 4 + 2; |
| 214 |
FInternalTrianglesCoordIndex.Items.L[I * 6 + 5] := I * 4 + 3; |
| 215 |
end; |
| 216 |
end; |
| 217 |
|
| 218 |
Result := FInternalTrianglesCoordIndex; |
| 219 |
end; |
| 220 |
|
| 221 |
procedure RegisterCADGeometryNodes; |
| 222 |
begin |
| 223 |
NodesManager.RegisterNodeClasses([ |
| 224 |
TCADAssemblyNode, |
| 225 |
TCADFaceNode, |
| 226 |
TCADLayerNode, |
| 227 |
TCADPartNode, |
| 228 |
TIndexedQuadSetNode, |
| 229 |
TQuadSetNode |
| 230 |
]); |
| 231 |
end; |
| 232 |
|
| 233 |
{$endif read_implementation} |