在我的项目中,我以编程方式创建顶点,这意味着我正在填充 在 struct 后面加上我在应用程序中创建的数据.
struct Vertex{
var position: SIMD3<Float>
var color: SIMD4<Float>
var textCoors: SIMD2<Float>
}
我的对象很简单,它们是由两个三角形组成的平面.
我用以下代码创建纹理坐标;
PI是平面索引.我的对象由一定数量的平面组成.
L是平面转角位置的指标.
如果我的平面是正方形或长方形,它就可以很好地工作,它可以无缝地放置纹理.
但如果四边形的角坐标不对称.
(正方形和矩形是四边形的特例) 纹理正在变形.
我找不到好的解决办法,我有一些 idea ,比如和许多人一起做飞机 小方块,但感觉很糟糕,应该有更好的解决方案.
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
}