Я пытаюсь изменить текстуру нажатием кнопки. Загрузка usdz (createData ()) или создание текста работают нормально, добавление материала и текстуры ...
Но при изменении состояния или привязанного объекта Object в моем ContentView текстура не обновляется ...
Подскажите, пожалуйста, где и как обновить мою модель?
struct ContentView : View {
@ObservedObject var store = TextureStore()
func setMyCurrentTexture(textureName: String){
store.myCurrentTexture = textureName
}
var body: some View {
VStack {
ARViewContainer(texture: $store.myCurrentTexture).edgesIgnoringSafeArea(.all)
Spacer()
HStack {
Button(action: {
self.setMyCurrentTexture(textureName: "one")
}) {Text("Use texture one")}
Spacer()
Button(action: {self.setMyCurrentTexture(textureName: "two")
}) {Text("Use texture two")}
}
}
}
}
struct ARViewContainer: UIViewRepresentable {
@Binding var texture: String
func makeUIView(context: Context) -> ARView {
let arView = ARView(frame: .zero)
let myurl = URL(fileURLWithPath: "cube", relativeTo: FileManager.documentDirectoryURL).appendingPathExtension("usdz")
let redbox = try? Entity.load(contentsOf: myurl)
let textShape = MeshResource.generateText("nice text shape")
var textMaterial = SimpleMaterial()
do {
try textMaterial.baseColor = .texture(.load(named: texture))
} catch {
textMaterial.tintColor = UIColor(cgColor: #colorLiteral(red: 0.4666666687, green: 0.7647058964, blue: 0.2666666806, alpha: 1))
}
let myText = ModelEntity(mesh: textShape, materials: [textMaterial])
redbox?.scale = [0.3, 0.3, 0.3]
myText.orientation = simd_quatf(angle: 3*Float.pi/2, axis: [1, 0, 0])
myText.scale = [0.1, 0.1, 0.1]
// Creating parent ModelEntity
let parentEntity = ModelEntity()
parentEntity.addChild(redbox!)
parentEntity.addChild(myText)
let entityBounds = redbox!.visualBounds(relativeTo: parentEntity)
parentEntity.collision = CollisionComponent(shapes: [ShapeResource.generateBox(size: entityBounds.extents).offsetBy(translation: entityBounds.center)])
let anchor = AnchorEntity(plane: .any , classification: .any )
anchor.addChild(parentEntity)
arView.scene.anchors.append(anchor)
arView.installGestures(.all, for: parentEntity)
return arView
}
func updateUIView(_ uiView: ARView, context: Context) {
// I COULD PRINT INTO CONSOLE, THAT texture IS CHANGING
var material = SimpleMaterial()
do {
try material.baseColor = .texture(.load(named: texture))
} catch {
material.tintColor = UIColor(cgColor: #colorLiteral(red: 0.4666666687, green: 0.7647058964, blue: 0.2666666806, alpha: 1))
}
// BUT HOW TO ATTACH TO MY CUBE OR TO MY TEXT
// I CAN TRIGGER THE VISIBILITY TOUGH WITH isEnabled
}
}