| 1 |
{%MainUnit x3dnodes.pas} |
| 2 |
{ |
| 3 |
Copyright 2008-2021 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 |
{ Base type for all node types that specify 3D sources for texture images. } |
| 19 |
TAbstractTexture3DNode = class(TAbstractSingleTextureNode) |
| 20 |
private |
| 21 |
UsedCache: TTexturesVideosCache; |
| 22 |
FTextureLoaded: boolean; |
| 23 |
FTextureImage: TEncodedImage; |
| 24 |
FTextureComposite: TCompositeImage; |
| 25 |
procedure SetTextureLoaded(const Value: boolean); |
| 26 |
strict protected |
| 27 |
{ This does the actual loading of texture data. |
| 28 |
|
| 29 |
You can assume here that texture is not loaded now, |
| 30 |
UsedCache, FTextureImage, FTextureComposite are all nil. |
| 31 |
Try to load the texture data, making WritelnWarning as appropriate, |
| 32 |
and set all UsedCache, FTextureImage, FTextureComposite, FAlphaChannelData. } |
| 33 |
procedure LoadTextureData; virtual; abstract; |
| 34 |
public |
| 35 |
destructor Destroy; override; |
| 36 |
|
| 37 |
{ Load and unload 3D texture. |
| 38 |
|
| 39 |
When loading, we will try to set TextureImage and TextureComposite. |
| 40 |
If loading is successful, TextureImage will be always set to non-nil |
| 41 |
(TextureComposite will be set to non-nil only if image came from some |
| 42 |
composite (DDS, KTX...) image). |
| 43 |
If loading failed, both TextureImage and TextureComposite will be @nil. |
| 44 |
|
| 45 |
Note that even when loading failed (TextureImage remained @nil), |
| 46 |
TextureLoaded value still changes to @true. This is good --- it makes |
| 47 |
assignment @code(TextureLoaded := true) always work, and (more important) |
| 48 |
it prevents trying to load (and displaying uselessly the same |
| 49 |
WritelnWarning) again at successful @code(TextureLoaded := true). |
| 50 |
|
| 51 |
In case of problems (for example when some URL cannot be loaded), |
| 52 |
we will make WritelnWarning. } |
| 53 |
property TextureLoaded: boolean |
| 54 |
read FTextureLoaded write SetTextureLoaded; |
| 55 |
|
| 56 |
property TextureImage: TEncodedImage read FTextureImage; |
| 57 |
property TextureComposite: TCompositeImage read FTextureComposite; |
| 58 |
|
| 59 |
{$I auto_generated_node_helpers/x3dnodes_x3dtexture3dnode.inc} |
| 60 |
end; |
| 61 |
|
| 62 |
{ 3D texture defined as a collection of 2D texture sources at various depths. } |
| 63 |
TComposedTexture3DNode = class(TAbstractTexture3DNode) |
| 64 |
strict private |
| 65 |
FAlphaChannelData: TAlphaChannel; |
| 66 |
strict protected |
| 67 |
function AlphaChannelData: TAlphaChannel; override; |
| 68 |
public |
| 69 |
procedure LoadTextureData; override; |
| 70 |
|
| 71 |
{$I auto_generated_node_helpers/x3dnodes_composedtexture3d.inc} |
| 72 |
end; |
| 73 |
|
| 74 |
{ 3D texture loaded from a single file like DDS. } |
| 75 |
TImageTexture3DNode = class(TAbstractTexture3DNode) |
| 76 |
strict private |
| 77 |
FAlphaChannelData: TAlphaChannel; |
| 78 |
strict protected |
| 79 |
function AlphaChannelData: TAlphaChannel; override; |
| 80 |
procedure LoadTextureData; override; |
| 81 |
public |
| 82 |
constructor Create(const AX3DName: String = ''; const ABaseUrl: String = ''); override; |
| 83 |
|
| 84 |
{$I auto_generated_node_helpers/x3dnodes_imagetexture3d.inc} |
| 85 |
end; |
| 86 |
|
| 87 |
{ 3D texture defined as an explicit array of pixel values inside the X3D file. } |
| 88 |
TPixelTexture3DNode = class(TAbstractTexture3DNode) |
| 89 |
strict private |
| 90 |
FAlphaChannelData: TAlphaChannel; |
| 91 |
strict protected |
| 92 |
function AlphaChannelData: TAlphaChannel; override; |
| 93 |
public |
| 94 |
procedure LoadTextureData; override; |
| 95 |
|
| 96 |
{$I auto_generated_node_helpers/x3dnodes_pixeltexture3d.inc} |
| 97 |
end; |
| 98 |
|
| 99 |
{ 3D texture coordinates, partcularly useful with 3D textures. } |
| 100 |
TTextureCoordinate3DNode = class(TAbstractSingleTextureCoordinateNode) |
| 101 |
{$I auto_generated_node_helpers/x3dnodes_texturecoordinate3d.inc} |
| 102 |
end; |
| 103 |
|
| 104 |
{ 4D (homogeneous 3D) texture coordinates, partcularly useful with 3D textures. } |
| 105 |
TTextureCoordinate4DNode = class(TAbstractSingleTextureCoordinateNode) |
| 106 |
{$I auto_generated_node_helpers/x3dnodes_texturecoordinate4d.inc} |
| 107 |
end; |
| 108 |
|
| 109 |
{ Transformation of texture coordinates by a 4x4 matrix, |
| 110 |
particularly useful with 3D textures. } |
| 111 |
TTextureTransformMatrix3DNode = class(TAbstractSingleTextureTransformNode) |
| 112 |
public |
| 113 |
function TransformMatrix: TMatrix4; override; |
| 114 |
|
| 115 |
{$I auto_generated_node_helpers/x3dnodes_texturetransformmatrix3d.inc} |
| 116 |
end; |
| 117 |
|
| 118 |
{ Transformation of texture coordinates in 3D, |
| 119 |
partcularly useful with 3D textures. } |
| 120 |
TTextureTransform3DNode = class(TAbstractSingleTextureTransformNode) |
| 121 |
public |
| 122 |
function TransformMatrix: TMatrix4; override; |
| 123 |
|
| 124 |
{$I auto_generated_node_helpers/x3dnodes_texturetransform3d.inc} |
| 125 |
end; |
| 126 |
|
| 127 |
{$endif read_interface} |
| 128 |
|
| 129 |
{$ifdef read_implementation} |
| 130 |
|
| 131 |
{ TAbstractTexture3DNode ----------------------------------------------------- } |
| 132 |
|
| 133 |
destructor TAbstractTexture3DNode.Destroy; |
| 134 |
begin |
| 135 |
TextureLoaded := false; |
| 136 |
inherited; |
| 137 |
end; |
| 138 |
|
| 139 |
procedure TAbstractTexture3DNode.SetTextureLoaded(const Value: boolean); |
| 140 |
begin |
| 141 |
if Value <> TextureLoaded then |
| 142 |
begin |
| 143 |
if Value then |
| 144 |
LoadTextureData else |
| 145 |
begin |
| 146 |
if UsedCache <> nil then |
| 147 |
UsedCache.TextureImage_DecReference(FTextureImage, FTextureComposite) else |
| 148 |
begin |
| 149 |
FreeAndNil(FTextureImage); |
| 150 |
FreeAndNil(FTextureComposite); |
| 151 |
end; |
| 152 |
UsedCache := nil; |
| 153 |
end; |
| 154 |
|
| 155 |
FTextureLoaded := Value; |
| 156 |
end; |
| 157 |
end; |
| 158 |
|
| 159 |
{ TComposedTexture3DNode ----------------------------------------------------- } |
| 160 |
|
| 161 |
{ TODO: TComposedTexture3DNode cannnot correctly update on changes |
| 162 |
inside nodes in "texture", just like ComposedCubeMapTexture. } |
| 163 |
|
| 164 |
procedure TComposedTexture3DNode.LoadTextureData; |
| 165 |
|
| 166 |
{ Load one slice (child texture). Returns @nil and makes WritelnWarning if failed. } |
| 167 |
function LoadOneSlice(const Index: Integer): TCastleImage; |
| 168 |
var |
| 169 |
Tex: TAbstractTexture2DNode; |
| 170 |
begin |
| 171 |
if not ((FdTexture[Index] <> nil) and |
| 172 |
(FdTexture[Index] is TAbstractTexture2DNode)) then |
| 173 |
begin |
| 174 |
WritelnWarning('X3D', Format('ComposedTexture3D slice %d is not a valid 2D texture node', |
| 175 |
[Index])); |
| 176 |
Exit(nil); |
| 177 |
end; |
| 178 |
|
| 179 |
Tex := TAbstractTexture2DNode(FdTexture[Index]); |
| 180 |
if not Tex.IsTextureImage then |
| 181 |
begin |
| 182 |
Exit(nil); { Tex.IsTextureImage already did proper WritelnWarning } |
| 183 |
end; |
| 184 |
|
| 185 |
if not (Tex.TextureImage is TCastleImage) then |
| 186 |
begin |
| 187 |
WritelnWarning('X3D', Format('ComposedTexture3D slice %d is a GPU-compressed texture, not allowed (we need to be able to glue 2D textures for 3D texture)', |
| 188 |
[Index])); |
| 189 |
Exit(nil); |
| 190 |
end; |
| 191 |
|
| 192 |
Result := TCastleImage(Tex.TextureImage); |
| 193 |
|
| 194 |
{ If any slice has full-range alpha, then assume whole texture does. } |
| 195 |
AlphaMaxVar(FAlphaChannelData, Tex.AlphaChannelFinal); |
| 196 |
end; |
| 197 |
|
| 198 |
var |
| 199 |
I: Integer; |
| 200 |
Slice: TCastleImage; |
| 201 |
begin |
| 202 |
if FdTexture.Count = 0 then |
| 203 |
begin |
| 204 |
WritelnWarning('X3D', 'ComposedTexture3D has no textures'); |
| 205 |
Exit; |
| 206 |
end; |
| 207 |
|
| 208 |
FAlphaChannelData := acNone; |
| 209 |
|
| 210 |
{ First Slice is treated specially, as it determines width/height/class |
| 211 |
of the resulting image. } |
| 212 |
|
| 213 |
Slice := LoadOneSlice(0); |
| 214 |
if Slice = nil then Exit; |
| 215 |
|
| 216 |
FTextureImage := TCastleImageClass(Slice.ClassType).Create( |
| 217 |
Slice.Width, Slice.Height, FdTexture.Count); |
| 218 |
|
| 219 |
try |
| 220 |
Move(Slice.RawPixels^, TCastleImage(FTextureImage).RawPixels^, |
| 221 |
Slice.Width * Slice.Height * Slice.PixelSize); |
| 222 |
|
| 223 |
for I := 1 to FdTexture.Count - 1 do |
| 224 |
begin |
| 225 |
Slice := LoadOneSlice(I); |
| 226 |
if Slice = nil then |
| 227 |
begin |
| 228 |
FreeAndNil(FTextureImage); |
| 229 |
Exit; |
| 230 |
end; |
| 231 |
|
| 232 |
if (Slice.Width <> FTextureImage.Width) or |
| 233 |
(Slice.Height <> FTextureImage.Height) or |
| 234 |
(Slice.ClassType <> FTextureImage.ClassType) then |
| 235 |
begin |
| 236 |
WritelnWarning('X3D', Format('ComposedTexture3D slice %d doesn''t have matching size/type (%d x %d of %s), all slices must have the same size/type (first slice had %d x %d of %s)', |
| 237 |
[I, Slice .Width, Slice .Height, Slice .ClassName, |
| 238 |
FTextureImage.Width, FTextureImage.Height, FTextureImage.ClassName])); |
| 239 |
FreeAndNil(FTextureImage); |
| 240 |
Exit; |
| 241 |
end; |
| 242 |
|
| 243 |
Move(Slice.RawPixels^, TCastleImage(FTextureImage).PixelPtr(0, 0, I)^, |
| 244 |
Slice.Width * Slice.Height * Slice.PixelSize); |
| 245 |
end; |
| 246 |
except |
| 247 |
FreeAndNil(FTextureImage); |
| 248 |
raise; |
| 249 |
end; |
| 250 |
end; |
| 251 |
|
| 252 |
function TComposedTexture3DNode.AlphaChannelData: TAlphaChannel; |
| 253 |
begin |
| 254 |
Result := FAlphaChannelData; |
| 255 |
end; |
| 256 |
|
| 257 |
{ TImageTexture3DNode -------------------------------------------------------- } |
| 258 |
|
| 259 |
constructor TImageTexture3DNode.Create(const AX3DName, ABaseUrl: String); |
| 260 |
begin |
| 261 |
inherited; |
| 262 |
AddFunctionality(TUrlFunctionality.Create(Self)); |
| 263 |
end; |
| 264 |
|
| 265 |
procedure TImageTexture3DNode.LoadTextureData; |
| 266 |
var |
| 267 |
I: Integer; |
| 268 |
FullUrl: String; |
| 269 |
begin |
| 270 |
for I := 0 to FdUrl.Items.Count - 1 do |
| 271 |
begin |
| 272 |
FullUrl := PathFromBaseUrl(FdUrl.Items[I]); |
| 273 |
|
| 274 |
try |
| 275 |
FTextureImage := X3DCache.TextureImage_IncReference( |
| 276 |
FullUrl, FTextureComposite, FAlphaChannelData); |
| 277 |
except |
| 278 |
on E: Exception do |
| 279 |
begin |
| 280 |
FTextureComposite := nil; |
| 281 |
WritelnWarning('X3D', Format('Error when loading ImageTexture3D file "%s": %s', [FullUrl, E.Message])); |
| 282 |
Continue; |
| 283 |
end; |
| 284 |
end; |
| 285 |
|
| 286 |
if FTextureComposite = nil then |
| 287 |
begin |
| 288 |
X3DCache.TextureImage_DecReference(FTextureImage, FTextureComposite); |
| 289 |
WritelnWarning('X3D', Format('Only composite formats (DDS, KTX...) are supported for ImageTexture3D node, but URL is "%s"', [FullUrl])); |
| 290 |
Continue; |
| 291 |
end; |
| 292 |
|
| 293 |
if FTextureComposite.CompositeType <> ctVolume then |
| 294 |
begin |
| 295 |
X3DCache.TextureImage_DecReference(FTextureImage, FTextureComposite); |
| 296 |
WritelnWarning('X3D', Format('Composite image "%s" given for ImageTexture3D doesn''t describe a volume (3d) texture', [FullUrl])); |
| 297 |
Continue; |
| 298 |
end; |
| 299 |
|
| 300 |
{ Success } |
| 301 |
UsedCache := X3DCache; |
| 302 |
Exit; |
| 303 |
end; |
| 304 |
end; |
| 305 |
|
| 306 |
function TImageTexture3DNode.AlphaChannelData: TAlphaChannel; |
| 307 |
begin |
| 308 |
Result := FAlphaChannelData; |
| 309 |
end; |
| 310 |
|
| 311 |
{ TPixelTexture3DNode -------------------------------------------------------- } |
| 312 |
|
| 313 |
procedure TPixelTexture3DNode.LoadTextureData; |
| 314 |
var |
| 315 |
TexComponents, Width, Height, Depth: Cardinal; |
| 316 |
G: PByte; |
| 317 |
GA: PVector2Byte; |
| 318 |
RGB: PVector3Byte; |
| 319 |
RGBA: PVector4Byte; |
| 320 |
I: Integer; |
| 321 |
begin |
| 322 |
if FdImage.Count < 4 then |
| 323 |
begin |
| 324 |
WritelnWarning('X3D', 'PixelTexture3D.image has too few items (must have at least 4)'); |
| 325 |
Exit; |
| 326 |
end; |
| 327 |
|
| 328 |
TexComponents := FdImage.Items.Items[0]; |
| 329 |
Width := FdImage.Items.Items[1]; |
| 330 |
Height := FdImage.Items.Items[2]; |
| 331 |
Depth := FdImage.Items.Items[3]; |
| 332 |
|
| 333 |
if Cardinal(FdImage.Count) < 4 + Width * Height * Depth then |
| 334 |
begin |
| 335 |
WritelnWarning('X3D', Format('PixelTexture3D.image has too few items, must have at least %d (= 4 + Width * Height * Depth = 4 + %d * %d * %d)', |
| 336 |
[ 4 + Width * Height * Depth, Width, Height, Depth ])); |
| 337 |
Exit; |
| 338 |
end; |
| 339 |
|
| 340 |
case TexComponents of |
| 341 |
0: { 0 components is allowed by the spec, and results in an empty image }; |
| 342 |
1: begin |
| 343 |
FTextureImage := TGrayscaleImage.Create(Width, Height, Depth); |
| 344 |
G := TGrayscaleImage(FTextureImage).Pixels; |
| 345 |
for I := 0 to Width * Height * Depth - 1 do |
| 346 |
begin |
| 347 |
DecodeX3DColor(FdImage.Items.Items[4 + I], G^); |
| 348 |
Inc(G); |
| 349 |
end; |
| 350 |
end; |
| 351 |
2: begin |
| 352 |
FTextureImage := TGrayscaleAlphaImage.Create(Width, Height, Depth); |
| 353 |
GA := TGrayscaleAlphaImage(FTextureImage).Pixels; |
| 354 |
for I := 0 to Width * Height * Depth - 1 do |
| 355 |
begin |
| 356 |
DecodeX3DColor(FdImage.Items.Items[4 + I], GA^); |
| 357 |
Inc(GA); |
| 358 |
end; |
| 359 |
end; |
| 360 |
3: begin |
| 361 |
FTextureImage := TRGBImage.Create(Width, Height, Depth); |
| 362 |
RGB := TRGBImage(FTextureImage).Pixels; |
| 363 |
for I := 0 to Width * Height * Depth - 1 do |
| 364 |
begin |
| 365 |
DecodeX3DColor(FdImage.Items.Items[4 + I], RGB^); |
| 366 |
Inc(RGB); |
| 367 |
end; |
| 368 |
end; |
| 369 |
4: begin |
| 370 |
FTextureImage := TRGBAlphaImage.Create(Width, Height, Depth); |
| 371 |
RGBA := TRGBAlphaImage(FTextureImage).Pixels; |
| 372 |
for I := 0 to Width * Height * Depth - 1 do |
| 373 |
begin |
| 374 |
DecodeX3DColor(FdImage.Items.Items[4 + I], RGBA^); |
| 375 |
Inc(RGBA); |
| 376 |
end; |
| 377 |
end; |
| 378 |
else |
| 379 |
begin |
| 380 |
WritelnWarning('X3D', 'PixelTexture3D.image has invalid components count (must be between 0..4)'); |
| 381 |
Exit; |
| 382 |
end; |
| 383 |
end; |
| 384 |
|
| 385 |
FAlphaChannelData := FTextureImage.AlphaChannel; |
| 386 |
end; |
| 387 |
|
| 388 |
function TPixelTexture3DNode.AlphaChannelData: TAlphaChannel; |
| 389 |
begin |
| 390 |
Result := FAlphaChannelData; |
| 391 |
end; |
| 392 |
|
| 393 |
{ TTextureTransformMatrix3DNode ---------------------------------------------- } |
| 394 |
|
| 395 |
function TTextureTransformMatrix3DNode.TransformMatrix: TMatrix4; |
| 396 |
begin |
| 397 |
Result := FdMatrix.Value; |
| 398 |
end; |
| 399 |
|
| 400 |
{ TTextureTransform3DNode ---------------------------------------------------- } |
| 401 |
|
| 402 |
function TTextureTransform3DNode.TransformMatrix: TMatrix4; |
| 403 |
begin |
| 404 |
Result := |
| 405 |
TranslationMatrix(FdTranslation.Value + FdCenter.Value) * |
| 406 |
RotationMatrixRad(FdRotation.RotationRad, FdRotation.Axis) * |
| 407 |
ScalingMatrix(FdScale.Value) * |
| 408 |
TranslationMatrix(-FdCenter.Value); |
| 409 |
end; |
| 410 |
|
| 411 |
procedure RegisterTexturing3DNodes; |
| 412 |
begin |
| 413 |
NodesManager.RegisterNodeClasses([ |
| 414 |
TComposedTexture3DNode, |
| 415 |
TImageTexture3DNode, |
| 416 |
TPixelTexture3DNode, |
| 417 |
TTextureCoordinate3DNode, |
| 418 |
TTextureCoordinate4DNode, |
| 419 |
TTextureTransformMatrix3DNode, |
| 420 |
TTextureTransform3DNode |
| 421 |
]); |
| 422 |
end; |
| 423 |
|
| 424 |
{$endif read_implementation} |