| 1 |
{ |
| 2 |
Copyright 2003-2023 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 |
{ Utilities for converting other 3D model formats into VRML/X3D. } |
| 17 |
unit X3DLoadInternalUtils; |
| 18 |
|
| 19 |
{$I castleconf.inc} |
| 20 |
|
| 21 |
interface |
| 22 |
|
| 23 |
uses CastleVectors, X3DNodes; |
| 24 |
|
| 25 |
const |
| 26 |
NiceCreaseAngle = DefaultVRML1CreaseAngle; |
| 27 |
|
| 28 |
{ Store X3D name in file, in format that allows to store @italic(any) possible value |
| 29 |
(regardless of X3D node name limitations). |
| 30 |
Names that are already valid in X3D are stored as-is, |
| 31 |
special names have a prefix CastleEncoded_ added and are encoded (will be decoded later |
| 32 |
with DecodeX3DName). |
| 33 |
|
| 34 |
This allows to set as TX3DNode.X3DName anything, and it will work OK (and survive |
| 35 |
model save+load to file). This is useful to us, e.g. to preserve animation names |
| 36 |
from Spine or glTF in X3D TouchSensor node names. } |
| 37 |
function EncodeX3DName(const S: String): String; |
| 38 |
function DecodeX3DName(const S: String): String; |
| 39 |
|
| 40 |
{ Calculate best possible ambientIntensity. This is a float that tries to |
| 41 |
satisfy the equation AmbientColor = AmbientIntensity * DiffuseColor. |
| 42 |
Suitable for VRML 2.0/X3D Material.ambientIntensity (as there's no |
| 43 |
Material.ambientColor in VRML 2.0/X3D). } |
| 44 |
function AmbientIntensity(const AmbientColor, DiffuseColor: TVector3): Single; overload; |
| 45 |
function AmbientIntensity(const AmbientColor, DiffuseColor: TVector4): Single; overload; |
| 46 |
|
| 47 |
{ Search harder for file named Base inside directory of BaseUrl. |
| 48 |
BaseUrl must be an absolute URL, we will extract path from it. |
| 49 |
Returns URL relative to BaseUrl. |
| 50 |
|
| 51 |
Automatically uses FixRelativeUrl on Base, fixing backslashes to slashes, |
| 52 |
so assuming that this is an old 3D format and backslash should be interpreted |
| 53 |
as directory separator (like on Windows). |
| 54 |
|
| 55 |
We prefer to return just Base, if it exists, or when no alternative exists. |
| 56 |
When Base doesn't exist but some likely alternative exists (e.g. with |
| 57 |
different case), we return it. } |
| 58 |
function SearchTextureFile(const BaseUrl: String; Base: string): string; |
| 59 |
|
| 60 |
{ Convert backslashes to slashes. Use for formats where this interpretation |
| 61 |
of backslash (instead of %-encoding actual backslash, see |
| 62 |
https://sourceforge.net/p/castle-engine/tickets/21/ ) seems more common. } |
| 63 |
function FixRelativeUrl(const Url: String): string; |
| 64 |
|
| 65 |
var |
| 66 |
{ Use CGE X3D extensions when importing the model. |
| 67 |
Set to false to force creating a valid X3D model (but maybe with less functionality). } |
| 68 |
CastleX3dExtensions: Boolean = true; |
| 69 |
|
| 70 |
implementation |
| 71 |
|
| 72 |
uses SysUtils, Math, URIParser, StrUtils, |
| 73 |
CastleStringUtils, CastleFindFiles, CastleLog, CastleUriUtils, CastleUnicode; |
| 74 |
|
| 75 |
const |
| 76 |
EncodedPrefix = 'CastleEncoded_'; |
| 77 |
|
| 78 |
function EncodeX3DName(const S: String): String; |
| 79 |
const |
| 80 |
{ Similar to most conservative version of VRMLNameChars, |
| 81 |
which follow VRML and X3D specifications. } |
| 82 |
NonAllowedChars = [#0..#$1f, ' ', '''', '"', '#', ',', '.', '[', ']', '\', '{', '}', '(', ')', '|', ':']; |
| 83 |
// first character cannot be digit etc. |
| 84 |
NonAllowedFirstChars = NonAllowedChars + ['0'..'9', '-', '+']; |
| 85 |
|
| 86 |
function SafeX3DName(const S: String): Boolean; |
| 87 |
begin |
| 88 |
Result := not ( |
| 89 |
SCharIs(S, 1, NonAllowedFirstChars) or |
| 90 |
(CharsPos(NonAllowedChars, S) <> 0)); |
| 91 |
end; |
| 92 |
|
| 93 |
function DoEncodeUTF8(const S: String): String; |
| 94 |
var |
| 95 |
Iter: TCastleStringIterator; |
| 96 |
C: TUnicodeChar; |
| 97 |
begin |
| 98 |
Result := ''; |
| 99 |
Iter.Start(S); |
| 100 |
while Iter.GetNext do |
| 101 |
begin |
| 102 |
C := Iter.Current; |
| 103 |
|
| 104 |
{ We use $ to mark encoded chars, so we need to encode it too. |
| 105 |
Note that we don't worry NonAllowedFirstChars, |
| 106 |
since we add prefix EncodedPrefix anyway, so S[1] |
| 107 |
is not the first character in the final encoded X3D name. } |
| 108 |
if (C < 128) and (Chr(C) <> '$') and (not CharInSet(Chr(C), NonAllowedChars)) then |
| 109 |
Result := Result + Chr(C) |
| 110 |
else |
| 111 |
Result := Result + '$' + IntToStr(C) + '$'; |
| 112 |
end; |
| 113 |
end; |
| 114 |
|
| 115 |
begin |
| 116 |
if IsPrefix(EncodedPrefix, S, false) then |
| 117 |
WritelnWarning('Encoding X3D name "%s", it seems like you encode it twice (EncodeX3DName)', [S]); |
| 118 |
|
| 119 |
if SafeX3DName(S) then |
| 120 |
Result := S |
| 121 |
else |
| 122 |
Result := EncodedPrefix + DoEncodeUTF8(S); |
| 123 |
end; |
| 124 |
|
| 125 |
function DecodeX3DName(const S: String): String; |
| 126 |
|
| 127 |
function DoDecodeUTF8(const S: String): String; |
| 128 |
var |
| 129 |
I, EndingDollar: Integer; |
| 130 |
CharCode: TUnicodeChar; |
| 131 |
CharCodeInt: Integer; |
| 132 |
begin |
| 133 |
Result := ''; |
| 134 |
I := 1; |
| 135 |
while I <= Length(S) do |
| 136 |
begin |
| 137 |
if S[I] = '$' then |
| 138 |
begin |
| 139 |
EndingDollar := PosEx('$', S, I + 1); |
| 140 |
if EndingDollar = 0 then |
| 141 |
begin |
| 142 |
WritelnWarning('No matching $ found in encoded X3D name %s. Invalid encoding of X3D name, assuming not encoded at all.', [S]); |
| 143 |
// resulting name will be just like S, only with EncodedPrefix removed |
| 144 |
Exit(S); |
| 145 |
end; |
| 146 |
|
| 147 |
if not TryStrToInt(Copy(S, I + 1, EndingDollar - I - 1), CharCodeInt) then |
| 148 |
begin |
| 149 |
WritelnWarning('Invalid UTF-8 code in encoded X3D name %s. Invalid encoding of X3D name, assuming not encoded at all.', [S]); |
| 150 |
Exit(S); |
| 151 |
end; |
| 152 |
CharCode := CharCodeInt; |
| 153 |
|
| 154 |
Result := Result + UnicodeCharToString(CharCode); |
| 155 |
I := EndingDollar + 1; |
| 156 |
end else |
| 157 |
begin |
| 158 |
Result := Result + S[I]; |
| 159 |
Inc(I); |
| 160 |
end; |
| 161 |
end; |
| 162 |
end; |
| 163 |
|
| 164 |
begin |
| 165 |
if IsPrefix(EncodedPrefix, S, false) then |
| 166 |
Result := DoDecodeUTF8(PrefixRemove(EncodedPrefix, S, false)) |
| 167 |
else |
| 168 |
Result := S; |
| 169 |
end; |
| 170 |
|
| 171 |
function AmbientIntensity(const AmbientColor, DiffuseColor: TVector3): Single; |
| 172 |
begin |
| 173 |
Result := 0; |
| 174 |
if not IsZero(DiffuseColor[0]) then |
| 175 |
Result := Result + (AmbientColor[0] / DiffuseColor[0]); |
| 176 |
if not IsZero(DiffuseColor[1]) then |
| 177 |
Result := Result + (AmbientColor[1] / DiffuseColor[1]); |
| 178 |
if not IsZero(DiffuseColor[2]) then |
| 179 |
Result := Result + (AmbientColor[2] / DiffuseColor[2]); |
| 180 |
Result := Result / 3; |
| 181 |
end; |
| 182 |
|
| 183 |
function AmbientIntensity(const AmbientColor, DiffuseColor: TVector4): Single; |
| 184 |
begin |
| 185 |
Result := AmbientIntensity( |
| 186 |
AmbientColor.XYZ, |
| 187 |
DiffuseColor.XYZ); |
| 188 |
end; |
| 189 |
|
| 190 |
function SearchTextureFile(const BaseUrl: String; Base: string): string; |
| 191 |
var |
| 192 |
SomePathDelim: Integer; |
| 193 |
BaseShort, Path: string; |
| 194 |
begin |
| 195 |
Path := ExtractURIPath(BaseUrl); |
| 196 |
Base := FixRelativeUrl(Base); |
| 197 |
|
| 198 |
try |
| 199 |
if SearchFileHard(Path, Base, Result) then |
| 200 |
Exit; |
| 201 |
|
| 202 |
{ According to https://sourceforge.net/tracker/index.php?func=detail&aid=3305661&group_id=200653&atid=974391 |
| 203 |
some archives expect search within textures/ subdirectory. |
| 204 |
Example on http://www.gfx-3d-model.com/2008/06/house-07/#more-445 |
| 205 |
for Wavefront OBJ. } |
| 206 |
if SearchFileHard(Path + 'textures/', Base, Result) then |
| 207 |
begin |
| 208 |
Result := 'textures/' + Result; |
| 209 |
Exit; |
| 210 |
end; |
| 211 |
if SearchFileHard(Path + 'Textures/', Base, Result) then |
| 212 |
begin |
| 213 |
Result := 'Textures/' + Result; |
| 214 |
Exit; |
| 215 |
end; |
| 216 |
|
| 217 |
{ Some invalid models place full (absolute) path inside texture filename. |
| 218 |
Try to handle it, by stripping path part (from any OS), and trying |
| 219 |
to match new name. } |
| 220 |
SomePathDelim := BackCharsPos(['/', '\'], Base); |
| 221 |
if SomePathDelim <> 0 then |
| 222 |
begin |
| 223 |
BaseShort := SEnding(Base, SomePathDelim + 1); |
| 224 |
|
| 225 |
if SearchFileHard(Path, BaseShort, Result) then |
| 226 |
Exit; |
| 227 |
if SearchFileHard(Path + 'textures/', BaseShort, Result) then |
| 228 |
begin |
| 229 |
Result := 'textures/' + Result; |
| 230 |
Exit; |
| 231 |
end; |
| 232 |
if SearchFileHard(Path + 'Textures/', BaseShort, Result) then |
| 233 |
begin |
| 234 |
Result := 'Textures/' + Result; |
| 235 |
Exit; |
| 236 |
end; |
| 237 |
end; |
| 238 |
|
| 239 |
finally |
| 240 |
if Result <> Base then |
| 241 |
{ Texture file found, but not under original name } |
| 242 |
WritelnWarning('Texture', Format('Exact texture URL "%s" not found, using instead "%s"', |
| 243 |
[Base, Result])); |
| 244 |
end; |
| 245 |
|
| 246 |
{ default result if nowhere found } |
| 247 |
Result := Base; |
| 248 |
end; |
| 249 |
|
| 250 |
function FixRelativeUrl(const Url: String): string; |
| 251 |
begin |
| 252 |
Result := SReplaceChars(Url, '\', '/'); |
| 253 |
end; |
| 254 |
|
| 255 |
end. |