@plastic-software/three 0.182.0 → 0.183.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (298) hide show
  1. package/LICENSE +1 -1
  2. package/build/three.cjs +11520 -10877
  3. package/build/three.core.js +11732 -11340
  4. package/build/three.core.min.js +2 -2
  5. package/build/three.module.js +509 -262
  6. package/build/three.module.min.js +2 -2
  7. package/build/three.tsl.js +7 -11
  8. package/build/three.tsl.min.js +2 -2
  9. package/build/three.webgpu.js +3072 -2607
  10. package/build/three.webgpu.min.js +2 -2
  11. package/build/three.webgpu.nodes.js +3071 -2607
  12. package/build/three.webgpu.nodes.min.js +2 -2
  13. package/examples/jsm/Addons.js +0 -3
  14. package/examples/jsm/animation/CCDIKSolver.js +2 -2
  15. package/examples/jsm/controls/ArcballControls.js +3 -3
  16. package/examples/jsm/controls/OrbitControls.js +103 -0
  17. package/examples/jsm/effects/AnaglyphEffect.js +102 -7
  18. package/examples/jsm/environments/ColorEnvironment.js +59 -0
  19. package/examples/jsm/environments/RoomEnvironment.js +1 -0
  20. package/examples/jsm/exporters/EXRExporter.js +1 -1
  21. package/examples/jsm/exporters/GLTFExporter.js +131 -4
  22. package/examples/jsm/exporters/USDZExporter.js +22 -3
  23. package/examples/jsm/helpers/AnimationPathHelper.js +302 -0
  24. package/examples/jsm/helpers/ViewHelper.js +67 -8
  25. package/examples/jsm/inspector/Inspector.js +21 -5
  26. package/examples/jsm/inspector/tabs/Console.js +39 -5
  27. package/examples/jsm/inspector/tabs/Parameters.js +16 -0
  28. package/examples/jsm/inspector/ui/Style.js +25 -1
  29. package/examples/jsm/libs/meshopt_decoder.module.js +6 -5
  30. package/examples/jsm/lines/LineMaterial.js +6 -0
  31. package/examples/jsm/loaders/3MFLoader.js +2 -2
  32. package/examples/jsm/loaders/AMFLoader.js +2 -2
  33. package/examples/jsm/loaders/ColladaLoader.js +24 -4026
  34. package/examples/jsm/loaders/EXRLoader.js +5 -5
  35. package/examples/jsm/loaders/FBXLoader.js +2 -2
  36. package/examples/jsm/loaders/GCodeLoader.js +34 -8
  37. package/examples/jsm/loaders/GLTFLoader.js +122 -171
  38. package/examples/jsm/loaders/KMZLoader.js +5 -5
  39. package/examples/jsm/loaders/KTX2Loader.js +5 -5
  40. package/examples/jsm/loaders/LWOLoader.js +7 -39
  41. package/examples/jsm/loaders/NRRDLoader.js +2 -2
  42. package/examples/jsm/loaders/PCDLoader.js +3 -2
  43. package/examples/jsm/loaders/USDLoader.js +100 -40
  44. package/examples/jsm/loaders/UltraHDRLoader.js +182 -30
  45. package/examples/jsm/loaders/VRMLLoader.js +77 -0
  46. package/examples/jsm/loaders/VTKLoader.js +37 -24
  47. package/examples/jsm/loaders/collada/ColladaComposer.js +2950 -0
  48. package/examples/jsm/loaders/collada/ColladaParser.js +1962 -0
  49. package/examples/jsm/loaders/usd/USDAParser.js +447 -366
  50. package/examples/jsm/loaders/usd/USDCParser.js +1841 -6
  51. package/examples/jsm/loaders/usd/USDComposer.js +4041 -0
  52. package/examples/jsm/materials/LDrawConditionalLineNodeMaterial.js +2 -2
  53. package/examples/jsm/objects/LensflareMesh.js +1 -1
  54. package/examples/jsm/objects/Sky.js +76 -4
  55. package/examples/jsm/objects/SkyMesh.js +114 -7
  56. package/examples/jsm/objects/Water.js +4 -3
  57. package/examples/jsm/objects/Water2.js +5 -3
  58. package/examples/jsm/objects/WaterMesh.js +5 -7
  59. package/examples/jsm/physics/JoltPhysics.js +7 -5
  60. package/examples/jsm/physics/RapierPhysics.js +6 -4
  61. package/examples/jsm/postprocessing/EffectComposer.js +7 -5
  62. package/examples/jsm/postprocessing/RenderTransitionPass.js +1 -1
  63. package/examples/jsm/renderers/CSS3DRenderer.js +1 -1
  64. package/examples/jsm/renderers/SVGRenderer.js +2 -2
  65. package/examples/jsm/shaders/GTAOShader.js +19 -6
  66. package/examples/jsm/shaders/HalftoneShader.js +12 -1
  67. package/examples/jsm/shaders/PoissonDenoiseShader.js +6 -2
  68. package/examples/jsm/shaders/SAOShader.js +17 -4
  69. package/examples/jsm/shaders/SSAOShader.js +11 -1
  70. package/examples/jsm/shaders/SSRShader.js +6 -5
  71. package/examples/jsm/shaders/VignetteShader.js +1 -1
  72. package/examples/jsm/tsl/display/AfterImageNode.js +1 -1
  73. package/examples/jsm/tsl/display/AnaglyphPassNode.js +456 -16
  74. package/examples/jsm/tsl/display/AnamorphicNode.js +1 -1
  75. package/examples/jsm/tsl/display/BilateralBlurNode.js +364 -0
  76. package/examples/jsm/tsl/display/BloomNode.js +5 -5
  77. package/examples/jsm/tsl/display/CRT.js +150 -0
  78. package/examples/jsm/tsl/display/DenoiseNode.js +1 -1
  79. package/examples/jsm/tsl/display/DepthOfFieldNode.js +1 -1
  80. package/examples/jsm/tsl/display/DotScreenNode.js +1 -1
  81. package/examples/jsm/tsl/display/FXAANode.js +2 -2
  82. package/examples/jsm/tsl/display/GTAONode.js +2 -2
  83. package/examples/jsm/tsl/display/GaussianBlurNode.js +11 -2
  84. package/examples/jsm/tsl/display/GodraysNode.js +624 -0
  85. package/examples/jsm/tsl/display/LensflareNode.js +1 -1
  86. package/examples/jsm/tsl/display/Lut3DNode.js +1 -1
  87. package/examples/jsm/tsl/display/OutlineNode.js +3 -3
  88. package/examples/jsm/tsl/display/ParallaxBarrierPassNode.js +2 -2
  89. package/examples/jsm/tsl/display/PixelationPassNode.js +5 -5
  90. package/examples/jsm/tsl/display/RGBShiftNode.js +2 -2
  91. package/examples/jsm/tsl/display/RetroPassNode.js +263 -0
  92. package/examples/jsm/tsl/display/SMAANode.js +2 -2
  93. package/examples/jsm/tsl/display/SSAAPassNode.js +2 -2
  94. package/examples/jsm/tsl/display/SSGINode.js +2 -2
  95. package/examples/jsm/tsl/display/SSRNode.js +7 -7
  96. package/examples/jsm/tsl/display/SSSNode.js +2 -2
  97. package/examples/jsm/tsl/display/Shape.js +29 -0
  98. package/examples/jsm/tsl/display/SobelOperatorNode.js +2 -2
  99. package/examples/jsm/tsl/display/StereoPassNode.js +1 -2
  100. package/examples/jsm/tsl/display/TRAANode.js +9 -12
  101. package/examples/jsm/tsl/display/TransitionNode.js +1 -1
  102. package/examples/jsm/tsl/display/depthAwareBlend.js +80 -0
  103. package/examples/jsm/tsl/math/Bayer.js +40 -1
  104. package/examples/jsm/utils/LDrawUtils.js +1 -1
  105. package/package.json +11 -19
  106. package/src/Three.Core.js +1 -1
  107. package/src/Three.TSL.js +5 -9
  108. package/src/Three.WebGPU.Nodes.js +2 -0
  109. package/src/Three.WebGPU.js +3 -0
  110. package/src/Three.js +1 -0
  111. package/src/animation/AnimationAction.js +1 -1
  112. package/src/animation/AnimationClip.js +1 -1
  113. package/src/animation/AnimationMixer.js +6 -0
  114. package/src/animation/KeyframeTrack.js +46 -7
  115. package/src/animation/PropertyMixer.js +4 -4
  116. package/src/audio/Audio.js +1 -1
  117. package/src/audio/AudioListener.js +5 -3
  118. package/src/cameras/Camera.js +32 -2
  119. package/src/cameras/CubeCamera.js +20 -0
  120. package/src/constants.js +30 -1
  121. package/src/core/Clock.js +7 -0
  122. package/src/core/Object3D.js +56 -4
  123. package/src/core/RenderTarget.js +3 -4
  124. package/src/extras/PMREMGenerator.js +4 -8
  125. package/src/geometries/TorusGeometry.js +8 -3
  126. package/src/helpers/CameraHelper.js +3 -0
  127. package/src/helpers/DirectionalLightHelper.js +4 -1
  128. package/src/helpers/HemisphereLightHelper.js +3 -0
  129. package/src/helpers/PointLightHelper.js +0 -24
  130. package/src/helpers/SpotLightHelper.js +3 -0
  131. package/src/lights/LightShadow.js +15 -3
  132. package/src/lights/webgpu/IESSpotLight.js +2 -1
  133. package/src/loaders/Cache.js +28 -0
  134. package/src/loaders/FileLoader.js +1 -1
  135. package/src/loaders/ImageBitmapLoader.js +8 -3
  136. package/src/loaders/Loader.js +6 -0
  137. package/src/loaders/ObjectLoader.js +18 -1
  138. package/src/materials/MeshLambertMaterial.js +9 -0
  139. package/src/materials/MeshPhongMaterial.js +9 -0
  140. package/src/materials/nodes/Line2NodeMaterial.js +5 -5
  141. package/src/materials/nodes/MeshPhysicalNodeMaterial.js +2 -0
  142. package/src/materials/nodes/NodeMaterial.js +15 -24
  143. package/src/materials/nodes/manager/NodeMaterialObserver.js +9 -3
  144. package/src/math/Line3.js +3 -5
  145. package/src/math/MathUtils.js +10 -10
  146. package/src/math/Matrix4.js +35 -26
  147. package/src/math/Quaternion.js +3 -29
  148. package/src/math/Vector3.js +3 -3
  149. package/src/math/interpolants/BezierInterpolant.js +108 -0
  150. package/src/nodes/Nodes.js +87 -68
  151. package/src/nodes/TSL.js +2 -5
  152. package/src/nodes/accessors/Arrays.js +1 -1
  153. package/src/nodes/accessors/Bitangent.js +5 -5
  154. package/src/nodes/accessors/BufferAttributeNode.js +1 -1
  155. package/src/nodes/accessors/Camera.js +149 -28
  156. package/src/nodes/accessors/InstanceNode.js +105 -40
  157. package/src/nodes/accessors/Normal.js +9 -9
  158. package/src/nodes/accessors/Position.js +34 -2
  159. package/src/nodes/accessors/SceneProperties.js +53 -0
  160. package/src/nodes/accessors/SkinningNode.js +12 -24
  161. package/src/nodes/accessors/StorageBufferNode.js +0 -19
  162. package/src/nodes/accessors/StorageTextureNode.js +37 -1
  163. package/src/nodes/accessors/Tangent.js +3 -3
  164. package/src/nodes/accessors/Texture3DNode.js +6 -34
  165. package/src/nodes/accessors/TextureNode.js +58 -22
  166. package/src/nodes/accessors/UniformArrayNode.js +2 -0
  167. package/src/nodes/core/MRTNode.js +48 -2
  168. package/src/nodes/core/Node.js +29 -3
  169. package/src/nodes/core/NodeBuilder.js +115 -40
  170. package/src/nodes/core/NodeError.js +28 -0
  171. package/src/nodes/core/NodeUtils.js +5 -3
  172. package/src/nodes/core/OutputStructNode.js +12 -10
  173. package/src/nodes/core/ParameterNode.js +2 -1
  174. package/src/nodes/core/StackNode.js +9 -8
  175. package/src/nodes/core/StackTrace.js +139 -0
  176. package/src/nodes/core/StructNode.js +15 -0
  177. package/src/nodes/core/SubBuildNode.js +1 -1
  178. package/src/nodes/core/UniformNode.js +2 -1
  179. package/src/nodes/core/VarNode.js +1 -1
  180. package/src/nodes/core/VaryingNode.js +1 -18
  181. package/src/nodes/display/BlendModes.js +0 -64
  182. package/src/nodes/display/ColorAdjustment.js +17 -0
  183. package/src/nodes/display/ColorSpaceNode.js +3 -3
  184. package/src/nodes/display/NormalMapNode.js +2 -2
  185. package/src/nodes/display/PassNode.js +21 -2
  186. package/src/nodes/display/RenderOutputNode.js +3 -3
  187. package/src/nodes/display/ScreenNode.js +2 -1
  188. package/src/nodes/display/ToneMappingNode.js +1 -1
  189. package/src/nodes/display/ToonOutlinePassNode.js +2 -2
  190. package/src/nodes/display/ViewportDepthNode.js +52 -4
  191. package/src/nodes/display/ViewportTextureNode.js +21 -4
  192. package/src/nodes/fog/Fog.js +18 -35
  193. package/src/nodes/functions/PhysicalLightingModel.js +25 -3
  194. package/src/nodes/geometry/RangeNode.js +4 -2
  195. package/src/nodes/gpgpu/ComputeNode.js +5 -4
  196. package/src/nodes/gpgpu/WorkgroupInfoNode.js +2 -1
  197. package/src/nodes/lighting/EnvironmentNode.js +28 -3
  198. package/src/nodes/lighting/PointShadowNode.js +24 -12
  199. package/src/nodes/lighting/ShadowFilterNode.js +15 -43
  200. package/src/nodes/lighting/ShadowNode.js +54 -32
  201. package/src/nodes/math/ConditionalNode.js +2 -2
  202. package/src/nodes/math/MathNode.js +3 -40
  203. package/src/nodes/math/OperatorNode.js +2 -1
  204. package/src/nodes/pmrem/PMREMUtils.js +9 -15
  205. package/src/nodes/tsl/TSLCore.js +13 -10
  206. package/src/nodes/utils/DebugNode.js +11 -11
  207. package/src/nodes/utils/JoinNode.js +2 -2
  208. package/src/nodes/utils/LoopNode.js +1 -1
  209. package/src/nodes/utils/MemberNode.js +1 -1
  210. package/src/nodes/utils/RTTNode.js +1 -1
  211. package/src/nodes/utils/ReflectorNode.js +2 -3
  212. package/src/nodes/utils/SpriteSheetUV.js +35 -0
  213. package/src/nodes/utils/UVUtils.js +4 -2
  214. package/src/objects/BatchedMesh.js +22 -12
  215. package/src/objects/InstancedMesh.js +11 -0
  216. package/src/renderers/WebGLRenderer.js +34 -60
  217. package/src/renderers/common/Backend.js +21 -0
  218. package/src/renderers/common/Background.js +7 -4
  219. package/src/renderers/common/BindGroup.js +1 -9
  220. package/src/renderers/common/Bindings.js +20 -5
  221. package/src/renderers/common/BlendMode.js +143 -0
  222. package/src/renderers/common/BundleGroup.js +1 -1
  223. package/src/renderers/common/CubeRenderTarget.js +50 -6
  224. package/src/renderers/common/Geometries.js +17 -3
  225. package/src/renderers/common/Lighting.js +5 -21
  226. package/src/renderers/common/Pipelines.js +4 -4
  227. package/src/renderers/common/PostProcessing.js +8 -206
  228. package/src/renderers/common/RenderBundles.js +2 -1
  229. package/src/renderers/common/RenderContext.js +16 -0
  230. package/src/renderers/common/RenderContexts.js +33 -56
  231. package/src/renderers/common/RenderLists.js +2 -1
  232. package/src/renderers/common/RenderObject.js +2 -3
  233. package/src/renderers/common/RenderObjectPipeline.js +40 -0
  234. package/src/renderers/common/RenderObjects.js +18 -2
  235. package/src/renderers/common/RenderPipeline.js +203 -17
  236. package/src/renderers/common/Renderer.js +207 -40
  237. package/src/renderers/common/Sampler.js +4 -4
  238. package/src/renderers/common/StorageBuffer.js +13 -1
  239. package/src/renderers/common/Textures.js +16 -0
  240. package/src/renderers/common/TimestampQueryPool.js +5 -3
  241. package/src/renderers/common/Uniform.js +8 -0
  242. package/src/renderers/common/UniformsGroup.js +60 -0
  243. package/src/renderers/common/XRManager.js +2 -2
  244. package/src/renderers/common/nodes/NodeBuilderState.js +1 -1
  245. package/src/renderers/common/nodes/{Nodes.js → NodeManager.js} +18 -6
  246. package/src/renderers/common/nodes/NodeStorageBuffer.js +13 -2
  247. package/src/renderers/shaders/ShaderChunk/batching_pars_vertex.glsl.js +2 -2
  248. package/src/renderers/shaders/ShaderChunk/color_fragment.glsl.js +1 -5
  249. package/src/renderers/shaders/ShaderChunk/color_pars_fragment.glsl.js +1 -5
  250. package/src/renderers/shaders/ShaderChunk/color_pars_vertex.glsl.js +1 -5
  251. package/src/renderers/shaders/ShaderChunk/color_vertex.glsl.js +8 -10
  252. package/src/renderers/shaders/ShaderChunk/envmap_fragment.glsl.js +7 -11
  253. package/src/renderers/shaders/ShaderChunk/lights_fragment_end.glsl.js +6 -0
  254. package/src/renderers/shaders/ShaderChunk/lights_fragment_maps.glsl.js +6 -2
  255. package/src/renderers/shaders/ShaderChunk/lights_physical_pars_fragment.glsl.js +23 -1
  256. package/src/renderers/shaders/ShaderChunk/packing.glsl.js +20 -4
  257. package/src/renderers/shaders/ShaderChunk/shadowmap_pars_fragment.glsl.js +55 -24
  258. package/src/renderers/shaders/ShaderLib/meshlambert.glsl.js +2 -0
  259. package/src/renderers/shaders/ShaderLib/meshphong.glsl.js +2 -0
  260. package/src/renderers/shaders/ShaderLib/shadow.glsl.js +1 -0
  261. package/src/renderers/shaders/ShaderLib.js +4 -2
  262. package/src/renderers/shaders/UniformsLib.js +0 -3
  263. package/src/renderers/webgl/WebGLBackground.js +2 -2
  264. package/src/renderers/webgl/WebGLBindingStates.js +99 -27
  265. package/src/renderers/webgl/WebGLEnvironments.js +228 -0
  266. package/src/renderers/webgl/WebGLGeometries.js +10 -7
  267. package/src/renderers/webgl/WebGLMaterials.js +12 -0
  268. package/src/renderers/webgl/WebGLObjects.js +3 -1
  269. package/src/renderers/webgl/WebGLProgram.js +2 -2
  270. package/src/renderers/webgl/WebGLPrograms.js +10 -4
  271. package/src/renderers/webgl/WebGLRenderLists.js +15 -0
  272. package/src/renderers/webgl/WebGLShadowMap.js +5 -4
  273. package/src/renderers/webgl/WebGLState.js +12 -17
  274. package/src/renderers/webgl-fallback/WebGLBackend.js +71 -7
  275. package/src/renderers/webgl-fallback/nodes/GLSLNodeBuilder.js +98 -29
  276. package/src/renderers/webgl-fallback/utils/WebGLState.js +168 -7
  277. package/src/renderers/webgpu/WebGPUBackend.js +58 -9
  278. package/src/renderers/webgpu/WebGPURenderer.js +1 -0
  279. package/src/renderers/webgpu/nodes/WGSLNodeBuilder.js +257 -45
  280. package/src/renderers/webgpu/utils/WebGPUBindingUtils.js +8 -19
  281. package/src/renderers/webgpu/utils/WebGPUConstants.js +1 -1
  282. package/src/renderers/webgpu/utils/WebGPUPipelineUtils.js +56 -31
  283. package/src/renderers/webgpu/utils/WebGPUTexturePassUtils.js +152 -200
  284. package/src/renderers/webgpu/utils/WebGPUTextureUtils.js +25 -25
  285. package/src/renderers/webgpu/utils/WebGPUUtils.js +10 -6
  286. package/src/renderers/webxr/WebXRManager.js +2 -2
  287. package/src/textures/Texture.js +2 -2
  288. package/src/utils.js +246 -3
  289. package/examples/jsm/materials/MeshGouraudMaterial.js +0 -433
  290. package/examples/jsm/materials/MeshPostProcessingMaterial.js +0 -167
  291. package/examples/jsm/shaders/GodRaysShader.js +0 -333
  292. package/src/nodes/accessors/SceneNode.js +0 -145
  293. package/src/nodes/code/ScriptableNode.js +0 -726
  294. package/src/nodes/code/ScriptableValueNode.js +0 -253
  295. package/src/nodes/display/PosterizeNode.js +0 -65
  296. package/src/nodes/utils/SpriteSheetUVNode.js +0 -90
  297. package/src/renderers/webgl/WebGLCubeMaps.js +0 -99
  298. package/src/renderers/webgl/WebGLCubeUVMaps.js +0 -134
@@ -1,45 +1,14 @@
1
1
  import {
2
- AmbientLight,
3
- AnimationClip,
4
- Bone,
5
- BufferGeometry,
6
- ClampToEdgeWrapping,
7
- Color,
8
- ColorManagement,
9
- DirectionalLight,
10
- DoubleSide,
11
2
  FileLoader,
12
- Float32BufferAttribute,
13
- FrontSide,
14
- Group,
15
- Line,
16
- LineBasicMaterial,
17
- LineSegments,
18
3
  Loader,
19
4
  LoaderUtils,
20
- MathUtils,
21
- Matrix4,
22
- Mesh,
23
- MeshBasicMaterial,
24
- MeshLambertMaterial,
25
- MeshPhongMaterial,
26
- OrthographicCamera,
27
- PerspectiveCamera,
28
- PointLight,
29
- Quaternion,
30
- QuaternionKeyframeTrack,
31
- RepeatWrapping,
32
5
  Scene,
33
- Skeleton,
34
- SkinnedMesh,
35
- SpotLight,
36
- TextureLoader,
37
- Vector2,
38
- Vector3,
39
- VectorKeyframeTrack,
40
- SRGBColorSpace
6
+ TextureLoader
41
7
  } from 'three';
8
+
42
9
  import { TGALoader } from '../loaders/TGALoader.js';
10
+ import { ColladaParser } from './collada/ColladaParser.js';
11
+ import { ColladaComposer } from './collada/ColladaComposer.js';
43
12
 
44
13
  /**
45
14
  * A loader for the Collada format.
@@ -117,4016 +86,44 @@ class ColladaLoader extends Loader {
117
86
  */
118
87
  parse( text, path ) {
119
88
 
120
- function getElementsByTagName( xml, name ) {
121
-
122
- // Non recursive xml.getElementsByTagName() ...
123
-
124
- const array = [];
125
- const childNodes = xml.childNodes;
126
-
127
- for ( let i = 0, l = childNodes.length; i < l; i ++ ) {
128
-
129
- const child = childNodes[ i ];
130
-
131
- if ( child.nodeName === name ) {
132
-
133
- array.push( child );
134
-
135
- }
136
-
137
- }
138
-
139
- return array;
140
-
141
- }
142
-
143
- function parseStrings( text ) {
144
-
145
- if ( text.length === 0 ) return [];
146
-
147
- const parts = text.trim().split( /\s+/ );
148
- const array = new Array( parts.length );
149
-
150
- for ( let i = 0, l = parts.length; i < l; i ++ ) {
151
-
152
- array[ i ] = parts[ i ];
153
-
154
- }
155
-
156
- return array;
157
-
158
- }
159
-
160
- function parseFloats( text ) {
161
-
162
- if ( text.length === 0 ) return [];
163
-
164
- const parts = text.trim().split( /\s+/ );
165
- const array = new Array( parts.length );
166
-
167
- for ( let i = 0, l = parts.length; i < l; i ++ ) {
168
-
169
- array[ i ] = parseFloat( parts[ i ] );
170
-
171
- }
172
-
173
- return array;
174
-
175
- }
176
-
177
- function parseInts( text ) {
178
-
179
- if ( text.length === 0 ) return [];
180
-
181
- const parts = text.trim().split( /\s+/ );
182
- const array = new Array( parts.length );
183
-
184
- for ( let i = 0, l = parts.length; i < l; i ++ ) {
185
-
186
- array[ i ] = parseInt( parts[ i ] );
187
-
188
- }
189
-
190
- return array;
191
-
192
- }
193
-
194
- function parseId( text ) {
195
-
196
- return text.substring( 1 );
197
-
198
- }
199
-
200
- function generateId() {
201
-
202
- return 'three_default_' + ( count ++ );
203
-
204
- }
205
-
206
- function isEmpty( object ) {
207
-
208
- return Object.keys( object ).length === 0;
209
-
210
- }
211
-
212
- // asset
213
-
214
- function parseAsset( xml ) {
215
-
216
- return {
217
- unit: parseAssetUnit( getElementsByTagName( xml, 'unit' )[ 0 ] ),
218
- upAxis: parseAssetUpAxis( getElementsByTagName( xml, 'up_axis' )[ 0 ] )
219
- };
220
-
221
- }
222
-
223
- function parseAssetUnit( xml ) {
224
-
225
- if ( ( xml !== undefined ) && ( xml.hasAttribute( 'meter' ) === true ) ) {
226
-
227
- return parseFloat( xml.getAttribute( 'meter' ) );
228
-
229
- } else {
230
-
231
- return 1; // default 1 meter
232
-
233
- }
234
-
235
- }
236
-
237
- function parseAssetUpAxis( xml ) {
238
-
239
- return xml !== undefined ? xml.textContent : 'Y_UP';
240
-
241
- }
242
-
243
- // library
244
-
245
- function parseLibrary( xml, libraryName, nodeName, parser ) {
246
-
247
- const library = getElementsByTagName( xml, libraryName )[ 0 ];
248
-
249
- if ( library !== undefined ) {
250
-
251
- const elements = getElementsByTagName( library, nodeName );
252
-
253
- for ( let i = 0; i < elements.length; i ++ ) {
254
-
255
- parser( elements[ i ] );
256
-
257
- }
258
-
259
- }
260
-
261
- }
262
-
263
- function buildLibrary( data, builder ) {
264
-
265
- for ( const name in data ) {
266
-
267
- const object = data[ name ];
268
- object.build = builder( data[ name ] );
269
-
270
- }
271
-
272
- }
273
-
274
- // get
275
-
276
- function getBuild( data, builder ) {
277
-
278
- if ( data.build !== undefined ) return data.build;
279
-
280
- data.build = builder( data );
281
-
282
- return data.build;
283
-
284
- }
285
-
286
- // animation
287
-
288
- function parseAnimation( xml ) {
289
-
290
- const data = {
291
- sources: {},
292
- samplers: {},
293
- channels: {}
294
- };
295
-
296
- let hasChildren = false;
297
-
298
- for ( let i = 0, l = xml.childNodes.length; i < l; i ++ ) {
299
-
300
- const child = xml.childNodes[ i ];
301
-
302
- if ( child.nodeType !== 1 ) continue;
303
-
304
- let id;
305
-
306
- switch ( child.nodeName ) {
307
-
308
- case 'source':
309
- id = child.getAttribute( 'id' );
310
- data.sources[ id ] = parseSource( child );
311
- break;
312
-
313
- case 'sampler':
314
- id = child.getAttribute( 'id' );
315
- data.samplers[ id ] = parseAnimationSampler( child );
316
- break;
317
-
318
- case 'channel':
319
- id = child.getAttribute( 'target' );
320
- data.channels[ id ] = parseAnimationChannel( child );
321
- break;
322
-
323
- case 'animation':
324
- // hierarchy of related animations
325
- parseAnimation( child );
326
- hasChildren = true;
327
- break;
328
-
329
- default:
330
- console.log( child );
331
-
332
- }
333
-
334
- }
335
-
336
- if ( hasChildren === false ) {
337
-
338
- // since 'id' attributes can be optional, it's necessary to generate a UUID for unique assignment
339
-
340
- library.animations[ xml.getAttribute( 'id' ) || MathUtils.generateUUID() ] = data;
341
-
342
- }
343
-
344
- }
345
-
346
- function parseAnimationSampler( xml ) {
347
-
348
- const data = {
349
- inputs: {},
350
- };
351
-
352
- for ( let i = 0, l = xml.childNodes.length; i < l; i ++ ) {
353
-
354
- const child = xml.childNodes[ i ];
355
-
356
- if ( child.nodeType !== 1 ) continue;
357
-
358
- switch ( child.nodeName ) {
359
-
360
- case 'input':
361
- const id = parseId( child.getAttribute( 'source' ) );
362
- const semantic = child.getAttribute( 'semantic' );
363
- data.inputs[ semantic ] = id;
364
- break;
365
-
366
- }
367
-
368
- }
369
-
370
- return data;
371
-
372
- }
373
-
374
- function parseAnimationChannel( xml ) {
375
-
376
- const data = {};
377
-
378
- const target = xml.getAttribute( 'target' );
379
-
380
- // parsing SID Addressing Syntax
381
-
382
- let parts = target.split( '/' );
383
-
384
- const id = parts.shift();
385
- let sid = parts.shift();
386
-
387
- // check selection syntax
388
-
389
- const arraySyntax = ( sid.indexOf( '(' ) !== - 1 );
390
- const memberSyntax = ( sid.indexOf( '.' ) !== - 1 );
391
-
392
- if ( memberSyntax ) {
393
-
394
- // member selection access
395
-
396
- parts = sid.split( '.' );
397
- sid = parts.shift();
398
- data.member = parts.shift();
399
-
400
- } else if ( arraySyntax ) {
401
-
402
- // array-access syntax. can be used to express fields in one-dimensional vectors or two-dimensional matrices.
403
-
404
- const indices = sid.split( '(' );
405
- sid = indices.shift();
406
-
407
- for ( let i = 0; i < indices.length; i ++ ) {
408
-
409
- indices[ i ] = parseInt( indices[ i ].replace( /\)/, '' ) );
410
-
411
- }
412
-
413
- data.indices = indices;
414
-
415
- }
416
-
417
- data.id = id;
418
- data.sid = sid;
419
-
420
- data.arraySyntax = arraySyntax;
421
- data.memberSyntax = memberSyntax;
422
-
423
- data.sampler = parseId( xml.getAttribute( 'source' ) );
424
-
425
- return data;
426
-
427
- }
428
-
429
- function buildAnimation( data ) {
430
-
431
- const tracks = [];
432
-
433
- const channels = data.channels;
434
- const samplers = data.samplers;
435
- const sources = data.sources;
436
-
437
- for ( const target in channels ) {
438
-
439
- if ( channels.hasOwnProperty( target ) ) {
440
-
441
- const channel = channels[ target ];
442
- const sampler = samplers[ channel.sampler ];
443
-
444
- const inputId = sampler.inputs.INPUT;
445
- const outputId = sampler.inputs.OUTPUT;
446
-
447
- const inputSource = sources[ inputId ];
448
- const outputSource = sources[ outputId ];
449
-
450
- const animation = buildAnimationChannel( channel, inputSource, outputSource );
451
-
452
- createKeyframeTracks( animation, tracks );
453
-
454
- }
455
-
456
- }
457
-
458
- return tracks;
459
-
460
- }
461
-
462
- function getAnimation( id ) {
463
-
464
- return getBuild( library.animations[ id ], buildAnimation );
465
-
466
- }
467
-
468
- function buildAnimationChannel( channel, inputSource, outputSource ) {
469
-
470
- const node = library.nodes[ channel.id ];
471
- const object3D = getNode( node.id );
472
-
473
- const transform = node.transforms[ channel.sid ];
474
- const defaultMatrix = node.matrix.clone().transpose();
475
-
476
- let time, stride;
477
- let i, il, j, jl;
478
-
479
- const data = {};
480
-
481
- // the collada spec allows the animation of data in various ways.
482
- // depending on the transform type (matrix, translate, rotate, scale), we execute different logic
483
-
484
- switch ( transform ) {
485
-
486
- case 'matrix':
487
-
488
- for ( i = 0, il = inputSource.array.length; i < il; i ++ ) {
489
-
490
- time = inputSource.array[ i ];
491
- stride = i * outputSource.stride;
492
-
493
- if ( data[ time ] === undefined ) data[ time ] = {};
494
-
495
- if ( channel.arraySyntax === true ) {
496
-
497
- const value = outputSource.array[ stride ];
498
- const index = channel.indices[ 0 ] + 4 * channel.indices[ 1 ];
499
-
500
- data[ time ][ index ] = value;
501
-
502
- } else {
503
-
504
- for ( j = 0, jl = outputSource.stride; j < jl; j ++ ) {
505
-
506
- data[ time ][ j ] = outputSource.array[ stride + j ];
507
-
508
- }
509
-
510
- }
511
-
512
- }
513
-
514
- break;
515
-
516
- case 'translate':
517
- console.warn( 'THREE.ColladaLoader: Animation transform type "%s" not yet implemented.', transform );
518
- break;
519
-
520
- case 'rotate':
521
- console.warn( 'THREE.ColladaLoader: Animation transform type "%s" not yet implemented.', transform );
522
- break;
523
-
524
- case 'scale':
525
- console.warn( 'THREE.ColladaLoader: Animation transform type "%s" not yet implemented.', transform );
526
- break;
527
-
528
- }
529
-
530
- const keyframes = prepareAnimationData( data, defaultMatrix );
531
-
532
- const animation = {
533
- name: object3D.uuid,
534
- keyframes: keyframes
535
- };
89
+ if ( text.length === 0 ) {
536
90
 
537
- return animation;
91
+ return { scene: new Scene() };
538
92
 
539
93
  }
540
94
 
541
- function prepareAnimationData( data, defaultMatrix ) {
542
-
543
- const keyframes = [];
544
-
545
- // transfer data into a sortable array
546
-
547
- for ( const time in data ) {
548
-
549
- keyframes.push( { time: parseFloat( time ), value: data[ time ] } );
550
-
551
- }
552
-
553
- // ensure keyframes are sorted by time
554
-
555
- keyframes.sort( ascending );
556
-
557
- // now we clean up all animation data, so we can use them for keyframe tracks
558
-
559
- for ( let i = 0; i < 16; i ++ ) {
560
-
561
- transformAnimationData( keyframes, i, defaultMatrix.elements[ i ] );
562
-
563
- }
564
-
565
- return keyframes;
95
+ // Parse XML to library data
96
+ const parser = new ColladaParser();
97
+ const parseResult = parser.parse( text );
566
98
 
567
- // array sort function
99
+ if ( parseResult === null ) {
568
100
 
569
- function ascending( a, b ) {
570
-
571
- return a.time - b.time;
572
-
573
- }
101
+ return null;
574
102
 
575
103
  }
576
104
 
577
- const position = new Vector3();
578
- const scale = new Vector3();
579
- const quaternion = new Quaternion();
580
-
581
- function createKeyframeTracks( animation, tracks ) {
582
-
583
- const keyframes = animation.keyframes;
584
- const name = animation.name;
585
-
586
- const times = [];
587
- const positionData = [];
588
- const quaternionData = [];
589
- const scaleData = [];
590
-
591
- for ( let i = 0, l = keyframes.length; i < l; i ++ ) {
105
+ const { library, asset, collada } = parseResult;
592
106
 
593
- const keyframe = keyframes[ i ];
594
-
595
- const time = keyframe.time;
596
- const value = keyframe.value;
597
-
598
- matrix.fromArray( value ).transpose();
599
- matrix.decompose( position, quaternion, scale );
600
-
601
- times.push( time );
602
- positionData.push( position.x, position.y, position.z );
603
- quaternionData.push( quaternion.x, quaternion.y, quaternion.z, quaternion.w );
604
- scaleData.push( scale.x, scale.y, scale.z );
107
+ // Setup texture loaders
108
+ const textureLoader = new TextureLoader( this.manager );
109
+ textureLoader.setPath( this.resourcePath || path ).setCrossOrigin( this.crossOrigin );
605
110
 
606
- }
111
+ let tgaLoader;
607
112
 
608
- if ( positionData.length > 0 ) tracks.push( new VectorKeyframeTrack( name + '.position', times, positionData ) );
609
- if ( quaternionData.length > 0 ) tracks.push( new QuaternionKeyframeTrack( name + '.quaternion', times, quaternionData ) );
610
- if ( scaleData.length > 0 ) tracks.push( new VectorKeyframeTrack( name + '.scale', times, scaleData ) );
113
+ if ( TGALoader ) {
611
114
 
612
- return tracks;
115
+ tgaLoader = new TGALoader( this.manager );
116
+ tgaLoader.setPath( this.resourcePath || path );
613
117
 
614
118
  }
615
119
 
616
- function transformAnimationData( keyframes, property, defaultValue ) {
617
-
618
- let keyframe;
619
-
620
- let empty = true;
621
- let i, l;
622
-
623
- // check, if values of a property are missing in our keyframes
624
-
625
- for ( i = 0, l = keyframes.length; i < l; i ++ ) {
626
-
627
- keyframe = keyframes[ i ];
628
-
629
- if ( keyframe.value[ property ] === undefined ) {
630
-
631
- keyframe.value[ property ] = null; // mark as missing
632
-
633
- } else {
634
-
635
- empty = false;
636
-
637
- }
638
-
639
- }
640
-
641
- if ( empty === true ) {
642
-
643
- // no values at all, so we set a default value
644
-
645
- for ( i = 0, l = keyframes.length; i < l; i ++ ) {
646
-
647
- keyframe = keyframes[ i ];
648
-
649
- keyframe.value[ property ] = defaultValue;
650
-
651
- }
652
-
653
- } else {
654
-
655
- // filling gaps
656
-
657
- createMissingKeyframes( keyframes, property );
658
-
659
- }
660
-
661
- }
662
-
663
- function createMissingKeyframes( keyframes, property ) {
664
-
665
- let prev, next;
666
-
667
- for ( let i = 0, l = keyframes.length; i < l; i ++ ) {
668
-
669
- const keyframe = keyframes[ i ];
670
-
671
- if ( keyframe.value[ property ] === null ) {
672
-
673
- prev = getPrev( keyframes, i, property );
674
- next = getNext( keyframes, i, property );
675
-
676
- if ( prev === null ) {
677
-
678
- keyframe.value[ property ] = next.value[ property ];
679
- continue;
680
-
681
- }
682
-
683
- if ( next === null ) {
684
-
685
- keyframe.value[ property ] = prev.value[ property ];
686
- continue;
687
-
688
- }
689
-
690
- interpolate( keyframe, prev, next, property );
691
-
692
- }
693
-
694
- }
695
-
696
- }
697
-
698
- function getPrev( keyframes, i, property ) {
699
-
700
- while ( i >= 0 ) {
701
-
702
- const keyframe = keyframes[ i ];
703
-
704
- if ( keyframe.value[ property ] !== null ) return keyframe;
705
-
706
- i --;
707
-
708
- }
709
-
710
- return null;
711
-
712
- }
713
-
714
- function getNext( keyframes, i, property ) {
715
-
716
- while ( i < keyframes.length ) {
717
-
718
- const keyframe = keyframes[ i ];
719
-
720
- if ( keyframe.value[ property ] !== null ) return keyframe;
721
-
722
- i ++;
723
-
724
- }
725
-
726
- return null;
727
-
728
- }
729
-
730
- function interpolate( key, prev, next, property ) {
731
-
732
- if ( ( next.time - prev.time ) === 0 ) {
733
-
734
- key.value[ property ] = prev.value[ property ];
735
- return;
736
-
737
- }
738
-
739
- key.value[ property ] = ( ( key.time - prev.time ) * ( next.value[ property ] - prev.value[ property ] ) / ( next.time - prev.time ) ) + prev.value[ property ];
740
-
741
- }
742
-
743
- // animation clips
744
-
745
- function parseAnimationClip( xml ) {
746
-
747
- const data = {
748
- name: xml.getAttribute( 'id' ) || 'default',
749
- start: parseFloat( xml.getAttribute( 'start' ) || 0 ),
750
- end: parseFloat( xml.getAttribute( 'end' ) || 0 ),
751
- animations: []
752
- };
753
-
754
- for ( let i = 0, l = xml.childNodes.length; i < l; i ++ ) {
755
-
756
- const child = xml.childNodes[ i ];
757
-
758
- if ( child.nodeType !== 1 ) continue;
759
-
760
- switch ( child.nodeName ) {
761
-
762
- case 'instance_animation':
763
- data.animations.push( parseId( child.getAttribute( 'url' ) ) );
764
- break;
765
-
766
- }
767
-
768
- }
769
-
770
- library.clips[ xml.getAttribute( 'id' ) ] = data;
771
-
772
- }
773
-
774
- function buildAnimationClip( data ) {
775
-
776
- const tracks = [];
777
-
778
- const name = data.name;
779
- const duration = ( data.end - data.start ) || - 1;
780
- const animations = data.animations;
781
-
782
- for ( let i = 0, il = animations.length; i < il; i ++ ) {
783
-
784
- const animationTracks = getAnimation( animations[ i ] );
785
-
786
- for ( let j = 0, jl = animationTracks.length; j < jl; j ++ ) {
787
-
788
- tracks.push( animationTracks[ j ] );
789
-
790
- }
791
-
792
- }
793
-
794
- return new AnimationClip( name, duration, tracks );
795
-
796
- }
797
-
798
- function getAnimationClip( id ) {
799
-
800
- return getBuild( library.clips[ id ], buildAnimationClip );
801
-
802
- }
803
-
804
- // controller
805
-
806
- function parseController( xml ) {
807
-
808
- const data = {};
809
-
810
- for ( let i = 0, l = xml.childNodes.length; i < l; i ++ ) {
811
-
812
- const child = xml.childNodes[ i ];
813
-
814
- if ( child.nodeType !== 1 ) continue;
815
-
816
- switch ( child.nodeName ) {
817
-
818
- case 'skin':
819
- // there is exactly one skin per controller
820
- data.id = parseId( child.getAttribute( 'source' ) );
821
- data.skin = parseSkin( child );
822
- break;
823
-
824
- case 'morph':
825
- data.id = parseId( child.getAttribute( 'source' ) );
826
- console.warn( 'THREE.ColladaLoader: Morph target animation not supported yet.' );
827
- break;
828
-
829
- }
830
-
831
- }
832
-
833
- library.controllers[ xml.getAttribute( 'id' ) ] = data;
834
-
835
- }
836
-
837
- function parseSkin( xml ) {
838
-
839
- const data = {
840
- sources: {}
841
- };
842
-
843
- for ( let i = 0, l = xml.childNodes.length; i < l; i ++ ) {
844
-
845
- const child = xml.childNodes[ i ];
846
-
847
- if ( child.nodeType !== 1 ) continue;
848
-
849
- switch ( child.nodeName ) {
850
-
851
- case 'bind_shape_matrix':
852
- data.bindShapeMatrix = parseFloats( child.textContent );
853
- break;
854
-
855
- case 'source':
856
- const id = child.getAttribute( 'id' );
857
- data.sources[ id ] = parseSource( child );
858
- break;
859
-
860
- case 'joints':
861
- data.joints = parseJoints( child );
862
- break;
863
-
864
- case 'vertex_weights':
865
- data.vertexWeights = parseVertexWeights( child );
866
- break;
867
-
868
- }
869
-
870
- }
871
-
872
- return data;
873
-
874
- }
875
-
876
- function parseJoints( xml ) {
877
-
878
- const data = {
879
- inputs: {}
880
- };
881
-
882
- for ( let i = 0, l = xml.childNodes.length; i < l; i ++ ) {
883
-
884
- const child = xml.childNodes[ i ];
885
-
886
- if ( child.nodeType !== 1 ) continue;
887
-
888
- switch ( child.nodeName ) {
889
-
890
- case 'input':
891
- const semantic = child.getAttribute( 'semantic' );
892
- const id = parseId( child.getAttribute( 'source' ) );
893
- data.inputs[ semantic ] = id;
894
- break;
895
-
896
- }
897
-
898
- }
899
-
900
- return data;
901
-
902
- }
903
-
904
- function parseVertexWeights( xml ) {
905
-
906
- const data = {
907
- inputs: {}
908
- };
909
-
910
- for ( let i = 0, l = xml.childNodes.length; i < l; i ++ ) {
911
-
912
- const child = xml.childNodes[ i ];
913
-
914
- if ( child.nodeType !== 1 ) continue;
915
-
916
- switch ( child.nodeName ) {
917
-
918
- case 'input':
919
- const semantic = child.getAttribute( 'semantic' );
920
- const id = parseId( child.getAttribute( 'source' ) );
921
- const offset = parseInt( child.getAttribute( 'offset' ) );
922
- data.inputs[ semantic ] = { id: id, offset: offset };
923
- break;
924
-
925
- case 'vcount':
926
- data.vcount = parseInts( child.textContent );
927
- break;
928
-
929
- case 'v':
930
- data.v = parseInts( child.textContent );
931
- break;
932
-
933
- }
934
-
935
- }
936
-
937
- return data;
938
-
939
- }
940
-
941
- function buildController( data ) {
942
-
943
- const build = {
944
- id: data.id
945
- };
946
-
947
- const geometry = library.geometries[ build.id ];
948
-
949
- if ( data.skin !== undefined ) {
950
-
951
- build.skin = buildSkin( data.skin );
952
-
953
- // we enhance the 'sources' property of the corresponding geometry with our skin data
954
-
955
- geometry.sources.skinIndices = build.skin.indices;
956
- geometry.sources.skinWeights = build.skin.weights;
957
-
958
- }
959
-
960
- return build;
961
-
962
- }
963
-
964
- function buildSkin( data ) {
965
-
966
- const BONE_LIMIT = 4;
967
-
968
- const build = {
969
- joints: [], // this must be an array to preserve the joint order
970
- indices: {
971
- array: [],
972
- stride: BONE_LIMIT
973
- },
974
- weights: {
975
- array: [],
976
- stride: BONE_LIMIT
977
- }
978
- };
979
-
980
- const sources = data.sources;
981
- const vertexWeights = data.vertexWeights;
982
-
983
- const vcount = vertexWeights.vcount;
984
- const v = vertexWeights.v;
985
- const jointOffset = vertexWeights.inputs.JOINT.offset;
986
- const weightOffset = vertexWeights.inputs.WEIGHT.offset;
987
-
988
- const jointSource = data.sources[ data.joints.inputs.JOINT ];
989
- const inverseSource = data.sources[ data.joints.inputs.INV_BIND_MATRIX ];
990
-
991
- const weights = sources[ vertexWeights.inputs.WEIGHT.id ].array;
992
- let stride = 0;
993
-
994
- let i, j, l;
995
-
996
- // process skin data for each vertex
997
-
998
- for ( i = 0, l = vcount.length; i < l; i ++ ) {
999
-
1000
- const jointCount = vcount[ i ]; // this is the amount of joints that affect a single vertex
1001
- const vertexSkinData = [];
1002
-
1003
- for ( j = 0; j < jointCount; j ++ ) {
1004
-
1005
- const skinIndex = v[ stride + jointOffset ];
1006
- const weightId = v[ stride + weightOffset ];
1007
- const skinWeight = weights[ weightId ];
1008
-
1009
- vertexSkinData.push( { index: skinIndex, weight: skinWeight } );
1010
-
1011
- stride += 2;
1012
-
1013
- }
1014
-
1015
- // we sort the joints in descending order based on the weights.
1016
- // this ensures, we only proceed the most important joints of the vertex
1017
-
1018
- vertexSkinData.sort( descending );
1019
-
1020
- // now we provide for each vertex a set of four index and weight values.
1021
- // the order of the skin data matches the order of vertices
1022
-
1023
- for ( j = 0; j < BONE_LIMIT; j ++ ) {
1024
-
1025
- const d = vertexSkinData[ j ];
1026
-
1027
- if ( d !== undefined ) {
1028
-
1029
- build.indices.array.push( d.index );
1030
- build.weights.array.push( d.weight );
1031
-
1032
- } else {
1033
-
1034
- build.indices.array.push( 0 );
1035
- build.weights.array.push( 0 );
1036
-
1037
- }
1038
-
1039
- }
1040
-
1041
- }
1042
-
1043
- // setup bind matrix
1044
-
1045
- if ( data.bindShapeMatrix ) {
1046
-
1047
- build.bindMatrix = new Matrix4().fromArray( data.bindShapeMatrix ).transpose();
1048
-
1049
- } else {
1050
-
1051
- build.bindMatrix = new Matrix4().identity();
1052
-
1053
- }
1054
-
1055
- // process bones and inverse bind matrix data
1056
-
1057
- for ( i = 0, l = jointSource.array.length; i < l; i ++ ) {
1058
-
1059
- const name = jointSource.array[ i ];
1060
- const boneInverse = new Matrix4().fromArray( inverseSource.array, i * inverseSource.stride ).transpose();
1061
-
1062
- build.joints.push( { name: name, boneInverse: boneInverse } );
1063
-
1064
- }
1065
-
1066
- return build;
1067
-
1068
- // array sort function
1069
-
1070
- function descending( a, b ) {
1071
-
1072
- return b.weight - a.weight;
1073
-
1074
- }
1075
-
1076
- }
1077
-
1078
- function getController( id ) {
1079
-
1080
- return getBuild( library.controllers[ id ], buildController );
1081
-
1082
- }
1083
-
1084
- // image
1085
-
1086
- function parseImage( xml ) {
1087
-
1088
- const data = {
1089
- init_from: getElementsByTagName( xml, 'init_from' )[ 0 ].textContent
1090
- };
1091
-
1092
- library.images[ xml.getAttribute( 'id' ) ] = data;
1093
-
1094
- }
1095
-
1096
- function buildImage( data ) {
1097
-
1098
- if ( data.build !== undefined ) return data.build;
1099
-
1100
- return data.init_from;
1101
-
1102
- }
1103
-
1104
- function getImage( id ) {
1105
-
1106
- const data = library.images[ id ];
1107
-
1108
- if ( data !== undefined ) {
1109
-
1110
- return getBuild( data, buildImage );
1111
-
1112
- }
1113
-
1114
- console.warn( 'THREE.ColladaLoader: Couldn\'t find image with ID:', id );
1115
-
1116
- return null;
1117
-
1118
- }
1119
-
1120
- // effect
1121
-
1122
- function parseEffect( xml ) {
1123
-
1124
- const data = {};
1125
-
1126
- for ( let i = 0, l = xml.childNodes.length; i < l; i ++ ) {
1127
-
1128
- const child = xml.childNodes[ i ];
1129
-
1130
- if ( child.nodeType !== 1 ) continue;
1131
-
1132
- switch ( child.nodeName ) {
1133
-
1134
- case 'profile_COMMON':
1135
- data.profile = parseEffectProfileCOMMON( child );
1136
- break;
1137
-
1138
- }
1139
-
1140
- }
1141
-
1142
- library.effects[ xml.getAttribute( 'id' ) ] = data;
1143
-
1144
- }
1145
-
1146
- function parseEffectProfileCOMMON( xml ) {
1147
-
1148
- const data = {
1149
- surfaces: {},
1150
- samplers: {}
1151
- };
1152
-
1153
- for ( let i = 0, l = xml.childNodes.length; i < l; i ++ ) {
1154
-
1155
- const child = xml.childNodes[ i ];
1156
-
1157
- if ( child.nodeType !== 1 ) continue;
1158
-
1159
- switch ( child.nodeName ) {
1160
-
1161
- case 'newparam':
1162
- parseEffectNewparam( child, data );
1163
- break;
1164
-
1165
- case 'technique':
1166
- data.technique = parseEffectTechnique( child );
1167
- break;
1168
-
1169
- case 'extra':
1170
- data.extra = parseEffectExtra( child );
1171
- break;
1172
-
1173
- }
1174
-
1175
- }
1176
-
1177
- return data;
1178
-
1179
- }
1180
-
1181
- function parseEffectNewparam( xml, data ) {
1182
-
1183
- const sid = xml.getAttribute( 'sid' );
1184
-
1185
- for ( let i = 0, l = xml.childNodes.length; i < l; i ++ ) {
1186
-
1187
- const child = xml.childNodes[ i ];
1188
-
1189
- if ( child.nodeType !== 1 ) continue;
1190
-
1191
- switch ( child.nodeName ) {
1192
-
1193
- case 'surface':
1194
- data.surfaces[ sid ] = parseEffectSurface( child );
1195
- break;
1196
-
1197
- case 'sampler2D':
1198
- data.samplers[ sid ] = parseEffectSampler( child );
1199
- break;
1200
-
1201
- }
1202
-
1203
- }
1204
-
1205
- }
1206
-
1207
- function parseEffectSurface( xml ) {
1208
-
1209
- const data = {};
1210
-
1211
- for ( let i = 0, l = xml.childNodes.length; i < l; i ++ ) {
1212
-
1213
- const child = xml.childNodes[ i ];
1214
-
1215
- if ( child.nodeType !== 1 ) continue;
1216
-
1217
- switch ( child.nodeName ) {
1218
-
1219
- case 'init_from':
1220
- data.init_from = child.textContent;
1221
- break;
1222
-
1223
- }
1224
-
1225
- }
1226
-
1227
- return data;
1228
-
1229
- }
1230
-
1231
- function parseEffectSampler( xml ) {
1232
-
1233
- const data = {};
1234
-
1235
- for ( let i = 0, l = xml.childNodes.length; i < l; i ++ ) {
1236
-
1237
- const child = xml.childNodes[ i ];
1238
-
1239
- if ( child.nodeType !== 1 ) continue;
1240
-
1241
- switch ( child.nodeName ) {
1242
-
1243
- case 'source':
1244
- data.source = child.textContent;
1245
- break;
1246
-
1247
- }
1248
-
1249
- }
1250
-
1251
- return data;
1252
-
1253
- }
1254
-
1255
- function parseEffectTechnique( xml ) {
1256
-
1257
- const data = {};
1258
-
1259
- for ( let i = 0, l = xml.childNodes.length; i < l; i ++ ) {
1260
-
1261
- const child = xml.childNodes[ i ];
1262
-
1263
- if ( child.nodeType !== 1 ) continue;
1264
-
1265
- switch ( child.nodeName ) {
1266
-
1267
- case 'constant':
1268
- case 'lambert':
1269
- case 'blinn':
1270
- case 'phong':
1271
- data.type = child.nodeName;
1272
- data.parameters = parseEffectParameters( child );
1273
- break;
1274
-
1275
- case 'extra':
1276
- data.extra = parseEffectExtra( child );
1277
- break;
1278
-
1279
- }
1280
-
1281
- }
1282
-
1283
- return data;
1284
-
1285
- }
1286
-
1287
- function parseEffectParameters( xml ) {
1288
-
1289
- const data = {};
1290
-
1291
- for ( let i = 0, l = xml.childNodes.length; i < l; i ++ ) {
1292
-
1293
- const child = xml.childNodes[ i ];
1294
-
1295
- if ( child.nodeType !== 1 ) continue;
1296
-
1297
- switch ( child.nodeName ) {
1298
-
1299
- case 'emission':
1300
- case 'diffuse':
1301
- case 'specular':
1302
- case 'bump':
1303
- case 'ambient':
1304
- case 'shininess':
1305
- case 'transparency':
1306
- data[ child.nodeName ] = parseEffectParameter( child );
1307
- break;
1308
- case 'transparent':
1309
- data[ child.nodeName ] = {
1310
- opaque: child.hasAttribute( 'opaque' ) ? child.getAttribute( 'opaque' ) : 'A_ONE',
1311
- data: parseEffectParameter( child )
1312
- };
1313
- break;
1314
-
1315
- }
1316
-
1317
- }
1318
-
1319
- return data;
1320
-
1321
- }
1322
-
1323
- function parseEffectParameter( xml ) {
1324
-
1325
- const data = {};
1326
-
1327
- for ( let i = 0, l = xml.childNodes.length; i < l; i ++ ) {
1328
-
1329
- const child = xml.childNodes[ i ];
1330
-
1331
- if ( child.nodeType !== 1 ) continue;
1332
-
1333
- switch ( child.nodeName ) {
1334
-
1335
- case 'color':
1336
- data[ child.nodeName ] = parseFloats( child.textContent );
1337
- break;
1338
-
1339
- case 'float':
1340
- data[ child.nodeName ] = parseFloat( child.textContent );
1341
- break;
1342
-
1343
- case 'texture':
1344
- data[ child.nodeName ] = { id: child.getAttribute( 'texture' ), extra: parseEffectParameterTexture( child ) };
1345
- break;
1346
-
1347
- }
1348
-
1349
- }
1350
-
1351
- return data;
1352
-
1353
- }
1354
-
1355
- function parseEffectParameterTexture( xml ) {
1356
-
1357
- const data = {
1358
- technique: {}
1359
- };
1360
-
1361
- for ( let i = 0, l = xml.childNodes.length; i < l; i ++ ) {
1362
-
1363
- const child = xml.childNodes[ i ];
1364
-
1365
- if ( child.nodeType !== 1 ) continue;
1366
-
1367
- switch ( child.nodeName ) {
1368
-
1369
- case 'extra':
1370
- parseEffectParameterTextureExtra( child, data );
1371
- break;
1372
-
1373
- }
1374
-
1375
- }
1376
-
1377
- return data;
1378
-
1379
- }
1380
-
1381
- function parseEffectParameterTextureExtra( xml, data ) {
1382
-
1383
- for ( let i = 0, l = xml.childNodes.length; i < l; i ++ ) {
1384
-
1385
- const child = xml.childNodes[ i ];
1386
-
1387
- if ( child.nodeType !== 1 ) continue;
1388
-
1389
- switch ( child.nodeName ) {
1390
-
1391
- case 'technique':
1392
- parseEffectParameterTextureExtraTechnique( child, data );
1393
- break;
1394
-
1395
- }
1396
-
1397
- }
1398
-
1399
- }
1400
-
1401
- function parseEffectParameterTextureExtraTechnique( xml, data ) {
1402
-
1403
- for ( let i = 0, l = xml.childNodes.length; i < l; i ++ ) {
1404
-
1405
- const child = xml.childNodes[ i ];
1406
-
1407
- if ( child.nodeType !== 1 ) continue;
1408
-
1409
- switch ( child.nodeName ) {
1410
-
1411
- case 'repeatU':
1412
- case 'repeatV':
1413
- case 'offsetU':
1414
- case 'offsetV':
1415
- data.technique[ child.nodeName ] = parseFloat( child.textContent );
1416
- break;
1417
-
1418
- case 'wrapU':
1419
- case 'wrapV':
1420
-
1421
- // some files have values for wrapU/wrapV which become NaN via parseInt
1422
-
1423
- if ( child.textContent.toUpperCase() === 'TRUE' ) {
1424
-
1425
- data.technique[ child.nodeName ] = 1;
1426
-
1427
- } else if ( child.textContent.toUpperCase() === 'FALSE' ) {
1428
-
1429
- data.technique[ child.nodeName ] = 0;
1430
-
1431
- } else {
1432
-
1433
- data.technique[ child.nodeName ] = parseInt( child.textContent );
1434
-
1435
- }
1436
-
1437
- break;
1438
-
1439
- case 'bump':
1440
- data[ child.nodeName ] = parseEffectExtraTechniqueBump( child );
1441
- break;
1442
-
1443
- }
1444
-
1445
- }
1446
-
1447
- }
1448
-
1449
- function parseEffectExtra( xml ) {
1450
-
1451
- const data = {};
1452
-
1453
- for ( let i = 0, l = xml.childNodes.length; i < l; i ++ ) {
1454
-
1455
- const child = xml.childNodes[ i ];
1456
-
1457
- if ( child.nodeType !== 1 ) continue;
1458
-
1459
- switch ( child.nodeName ) {
1460
-
1461
- case 'technique':
1462
- data.technique = parseEffectExtraTechnique( child );
1463
- break;
1464
-
1465
- }
1466
-
1467
- }
1468
-
1469
- return data;
1470
-
1471
- }
1472
-
1473
- function parseEffectExtraTechnique( xml ) {
1474
-
1475
- const data = {};
1476
-
1477
- for ( let i = 0, l = xml.childNodes.length; i < l; i ++ ) {
1478
-
1479
- const child = xml.childNodes[ i ];
1480
-
1481
- if ( child.nodeType !== 1 ) continue;
1482
-
1483
- switch ( child.nodeName ) {
1484
-
1485
- case 'double_sided':
1486
- data[ child.nodeName ] = parseInt( child.textContent );
1487
- break;
1488
-
1489
- case 'bump':
1490
- data[ child.nodeName ] = parseEffectExtraTechniqueBump( child );
1491
- break;
1492
-
1493
- }
1494
-
1495
- }
1496
-
1497
- return data;
1498
-
1499
- }
1500
-
1501
- function parseEffectExtraTechniqueBump( xml ) {
1502
-
1503
- const data = {};
1504
-
1505
- for ( let i = 0, l = xml.childNodes.length; i < l; i ++ ) {
1506
-
1507
- const child = xml.childNodes[ i ];
1508
-
1509
- if ( child.nodeType !== 1 ) continue;
1510
-
1511
- switch ( child.nodeName ) {
1512
-
1513
- case 'texture':
1514
- data[ child.nodeName ] = { id: child.getAttribute( 'texture' ), texcoord: child.getAttribute( 'texcoord' ), extra: parseEffectParameterTexture( child ) };
1515
- break;
1516
-
1517
- }
1518
-
1519
- }
1520
-
1521
- return data;
1522
-
1523
- }
1524
-
1525
- function buildEffect( data ) {
1526
-
1527
- return data;
1528
-
1529
- }
1530
-
1531
- function getEffect( id ) {
1532
-
1533
- return getBuild( library.effects[ id ], buildEffect );
1534
-
1535
- }
1536
-
1537
- // material
1538
-
1539
- function parseMaterial( xml ) {
1540
-
1541
- const data = {
1542
- name: xml.getAttribute( 'name' )
1543
- };
1544
-
1545
- for ( let i = 0, l = xml.childNodes.length; i < l; i ++ ) {
1546
-
1547
- const child = xml.childNodes[ i ];
1548
-
1549
- if ( child.nodeType !== 1 ) continue;
1550
-
1551
- switch ( child.nodeName ) {
1552
-
1553
- case 'instance_effect':
1554
- data.url = parseId( child.getAttribute( 'url' ) );
1555
- break;
1556
-
1557
- }
1558
-
1559
- }
1560
-
1561
- library.materials[ xml.getAttribute( 'id' ) ] = data;
1562
-
1563
- }
1564
-
1565
- function getTextureLoader( image ) {
1566
-
1567
- let loader;
1568
-
1569
- let extension = image.slice( ( image.lastIndexOf( '.' ) - 1 >>> 0 ) + 2 ); // http://www.jstips.co/en/javascript/get-file-extension/
1570
- extension = extension.toLowerCase();
1571
-
1572
- switch ( extension ) {
1573
-
1574
- case 'tga':
1575
- loader = tgaLoader;
1576
- break;
1577
-
1578
- default:
1579
- loader = textureLoader;
1580
-
1581
- }
1582
-
1583
- return loader;
1584
-
1585
- }
1586
-
1587
- function buildMaterial( data ) {
1588
-
1589
- const effect = getEffect( data.url );
1590
- const technique = effect.profile.technique;
1591
-
1592
- let material;
1593
-
1594
- switch ( technique.type ) {
1595
-
1596
- case 'phong':
1597
- case 'blinn':
1598
- material = new MeshPhongMaterial();
1599
- break;
1600
-
1601
- case 'lambert':
1602
- material = new MeshLambertMaterial();
1603
- break;
1604
-
1605
- default:
1606
- material = new MeshBasicMaterial();
1607
- break;
1608
-
1609
- }
1610
-
1611
- material.name = data.name || '';
1612
-
1613
- function getTexture( textureObject, colorSpace = null ) {
1614
-
1615
- const sampler = effect.profile.samplers[ textureObject.id ];
1616
- let image = null;
1617
-
1618
- // get image
1619
-
1620
- if ( sampler !== undefined ) {
1621
-
1622
- const surface = effect.profile.surfaces[ sampler.source ];
1623
- image = getImage( surface.init_from );
1624
-
1625
- } else {
1626
-
1627
- console.warn( 'THREE.ColladaLoader: Undefined sampler. Access image directly (see #12530).' );
1628
- image = getImage( textureObject.id );
1629
-
1630
- }
1631
-
1632
- // create texture if image is available
1633
-
1634
- if ( image !== null ) {
1635
-
1636
- const loader = getTextureLoader( image );
1637
-
1638
- if ( loader !== undefined ) {
1639
-
1640
- const texture = loader.load( image );
1641
-
1642
- const extra = textureObject.extra;
1643
-
1644
- if ( extra !== undefined && extra.technique !== undefined && isEmpty( extra.technique ) === false ) {
1645
-
1646
- const technique = extra.technique;
1647
-
1648
- texture.wrapS = technique.wrapU ? RepeatWrapping : ClampToEdgeWrapping;
1649
- texture.wrapT = technique.wrapV ? RepeatWrapping : ClampToEdgeWrapping;
1650
-
1651
- texture.offset.set( technique.offsetU || 0, technique.offsetV || 0 );
1652
- texture.repeat.set( technique.repeatU || 1, technique.repeatV || 1 );
1653
-
1654
- } else {
1655
-
1656
- texture.wrapS = RepeatWrapping;
1657
- texture.wrapT = RepeatWrapping;
1658
-
1659
- }
1660
-
1661
- if ( colorSpace !== null ) {
1662
-
1663
- texture.colorSpace = colorSpace;
1664
-
1665
- }
1666
-
1667
- return texture;
1668
-
1669
- } else {
1670
-
1671
- console.warn( 'THREE.ColladaLoader: Loader for texture %s not found.', image );
1672
-
1673
- return null;
1674
-
1675
- }
1676
-
1677
- } else {
1678
-
1679
- console.warn( 'THREE.ColladaLoader: Couldn\'t create texture with ID:', textureObject.id );
1680
-
1681
- return null;
1682
-
1683
- }
1684
-
1685
- }
1686
-
1687
- const parameters = technique.parameters;
1688
-
1689
- for ( const key in parameters ) {
1690
-
1691
- const parameter = parameters[ key ];
1692
-
1693
- switch ( key ) {
1694
-
1695
- case 'diffuse':
1696
- if ( parameter.color ) material.color.fromArray( parameter.color );
1697
- if ( parameter.texture ) material.map = getTexture( parameter.texture, SRGBColorSpace );
1698
- break;
1699
- case 'specular':
1700
- if ( parameter.color && material.specular ) material.specular.fromArray( parameter.color );
1701
- if ( parameter.texture ) material.specularMap = getTexture( parameter.texture );
1702
- break;
1703
- case 'bump':
1704
- if ( parameter.texture ) material.normalMap = getTexture( parameter.texture );
1705
- break;
1706
- case 'ambient':
1707
- if ( parameter.texture ) material.lightMap = getTexture( parameter.texture, SRGBColorSpace );
1708
- break;
1709
- case 'shininess':
1710
- if ( parameter.float && material.shininess ) material.shininess = parameter.float;
1711
- break;
1712
- case 'emission':
1713
- if ( parameter.color && material.emissive ) material.emissive.fromArray( parameter.color );
1714
- if ( parameter.texture ) material.emissiveMap = getTexture( parameter.texture, SRGBColorSpace );
1715
- break;
1716
-
1717
- }
1718
-
1719
- }
1720
-
1721
- ColorManagement.colorSpaceToWorking( material.color, SRGBColorSpace );
1722
- if ( material.specular ) ColorManagement.colorSpaceToWorking( material.specular, SRGBColorSpace );
1723
- if ( material.emissive ) ColorManagement.colorSpaceToWorking( material.emissive, SRGBColorSpace );
1724
-
1725
- //
1726
-
1727
- let transparent = parameters[ 'transparent' ];
1728
- let transparency = parameters[ 'transparency' ];
1729
-
1730
- // <transparency> does not exist but <transparent>
1731
-
1732
- if ( transparency === undefined && transparent ) {
1733
-
1734
- transparency = {
1735
- float: 1
1736
- };
1737
-
1738
- }
1739
-
1740
- // <transparent> does not exist but <transparency>
1741
-
1742
- if ( transparent === undefined && transparency ) {
1743
-
1744
- transparent = {
1745
- opaque: 'A_ONE',
1746
- data: {
1747
- color: [ 1, 1, 1, 1 ]
1748
- } };
1749
-
1750
- }
1751
-
1752
- if ( transparent && transparency ) {
1753
-
1754
- // handle case if a texture exists but no color
1755
-
1756
- if ( transparent.data.texture ) {
1757
-
1758
- // we do not set an alpha map (see #13792)
1759
-
1760
- material.transparent = true;
1761
-
1762
- } else {
1763
-
1764
- const color = transparent.data.color;
1765
-
1766
- switch ( transparent.opaque ) {
1767
-
1768
- case 'A_ONE':
1769
- material.opacity = color[ 3 ] * transparency.float;
1770
- break;
1771
- case 'RGB_ZERO':
1772
- material.opacity = 1 - ( color[ 0 ] * transparency.float );
1773
- break;
1774
- case 'A_ZERO':
1775
- material.opacity = 1 - ( color[ 3 ] * transparency.float );
1776
- break;
1777
- case 'RGB_ONE':
1778
- material.opacity = color[ 0 ] * transparency.float;
1779
- break;
1780
- default:
1781
- console.warn( 'THREE.ColladaLoader: Invalid opaque type "%s" of transparent tag.', transparent.opaque );
1782
-
1783
- }
1784
-
1785
- if ( material.opacity < 1 ) material.transparent = true;
1786
-
1787
- }
1788
-
1789
- }
1790
-
1791
- //
1792
-
1793
-
1794
- if ( technique.extra !== undefined && technique.extra.technique !== undefined ) {
1795
-
1796
- const techniques = technique.extra.technique;
1797
-
1798
- for ( const k in techniques ) {
1799
-
1800
- const v = techniques[ k ];
1801
-
1802
- switch ( k ) {
1803
-
1804
- case 'double_sided':
1805
- material.side = ( v === 1 ? DoubleSide : FrontSide );
1806
- break;
1807
-
1808
- case 'bump':
1809
- material.normalMap = getTexture( v.texture );
1810
- material.normalScale = new Vector2( 1, 1 );
1811
- break;
1812
-
1813
- }
1814
-
1815
- }
1816
-
1817
- }
1818
-
1819
- return material;
1820
-
1821
- }
1822
-
1823
- function getMaterial( id ) {
1824
-
1825
- return getBuild( library.materials[ id ], buildMaterial );
1826
-
1827
- }
1828
-
1829
- // camera
1830
-
1831
- function parseCamera( xml ) {
1832
-
1833
- const data = {
1834
- name: xml.getAttribute( 'name' )
1835
- };
1836
-
1837
- for ( let i = 0, l = xml.childNodes.length; i < l; i ++ ) {
1838
-
1839
- const child = xml.childNodes[ i ];
1840
-
1841
- if ( child.nodeType !== 1 ) continue;
1842
-
1843
- switch ( child.nodeName ) {
1844
-
1845
- case 'optics':
1846
- data.optics = parseCameraOptics( child );
1847
- break;
1848
-
1849
- }
1850
-
1851
- }
1852
-
1853
- library.cameras[ xml.getAttribute( 'id' ) ] = data;
1854
-
1855
- }
1856
-
1857
- function parseCameraOptics( xml ) {
1858
-
1859
- for ( let i = 0; i < xml.childNodes.length; i ++ ) {
1860
-
1861
- const child = xml.childNodes[ i ];
1862
-
1863
- switch ( child.nodeName ) {
1864
-
1865
- case 'technique_common':
1866
- return parseCameraTechnique( child );
1867
-
1868
- }
1869
-
1870
- }
1871
-
1872
- return {};
1873
-
1874
- }
1875
-
1876
- function parseCameraTechnique( xml ) {
1877
-
1878
- const data = {};
1879
-
1880
- for ( let i = 0; i < xml.childNodes.length; i ++ ) {
1881
-
1882
- const child = xml.childNodes[ i ];
1883
-
1884
- switch ( child.nodeName ) {
1885
-
1886
- case 'perspective':
1887
- case 'orthographic':
1888
-
1889
- data.technique = child.nodeName;
1890
- data.parameters = parseCameraParameters( child );
1891
-
1892
- break;
1893
-
1894
- }
1895
-
1896
- }
1897
-
1898
- return data;
1899
-
1900
- }
1901
-
1902
- function parseCameraParameters( xml ) {
1903
-
1904
- const data = {};
1905
-
1906
- for ( let i = 0; i < xml.childNodes.length; i ++ ) {
1907
-
1908
- const child = xml.childNodes[ i ];
1909
-
1910
- switch ( child.nodeName ) {
1911
-
1912
- case 'xfov':
1913
- case 'yfov':
1914
- case 'xmag':
1915
- case 'ymag':
1916
- case 'znear':
1917
- case 'zfar':
1918
- case 'aspect_ratio':
1919
- data[ child.nodeName ] = parseFloat( child.textContent );
1920
- break;
1921
-
1922
- }
1923
-
1924
- }
1925
-
1926
- return data;
1927
-
1928
- }
1929
-
1930
- function buildCamera( data ) {
1931
-
1932
- let camera;
1933
-
1934
- switch ( data.optics.technique ) {
1935
-
1936
- case 'perspective':
1937
- camera = new PerspectiveCamera(
1938
- data.optics.parameters.yfov,
1939
- data.optics.parameters.aspect_ratio,
1940
- data.optics.parameters.znear,
1941
- data.optics.parameters.zfar
1942
- );
1943
- break;
1944
-
1945
- case 'orthographic':
1946
- let ymag = data.optics.parameters.ymag;
1947
- let xmag = data.optics.parameters.xmag;
1948
- const aspectRatio = data.optics.parameters.aspect_ratio;
1949
-
1950
- xmag = ( xmag === undefined ) ? ( ymag * aspectRatio ) : xmag;
1951
- ymag = ( ymag === undefined ) ? ( xmag / aspectRatio ) : ymag;
1952
-
1953
- xmag *= 0.5;
1954
- ymag *= 0.5;
1955
-
1956
- camera = new OrthographicCamera(
1957
- - xmag, xmag, ymag, - ymag, // left, right, top, bottom
1958
- data.optics.parameters.znear,
1959
- data.optics.parameters.zfar
1960
- );
1961
- break;
1962
-
1963
- default:
1964
- camera = new PerspectiveCamera();
1965
- break;
1966
-
1967
- }
1968
-
1969
- camera.name = data.name || '';
1970
-
1971
- return camera;
1972
-
1973
- }
1974
-
1975
- function getCamera( id ) {
1976
-
1977
- const data = library.cameras[ id ];
1978
-
1979
- if ( data !== undefined ) {
1980
-
1981
- return getBuild( data, buildCamera );
1982
-
1983
- }
1984
-
1985
- console.warn( 'THREE.ColladaLoader: Couldn\'t find camera with ID:', id );
1986
-
1987
- return null;
1988
-
1989
- }
1990
-
1991
- // light
1992
-
1993
- function parseLight( xml ) {
1994
-
1995
- let data = {};
1996
-
1997
- for ( let i = 0, l = xml.childNodes.length; i < l; i ++ ) {
1998
-
1999
- const child = xml.childNodes[ i ];
2000
-
2001
- if ( child.nodeType !== 1 ) continue;
2002
-
2003
- switch ( child.nodeName ) {
2004
-
2005
- case 'technique_common':
2006
- data = parseLightTechnique( child );
2007
- break;
2008
-
2009
- }
2010
-
2011
- }
2012
-
2013
- library.lights[ xml.getAttribute( 'id' ) ] = data;
2014
-
2015
- }
2016
-
2017
- function parseLightTechnique( xml ) {
2018
-
2019
- const data = {};
2020
-
2021
- for ( let i = 0, l = xml.childNodes.length; i < l; i ++ ) {
2022
-
2023
- const child = xml.childNodes[ i ];
2024
-
2025
- if ( child.nodeType !== 1 ) continue;
2026
-
2027
- switch ( child.nodeName ) {
2028
-
2029
- case 'directional':
2030
- case 'point':
2031
- case 'spot':
2032
- case 'ambient':
2033
-
2034
- data.technique = child.nodeName;
2035
- data.parameters = parseLightParameters( child );
2036
-
2037
- }
2038
-
2039
- }
2040
-
2041
- return data;
2042
-
2043
- }
2044
-
2045
- function parseLightParameters( xml ) {
2046
-
2047
- const data = {};
2048
-
2049
- for ( let i = 0, l = xml.childNodes.length; i < l; i ++ ) {
2050
-
2051
- const child = xml.childNodes[ i ];
2052
-
2053
- if ( child.nodeType !== 1 ) continue;
2054
-
2055
- switch ( child.nodeName ) {
2056
-
2057
- case 'color':
2058
- const array = parseFloats( child.textContent );
2059
- data.color = new Color().fromArray( array );
2060
- ColorManagement.colorSpaceToWorking( data.color, SRGBColorSpace );
2061
- break;
2062
-
2063
- case 'falloff_angle':
2064
- data.falloffAngle = parseFloat( child.textContent );
2065
- break;
2066
-
2067
- case 'quadratic_attenuation':
2068
- const f = parseFloat( child.textContent );
2069
- data.distance = f ? Math.sqrt( 1 / f ) : 0;
2070
- break;
2071
-
2072
- }
2073
-
2074
- }
2075
-
2076
- return data;
2077
-
2078
- }
2079
-
2080
- function buildLight( data ) {
2081
-
2082
- let light;
2083
-
2084
- switch ( data.technique ) {
2085
-
2086
- case 'directional':
2087
- light = new DirectionalLight();
2088
- break;
2089
-
2090
- case 'point':
2091
- light = new PointLight();
2092
- break;
2093
-
2094
- case 'spot':
2095
- light = new SpotLight();
2096
- break;
2097
-
2098
- case 'ambient':
2099
- light = new AmbientLight();
2100
- break;
2101
-
2102
- }
2103
-
2104
- if ( data.parameters.color ) light.color.copy( data.parameters.color );
2105
- if ( data.parameters.distance ) light.distance = data.parameters.distance;
2106
-
2107
- return light;
2108
-
2109
- }
2110
-
2111
- function getLight( id ) {
2112
-
2113
- const data = library.lights[ id ];
2114
-
2115
- if ( data !== undefined ) {
2116
-
2117
- return getBuild( data, buildLight );
2118
-
2119
- }
2120
-
2121
- console.warn( 'THREE.ColladaLoader: Couldn\'t find light with ID:', id );
2122
-
2123
- return null;
2124
-
2125
- }
2126
-
2127
- // geometry
2128
-
2129
- function parseGeometry( xml ) {
2130
-
2131
- const data = {
2132
- name: xml.getAttribute( 'name' ),
2133
- sources: {},
2134
- vertices: {},
2135
- primitives: []
2136
- };
2137
-
2138
- const mesh = getElementsByTagName( xml, 'mesh' )[ 0 ];
2139
-
2140
- // the following tags inside geometry are not supported yet (see https://github.com/mrdoob/three.js/pull/12606): convex_mesh, spline, brep
2141
- if ( mesh === undefined ) return;
2142
-
2143
- for ( let i = 0; i < mesh.childNodes.length; i ++ ) {
2144
-
2145
- const child = mesh.childNodes[ i ];
2146
-
2147
- if ( child.nodeType !== 1 ) continue;
2148
-
2149
- const id = child.getAttribute( 'id' );
2150
-
2151
- switch ( child.nodeName ) {
2152
-
2153
- case 'source':
2154
- data.sources[ id ] = parseSource( child );
2155
- break;
2156
-
2157
- case 'vertices':
2158
- // data.sources[ id ] = data.sources[ parseId( getElementsByTagName( child, 'input' )[ 0 ].getAttribute( 'source' ) ) ];
2159
- data.vertices = parseGeometryVertices( child );
2160
- break;
2161
-
2162
- case 'polygons':
2163
- console.warn( 'THREE.ColladaLoader: Unsupported primitive type: ', child.nodeName );
2164
- break;
2165
-
2166
- case 'lines':
2167
- case 'linestrips':
2168
- case 'polylist':
2169
- case 'triangles':
2170
- data.primitives.push( parseGeometryPrimitive( child ) );
2171
- break;
2172
-
2173
- default:
2174
- console.log( child );
2175
-
2176
- }
2177
-
2178
- }
2179
-
2180
- library.geometries[ xml.getAttribute( 'id' ) ] = data;
2181
-
2182
- }
2183
-
2184
- function parseSource( xml ) {
2185
-
2186
- const data = {
2187
- array: [],
2188
- stride: 3
2189
- };
2190
-
2191
- for ( let i = 0; i < xml.childNodes.length; i ++ ) {
2192
-
2193
- const child = xml.childNodes[ i ];
2194
-
2195
- if ( child.nodeType !== 1 ) continue;
2196
-
2197
- switch ( child.nodeName ) {
2198
-
2199
- case 'float_array':
2200
- data.array = parseFloats( child.textContent );
2201
- break;
2202
-
2203
- case 'Name_array':
2204
- data.array = parseStrings( child.textContent );
2205
- break;
2206
-
2207
- case 'technique_common':
2208
- const accessor = getElementsByTagName( child, 'accessor' )[ 0 ];
2209
-
2210
- if ( accessor !== undefined ) {
2211
-
2212
- data.stride = parseInt( accessor.getAttribute( 'stride' ) );
2213
-
2214
- }
2215
-
2216
- break;
2217
-
2218
- }
2219
-
2220
- }
2221
-
2222
- return data;
2223
-
2224
- }
2225
-
2226
- function parseGeometryVertices( xml ) {
2227
-
2228
- const data = {};
2229
-
2230
- for ( let i = 0; i < xml.childNodes.length; i ++ ) {
2231
-
2232
- const child = xml.childNodes[ i ];
2233
-
2234
- if ( child.nodeType !== 1 ) continue;
2235
-
2236
- data[ child.getAttribute( 'semantic' ) ] = parseId( child.getAttribute( 'source' ) );
2237
-
2238
- }
2239
-
2240
- return data;
2241
-
2242
- }
2243
-
2244
- function parseGeometryPrimitive( xml ) {
2245
-
2246
- const primitive = {
2247
- type: xml.nodeName,
2248
- material: xml.getAttribute( 'material' ),
2249
- count: parseInt( xml.getAttribute( 'count' ) ),
2250
- inputs: {},
2251
- stride: 0,
2252
- hasUV: false
2253
- };
2254
-
2255
- for ( let i = 0, l = xml.childNodes.length; i < l; i ++ ) {
2256
-
2257
- const child = xml.childNodes[ i ];
2258
-
2259
- if ( child.nodeType !== 1 ) continue;
2260
-
2261
- switch ( child.nodeName ) {
2262
-
2263
- case 'input':
2264
- const id = parseId( child.getAttribute( 'source' ) );
2265
- const semantic = child.getAttribute( 'semantic' );
2266
- const offset = parseInt( child.getAttribute( 'offset' ) );
2267
- const set = parseInt( child.getAttribute( 'set' ) );
2268
- const inputname = ( set > 0 ? semantic + set : semantic );
2269
- primitive.inputs[ inputname ] = { id: id, offset: offset };
2270
- primitive.stride = Math.max( primitive.stride, offset + 1 );
2271
- if ( semantic === 'TEXCOORD' ) primitive.hasUV = true;
2272
- break;
2273
-
2274
- case 'vcount':
2275
- primitive.vcount = parseInts( child.textContent );
2276
- break;
2277
-
2278
- case 'p':
2279
- primitive.p = parseInts( child.textContent );
2280
- break;
2281
-
2282
- }
2283
-
2284
- }
2285
-
2286
- return primitive;
2287
-
2288
- }
2289
-
2290
- function groupPrimitives( primitives ) {
2291
-
2292
- const build = {};
2293
-
2294
- for ( let i = 0; i < primitives.length; i ++ ) {
2295
-
2296
- const primitive = primitives[ i ];
2297
-
2298
- if ( build[ primitive.type ] === undefined ) build[ primitive.type ] = [];
2299
-
2300
- build[ primitive.type ].push( primitive );
2301
-
2302
- }
2303
-
2304
- return build;
2305
-
2306
- }
2307
-
2308
- function checkUVCoordinates( primitives ) {
2309
-
2310
- let count = 0;
2311
-
2312
- for ( let i = 0, l = primitives.length; i < l; i ++ ) {
2313
-
2314
- const primitive = primitives[ i ];
2315
-
2316
- if ( primitive.hasUV === true ) {
2317
-
2318
- count ++;
2319
-
2320
- }
2321
-
2322
- }
2323
-
2324
- if ( count > 0 && count < primitives.length ) {
2325
-
2326
- primitives.uvsNeedsFix = true;
2327
-
2328
- }
2329
-
2330
- }
2331
-
2332
- function buildGeometry( data ) {
2333
-
2334
- const build = {};
2335
-
2336
- const sources = data.sources;
2337
- const vertices = data.vertices;
2338
- const primitives = data.primitives;
2339
-
2340
- if ( primitives.length === 0 ) return {};
2341
-
2342
- // our goal is to create one buffer geometry for a single type of primitives
2343
- // first, we group all primitives by their type
2344
-
2345
- const groupedPrimitives = groupPrimitives( primitives );
2346
-
2347
- for ( const type in groupedPrimitives ) {
2348
-
2349
- const primitiveType = groupedPrimitives[ type ];
2350
-
2351
- // second, ensure consistent uv coordinates for each type of primitives (polylist,triangles or lines)
2352
-
2353
- checkUVCoordinates( primitiveType );
2354
-
2355
- // third, create a buffer geometry for each type of primitives
2356
-
2357
- build[ type ] = buildGeometryType( primitiveType, sources, vertices );
2358
-
2359
- }
2360
-
2361
- return build;
2362
-
2363
- }
2364
-
2365
- function buildGeometryType( primitives, sources, vertices ) {
2366
-
2367
- const build = {};
2368
-
2369
- const position = { array: [], stride: 0 };
2370
- const normal = { array: [], stride: 0 };
2371
- const uv = { array: [], stride: 0 };
2372
- const uv1 = { array: [], stride: 0 };
2373
- const color = { array: [], stride: 0 };
2374
-
2375
- const skinIndex = { array: [], stride: 4 };
2376
- const skinWeight = { array: [], stride: 4 };
2377
-
2378
- const geometry = new BufferGeometry();
2379
-
2380
- const materialKeys = [];
2381
-
2382
- let start = 0;
2383
-
2384
- for ( let p = 0; p < primitives.length; p ++ ) {
2385
-
2386
- const primitive = primitives[ p ];
2387
- const inputs = primitive.inputs;
2388
-
2389
- // groups
2390
-
2391
- let count = 0;
2392
-
2393
- switch ( primitive.type ) {
2394
-
2395
- case 'lines':
2396
- case 'linestrips':
2397
- count = primitive.count * 2;
2398
- break;
2399
-
2400
- case 'triangles':
2401
- count = primitive.count * 3;
2402
- break;
2403
-
2404
- case 'polylist':
2405
-
2406
- for ( let g = 0; g < primitive.count; g ++ ) {
2407
-
2408
- const vc = primitive.vcount[ g ];
2409
-
2410
- switch ( vc ) {
2411
-
2412
- case 3:
2413
- count += 3; // single triangle
2414
- break;
2415
-
2416
- case 4:
2417
- count += 6; // quad, subdivided into two triangles
2418
- break;
2419
-
2420
- default:
2421
- count += ( vc - 2 ) * 3; // polylist with more than four vertices
2422
- break;
2423
-
2424
- }
2425
-
2426
- }
2427
-
2428
- break;
2429
-
2430
- default:
2431
- console.warn( 'THREE.ColladaLoader: Unknown primitive type:', primitive.type );
2432
-
2433
- }
2434
-
2435
- geometry.addGroup( start, count, p );
2436
- start += count;
2437
-
2438
- // material
2439
-
2440
- if ( primitive.material ) {
2441
-
2442
- materialKeys.push( primitive.material );
2443
-
2444
- }
2445
-
2446
- // geometry data
2447
-
2448
- for ( const name in inputs ) {
2449
-
2450
- const input = inputs[ name ];
2451
-
2452
- switch ( name ) {
2453
-
2454
- case 'VERTEX':
2455
- for ( const key in vertices ) {
2456
-
2457
- const id = vertices[ key ];
2458
-
2459
- switch ( key ) {
2460
-
2461
- case 'POSITION':
2462
- const prevLength = position.array.length;
2463
- buildGeometryData( primitive, sources[ id ], input.offset, position.array );
2464
- position.stride = sources[ id ].stride;
2465
-
2466
- if ( sources.skinWeights && sources.skinIndices ) {
2467
-
2468
- buildGeometryData( primitive, sources.skinIndices, input.offset, skinIndex.array );
2469
- buildGeometryData( primitive, sources.skinWeights, input.offset, skinWeight.array );
2470
-
2471
- }
2472
-
2473
- // see #3803
2474
-
2475
- if ( primitive.hasUV === false && primitives.uvsNeedsFix === true ) {
2476
-
2477
- const count = ( position.array.length - prevLength ) / position.stride;
2478
-
2479
- for ( let i = 0; i < count; i ++ ) {
2480
-
2481
- // fill missing uv coordinates
2482
-
2483
- uv.array.push( 0, 0 );
2484
-
2485
- }
2486
-
2487
- }
2488
-
2489
- break;
2490
-
2491
- case 'NORMAL':
2492
- buildGeometryData( primitive, sources[ id ], input.offset, normal.array );
2493
- normal.stride = sources[ id ].stride;
2494
- break;
2495
-
2496
- case 'COLOR':
2497
- buildGeometryData( primitive, sources[ id ], input.offset, color.array );
2498
- color.stride = sources[ id ].stride;
2499
- break;
2500
-
2501
- case 'TEXCOORD':
2502
- buildGeometryData( primitive, sources[ id ], input.offset, uv.array );
2503
- uv.stride = sources[ id ].stride;
2504
- break;
2505
-
2506
- case 'TEXCOORD1':
2507
- buildGeometryData( primitive, sources[ id ], input.offset, uv1.array );
2508
- uv.stride = sources[ id ].stride;
2509
- break;
2510
-
2511
- default:
2512
- console.warn( 'THREE.ColladaLoader: Semantic "%s" not handled in geometry build process.', key );
2513
-
2514
- }
2515
-
2516
- }
2517
-
2518
- break;
2519
-
2520
- case 'NORMAL':
2521
- buildGeometryData( primitive, sources[ input.id ], input.offset, normal.array );
2522
- normal.stride = sources[ input.id ].stride;
2523
- break;
2524
-
2525
- case 'COLOR':
2526
- buildGeometryData( primitive, sources[ input.id ], input.offset, color.array, true );
2527
- color.stride = sources[ input.id ].stride;
2528
- break;
2529
-
2530
- case 'TEXCOORD':
2531
- buildGeometryData( primitive, sources[ input.id ], input.offset, uv.array );
2532
- uv.stride = sources[ input.id ].stride;
2533
- break;
2534
-
2535
- case 'TEXCOORD1':
2536
- buildGeometryData( primitive, sources[ input.id ], input.offset, uv1.array );
2537
- uv1.stride = sources[ input.id ].stride;
2538
- break;
2539
-
2540
- }
2541
-
2542
- }
2543
-
2544
- }
2545
-
2546
- // build geometry
2547
-
2548
- if ( position.array.length > 0 ) geometry.setAttribute( 'position', new Float32BufferAttribute( position.array, position.stride ) );
2549
- if ( normal.array.length > 0 ) geometry.setAttribute( 'normal', new Float32BufferAttribute( normal.array, normal.stride ) );
2550
- if ( color.array.length > 0 ) geometry.setAttribute( 'color', new Float32BufferAttribute( color.array, color.stride ) );
2551
- if ( uv.array.length > 0 ) geometry.setAttribute( 'uv', new Float32BufferAttribute( uv.array, uv.stride ) );
2552
- if ( uv1.array.length > 0 ) geometry.setAttribute( 'uv1', new Float32BufferAttribute( uv1.array, uv1.stride ) );
2553
-
2554
- if ( skinIndex.array.length > 0 ) geometry.setAttribute( 'skinIndex', new Float32BufferAttribute( skinIndex.array, skinIndex.stride ) );
2555
- if ( skinWeight.array.length > 0 ) geometry.setAttribute( 'skinWeight', new Float32BufferAttribute( skinWeight.array, skinWeight.stride ) );
2556
-
2557
- build.data = geometry;
2558
- build.type = primitives[ 0 ].type;
2559
- build.materialKeys = materialKeys;
2560
-
2561
- return build;
2562
-
2563
- }
2564
-
2565
- function buildGeometryData( primitive, source, offset, array, isColor = false ) {
2566
-
2567
- const indices = primitive.p;
2568
- const stride = primitive.stride;
2569
- const vcount = primitive.vcount;
2570
-
2571
- function pushVector( i ) {
2572
-
2573
- let index = indices[ i + offset ] * sourceStride;
2574
- const length = index + sourceStride;
2575
-
2576
- for ( ; index < length; index ++ ) {
2577
-
2578
- array.push( sourceArray[ index ] );
2579
-
2580
- }
2581
-
2582
- if ( isColor ) {
2583
-
2584
- // convert the vertex colors from srgb to linear if present
2585
- const startIndex = array.length - sourceStride - 1;
2586
- tempColor.setRGB(
2587
- array[ startIndex + 0 ],
2588
- array[ startIndex + 1 ],
2589
- array[ startIndex + 2 ],
2590
- SRGBColorSpace
2591
- );
2592
-
2593
- array[ startIndex + 0 ] = tempColor.r;
2594
- array[ startIndex + 1 ] = tempColor.g;
2595
- array[ startIndex + 2 ] = tempColor.b;
2596
-
2597
- }
2598
-
2599
- }
2600
-
2601
- const sourceArray = source.array;
2602
- const sourceStride = source.stride;
2603
-
2604
- if ( primitive.vcount !== undefined ) {
2605
-
2606
- let index = 0;
2607
-
2608
- for ( let i = 0, l = vcount.length; i < l; i ++ ) {
2609
-
2610
- const count = vcount[ i ];
2611
-
2612
- if ( count === 4 ) {
2613
-
2614
- const a = index + stride * 0;
2615
- const b = index + stride * 1;
2616
- const c = index + stride * 2;
2617
- const d = index + stride * 3;
2618
-
2619
- pushVector( a ); pushVector( b ); pushVector( d );
2620
- pushVector( b ); pushVector( c ); pushVector( d );
2621
-
2622
- } else if ( count === 3 ) {
2623
-
2624
- const a = index + stride * 0;
2625
- const b = index + stride * 1;
2626
- const c = index + stride * 2;
2627
-
2628
- pushVector( a ); pushVector( b ); pushVector( c );
2629
-
2630
- } else if ( count > 4 ) {
2631
-
2632
- for ( let k = 1, kl = ( count - 2 ); k <= kl; k ++ ) {
2633
-
2634
- const a = index + stride * 0;
2635
- const b = index + stride * k;
2636
- const c = index + stride * ( k + 1 );
2637
-
2638
- pushVector( a ); pushVector( b ); pushVector( c );
2639
-
2640
- }
2641
-
2642
- }
2643
-
2644
- index += stride * count;
2645
-
2646
- }
2647
-
2648
- } else {
2649
-
2650
- for ( let i = 0, l = indices.length; i < l; i += stride ) {
2651
-
2652
- pushVector( i );
2653
-
2654
- }
2655
-
2656
- }
2657
-
2658
- }
2659
-
2660
- function getGeometry( id ) {
2661
-
2662
- return getBuild( library.geometries[ id ], buildGeometry );
2663
-
2664
- }
2665
-
2666
- // kinematics
2667
-
2668
- function parseKinematicsModel( xml ) {
2669
-
2670
- const data = {
2671
- name: xml.getAttribute( 'name' ) || '',
2672
- joints: {},
2673
- links: []
2674
- };
2675
-
2676
- for ( let i = 0; i < xml.childNodes.length; i ++ ) {
2677
-
2678
- const child = xml.childNodes[ i ];
2679
-
2680
- if ( child.nodeType !== 1 ) continue;
2681
-
2682
- switch ( child.nodeName ) {
2683
-
2684
- case 'technique_common':
2685
- parseKinematicsTechniqueCommon( child, data );
2686
- break;
2687
-
2688
- }
2689
-
2690
- }
2691
-
2692
- library.kinematicsModels[ xml.getAttribute( 'id' ) ] = data;
2693
-
2694
- }
2695
-
2696
- function buildKinematicsModel( data ) {
2697
-
2698
- if ( data.build !== undefined ) return data.build;
2699
-
2700
- return data;
2701
-
2702
- }
2703
-
2704
- function getKinematicsModel( id ) {
2705
-
2706
- return getBuild( library.kinematicsModels[ id ], buildKinematicsModel );
2707
-
2708
- }
2709
-
2710
- function parseKinematicsTechniqueCommon( xml, data ) {
2711
-
2712
- for ( let i = 0; i < xml.childNodes.length; i ++ ) {
2713
-
2714
- const child = xml.childNodes[ i ];
2715
-
2716
- if ( child.nodeType !== 1 ) continue;
2717
-
2718
- switch ( child.nodeName ) {
2719
-
2720
- case 'joint':
2721
- data.joints[ child.getAttribute( 'sid' ) ] = parseKinematicsJoint( child );
2722
- break;
2723
-
2724
- case 'link':
2725
- data.links.push( parseKinematicsLink( child ) );
2726
- break;
2727
-
2728
- }
2729
-
2730
- }
2731
-
2732
- }
2733
-
2734
- function parseKinematicsJoint( xml ) {
2735
-
2736
- let data;
2737
-
2738
- for ( let i = 0; i < xml.childNodes.length; i ++ ) {
2739
-
2740
- const child = xml.childNodes[ i ];
2741
-
2742
- if ( child.nodeType !== 1 ) continue;
2743
-
2744
- switch ( child.nodeName ) {
2745
-
2746
- case 'prismatic':
2747
- case 'revolute':
2748
- data = parseKinematicsJointParameter( child );
2749
- break;
2750
-
2751
- }
2752
-
2753
- }
2754
-
2755
- return data;
2756
-
2757
- }
2758
-
2759
- function parseKinematicsJointParameter( xml ) {
2760
-
2761
- const data = {
2762
- sid: xml.getAttribute( 'sid' ),
2763
- name: xml.getAttribute( 'name' ) || '',
2764
- axis: new Vector3(),
2765
- limits: {
2766
- min: 0,
2767
- max: 0
2768
- },
2769
- type: xml.nodeName,
2770
- static: false,
2771
- zeroPosition: 0,
2772
- middlePosition: 0
2773
- };
2774
-
2775
- for ( let i = 0; i < xml.childNodes.length; i ++ ) {
2776
-
2777
- const child = xml.childNodes[ i ];
2778
-
2779
- if ( child.nodeType !== 1 ) continue;
2780
-
2781
- switch ( child.nodeName ) {
2782
-
2783
- case 'axis':
2784
- const array = parseFloats( child.textContent );
2785
- data.axis.fromArray( array );
2786
- break;
2787
- case 'limits':
2788
- const max = child.getElementsByTagName( 'max' )[ 0 ];
2789
- const min = child.getElementsByTagName( 'min' )[ 0 ];
2790
-
2791
- data.limits.max = parseFloat( max.textContent );
2792
- data.limits.min = parseFloat( min.textContent );
2793
- break;
2794
-
2795
- }
2796
-
2797
- }
2798
-
2799
- // if min is equal to or greater than max, consider the joint static
2800
-
2801
- if ( data.limits.min >= data.limits.max ) {
2802
-
2803
- data.static = true;
2804
-
2805
- }
2806
-
2807
- // calculate middle position
2808
-
2809
- data.middlePosition = ( data.limits.min + data.limits.max ) / 2.0;
2810
-
2811
- return data;
2812
-
2813
- }
2814
-
2815
- function parseKinematicsLink( xml ) {
2816
-
2817
- const data = {
2818
- sid: xml.getAttribute( 'sid' ),
2819
- name: xml.getAttribute( 'name' ) || '',
2820
- attachments: [],
2821
- transforms: []
2822
- };
2823
-
2824
- for ( let i = 0; i < xml.childNodes.length; i ++ ) {
2825
-
2826
- const child = xml.childNodes[ i ];
2827
-
2828
- if ( child.nodeType !== 1 ) continue;
2829
-
2830
- switch ( child.nodeName ) {
2831
-
2832
- case 'attachment_full':
2833
- data.attachments.push( parseKinematicsAttachment( child ) );
2834
- break;
2835
-
2836
- case 'matrix':
2837
- case 'translate':
2838
- case 'rotate':
2839
- data.transforms.push( parseKinematicsTransform( child ) );
2840
- break;
2841
-
2842
- }
2843
-
2844
- }
2845
-
2846
- return data;
2847
-
2848
- }
2849
-
2850
- function parseKinematicsAttachment( xml ) {
2851
-
2852
- const data = {
2853
- joint: xml.getAttribute( 'joint' ).split( '/' ).pop(),
2854
- transforms: [],
2855
- links: []
2856
- };
2857
-
2858
- for ( let i = 0; i < xml.childNodes.length; i ++ ) {
2859
-
2860
- const child = xml.childNodes[ i ];
2861
-
2862
- if ( child.nodeType !== 1 ) continue;
2863
-
2864
- switch ( child.nodeName ) {
2865
-
2866
- case 'link':
2867
- data.links.push( parseKinematicsLink( child ) );
2868
- break;
2869
-
2870
- case 'matrix':
2871
- case 'translate':
2872
- case 'rotate':
2873
- data.transforms.push( parseKinematicsTransform( child ) );
2874
- break;
2875
-
2876
- }
2877
-
2878
- }
2879
-
2880
- return data;
2881
-
2882
- }
2883
-
2884
- function parseKinematicsTransform( xml ) {
2885
-
2886
- const data = {
2887
- type: xml.nodeName
2888
- };
2889
-
2890
- const array = parseFloats( xml.textContent );
2891
-
2892
- switch ( data.type ) {
2893
-
2894
- case 'matrix':
2895
- data.obj = new Matrix4();
2896
- data.obj.fromArray( array ).transpose();
2897
- break;
2898
-
2899
- case 'translate':
2900
- data.obj = new Vector3();
2901
- data.obj.fromArray( array );
2902
- break;
2903
-
2904
- case 'rotate':
2905
- data.obj = new Vector3();
2906
- data.obj.fromArray( array );
2907
- data.angle = MathUtils.degToRad( array[ 3 ] );
2908
- break;
2909
-
2910
- }
2911
-
2912
- return data;
2913
-
2914
- }
2915
-
2916
- // physics
2917
-
2918
- function parsePhysicsModel( xml ) {
2919
-
2920
- const data = {
2921
- name: xml.getAttribute( 'name' ) || '',
2922
- rigidBodies: {}
2923
- };
2924
-
2925
- for ( let i = 0; i < xml.childNodes.length; i ++ ) {
2926
-
2927
- const child = xml.childNodes[ i ];
2928
-
2929
- if ( child.nodeType !== 1 ) continue;
2930
-
2931
- switch ( child.nodeName ) {
2932
-
2933
- case 'rigid_body':
2934
- data.rigidBodies[ child.getAttribute( 'name' ) ] = {};
2935
- parsePhysicsRigidBody( child, data.rigidBodies[ child.getAttribute( 'name' ) ] );
2936
- break;
2937
-
2938
- }
2939
-
2940
- }
2941
-
2942
- library.physicsModels[ xml.getAttribute( 'id' ) ] = data;
2943
-
2944
- }
2945
-
2946
- function parsePhysicsRigidBody( xml, data ) {
2947
-
2948
- for ( let i = 0; i < xml.childNodes.length; i ++ ) {
2949
-
2950
- const child = xml.childNodes[ i ];
2951
-
2952
- if ( child.nodeType !== 1 ) continue;
2953
-
2954
- switch ( child.nodeName ) {
2955
-
2956
- case 'technique_common':
2957
- parsePhysicsTechniqueCommon( child, data );
2958
- break;
2959
-
2960
- }
2961
-
2962
- }
2963
-
2964
- }
2965
-
2966
- function parsePhysicsTechniqueCommon( xml, data ) {
2967
-
2968
- for ( let i = 0; i < xml.childNodes.length; i ++ ) {
2969
-
2970
- const child = xml.childNodes[ i ];
2971
-
2972
- if ( child.nodeType !== 1 ) continue;
2973
-
2974
- switch ( child.nodeName ) {
2975
-
2976
- case 'inertia':
2977
- data.inertia = parseFloats( child.textContent );
2978
- break;
2979
-
2980
- case 'mass':
2981
- data.mass = parseFloats( child.textContent )[ 0 ];
2982
- break;
2983
-
2984
- }
2985
-
2986
- }
2987
-
2988
- }
2989
-
2990
- // scene
2991
-
2992
- function parseKinematicsScene( xml ) {
2993
-
2994
- const data = {
2995
- bindJointAxis: []
2996
- };
2997
-
2998
- for ( let i = 0; i < xml.childNodes.length; i ++ ) {
2999
-
3000
- const child = xml.childNodes[ i ];
3001
-
3002
- if ( child.nodeType !== 1 ) continue;
3003
-
3004
- switch ( child.nodeName ) {
3005
-
3006
- case 'bind_joint_axis':
3007
- data.bindJointAxis.push( parseKinematicsBindJointAxis( child ) );
3008
- break;
3009
-
3010
- }
3011
-
3012
- }
3013
-
3014
- library.kinematicsScenes[ parseId( xml.getAttribute( 'url' ) ) ] = data;
3015
-
3016
- }
3017
-
3018
- function parseKinematicsBindJointAxis( xml ) {
3019
-
3020
- const data = {
3021
- target: xml.getAttribute( 'target' ).split( '/' ).pop()
3022
- };
3023
-
3024
- for ( let i = 0; i < xml.childNodes.length; i ++ ) {
3025
-
3026
- const child = xml.childNodes[ i ];
3027
-
3028
- if ( child.nodeType !== 1 ) continue;
3029
-
3030
- switch ( child.nodeName ) {
3031
-
3032
- case 'axis':
3033
- const param = child.getElementsByTagName( 'param' )[ 0 ];
3034
- data.axis = param.textContent;
3035
- const tmpJointIndex = data.axis.split( 'inst_' ).pop().split( 'axis' )[ 0 ];
3036
- data.jointIndex = tmpJointIndex.substring( 0, tmpJointIndex.length - 1 );
3037
- break;
3038
-
3039
- }
3040
-
3041
- }
3042
-
3043
- return data;
3044
-
3045
- }
3046
-
3047
- function buildKinematicsScene( data ) {
3048
-
3049
- if ( data.build !== undefined ) return data.build;
3050
-
3051
- return data;
3052
-
3053
- }
3054
-
3055
- function getKinematicsScene( id ) {
3056
-
3057
- return getBuild( library.kinematicsScenes[ id ], buildKinematicsScene );
3058
-
3059
- }
3060
-
3061
- function setupKinematics() {
3062
-
3063
- const kinematicsModelId = Object.keys( library.kinematicsModels )[ 0 ];
3064
- const kinematicsSceneId = Object.keys( library.kinematicsScenes )[ 0 ];
3065
- const visualSceneId = Object.keys( library.visualScenes )[ 0 ];
3066
-
3067
- if ( kinematicsModelId === undefined || kinematicsSceneId === undefined ) return;
3068
-
3069
- const kinematicsModel = getKinematicsModel( kinematicsModelId );
3070
- const kinematicsScene = getKinematicsScene( kinematicsSceneId );
3071
- const visualScene = getVisualScene( visualSceneId );
3072
-
3073
- const bindJointAxis = kinematicsScene.bindJointAxis;
3074
- const jointMap = {};
3075
-
3076
- for ( let i = 0, l = bindJointAxis.length; i < l; i ++ ) {
3077
-
3078
- const axis = bindJointAxis[ i ];
3079
-
3080
- // the result of the following query is an element of type 'translate', 'rotate','scale' or 'matrix'
3081
-
3082
- const targetElement = collada.querySelector( '[sid="' + axis.target + '"]' );
3083
-
3084
- if ( targetElement ) {
3085
-
3086
- // get the parent of the transform element
3087
-
3088
- const parentVisualElement = targetElement.parentElement;
3089
-
3090
- // connect the joint of the kinematics model with the element in the visual scene
3091
-
3092
- connect( axis.jointIndex, parentVisualElement );
3093
-
3094
- }
3095
-
3096
- }
3097
-
3098
- function connect( jointIndex, visualElement ) {
3099
-
3100
- const visualElementName = visualElement.getAttribute( 'name' );
3101
- const joint = kinematicsModel.joints[ jointIndex ];
3102
-
3103
- visualScene.traverse( function ( object ) {
3104
-
3105
- if ( object.name === visualElementName ) {
3106
-
3107
- jointMap[ jointIndex ] = {
3108
- object: object,
3109
- transforms: buildTransformList( visualElement ),
3110
- joint: joint,
3111
- position: joint.zeroPosition
3112
- };
3113
-
3114
- }
3115
-
3116
- } );
3117
-
3118
- }
3119
-
3120
- const m0 = new Matrix4();
3121
-
3122
- kinematics = {
3123
-
3124
- joints: kinematicsModel && kinematicsModel.joints,
3125
-
3126
- getJointValue: function ( jointIndex ) {
3127
-
3128
- const jointData = jointMap[ jointIndex ];
3129
-
3130
- if ( jointData ) {
3131
-
3132
- return jointData.position;
3133
-
3134
- } else {
3135
-
3136
- console.warn( 'THREE.ColladaLoader: Joint ' + jointIndex + ' doesn\'t exist.' );
3137
-
3138
- }
3139
-
3140
- },
3141
-
3142
- setJointValue: function ( jointIndex, value ) {
3143
-
3144
- const jointData = jointMap[ jointIndex ];
3145
-
3146
- if ( jointData ) {
3147
-
3148
- const joint = jointData.joint;
3149
-
3150
- if ( value > joint.limits.max || value < joint.limits.min ) {
3151
-
3152
- console.warn( 'THREE.ColladaLoader: Joint ' + jointIndex + ' value ' + value + ' outside of limits (min: ' + joint.limits.min + ', max: ' + joint.limits.max + ').' );
3153
-
3154
- } else if ( joint.static ) {
3155
-
3156
- console.warn( 'THREE.ColladaLoader: Joint ' + jointIndex + ' is static.' );
3157
-
3158
- } else {
3159
-
3160
- const object = jointData.object;
3161
- const axis = joint.axis;
3162
- const transforms = jointData.transforms;
3163
-
3164
- matrix.identity();
3165
-
3166
- // each update, we have to apply all transforms in the correct order
3167
-
3168
- for ( let i = 0; i < transforms.length; i ++ ) {
3169
-
3170
- const transform = transforms[ i ];
3171
-
3172
- // if there is a connection of the transform node with a joint, apply the joint value
3173
-
3174
- if ( transform.sid && transform.sid.indexOf( jointIndex ) !== - 1 ) {
3175
-
3176
- switch ( joint.type ) {
3177
-
3178
- case 'revolute':
3179
- matrix.multiply( m0.makeRotationAxis( axis, MathUtils.degToRad( value ) ) );
3180
- break;
3181
-
3182
- case 'prismatic':
3183
- matrix.multiply( m0.makeTranslation( axis.x * value, axis.y * value, axis.z * value ) );
3184
- break;
3185
-
3186
- default:
3187
- console.warn( 'THREE.ColladaLoader: Unknown joint type: ' + joint.type );
3188
- break;
3189
-
3190
- }
3191
-
3192
- } else {
3193
-
3194
- switch ( transform.type ) {
3195
-
3196
- case 'matrix':
3197
- matrix.multiply( transform.obj );
3198
- break;
3199
-
3200
- case 'translate':
3201
- matrix.multiply( m0.makeTranslation( transform.obj.x, transform.obj.y, transform.obj.z ) );
3202
- break;
3203
-
3204
- case 'scale':
3205
- matrix.scale( transform.obj );
3206
- break;
3207
-
3208
- case 'rotate':
3209
- matrix.multiply( m0.makeRotationAxis( transform.obj, transform.angle ) );
3210
- break;
3211
-
3212
- }
3213
-
3214
- }
3215
-
3216
- }
3217
-
3218
- object.matrix.copy( matrix );
3219
- object.matrix.decompose( object.position, object.quaternion, object.scale );
3220
-
3221
- jointMap[ jointIndex ].position = value;
3222
-
3223
- }
3224
-
3225
- } else {
3226
-
3227
- console.log( 'THREE.ColladaLoader: ' + jointIndex + ' does not exist.' );
3228
-
3229
- }
3230
-
3231
- }
3232
-
3233
- };
3234
-
3235
- }
3236
-
3237
- function buildTransformList( node ) {
3238
-
3239
- const transforms = [];
3240
-
3241
- const xml = collada.querySelector( '[id="' + node.id + '"]' );
3242
-
3243
- for ( let i = 0; i < xml.childNodes.length; i ++ ) {
3244
-
3245
- const child = xml.childNodes[ i ];
3246
-
3247
- if ( child.nodeType !== 1 ) continue;
3248
-
3249
- let array, vector;
3250
-
3251
- switch ( child.nodeName ) {
3252
-
3253
- case 'matrix':
3254
- array = parseFloats( child.textContent );
3255
- const matrix = new Matrix4().fromArray( array ).transpose();
3256
- transforms.push( {
3257
- sid: child.getAttribute( 'sid' ),
3258
- type: child.nodeName,
3259
- obj: matrix
3260
- } );
3261
- break;
3262
-
3263
- case 'translate':
3264
- case 'scale':
3265
- array = parseFloats( child.textContent );
3266
- vector = new Vector3().fromArray( array );
3267
- transforms.push( {
3268
- sid: child.getAttribute( 'sid' ),
3269
- type: child.nodeName,
3270
- obj: vector
3271
- } );
3272
- break;
3273
-
3274
- case 'rotate':
3275
- array = parseFloats( child.textContent );
3276
- vector = new Vector3().fromArray( array );
3277
- const angle = MathUtils.degToRad( array[ 3 ] );
3278
- transforms.push( {
3279
- sid: child.getAttribute( 'sid' ),
3280
- type: child.nodeName,
3281
- obj: vector,
3282
- angle: angle
3283
- } );
3284
- break;
3285
-
3286
- }
3287
-
3288
- }
3289
-
3290
- return transforms;
3291
-
3292
- }
3293
-
3294
- // nodes
3295
-
3296
- function prepareNodes( xml ) {
3297
-
3298
- const elements = xml.getElementsByTagName( 'node' );
3299
-
3300
- // ensure all node elements have id attributes
3301
-
3302
- for ( let i = 0; i < elements.length; i ++ ) {
3303
-
3304
- const element = elements[ i ];
3305
-
3306
- if ( element.hasAttribute( 'id' ) === false ) {
3307
-
3308
- element.setAttribute( 'id', generateId() );
3309
-
3310
- }
3311
-
3312
- }
3313
-
3314
- }
3315
-
3316
- const matrix = new Matrix4();
3317
- const vector = new Vector3();
3318
-
3319
- function parseNode( xml ) {
3320
-
3321
- const data = {
3322
- name: xml.getAttribute( 'name' ) || '',
3323
- type: xml.getAttribute( 'type' ),
3324
- id: xml.getAttribute( 'id' ),
3325
- sid: xml.getAttribute( 'sid' ),
3326
- matrix: new Matrix4(),
3327
- nodes: [],
3328
- instanceCameras: [],
3329
- instanceControllers: [],
3330
- instanceLights: [],
3331
- instanceGeometries: [],
3332
- instanceNodes: [],
3333
- transforms: {}
3334
- };
3335
-
3336
- for ( let i = 0; i < xml.childNodes.length; i ++ ) {
3337
-
3338
- const child = xml.childNodes[ i ];
3339
-
3340
- if ( child.nodeType !== 1 ) continue;
3341
-
3342
- let array;
3343
-
3344
- switch ( child.nodeName ) {
3345
-
3346
- case 'node':
3347
- data.nodes.push( child.getAttribute( 'id' ) );
3348
- parseNode( child );
3349
- break;
3350
-
3351
- case 'instance_camera':
3352
- data.instanceCameras.push( parseId( child.getAttribute( 'url' ) ) );
3353
- break;
3354
-
3355
- case 'instance_controller':
3356
- data.instanceControllers.push( parseNodeInstance( child ) );
3357
- break;
3358
-
3359
- case 'instance_light':
3360
- data.instanceLights.push( parseId( child.getAttribute( 'url' ) ) );
3361
- break;
3362
-
3363
- case 'instance_geometry':
3364
- data.instanceGeometries.push( parseNodeInstance( child ) );
3365
- break;
3366
-
3367
- case 'instance_node':
3368
- data.instanceNodes.push( parseId( child.getAttribute( 'url' ) ) );
3369
- break;
3370
-
3371
- case 'matrix':
3372
- array = parseFloats( child.textContent );
3373
- data.matrix.multiply( matrix.fromArray( array ).transpose() );
3374
- data.transforms[ child.getAttribute( 'sid' ) ] = child.nodeName;
3375
- break;
3376
-
3377
- case 'translate':
3378
- array = parseFloats( child.textContent );
3379
- vector.fromArray( array );
3380
- data.matrix.multiply( matrix.makeTranslation( vector.x, vector.y, vector.z ) );
3381
- data.transforms[ child.getAttribute( 'sid' ) ] = child.nodeName;
3382
- break;
3383
-
3384
- case 'rotate':
3385
- array = parseFloats( child.textContent );
3386
- const angle = MathUtils.degToRad( array[ 3 ] );
3387
- data.matrix.multiply( matrix.makeRotationAxis( vector.fromArray( array ), angle ) );
3388
- data.transforms[ child.getAttribute( 'sid' ) ] = child.nodeName;
3389
- break;
3390
-
3391
- case 'scale':
3392
- array = parseFloats( child.textContent );
3393
- data.matrix.scale( vector.fromArray( array ) );
3394
- data.transforms[ child.getAttribute( 'sid' ) ] = child.nodeName;
3395
- break;
3396
-
3397
- case 'extra':
3398
- break;
3399
-
3400
- default:
3401
- console.log( child );
3402
-
3403
- }
3404
-
3405
- }
3406
-
3407
- if ( hasNode( data.id ) ) {
3408
-
3409
- console.warn( 'THREE.ColladaLoader: There is already a node with ID %s. Exclude current node from further processing.', data.id );
3410
-
3411
- } else {
3412
-
3413
- library.nodes[ data.id ] = data;
3414
-
3415
- }
3416
-
3417
- return data;
3418
-
3419
- }
3420
-
3421
- function parseNodeInstance( xml ) {
3422
-
3423
- const data = {
3424
- id: parseId( xml.getAttribute( 'url' ) ),
3425
- materials: {},
3426
- skeletons: []
3427
- };
3428
-
3429
- for ( let i = 0; i < xml.childNodes.length; i ++ ) {
3430
-
3431
- const child = xml.childNodes[ i ];
3432
-
3433
- switch ( child.nodeName ) {
3434
-
3435
- case 'bind_material':
3436
- const instances = child.getElementsByTagName( 'instance_material' );
3437
-
3438
- for ( let j = 0; j < instances.length; j ++ ) {
3439
-
3440
- const instance = instances[ j ];
3441
- const symbol = instance.getAttribute( 'symbol' );
3442
- const target = instance.getAttribute( 'target' );
3443
-
3444
- data.materials[ symbol ] = parseId( target );
3445
-
3446
- }
3447
-
3448
- break;
3449
-
3450
- case 'skeleton':
3451
- data.skeletons.push( parseId( child.textContent ) );
3452
- break;
3453
-
3454
- default:
3455
- break;
3456
-
3457
- }
3458
-
3459
- }
3460
-
3461
- return data;
3462
-
3463
- }
3464
-
3465
- function buildSkeleton( skeletons, joints ) {
3466
-
3467
- const boneData = [];
3468
- const sortedBoneData = [];
3469
-
3470
- let i, j, data;
3471
-
3472
- // a skeleton can have multiple root bones. collada expresses this
3473
- // situation with multiple "skeleton" tags per controller instance
3474
-
3475
- for ( i = 0; i < skeletons.length; i ++ ) {
3476
-
3477
- const skeleton = skeletons[ i ];
3478
-
3479
- let root;
3480
-
3481
- if ( hasNode( skeleton ) ) {
3482
-
3483
- root = getNode( skeleton );
3484
- buildBoneHierarchy( root, joints, boneData );
3485
-
3486
- } else if ( hasVisualScene( skeleton ) ) {
3487
-
3488
- // handle case where the skeleton refers to the visual scene (#13335)
3489
-
3490
- const visualScene = library.visualScenes[ skeleton ];
3491
- const children = visualScene.children;
3492
-
3493
- for ( let j = 0; j < children.length; j ++ ) {
3494
-
3495
- const child = children[ j ];
3496
-
3497
- if ( child.type === 'JOINT' ) {
3498
-
3499
- const root = getNode( child.id );
3500
- buildBoneHierarchy( root, joints, boneData );
3501
-
3502
- }
3503
-
3504
- }
3505
-
3506
- } else {
3507
-
3508
- console.error( 'THREE.ColladaLoader: Unable to find root bone of skeleton with ID:', skeleton );
3509
-
3510
- }
3511
-
3512
- }
3513
-
3514
- // sort bone data (the order is defined in the corresponding controller)
3515
-
3516
- for ( i = 0; i < joints.length; i ++ ) {
3517
-
3518
- for ( j = 0; j < boneData.length; j ++ ) {
3519
-
3520
- data = boneData[ j ];
3521
-
3522
- if ( data.bone.name === joints[ i ].name ) {
3523
-
3524
- sortedBoneData[ i ] = data;
3525
- data.processed = true;
3526
- break;
3527
-
3528
- }
3529
-
3530
- }
3531
-
3532
- }
3533
-
3534
- // add unprocessed bone data at the end of the list
3535
-
3536
- for ( i = 0; i < boneData.length; i ++ ) {
3537
-
3538
- data = boneData[ i ];
3539
-
3540
- if ( data.processed === false ) {
3541
-
3542
- sortedBoneData.push( data );
3543
- data.processed = true;
3544
-
3545
- }
3546
-
3547
- }
3548
-
3549
- // setup arrays for skeleton creation
3550
-
3551
- const bones = [];
3552
- const boneInverses = [];
3553
-
3554
- for ( i = 0; i < sortedBoneData.length; i ++ ) {
3555
-
3556
- data = sortedBoneData[ i ];
3557
-
3558
- bones.push( data.bone );
3559
- boneInverses.push( data.boneInverse );
3560
-
3561
- }
3562
-
3563
- return new Skeleton( bones, boneInverses );
3564
-
3565
- }
3566
-
3567
- function buildBoneHierarchy( root, joints, boneData ) {
3568
-
3569
- // setup bone data from visual scene
3570
-
3571
- root.traverse( function ( object ) {
3572
-
3573
- if ( object.isBone === true ) {
3574
-
3575
- let boneInverse;
3576
-
3577
- // retrieve the boneInverse from the controller data
3578
-
3579
- for ( let i = 0; i < joints.length; i ++ ) {
3580
-
3581
- const joint = joints[ i ];
3582
-
3583
- if ( joint.name === object.name ) {
3584
-
3585
- boneInverse = joint.boneInverse;
3586
- break;
3587
-
3588
- }
3589
-
3590
- }
3591
-
3592
- if ( boneInverse === undefined ) {
3593
-
3594
- // Unfortunately, there can be joints in the visual scene that are not part of the
3595
- // corresponding controller. In this case, we have to create a dummy boneInverse matrix
3596
- // for the respective bone. This bone won't affect any vertices, because there are no skin indices
3597
- // and weights defined for it. But we still have to add the bone to the sorted bone list in order to
3598
- // ensure a correct animation of the model.
3599
-
3600
- boneInverse = new Matrix4();
3601
-
3602
- }
3603
-
3604
- boneData.push( { bone: object, boneInverse: boneInverse, processed: false } );
3605
-
3606
- }
3607
-
3608
- } );
3609
-
3610
- }
3611
-
3612
- function buildNode( data ) {
3613
-
3614
- const objects = [];
3615
-
3616
- const matrix = data.matrix;
3617
- const nodes = data.nodes;
3618
- const type = data.type;
3619
- const instanceCameras = data.instanceCameras;
3620
- const instanceControllers = data.instanceControllers;
3621
- const instanceLights = data.instanceLights;
3622
- const instanceGeometries = data.instanceGeometries;
3623
- const instanceNodes = data.instanceNodes;
3624
-
3625
- // nodes
3626
-
3627
- for ( let i = 0, l = nodes.length; i < l; i ++ ) {
3628
-
3629
- objects.push( getNode( nodes[ i ] ) );
3630
-
3631
- }
3632
-
3633
- // instance cameras
3634
-
3635
- for ( let i = 0, l = instanceCameras.length; i < l; i ++ ) {
3636
-
3637
- const instanceCamera = getCamera( instanceCameras[ i ] );
3638
-
3639
- if ( instanceCamera !== null ) {
3640
-
3641
- objects.push( instanceCamera.clone() );
3642
-
3643
- }
3644
-
3645
- }
3646
-
3647
- // instance controllers
3648
-
3649
- for ( let i = 0, l = instanceControllers.length; i < l; i ++ ) {
3650
-
3651
- const instance = instanceControllers[ i ];
3652
- const controller = getController( instance.id );
3653
- const geometries = getGeometry( controller.id );
3654
- const newObjects = buildObjects( geometries, instance.materials );
3655
-
3656
- const skeletons = instance.skeletons;
3657
- const joints = controller.skin.joints;
3658
-
3659
- const skeleton = buildSkeleton( skeletons, joints );
3660
-
3661
- for ( let j = 0, jl = newObjects.length; j < jl; j ++ ) {
3662
-
3663
- const object = newObjects[ j ];
3664
-
3665
- if ( object.isSkinnedMesh ) {
3666
-
3667
- object.bind( skeleton, controller.skin.bindMatrix );
3668
- object.normalizeSkinWeights();
3669
-
3670
- }
3671
-
3672
- objects.push( object );
3673
-
3674
- }
3675
-
3676
- }
3677
-
3678
- // instance lights
3679
-
3680
- for ( let i = 0, l = instanceLights.length; i < l; i ++ ) {
3681
-
3682
- const instanceLight = getLight( instanceLights[ i ] );
3683
-
3684
- if ( instanceLight !== null ) {
3685
-
3686
- objects.push( instanceLight.clone() );
3687
-
3688
- }
3689
-
3690
- }
3691
-
3692
- // instance geometries
3693
-
3694
- for ( let i = 0, l = instanceGeometries.length; i < l; i ++ ) {
3695
-
3696
- const instance = instanceGeometries[ i ];
3697
-
3698
- // a single geometry instance in collada can lead to multiple object3Ds.
3699
- // this is the case when primitives are combined like triangles and lines
3700
-
3701
- const geometries = getGeometry( instance.id );
3702
- const newObjects = buildObjects( geometries, instance.materials );
3703
-
3704
- for ( let j = 0, jl = newObjects.length; j < jl; j ++ ) {
3705
-
3706
- objects.push( newObjects[ j ] );
3707
-
3708
- }
3709
-
3710
- }
3711
-
3712
- // instance nodes
3713
-
3714
- for ( let i = 0, l = instanceNodes.length; i < l; i ++ ) {
3715
-
3716
- objects.push( getNode( instanceNodes[ i ] ).clone() );
3717
-
3718
- }
3719
-
3720
- let object;
3721
-
3722
- if ( nodes.length === 0 && objects.length === 1 ) {
3723
-
3724
- object = objects[ 0 ];
3725
-
3726
- } else {
3727
-
3728
- object = ( type === 'JOINT' ) ? new Bone() : new Group();
3729
-
3730
- for ( let i = 0; i < objects.length; i ++ ) {
3731
-
3732
- object.add( objects[ i ] );
3733
-
3734
- }
3735
-
3736
- }
3737
-
3738
- object.name = ( type === 'JOINT' ) ? data.sid : data.name;
3739
- object.matrix.copy( matrix );
3740
- object.matrix.decompose( object.position, object.quaternion, object.scale );
3741
-
3742
- return object;
3743
-
3744
- }
3745
-
3746
- const fallbackMaterial = new MeshBasicMaterial( {
3747
- name: Loader.DEFAULT_MATERIAL_NAME,
3748
- color: 0xff00ff
3749
- } );
3750
-
3751
- function resolveMaterialBinding( keys, instanceMaterials ) {
3752
-
3753
- const materials = [];
3754
-
3755
- for ( let i = 0, l = keys.length; i < l; i ++ ) {
3756
-
3757
- const id = instanceMaterials[ keys[ i ] ];
3758
-
3759
- if ( id === undefined ) {
3760
-
3761
- console.warn( 'THREE.ColladaLoader: Material with key %s not found. Apply fallback material.', keys[ i ] );
3762
- materials.push( fallbackMaterial );
3763
-
3764
- } else {
3765
-
3766
- materials.push( getMaterial( id ) );
3767
-
3768
- }
3769
-
3770
- }
3771
-
3772
- return materials;
3773
-
3774
- }
3775
-
3776
- function buildObjects( geometries, instanceMaterials ) {
3777
-
3778
- const objects = [];
3779
-
3780
- for ( const type in geometries ) {
3781
-
3782
- const geometry = geometries[ type ];
3783
-
3784
- const materials = resolveMaterialBinding( geometry.materialKeys, instanceMaterials );
3785
-
3786
- // handle case if no materials are defined
3787
-
3788
- if ( materials.length === 0 ) {
3789
-
3790
- if ( type === 'lines' || type === 'linestrips' ) {
3791
-
3792
- materials.push( new LineBasicMaterial() );
3793
-
3794
- } else {
3795
-
3796
- materials.push( new MeshPhongMaterial() );
3797
-
3798
- }
3799
-
3800
- }
3801
-
3802
- // Collada allows to use phong and lambert materials with lines. Replacing these cases with LineBasicMaterial.
3803
-
3804
- if ( type === 'lines' || type === 'linestrips' ) {
3805
-
3806
- for ( let i = 0, l = materials.length; i < l; i ++ ) {
3807
-
3808
- const material = materials[ i ];
3809
-
3810
- if ( material.isMeshPhongMaterial === true || material.isMeshLambertMaterial === true ) {
3811
-
3812
- const lineMaterial = new LineBasicMaterial();
3813
-
3814
- // copy compatible properties
3815
-
3816
- lineMaterial.color.copy( material.color );
3817
- lineMaterial.opacity = material.opacity;
3818
- lineMaterial.transparent = material.transparent;
3819
-
3820
- // replace material
3821
-
3822
- materials[ i ] = lineMaterial;
3823
-
3824
- }
3825
-
3826
- }
3827
-
3828
- }
3829
-
3830
- // regard skinning
3831
-
3832
- const skinning = ( geometry.data.attributes.skinIndex !== undefined );
3833
-
3834
- // choose between a single or multi materials (material array)
3835
-
3836
- const material = ( materials.length === 1 ) ? materials[ 0 ] : materials;
3837
-
3838
- // now create a specific 3D object
3839
-
3840
- let object;
3841
-
3842
- switch ( type ) {
3843
-
3844
- case 'lines':
3845
- object = new LineSegments( geometry.data, material );
3846
- break;
3847
-
3848
- case 'linestrips':
3849
- object = new Line( geometry.data, material );
3850
- break;
3851
-
3852
- case 'triangles':
3853
- case 'polylist':
3854
- if ( skinning ) {
3855
-
3856
- object = new SkinnedMesh( geometry.data, material );
3857
-
3858
- } else {
3859
-
3860
- object = new Mesh( geometry.data, material );
3861
-
3862
- }
3863
-
3864
- break;
3865
-
3866
- }
3867
-
3868
- objects.push( object );
3869
-
3870
- }
3871
-
3872
- return objects;
3873
-
3874
- }
3875
-
3876
- function hasNode( id ) {
3877
-
3878
- return library.nodes[ id ] !== undefined;
3879
-
3880
- }
3881
-
3882
- function getNode( id ) {
3883
-
3884
- return getBuild( library.nodes[ id ], buildNode );
3885
-
3886
- }
3887
-
3888
- // visual scenes
3889
-
3890
- function parseVisualScene( xml ) {
3891
-
3892
- const data = {
3893
- name: xml.getAttribute( 'name' ),
3894
- children: []
3895
- };
3896
-
3897
- prepareNodes( xml );
3898
-
3899
- const elements = getElementsByTagName( xml, 'node' );
3900
-
3901
- for ( let i = 0; i < elements.length; i ++ ) {
3902
-
3903
- data.children.push( parseNode( elements[ i ] ) );
3904
-
3905
- }
3906
-
3907
- library.visualScenes[ xml.getAttribute( 'id' ) ] = data;
3908
-
3909
- }
3910
-
3911
- function buildVisualScene( data ) {
3912
-
3913
- const group = new Group();
3914
- group.name = data.name;
3915
-
3916
- const children = data.children;
3917
-
3918
- for ( let i = 0; i < children.length; i ++ ) {
3919
-
3920
- const child = children[ i ];
3921
-
3922
- group.add( getNode( child.id ) );
3923
-
3924
- }
3925
-
3926
- return group;
3927
-
3928
- }
3929
-
3930
- function hasVisualScene( id ) {
3931
-
3932
- return library.visualScenes[ id ] !== undefined;
3933
-
3934
- }
3935
-
3936
- function getVisualScene( id ) {
3937
-
3938
- return getBuild( library.visualScenes[ id ], buildVisualScene );
3939
-
3940
- }
3941
-
3942
- // scenes
3943
-
3944
- function parseScene( xml ) {
3945
-
3946
- const instance = getElementsByTagName( xml, 'instance_visual_scene' )[ 0 ];
3947
- return getVisualScene( parseId( instance.getAttribute( 'url' ) ) );
3948
-
3949
- }
3950
-
3951
- function setupAnimations() {
3952
-
3953
- const clips = library.clips;
3954
-
3955
- if ( isEmpty( clips ) === true ) {
3956
-
3957
- if ( isEmpty( library.animations ) === false ) {
3958
-
3959
- // if there are animations but no clips, we create a default clip for playback
3960
-
3961
- const tracks = [];
3962
-
3963
- for ( const id in library.animations ) {
3964
-
3965
- const animationTracks = getAnimation( id );
3966
-
3967
- for ( let i = 0, l = animationTracks.length; i < l; i ++ ) {
3968
-
3969
- tracks.push( animationTracks[ i ] );
3970
-
3971
- }
3972
-
3973
- }
3974
-
3975
- animations.push( new AnimationClip( 'default', - 1, tracks ) );
3976
-
3977
- }
3978
-
3979
- } else {
3980
-
3981
- for ( const id in clips ) {
3982
-
3983
- animations.push( getAnimationClip( id ) );
3984
-
3985
- }
3986
-
3987
- }
3988
-
3989
- }
3990
-
3991
- // convert the parser error element into text with each child elements text
3992
- // separated by new lines.
3993
-
3994
- function parserErrorToText( parserError ) {
3995
-
3996
- let result = '';
3997
- const stack = [ parserError ];
3998
-
3999
- while ( stack.length ) {
4000
-
4001
- const node = stack.shift();
4002
-
4003
- if ( node.nodeType === Node.TEXT_NODE ) {
4004
-
4005
- result += node.textContent;
4006
-
4007
- } else {
4008
-
4009
- result += '\n';
4010
- stack.push( ...node.childNodes );
4011
-
4012
- }
4013
-
4014
- }
4015
-
4016
- return result.trim();
4017
-
4018
- }
4019
-
4020
- if ( text.length === 0 ) {
4021
-
4022
- return { scene: new Scene() };
4023
-
4024
- }
4025
-
4026
- const xml = new DOMParser().parseFromString( text, 'application/xml' );
4027
-
4028
- const collada = getElementsByTagName( xml, 'COLLADA' )[ 0 ];
4029
-
4030
- const parserError = xml.getElementsByTagName( 'parsererror' )[ 0 ];
4031
- if ( parserError !== undefined ) {
4032
-
4033
- // Chrome will return parser error with a div in it
4034
-
4035
- const errorElement = getElementsByTagName( parserError, 'div' )[ 0 ];
4036
- let errorText;
4037
-
4038
- if ( errorElement ) {
4039
-
4040
- errorText = errorElement.textContent;
4041
-
4042
- } else {
4043
-
4044
- errorText = parserErrorToText( parserError );
4045
-
4046
- }
4047
-
4048
- console.error( 'THREE.ColladaLoader: Failed to parse collada file.\n', errorText );
4049
-
4050
- return null;
4051
-
4052
- }
4053
-
4054
- // metadata
4055
-
4056
- const version = collada.getAttribute( 'version' );
4057
- console.debug( 'THREE.ColladaLoader: File version', version );
4058
-
4059
- const asset = parseAsset( getElementsByTagName( collada, 'asset' )[ 0 ] );
4060
- const textureLoader = new TextureLoader( this.manager );
4061
- textureLoader.setPath( this.resourcePath || path ).setCrossOrigin( this.crossOrigin );
4062
-
4063
- let tgaLoader;
4064
-
4065
- if ( TGALoader ) {
4066
-
4067
- tgaLoader = new TGALoader( this.manager );
4068
- tgaLoader.setPath( this.resourcePath || path );
4069
-
4070
- }
4071
-
4072
- //
4073
-
4074
- const tempColor = new Color();
4075
- const animations = [];
4076
- let kinematics = {};
4077
- let count = 0;
4078
-
4079
- //
4080
-
4081
- const library = {
4082
- animations: {},
4083
- clips: {},
4084
- controllers: {},
4085
- images: {},
4086
- effects: {},
4087
- materials: {},
4088
- cameras: {},
4089
- lights: {},
4090
- geometries: {},
4091
- nodes: {},
4092
- visualScenes: {},
4093
- kinematicsModels: {},
4094
- physicsModels: {},
4095
- kinematicsScenes: {}
4096
- };
4097
-
4098
- parseLibrary( collada, 'library_animations', 'animation', parseAnimation );
4099
- parseLibrary( collada, 'library_animation_clips', 'animation_clip', parseAnimationClip );
4100
- parseLibrary( collada, 'library_controllers', 'controller', parseController );
4101
- parseLibrary( collada, 'library_images', 'image', parseImage );
4102
- parseLibrary( collada, 'library_effects', 'effect', parseEffect );
4103
- parseLibrary( collada, 'library_materials', 'material', parseMaterial );
4104
- parseLibrary( collada, 'library_cameras', 'camera', parseCamera );
4105
- parseLibrary( collada, 'library_lights', 'light', parseLight );
4106
- parseLibrary( collada, 'library_geometries', 'geometry', parseGeometry );
4107
- parseLibrary( collada, 'library_nodes', 'node', parseNode );
4108
- parseLibrary( collada, 'library_visual_scenes', 'visual_scene', parseVisualScene );
4109
- parseLibrary( collada, 'library_kinematics_models', 'kinematics_model', parseKinematicsModel );
4110
- parseLibrary( collada, 'library_physics_models', 'physics_model', parsePhysicsModel );
4111
- parseLibrary( collada, 'scene', 'instance_kinematics_scene', parseKinematicsScene );
4112
-
4113
- buildLibrary( library.animations, buildAnimation );
4114
- buildLibrary( library.clips, buildAnimationClip );
4115
- buildLibrary( library.controllers, buildController );
4116
- buildLibrary( library.images, buildImage );
4117
- buildLibrary( library.effects, buildEffect );
4118
- buildLibrary( library.materials, buildMaterial );
4119
- buildLibrary( library.cameras, buildCamera );
4120
- buildLibrary( library.lights, buildLight );
4121
- buildLibrary( library.geometries, buildGeometry );
4122
- buildLibrary( library.visualScenes, buildVisualScene );
4123
-
4124
- setupAnimations();
4125
- setupKinematics();
120
+ // Compose Three.js objects from library data
121
+ const composer = new ColladaComposer( library, collada, textureLoader, tgaLoader );
122
+ const { scene, animations, kinematics } = composer.compose();
4126
123
 
4127
- const scene = parseScene( getElementsByTagName( collada, 'scene' )[ 0 ] );
4128
124
  scene.animations = animations;
4129
125
 
126
+ // Handle coordinate system conversion
4130
127
  if ( asset.upAxis === 'Z_UP' ) {
4131
128
 
4132
129
  console.warn( 'THREE.ColladaLoader: You are loading an asset with a Z-UP coordinate system. The loader just rotates the asset to transform it into Y-UP. The vertex data are not converted, see #24289.' );
@@ -4134,6 +131,7 @@ class ColladaLoader extends Loader {
4134
131
 
4135
132
  }
4136
133
 
134
+ // Apply unit scale
4137
135
  scene.scale.multiplyScalar( asset.unit );
4138
136
 
4139
137
  return {