From 351eafb82c01400fefd1b74400375a49e1e628f4 Mon Sep 17 00:00:00 2001 From: Max Cobb Date: Sat, 30 Jan 2021 18:59:08 +0000 Subject: [PATCH 1/2] update swiftlint fixes and github action --- .github/workflows/swift-lint.yml | 4 +- .../FocusEntity/FocusEntity+Alignment.swift | 69 +++++++++---------- Sources/FocusEntity/FocusEntity.swift | 42 +++++------ 3 files changed, 58 insertions(+), 57 deletions(-) diff --git a/.github/workflows/swift-lint.yml b/.github/workflows/swift-lint.yml index 3797acd..1048071 100644 --- a/.github/workflows/swift-lint.yml +++ b/.github/workflows/swift-lint.yml @@ -14,4 +14,6 @@ jobs: steps: - uses: actions/checkout@v1 - name: GitHub Action for SwiftLint - uses: norio-nomura/action-swiftlint@3.1.0 \ No newline at end of file + uses: norio-nomura/action-swiftlint@3.2.1 + with: + args: --strict diff --git a/Sources/FocusEntity/FocusEntity+Alignment.swift b/Sources/FocusEntity/FocusEntity+Alignment.swift index 3e8c7d3..09dbef3 100644 --- a/Sources/FocusEntity/FocusEntity+Alignment.swift +++ b/Sources/FocusEntity/FocusEntity+Alignment.swift @@ -14,24 +14,25 @@ import Combine extension FocusEntity { // MARK: Helper Methods - + /// Update the position of the focus square. - internal func updatePosition(){ - // Average using several most recent positions. - recentFocusEntityPositions = Array(recentFocusEntityPositions.suffix(10)) - - // Move to average of recent positions to avoid jitter. - let average = recentFocusEntityPositions.reduce( - SIMD3(repeating: 0), { $0 + $1 } - ) / Float(recentFocusEntityPositions.count) - self.position = average - } + internal func updatePosition() { + // Average using several most recent positions. + recentFocusEntityPositions = Array(recentFocusEntityPositions.suffix(10)) + + // Move to average of recent positions to avoid jitter. + let average = recentFocusEntityPositions.reduce( + SIMD3.zero, { $0 + $1 } + ) / Float(recentFocusEntityPositions.count) + self.position = average + } /// Update the transform of the focus square to be aligned with the camera. - internal func updateTransform(for position: SIMD3, raycastResult: ARRaycastResult, camera: ARCamera?) { - + internal func updateTransform( + for position: SIMD3, raycastResult: ARRaycastResult, camera: ARCamera? + ) { self.updatePosition() - + //Produces odd scaling when focus entity is moving towards the user along a horizontal plane; //looks like the focus entity is sinking downwards. // if self.scaleEntityBasedOnDistance { @@ -101,24 +102,23 @@ extension FocusEntity { // Alignment is different than most of the history - ignore it return } - - var targetAlignment : simd_quatf + + var targetAlignment: simd_quatf if alignment == .horizontal { - targetAlignment = simd_quatf(angle: angle, axis: [0,1,0]) + targetAlignment = simd_quatf(angle: angle, axis: [0, 1, 0]) } else { - targetAlignment = raycastResult.worldTransform.orientation + targetAlignment = raycastResult.worldTransform.orientation } // Change the focus entity's alignment if isChangingAlignment { - //Uses interpolation. - //Needs to be called on every frame that the animation is desired, Not just the first frame. - performAlignmentAnimation(to: targetAlignment) + // Uses interpolation. + // Needs to be called on every frame that the animation is desired, Not just the first frame. + performAlignmentAnimation(to: targetAlignment) } else { - orientation = targetAlignment + orientation = targetAlignment } } - internal func normalize(_ angle: Float, forMinimalRotationTo ref: Float) -> Float { // Normalize angle in steps of 90 degrees such that the rotation to the other angle is minimal @@ -166,18 +166,17 @@ extension FocusEntity { } ///Uses interpolation between orientations to create a smooth `easeOut` orientation adjustment animation. - internal func performAlignmentAnimation(to newOrientation: simd_quatf) { - //Interpolate between current and target orientations. - orientation = simd_slerp(orientation, newOrientation, 0.15) - //This length creates a normalized vector (of length 1) with all 3 components being equal. - let axisLength = 1 / sqrtf(3) - let testVector: simd_float3 = [axisLength, axisLength, axisLength] - let point1 = orientation.act(testVector) - let point2 = newOrientation.act(testVector) - let vectorsDot = simd_dot(point1, point2) - //Stop interpolating when the rotations are close enough to each other. - self.isChangingAlignment = vectorsDot < 0.999 - } + internal func performAlignmentAnimation(to newOrientation: simd_quatf) { + // Interpolate between current and target orientations. + orientation = simd_slerp(orientation, newOrientation, 0.15) + // This length creates a normalized vector (of length 1) with all 3 components being equal. + let testVector = simd_float3(repeating: 1 / sqrtf(3)) + let point1 = orientation.act(testVector) + let point2 = newOrientation.act(testVector) + let vectorsDot = simd_dot(point1, point2) + // Stop interpolating when the rotations are close enough to each other. + self.isChangingAlignment = vectorsDot < 0.999 + } /** Reduce visual size change with distance by scaling up when close and down when far away. diff --git a/Sources/FocusEntity/FocusEntity.swift b/Sources/FocusEntity/FocusEntity.swift index 3f88e89..dc0b35a 100644 --- a/Sources/FocusEntity/FocusEntity.swift +++ b/Sources/FocusEntity/FocusEntity.swift @@ -156,7 +156,7 @@ open class FocusEntity: Entity, HasAnchoring, HasFocusEntity { ///A camera anchor used for placing the focus entity in front of the camera. internal var cameraAnchor: AnchorEntity! - + /// The focus square's current alignment. internal var currentAlignment: ARPlaneAnchor.Alignment? @@ -201,7 +201,7 @@ open class FocusEntity: Entity, HasAnchoring, HasFocusEntity { cameraAnchor = AnchorEntity(.camera) arView.scene.addAnchor(cameraAnchor) - + // Start the focus square as a billboard. displayAsBillboard() self.delegate?.toInitializingState?() @@ -239,19 +239,19 @@ open class FocusEntity: Entity, HasAnchoring, HasFocusEntity { self.currentAlignment = .none stateChangedSetup() } - - ///Places the focus entity in front of the camera instead of on a plane. - private func putInFrontOfCamera(){ - - //Works better than arView.ray() - let newPosition = cameraAnchor.convert(position: [0,0,-1.1], to: nil) - recentFocusEntityPositions.append(newPosition) - updatePosition() - //--// - //Make focus entity face the camera with a smooth animation. - var newRotation = arView?.cameraTransform.rotation ?? simd_quatf() - newRotation *= simd_quatf(angle: .pi / 2, axis: [1,0,0]) - performAlignmentAnimation(to: newRotation) + + /// Places the focus entity in front of the camera instead of on a plane. + private func putInFrontOfCamera() { + + // Works better than arView.ray() + let newPosition = cameraAnchor.convert(position: [0, 0, -1.1], to: nil) + recentFocusEntityPositions.append(newPosition) + updatePosition() + //--// + // Make focus entity face the camera with a smooth animation. + var newRotation = arView?.cameraTransform.rotation ?? simd_quatf() + newRotation *= simd_quatf(angle: .pi / 2, axis: [1, 0, 0]) + performAlignmentAnimation(to: newRotation) } /// Called when a surface has been detected. @@ -259,10 +259,10 @@ open class FocusEntity: Entity, HasAnchoring, HasFocusEntity { self.stateChangedSetup() let position = raycastResult.worldTransform.translation if self.currentAlignment != .none { - //It is ready to move over to a new surface. - recentFocusEntityPositions.append(position) + // It is ready to move over to a new surface. + recentFocusEntityPositions.append(position) } else { - putInFrontOfCamera() + putInFrontOfCamera() } updateTransform(for: position, raycastResult: raycastResult, camera: camera) } @@ -274,10 +274,10 @@ open class FocusEntity: Entity, HasAnchoring, HasFocusEntity { anchorsOfVisitedPlanes.insert(planeAnchor) let position = raycastResult.worldTransform.translation if self.currentAlignment != .none { - //It is ready to move over to a new surface. - recentFocusEntityPositions.append(position) + // It is ready to move over to a new surface. + recentFocusEntityPositions.append(position) } else { - putInFrontOfCamera() + putInFrontOfCamera() } updateTransform(for: position, raycastResult: raycastResult, camera: camera) } From 361a23766ac891e1c115f7818d08a8389f341832 Mon Sep 17 00:00:00 2001 From: Max Cobb Date: Sat, 30 Jan 2021 19:04:03 +0000 Subject: [PATCH 2/2] swiftlint updated fixes --- FocusEntity-Example/FocusEntity-Example/FocusARView.swift | 2 +- Sources/FocusEntity/FocusEntity+Alignment.swift | 6 +++--- Sources/FocusEntity/FocusEntity+Colored.swift | 4 ++-- Sources/FocusEntity/FocusEntity.swift | 6 +++--- Sources/FocusEntity/FocusEntityComponent.swift | 2 +- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/FocusEntity-Example/FocusEntity-Example/FocusARView.swift b/FocusEntity-Example/FocusEntity-Example/FocusARView.swift index 697266d..4c345e9 100644 --- a/FocusEntity-Example/FocusEntity-Example/FocusARView.swift +++ b/FocusEntity-Example/FocusEntity-Example/FocusARView.swift @@ -7,7 +7,7 @@ // import RealityKit -//import FocusEntity +// import FocusEntity import Combine import ARKit diff --git a/Sources/FocusEntity/FocusEntity+Alignment.swift b/Sources/FocusEntity/FocusEntity+Alignment.swift index 09dbef3..dd37ba2 100644 --- a/Sources/FocusEntity/FocusEntity+Alignment.swift +++ b/Sources/FocusEntity/FocusEntity+Alignment.swift @@ -33,8 +33,8 @@ extension FocusEntity { ) { self.updatePosition() - //Produces odd scaling when focus entity is moving towards the user along a horizontal plane; - //looks like the focus entity is sinking downwards. + // Produces odd scaling when focus entity is moving towards the user along a horizontal plane; + // looks like the focus entity is sinking downwards. // if self.scaleEntityBasedOnDistance { // self.scale = SIMD3(repeating: scaleBasedOnDistance(camera: camera)) // } @@ -165,7 +165,7 @@ extension FocusEntity { return results.first(where: { $0.target == .estimatedPlane }) } - ///Uses interpolation between orientations to create a smooth `easeOut` orientation adjustment animation. + /// Uses interpolation between orientations to create a smooth `easeOut` orientation adjustment animation. internal func performAlignmentAnimation(to newOrientation: simd_quatf) { // Interpolate between current and target orientations. orientation = simd_slerp(orientation, newOrientation, 0.15) diff --git a/Sources/FocusEntity/FocusEntity+Colored.swift b/Sources/FocusEntity/FocusEntity+Colored.swift index 295721c..b4b4ea7 100644 --- a/Sources/FocusEntity/FocusEntity+Colored.swift +++ b/Sources/FocusEntity/FocusEntity+Colored.swift @@ -25,10 +25,10 @@ public extension FocusEntity { if self.fillPlane?.model?.materials.count == 0 { self.fillPlane?.model?.materials = [SimpleMaterial()] } - //Necessary for transparency. + // Necessary for transparency. var modelMaterial = UnlitMaterial(color: .clear) modelMaterial.baseColor = endColor - //Necessary for transparency. + // Necessary for transparency. modelMaterial.tintColor = Material.Color.white.withAlphaComponent(0.995) self.fillPlane?.model?.materials[0] = modelMaterial } diff --git a/Sources/FocusEntity/FocusEntity.swift b/Sources/FocusEntity/FocusEntity.swift index dc0b35a..d5572b0 100644 --- a/Sources/FocusEntity/FocusEntity.swift +++ b/Sources/FocusEntity/FocusEntity.swift @@ -154,7 +154,7 @@ open class FocusEntity: Entity, HasAnchoring, HasFocusEntity { /// Indicates if the square is currently changing its alignment. public internal(set) var isChangingAlignment = false - ///A camera anchor used for placing the focus entity in front of the camera. + /// A camera anchor used for placing the focus entity in front of the camera. internal var cameraAnchor: AnchorEntity! /// The focus square's current alignment. @@ -247,7 +247,7 @@ open class FocusEntity: Entity, HasAnchoring, HasFocusEntity { let newPosition = cameraAnchor.convert(position: [0, 0, -1.1], to: nil) recentFocusEntityPositions.append(newPosition) updatePosition() - //--// + // --// // Make focus entity face the camera with a smooth animation. var newRotation = arView?.cameraTransform.rotation ?? simd_quatf() newRotation *= simd_quatf(angle: .pi / 2, axis: [1, 0, 0]) @@ -309,7 +309,7 @@ open class FocusEntity: Entity, HasAnchoring, HasFocusEntity { case .normal = camera.trackingState, let result = self.smartRaycast() else { - //We should place the focus entity in front of the camera instead of on a plane. + // We should place the focus entity in front of the camera instead of on a plane. putInFrontOfCamera() self.state = .initializing return diff --git a/Sources/FocusEntity/FocusEntityComponent.swift b/Sources/FocusEntity/FocusEntityComponent.swift index d2c0007..03616a4 100644 --- a/Sources/FocusEntity/FocusEntityComponent.swift +++ b/Sources/FocusEntity/FocusEntityComponent.swift @@ -58,7 +58,7 @@ public struct FocusEntityComponent: Component { } } - ///Convenient presets + /// Convenient presets public static let classic = FocusEntityComponent(style: .classic(color: #colorLiteral(red: 1, green: 0.8, blue: 0, alpha: 1))) public static let plane = FocusEntityComponent( style: .colored(