我有一个3D模型(USDZ)在RealityKit中有一个PBR材质。我可以通过UILongPressGestureRecognizer选择模型。当我选择它时,我需要3D模型改变颜色,这样用户就知道它被选中了。我通过向模型附加一个橙子的UnlitMaterial
来让它工作,然后将其分配给材料[0],并将PBR分配给材料[1]。一旦选择了3D模型,用户再次通过LongPress
取消选择模型,交换2种材料,并删除最后一种材料。第一次运行正常,如果我再次启动这个过程(2次循环),“UnlitMaterial”是白色的。在第三十三次循环时,它是橙色的?
我试着把var simpleMat = UnlitMaterial(color: .orange)
放在“If语句”中的不同位置,因为我认为当不亮的是白色时,它不知何故没有正确地分配橙子,它只是一个没有颜色的UnlitMaterial
。
@objc func handleLongPress(recognizer: UILongPressGestureRecognizer) {
let location = recognizer.location(in: arView)
if let entity = arView.entity(at: location){
if let anchorEntity = entity.anchor{
currectAnchorEntity = anchorEntity
switch recognizer.state {
case .began:
//if the model/object is not currently selected and nothing assigned to currentModelEntity (when you first start AR session)
if currentModelEntity != entity && currentModelEntity.children.isEmpty {
var simpleMat = UnlitMaterial(color: .orange)
//--- 1 making it the current
currentModelEntity = entity
//--- 2 finding the model
entModel = (entity.findEntity(named: "model") as? ModelEntity)!
//--- 3 Adding the unlitmaterial colour materal to material [1]
entModel.model?.materials.append(simpleMat)
//-- 4 Assigning Varibles to [0] & [1] for reference
let matLastL = entModel.model?.materials.last
let matLastF = entModel.model?.materials.first
//--- 5 Swaps the materials on the model, so i can keep the original material
entModel.model?.materials[1] = matLastF!
entModel.model?.materials[0] = simpleMat
//--- 6 Unhides the menu
objectMenu.isHidden = false
recognizer.state = .cancelled
}
// not the current model but there is another model that is selcted which is the 'CurrentModelEntity'
else if currentModelEntity != entity && currentModelEntity.children.count == 1 {
//--- 1 finding the model on currently selected model
entModel = (currentModelEntity.findEntity(named: "model") as? ModelEntity)!
//--- 2 Assigning Varibles to [0] & [1] for reference
let matLastL = entModel.model?.materials.last
let matLastF = entModel.model?.materials.first
//--- 3 Assigning the last material(original) to [0] slot
entModel.model?.materials[0] = matLastL!
//--- 4 Removing the 'UnlitMaterial' from [1]
entModel.model?.materials.remove(at: 1)
//--- 5 Unassigning the currentModel
currentModelEntity = Entity()
//--- 6 Hinding the menu
objectMenu.isHidden = true
//--- 7 stopping the touches
recognizer.state = .cancelled
}
// is the 'CurrentModelEntity'- at this point the materials are Unlit=[0], Original=[1]
else if currentModelEntity == entity && currentModelEntity.children.count == 1{
//--- 1 finding the model on currently selected model
entModel = (entity.findEntity(named: "model") as? ModelEntity)!
//--- 2 Assigning Varibles to [0] & [1] for reference
let matLastL = entModel.model?.materials.last
let matLastF = entModel.model?.materials.first
//--- 3 Swaps the materials on the model
entModel.model?.materials[1] = matLastF!
entModel.model?.materials[0] = matLastL!
//--- 4 Remove the simpleMat from slot 1
entModel.model?.materials.remove(at: 1)
//--- 5 Hinding the menu
objectMenu.isHidden = true
//--- 6 Unasigning the current model
currentModelEntity = Entity()
//--- 7 stopping the touches
recognizer.state = .cancelled
}
case .ended, .cancelled: break
default: break
}
}
}
}
1条答案
按热度按时间vlf7wbxs1#
使用以下策略来实现所需的结果。
这个答案也会很有帮助。