| 1 |
{ |
| 2 |
Copyright 2008-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 |
{$ifdef read_interface} |
| 17 |
|
| 18 |
type |
| 19 |
TStringTextureNodeMap = {$ifdef FPC}specialize{$endif} TDictionary<string, TAbstractTextureNode>; |
| 20 |
|
| 21 |
TColladaEffect = class |
| 22 |
Appearance: TAppearanceNode; |
| 23 |
{ If this effect contains a texture for diffuse, this is the name |
| 24 |
of texture coordinates in Collada. |
| 25 |
For now not used (we always take first input with TEXCOORD semantic |
| 26 |
to control the main texture, that affects the look (after lighting |
| 27 |
calculation, so not only diffuse). } |
| 28 |
DiffuseTexCoordName: string; |
| 29 |
destructor Destroy; override; |
| 30 |
end; |
| 31 |
|
| 32 |
{$endif read_interface} |
| 33 |
|
| 34 |
{$ifdef read_implementation} |
| 35 |
|
| 36 |
destructor TColladaEffect.Destroy; |
| 37 |
begin |
| 38 |
FreeIfUnusedAndNil(Appearance); |
| 39 |
inherited; |
| 40 |
end; |
| 41 |
|
| 42 |
type |
| 43 |
TColladaEffectList = class({$ifdef FPC}specialize{$endif} TObjectList<TColladaEffect>) |
| 44 |
{ Find a TColladaEffect with given Name, @nil if not found. } |
| 45 |
function Find(const Name: string): TColladaEffect; |
| 46 |
end; |
| 47 |
|
| 48 |
function TColladaEffectList.Find(const Name: string): TColladaEffect; |
| 49 |
var |
| 50 |
I: Integer; |
| 51 |
begin |
| 52 |
for I := 0 to Count - 1 do |
| 53 |
if Items[I].Appearance.X3DName = Name then |
| 54 |
Exit(Items[I]); |
| 55 |
Result := nil; |
| 56 |
end; |
| 57 |
|
| 58 |
{$endif read_implementation} |
| 59 |
|
| 60 |
{$ifdef read_implementation_nested} |
| 61 |
|
| 62 |
var |
| 63 |
{ List of Collada effects. Each contains an X3D Appearance node, |
| 64 |
with a name equal to Collada effect name. } |
| 65 |
Effects: TColladaEffectList; |
| 66 |
|
| 67 |
{ Read elements of type "common_color_or_texture_type" in Collada >= 1.4.x. |
| 68 |
If we have <color>, return this color (texture and coord names are then set to empty). |
| 69 |
If we have <texture>, return white color (and the appropriate texture |
| 70 |
and coord names; if texture name was empty, you want to treat it like not |
| 71 |
existing anyway). } |
| 72 |
function ReadColorOrTexture(Element: TDOMElement; |
| 73 |
out TextureName, TexCoordName: string): TCastleColor; |
| 74 |
var |
| 75 |
Child: TDOMElement; |
| 76 |
begin |
| 77 |
TextureName := ''; |
| 78 |
TexCoordName := ''; |
| 79 |
Result := White; |
| 80 |
Child := Element.ChildElement('color', false); |
| 81 |
if Child <> nil then |
| 82 |
begin |
| 83 |
Result := Vector4FromStr(Child.TextData); |
| 84 |
end else |
| 85 |
begin |
| 86 |
Child := Element.ChildElement('texture', false); |
| 87 |
if Child <> nil then |
| 88 |
begin |
| 89 |
Child.AttributeString('texture', TextureName); |
| 90 |
Child.AttributeString('texcoord', TexCoordName); |
| 91 |
end; |
| 92 |
end; |
| 93 |
end; |
| 94 |
|
| 95 |
function ReadColorOrTextureRGB(Element: TDOMElement; |
| 96 |
out TextureName, TexCoordName: string): TCastleColorRGB; |
| 97 |
begin |
| 98 |
{ use ReadColorOrTexture and just ignore the alpha value } |
| 99 |
Result := ReadColorOrTexture(Element, TextureName, TexCoordName).XYZ; |
| 100 |
end; |
| 101 |
|
| 102 |
{ Read elements of type "common_color_or_texture_type" in Collada >= 1.4.x, |
| 103 |
but allow only color specification. } |
| 104 |
function ReadColorRGB(Element: TDOMElement): TVector3; |
| 105 |
var |
| 106 |
IgnoreTextureName, IgnoreTexCoordName: string; |
| 107 |
begin |
| 108 |
Result := ReadColorOrTextureRGB(Element, IgnoreTextureName, IgnoreTexCoordName); |
| 109 |
end; |
| 110 |
|
| 111 |
{ Read elements of type "common_color_or_texture_type" in Collada >= 1.4.x, |
| 112 |
but allow only color specification. } |
| 113 |
function ReadColor(Element: TDOMElement): TVector4; |
| 114 |
var |
| 115 |
IgnoreTextureName, IgnoreTexCoordName: string; |
| 116 |
begin |
| 117 |
Result := ReadColorOrTexture(Element, IgnoreTextureName, IgnoreTexCoordName); |
| 118 |
end; |
| 119 |
|
| 120 |
{ Read elements of type "common_float_or_param_type" in Collada >= 1.4.x. } |
| 121 |
function ReadFloatOrParam(Element: TDOMElement): Float; |
| 122 |
var |
| 123 |
FloatElement: TDOMElement; |
| 124 |
begin |
| 125 |
FloatElement := Element.ChildElement('float', false); |
| 126 |
if FloatElement <> nil then |
| 127 |
begin |
| 128 |
Result := StrToFloatDot(FloatElement.TextData); |
| 129 |
end else |
| 130 |
{ We don't support anything else than <float> here, just use |
| 131 |
default 1 eventually. } |
| 132 |
Result := 1.0; |
| 133 |
end; |
| 134 |
|
| 135 |
{ Read <transparent> element. |
| 136 |
Returns transparency (1 = transparent, 0 = opaque). } |
| 137 |
function ReadTransparentElement(const Element: TDOMElement): Single; |
| 138 |
var |
| 139 |
OpaqueMode: string; |
| 140 |
Color: TCastleColor; |
| 141 |
begin |
| 142 |
OpaqueMode := Element.AttributeStringDef('opaque', 'A_ONE'); |
| 143 |
Color := ReadColor(Element); |
| 144 |
|
| 145 |
if OpaqueMode = 'A_ONE' then |
| 146 |
begin |
| 147 |
{ Collada 1.4 spefication about A_ONE: |
| 148 |
Takes the transparency information from the color’s |
| 149 |
alpha channel, where the value 1.0 is opaque. } |
| 150 |
Result := 1 - Color.W; |
| 151 |
end else |
| 152 |
if OpaqueMode = 'RGB_ZERO' then |
| 153 |
begin |
| 154 |
{ Collada 1.4 spefication about RGB_ZERO: |
| 155 |
Takes the transparency information from the color’s red, green, |
| 156 |
and blue channels, where the value 0.0 is opaque, |
| 157 |
with each channel modulated independently. } |
| 158 |
Result := Color.XYZ.Average; |
| 159 |
end else |
| 160 |
begin |
| 161 |
WritelnWarning('Invalid Collada opaque mode "%s"', [OpaqueMode]); |
| 162 |
Result := 0; |
| 163 |
end; |
| 164 |
end; |
| 165 |
|
| 166 |
{ Read <effect>. Only for Collada >= 1.4.x. |
| 167 |
Adds effect to the Effects list. } |
| 168 |
procedure ReadEffect(EffectElement: TDOMElement); |
| 169 |
var |
| 170 |
{ Effect instance and nodes, available to local procedures inside ReadEffect. } |
| 171 |
Effect: TColladaEffect; |
| 172 |
Appearance: TAppearanceNode; |
| 173 |
Mat: TMaterialNode; |
| 174 |
|
| 175 |
// TODO: generate CommonSurfaceShader as an alternative? |
| 176 |
|
| 177 |
{ Map of <surface> names to images (references from Images list) } |
| 178 |
Surfaces: TStringTextureNodeMap; |
| 179 |
{ Map of <sampler2D> names to images (references from Images list) } |
| 180 |
Samplers2D: TStringTextureNodeMap; |
| 181 |
|
| 182 |
procedure ReadTechnique(TechniqueElement: TDOMElement); |
| 183 |
var |
| 184 |
Image: TAbstractTextureNode; |
| 185 |
TechniqueChild: TDOMElement; |
| 186 |
I: TXMLElementIterator; |
| 187 |
DiffuseTextureName, DiffuseTexCoordName: string; |
| 188 |
Transparency: Single; |
| 189 |
HasTransparency: boolean; |
| 190 |
begin |
| 191 |
{ We actually treat <phong> and <blinn> and even <lambert> elements the same. |
| 192 |
X3D lighting equations specify that always Blinn |
| 193 |
(half-vector) technique is used. What's much more practically |
| 194 |
important, OpenGL uses Blinn method. So actually I always do |
| 195 |
blinn method (at least for real-time rendering). } |
| 196 |
TechniqueChild := TechniqueElement.ChildElement('phong', false); |
| 197 |
if TechniqueChild = nil then |
| 198 |
TechniqueChild := TechniqueElement.ChildElement('blinn', false); |
| 199 |
if TechniqueChild = nil then |
| 200 |
TechniqueChild := TechniqueElement.ChildElement('lambert', false); |
| 201 |
|
| 202 |
if TechniqueChild <> nil then |
| 203 |
begin |
| 204 |
{ <transparent> (color) and <transparency> (float) should |
| 205 |
be multiplied with each other. So says the Collada spec |
| 206 |
("Determining Transparency (Opacity)" in Chapter 7, |
| 207 |
and forum https://collada.org/public_forum/viewtopic.php?t=386). |
| 208 |
When only one is specified, the other is like 1. |
| 209 |
However, when neither one is specified, we obviously want to leave |
| 210 |
the model opaque (transparency = 0) so we need boolean HasTransparency. } |
| 211 |
Transparency := 1; |
| 212 |
HasTransparency := false; |
| 213 |
|
| 214 |
I := TechniqueChild.ChildrenIterator; |
| 215 |
try |
| 216 |
while I.GetNext do |
| 217 |
begin |
| 218 |
if I.Current.TagName = 'emission' then |
| 219 |
Mat.FdEmissiveColor.Value := ReadColorRGB(I.Current) else |
| 220 |
if I.Current.TagName = 'ambient' then |
| 221 |
Mat.FdAmbientIntensity.Value := ReadColorRGB(I.Current).Average else |
| 222 |
if I.Current.TagName = 'diffuse' then |
| 223 |
begin |
| 224 |
Mat.FdDiffuseColor.Value := ReadColorOrTextureRGB(I.Current, |
| 225 |
DiffuseTextureName, DiffuseTexCoordName); |
| 226 |
if DiffuseTextureName <> '' then |
| 227 |
begin |
| 228 |
Samplers2D.TryGetValue(DiffuseTextureName, Image); // Image will be nil if not found |
| 229 |
if Image <> nil then |
| 230 |
begin |
| 231 |
Effect.DiffuseTexCoordName := DiffuseTexCoordName; |
| 232 |
Appearance.FdTexture.Value := Image; |
| 233 |
end else |
| 234 |
begin |
| 235 |
Image := Images.FindName(DiffuseTextureName) as TAbstractTextureNode; |
| 236 |
if Image <> nil then |
| 237 |
begin |
| 238 |
Effect.DiffuseTexCoordName := DiffuseTexCoordName; |
| 239 |
Appearance.FdTexture.Value := Image; |
| 240 |
{ Happens e.g. on "Private Section/Faerie_Forrest_DAY/Faerie_Forrest_DAY.dae" |
| 241 |
from collada.org/owl models. } |
| 242 |
WritelnWarning('Collada', Format('<diffuse> texture refers to missing sampler2D name "%s". Found <image> with the same id, will use it', |
| 243 |
[DiffuseTextureName])); |
| 244 |
end else |
| 245 |
WritelnWarning('Collada', Format('<diffuse> texture refers to missing sampler2D name "%s"', |
| 246 |
[DiffuseTextureName])); |
| 247 |
end; |
| 248 |
end; |
| 249 |
end else |
| 250 |
if I.Current.TagName = 'specular' then |
| 251 |
Mat.FdSpecularColor.Value := ReadColorRGB(I.Current) else |
| 252 |
if I.Current.TagName = 'shininess' then |
| 253 |
Mat.FdShininess.Value := ReadFloatOrParam(I.Current) / 128.0 else |
| 254 |
if I.Current.TagName = 'reflective' then |
| 255 |
{Mat.FdMirrorColor.Value := } ReadColorRGB(I.Current) else |
| 256 |
if I.Current.TagName = 'reflectivity' then |
| 257 |
begin |
| 258 |
if AllowCastleExtensions then |
| 259 |
Mat.FdMirror.Value := ReadFloatOrParam(I.Current) |
| 260 |
else |
| 261 |
ReadFloatOrParam(I.Current); |
| 262 |
end else |
| 263 |
if I.Current.TagName = 'transparent' then |
| 264 |
begin |
| 265 |
Transparency := Transparency * ReadTransparentElement(I.Current); |
| 266 |
HasTransparency := true; |
| 267 |
end else |
| 268 |
if I.Current.TagName = 'transparency' then |
| 269 |
begin |
| 270 |
Transparency := Transparency * ReadFloatOrParam(I.Current); |
| 271 |
HasTransparency := true; |
| 272 |
end else |
| 273 |
if I.Current.TagName = 'index_of_refraction' then |
| 274 |
{Mat.FdIndexOfRefraction.Value := } ReadFloatOrParam(I.Current); |
| 275 |
end; |
| 276 |
finally FreeAndNil(I) end; |
| 277 |
|
| 278 |
if HasTransparency then |
| 279 |
Mat.FdTransparency.Value := Transparency; |
| 280 |
end; |
| 281 |
end; |
| 282 |
|
| 283 |
{ Read <newparam>. } |
| 284 |
procedure ReadNewParam(Element: TDOMElement); |
| 285 |
var |
| 286 |
Child: TDOMElement; |
| 287 |
Name, RefersTo: string; |
| 288 |
Image: TAbstractTextureNode; |
| 289 |
begin |
| 290 |
if Element.AttributeString('sid', Name) then |
| 291 |
begin |
| 292 |
Child := Element.ChildElement('surface', false); |
| 293 |
if Child <> nil then |
| 294 |
begin |
| 295 |
{ Read <surface>. It has <init_from>, referring to name on Images. } |
| 296 |
RefersTo := ReadChildText(Child, 'init_from'); |
| 297 |
Image := Images.FindName(RefersTo) as TAbstractTextureNode; |
| 298 |
if Image <> nil then |
| 299 |
Surfaces.AddOrSetValue(Name, Image) else |
| 300 |
WritelnWarning('Collada', Format('<surface> refers to missing image name "%s"', |
| 301 |
[RefersTo])); |
| 302 |
end else |
| 303 |
begin |
| 304 |
Child := Element.ChildElement('sampler2D', false); |
| 305 |
if Child <> nil then |
| 306 |
begin |
| 307 |
{ Read <sampler2D>. It has <source>, referring to name on Surfaces. } |
| 308 |
RefersTo := ReadChildText(Child, 'source'); |
| 309 |
Surfaces.TryGetValue(RefersTo, Image); // Image will be nil if not found |
| 310 |
if Image <> nil then |
| 311 |
Samplers2D.AddOrSetValue(Name, Image) |
| 312 |
else |
| 313 |
WritelnWarning('Collada', Format('<sampler2D> refers to missing surface name "%s"', |
| 314 |
[RefersTo])); |
| 315 |
end; { else not handled <newparam> } |
| 316 |
end; |
| 317 |
end; |
| 318 |
end; |
| 319 |
|
| 320 |
var |
| 321 |
Id: string; |
| 322 |
I: TXMLElementIterator; |
| 323 |
ProfileElement: TDOMElement; |
| 324 |
begin |
| 325 |
if not EffectElement.AttributeString('id', Id) then |
| 326 |
Id := ''; |
| 327 |
|
| 328 |
Effect := TColladaEffect.Create; |
| 329 |
Effects.Add(Effect); |
| 330 |
|
| 331 |
Appearance := TAppearanceNode.Create(Id, BaseUrl); |
| 332 |
Effect.Appearance := Appearance; |
| 333 |
|
| 334 |
Mat := TMaterialNode.Create('', BaseUrl); |
| 335 |
Appearance.FdMaterial.Value := Mat; |
| 336 |
|
| 337 |
ProfileElement := EffectElement.ChildElement('profile_COMMON', false); |
| 338 |
if ProfileElement <> nil then |
| 339 |
begin |
| 340 |
Surfaces := TStringTextureNodeMap.Create; |
| 341 |
Samplers2D := TStringTextureNodeMap.Create; |
| 342 |
try |
| 343 |
I := ProfileElement.ChildrenIterator; |
| 344 |
try |
| 345 |
while I.GetNext do |
| 346 |
if I.Current.TagName = 'technique' then |
| 347 |
{ Actually only one <technique> within <profile_COMMON> is allowed. |
| 348 |
But, since we loop anyway, it's not a problem to handle many. } |
| 349 |
ReadTechnique(I.Current) else |
| 350 |
if I.Current.TagName = 'newparam' then |
| 351 |
ReadNewParam(I.Current); |
| 352 |
finally FreeAndNil(I) end; |
| 353 |
finally |
| 354 |
FreeAndNil(Surfaces); |
| 355 |
FreeAndNil(Samplers2D); |
| 356 |
end; |
| 357 |
end; |
| 358 |
end; |
| 359 |
|
| 360 |
{ Read <library_effects>. Only for Collada >= 1.4.x. |
| 361 |
All effects are added to the Effects list. } |
| 362 |
procedure ReadLibraryEffects(LibraryElement: TDOMElement); |
| 363 |
var |
| 364 |
I: TXMLElementIterator; |
| 365 |
begin |
| 366 |
I := LibraryElement.ChildrenIterator('effect'); |
| 367 |
try |
| 368 |
while I.GetNext do |
| 369 |
ReadEffect(I.Current); |
| 370 |
{ other I.Current.TagName not supported for now } |
| 371 |
finally FreeAndNil(I) end; |
| 372 |
end; |
| 373 |
|
| 374 |
{$endif read_implementation_nested} |