[英]How to resize/scale an MTLTexture
我有一個 MTLTexture,它是它出現的 MTLView 的大小。但是,我正在將紋理寫入AVAssetWriterInputPixelBufferAdaptor
以記錄需要紋理為標准視頻大小(即 1280x720)的視頻。
目前我正在使用MTLRegionMake2D
指定紋理中要提取的區域。 在寬度較長的手機上,裁剪的效果並不明顯。 但是在形狀和尺寸更方形的 iPad 上,裁剪很明顯。 理想情況下,我會先將紋理調整為與視頻相同的寬度或高度,然后裁剪其余部分(而不是僅僅裁剪)。
調整 MTLTexture 大小的適當方法是什么?
示例代碼:
guard let pixelBufferPool = assetWriterPixelBufferInput.pixelBufferPool else { return }
var maybePixelBuffer: CVPixelBuffer? = nil
let status = CVPixelBufferPoolCreatePixelBuffer(nil, pixelBufferPool, &maybePixelBuffer)
if status != kCVReturnSuccess { return }
guard let pixelBuffer = maybePixelBuffer else { return }
CVPixelBufferLockBaseAddress(pixelBuffer, [])
let pixelBufferBytes = CVPixelBufferGetBaseAddress(pixelBuffer)!
let bytesPerRow = CVPixelBufferGetBytesPerRow(pixelBuffer)
let region = MTLRegionMake2D(0, 0, Int(videoSize.width), Int(videoSize.height))
texture.getBytes(pixelBufferBytes, bytesPerRow: bytesPerRow, from: region, mipmapLevel: 0)
let frameTime = CACurrentMediaTime()
let presentationTime = CMTimeMakeWithSeconds(frameTime, 240)
assetWriterPixelBufferInput.append(pixelBuffer, withPresentationTime: presentationTime)
CVPixelBufferUnlockBaseAddress(pixelBuffer, [])
最后我使用MPSImageLanczosScale
來縮放紋理,見:
蘋果文檔: https://developer.apple.com/documentation/metalperformanceshaders/mpsimagelanczosscale
示例: 裁剪和縮放 MTLTexture
重新縮放后,我仍然需要使用一個區域來裁剪剩余部分。 我有一些邏輯來檢測縱向/橫向,以及平板電腦或手機來確定合適的作物。 在我的情況下(對於縱向),我選擇垂直居中用於裁剪頂部/底部的瘦設備(手機),以及水平居中裁剪左/右的寬設備(平板電腦)。 對景觀有類似的邏輯,但相反。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.