Skip to content

Instantly share code, notes, and snippets.

let noseOptions = ["👃", "🐽", "💧", " "]
let eyeOptions = ["👁", "🌕", "🌟", "🔥", "⚽️", "🔎", " "]
let mouthOptions = ["👄", "👅", "❤️", " "]
let hatOptions = ["🎓", "🎩", "🧢", "⛑", "👒", " "]
let features = ["nose", "leftEye", "rightEye", "mouth", "hat"]
let featureIndices = [[9], [1064], [42], [24, 25], [20]]
func renderer(_ renderer: SCNSceneRenderer, didUpdate node: SCNNode, for anchor: ARAnchor) {
guard let faceAnchor = anchor as? ARFaceAnchor, let faceGeometry = node.geometry as? ARSCNFaceGeometry else { return }
// 更新,臉部表情變化時的位置
faceGeometry.update(from: faceAnchor.geometry)
updateFeatures(for: node, using: faceAnchor)
}
public enum SCNFillMode : UInt {
case fill
case lines
}
var originalSource: Any? = nil
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if originalSource == nil {
originalSource = sceneView.scene.background.contents
sceneView.scene.background.contents = UIColor.black
} else {
sceneView.scene.background.contents = originalSource
originalSource = nil
}
}
extension ViewController: ARSCNViewDelegate {
func renderer(_ renderer: SCNSceneRenderer, nodeFor anchor: ARAnchor) -> SCNNode? {
guard let faceAnchor = anchor as? ARFaceAnchor,
let device = sceneView.device else { return nil }
let faceGeometry = ARSCNFaceGeometry(device: device)
let node = SCNNode(geometry: faceGeometry)
node.geometry?.firstMaterial?.fillMode = .lines
DispatchQueue.main.async {
override func viewDidLoad() {
super.viewDidLoad()
// 判斷是否裝置支援 ar face 功能
guard ARFaceTrackingConfiguration.isSupported else { fatalError() }
sceneView.delegate = self
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
@IBOutlet weak var sceneView: ARSCNView!
- (void)sendMessage:(NSString *)context {
if ([self.peripherals count] < 1 || context == nil) {
return;
}
NSData *xmlData = [context dataUsingEncoding:NSUTF8StringEncoding];
CBPeripheral *peripheral = [self.peripherals firstObject];
[peripheral writeValue:xmlData forCharacteristic:self.cbCharacteristic type:CBCharacteristicWriteWithResponse];
}
- (void)peripheral:(CBPeripheral *)peripheral
didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic
error:(NSError *)error {
if (error) {
return;
}
if (characteristic.value == nil) {
return;
}
- (void)centralManager:(CBCentralManager *)central
didConnectPeripheral:(CBPeripheral *)peripheral {
[self.centralManager stopScan];
peripheral.delegate = self;
[peripheral discoverServices:@[[CBUUID UUIDWithString:kServiceUUID]]];
}
- (void)centralManager:(CBCentralManager *)central
didFailToConnectPeripheral:(CBPeripheral *)peripheral