| 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 |
{ X3D Shape component. } |
| 18 |
|
| 19 |
{$ifdef read_interface} |
| 20 |
{ } |
| 21 |
TAbstractShaderNode = class; |
| 22 |
TAbstractOneSidedMaterialNode = class; |
| 23 |
TAbstractSingleTextureNode = class; |
| 24 |
|
| 25 |
{ Base node type for the child nodes of @link(TAbstractAppearanceNode). } |
| 26 |
TAbstractAppearanceChildNode = class(TAbstractNode) |
| 27 |
{$I auto_generated_node_helpers/x3dnodes_x3dappearancechildnode.inc} |
| 28 |
end; |
| 29 |
|
| 30 |
{ Base node type for all Appearance nodes. } |
| 31 |
TAbstractAppearanceNode = class(TAbstractNode) |
| 32 |
{$I auto_generated_node_helpers/x3dnodes_x3dappearancenode.inc} |
| 33 |
end; |
| 34 |
|
| 35 |
{ Base node type for all Material nodes. } |
| 36 |
TAbstractMaterialNode = class(TAbstractAppearanceChildNode) |
| 37 |
public |
| 38 |
{ Material information based on this node. |
| 39 |
It is automatically updated when properties of this material change. |
| 40 |
Do not free it yourself, it will be automatically freed when |
| 41 |
this node is freed. } |
| 42 |
function MaterialInfo: TMaterialInfo; virtual; abstract; |
| 43 |
|
| 44 |
{$I auto_generated_node_helpers/x3dnodes_x3dmaterialnode.inc} |
| 45 |
end; |
| 46 |
|
| 47 |
TAppearanceNode = class; |
| 48 |
TMaterialNode = class; |
| 49 |
TComposedShaderNode = class; |
| 50 |
TLinePropertiesNode = class; |
| 51 |
TCommonSurfaceShaderNode = class; |
| 52 |
|
| 53 |
TShading = (shDefault, shGouraud, shPhong, shWireframe); |
| 54 |
TShapeCollision = (scDefault, scBox, scNone); |
| 55 |
|
| 56 |
{ Alpha mode, for @link(TAppearanceNode.AlphaMode). } |
| 57 |
TAlphaMode = ( |
| 58 |
{ Automatically detect alpha treatment. } |
| 59 |
amAuto, |
| 60 |
{ Render shape as opaque (ignore resulting alpha). } |
| 61 |
amOpaque, |
| 62 |
{ Render shape using alpha testing (resulting alpha < 0.5 implies a transparent shape). } |
| 63 |
amMask, |
| 64 |
{ Render shape using blending (supports partial transparency). } |
| 65 |
amBlend |
| 66 |
); |
| 67 |
|
| 68 |
{ Base node type for all Shape nodes. } |
| 69 |
TAbstractShapeNode = class(TAbstractChildNode) |
| 70 |
strict private |
| 71 |
function GetMaterial: TAbstractMaterialNode; |
| 72 |
procedure SetMaterial(const Value: TAbstractMaterialNode); |
| 73 |
function GetTexture: TAbstractTextureNode; |
| 74 |
procedure SetTexture(const Value: TAbstractTextureNode); |
| 75 |
function GetTextureTransform: TAbstractTextureTransformNode; |
| 76 |
procedure SetTextureTransform(const Value: TAbstractTextureTransformNode); |
| 77 |
function GetLineProperties: TLinePropertiesNode; |
| 78 |
procedure SetLineProperties(const Value: TLinePropertiesNode); |
| 79 |
procedure SetAppearanceBefore(const Sender: TSFNode; const NewNode: TX3DNode); |
| 80 |
protected |
| 81 |
function DirectEnumerateActive(Func: TEnumerateChildrenFunction): Pointer; override; |
| 82 |
public |
| 83 |
InternalWasVisibleFrameId: TFrameId; |
| 84 |
|
| 85 |
constructor Create(const AX3DName: string = ''; const ABaseUrl: String = ''); override; |
| 86 |
procedure BeforeTraverse(StateStack: TX3DGraphTraverseStateStack); override; |
| 87 |
procedure AfterTraverse(StateStack: TX3DGraphTraverseStateStack); override; |
| 88 |
|
| 89 |
{ The texture of this shape. |
| 90 |
This is a shortcut for accessing "texture" field of @link(Appearance), |
| 91 |
that is @code(TAppearanceNode(FdAppearance.Value).FdTexture.Value), |
| 92 |
checking whether nodes are set to appropriate types along the way. |
| 93 |
|
| 94 |
When setting this to something non-nil, we make sure |
| 95 |
to also set Appearance to something non-nil. } |
| 96 |
property Texture: TAbstractTextureNode read GetTexture write SetTexture; |
| 97 |
{$ifdef FPC}deprecated 'create and assign texture node instance explicitly, then set Appearance.Texture';{$endif} |
| 98 |
|
| 99 |
{ Texture transformation of this shape. |
| 100 |
Similar to @link(Texture), this is just a comfortable property |
| 101 |
for getting and setting the appropriate Appearance field, |
| 102 |
checking class types along the way. } |
| 103 |
property TextureTransform: TAbstractTextureTransformNode |
| 104 |
read GetTextureTransform write SetTextureTransform; |
| 105 |
{$ifdef FPC}deprecated 'create and assign texture transformatiom node instance explicitly, then set Appearance.TextureTransform';{$endif} |
| 106 |
|
| 107 |
{ Get / set the material of this shape. |
| 108 |
This is a shortcut to access @link(Appearance) and then set @code(Appearance.Material). |
| 109 |
When setting the material to something non-nil, we make sure |
| 110 |
to create @link(Appearance) node if necessary. } |
| 111 |
property Material: TAbstractMaterialNode read GetMaterial write SetMaterial; |
| 112 |
{$ifdef FPC}deprecated 'create and assign TAppearanceNode instance explicitly, then set Appearance.Material';{$endif} |
| 113 |
|
| 114 |
{ Line properties of this shape. This is a clean, type-safe way |
| 115 |
for accessing Appearance.lineProperties field of this shape. } |
| 116 |
property LineProperties: TLinePropertiesNode |
| 117 |
read GetLineProperties write SetLineProperties; |
| 118 |
{$ifdef FPC}deprecated 'create and assign TLinePropertiesNode instance explicitly, then set Appearance.LineProperties';{$endif} |
| 119 |
|
| 120 |
{ Find a suitable CommonSurfaceShader to be used when rendering this shape, |
| 121 |
or @nil. } |
| 122 |
function CommonSurfaceShader: TCommonSurfaceShaderNode; |
| 123 |
deprecated 'use TMaterialNode instead of TCommonSurfaceShaderNode (it has the same functionality and is standardized in X3D v4)'; |
| 124 |
|
| 125 |
{ Whether the shape was (potentially, at least partially) visible in the last rendering event. |
| 126 |
|
| 127 |
Note that the same shape reference may be present multiple times |
| 128 |
within one scene. |
| 129 |
Moreover, the same scene may be present multiple times within one viewport. |
| 130 |
Moreover, multiple viewports may render the same scene. |
| 131 |
The shape is marked as "was visible" if any instance of it was visible |
| 132 |
in any viewport in any scene. } |
| 133 |
function WasVisible: Boolean; |
| 134 |
|
| 135 |
{$I auto_generated_node_helpers/x3dnodes_x3dshapenode.inc} |
| 136 |
|
| 137 |
public |
| 138 |
{ @deprecated Deprecated name for @link(Visible). } |
| 139 |
property Render: Boolean read GetVisible write SetVisible default true; |
| 140 |
{$ifdef FPC}deprecated 'use Visible';{$endif} |
| 141 |
end; |
| 142 |
|
| 143 |
TFillPropertiesNode = class; |
| 144 |
|
| 145 |
{ Visual properties of geometry. |
| 146 |
|
| 147 |
Note that the geometry is unlit (pure white, regardless of lighting) |
| 148 |
if no @link(Material) is assigned. |
| 149 |
To make the geometry lit, you can just set there a default |
| 150 |
material created by @code(TMaterialNode.Create). } |
| 151 |
TAppearanceNode = class(TAbstractAppearanceNode) |
| 152 |
protected |
| 153 |
function DirectEnumerateActive(Func: TEnumerateChildrenFunction): Pointer; override; |
| 154 |
procedure ParseAfter(Reader: TX3DReaderNames); override; |
| 155 |
public |
| 156 |
{ Find a suitable CommonSurfaceShader to be used when rendering this shape, |
| 157 |
or @nil. } |
| 158 |
function CommonSurfaceShader: TCommonSurfaceShaderNode; |
| 159 |
deprecated 'use TMaterialNode instead of TCommonSurfaceShaderNode (it has the same functionality and is standardized in X3D v4)'; |
| 160 |
|
| 161 |
{ Texture that determines the primary RGB color of the material, |
| 162 |
and transparency of the material. |
| 163 |
|
| 164 |
This is the texture used when rendering using Gouraud shading |
| 165 |
(following the new X3D 4 specification advise). |
| 166 |
|
| 167 |
For Phong shading, this is the texture: |
| 168 |
- diffuseTexture for Material |
| 169 |
- emissiveTexture for UnlitMaterial |
| 170 |
- baseTexture for PhysicalMaterial |
| 171 |
|
| 172 |
This is also the texture that determines the opacity of the material. |
| 173 |
|
| 174 |
This is also the texture which is affected by @link(FdTexture) |
| 175 |
field (if appropriate material slot is empty). |
| 176 |
|
| 177 |
The overloaded version with TexCoordIndex parameter also specifies which |
| 178 |
texture coordinates / transformations should be used for this texture. |
| 179 |
It returns -1 when we should use standard MultiTexture specification for this, |
| 180 |
which also covers the case when MainTexture is a MultiTexture node, |
| 181 |
and should use appropriate texture coordinates for MultiTexture children. |
| 182 |
|
| 183 |
Returns @nil if none. } |
| 184 |
function MainTexture: TAbstractTextureNode; overload; |
| 185 |
function MainTexture(const GeometryForTexCoord: TAbstractGeometryNode; |
| 186 |
out TexCoordIndex: Integer): TAbstractTextureNode; overload; |
| 187 |
|
| 188 |
function InternalMaterialProperty: TMaterialProperty; |
| 189 |
function MaterialProperty: TMaterialProperty; deprecated 'use InternalMaterialProperty, or (better) do not use it at all -- this is internal'; |
| 190 |
|
| 191 |
{$I auto_generated_node_helpers/x3dnodes_appearance.inc} |
| 192 |
end; |
| 193 |
|
| 194 |
{ Additional visual properties to be applied to all polygonal areas. } |
| 195 |
TFillPropertiesNode = class(TAbstractAppearanceChildNode) |
| 196 |
{$I auto_generated_node_helpers/x3dnodes_fillproperties.inc} |
| 197 |
end; |
| 198 |
|
| 199 |
{ Additional visible properties to be applied to all line geometry. } |
| 200 |
TLinePropertiesNode = class(TAbstractAppearanceChildNode) |
| 201 |
strict private |
| 202 |
function GetLineType: TLineType; |
| 203 |
procedure SetLineType(const Value: TLineType); |
| 204 |
public |
| 205 |
{ Line type (pattern). } |
| 206 |
property LineType: TLineType read GetLineType write SetLineType default ltSolid; |
| 207 |
{$I auto_generated_node_helpers/x3dnodes_lineproperties.inc} |
| 208 |
end; |
| 209 |
|
| 210 |
TAbstractOneSidedMaterialNode = class(TAbstractMaterialNode) |
| 211 |
{$I auto_generated_node_helpers/x3dnodes_x3donesidedmaterialnode.inc} |
| 212 |
end; |
| 213 |
|
| 214 |
{ Material determines how the geometry looks on the screen, |
| 215 |
by providing parameters to the lighting equations. |
| 216 |
This is the material responsible for the Phong lighting model. |
| 217 |
|
| 218 |
It specifies how light affects one side of the surface. |
| 219 |
|
| 220 |
Note: Whether the shape is visible from both sides depends on the |
| 221 |
geometry @link(TAbstractGeometryNode.Solid) field, |
| 222 |
and is independent of whether you use this node or @link(TTwoSidedMaterialNode). } |
| 223 |
TMaterialNode = class(TAbstractOneSidedMaterialNode) |
| 224 |
strict private |
| 225 |
type |
| 226 |
TMyMaterialInfo = class(TPhongMaterialInfo) |
| 227 |
strict private |
| 228 |
FNode: TMaterialNode; |
| 229 |
protected |
| 230 |
function GetDiffuseColor: TVector3; override; |
| 231 |
procedure SetDiffuseColor(const Value: TVector3); override; |
| 232 |
function GetSpecularColor: TVector3; override; |
| 233 |
procedure SetSpecularColor(const Value: TVector3); override; |
| 234 |
function GetEmissiveColor: TVector3; override; |
| 235 |
procedure SetEmissiveColor(const Value: TVector3); override; |
| 236 |
public |
| 237 |
constructor Create(ANode: TMaterialNode); |
| 238 |
|
| 239 |
function AmbientColor: TVector3; override; |
| 240 |
function Shininess: Single; override; |
| 241 |
function ReflectionColor: TVector3; override; |
| 242 |
function Transparency: Single; override; |
| 243 |
|
| 244 |
{$ifndef CASTLE_SLIM_NODES} |
| 245 |
function ReflSpecular: TVector3; override; |
| 246 |
function ReflDiffuse: TVector3; override; |
| 247 |
function TransSpecular: TVector3; override; |
| 248 |
function TransDiffuse: TVector3; override; |
| 249 |
function ReflSpecularExp: Single; override; |
| 250 |
function TransSpecularExp: Single; override; |
| 251 |
{$endif} |
| 252 |
end; |
| 253 |
var |
| 254 |
FMaterialInfo: TMyMaterialInfo; |
| 255 |
public |
| 256 |
destructor Destroy; override; |
| 257 |
|
| 258 |
class function ForVRMLVersion(const Version: TX3DVersion): boolean; |
| 259 |
override; |
| 260 |
|
| 261 |
{ Opacity is just a 1 - FdTransparency.Value. |
| 262 |
Defined for your comfort --- for |
| 263 |
OpenGL you will usually want to pass Opacity, not Transparency. } |
| 264 |
function Opacity: Single; |
| 265 |
|
| 266 |
{ ShininessExp is just 128 * FdShininess.Value, this is the "real" |
| 267 |
exponent indicated by shininess field value. |
| 268 |
Defined for your comfort --- for any graphic library you will usually |
| 269 |
want to pass the "real" exponent given by this function, not just |
| 270 |
value of shininess field. } |
| 271 |
function ShininessExp: Single; |
| 272 |
|
| 273 |
function MaterialInfo: TMaterialInfo; override; |
| 274 |
|
| 275 |
{ Only the emissiveColor is not black (zero), |
| 276 |
which means that the material behaves like unlit. |
| 277 |
|
| 278 |
This checks that ambient and diffuse and specular colors are all zero. |
| 279 |
It's an important information about the material sometimes. |
| 280 |
It is similar to the NULL material situation (when "Appearance.material=NULL" |
| 281 |
case), but the color and transparency are still |
| 282 |
configurable (using FdEmissiveColor and FdTransparency fields). |
| 283 |
|
| 284 |
We can optimize this case when rendering. } |
| 285 |
function PureEmissive: boolean; |
| 286 |
|
| 287 |
{ Force the material pure emissive (see @link(PureEmissive)) by setting |
| 288 |
other colors to black. } |
| 289 |
procedure ForcePureEmissive; deprecated 'use TUnlitMaterialNode for unlit materials'; |
| 290 |
|
| 291 |
{$I auto_generated_node_helpers/x3dnodes_material.inc} |
| 292 |
end; |
| 293 |
TMaterialNode_2 = TMaterialNode; |
| 294 |
|
| 295 |
TPhysicalMaterialNode = class(TAbstractOneSidedMaterialNode) |
| 296 |
strict private |
| 297 |
type |
| 298 |
TMyPhysicalMaterialInfo = class(TPhysicalMaterialInfo) |
| 299 |
strict private |
| 300 |
FNode: TPhysicalMaterialNode; |
| 301 |
protected |
| 302 |
function GetEmissiveColor: TVector3; override; |
| 303 |
procedure SetEmissiveColor(const Value: TVector3); override; |
| 304 |
function GetBaseColor: TVector3; override; |
| 305 |
procedure SetBaseColor(const Value: TVector3); override; |
| 306 |
public |
| 307 |
constructor Create(ANode: TPhysicalMaterialNode); |
| 308 |
function Transparency: Single; override; |
| 309 |
function Metallic: Single; override; |
| 310 |
function Roughness: Single; override; |
| 311 |
end; |
| 312 |
var |
| 313 |
FMaterialInfo: TMyPhysicalMaterialInfo; |
| 314 |
public |
| 315 |
destructor Destroy; override; |
| 316 |
function MaterialInfo: TMaterialInfo; override; |
| 317 |
|
| 318 |
{$I auto_generated_node_helpers/x3dnodes_physicalmaterial.inc} |
| 319 |
end; |
| 320 |
|
| 321 |
EInvalidGeometryIndex = class(Exception); |
| 322 |
|
| 323 |
{ Shape is a rendered object in the world, with an appearance and geometry. } |
| 324 |
TShapeNode = class(TAbstractShapeNode) |
| 325 |
public |
| 326 |
{ Generate tangent vectors on geometry if they are |
| 327 |
(1) necessary and (2) possible to generate and (3) not present yet. |
| 328 |
|
| 329 |
@raises EInvalidGeometryIndex If the indexes in geometry are invalid. } |
| 330 |
procedure GenerateTangents; |
| 331 |
|
| 332 |
{$I auto_generated_node_helpers/x3dnodes_shape.inc} |
| 333 |
end; |
| 334 |
|
| 335 |
{ @deprecated |
| 336 |
|
| 337 |
Note: Whether the shape is visible from both sides depends on the |
| 338 |
geometry @link(TAbstractGeometryNode.Solid) field, |
| 339 |
and is independent of whether you use this or other (one-sided) material nodes. } |
| 340 |
TTwoSidedMaterialNode = class(TAbstractMaterialNode) |
| 341 |
strict private |
| 342 |
type |
| 343 |
TTwoSidedMaterialInfo = class(TPhongMaterialInfo) |
| 344 |
strict private |
| 345 |
WarningSeparateBackColorDone: Boolean; |
| 346 |
FNode: TTwoSidedMaterialNode; |
| 347 |
protected |
| 348 |
function GetDiffuseColor: TVector3; override; |
| 349 |
procedure SetDiffuseColor(const Value: TVector3); override; |
| 350 |
function GetSpecularColor: TVector3; override; |
| 351 |
procedure SetSpecularColor(const Value: TVector3); override; |
| 352 |
function GetEmissiveColor: TVector3; override; |
| 353 |
procedure SetEmissiveColor(const Value: TVector3); override; |
| 354 |
public |
| 355 |
constructor Create(ANode: TTwoSidedMaterialNode); |
| 356 |
|
| 357 |
function AmbientColor: TVector3; override; |
| 358 |
function Shininess: Single; override; |
| 359 |
function ReflectionColor: TVector3; override; |
| 360 |
function Transparency: Single; override; |
| 361 |
end; |
| 362 |
var |
| 363 |
FMaterialInfo: TMaterialInfo; |
| 364 |
public |
| 365 |
destructor Destroy; override; |
| 366 |
function MaterialInfo: TMaterialInfo; override; |
| 367 |
|
| 368 |
{$I auto_generated_node_helpers/x3dnodes_twosidedmaterial.inc} |
| 369 |
end; |
| 370 |
|
| 371 |
TUnlitMaterialNode = class(TAbstractOneSidedMaterialNode) |
| 372 |
strict private |
| 373 |
type |
| 374 |
TMyUnlitMaterialInfo = class(TUnlitMaterialInfo) |
| 375 |
strict private |
| 376 |
FNode: TUnlitMaterialNode; |
| 377 |
protected |
| 378 |
function GetEmissiveColor: TVector3; override; |
| 379 |
procedure SetEmissiveColor(const Value: TVector3); override; |
| 380 |
public |
| 381 |
constructor Create(ANode: TUnlitMaterialNode); |
| 382 |
function Transparency: Single; override; |
| 383 |
end; |
| 384 |
var |
| 385 |
FMaterialInfo: TMyUnlitMaterialInfo; |
| 386 |
public |
| 387 |
constructor Create(const AX3DName: string = ''; const ABaseUrl: String = ''); override; |
| 388 |
destructor Destroy; override; |
| 389 |
function MaterialInfo: TMaterialInfo; override; |
| 390 |
|
| 391 |
{$I auto_generated_node_helpers/x3dnodes_unlitmaterial.inc} |
| 392 |
end; |
| 393 |
|
| 394 |
{$endif read_interface} |
| 395 |
|
| 396 |
{$ifdef read_implementation} |
| 397 |
|
| 398 |
const |
| 399 |
AlphaModeToString: array [TAlphaMode] of string = |
| 400 |
( |
| 401 |
'AUTO', |
| 402 |
'OPAQUE', |
| 403 |
'MASK', |
| 404 |
'BLEND' |
| 405 |
); |
| 406 |
|
| 407 |
{ TSFShading ----------------------------------------------------------------- } |
| 408 |
|
| 409 |
(*TODO: |
| 410 |
If wireframe changes, then ExecuteChange should be chWireframe. |
| 411 |
Below there's a convoluted way to do this. |
| 412 |
Not used for now, as FdShading is now just TSFStringEnum. |
| 413 |
|
| 414 |
class function TAbstractShapeNode.TSFShading.ExposedEventsFieldClass: TX3DFieldClass; |
| 415 |
begin |
| 416 |
Result := TSFString; |
| 417 |
end; |
| 418 |
|
| 419 |
procedure TAbstractShapeNode.TSFShading.ExposedEventReceive(Event: TX3DEvent; NewValue: TX3DField; const Time: TX3DTime); |
| 420 |
var |
| 421 |
OldWireframe, NewWireframe: boolean; |
| 422 |
begin |
| 423 |
OldWireframe := TShading(EnumValue) = shWireframe; |
| 424 |
NewWireframe := TShading(StringToEnumValue((NewValue as TSFString).Value)) = shWireframe; |
| 425 |
if OldWireframe <> NewWireframe then |
| 426 |
WireframeChanged := true; |
| 427 |
|
| 428 |
inherited; |
| 429 |
end; |
| 430 |
|
| 431 |
function TAbstractShapeNode.TSFShading.ExecuteChange: TX3DChange; |
| 432 |
begin |
| 433 |
Result := inherited; |
| 434 |
if WireframeChanged then |
| 435 |
begin |
| 436 |
WireframeChanged := false; |
| 437 |
Exit(chWireframe); |
| 438 |
end; |
| 439 |
end; |
| 440 |
*) |
| 441 |
|
| 442 |
{ TAbstractShapeNode --------------------------------------------------------- } |
| 443 |
|
| 444 |
const |
| 445 |
ShadingNames: array [TShading] of String = ( |
| 446 |
'DEFAULT', |
| 447 |
'GOURAUD', |
| 448 |
'PHONG', |
| 449 |
'WIREFRAME' |
| 450 |
); |
| 451 |
ShapeCollisionNames: array [TShapeCollision] of String = ( |
| 452 |
'DEFAULT', |
| 453 |
'BOX', |
| 454 |
'NONE' |
| 455 |
); |
| 456 |
|
| 457 |
constructor TAbstractShapeNode.Create(const AX3DName: string = ''; const ABaseUrl: String = ''); |
| 458 |
begin |
| 459 |
inherited; |
| 460 |
FdBboxCenter.ChangeAlways := chBBox; |
| 461 |
FdBboxSize.ChangeAlways := chBBox; |
| 462 |
FdAppearance.ChangeAlways := chEverything; |
| 463 |
FdGeometry.ChangeAlways := chEverything; |
| 464 |
// Older name for this field, specific to CGE and InstantReality |
| 465 |
FdVisible.AddAlternativeName('render', 0); |
| 466 |
end; |
| 467 |
|
| 468 |
procedure TAbstractShapeNode.SetAppearanceBefore(const Sender: TSFNode; const NewNode: TX3DNode); |
| 469 |
var |
| 470 |
OldMaterial, NewMaterial: TX3DNode; |
| 471 |
begin |
| 472 |
// caller of OnBeforeVisibleChange should have checked this |
| 473 |
Assert(Sender.Value <> NewNode); |
| 474 |
Assert(Sender = FdAppearance); |
| 475 |
|
| 476 |
if Scene <> nil then |
| 477 |
begin |
| 478 |
// move shape associations from old Appearance to new |
| 479 |
Scene.InternalMoveShapeAssociations(Sender.Value, NewNode, InternalSceneShape); |
| 480 |
|
| 481 |
// move shape associations from old XxxMaterial to new |
| 482 |
if Sender.Value is TAppearanceNode then |
| 483 |
OldMaterial := TAppearanceNode(Sender.Value).FdMaterial.Value |
| 484 |
else |
| 485 |
OldMaterial := nil; |
| 486 |
if NewNode is TAppearanceNode then |
| 487 |
NewMaterial := TAppearanceNode(NewNode).FdMaterial.Value |
| 488 |
else |
| 489 |
NewMaterial := nil; |
| 490 |
if OldMaterial <> NewMaterial then |
| 491 |
Scene.InternalMoveShapeAssociations(OldMaterial, NewMaterial, InternalSceneShape); |
| 492 |
end; |
| 493 |
end; |
| 494 |
|
| 495 |
function TAbstractShapeNode.DirectEnumerateActive(Func: TEnumerateChildrenFunction): Pointer; |
| 496 |
begin |
| 497 |
Result := nil; |
| 498 |
|
| 499 |
{ According to X3D spec, when geometry is NULL then object is not |
| 500 |
drawn so appearance doesn't matter. } |
| 501 |
|
| 502 |
if Geometry <> nil then // checks that FdGeometry has value with correct class |
| 503 |
begin |
| 504 |
Result := FdAppearance.Enumerate(Func); |
| 505 |
if Result <> nil then Exit; |
| 506 |
|
| 507 |
Result := FdGeometry.Enumerate(Func); |
| 508 |
if Result <> nil then Exit; |
| 509 |
end; |
| 510 |
end; |
| 511 |
|
| 512 |
procedure TAbstractShapeNode.BeforeTraverse(StateStack: TX3DGraphTraverseStateStack); |
| 513 |
begin |
| 514 |
inherited; |
| 515 |
StateStack.Top.ShapeNode := Self; |
| 516 |
end; |
| 517 |
|
| 518 |
procedure TAbstractShapeNode.AfterTraverse(StateStack: TX3DGraphTraverseStateStack); |
| 519 |
begin |
| 520 |
StateStack.Top.ShapeNode := nil; |
| 521 |
inherited; |
| 522 |
end; |
| 523 |
|
| 524 |
function TAbstractShapeNode.GetMaterial: TAbstractMaterialNode; |
| 525 |
var |
| 526 |
App: TAppearanceNode; |
| 527 |
begin |
| 528 |
App := Appearance; |
| 529 |
if App <> nil then |
| 530 |
Result := App.Material |
| 531 |
else |
| 532 |
Result := nil; |
| 533 |
end; |
| 534 |
|
| 535 |
function TAbstractShapeNode.CommonSurfaceShader: TCommonSurfaceShaderNode; |
| 536 |
var |
| 537 |
App: TAppearanceNode; |
| 538 |
begin |
| 539 |
App := Appearance; |
| 540 |
if App <> nil then |
| 541 |
{$warnings off} // using deprecated in deprecated |
| 542 |
Result := App.CommonSurfaceShader |
| 543 |
{$warnings on} |
| 544 |
else |
| 545 |
Result := nil; |
| 546 |
end; |
| 547 |
|
| 548 |
procedure TAbstractShapeNode.SetMaterial(const Value: TAbstractMaterialNode); |
| 549 |
var |
| 550 |
App: TAppearanceNode; |
| 551 |
begin |
| 552 |
App := Appearance; |
| 553 |
if App <> nil then |
| 554 |
App.Material := Value |
| 555 |
else |
| 556 |
if Value <> nil then |
| 557 |
begin |
| 558 |
App := TAppearanceNode.Create('', BaseUrl); |
| 559 |
App.Scene := Scene; |
| 560 |
App.Material := Value; |
| 561 |
Appearance := App; |
| 562 |
end; |
| 563 |
end; |
| 564 |
|
| 565 |
function TAbstractShapeNode.GetTexture: TAbstractTextureNode; |
| 566 |
var |
| 567 |
App: TAppearanceNode; |
| 568 |
begin |
| 569 |
App := Appearance; |
| 570 |
if App <> nil then |
| 571 |
Result := App.Texture |
| 572 |
else |
| 573 |
Result := nil; |
| 574 |
end; |
| 575 |
|
| 576 |
procedure TAbstractShapeNode.SetTexture(const Value: TAbstractTextureNode); |
| 577 |
var |
| 578 |
App: TAppearanceNode; |
| 579 |
begin |
| 580 |
App := Appearance; |
| 581 |
if App <> nil then |
| 582 |
App.Texture := Value |
| 583 |
else |
| 584 |
if Value <> nil then |
| 585 |
begin |
| 586 |
App := TAppearanceNode.Create('', BaseUrl); |
| 587 |
App.Scene := Scene; |
| 588 |
App.Texture := Value; |
| 589 |
Appearance := App; |
| 590 |
end; |
| 591 |
end; |
| 592 |
|
| 593 |
function TAbstractShapeNode.GetTextureTransform: TAbstractTextureTransformNode; |
| 594 |
var |
| 595 |
App: TAppearanceNode; |
| 596 |
begin |
| 597 |
App := Appearance; |
| 598 |
if App <> nil then |
| 599 |
Result := App.TextureTransform |
| 600 |
else |
| 601 |
Result := nil; |
| 602 |
end; |
| 603 |
|
| 604 |
procedure TAbstractShapeNode.SetTextureTransform(const Value: TAbstractTextureTransformNode); |
| 605 |
var |
| 606 |
App: TAppearanceNode; |
| 607 |
begin |
| 608 |
App := Appearance; |
| 609 |
if App <> nil then |
| 610 |
App.TextureTransform := Value |
| 611 |
else |
| 612 |
if Value <> nil then |
| 613 |
begin |
| 614 |
App := TAppearanceNode.Create('', BaseUrl); |
| 615 |
App.Scene := Scene; |
| 616 |
App.TextureTransform := Value; |
| 617 |
Appearance := App; |
| 618 |
end; |
| 619 |
end; |
| 620 |
|
| 621 |
function TAbstractShapeNode.GetLineProperties: TLinePropertiesNode; |
| 622 |
var |
| 623 |
App: TAppearanceNode; |
| 624 |
begin |
| 625 |
App := Appearance; |
| 626 |
if App <> nil then |
| 627 |
Result := App.LineProperties |
| 628 |
else |
| 629 |
Result := nil; |
| 630 |
end; |
| 631 |
|
| 632 |
procedure TAbstractShapeNode.SetLineProperties(const Value: TLinePropertiesNode); |
| 633 |
var |
| 634 |
App: TAppearanceNode; |
| 635 |
begin |
| 636 |
App := Appearance; |
| 637 |
if App <> nil then |
| 638 |
App.LineProperties := Value |
| 639 |
else |
| 640 |
if Value <> nil then |
| 641 |
begin |
| 642 |
App := TAppearanceNode.Create('', BaseUrl); |
| 643 |
App.Scene := Scene; |
| 644 |
App.LineProperties := Value; |
| 645 |
Appearance := App; |
| 646 |
end; |
| 647 |
end; |
| 648 |
|
| 649 |
function TAbstractShapeNode.WasVisible: Boolean; |
| 650 |
begin |
| 651 |
{ Note: InternalWasVisibleFrameId is 0 by default, |
| 652 |
and TFramesPerSecond.RenderFrameId is always >= 1, |
| 653 |
which means that this will return (correctly) false at the beginning before any rendering run. } |
| 654 |
Result := TFramesPerSecond.RenderFrameId = InternalWasVisibleFrameId; |
| 655 |
end; |
| 656 |
|
| 657 |
function TAppearanceNode.DirectEnumerateActive(Func: TEnumerateChildrenFunction): Pointer; |
| 658 |
begin |
| 659 |
{$ifndef CASTLE_SLIM_NODES} |
| 660 |
Result := FFdFillProperties.Enumerate(Func); |
| 661 |
if Result <> nil then Exit; |
| 662 |
{$endif} |
| 663 |
|
| 664 |
Result := FFdLineProperties.Enumerate(Func); |
| 665 |
if Result <> nil then Exit; |
| 666 |
|
| 667 |
Result := FFdMaterial.Enumerate(Func); |
| 668 |
if Result <> nil then Exit; |
| 669 |
|
| 670 |
Result := FFdShaders.Enumerate(Func); |
| 671 |
if Result <> nil then Exit; |
| 672 |
|
| 673 |
Result := FFdTexture.Enumerate(Func); |
| 674 |
if Result <> nil then Exit; |
| 675 |
|
| 676 |
Result := FFdTextureTransform.Enumerate(Func); |
| 677 |
if Result <> nil then Exit; |
| 678 |
end; |
| 679 |
|
| 680 |
function TAppearanceNode.CommonSurfaceShader: TCommonSurfaceShaderNode; |
| 681 |
var |
| 682 |
I: Integer; |
| 683 |
Shader: TX3DNode; |
| 684 |
begin |
| 685 |
Result := nil; |
| 686 |
for I := 0 to FdShaders.Count - 1 do |
| 687 |
begin |
| 688 |
Shader := FdShaders[I]; |
| 689 |
if Shader is TCommonSurfaceShaderNode then |
| 690 |
Exit(TCommonSurfaceShaderNode(Shader)); |
| 691 |
end; |
| 692 |
end; |
| 693 |
|
| 694 |
function TAppearanceNode.MainTexture: TAbstractTextureNode; |
| 695 |
// var |
| 696 |
// TexCoordIndex: Integer; |
| 697 |
var |
| 698 |
SurfaceShader: TCommonSurfaceShaderNode; |
| 699 |
begin |
| 700 |
// It's like |
| 701 |
// Result := MainTexture(GeometryForTexCoord, TexCoordIndex); |
| 702 |
// but we don't need GeometryForTexCoord in this case. |
| 703 |
|
| 704 |
Result := nil; |
| 705 |
{$warnings off} // using deprecated to keep backward compatibility |
| 706 |
SurfaceShader := CommonSurfaceShader; |
| 707 |
{$warnings on} |
| 708 |
if SurfaceShader <> nil then |
| 709 |
begin |
| 710 |
if SurfaceShader.MultiDiffuseAlphaTexture <> nil then |
| 711 |
begin |
| 712 |
Exit(SurfaceShader.MultiDiffuseAlphaTexture); |
| 713 |
end; |
| 714 |
if SurfaceShader.DiffuseTexture <> nil then |
| 715 |
begin |
| 716 |
Exit(SurfaceShader.DiffuseTexture); |
| 717 |
end; |
| 718 |
end else |
| 719 |
begin |
| 720 |
if (Material is TMaterialNode) and |
| 721 |
(TMaterialNode(Material).DiffuseTexture <> nil) then |
| 722 |
begin |
| 723 |
Exit(TMaterialNode(Material).DiffuseTexture); |
| 724 |
end; |
| 725 |
if (Material is TPhysicalMaterialNode) and |
| 726 |
(TPhysicalMaterialNode(Material).BaseTexture <> nil) then |
| 727 |
begin |
| 728 |
Exit(TPhysicalMaterialNode(Material).BaseTexture); |
| 729 |
end; |
| 730 |
if (Material is TUnlitMaterialNode) and |
| 731 |
(TUnlitMaterialNode(Material).EmissiveTexture <> nil) then |
| 732 |
begin |
| 733 |
Exit(TUnlitMaterialNode(Material).EmissiveTexture); |
| 734 |
end; |
| 735 |
Result := Texture; |
| 736 |
end; |
| 737 |
end; |
| 738 |
|
| 739 |
function TAppearanceNode.MainTexture(const GeometryForTexCoord: TAbstractGeometryNode; |
| 740 |
out TexCoordIndex: Integer): TAbstractTextureNode; |
| 741 |
var |
| 742 |
SurfaceShader: TCommonSurfaceShaderNode; |
| 743 |
begin |
| 744 |
Result := nil; |
| 745 |
TexCoordIndex := -1; |
| 746 |
{$warnings off} // using deprecated to keep backward compatibility |
| 747 |
SurfaceShader := CommonSurfaceShader; |
| 748 |
{$warnings on} |
| 749 |
if SurfaceShader <> nil then |
| 750 |
begin |
| 751 |
if SurfaceShader.MultiDiffuseAlphaTexture <> nil then |
| 752 |
begin |
| 753 |
TexCoordIndex := SurfaceShader.DiffuseTextureCoordinatesId; // TODO: is this what X3DOM/InstantReality are doing? test. |
| 754 |
Exit(SurfaceShader.MultiDiffuseAlphaTexture); |
| 755 |
end; |
| 756 |
if SurfaceShader.DiffuseTexture <> nil then |
| 757 |
begin |
| 758 |
TexCoordIndex := SurfaceShader.DiffuseTextureCoordinatesId; |
| 759 |
Exit(SurfaceShader.DiffuseTexture); |
| 760 |
end; |
| 761 |
end else |
| 762 |
begin |
| 763 |
if (Material is TMaterialNode) and |
| 764 |
(TMaterialNode(Material).DiffuseTexture <> nil) then |
| 765 |
begin |
| 766 |
GeometryForTexCoord.FindTextureMapping(TMaterialNode(Material).DiffuseTextureMapping, TexCoordIndex, false); |
| 767 |
Exit(TMaterialNode(Material).DiffuseTexture); |
| 768 |
end; |
| 769 |
if (Material is TPhysicalMaterialNode) and |
| 770 |
(TPhysicalMaterialNode(Material).BaseTexture <> nil) then |
| 771 |
begin |
| 772 |
GeometryForTexCoord.FindTextureMapping(TPhysicalMaterialNode(Material).BaseTextureMapping, TexCoordIndex, false); |
| 773 |
Exit(TPhysicalMaterialNode(Material).BaseTexture); |
| 774 |
end; |
| 775 |
if (Material is TUnlitMaterialNode) and |
| 776 |
(TUnlitMaterialNode(Material).EmissiveTexture <> nil) then |
| 777 |
begin |
| 778 |
GeometryForTexCoord.FindTextureMapping(TUnlitMaterialNode(Material).EmissiveTextureMapping, TexCoordIndex, false); |
| 779 |
Exit(TUnlitMaterialNode(Material).EmissiveTexture); |
| 780 |
end; |
| 781 |
Result := Texture; |
| 782 |
end; |
| 783 |
end; |
| 784 |
|
| 785 |
function TAppearanceNode.MaterialProperty: TMaterialProperty; |
| 786 |
begin |
| 787 |
Result := InternalMaterialProperty; |
| 788 |
end; |
| 789 |
|
| 790 |
function TAppearanceNode.InternalMaterialProperty: TMaterialProperty; |
| 791 |
|
| 792 |
function TryUrl(const Url: TMFString): TMaterialProperty; |
| 793 |
var |
| 794 |
TextureUrl: String; |
| 795 |
begin |
| 796 |
Result := nil; |
| 797 |
if Url.Count <> 0 then |
| 798 |
begin |
| 799 |
TextureUrl := Url.Items[0]; |
| 800 |
if TextureUrl <> '' then |
| 801 |
Result := MaterialProperties.FindTextureBaseName( |
| 802 |
DeleteURIExt(ExtractURIName(TextureUrl))); |
| 803 |
end; |
| 804 |
end; |
| 805 |
|
| 806 |
var |
| 807 |
Tex: TAbstractTextureNode; |
| 808 |
begin |
| 809 |
Result := nil; |
| 810 |
|
| 811 |
Tex := MainTexture; |
| 812 |
if Tex is TImageTextureNode then |
| 813 |
Result := TryUrl(TImageTextureNode(Tex).FdUrl); |
| 814 |
if Tex is TMovieTextureNode then |
| 815 |
Result := TryUrl(TMovieTextureNode(Tex).FdUrl); |
| 816 |
end; |
| 817 |
|
| 818 |
procedure TAppearanceNode.ParseAfter(Reader: TX3DReaderNames); |
| 819 |
var |
| 820 |
MP: TMaterialProperty; |
| 821 |
NormalMapNode: TImageTextureNode; |
| 822 |
begin |
| 823 |
inherited; |
| 824 |
|
| 825 |
{$warnings off} // using deprecated material props to keep them working |
| 826 |
MP := InternalMaterialProperty; |
| 827 |
|
| 828 |
if (MP <> nil) and (MP.NormalMap <> '') and (FdNormalMap.Value = nil) then |
| 829 |
begin |
| 830 |
{ use normalMap from MaterialProperty, if our normalMap field is empty now } |
| 831 |
NormalMapNode := TImageTextureNode.Create('', BaseUrl); |
| 832 |
NormalMapNode.FdUrl.Items.Add(MP.NormalMap); |
| 833 |
FdNormalMap.Value := NormalMapNode; |
| 834 |
end; |
| 835 |
|
| 836 |
if (MP <> nil) and |
| 837 |
(MP.AlphaChannel <> '') and |
| 838 |
(Texture is TAbstractSingleTextureNode) { checks also Texture <> nil } then |
| 839 |
TAbstractSingleTextureNode(Texture).FdAlphaChannel.Value := MP.AlphaChannel; |
| 840 |
{$warnings on} |
| 841 |
end; |
| 842 |
|
| 843 |
function TLinePropertiesNode.GetLineType: TLineType; |
| 844 |
begin |
| 845 |
case FdLineType.Value of |
| 846 |
1: Result := ltSolid; |
| 847 |
2: Result := ltDashed; |
| 848 |
3: Result := ltDotted; |
| 849 |
4: Result := ltDashedDotted; |
| 850 |
5: Result := ltDashDotDot; |
| 851 |
else Result := ltSolid; // unsupported FdLineType.Value |
| 852 |
end; |
| 853 |
end; |
| 854 |
|
| 855 |
procedure TLinePropertiesNode.SetLineType(const Value: TLineType); |
| 856 |
begin |
| 857 |
case Value of |
| 858 |
ltSolid : FdLineType.Send(1); |
| 859 |
ltDashed : FdLineType.Send(2); |
| 860 |
ltDotted : FdLineType.Send(3); |
| 861 |
ltDashedDotted: FdLineType.Send(4); |
| 862 |
ltDashDotDot : FdLineType.Send(5); |
| 863 |
{$ifndef COMPILER_CASE_ANALYSIS} |
| 864 |
else raise EInternalError.Create('TLinePropertiesNode.SetLineType:this Value not implemented'); |
| 865 |
{$endif} |
| 866 |
end; |
| 867 |
end; |
| 868 |
|
| 869 |
{ TMaterialNode -------------------------------------------------------------- } |
| 870 |
|
| 871 |
destructor TMaterialNode.Destroy; |
| 872 |
begin |
| 873 |
FreeAndNil(FMaterialInfo); |
| 874 |
inherited; |
| 875 |
end; |
| 876 |
|
| 877 |
class function TMaterialNode.ForVRMLVersion(const Version: TX3DVersion): boolean; |
| 878 |
begin |
| 879 |
Result := Version.Major >= 2; |
| 880 |
end; |
| 881 |
|
| 882 |
function TMaterialNode.Opacity: Single; |
| 883 |
begin |
| 884 |
Result := 1- FdTransparency.Value; |
| 885 |
end; |
| 886 |
|
| 887 |
function TMaterialNode.ShininessExp: Single; |
| 888 |
begin |
| 889 |
Result := Clamped(FdShininess.Value * 128.0, 0.0, 128.0); |
| 890 |
end; |
| 891 |
|
| 892 |
function TMaterialNode.MaterialInfo: TMaterialInfo; |
| 893 |
begin |
| 894 |
if FMaterialInfo = nil then |
| 895 |
FMaterialInfo := TMyMaterialInfo.Create(Self); |
| 896 |
Result := FMaterialInfo; |
| 897 |
end; |
| 898 |
|
| 899 |
function TMaterialNode.PureEmissive: boolean; |
| 900 |
begin |
| 901 |
Result := (FdAmbientIntensity.Value = 0) and |
| 902 |
FdDiffuseColor.Value.IsPerfectlyZero and |
| 903 |
FdSpecularColor.Value.IsPerfectlyZero; |
| 904 |
end; |
| 905 |
|
| 906 |
procedure TMaterialNode.ForcePureEmissive; |
| 907 |
begin |
| 908 |
FdDiffuseColor.Send(TVector3.Zero); |
| 909 |
FdSpecularColor.Send(TVector3.Zero); |
| 910 |
FdAmbientIntensity.Send(0); |
| 911 |
Assert(PureEmissive); |
| 912 |
end; |
| 913 |
|
| 914 |
{ TMaterialNode.TMyMaterialInfo -------------------------------------------------------- } |
| 915 |
|
| 916 |
constructor TMaterialNode.TMyMaterialInfo.Create(ANode: TMaterialNode); |
| 917 |
begin |
| 918 |
inherited Create(ANode); |
| 919 |
FNode := ANode; |
| 920 |
end; |
| 921 |
|
| 922 |
function TMaterialNode.TMyMaterialInfo.AmbientColor: TVector3; |
| 923 |
begin |
| 924 |
Result := FNode.DiffuseColor * FNode.AmbientIntensity; |
| 925 |
end; |
| 926 |
|
| 927 |
function TMaterialNode.TMyMaterialInfo.GetDiffuseColor: TVector3; |
| 928 |
begin |
| 929 |
Result := FNode.DiffuseColor; |
| 930 |
end; |
| 931 |
|
| 932 |
procedure TMaterialNode.TMyMaterialInfo.SetDiffuseColor(const Value: TVector3); |
| 933 |
begin |
| 934 |
FNode.DiffuseColor := Value; |
| 935 |
end; |
| 936 |
|
| 937 |
function TMaterialNode.TMyMaterialInfo.GetSpecularColor: TVector3; |
| 938 |
begin |
| 939 |
Result := FNode.SpecularColor; |
| 940 |
end; |
| 941 |
|
| 942 |
procedure TMaterialNode.TMyMaterialInfo.SetSpecularColor(const Value: TVector3); |
| 943 |
begin |
| 944 |
FNode.SpecularColor := Value; |
| 945 |
end; |
| 946 |
|
| 947 |
function TMaterialNode.TMyMaterialInfo.GetEmissiveColor: TVector3; |
| 948 |
begin |
| 949 |
Result := FNode.EmissiveColor; |
| 950 |
end; |
| 951 |
|
| 952 |
procedure TMaterialNode.TMyMaterialInfo.SetEmissiveColor(const Value: TVector3); |
| 953 |
begin |
| 954 |
FNode.EmissiveColor := Value; |
| 955 |
end; |
| 956 |
|
| 957 |
function TMaterialNode.TMyMaterialInfo.Shininess: Single; |
| 958 |
begin |
| 959 |
Result := FNode.Shininess; |
| 960 |
end; |
| 961 |
|
| 962 |
function TMaterialNode.TMyMaterialInfo.ReflectionColor: TVector3; |
| 963 |
var |
| 964 |
M: Single; |
| 965 |
begin |
| 966 |
M := FNode.Mirror; |
| 967 |
Result := Vector3(M, M, M); |
| 968 |
end; |
| 969 |
|
| 970 |
function TMaterialNode.TMyMaterialInfo.Transparency: Single; |
| 971 |
begin |
| 972 |
Result := FNode.Transparency; |
| 973 |
end; |
| 974 |
|
| 975 |
{$ifndef CASTLE_SLIM_NODES} |
| 976 |
|
| 977 |
function TMaterialNode.TMyMaterialInfo.ReflSpecular: TVector3; |
| 978 |
var |
| 979 |
A: TVector3List; |
| 980 |
begin |
| 981 |
A := FNode.FdReflSpecular.Items; |
| 982 |
if A.Count = 0 then |
| 983 |
Result := inherited |
| 984 |
else |
| 985 |
Result := A.L[0]; |
| 986 |
end; |
| 987 |
|
| 988 |
function TMaterialNode.TMyMaterialInfo.ReflDiffuse: TVector3; |
| 989 |
var |
| 990 |
A: TVector3List; |
| 991 |
begin |
| 992 |
A := FNode.FdReflDiffuse.Items; |
| 993 |
if A.Count = 0 then |
| 994 |
Result := inherited |
| 995 |
else |
| 996 |
Result := A.L[0]; |
| 997 |
end; |
| 998 |
|
| 999 |
function TMaterialNode.TMyMaterialInfo.TransSpecular: TVector3; |
| 1000 |
var |
| 1001 |
A: TVector3List; |
| 1002 |
begin |
| 1003 |
A := FNode.FdTransSpecular.Items; |
| 1004 |
if A.Count = 0 then |
| 1005 |
Result := inherited |
| 1006 |
else |
| 1007 |
Result := A.L[0]; |
| 1008 |
end; |
| 1009 |
|
| 1010 |
function TMaterialNode.TMyMaterialInfo.TransDiffuse: TVector3; |
| 1011 |
var |
| 1012 |
A: TVector3List; |
| 1013 |
begin |
| 1014 |
A := FNode.FdTransDiffuse.Items; |
| 1015 |
if A.Count = 0 then |
| 1016 |
Result := inherited |
| 1017 |
else |
| 1018 |
Result := A.L[0]; |
| 1019 |
end; |
| 1020 |
|
| 1021 |
function TMaterialNode.TMyMaterialInfo.ReflSpecularExp: Single; |
| 1022 |
begin |
| 1023 |
Result := FNode.FdReflSpecularExp.Value; |
| 1024 |
end; |
| 1025 |
|
| 1026 |
function TMaterialNode.TMyMaterialInfo.TransSpecularExp: Single; |
| 1027 |
begin |
| 1028 |
Result := FNode.FdTransSpecularExp.Value; |
| 1029 |
end; |
| 1030 |
|
| 1031 |
{$endif not CASTLE_SLIM_NODES} |
| 1032 |
|
| 1033 |
{ TPhysicalMaterialNode.TMyPhysicalMaterialInfo --------------------------------------------------- } |
| 1034 |
|
| 1035 |
constructor TPhysicalMaterialNode.TMyPhysicalMaterialInfo.Create(ANode: TPhysicalMaterialNode); |
| 1036 |
begin |
| 1037 |
inherited Create(ANode); |
| 1038 |
FNode := ANode; |
| 1039 |
end; |
| 1040 |
|
| 1041 |
function TPhysicalMaterialNode.TMyPhysicalMaterialInfo.GetEmissiveColor: TVector3; |
| 1042 |
begin |
| 1043 |
Result := FNode.EmissiveColor; |
| 1044 |
end; |
| 1045 |
|
| 1046 |
procedure TPhysicalMaterialNode.TMyPhysicalMaterialInfo.SetEmissiveColor(const Value: TVector3); |
| 1047 |
begin |
| 1048 |
FNode.EmissiveColor := Value; |
| 1049 |
end; |
| 1050 |
|
| 1051 |
function TPhysicalMaterialNode.TMyPhysicalMaterialInfo.Transparency: Single; |
| 1052 |
begin |
| 1053 |
Result := FNode.Transparency; |
| 1054 |
end; |
| 1055 |
|
| 1056 |
function TPhysicalMaterialNode.TMyPhysicalMaterialInfo.GetBaseColor: TVector3; |
| 1057 |
begin |
| 1058 |
Result := FNode.BaseColor; |
| 1059 |
end; |
| 1060 |
|
| 1061 |
procedure TPhysicalMaterialNode.TMyPhysicalMaterialInfo.SetBaseColor(const Value: TVector3); |
| 1062 |
begin |
| 1063 |
FNode.BaseColor := Value; |
| 1064 |
end; |
| 1065 |
|
| 1066 |
function TPhysicalMaterialNode.TMyPhysicalMaterialInfo.Metallic: Single; |
| 1067 |
begin |
| 1068 |
Result := FNode.Metallic; |
| 1069 |
end; |
| 1070 |
|
| 1071 |
function TPhysicalMaterialNode.TMyPhysicalMaterialInfo.Roughness: Single; |
| 1072 |
begin |
| 1073 |
Result := FNode.Roughness; |
| 1074 |
end; |
| 1075 |
|
| 1076 |
{ TPhysicalMaterialNode ------------------------------------------------------ } |
| 1077 |
|
| 1078 |
destructor TPhysicalMaterialNode.Destroy; |
| 1079 |
begin |
| 1080 |
FreeAndNil(FMaterialInfo); |
| 1081 |
inherited; |
| 1082 |
end; |
| 1083 |
|
| 1084 |
function TPhysicalMaterialNode.MaterialInfo: TMaterialInfo; |
| 1085 |
begin |
| 1086 |
if FMaterialInfo = nil then |
| 1087 |
FMaterialInfo := TMyPhysicalMaterialInfo.Create(Self); |
| 1088 |
Result := FMaterialInfo; |
| 1089 |
end; |
| 1090 |
|
| 1091 |
{ TShapeNode ----------------------------------------------------------------- } |
| 1092 |
|
| 1093 |
procedure TShapeNode.GenerateTangents; |
| 1094 |
|
| 1095 |
{ Calculate tangents, having all array (coords, normal, tex coords, tangents) prepared |
| 1096 |
and equal length (CoordsCount). } |
| 1097 |
procedure GenerateTangentsCore(const CoordsCount: Integer; |
| 1098 |
const Coords, Normals: TVector3List; |
| 1099 |
const TexCoords: TVector2List; |
| 1100 |
const Tangents: TVector3List); |
| 1101 |
var |
| 1102 |
TriangleIndexes: TInt32List; |
| 1103 |
Tangent: TVector3; |
| 1104 |
I, Index0, Index1, Index2: Integer; |
| 1105 |
TriangleCoord: TTriangle3; |
| 1106 |
TriangleTexCoord: TTriangle2; |
| 1107 |
begin |
| 1108 |
// Too verbose. |
| 1109 |
// Also, it seems (test on soldier1.blend) that Blender cannot generate these tangents, so this is not easy to address by author. |
| 1110 |
// WritelnLog('Calculating tangents for %s with geometry %s. Consider exporting with tangents already generated, to make loading faster.', [NiceName, Geometry.NiceName]); |
| 1111 |
|
| 1112 |
{ TODO: for now, we only handle TIndexedTriangleSetNode |
| 1113 |
(it is checked that Geometry is of this class in the main body of TShapeNode.GenerateTangents). |
| 1114 |
But we should be able to handle all geometries. |
| 1115 |
|
| 1116 |
We need for this something like TAbstractGeometryNode.Triangulate, |
| 1117 |
instead of only helpers like TAbstractGeometryNode.InternalMakeCoordRanges . |
| 1118 |
We have TShape.Triangulate (using constructed array by TArraysGenerator), |
| 1119 |
but we cannot use TShape or TArraysGenerator here -- as lots of other data |
| 1120 |
for this is not ready yet. |
| 1121 |
We want GenerateTangents here, in X3DNodes, to use them when importing glTF). } |
| 1122 |
|
| 1123 |
TriangleIndexes := TIndexedTriangleSetNode(Geometry).FdIndex.Items; |
| 1124 |
I := 0; |
| 1125 |
while I + 2 < TriangleIndexes.Count do |
| 1126 |
begin |
| 1127 |
Index0 := TriangleIndexes[I ]; |
| 1128 |
Index1 := TriangleIndexes[I + 1]; |
| 1129 |
Index2 := TriangleIndexes[I + 2]; |
| 1130 |
|
| 1131 |
if (Index0 >= CoordsCount) or |
| 1132 |
(Index1 >= CoordsCount) or |
| 1133 |
(Index2 >= CoordsCount) then |
| 1134 |
raise EInvalidGeometryIndex.CreateFmt('Invalid index in geometry: %d, %d, %d, but count is %d', [ |
| 1135 |
Index0, |
| 1136 |
Index1, |
| 1137 |
Index2, |
| 1138 |
CoordsCount |
| 1139 |
]); |
| 1140 |
|
| 1141 |
TriangleCoord.Data[0] := Coords.L[Index0]; |
| 1142 |
TriangleCoord.Data[1] := Coords.L[Index1]; |
| 1143 |
TriangleCoord.Data[2] := Coords.L[Index2]; |
| 1144 |
TriangleTexCoord.Data[0] := TexCoords.L[Index0]; |
| 1145 |
TriangleTexCoord.Data[1] := TexCoords.L[Index1]; |
| 1146 |
TriangleTexCoord.Data[2] := TexCoords.L[Index2]; |
| 1147 |
|
| 1148 |
if not CalculateTangent(true, Tangent, TriangleCoord, TriangleTexCoord) then |
| 1149 |
Tangent := TVector3.One[0]; |
| 1150 |
|
| 1151 |
Tangents.L[Index0] := MakeVectorOrthogonal(Tangent, Normals.L[Index0]); |
| 1152 |
Tangents.L[Index1] := MakeVectorOrthogonal(Tangent, Normals.L[Index1]); |
| 1153 |
Tangents.L[Index2] := MakeVectorOrthogonal(Tangent, Normals.L[Index2]); |
| 1154 |
|
| 1155 |
Inc(I, 3); |
| 1156 |
end; |
| 1157 |
end; |
| 1158 |
|
| 1159 |
var |
| 1160 |
OneSidedMat: TAbstractOneSidedMaterialNode; |
| 1161 |
NormalTextureCoords: TAbstractSingleTextureCoordinateNode; |
| 1162 |
Normal: TNormalNode; |
| 1163 |
Tangent: TTangentNode; |
| 1164 |
CoordsCount: Integer; |
| 1165 |
Coords, Normals: TVector3List; |
| 1166 |
TexCoords: TVector2List; |
| 1167 |
Tangents: TVector3List; |
| 1168 |
begin |
| 1169 |
if { geometry has normals per-vertex } |
| 1170 |
(Geometry is TAbstractComposedGeometryNode) and // also checks Geometry <> nil |
| 1171 |
(TAbstractComposedGeometryNode(Geometry).Normal is TNormalNode) and // also checks Normal <> nil |
| 1172 |
(TAbstractComposedGeometryNode(Geometry).NormalPerVertex) and |
| 1173 |
{ geometry doesn't have tangents } |
| 1174 |
(TAbstractComposedGeometryNode(Geometry).Tangent = nil) and |
| 1175 |
{ appearance has normal texture } |
| 1176 |
(Appearance <> nil) and |
| 1177 |
(Appearance.Material is TAbstractOneSidedMaterialNode) and // checks also Appearance.Material <> nil |
| 1178 |
(TAbstractOneSidedMaterialNode(Appearance.Material).NormalTexture <> nil) then |
| 1179 |
begin |
| 1180 |
OneSidedMat := TAbstractOneSidedMaterialNode(Appearance.Material); |
| 1181 |
NormalTextureCoords := Geometry.FindTextureMapping(OneSidedMat.NormalTextureMapping, false); |
| 1182 |
{ geometry has matching normal texture coordinates } |
| 1183 |
if NormalTextureCoords is TTextureCoordinateNode then // checks also NormalTextureCoords <> nil |
| 1184 |
begin |
| 1185 |
if (Geometry.CoordField <> nil) and |
| 1186 |
(Geometry.CoordField.Value is TCoordinateNode) then |
| 1187 |
begin |
| 1188 |
{ calculate Coords, CoordsCount } |
| 1189 |
Coords := TCoordinateNode(Geometry.CoordField.Value).FdPoint.Items; |
| 1190 |
CoordsCount := Coords.Count; |
| 1191 |
|
| 1192 |
{ detect and abort in case normal / tex coord ordering is more complicated } |
| 1193 |
if (Geometry is TIndexedFaceSetNode) and |
| 1194 |
( (TIndexedFaceSetNode(Geometry).FdNormalIndex.Items.Count <> 0) or |
| 1195 |
(TIndexedFaceSetNode(Geometry).FdTexCoordIndex.Items.Count <> 0) ) then |
| 1196 |
Exit; |
| 1197 |
|
| 1198 |
{ calculate Normals } |
| 1199 |
Normal := TNormalNode(TAbstractComposedGeometryNode(Geometry).Normal); |
| 1200 |
Normals := Normal.FdVector.Items; |
| 1201 |
if CoordsCount <> Normals.Count then |
| 1202 |
Exit; |
| 1203 |
|
| 1204 |
{ calculate TexCoords } |
| 1205 |
TexCoords := TTextureCoordinateNode(NormalTextureCoords).FdPoint.Items; |
| 1206 |
if CoordsCount <> TexCoords.Count then |
| 1207 |
Exit; |
| 1208 |
|
| 1209 |
// TODO: for now we can do this only for IndexedTriangleSet |
| 1210 |
if not (Geometry is TIndexedTriangleSetNode) then |
| 1211 |
Exit; |
| 1212 |
|
| 1213 |
{ calculate Tangents } |
| 1214 |
Tangent := TTangentNode.Create; |
| 1215 |
Tangents := Tangent.FdVector.Items; |
| 1216 |
Tangents.Count := CoordsCount; |
| 1217 |
|
| 1218 |
try |
| 1219 |
GenerateTangentsCore(CoordsCount, Coords, Normals, TexCoords, Tangents); |
| 1220 |
except |
| 1221 |
// in case EInvalidGeometryIndex is raised, do not leak Tangent |
| 1222 |
FreeAndNil(Tangent); |
| 1223 |
raise; |
| 1224 |
end; |
| 1225 |
|
| 1226 |
{ assign this at the end, just in case it will cause TCastleSceneCore processing on change } |
| 1227 |
TAbstractComposedGeometryNode(Geometry).Tangent := Tangent; |
| 1228 |
end; |
| 1229 |
end; |
| 1230 |
end; |
| 1231 |
end; |
| 1232 |
|
| 1233 |
{ TTwoSidedMaterialInfo --------------------------------------------------- } |
| 1234 |
|
| 1235 |
constructor TTwoSidedMaterialNode.TTwoSidedMaterialInfo.Create(ANode: TTwoSidedMaterialNode); |
| 1236 |
begin |
| 1237 |
inherited Create(ANode); |
| 1238 |
FNode := ANode; |
| 1239 |
end; |
| 1240 |
|
| 1241 |
function TTwoSidedMaterialNode.TTwoSidedMaterialInfo.AmbientColor: TVector3; |
| 1242 |
begin |
| 1243 |
Result := FNode.DiffuseColor * FNode.AmbientIntensity; |
| 1244 |
end; |
| 1245 |
|
| 1246 |
function TTwoSidedMaterialNode.TTwoSidedMaterialInfo.GetDiffuseColor: TVector3; |
| 1247 |
begin |
| 1248 |
{ Warn about SeparateBackColor not being implemented, in 1st call to DiffuseColor |
| 1249 |
(most often used method). } |
| 1250 |
if FNode.SeparateBackColor and not WarningSeparateBackColorDone then |
| 1251 |
begin |
| 1252 |
WritelnWarning('TwoSidedMaterial X3D node (%s) with SeparateBackColor = true is not supported yet', [ |
| 1253 |
FNode.NiceName |
| 1254 |
]); |
| 1255 |
WarningSeparateBackColorDone := true; |
| 1256 |
end; |
| 1257 |
Result := FNode.DiffuseColor; |
| 1258 |
end; |
| 1259 |
|
| 1260 |
procedure TTwoSidedMaterialNode.TTwoSidedMaterialInfo.SetDiffuseColor(const Value: TVector3); |
| 1261 |
begin |
| 1262 |
FNode.DiffuseColor := Value; |
| 1263 |
end; |
| 1264 |
|
| 1265 |
function TTwoSidedMaterialNode.TTwoSidedMaterialInfo.GetSpecularColor: TVector3; |
| 1266 |
begin |
| 1267 |
Result := FNode.SpecularColor; |
| 1268 |
end; |
| 1269 |
|
| 1270 |
procedure TTwoSidedMaterialNode.TTwoSidedMaterialInfo.SetSpecularColor(const Value: TVector3); |
| 1271 |
begin |
| 1272 |
FNode.SpecularColor := Value; |
| 1273 |
end; |
| 1274 |
|
| 1275 |
function TTwoSidedMaterialNode.TTwoSidedMaterialInfo.GetEmissiveColor: TVector3; |
| 1276 |
begin |
| 1277 |
Result := FNode.EmissiveColor; |
| 1278 |
end; |
| 1279 |
|
| 1280 |
procedure TTwoSidedMaterialNode.TTwoSidedMaterialInfo.SetEmissiveColor(const Value: TVector3); |
| 1281 |
begin |
| 1282 |
FNode.EmissiveColor := Value; |
| 1283 |
end; |
| 1284 |
|
| 1285 |
function TTwoSidedMaterialNode.TTwoSidedMaterialInfo.Shininess: Single; |
| 1286 |
begin |
| 1287 |
Result := FNode.Shininess; |
| 1288 |
end; |
| 1289 |
|
| 1290 |
function TTwoSidedMaterialNode.TTwoSidedMaterialInfo.ReflectionColor: TVector3; |
| 1291 |
begin |
| 1292 |
Result := TVector3.Zero; |
| 1293 |
end; |
| 1294 |
|
| 1295 |
function TTwoSidedMaterialNode.TTwoSidedMaterialInfo.Transparency: Single; |
| 1296 |
begin |
| 1297 |
Result := FNode.Transparency; |
| 1298 |
end; |
| 1299 |
|
| 1300 |
{ TTwoSidedMaterialNode ------------------------------------------------------ } |
| 1301 |
|
| 1302 |
destructor TTwoSidedMaterialNode.Destroy; |
| 1303 |
begin |
| 1304 |
FreeAndNil(FMaterialInfo); |
| 1305 |
inherited; |
| 1306 |
end; |
| 1307 |
|
| 1308 |
function TTwoSidedMaterialNode.MaterialInfo: TMaterialInfo; |
| 1309 |
begin |
| 1310 |
if FMaterialInfo = nil then |
| 1311 |
FMaterialInfo := TTwoSidedMaterialInfo.Create(Self); |
| 1312 |
Result := FMaterialInfo; |
| 1313 |
end; |
| 1314 |
|
| 1315 |
{ TUnlitMaterialNode.TMyUnlitMaterialInfo --------------------------------------------------- } |
| 1316 |
|
| 1317 |
constructor TUnlitMaterialNode.TMyUnlitMaterialInfo.Create(ANode: TUnlitMaterialNode); |
| 1318 |
begin |
| 1319 |
inherited Create(ANode); |
| 1320 |
FNode := ANode; |
| 1321 |
end; |
| 1322 |
|
| 1323 |
function TUnlitMaterialNode.TMyUnlitMaterialInfo.GetEmissiveColor: TVector3; |
| 1324 |
begin |
| 1325 |
Result := FNode.EmissiveColor; |
| 1326 |
end; |
| 1327 |
|
| 1328 |
procedure TUnlitMaterialNode.TMyUnlitMaterialInfo.SetEmissiveColor(const Value: TVector3); |
| 1329 |
begin |
| 1330 |
FNode.EmissiveColor := Value; |
| 1331 |
end; |
| 1332 |
|
| 1333 |
function TUnlitMaterialNode.TMyUnlitMaterialInfo.Transparency: Single; |
| 1334 |
begin |
| 1335 |
Result := FNode.Transparency; |
| 1336 |
end; |
| 1337 |
|
| 1338 |
{ TUnlitMaterialNode ------------------------------------------------------ } |
| 1339 |
|
| 1340 |
constructor TUnlitMaterialNode.Create(const AX3DName: string; const ABaseUrl: String); |
| 1341 |
begin |
| 1342 |
inherited; |
| 1343 |
// default is different |
| 1344 |
FdEmissiveColor.DefaultValue := Vector3(1, 1, 1); |
| 1345 |
FdEmissiveColor.Value := Vector3(1, 1, 1); |
| 1346 |
end; |
| 1347 |
|
| 1348 |
destructor TUnlitMaterialNode.Destroy; |
| 1349 |
begin |
| 1350 |
FreeAndNil(FMaterialInfo); |
| 1351 |
inherited; |
| 1352 |
end; |
| 1353 |
|
| 1354 |
function TUnlitMaterialNode.MaterialInfo: TMaterialInfo; |
| 1355 |
begin |
| 1356 |
if FMaterialInfo = nil then |
| 1357 |
FMaterialInfo := TMyUnlitMaterialInfo.Create(Self); |
| 1358 |
Result := FMaterialInfo; |
| 1359 |
end; |
| 1360 |
|
| 1361 |
{ register ------------------------------------------------------------------- } |
| 1362 |
|
| 1363 |
procedure RegisterShapeNodes; |
| 1364 |
begin |
| 1365 |
NodesManager.RegisterNodeClasses([ |
| 1366 |
TAppearanceNode, |
| 1367 |
TFillPropertiesNode, |
| 1368 |
TLinePropertiesNode, |
| 1369 |
TMaterialNode, |
| 1370 |
TPhysicalMaterialNode, |
| 1371 |
TShapeNode, |
| 1372 |
TTwoSidedMaterialNode, |
| 1373 |
TUnlitMaterialNode |
| 1374 |
]); |
| 1375 |
end; |
| 1376 |
|
| 1377 |
{$endif read_implementation} |