Skip to content

Commit dc14287

Browse files
committed
Filter tangent values to vec3
1 parent ed7450b commit dc14287

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

Sources/GateEngine/Resources/Import & Export/Importers/GLTransmissionFormat.swift

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -632,8 +632,23 @@ extension GLTransmissionFormat: GeometryImporter {
632632

633633
var tangents: [Float]? = nil
634634
if let accessorIndex = primitive[.tangent] {
635-
tangents = await gltf.values(forAccessor: accessorIndex)
636-
#warning("Tangents not filtered to vec3")
635+
let accessor = gltf.accessors[accessorIndex]
636+
switch accessor.type {
637+
case .vec3:
638+
tangents = await gltf.values(forAccessor: accessorIndex)
639+
case .vec4:
640+
// the 4th scalar in each primitive is for generating a bitangent
641+
// Bitangent can be generated without this value, so we discard it.
642+
if var _tangents: [Float] = await gltf.values(forAccessor: accessorIndex) {
643+
for indexOfW in stride(from: 3, to: _tangents.count, by: 4) {
644+
_tangents.remove(at: indexOfW)
645+
}
646+
tangents = _tangents
647+
}
648+
default:
649+
throw GateEngineError.failedToDecode("Unhandled accessor type for tangents: \(accessor.type)")
650+
}
651+
Log.assert(tangents == nil || tangents!.count == positions.count, "Tangent count doesn't match position count.")
637652
}
638653

639654
var colors: [Float]? = nil

0 commit comments

Comments
 (0)