| 1 |
{ |
| 2 |
Copyright 2020-2024 Andrzej Kilijański (and3md), Michalis Kamburelis. |
| 3 |
|
| 4 |
This file is part of "Castle Game Engine". |
| 5 |
|
| 6 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 7 |
included in this distribution, for details about the copyright. |
| 8 |
|
| 9 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 |
|
| 13 |
---------------------------------------------------------------------------- |
| 14 |
} |
| 15 |
|
| 16 |
{ Load image as a model (TX3DRootNode, which can be loaded to TCastleScene). } |
| 17 |
unit X3DLoadInternalImage; |
| 18 |
|
| 19 |
{$I castleconf.inc} |
| 20 |
|
| 21 |
interface |
| 22 |
|
| 23 |
implementation |
| 24 |
|
| 25 |
uses |
| 26 |
Classes, SysUtils, Generics.Collections, |
| 27 |
CastleImages, CastleLog, CastleUriUtils, CastleStringUtils, |
| 28 |
CastleTextureImages, CastleVectors, X3DNodes, X3DLoad; |
| 29 |
|
| 30 |
type |
| 31 |
|
| 32 |
TImageAsX3DModelLoader = class |
| 33 |
strict private |
| 34 |
FImage: TEncodedImage; |
| 35 |
FBaseUrl: String; |
| 36 |
FDisplayUrl: String; |
| 37 |
|
| 38 |
FLeft: Integer; |
| 39 |
FBottom: Integer; |
| 40 |
FWidth: Integer; |
| 41 |
FHeight: Integer; |
| 42 |
|
| 43 |
FRoot: TX3DRootNode; |
| 44 |
FShapeCoord: TCoordinateNode; |
| 45 |
FShapeTexCoord: TTextureCoordinateNode; |
| 46 |
|
| 47 |
FCoordArray: array of TVector3; |
| 48 |
FTexCoordArray: array of TVector2; |
| 49 |
|
| 50 |
procedure ReadImportSettings; |
| 51 |
|
| 52 |
procedure PrepareX3DRoot; |
| 53 |
|
| 54 |
procedure CalculateCoords; |
| 55 |
|
| 56 |
procedure PrepareShape(const CoordArray: array of TVector3; |
| 57 |
const TexCoordArray: array of TVector2); |
| 58 |
|
| 59 |
public |
| 60 |
constructor Create(const Stream: TStream; const BaseUrl, MimeType: String); |
| 61 |
|
| 62 |
function Load: TX3DRootNode; |
| 63 |
end; |
| 64 |
|
| 65 |
function LoadImageAsNode(const Stream: TStream; const BaseUrl: String): TX3DRootNode; |
| 66 |
var |
| 67 |
ImageLoader: TImageAsX3DModelLoader; |
| 68 |
MimeType: String; |
| 69 |
begin |
| 70 |
MimeType := URIMimeType(BaseUrl); |
| 71 |
if not IsImageMimeType(MimeType, true, false) then |
| 72 |
raise Exception.CreateFmt('Unsupported image MIME type "%s" for loading image "%s"', [ |
| 73 |
MimeType, |
| 74 |
BaseUrl |
| 75 |
]); |
| 76 |
|
| 77 |
ImageLoader := TImageAsX3DModelLoader.Create(Stream, BaseUrl, MimeType); |
| 78 |
try |
| 79 |
Result := ImageLoader.Load; |
| 80 |
finally |
| 81 |
FreeAndNil(ImageLoader); |
| 82 |
end; |
| 83 |
end; |
| 84 |
|
| 85 |
{ TImageAsX3DModelLoader ---------------------------------------------------- } |
| 86 |
|
| 87 |
procedure TImageAsX3DModelLoader.ReadImportSettings; |
| 88 |
var |
| 89 |
SettingsMap: TStringStringMap; |
| 90 |
Setting: {$ifdef FPC}TStringStringMap.TDictionaryPair{$else}TPair<string, string>{$endif}; |
| 91 |
begin |
| 92 |
FLeft := 0; |
| 93 |
FBottom := 0; |
| 94 |
FWidth := 0; |
| 95 |
FHeight := 0; |
| 96 |
|
| 97 |
SettingsMap := TStringStringMap.Create; |
| 98 |
try |
| 99 |
URIGetSettingsFromAnchor(FBaseUrl, SettingsMap); |
| 100 |
for Setting in SettingsMap do |
| 101 |
begin |
| 102 |
if LowerCase(Setting.Key) = 'left' then |
| 103 |
FLeft := StrToInt(Setting.Value) |
| 104 |
else |
| 105 |
if LowerCase(Setting.Key) = 'bottom' then |
| 106 |
FBottom := StrToInt(Setting.Value) |
| 107 |
else |
| 108 |
if LowerCase(Setting.Key) = 'width' then |
| 109 |
FWidth := StrToInt(Setting.Value) |
| 110 |
else |
| 111 |
if LowerCase(Setting.Key) = 'height' then |
| 112 |
FHeight := StrToInt(Setting.Value) |
| 113 |
else |
| 114 |
WritelnWarning('LoadNode(Image)', 'Unknown setting (%s) in "%s" anchor.', |
| 115 |
[Setting.Key, FDisplayUrl]); |
| 116 |
end; |
| 117 |
finally |
| 118 |
FreeAndNil(SettingsMap); |
| 119 |
end; |
| 120 |
|
| 121 |
if FWidth = 0 then |
| 122 |
FWidth := FImage.Width; |
| 123 |
if FHeight = 0 then |
| 124 |
FHeight := FImage.Height; |
| 125 |
end; |
| 126 |
|
| 127 |
procedure TImageAsX3DModelLoader.PrepareX3DRoot; |
| 128 |
begin |
| 129 |
FRoot.Meta['generator'] := 'Castle Game Engine, https://castle-engine.io'; |
| 130 |
FRoot.Meta['source'] := ExtractURIName(FBaseUrl); |
| 131 |
end; |
| 132 |
|
| 133 |
procedure TImageAsX3DModelLoader.CalculateCoords; |
| 134 |
var |
| 135 |
X1, X2, Y1, Y2: Single; |
| 136 |
AnchorX, AnchorY: Single; |
| 137 |
begin |
| 138 |
AnchorX := 0.5; |
| 139 |
AnchorY := 0.5; |
| 140 |
|
| 141 |
X1 := -FWidth * AnchorX; |
| 142 |
X2 := FWidth * (1 - AnchorX); |
| 143 |
Y1 := -FHeight * AnchorY; |
| 144 |
Y2 := FHeight * (1 - AnchorY); |
| 145 |
|
| 146 |
FCoordArray[0] := Vector3(X1, Y1, 0); |
| 147 |
FCoordArray[1] := Vector3(X2, Y1, 0); |
| 148 |
FCoordArray[2] := Vector3(X2, Y2, 0); |
| 149 |
FCoordArray[3] := Vector3(X1, Y2, 0); |
| 150 |
|
| 151 |
X1 := 1 / FImage.Width * FLeft; |
| 152 |
X2 := 1 / FImage.Width * (FLeft + FWidth); |
| 153 |
Y1 := 1 / FImage.Height * FBottom; |
| 154 |
Y2 := 1 / FImage.Height * (FBottom + FHeight); |
| 155 |
|
| 156 |
FTexCoordArray[0] := Vector2(X1, Y1); |
| 157 |
FTexCoordArray[1] := Vector2(X2, Y1); |
| 158 |
FTexCoordArray[2] := Vector2(X2, Y2); |
| 159 |
FTexCoordArray[3] := Vector2(X1, Y2); |
| 160 |
end; |
| 161 |
|
| 162 |
procedure TImageAsX3DModelLoader.PrepareShape( |
| 163 |
const CoordArray: array of TVector3; const TexCoordArray: array of TVector2); |
| 164 |
var |
| 165 |
Material: TUnlitMaterialNode; |
| 166 |
Appearance: TAppearanceNode; |
| 167 |
Shape: TShapeNode; |
| 168 |
Tri: TIndexedTriangleSetNode; |
| 169 |
Tex: TImageTextureNode; |
| 170 |
TexProperties: TTexturePropertiesNode; |
| 171 |
begin |
| 172 |
Material := TUnlitMaterialNode.Create; |
| 173 |
|
| 174 |
Appearance := TAppearanceNode.Create; |
| 175 |
Appearance.Material := Material; |
| 176 |
|
| 177 |
Shape := TShapeNode.Create; |
| 178 |
Shape.Appearance := Appearance; |
| 179 |
|
| 180 |
Tex := TImageTextureNode.Create('', FBaseUrl); |
| 181 |
{ Take FImage ownership, we will not free it here } |
| 182 |
Tex.LoadFromImage(FImage, true, FBaseUrl); |
| 183 |
{ No point in adjusting RepeatS/T: TextureProperties override it. |
| 184 |
Tex.RepeatS := false; |
| 185 |
Tex.RepeatT := false; } |
| 186 |
Appearance.Texture := Tex; |
| 187 |
|
| 188 |
TexProperties := TTexturePropertiesNode.Create; |
| 189 |
TexProperties.MagnificationFilter := magDefault; |
| 190 |
TexProperties.MinificationFilter := minDefault; |
| 191 |
TexProperties.BoundaryModeS := bmClampToEdge; |
| 192 |
TexProperties.BoundaryModeT := bmClampToEdge; |
| 193 |
{ Do not force "power of 2" size, which may prevent mipmaps. |
| 194 |
This seems like a better default (otherwise the resizing underneath |
| 195 |
may cause longer loading time, and loss of quality, if not expected). } |
| 196 |
TexProperties.GuiTexture := true; |
| 197 |
Tex.TextureProperties := TexProperties; |
| 198 |
|
| 199 |
Tri := TIndexedTriangleSetNode.Create; |
| 200 |
Tri.SetIndex([0, 1, 2, 0, 2, 3]); |
| 201 |
Tri.Solid := false; |
| 202 |
|
| 203 |
FShapeCoord := TCoordinateNode.Create('coord'); |
| 204 |
FShapeCoord.SetPoint([ |
| 205 |
CoordArray[0], |
| 206 |
CoordArray[1], |
| 207 |
CoordArray[2], |
| 208 |
CoordArray[3] |
| 209 |
]); |
| 210 |
|
| 211 |
FShapeTexCoord := TTextureCoordinateNode.Create('texcoord'); |
| 212 |
FShapeTexCoord.SetPoint([ |
| 213 |
TexCoordArray[0], |
| 214 |
TexCoordArray[1], |
| 215 |
TexCoordArray[2], |
| 216 |
TexCoordArray[3] |
| 217 |
]); |
| 218 |
|
| 219 |
Tri.Coord := FShapeCoord; |
| 220 |
Tri.TexCoord := FShapeTexCoord; |
| 221 |
Shape.Geometry := Tri; |
| 222 |
|
| 223 |
FRoot.AddChildren(Shape); |
| 224 |
end; |
| 225 |
|
| 226 |
constructor TImageAsX3DModelLoader.Create(const Stream: TStream; const BaseUrl, MimeType: String); |
| 227 |
begin |
| 228 |
inherited Create; |
| 229 |
|
| 230 |
FImage := LoadEncodedImage(Stream, MimeType, []); |
| 231 |
FBaseUrl := BaseUrl; |
| 232 |
FDisplayUrl := UriDisplay(FBaseUrl); |
| 233 |
|
| 234 |
SetLength(FCoordArray, 4); |
| 235 |
SetLength(FTexCoordArray, 4); |
| 236 |
end; |
| 237 |
|
| 238 |
function TImageAsX3DModelLoader.Load: TX3DRootNode; |
| 239 |
begin |
| 240 |
FRoot := nil; |
| 241 |
try |
| 242 |
FRoot := TX3DRootNode.Create; |
| 243 |
ReadImportSettings; |
| 244 |
|
| 245 |
CalculateCoords; |
| 246 |
PrepareShape(FCoordArray, FTexCoordArray); |
| 247 |
|
| 248 |
Result := FRoot; |
| 249 |
except |
| 250 |
FreeAndNil(FRoot); |
| 251 |
raise; |
| 252 |
end; |
| 253 |
end; |
| 254 |
|
| 255 |
var |
| 256 |
ModelFormat: TModelFormat; |
| 257 |
initialization |
| 258 |
ModelFormat := TModelFormat.Create; |
| 259 |
ModelFormat.OnLoad := {$ifdef FPC}@{$endif} LoadImageAsNode; |
| 260 |
ModelFormat.OnLoadForceMemoryStream := true; |
| 261 |
AddImageMimeTypes(ModelFormat.MimeTypes, true, false); |
| 262 |
// We don't list extensions here, would be too long |
| 263 |
ModelFormat.FileFilterName := 'Images'; |
| 264 |
AddImageExtensions(ModelFormat.Extensions, true, false); |
| 265 |
RegisterModelFormat(ModelFormat); |
| 266 |
end. |