在我的项目中,我以编程方式创建顶点,这意味着我正在填充 在 struct 后面加上我在应用程序中创建的数据.

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

我的对象很简单,它们是由两个三角形组成的平面.

我用以下代码创建纹理坐标;

PI是平面索引.我的对象由一定数量的平面组成.

L是平面转角位置的指标.

如果我的平面是正方形或长方形,它就可以很好地工作,它可以无缝地放置纹理.

enter image description here

但如果四边形的角坐标不对称.

enter image description here

(正方形和矩形是四边形的特例) 纹理正在变形.

我找不到好的解决办法,我有一些 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
    }

推荐答案

嗯,我的错误,一般来说.

我没有使用四边形的四角,而是使用了四边形的两个角数据,就好像它总是正方形或矩形一样.

我修改了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
    }

正如可以看到的那样,我的测试图像看起来质感很好. 每一个四边形实际上都是独立的物体,可以独立移动.

enter image description here

Ios相关问答推荐

将图案UI视图动画化以模拟进度条

iOS中的分段拾取器—手柄点击已 Select 的项目

UIBezierPath包含:方法不检测到厚度大于1像素的线上的touch

无法在所有窗口上方显示图像

以编程方式更改VC&39;S导航栏按钮的属性,以响应另一个VC中的按钮按下

SwiftUI Divider过大了其父视图

SWIFT-仅解码感兴趣的元素

更改初始控制器无效

如何解决错误 HE0004:无法在 Visual Studio 2022 中加载框架ContentDeliveryServices

核心媒体基础知识,了解 CMSampleBuffer、CMBlockBuffer 和 CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer

CGPath intersection(_:using:) 和朋友(iOS 16 中的新功能)坏了?

iOS应用程序崩溃,必需条件为false:isformatsamplerateandchannelcountvalid(format)

如何擦除 Swift 中的 Core Graphics Context 绘图?

SwiftUI - 使用切换switch 切换键盘类型(默认/数字)?

Xcode 14 需要为 Pod Bundles Select 开发团队

-1103 错误域=NSURLErrorDomain 代码=-1103资源超过最大大小iOS 13

如何判断 `NSManagedObject` 是否已被删除?

iOS:使用 UIView 的 'drawRect:' 与其层的委托 'drawLayer:inContext:'

使用 swift 3 在 UIView 上添加阴影

具有两种不同字体大小的 NSAttributedString 示例?