ios 在Metal中设置纹理的UV坐标可编程地使纹理变形

zzoitvuj  于 11个月前  发布在  iOS
关注(0)|答案(1)|浏览(111)

在我的项目中,我以编程方式创建顶点,这意味着我用在应用程序中创建的数据填充下面的结构。

struct Vertex{
var position: SIMD3<Float>
var color: SIMD4<Float>
var textCoors: SIMD2<Float>
}

字符串
我的对象很简单,它们是由两个三角形组成的平面。
我正在用下面的代码创建纹理坐标;
pI是平面索引,我的对象由一定数量的平面组成。
l是平面的角位置的索引。
如果我的平面是正方形或长方形,它可以很好地无缝放置纹理。
x1c 0d1x的数据
但如果我的四边形角坐标不对称。



(正方形和矩形是四边形的特例)纹理正在变形。
我找不到很好的解决方案,我有一些想法,如使五月平面与许多小方块,但感觉蹩脚,应该有更好的解决方案。

func textureRip(pI: Int, l: Int, vertex: [[SIMD3<Float>]]) -> SIMD2<Float> {
        
        var textCoor0 = SIMD2<Float>()
        
        // We have this ratio because metalview coordinate system 2x2 unit size
        // from -1 to 1 for both x and y. Texture coordinate sistem is 1 unit wide and
        // unit height. (0 to 1). So metal view twice as much bigger.
        let ratio:Float = 2
        
        // We are adding 1 to metal view values because its values begins
        // from -1 so we are shifting its coordinate system to texture coordinate
        // system along wiht ratio.
        let x0  =  (vertex[pI][0].x + 1) / ratio
        let x   =  (vertex[pI][1].x + 1) / ratio
        
        // We are using "1 -" because texture coordinate sistem origin is
        // at the top left corner and metal view coordinate system at center.
        // in that case texture look upside down so we are turning it using "1-".
        let y0  = 1 - (vertex[pI][0].y + 1) / ratio
        let y   = 1 - (vertex[pI][2].y + 1) / ratio
        
        
        if l == 0 {
            
            textCoor0 = SIMD2<Float>(x0, y0)
            
        } else if l == 1 {
            
            textCoor0 = SIMD2<Float>(x, y0)
            
        } else if l == 2 {
            
            textCoor0 = SIMD2<Float>(x, y)
            
        } else {
            
            textCoor0 = SIMD2<Float>(x0, y)
            
        }
        
        
        return textCoor0
    }

k4ymrczo

k4ymrczo1#

嗯,我的错误,一般来说。
而不是使用四边形的四个角,我一直在使用它的两个角的数据,好像它将永远是正方形或矩形。
我修改了func,它没有优化,正如你所看到的,我只是在尝试金属,看看我能用它做什么。

func textureRip(pI: Int, l: Int, vertex: [[SIMD3<Float>]]) -> SIMD2<Float> {
        
        var textCoor = SIMD2<Float>()
        
        // We have this ratio because metalview coordinate system 2x2 unit size
        // from -1 to 1 for both x and y. Texture coordinate sistem is 1 unit wide and
        // unit height. (0 to 1). So metal view twice as much bigger.
        let ratio:Float = 2
        
        // We are adding 1 to metal view values because its values begins
        // from -1 so we are shifting its coordinate system to texture coordinate
        // system along wiht ratio.
        let x1  =  (vertex[pI][0].x + 1) / ratio
        let x2  =  (vertex[pI][1].x + 1) / ratio
        
        let x3  =  (vertex[pI][2].x + 1) / ratio
        let x4  =  (vertex[pI][3].x + 1) / ratio
        
         
        
        // We are using "1 -" because texture coordinate sistem origin is
        // at the top left corner and metal view coordinate system at center.
        // in that case texture look upside down so we are turning it using "1-".
        let y1  = 1 - (vertex[pI][0].y + 1) / ratio
        let y2  = 1 - (vertex[pI][1].y + 1) / ratio
        
        let y3  = 1 - (vertex[pI][2].y + 1) / ratio
        let y4  = 1 - (vertex[pI][3].y + 1) / ratio
        
        
        
        if l == 0 {
            
            textCoor = SIMD2<Float>(x1, y1)
            
        } else if l == 1 {
            
            textCoor = SIMD2<Float>(x2, y2)
            
        } else if l == 2 {
            
            textCoor = SIMD2<Float>(x3 , y3)
            
        } else {
            
            textCoor = SIMD2<Float>(x4, y4)
            
        }
        
        
        return textCoor
    }

字符串
可以看出,我的测试图像看起来很像纹理。每个四边形实际上是独立的对象,可以独立地移动。


的数据

相关问题