Skip to content

Commit

Permalink
Merge pull request #16 from maxxfrazer/swiftlint-update
Browse files Browse the repository at this point in the history
SwiftLint Fixes + GitHub Actions Update
  • Loading branch information
maxxfrazer authored Jan 30, 2021
2 parents 1c8dbb7 + 361a237 commit b83ae04
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 66 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/swift-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ jobs:
steps:
- uses: actions/checkout@v1
- name: GitHub Action for SwiftLint
uses: norio-nomura/action-swiftlint@3.1.0
uses: norio-nomura/action-swiftlint@3.2.1
with:
args: --strict
2 changes: 1 addition & 1 deletion FocusEntity-Example/FocusEntity-Example/FocusARView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//

import RealityKit
//import FocusEntity
// import FocusEntity
import Combine
import ARKit

Expand Down
75 changes: 37 additions & 38 deletions Sources/FocusEntity/FocusEntity+Alignment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,27 @@ 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<Float>(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<Float>.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<Float>, raycastResult: ARRaycastResult, camera: ARCamera?) {

internal func updateTransform(
for position: SIMD3<Float>, 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.

// 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<Float>(repeating: scaleBasedOnDistance(camera: camera))
// }
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -165,19 +165,18 @@ extension FocusEntity {
return results.first(where: { $0.target == .estimatedPlane })
}

///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
}
/// 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 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.
Expand Down
4 changes: 2 additions & 2 deletions Sources/FocusEntity/FocusEntity+Colored.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
46 changes: 23 additions & 23 deletions Sources/FocusEntity/FocusEntity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,9 @@ 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.
internal var currentAlignment: ARPlaneAnchor.Alignment?

Expand Down Expand Up @@ -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?()
Expand Down Expand Up @@ -239,30 +239,30 @@ 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.
private func displayOffPlane(for raycastResult: ARRaycastResult, camera: ARCamera?) {
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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Sources/FocusEntity/FocusEntityComponent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit b83ae04

Please sign in to comment.