Fullscreen camera preview while using Metal to render?

I've been looking to use some filters on my live camera feed, and have been following guidance of the AVCamFilter sample project to use a MTKView to render these live.
Within this project, there is some code in PreviewMetalView.swift (in setupTransform()) which scales the X and Y dimensions of the preview down, such that the entire preview is visible on screen:

Code Block
if textureWidth > 0 && textureHeight > 0 {
switch textureRotation {
case .rotate0Degrees, .rotate180Degrees:
scaleX = Float(internalBounds.width / CGFloat(textureWidth))
scaleY = Float(internalBounds.height / CGFloat(textureHeight))
case .rotate90Degrees, .rotate270Degrees:
scaleX = Float(internalBounds.width / CGFloat(textureHeight))
scaleY = Float(internalBounds.height / CGFloat(textureWidth))
}
}
// Resize aspect ratio.
resizeAspect = min(scaleX, scaleY)
if scaleX < scaleY {
scaleY = scaleX / scaleY
scaleX = 1.0
} else {
scaleX = scaleY / scaleX
scaleY = 1.0
}


I am, instead, trying to scale the image such that it is zoomed in, filling the view ("Aspect Fill", think like the Snapchat camera view), but, as I'm not familiar with scaling textures for MetalKit to render, I'm not entirely sure how I can scale this such that part of the preview goes beyond the view bounds. What is the best approach to this (I'm imagine this is just some simply multiplication that is alluding me, but would love any help I can get)
Answered by in 616082022
To achieve the effect you're looking for, change the "if scaleX < scaleY" to if "scaleX > scaleY"; This is so the larger dimension gets scale=1, rather than the smaller dimension. With this change, the image should adjust to fill while preserving the aspect.
Accepted Answer
To achieve the effect you're looking for, change the "if scaleX < scaleY" to if "scaleX > scaleY"; This is so the larger dimension gets scale=1, rather than the smaller dimension. With this change, the image should adjust to fill while preserving the aspect.
Fullscreen camera preview while using Metal to render?
 
 
Q