Skip to content

Commit

Permalink
Added interpolation for ScalingScheme (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lumyk authored Apr 15, 2024
1 parent 9184d9c commit 968b457
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
15 changes: 9 additions & 6 deletions Sources/ARKit-CoreLocation/Nodes/LocationAnnotationNode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,15 @@ open class LocationAnnotationNode: LocationNode {
/// - location:The location of the node in the world.
/// - view:The view to display at the specified location.
public convenience init(location: CLLocation?, view: UIView) {
self.init(location: location, image: view.image)
self.init(location: location, image: view.toImage)
}

public init(location: CLLocation?, layer: CALayer) {
let plane = SCNPlane(width: layer.bounds.size.width / 100, height: layer.bounds.size.height / 100)
public convenience init(location: CLLocation?, layer: CALayer) {
self.init(location: location, layer: layer, size: layer.bounds.size)
}

public init(location: CLLocation?, layer: CALayer, size: CGSize) {
let plane = SCNPlane(width: size.width / 100, height: size.height / 100)
plane.firstMaterial?.diffuse.contents = layer
plane.firstMaterial?.lightingModel = .constant

Expand Down Expand Up @@ -125,15 +129,14 @@ open class LocationAnnotationNode: LocationNode {

// MARK: - Image from View

public extension UIView {
extension UIView {

@available(iOS 10.0, *)
/// Gets you an image from the view.
var image: UIImage {
var toImage: UIImage {
let renderer = UIGraphicsImageRenderer(bounds: bounds)
return renderer.image { rendererContext in
layer.render(in: rendererContext.cgContext)
}
}

}
18 changes: 16 additions & 2 deletions Sources/ARKit-CoreLocation/Nodes/ScalingScheme.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public enum ScalingScheme {
case doubleTiered(firstThreshold: Double, firstScale: Float, secondThreshold: Double, secondScale: Float)
case linear(threshold: Double)
case linearBuffer(threshold: Double, buffer: Double)
case interpolate(minDistance: Double, minScale: Double, maxDistance: Double, maxScale: Double, cutToMinMax: Bool)
case exponentialInterpolation(startDistance: Double, endDistance: Double, startScale: Double, endScale: Double, k: Double)

/// Returns a closure to compute appropriate scale factor based on the current value of `self` (a `ScalingSchee`).
/// The closure accepts two parameters and returns the scale factor to apply to an `AnnotationNode`.
Expand Down Expand Up @@ -84,8 +86,20 @@ public enum ScalingScheme {
}
return scale
}
}
case .interpolate(let minDistance, let minScale, let maxDistance, let maxScale, let cutToMinMax):
return { (distance, adjustedDistance) in

}
let clampedDistance = cutToMinMax ? min(max(minDistance, distance), maxDistance) : distance
let scale = minScale + (clampedDistance - minDistance) * (maxScale - minScale) / (maxDistance - minDistance)

return Float(scale)
}
case .exponentialInterpolation(let startDistance, let endDistance, let startScale, let endScale, let k):
return { (distance, adjustedDistance) in
let t = (distance - startDistance) / (endDistance - startDistance)
let scale = startScale + (endScale - startScale) * (1 - exp(-k * t))
return Float(scale)
}
}
}
}

0 comments on commit 968b457

Please sign in to comment.