多边形为原始类型的SCNGeometry [英] SCNGeometry with polygon as primitiveType

查看:249
本文介绍了多边形为原始类型的SCNGeometry的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图弄清楚我如何创建一个以多边形为primaryType的SCNGeometry, 我的目标是将多边形节点添加为球面节点的子节点,并使它看起来像MKPolygon的地图工具包,

Trying to figure out how I create a SCNGeometry with polygon as primitiveType, My goal is to add polygon shaped node as a child of a sphere node, and make it look like MKPolygon for map kit, like in this example.

我当前的代码是:

//Take an arbitrary array of vectors
let vertices: [SCNVector3] = [
SCNVector3Make(-0.1304485, 0.551937, 0.8236193),
SCNVector3Make(0.01393811, 0.601815, 0.7985139),
SCNVector3Make(0.2971005, 0.5591929, 0.7739732),
SCNVector3Make(0.4516893, 0.5150381, 0.7285002),
SCNVector3Make(0.4629132, 0.4383712, 0.7704169),
SCNVector3Make(0.1333823, 0.5224985, 0.8421428),
SCNVector3Make(-0.1684743, 0.4694716, 0.8667254)]

//Does polygon shape require indices?
let indices: [Int] = [0,1,2,3,4,5,6]

let vertexSource = SCNGeometrySource(vertices: vertices)
let indexData = Data(bytes: indices, count: indices.count * MemoryLayout<Int>.size)

//Note!!! I get compiler error if primitiveCount is greater than 0
let element = SCNGeometryElement(data: indexData, primitiveType: .polygon, primitiveCount: 0, bytesPerIndex: MemoryLayout<Int>.size)
let geometry = SCNGeometry(sources: [vertexSource], elements: [element])

let material = SCNMaterial()
material.diffuse.contents = UIColor.purple.withAlphaComponent(0.75)
material.isDoubleSided = true
geometry.firstMaterial = material

let node = SCNNode(geometry: geometry)

像这样使用 SCNGeometryElement 时,我得到一个空节点.

When using SCNGeometryElement like this I get an empty node.

推荐答案

您有两个问题:

  1. SceneKit(和Metal)仅支持32位整数作为索引(). 因此,索引数组的类型必须为[Int32].

  1. SceneKit (and Metal) only support 32 bit integers as indices (source). So the type of your indices array needs to be [Int32].

SceneKit需要两个关于多边形的信息:多边形中的点数和顶点数组中的点的索引. 摘自Apple文档上的 SCNGeometryPrimitiveTypePolygon (仅存在于Objective-C):

SceneKit needs two pieces of information for polygons: The number of points in the polygon and the index of the point in the vertex array. From Apple's documentation on SCNGeometryPrimitiveTypePolygon (which only exists in Objective-C):

元素的data属性包含两个值序列.

The element’s data property holds two sequences of values.

  • 第一个序列的值等于几何元素的 originalCount值.此序列中的每个值指定多边形图元中的顶点数.例如,如果第一个序列为[5,3],则几何元素包含一个五边形,后跟一个三角形.
  • 其余数据是一系列顶点索引.第一个序列中的每个条目都在第二个序列中指定了相应数量的条目.例如,如果第一个序列包含值[5,3],则第二个序列包含五边形的五个索引,然后是三角形的三个索引.
  • The first sequence has a number of values equal to the geometry element’s primitiveCount value. Each value in this sequence specifies the number of vertices in a polygon primitive. For example, if the first sequence is [5, 3], the geometry element contains a pentagon followed by a triangle.
  • The rest of the data is a sequence of vertex indices. Each entry in the first sequence specifies a corresponding number of entries in the second sequence. For example, if the first sequence includes the values [5, 3], the second sequence includes five indices for the pentagon, followed by three indices for the triangle.

您需要将索引数组更改为:

You need to change your index array to:

let indices: [Int32] = [7, /* We have a polygon with seven points */,
                        0,1,2,3,4,5,6 /* The seven indices for our polygon */
                       ]

然后,将primitiveCount设置为1(我们要绘制一个多边形)并更改缓冲区的大小:

Then, set the primitiveCount to 1 (we have one polygon to draw) and change the size of the buffer:

let indexData = Data(bytes: indices, 
                     count: indices.count * MemoryLayout<Int32>.size)

// Now without runtime error
let element = SCNGeometryElement(data: indexData, 
                                 primitiveType: .polygon,
                                 primitiveCount: 1, 
                                 bytesPerIndex: MemoryLayout<Int32>.size)

这篇关于多边形为原始类型的SCNGeometry的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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