Swift 3 中的 SCNGeometryElement 设置 [英] SCNGeometryElement setup in Swift 3

查看:13
本文介绍了Swift 3 中的 SCNGeometryElement 设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我硬着头皮开始将我的应用程序转换为 Swift 3.与往常一样,转换器仍有很多不足之处.在这种情况下,我不确定如何正确编码新版本.这是原文:

I bit the bullet and started converting my app to Swift 3. As always, the converter leaves very much to be desired. In this case, I'm not sure how to properly code the new version. Here is the original:

let indexes : [CInt] = [0,1,2,3]
let dat  = NSData(bytes: indexes, length: sizeofValue(indexes))
let ele = SCNGeometryElement(data:dat, primitiveType: .Triangles, primitiveCount: 2, bytesPerIndex: sizeof(Int))

在运行转换并编写新的 sizeof(谢谢)之后,我得到了这个:

After running the conversion and writing a new sizeof (thanks), I ended up with this:

let indexes : [CInt] = [0,1,2,3]
let dat  = Data(bytes: UnsafePointer<UInt8>(indexes), count: sizeof(indexes))
let ele = SCNGeometryElement(data:dat, primitiveType: .triangles, primitiveCount: 2, bytesPerIndex: MemoryLayout<Int>.size)

然而,这给了我(在 Data(bytes:length:) 调用上):

However, this gives me (on the Data(bytes:length:) call):

'init' 不可用:使用 'withMemoryRebound(to:capacity:_)' 暂时将内存视为另一种布局兼容类型.

'init' is unavailable: use 'withMemoryRebound(to:capacity:_)' to temporarily view memory as another layout-compatible type.

我在这里查看了一些主题,并阅读了涵盖此内容的发行说明,但我仍然对我在这里应该做什么感到困惑.

I've looked over a few threads here, and read the release notes that cover this, and I'm still baffled what I'm supposed to do here.

推荐答案

您固定了一个 sizeof 而不是另一个,并且您正在创建一个新的指针,其中不需要这样的指针 - 任何数组(给定正确的元素类型)可以传递给采用 C 风格指针的 API.您的代码的直接修复是:

You fixed one sizeof but not the other, and you're creating a new pointer where such is unnecessary — any array (given the right element type) can be passed to APIs that take C-style pointers. The direct fix for your code is then:

let indexes: [CInt] = [0,1,2,3]
let dat = Data(bytes: indexes, count: MemoryLayout<CInt>.size * indexes.count)
let ele = SCNGeometryElement(data:dat, primitiveType: .triangles, primitiveCount: 2, bytesPerIndex: MemoryLayout<CInt>.size)

(还要注意使您的 MemoryLayout 与它们描述的数据一致的修复.)

(Note also the fixes to make your MemoryLayouts consistent with the data they describe.)

然而,除非您需要额外的 Data 对象,为了使用指针的乐趣,或者为了描述元素的额外特性,您可以使用更简单的形式:

However, unless you have some need for the extra Data object, for fun with pointers, or for the extra specificity in describing your element, you can use the simpler form:

let indices: [UInt8] = [0,1,2,3] 
let element = SCNGeometryElement(indices: indices, primitiveType: .triangles)

这个泛型初始化器在进入的路上自动管理内存,推断数组的计数,并根据数组的计数和您指定的 primitiveType 推断 primitiveCount.

This generic initializer automatically manages memory on the way in, infers the count of the array, and infers the primitiveCount based on the count of the array and the primitiveType you specify.

(请注意,对于 .triangles 来说,一个包含四个索引的数组是一个不寻常的数字;要么你有一个三角形和一个未使用的索引,要么你实际上是指一个 .triangleStrip包含两个原语.)

(Note that an array of four indices is an unusual number for .triangles; either you have one triangle and one unused index, or you actually mean a .triangleStrip containing two primitives.)

这篇关于Swift 3 中的 SCNGeometryElement 设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆