Created
January 3, 2023 12:42
-
-
Save wizard1066/a76c63676c7157b20879b8eb40f3baa7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Coordinator: NSObject, ARSCNViewDelegate, ARSessionDelegate { | |
private var trackingView:ARSCNView | |
private var sphereNode: SCNNode! | |
private var cubeNode: SCNNode! | |
init(_ view: ARSCNView) { | |
self.trackingView = view | |
super.init() | |
guard ARFaceTrackingConfiguration.isSupported else { | |
fatalError("Face tracking not available on this on this device model!") | |
} | |
let configuration = ARFaceTrackingConfiguration() | |
self.trackingView.session.run(configuration) | |
self.trackingView.delegate = self | |
let geo3 = SCNSphere(radius: 0.5) | |
geo3.segmentCount = 16 | |
sphereNode = SCNNode(geometry: geo3) | |
sphereNode.geometry?.firstMaterial?.diffuse.contents = UIColor.white.withAlphaComponent(0.99) | |
sphereNode.geometry?.firstMaterial?.fillMode = .lines | |
sphereNode.simdPosition = SIMD3(x: 0, y: 1, z: -6) | |
self.trackingView.scene.rootNode.addChildNode(sphereNode) | |
let colorNames:[UIColor] = [UIColor.red, .blue, .green, .purple, .orange, .brown] | |
var colorMaterials:[SCNMaterial] = [] | |
for colors in colorNames { | |
let newMaterial = SCNMaterial() | |
newMaterial.diffuse.contents = colors | |
colorMaterials.append(newMaterial) | |
} | |
let geo2 = SCNBox(width: 1, height: 1, length: 1, chamferRadius: 0) | |
cubeNode = SCNNode(geometry: geo2) | |
cubeNode.geometry?.materials = colorMaterials | |
cubeNode.position = SCNVector3(x: 0, y: 0, z: -10) | |
self.trackingView.scene.rootNode.addChildNode(cubeNode) | |
} | |
func renderer(_ renderer: SCNSceneRenderer, didUpdate node: SCNNode, for anchor: ARAnchor) { | |
guard let faceAnchor = anchor as? ARFaceAnchor else { return } | |
DispatchQueue.main.async { [self] in | |
self.cubeNode.simdOrientation = faceAnchor.leftEyeTransform.orientation | |
if faceAnchor.blendShapes[.eyeLookInLeft]!.doubleValue < 0.3 && | |
faceAnchor.blendShapes[.eyeLookInRight]!.doubleValue < 0.3 && | |
faceAnchor.blendShapes[.eyeLookUpLeft]!.doubleValue < 0.3 && | |
faceAnchor.blendShapes[.eyeLookDownLeft]!.doubleValue < 0.3 { | |
SCNTransaction.begin() | |
SCNTransaction.animationDuration = 1.0 | |
sphereNode.simdPosition = SIMD3(x: 0, y: 0, z: -6) | |
SCNTransaction.commit() | |
} | |
if faceAnchor.blendShapes[.eyeLookInLeft]!.doubleValue > 0.3 { | |
SCNTransaction.begin() | |
SCNTransaction.animationDuration = 1.0 | |
sphereNode.simdPosition = SIMD3(x: -3, y: 0, z: -6) | |
SCNTransaction.commit() | |
} | |
if faceAnchor.blendShapes[.eyeLookInRight]!.doubleValue > 0.3 { | |
SCNTransaction.begin() | |
SCNTransaction.animationDuration = 1.0 | |
sphereNode.simdPosition = SIMD3(x: 3, y: 0, z: -6) | |
SCNTransaction.commit() | |
} | |
if faceAnchor.blendShapes[.eyeLookUpLeft]!.doubleValue > 0.3 { | |
SCNTransaction.begin() | |
SCNTransaction.animationDuration = 1.0 | |
sphereNode.simdPosition = SIMD3(x: 0, y: 1, z: -6) | |
SCNTransaction.commit() | |
} | |
if faceAnchor.blendShapes[.eyeLookDownLeft]!.doubleValue > 0.3 { | |
SCNTransaction.begin() | |
SCNTransaction.animationDuration = 1.0 | |
sphereNode.simdPosition = SIMD3(x: 0, y: -1, z: -6) | |
SCNTransaction.commit() | |
} | |
if faceAnchor.blendShapes[.eyeLookInLeft]!.doubleValue > 0.2 && | |
faceAnchor.blendShapes[.eyeLookDownLeft]!.doubleValue > 0.2 { | |
sphereNode.simdPosition = SIMD3(x: -2, y: -1, z: -6) | |
} | |
if faceAnchor.blendShapes[.eyeLookInLeft]!.doubleValue > 0.2 && | |
faceAnchor.blendShapes[.eyeLookUpLeft]!.doubleValue > 0.2 { | |
sphereNode.simdPosition = SIMD3(x: -2, y: 1, z: -6) | |
} | |
if faceAnchor.blendShapes[.eyeLookInRight]!.doubleValue > 0.2 && | |
faceAnchor.blendShapes[.eyeLookDownLeft]!.doubleValue > 0.2 { | |
sphereNode.simdPosition = SIMD3(x: 2, y: -1, z: -6) | |
} | |
if faceAnchor.blendShapes[.eyeLookInRight]!.doubleValue > 0.2 && | |
faceAnchor.blendShapes[.eyeLookUpLeft]!.doubleValue > 0.2 { | |
sphereNode.simdPosition = SIMD3(x: 2, y: 1, z: -6) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment