| 1 |
{%MainUnit x3dnodes.pas} |
| 2 |
{ |
| 3 |
Copyright 2008-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 |
{ Nodes that are defined by InstantReality extensions, see |
| 18 |
[http://instant-reality.com/] and in particular the specifications on |
| 19 |
[http://instant-reality.com/documentation/nodetype/]. } |
| 20 |
|
| 21 |
{$ifdef read_interface} |
| 22 |
{ Transform children by an explicit 4x4 matrix. |
| 23 |
|
| 24 |
This is an extension, not present in the X3D standard. |
| 25 |
See https://castle-engine.io/x3d_extensions.php#section_ext_matrix_transform . |
| 26 |
It is compatible with InstantReality, |
| 27 |
see http://instant-reality.com/documentation/nodetype/MatrixTransform/ . } |
| 28 |
TMatrixTransformNode = class(TAbstractGroupingNode) |
| 29 |
strict private |
| 30 |
type |
| 31 |
TMatrixTransformNodeTransformFunctionality = class(TTransformFunctionality) |
| 32 |
strict private |
| 33 |
FParent: TMatrixTransformNode; |
| 34 |
public |
| 35 |
constructor Create(const AParent: TMatrixTransformNode); |
| 36 |
property Parent: TMatrixTransformNode read FParent; |
| 37 |
procedure ApplyTransform(var Transformation: TTransformation); override; |
| 38 |
end; |
| 39 |
var |
| 40 |
FTransformFunctionality: TMatrixTransformNodeTransformFunctionality; |
| 41 |
protected |
| 42 |
function DirectEnumerateActive(Func: TEnumerateChildrenFunction): Pointer; override; |
| 43 |
public |
| 44 |
constructor Create(const AX3DName: string = ''; const ABaseUrl: String = ''); override; |
| 45 |
class function ForVRMLVersion(const Version: TX3DVersion): boolean; override; |
| 46 |
function TransformationChange: TNodeTransformationChange; override; |
| 47 |
{$I auto_generated_node_helpers/x3dnodes_matrixtransform.inc} |
| 48 |
end; |
| 49 |
TMatrixTransformNode_2 = TMatrixTransformNode; |
| 50 |
|
| 51 |
{ Teapot geometry, useful for testing purposes. |
| 52 |
See https://castle-engine.io/x3d_extensions.php#section_ext_teapot . } |
| 53 |
TTeapotNode = class(TAbstractGeometryNode) |
| 54 |
protected |
| 55 |
function DirectEnumerateActive(Func: TEnumerateChildrenFunction): Pointer; override; |
| 56 |
public |
| 57 |
constructor Create(const AX3DName: string = ''; const ABaseUrl: String = ''); override; |
| 58 |
function Proxy(var State: TX3DGraphTraverseState): TAbstractGeometryNode; override; |
| 59 |
function TexCoordField: TSFNode; override; |
| 60 |
function AutoGenerate3DTexCoords: boolean; override; |
| 61 |
function SolidField: TSFBool; override; |
| 62 |
|
| 63 |
{$I auto_generated_node_helpers/x3dnodes_teapot.inc} |
| 64 |
end; |
| 65 |
|
| 66 |
{ X3D events logging. |
| 67 |
See https://castle-engine.io/x3d_extensions.php#section_ext_logger . } |
| 68 |
TLoggerNode = class(TAbstractChildNode) |
| 69 |
strict private |
| 70 |
LogFileStream: TStream; |
| 71 |
procedure EventWriteReceive( |
| 72 |
const Event: TX3DEvent; const Value: TX3DField; const Time: TX3DTime); |
| 73 |
public |
| 74 |
constructor Create(const AX3DName: string = ''; const ABaseUrl: String = ''); override; |
| 75 |
destructor Destroy; override; |
| 76 |
{$I auto_generated_node_helpers/x3dnodes_logger.inc} |
| 77 |
end; |
| 78 |
|
| 79 |
TConverterNode = class(TAbstractChildNode) |
| 80 |
strict private |
| 81 |
procedure EventInReceive( |
| 82 |
const Event: TX3DEvent; const Value: TX3DField; const Time: TX3DTime); |
| 83 |
public |
| 84 |
constructor Create(const AX3DName, ABaseUrl: String); override; |
| 85 |
{$I auto_generated_node_helpers/x3dnodes_converter.inc} |
| 86 |
end; |
| 87 |
|
| 88 |
{ Texture with contents created by rendering the scene from a specific |
| 89 |
viewpoint. } |
| 90 |
TRenderedTextureNode = class(TAbstractSingleTextureNode) |
| 91 |
strict private |
| 92 |
type |
| 93 |
TRenderedTextureGenTex = class(TGeneratedTextureFunctionality) |
| 94 |
strict private |
| 95 |
FParent: TRenderedTextureNode; |
| 96 |
protected |
| 97 |
function GetUpdate: TTextureUpdate; override; |
| 98 |
procedure SetUpdate(const Value: TTextureUpdate); override; |
| 99 |
public |
| 100 |
constructor Create(const AParent: TRenderedTextureNode); |
| 101 |
end; |
| 102 |
var |
| 103 |
FGenTexFunctionality: TRenderedTextureGenTex; |
| 104 |
public |
| 105 |
constructor Create(const AX3DName: String = ''; const ABaseUrl: String = ''); override; |
| 106 |
{$I auto_generated_node_helpers/x3dnodes_renderedtexture.inc} |
| 107 |
end; |
| 108 |
|
| 109 |
{ Alternative (deprecated) version of TRectangle2DNode, |
| 110 |
compatible with InstantReality X3D browser. |
| 111 |
@deprecated } |
| 112 |
TPlaneNode = class(TAbstractGeometryNode) |
| 113 |
public |
| 114 |
function Proxy(var State: TX3DGraphTraverseState): TAbstractGeometryNode; override; |
| 115 |
function SolidField: TSFBool; override; |
| 116 |
|
| 117 |
{$I auto_generated_node_helpers/x3dnodes_plane.inc} |
| 118 |
end; |
| 119 |
|
| 120 |
{ Utility for setting and observing a boolean value in various ways. } |
| 121 |
TTogglerNode = class(TAbstractChildNode) |
| 122 |
strict private |
| 123 |
function IgnoreWhenDisabled(const ChangingField: TX3DField; const Event: TX3DEvent; |
| 124 |
const Value: TX3DField; const Time: TX3DTime): Boolean; |
| 125 |
procedure EventStatusReceive( |
| 126 |
const Event: TX3DEvent; const Value: TX3DField; const Time: TX3DTime); |
| 127 |
procedure EventNotStatusReceive( |
| 128 |
const Event: TX3DEvent; const Value: TX3DField; const Time: TX3DTime); |
| 129 |
procedure EventToggleReceive( |
| 130 |
const Event: TX3DEvent; const Value: TX3DField; const Time: TX3DTime); |
| 131 |
procedure EventSetReceive( |
| 132 |
const Event: TX3DEvent; const Value: TX3DField; const Time: TX3DTime); |
| 133 |
procedure EventResetReceive( |
| 134 |
const Event: TX3DEvent; const Value: TX3DField; const Time: TX3DTime); |
| 135 |
public |
| 136 |
constructor Create(const AX3DName: string = ''; const ABaseUrl: String = ''); override; |
| 137 |
{$I auto_generated_node_helpers/x3dnodes_toggler.inc} |
| 138 |
end; |
| 139 |
|
| 140 |
{ Advanced material that can be heavily configured using textures |
| 141 |
(specular maps, normal maps and much more). } |
| 142 |
TCommonSurfaceShaderNode = class(TAbstractShaderNode) |
| 143 |
strict private |
| 144 |
type |
| 145 |
TCommonSurfaceShaderInfo = class(TPhongMaterialInfo) |
| 146 |
strict private |
| 147 |
FNode: TCommonSurfaceShaderNode; |
| 148 |
protected |
| 149 |
function GetDiffuseColor: TVector3; override; |
| 150 |
procedure SetDiffuseColor(const Value: TVector3); override; |
| 151 |
function GetSpecularColor: TVector3; override; |
| 152 |
procedure SetSpecularColor(const Value: TVector3); override; |
| 153 |
function GetEmissiveColor: TVector3; override; |
| 154 |
procedure SetEmissiveColor(const Value: TVector3); override; |
| 155 |
public |
| 156 |
constructor Create(ANode: TCommonSurfaceShaderNode); |
| 157 |
|
| 158 |
function AmbientColor: TVector3; override; |
| 159 |
function Shininess: Single; override; |
| 160 |
function ReflectionColor: TVector3; override; |
| 161 |
function TransmissionColor: TVector3; override; |
| 162 |
function Transparency: Single; override; |
| 163 |
end; |
| 164 |
var |
| 165 |
FMaterialInfo: TMaterialInfo; |
| 166 |
public |
| 167 |
destructor Destroy; override; |
| 168 |
|
| 169 |
{ Shininess expressed as an exponent for shading equations. |
| 170 |
This is just @link(ShininessFactor) * 128. } |
| 171 |
function ShininessFactorExp: Single; |
| 172 |
|
| 173 |
{ Transparency determined by the AlphaFactor. |
| 174 |
This is just 1 - @link(AlphaFactor). } |
| 175 |
function Transparency: Single; |
| 176 |
|
| 177 |
{ Material information based on this node. |
| 178 |
It is automatically updated when properties of this material change. |
| 179 |
Do not free it yourself, it will be automatically freed when |
| 180 |
this node is freed. } |
| 181 |
function MaterialInfo: TMaterialInfo; |
| 182 |
|
| 183 |
{$I auto_generated_node_helpers/x3dnodes_commonsurfaceshader.inc} |
| 184 |
end; |
| 185 |
|
| 186 |
{ Display a 2D image as a background. } |
| 187 |
TImageBackgroundNode = class(TAbstractBackgroundNode) |
| 188 |
protected |
| 189 |
function DirectEnumerateActive(Func: TEnumerateChildrenFunction): Pointer; override; |
| 190 |
{$I auto_generated_node_helpers/x3dnodes_imagebackground.inc} |
| 191 |
end; |
| 192 |
|
| 193 |
{ Not implemented: |
| 194 |
Bindable node to setup rendering and culling parameters. |
| 195 |
|
| 196 |
Compatible with X3DOM. |
| 197 |
Not implemented in Castle Game Engine yet, we merely parse it now, |
| 198 |
but don't do anything with obtained information. |
| 199 |
|
| 200 |
See https://doc.x3dom.org/author/EnvironmentalEffects/Environment.html } |
| 201 |
TEnvironmentNode = class(TAbstractBindableNode) |
| 202 |
{$I auto_generated_node_helpers/x3dnodes_environment.inc} |
| 203 |
end; |
| 204 |
{$endif read_interface} |
| 205 |
|
| 206 |
{$ifdef read_implementation} |
| 207 |
|
| 208 |
const |
| 209 |
ToneMappingNames: array [TToneMapping] of String = ( |
| 210 |
'NONE', |
| 211 |
'UNCHARTED', |
| 212 |
'HEJL-RICHARD', |
| 213 |
'ACES' |
| 214 |
); |
| 215 |
|
| 216 |
{ TMatrixTransformNode.TMatrixTransformNodeTransformFunctionality --------------------------- } |
| 217 |
|
| 218 |
constructor TMatrixTransformNode.TMatrixTransformNodeTransformFunctionality. |
| 219 |
Create(const AParent: TMatrixTransformNode); |
| 220 |
begin |
| 221 |
inherited Create(AParent); |
| 222 |
FParent := AParent; |
| 223 |
end; |
| 224 |
|
| 225 |
procedure TMatrixTransformNode.TMatrixTransformNodeTransformFunctionality. |
| 226 |
ApplyTransform(var Transformation: TTransformation); |
| 227 |
begin |
| 228 |
inherited; |
| 229 |
Transformation.MultiplyMatrix(Parent.Matrix); |
| 230 |
end; |
| 231 |
|
| 232 |
{ TMatrixTransformNode --------------------------------------------------------------- } |
| 233 |
|
| 234 |
constructor TMatrixTransformNode.Create(const AX3DName, ABaseUrl: String); |
| 235 |
begin |
| 236 |
inherited; |
| 237 |
FdMatrix.ChangeAlways := chTransform; |
| 238 |
|
| 239 |
FTransformFunctionality := TMatrixTransformNodeTransformFunctionality.Create(Self); |
| 240 |
AddFunctionality(FTransformFunctionality); |
| 241 |
end; |
| 242 |
|
| 243 |
class function TMatrixTransformNode.ForVRMLVersion(const Version: TX3DVersion): boolean; |
| 244 |
begin |
| 245 |
Result := Version.Major >= 2; |
| 246 |
end; |
| 247 |
|
| 248 |
function TMatrixTransformNode.DirectEnumerateActive(Func: TEnumerateChildrenFunction): Pointer; |
| 249 |
var |
| 250 |
I: Integer; |
| 251 |
begin |
| 252 |
Result := nil; |
| 253 |
for I := 0 to FdChildren.Count - 1 do |
| 254 |
begin |
| 255 |
Result := Func(Self, FdChildren[I]); |
| 256 |
if Result <> nil then Exit; |
| 257 |
end; |
| 258 |
end; |
| 259 |
|
| 260 |
function TMatrixTransformNode.TransformationChange: TNodeTransformationChange; |
| 261 |
begin |
| 262 |
Result := ntcTransform; |
| 263 |
end; |
| 264 |
|
| 265 |
constructor TTeapotNode.Create(const AX3DName: string = ''; const ABaseUrl: String = ''); |
| 266 |
begin |
| 267 |
inherited; |
| 268 |
FdSize.ChangeAlways := chGeometry; |
| 269 |
FdSolid.ChangeAlways := chGeometry; |
| 270 |
FdManifold.ChangeAlways := chGeometry; |
| 271 |
FdTexCoord.ChangeAlways := chGeometry; |
| 272 |
end; |
| 273 |
|
| 274 |
function TTeapotNode.Proxy(var State: TX3DGraphTraverseState): TAbstractGeometryNode; |
| 275 |
const |
| 276 |
// Uncomment these if you need to temporarily compile without |
| 277 |
// auto_generated_teapot/teapot.inc. |
| 278 |
{ |
| 279 |
TeapotCoord: array [0..0] of TVector3 = ((X: 0; Y: 0; Z: 0)); |
| 280 |
TeapotCoordIndex: array [0..0] of Int32 = (0); |
| 281 |
TeapotManifoldCoord: array [0..0] of TVector3 = ((X: 0; Y: 0; Z: 0)); |
| 282 |
TeapotManifoldCoordIndex: array [0..0] of Int32 = (0); |
| 283 |
} |
| 284 |
{$I auto_generated_teapot/teapot.inc} |
| 285 |
var |
| 286 |
CoordNode: TCoordinateNode; |
| 287 |
IFS: TIndexedFaceSetNode absolute Result; |
| 288 |
begin |
| 289 |
IFS := TIndexedFaceSetNode.Create(X3DName, BaseUrl); |
| 290 |
try |
| 291 |
CoordNode := TCoordinateNode.Create('', BaseUrl); |
| 292 |
IFS.FdCoord.Value := CoordNode; |
| 293 |
if FdManifold.Value then |
| 294 |
CoordNode.SetPoint(TeapotManifoldCoord) |
| 295 |
else |
| 296 |
CoordNode.SetPoint(TeapotCoord); |
| 297 |
|
| 298 |
{ Scale Coords according to "size" field. |
| 299 |
Assumes that original coords are good for default size (3, 3, 3). } |
| 300 |
if not FdSize.EqualsDefaultValue then |
| 301 |
begin |
| 302 |
CoordNode.FdPoint.Items.MultiplyComponents(FdSize.Value * 1/3); |
| 303 |
end; |
| 304 |
|
| 305 |
if FdManifold.Value then |
| 306 |
IFS.FdCoordIndex.Items.AddRange(TeapotManifoldCoordIndex) |
| 307 |
else |
| 308 |
IFS.FdCoordIndex.Items.AddRange(TeapotCoordIndex); |
| 309 |
|
| 310 |
IFS.FdSolid.Value := FdSolid.Value; |
| 311 |
|
| 312 |
if FdTexCoord.CurrentChildAllowed then |
| 313 |
IFS.FdTexCoord.Value := FdTexCoord.Value; |
| 314 |
|
| 315 |
{ Set CreaseAngle to anything larger than Pi, to make this completely |
| 316 |
smooth later using fast CreateSmoothNormalsCoordinateNode routine. } |
| 317 |
IFS.FdCreaseAngle.Value := 4; |
| 318 |
except FreeAndNil(Result); raise end; |
| 319 |
end; |
| 320 |
|
| 321 |
function TTeapotNode.SolidField: TSFBool; |
| 322 |
begin |
| 323 |
Result := FdSolid; |
| 324 |
end; |
| 325 |
|
| 326 |
function TTeapotNode.DirectEnumerateActive(Func: TEnumerateChildrenFunction): Pointer; |
| 327 |
begin |
| 328 |
Result := inherited; |
| 329 |
if Result <> nil then Exit; |
| 330 |
|
| 331 |
Result := FdtexCoord.Enumerate(Func); |
| 332 |
if Result <> nil then Exit; |
| 333 |
end; |
| 334 |
|
| 335 |
function TTeapotNode.TexCoordField: TSFNode; |
| 336 |
begin |
| 337 |
Result := FdTexCoord; |
| 338 |
end; |
| 339 |
|
| 340 |
function TTeapotNode.AutoGenerate3DTexCoords: boolean; |
| 341 |
begin |
| 342 |
Result := (FdTexCoord.Value = nil) or not FdTexCoord.CurrentChildAllowed; |
| 343 |
end; |
| 344 |
|
| 345 |
constructor TLoggerNode.Create(const AX3DName, ABaseUrl: String); |
| 346 |
begin |
| 347 |
inherited; |
| 348 |
FEventWrite.AddNotification({$ifdef FPC}@{$endif} EventWriteReceive); |
| 349 |
end; |
| 350 |
|
| 351 |
destructor TLoggerNode.Destroy; |
| 352 |
begin |
| 353 |
FreeAndNil(LogFileStream); |
| 354 |
inherited; |
| 355 |
end; |
| 356 |
|
| 357 |
procedure TLoggerNode.EventWriteReceive( |
| 358 |
const Event: TX3DEvent; const Value: TX3DField; const Time: TX3DTime); |
| 359 |
|
| 360 |
function FieldValueToString(const Field: TX3DField): String; |
| 361 |
var |
| 362 |
Writer: TX3DWriterNames; |
| 363 |
StringStream: TStringStream; |
| 364 |
begin |
| 365 |
StringStream := TStringStream.Create(''); |
| 366 |
try |
| 367 |
Writer := TX3DWriterNames.Create(StringStream, X3DVersion, xeClassic); |
| 368 |
try |
| 369 |
Field.FieldSaveToStream(Writer, true); |
| 370 |
// Remove trailing newline |
| 371 |
Result := TrimRight(StringStream.DataString); |
| 372 |
finally FreeAndNil(Writer) end; |
| 373 |
finally FreeAndNil(StringStream) end; |
| 374 |
end; |
| 375 |
|
| 376 |
procedure WritelnLogMessage; |
| 377 |
var |
| 378 |
S: string; |
| 379 |
begin |
| 380 |
if FdLevel.Value >= 2 then |
| 381 |
begin |
| 382 |
S := FormatDot('Logger "%s": received field "%s" (%s). Time: %f.', |
| 383 |
[X3DName, Value.X3DName, Value.X3DType, Time.Seconds]); |
| 384 |
|
| 385 |
if (FdLevel.Value >= 3) and (Value.ParentNode <> nil) then |
| 386 |
begin |
| 387 |
S := S + FormatDot(' Sending node: "%s" (%s).', |
| 388 |
[(Value.ParentNode as TX3DNode).X3DName, |
| 389 |
(Value.ParentNode as TX3DNode).X3DType]); |
| 390 |
end; |
| 391 |
|
| 392 |
S := S + ' Value: ' + FieldValueToString(Value); |
| 393 |
|
| 394 |
if LogFileStream <> nil then |
| 395 |
WriteLnStr(LogFileStream, S) |
| 396 |
else |
| 397 |
WriteLnLog(S); |
| 398 |
end; |
| 399 |
end; |
| 400 |
|
| 401 |
begin |
| 402 |
if FdEnabled.Value and (Fdlevel.Value > 0) then |
| 403 |
begin |
| 404 |
if (LogFileStream = nil) and (FdLogFile.Value <> '') then |
| 405 |
begin |
| 406 |
LogFileStream := TFileStream.Create( |
| 407 |
FileNameAutoInc(ApplicationName + '_logger_' + |
| 408 |
DeleteURIExt(ExtractURIName(FdLogFile.Value)) + '_%d.log'), fmCreate); |
| 409 |
end; |
| 410 |
|
| 411 |
WritelnLogMessage; |
| 412 |
end; |
| 413 |
end; |
| 414 |
|
| 415 |
{ TConverterNode ------------------------------------------------------------- } |
| 416 |
|
| 417 |
constructor TConverterNode.Create(const AX3DName, ABaseUrl: String); |
| 418 |
begin |
| 419 |
inherited; |
| 420 |
FEventIn.AddNotification({$ifdef FPC}@{$endif} EventInReceive); |
| 421 |
end; |
| 422 |
|
| 423 |
procedure TConverterNode.EventInReceive( |
| 424 |
const Event: TX3DEvent; const Value: TX3DField; const Time: TX3DTime); |
| 425 |
begin |
| 426 |
if FdEnabled.Value then |
| 427 |
{ TODO: we cannot convert here (as we don't know to what we should |
| 428 |
convert...) Conversion must be done along the route, when destination |
| 429 |
is known. } |
| 430 |
EventOut.Send(Value, Time); |
| 431 |
end; |
| 432 |
|
| 433 |
{ TRenderedTextureNode.TRenderedTextureGenTex -------------------------------- } |
| 434 |
|
| 435 |
constructor TRenderedTextureNode.TRenderedTextureGenTex.Create(const AParent: TRenderedTextureNode); |
| 436 |
begin |
| 437 |
inherited Create(AParent); |
| 438 |
FParent := AParent; |
| 439 |
end; |
| 440 |
|
| 441 |
function TRenderedTextureNode.TRenderedTextureGenTex.GetUpdate: TTextureUpdate; |
| 442 |
begin |
| 443 |
Result := FParent.Update; |
| 444 |
end; |
| 445 |
|
| 446 |
procedure TRenderedTextureNode.TRenderedTextureGenTex.SetUpdate(const Value: TTextureUpdate); |
| 447 |
begin |
| 448 |
FParent.Update := Value; |
| 449 |
end; |
| 450 |
|
| 451 |
{ TRenderedTextureNode ------------------------------------------------------- } |
| 452 |
|
| 453 |
constructor TRenderedTextureNode.Create(const AX3DName, ABaseUrl: String); |
| 454 |
begin |
| 455 |
inherited; |
| 456 |
FGenTexFunctionality := TRenderedTextureGenTex.Create(Self); |
| 457 |
AddFunctionality(FGenTexFunctionality); |
| 458 |
end; |
| 459 |
|
| 460 |
{ TPlaneNode ----------------------------------------------------------------- } |
| 461 |
|
| 462 |
function TPlaneNode.Proxy(var State: TX3DGraphTraverseState): TAbstractGeometryNode; |
| 463 |
begin |
| 464 |
Result := Rectangle2DProxy(Self, FdSize.Value, FdSolid.Value); |
| 465 |
end; |
| 466 |
|
| 467 |
function TPlaneNode.SolidField: TSFBool; |
| 468 |
begin |
| 469 |
Result := FdSolid; |
| 470 |
end; |
| 471 |
|
| 472 |
{ TTogglerNode --------------------------------------------------------------- } |
| 473 |
|
| 474 |
constructor TTogglerNode.Create(const AX3DName, ABaseUrl: String); |
| 475 |
begin |
| 476 |
inherited; |
| 477 |
|
| 478 |
FdStatus.AddNotification({$ifdef FPC}@{$endif} EventStatusReceive); |
| 479 |
FdNotstatus.AddNotification({$ifdef FPC}@{$endif} EventNotStatusReceive); |
| 480 |
FEventToggle.AddNotification({$ifdef FPC}@{$endif} EventToggleReceive); |
| 481 |
EventSet.AddNotification({$ifdef FPC}@{$endif} EventSetReceive); |
| 482 |
EventReset.AddNotification({$ifdef FPC}@{$endif} EventResetReceive); |
| 483 |
|
| 484 |
FdStatus.OnInputIgnore := {$ifdef FPC}@{$endif} IgnoreWhenDisabled; |
| 485 |
FdNotStatus.OnInputIgnore := {$ifdef FPC}@{$endif} IgnoreWhenDisabled; |
| 486 |
end; |
| 487 |
|
| 488 |
function TTogglerNode.IgnoreWhenDisabled(const ChangingField: TX3DField; const Event: TX3DEvent; |
| 489 |
const Value: TX3DField; const Time: TX3DTime): Boolean; |
| 490 |
begin |
| 491 |
{ This makes status and notStatus input events ignored, |
| 492 |
and output events not generated when enabled = FALSE. } |
| 493 |
Result := not Enabled; |
| 494 |
end; |
| 495 |
|
| 496 |
procedure TTogglerNode.EventStatusReceive( |
| 497 |
const Event: TX3DEvent; const Value: TX3DField; const Time: TX3DTime); |
| 498 |
begin |
| 499 |
{ Do not send event to notStatus, if it already has correct value |
| 500 |
(so our own value didn't actually changed). |
| 501 |
This avoids calling status and notStatus events in a loop. |
| 502 |
By the way, also avoid sending excessive changed/on/off. } |
| 503 |
if FdnotStatus.Value <> not FdStatus.Value then |
| 504 |
begin |
| 505 |
FdnotStatus.Send(not FdStatus.Value, Value.InternalPartialSend); |
| 506 |
EventChanged.Send(true, Time, Value.InternalPartialSend); |
| 507 |
if FdStatus.Value then EventOn.Send(true, Time, Value.InternalPartialSend); |
| 508 |
if not FdStatus.Value then EventOff.Send(true, Time, Value.InternalPartialSend); |
| 509 |
end; |
| 510 |
end; |
| 511 |
|
| 512 |
procedure TTogglerNode.EventNotStatusReceive( |
| 513 |
const Event: TX3DEvent; const Value: TX3DField; const Time: TX3DTime); |
| 514 |
begin |
| 515 |
{ Do not send event to status, if it already has correct value |
| 516 |
(so our own value didn't actually changed). |
| 517 |
This avoids calling status and notStatus events in a loop. |
| 518 |
By the way, also avoid sending excessive changed/on/off. } |
| 519 |
if FdnotStatus.Value <> not FdStatus.Value then |
| 520 |
begin |
| 521 |
FdStatus.Send(not FdnotStatus.Value, Value.InternalPartialSend); |
| 522 |
EventChanged.Send(true, Time, Value.InternalPartialSend); |
| 523 |
if FdStatus.Value then EventOn.Send(true, Time, Value.InternalPartialSend); |
| 524 |
if not FdStatus.Value then EventOff.Send(true, Time, Value.InternalPartialSend); |
| 525 |
end; |
| 526 |
end; |
| 527 |
|
| 528 |
procedure TTogglerNode.EventToggleReceive( |
| 529 |
const Event: TX3DEvent; const Value: TX3DField; const Time: TX3DTime); |
| 530 |
begin |
| 531 |
if FdEnabled.Value then |
| 532 |
FdStatus.Send(not FdStatus.Value, Value.InternalPartialSend); |
| 533 |
end; |
| 534 |
|
| 535 |
procedure TTogglerNode.EventSetReceive( |
| 536 |
const Event: TX3DEvent; const Value: TX3DField; const Time: TX3DTime); |
| 537 |
begin |
| 538 |
if FdEnabled.Value then |
| 539 |
FdStatus.Send(true, Value.InternalPartialSend); |
| 540 |
end; |
| 541 |
|
| 542 |
procedure TTogglerNode.EventResetReceive( |
| 543 |
const Event: TX3DEvent; const Value: TX3DField; const Time: TX3DTime); |
| 544 |
begin |
| 545 |
if FdEnabled.Value then |
| 546 |
FdStatus.Send(false, Value.InternalPartialSend); |
| 547 |
end; |
| 548 |
|
| 549 |
{ TCommonSurfaceShaderInfo --------------------------------------------------- } |
| 550 |
|
| 551 |
constructor TCommonSurfaceShaderNode.TCommonSurfaceShaderInfo.Create(ANode: TCommonSurfaceShaderNode); |
| 552 |
begin |
| 553 |
inherited Create(ANode); |
| 554 |
FNode := ANode; |
| 555 |
end; |
| 556 |
|
| 557 |
function TCommonSurfaceShaderNode.TCommonSurfaceShaderInfo.AmbientColor: TVector3; |
| 558 |
begin |
| 559 |
Result := FNode.AmbientFactor; |
| 560 |
end; |
| 561 |
|
| 562 |
function TCommonSurfaceShaderNode.TCommonSurfaceShaderInfo.GetDiffuseColor: TVector3; |
| 563 |
begin |
| 564 |
Result := FNode.DiffuseFactor; |
| 565 |
end; |
| 566 |
|
| 567 |
procedure TCommonSurfaceShaderNode.TCommonSurfaceShaderInfo.SetDiffuseColor(const Value: TVector3); |
| 568 |
begin |
| 569 |
FNode.DiffuseFactor := Value; |
| 570 |
end; |
| 571 |
|
| 572 |
function TCommonSurfaceShaderNode.TCommonSurfaceShaderInfo.GetSpecularColor: TVector3; |
| 573 |
begin |
| 574 |
Result := FNode.SpecularFactor; |
| 575 |
end; |
| 576 |
|
| 577 |
procedure TCommonSurfaceShaderNode.TCommonSurfaceShaderInfo.SetSpecularColor(const Value: TVector3); |
| 578 |
begin |
| 579 |
FNode.SpecularFactor := Value; |
| 580 |
end; |
| 581 |
|
| 582 |
function TCommonSurfaceShaderNode.TCommonSurfaceShaderInfo.GetEmissiveColor: TVector3; |
| 583 |
begin |
| 584 |
Result := FNode.EmissiveFactor; |
| 585 |
end; |
| 586 |
|
| 587 |
procedure TCommonSurfaceShaderNode.TCommonSurfaceShaderInfo.SetEmissiveColor(const Value: TVector3); |
| 588 |
begin |
| 589 |
FNode.EmissiveFactor := Value; |
| 590 |
end; |
| 591 |
|
| 592 |
function TCommonSurfaceShaderNode.TCommonSurfaceShaderInfo.Shininess: Single; |
| 593 |
begin |
| 594 |
Result := FNode.ShininessFactor; |
| 595 |
end; |
| 596 |
|
| 597 |
function TCommonSurfaceShaderNode.TCommonSurfaceShaderInfo.ReflectionColor: TVector3; |
| 598 |
begin |
| 599 |
Result := FNode.ReflectionFactor; |
| 600 |
end; |
| 601 |
|
| 602 |
function TCommonSurfaceShaderNode.TCommonSurfaceShaderInfo.TransmissionColor: TVector3; |
| 603 |
begin |
| 604 |
Result := FNode.TransmissionFactor; |
| 605 |
end; |
| 606 |
|
| 607 |
function TCommonSurfaceShaderNode.TCommonSurfaceShaderInfo.Transparency: Single; |
| 608 |
begin |
| 609 |
Result := FNode.Transparency; |
| 610 |
end; |
| 611 |
|
| 612 |
{ TCommonSurfaceShaderNode ----------------------------------------------------- } |
| 613 |
|
| 614 |
destructor TCommonSurfaceShaderNode.Destroy; |
| 615 |
begin |
| 616 |
FreeAndNil(FMaterialInfo); |
| 617 |
inherited; |
| 618 |
end; |
| 619 |
|
| 620 |
function TCommonSurfaceShaderNode.ShininessFactorExp: Single; |
| 621 |
begin |
| 622 |
Result := Clamped(FdShininessFactor.Value * 128.0, 0.0, 128.0); |
| 623 |
end; |
| 624 |
|
| 625 |
function TCommonSurfaceShaderNode.Transparency: Single; |
| 626 |
begin |
| 627 |
Result := 1 - FdAlphaFactor.Value; |
| 628 |
end; |
| 629 |
|
| 630 |
function TCommonSurfaceShaderNode.MaterialInfo: TMaterialInfo; |
| 631 |
begin |
| 632 |
if FMaterialInfo = nil then |
| 633 |
FMaterialInfo := TCommonSurfaceShaderInfo.Create(Self); |
| 634 |
Result := FMaterialInfo; |
| 635 |
end; |
| 636 |
|
| 637 |
{ TImageBackgroundNode ------------------------------------------------------- } |
| 638 |
|
| 639 |
function TImageBackgroundNode.DirectEnumerateActive(Func: TEnumerateChildrenFunction): Pointer; |
| 640 |
begin |
| 641 |
Result := inherited; |
| 642 |
if Result <> nil then Exit; |
| 643 |
|
| 644 |
Result := FdTexture.Enumerate(Func); |
| 645 |
if Result <> nil then Exit; |
| 646 |
end; |
| 647 |
|
| 648 |
{ registration ----------------------------------------------------------------- } |
| 649 |
|
| 650 |
procedure RegisterInstantRealityNodes; |
| 651 |
begin |
| 652 |
NodesManager.RegisterNodeClasses([ |
| 653 |
TMatrixTransformNode, |
| 654 |
TTeapotNode, |
| 655 |
TLoggerNode, |
| 656 |
TConverterNode, |
| 657 |
TRenderedTextureNode, |
| 658 |
TPlaneNode, |
| 659 |
TTogglerNode, |
| 660 |
TCommonSurfaceShaderNode, |
| 661 |
TImageBackgroundNode, |
| 662 |
TEnvironmentNode |
| 663 |
]); |
| 664 |
end; |
| 665 |
{$endif read_implementation} |