| 1 |
{ |
| 2 |
Copyright 2008-2018 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 Collada. } |
| 17 |
unit X3DLoadInternalCollada; |
| 18 |
|
| 19 |
{$I castleconf.inc} |
| 20 |
|
| 21 |
interface |
| 22 |
|
| 23 |
implementation |
| 24 |
|
| 25 |
uses SysUtils, Classes, DOM, XMLRead, Generics.Collections, Math, URIParser, |
| 26 |
X3DNodes, X3DLoad, CastleUtils, CastleStringUtils, CastleVectors, CastleColors, |
| 27 |
CastleXmlUtils, CastleLog, CastleClassUtils, X3DLoadInternalUtils, |
| 28 |
CastleDownload, CastleUriUtils; |
| 29 |
|
| 30 |
{ Implementation written based on Collada 1.3.1 and 1.4.1 specifications. |
| 31 |
Should handle any Collada 1.3.x or 1.4.x or 1.5.x version. |
| 32 |
http://www.gamasutra.com/view/feature/1580/introduction_to_collada.php?page=6 |
| 33 |
suggests that "specification stayed quite stable between 1.1 and 1.3.1", |
| 34 |
which means that older versions (< 1.3.1) may be handled too. } |
| 35 |
|
| 36 |
var |
| 37 |
{ If @true we may use some of our engine specific |
| 38 |
extensions. For example, Material.mirror may be <> 0, |
| 39 |
see [https://castle-engine.io/x3d_extensions.php#section_ext_material_mirror].) } |
| 40 |
AllowCastleExtensions: Boolean = true; |
| 41 |
|
| 42 |
{ Large missing stuff: |
| 43 |
|
| 44 |
- GPU shaders. We fully support the equivalent X3D nodes (for GLSL), |
| 45 |
so importing this from Collada (converting to X3D) is possible. |
| 46 |
|
| 47 |
- Animations. As an argument for supporting it, Blender *can* export |
| 48 |
some animations to Collada: |
| 49 |
|
| 50 |
- Transformation animation: Blender exports it, |
| 51 |
in a format that is close to Blender animation curves. |
| 52 |
There's a separate info about location.x, location.y etc. animation. |
| 53 |
Animation of rotation is animating angle of rotation around the X axis, etc. |
| 54 |
Animations are connected to Collada nodes by |
| 55 |
|
| 56 |
<channel source="#Cube_rotation_euler_Z-sampler" target="Cube/rotationZ.ANGLE"/> |
| 57 |
|
| 58 |
"target" attribute uses Collada id path. |
| 59 |
We'd have to group such animations of a vector components into a single |
| 60 |
animation (in X3D, you cannot animate translation.x separately, |
| 61 |
you only animate whole 3D translation vector). |
| 62 |
This is doable for our importer. |
| 63 |
|
| 64 |
- Shape keys animation from Blender is not exported to Collada, it seems. |
| 65 |
|
| 66 |
- Armature animation: Blender exports armature bones animation. |
| 67 |
Also, armature hierarchy is exported as Collada nodes. |
| 68 |
Armature bone names and weights are exported too. |
| 69 |
|
| 70 |
So armature is exported --- in a format reflecting armature setup in Blender, |
| 71 |
not as a processed effect it has on a model skin. |
| 72 |
Rendering it means performing bones+skin animation at runtime. |
| 73 |
X3D equivalent of this is H-Anim with skin, which we support fully. |
| 74 |
So importing such Collada is possible. |
| 75 |
|
| 76 |
It depends on importing first normal transformation animation |
| 77 |
(as the bones are animated just like normal transformations; |
| 78 |
there's no point in importing skin stuff if you can't import bones animation). |
| 79 |
|
| 80 |
These are all doable ideas, but also not trivial. |
| 81 |
Since Blender can export animations to Collada (and not yet to X3D), |
| 82 |
it would be quite useful. |
| 83 |
Contributions are welcome! |
| 84 |
} |
| 85 |
|
| 86 |
{$define read_interface} |
| 87 |
{$I x3dloadinternalcollada_childrenlist.inc} |
| 88 |
{$I x3dloadinternalcollada_controllers.inc} |
| 89 |
{$I x3dloadinternalcollada_effects.inc} |
| 90 |
{$I x3dloadinternalcollada_primitives.inc} |
| 91 |
{$I x3dloadinternalcollada_geometries.inc} |
| 92 |
{$I x3dloadinternalcollada_integerparser.inc} |
| 93 |
{$I x3dloadinternalcollada_indexes.inc} |
| 94 |
{$I x3dloadinternalcollada_source.inc} |
| 95 |
{$I x3dloadinternalcollada_sources.inc} |
| 96 |
{$I x3dloadinternalcollada_librarynodes.inc} |
| 97 |
{$undef read_interface} |
| 98 |
|
| 99 |
{$define read_implementation} |
| 100 |
{$I x3dloadinternalcollada_childrenlist.inc} |
| 101 |
{$I x3dloadinternalcollada_controllers.inc} |
| 102 |
{$I x3dloadinternalcollada_effects.inc} |
| 103 |
{$I x3dloadinternalcollada_primitives.inc} |
| 104 |
{$I x3dloadinternalcollada_geometries.inc} |
| 105 |
{$I x3dloadinternalcollada_integerparser.inc} |
| 106 |
{$I x3dloadinternalcollada_indexes.inc} |
| 107 |
{$I x3dloadinternalcollada_source.inc} |
| 108 |
{$I x3dloadinternalcollada_sources.inc} |
| 109 |
{$I x3dloadinternalcollada_librarynodes.inc} |
| 110 |
{$undef read_implementation} |
| 111 |
|
| 112 |
{ LoadCollada ---------------------------------------------------------------- } |
| 113 |
|
| 114 |
function LoadCollada(const Stream: TStream; const ProposedBaseUrl: String): TX3DRootNode; |
| 115 |
var |
| 116 |
ResultModel: TGroupNode absolute Result; |
| 117 |
Version14: boolean; //< Collada version >= 1.4.x |
| 118 |
BaseUrl: String; //< use as BaseUrl for all nodes (do not use ProposedBaseUrl for this) |
| 119 |
|
| 120 |
{$define read_interface_nested} |
| 121 |
{$I x3dloadinternalcollada_effects.inc} |
| 122 |
{$I x3dloadinternalcollada_primitives.inc} |
| 123 |
{$I x3dloadinternalcollada_geometries.inc} |
| 124 |
{$I x3dloadinternalcollada_matrix.inc} |
| 125 |
{$I x3dloadinternalcollada_controllers.inc} |
| 126 |
{$I x3dloadinternalcollada_integerparser.inc} |
| 127 |
{$I x3dloadinternalcollada_indexes.inc} |
| 128 |
{$I x3dloadinternalcollada_source.inc} |
| 129 |
{$I x3dloadinternalcollada_sources.inc} |
| 130 |
{$I x3dloadinternalcollada_read_helpers.inc} |
| 131 |
{$I x3dloadinternalcollada_librarynodes.inc} |
| 132 |
{$I x3dloadinternalcollada_materials.inc} |
| 133 |
{$I x3dloadinternalcollada_cameras.inc} |
| 134 |
{$I x3dloadinternalcollada_lights.inc} |
| 135 |
{$I x3dloadinternalcollada_images.inc} |
| 136 |
{$I x3dloadinternalcollada_node.inc} |
| 137 |
{$I x3dloadinternalcollada_scenes.inc} |
| 138 |
{$undef read_interface_nested} |
| 139 |
|
| 140 |
{$define read_implementation_nested} |
| 141 |
{$I x3dloadinternalcollada_effects.inc} |
| 142 |
{$I x3dloadinternalcollada_primitives.inc} |
| 143 |
{$I x3dloadinternalcollada_geometries.inc} |
| 144 |
{$I x3dloadinternalcollada_matrix.inc} |
| 145 |
{$I x3dloadinternalcollada_controllers.inc} |
| 146 |
{$I x3dloadinternalcollada_integerparser.inc} |
| 147 |
{$I x3dloadinternalcollada_indexes.inc} |
| 148 |
{$I x3dloadinternalcollada_source.inc} |
| 149 |
{$I x3dloadinternalcollada_sources.inc} |
| 150 |
{$I x3dloadinternalcollada_read_helpers.inc} |
| 151 |
{$I x3dloadinternalcollada_librarynodes.inc} |
| 152 |
{$I x3dloadinternalcollada_materials.inc} |
| 153 |
{$I x3dloadinternalcollada_cameras.inc} |
| 154 |
{$I x3dloadinternalcollada_lights.inc} |
| 155 |
{$I x3dloadinternalcollada_images.inc} |
| 156 |
{$I x3dloadinternalcollada_node.inc} |
| 157 |
{$I x3dloadinternalcollada_scenes.inc} |
| 158 |
{$undef read_implementation_nested} |
| 159 |
|
| 160 |
var |
| 161 |
Doc: TXMLDocument; |
| 162 |
Version: string; |
| 163 |
I: TXMLElementIterator; |
| 164 |
LibraryE: TDOMElement; |
| 165 |
NewBaseUrl: String; |
| 166 |
begin |
| 167 |
Effects := nil; |
| 168 |
Materials := nil; |
| 169 |
Geometries := nil; |
| 170 |
VisualScenes := nil; |
| 171 |
Controllers := nil; |
| 172 |
Images := nil; |
| 173 |
Cameras := nil; |
| 174 |
Lights := nil; |
| 175 |
Result := nil; |
| 176 |
|
| 177 |
BaseUrl := ProposedBaseUrl; |
| 178 |
|
| 179 |
try |
| 180 |
ReadXMLFile(Doc, Stream); |
| 181 |
|
| 182 |
try |
| 183 |
Check(Doc.DocumentElement.TagName = 'COLLADA', |
| 184 |
'Root node of Collada file must be <COLLADA>'); |
| 185 |
|
| 186 |
if not Doc.DocumentElement.AttributeString('version', Version) then |
| 187 |
begin |
| 188 |
Version := ''; |
| 189 |
Version14 := false; |
| 190 |
WritelnWarning('Collada', '<COLLADA> element misses "version" attribute'); |
| 191 |
end else |
| 192 |
begin |
| 193 |
{ TODO: uhm, terrible hack... I should move my lazy ass and tokenize |
| 194 |
Version properly. } |
| 195 |
Version14 := IsPrefix('1.4.', Version) or IsPrefix('1.5.', Version); |
| 196 |
end; |
| 197 |
|
| 198 |
{ honour COLLADA.base (exactly for the same purpose as our BaseUrl), |
| 199 |
but only if it's absolute } |
| 200 |
if Doc.DocumentElement.AttributeString('base', NewBaseUrl) and |
| 201 |
IsAbsoluteURI(NewBaseUrl) then |
| 202 |
BaseUrl := NewBaseUrl; |
| 203 |
|
| 204 |
Effects := TColladaEffectList.Create; |
| 205 |
Materials := TColladaMaterialsMap.Create; |
| 206 |
Geometries := TColladaGeometryList.Create; |
| 207 |
VisualScenes := TX3DChildrenList.Create(false); |
| 208 |
Controllers := TColladaControllerList.Create; |
| 209 |
Images := TX3DNodeList.Create(false); |
| 210 |
Cameras := TX3DChildrenList.Create(false); |
| 211 |
Lights := TX3DChildrenList.Create(false); |
| 212 |
LibraryNodesSwitch := nil; // will be created on-demand if needed |
| 213 |
|
| 214 |
Result := TX3DRootNode.Create('', BaseUrl); |
| 215 |
Result.HasForceVersion := true; |
| 216 |
Result.ForceVersion := X3DVersion; |
| 217 |
|
| 218 |
{ Read library_images. These may be referred to inside effects. } |
| 219 |
LibraryE := Doc.DocumentElement.ChildElement('library_images', false); |
| 220 |
if LibraryE <> nil then |
| 221 |
ReadLibraryImages(LibraryE); |
| 222 |
|
| 223 |
{ Read library_effects. |
| 224 |
Effects may be referenced by materials, |
| 225 |
and there's no guarantee that library_effects will occur before |
| 226 |
library_materials. Testcase: "COLLLADA 1.4.1 Basic Samples/Cube/cube.dae". } |
| 227 |
LibraryE := Doc.DocumentElement.ChildElement('library_effects', false); |
| 228 |
if LibraryE <> nil then |
| 229 |
ReadLibraryEffects(LibraryE); |
| 230 |
|
| 231 |
{ Read libraries of things that may be referenced within library_visual_scenes. |
| 232 |
For some models, <library_visual_scenes> may be before them (test from |
| 233 |
collada.org/owl/ : "New Uploads/COLLADA 1.5.0 Kinematics/COLLADA 1.5.0 Kinematics/COLLADA/KR150/kr150.dae") } |
| 234 |
I := Doc.DocumentElement.ChildrenIterator; |
| 235 |
try |
| 236 |
while I.GetNext do |
| 237 |
if I.Current.TagName = 'library' then { only Collada < 1.4.x } |
| 238 |
ReadLibrary(I.Current) else |
| 239 |
if I.Current.TagName = 'library_materials' then { only Collada >= 1.4.x } |
| 240 |
ReadLibraryMaterials(I.Current) else |
| 241 |
if I.Current.TagName = 'library_geometries' then { only Collada >= 1.4.x } |
| 242 |
ReadLibraryGeometries(I.Current) else |
| 243 |
if I.Current.TagName = 'library_cameras' then { only Collada >= 1.4.x } |
| 244 |
ReadLibraryCameras(I.Current) else |
| 245 |
if I.Current.TagName = 'library_lights' then { only Collada >= 1.4.x } |
| 246 |
ReadLibraryLights(I.Current) else |
| 247 |
if I.Current.TagName = 'library_controllers' then { only Collada >= 1.4.x } |
| 248 |
ReadLibraryControllers(I.Current); |
| 249 |
finally FreeAndNil(I); end; |
| 250 |
|
| 251 |
I := Doc.DocumentElement.ChildrenIterator; |
| 252 |
try |
| 253 |
while I.GetNext do |
| 254 |
if I.Current.TagName = 'library_nodes' then |
| 255 |
ReadLibraryNodes(I.Current); |
| 256 |
finally FreeAndNil(I); end; |
| 257 |
|
| 258 |
I := Doc.DocumentElement.ChildrenIterator; |
| 259 |
try |
| 260 |
while I.GetNext do |
| 261 |
if I.Current.TagName = 'library_visual_scenes' then { only Collada >= 1.4.x } |
| 262 |
ReadLibraryVisualScenes(I.Current) else |
| 263 |
if I.Current.TagName = 'scene' then |
| 264 |
ReadSceneElement(I.Current); |
| 265 |
finally FreeAndNil(I); end; |
| 266 |
|
| 267 |
Result.Meta.PutPreserve('source', ExtractURIName(BaseUrl)); |
| 268 |
Result.Meta['source-collada-version'] := Version; |
| 269 |
finally |
| 270 |
FreeAndNil(Doc); |
| 271 |
|
| 272 |
{ Free unused Images before freeing Effects. |
| 273 |
That's because image may be used inside an effect, |
| 274 |
and would be invalid reference after freeing effect. } |
| 275 |
X3DNodeList_FreeUnusedAndNil(Images); |
| 276 |
|
| 277 |
FreeAndNil(Materials); |
| 278 |
|
| 279 |
{ Note: if some effect will be used by some geometry, but the |
| 280 |
geometry will not be used, everything will be still Ok |
| 281 |
(no memory leak). First freeing over Effects will not free this |
| 282 |
effect (since it's used), but then freeing over Geometries will |
| 283 |
free the geometry together with effect (since effect usage will |
| 284 |
drop to zero). |
| 285 |
|
| 286 |
This means that also other complicated case, when one effect is |
| 287 |
used twice, once by unused geometry node, second time by used geometry |
| 288 |
node, is also Ok. } |
| 289 |
FreeAndNil(Effects); |
| 290 |
FreeAndNil(Geometries); |
| 291 |
|
| 292 |
X3DChildrenList_FreeUnusedAndNil(Lights); |
| 293 |
X3DChildrenList_FreeUnusedAndNil(Cameras); |
| 294 |
X3DChildrenList_FreeUnusedAndNil(VisualScenes); |
| 295 |
FreeAndNil(Controllers); |
| 296 |
end; |
| 297 |
{ eventually free Result *after* freeing other lists, to make sure references |
| 298 |
on Images, Materials etc. are valid when their unused items are freed. } |
| 299 |
except FreeAndNil(Result); raise; end; |
| 300 |
end; |
| 301 |
|
| 302 |
var |
| 303 |
ModelFormat: TModelFormat; |
| 304 |
initialization |
| 305 |
ModelFormat := TModelFormat.Create; |
| 306 |
ModelFormat.OnLoad := {$ifdef FPC}@{$endif} LoadCollada; |
| 307 |
ModelFormat.MimeTypes.Add('model/vnd.collada+xml'); |
| 308 |
ModelFormat.FileFilterName := 'Collada (*.dae)'; |
| 309 |
ModelFormat.Extensions.Add('.dae'); |
| 310 |
RegisterModelFormat(ModelFormat); |
| 311 |
end. |