每秒记录 60 个 4x4 变换矩阵
写一个4x4 变换矩阵(即存储平移、旋转和缩放的复杂变换)到文本文件,我使用write(to:)
实例方法。点击后Record Transform Values
按钮,数据立即开始写入toMaya.txt
文件。为了方便起见,我将矩阵的所有 16 个值都放在屏幕上(我在 iPad 上测试了这一点,因此请使用屏幕更大的设备)。再次按下该按钮将停止录制。
数据来自嵌套列表 in the toMaya.txt
可以使用常规 Python 或 MEL 脚本轻松读取文件。看看嵌套是什么样子的。每一个16 个矩阵值在Float
type.
[x0,y0,z0,w0]
是第一个矩阵列,[x1,y1,z1,w1]
是第二个矩阵列,等等。
这是代码:
import SwiftUI
import RealityKit
import Combine
struct ARViewContainer : UIViewRepresentable {
@Binding var arView: ARView
func makeUIView(context: Context) -> ARView { return arView }
func updateUIView(_ view: ARView, context: Context) { }
}
struct ContentView : View {
@State private var arView = ARView(frame: .zero)
@State private var subs: [AnyCancellable] = []
@State private var array: [[[Float]]] = [[ [1,0,0,0], [0,1,0,0],
[0,0,1,0], [0,0,0,1] ]]
@State private var boolean = false
let url = FileManager.default.urls(for: .documentDirectory,
in: .userDomainMask)[0]
.appendingPathComponent("toMaya.txt")
var body: some View {
ZStack {
ARViewContainer(arView: $arView).ignoresSafeArea()
VStack {
Button("Record Transform Values") {
boolean.toggle()
DispatchQueue.main.async {
arView.scene.subscribe(to: SceneEvents.Update.self) { _ in
let col = arView.cameraTransform.matrix.columns
let mtx: [[Float]] = [
[col.0.x, col.0.y, col.0.z, col.0.w],
[col.1.x, col.1.y, col.1.z, col.1.w],
[col.2.x, col.2.y, col.2.z, col.2.w],
[col.3.x, col.3.y, col.3.z, col.3.w]
]
if boolean {
array.append(mtx)
}
if let data = try? JSONEncoder().encode(self.array) {
guard let str = String(data: data, encoding: .ascii)
else { return }
do {
if boolean {
try str.write(to: url, atomically: true,
encoding: .ascii)
}
} catch {
print(error.localizedDescription)
}
print(url)
}
}.store(in: &subs)
}
}
Spacer()
}
VStack {
Spacer()
Text("\(array.count)").foregroundColor(.white)
}
HStack {
VStack {
Text("\(array.last![0][0])").foregroundColor(.white)
Text("\(array.last![0][1])").foregroundColor(.white)
Text("\(array.last![0][2])").foregroundColor(.white)
Text("\(array.last![0][3])").foregroundColor(.white)
}
VStack {
Text("\(array.last![1][0])").foregroundColor(.white)
Text("\(array.last![1][1])").foregroundColor(.white)
Text("\(array.last![1][2])").foregroundColor(.white)
Text("\(array.last![1][3])").foregroundColor(.white)
}
VStack {
Text("\(array.last![2][0])").foregroundColor(.white)
Text("\(array.last![2][1])").foregroundColor(.white)
Text("\(array.last![2][2])").foregroundColor(.white)
Text("\(array.last![2][3])").foregroundColor(.white)
}
VStack {
Text("\(array.last![3][0])").foregroundColor(.white)
Text("\(array.last![3][1])").foregroundColor(.white)
Text("\(array.last![3][2])").foregroundColor(.white)
Text("\(array.last![3][3])").foregroundColor(.white)
}
}
}
}
}
My toMaya.txt
文件在下面等我调试目录:
file:///var/mobile/Containers/Data/Application/7C675F52-C78B-4252-98B5-3EBD37A3F832/Documents/toMaya.txt
之后您可以导出您的toMaya.txt
文件到 Mac 使用UIActivityViewController.