* Parse GLTF/GLB content * 解析 GLTF/GLB 内容
(content: IAssetContent, context: IAssetParseContext)
| 193 | * 解析 GLTF/GLB 内容 |
| 194 | */ |
| 195 | async parse(content: IAssetContent, context: IAssetParseContext): Promise<IGLTFAsset> { |
| 196 | const binary = content.binary; |
| 197 | if (!binary) { |
| 198 | throw new Error('GLTF loader requires binary content'); |
| 199 | } |
| 200 | |
| 201 | const isGLB = this.isGLB(binary); |
| 202 | let json: GLTFJson; |
| 203 | let binaryChunk: ArrayBuffer | null = null; |
| 204 | |
| 205 | if (isGLB) { |
| 206 | const glbData = this.parseGLB(binary); |
| 207 | json = glbData.json; |
| 208 | binaryChunk = glbData.binary; |
| 209 | } else { |
| 210 | // GLTF is JSON text |
| 211 | const decoder = new TextDecoder('utf-8'); |
| 212 | const text = decoder.decode(binary); |
| 213 | json = JSON.parse(text) as GLTFJson; |
| 214 | } |
| 215 | |
| 216 | // Validate GLTF version |
| 217 | if (!json.asset?.version?.startsWith('2.')) { |
| 218 | throw new Error(`Unsupported GLTF version: ${json.asset?.version}. Only GLTF 2.x is supported.`); |
| 219 | } |
| 220 | |
| 221 | // Load external buffers if needed |
| 222 | const buffers = await this.loadBuffers(json, binaryChunk, context); |
| 223 | |
| 224 | // Parse all components |
| 225 | const meshes = this.parseMeshes(json, buffers); |
| 226 | const materials = this.parseMaterials(json); |
| 227 | const textures = await this.parseTextures(json, buffers, context); |
| 228 | const nodes = this.parseNodes(json); |
| 229 | const rootNodes = this.getRootNodes(json); |
| 230 | const animations = this.parseAnimations(json, buffers); |
| 231 | const skeleton = this.parseSkeleton(json, buffers); |
| 232 | const bounds = this.calculateBounds(meshes); |
| 233 | |
| 234 | // Get model name from file path |
| 235 | const pathParts = context.metadata.path.split(/[\\/]/); |
| 236 | const fileName = pathParts[pathParts.length - 1]; |
| 237 | const name = fileName.replace(/\.(gltf|glb)$/i, ''); |
| 238 | |
| 239 | return { |
| 240 | name, |
| 241 | meshes, |
| 242 | materials, |
| 243 | textures, |
| 244 | nodes, |
| 245 | rootNodes, |
| 246 | animations: animations.length > 0 ? animations : undefined, |
| 247 | skeleton, |
| 248 | bounds, |
| 249 | sourcePath: context.metadata.path |
| 250 | }; |
| 251 | } |
| 252 |
no test coverage detected