staticfuncaverageMatrix(_matrices:[simd_float4x4], needScale:Bool=false) -> simd_float4x4 {
var averagePosition = simd_float4.zero
var averageScale:Float=0var averageRotation = simd_quatf()
for matrix in matrices {
averagePosition += matrix.columns.3if needScale {
let d = matrix.determinant
averageScale += cbrtf(d)
averageRotation += simd_quatf(matrix)
let count =Float(matrices.count)
averagePosition /= count
averageRotation = averageRotation.normalized
if needScale {
averageScale /= count
let s = simd_float4x4(diagonal: simd_float4(averageScale, averageScale, averageScale, 1))
let r = simd_float4x4(averageRotation)
let t = simd_float4x4(translation: averagePosition)
//复合变换约定顺序:缩放 —> 旋转 —> 平移return t * r * s
} else {
let r = simd_float4x4(averageRotation)
let t = simd_float4x4(translation: averagePosition)
//复合变换约定顺序:缩放 —> 旋转 —> 平移return t * r
复制代码