Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Swift中为SCNGeometrySource构建数据_Swift_Scenekit - Fatal编程技术网

如何在Swift中为SCNGeometrySource构建数据

如何在Swift中为SCNGeometrySource构建数据,swift,scenekit,Swift,Scenekit,我试图在swift中建立一个SCNGeometrySource 我想使用SCNGeometrySource(data:data,semantic:SCNGeometrySource.semantic,vectorCount:Int,usesFloatComponents:Bool,componentsPerVector:Int,bytespecomponent:Int,dataOffset:Int,dataStride:Int)初始值设定项,因为我已经有一个缓冲区,提供顶点位置、法线和颜色,或者

我试图在swift中建立一个SCNGeometrySource

我想使用
SCNGeometrySource(data:data,semantic:SCNGeometrySource.semantic,vectorCount:Int,usesFloatComponents:Bool,componentsPerVector:Int,bytespecomponent:Int,dataOffset:Int,dataStride:Int)
初始值设定项,因为我已经有一个缓冲区,提供顶点位置、法线和颜色,或者至少,我可以建造它

但是如何将缓冲区转换为数据类型(对于数据参数)

使用
数据(bytesNoCopy:buffer as!unsafemeutablerawpointer…)

我也尝试过使用
Data.append(xxx)
函数,但没有成功,但我无法找到将较大缓冲区的地址强制转换为任何类型的UnsafePointer(这也意味着大缓冲区的附加副本)

那怎么办

编辑:“大缓冲区”是

struct VertexInfo {
    var x: Float
    var y: Float
    var z: Float
    var nx: Float
    var ny: Float
    var nz: Float
    var a: Float
    var r: Float
    var g: Float
    var b: Float
}

我可以在任何类型下提供它,但目前,我使用
UnsafeMutableBufferPointer
构建它。这就是我解决问题的方法:我使用
UnsafeMutableRawPointer
直接使用
malloc()
分配“大缓冲区”,并使用
数据构建
数据(bytesnopy:length:freeWhenDone:)
这并不意味着数据的另一个副本

现在,为了填充“大缓冲区”,我使用了一个
unsafemeutablepointer
来简化操作,但秘密是使用
bindMemory(to:capacity:)
使unsafemeutablepointer指向大缓冲区

这样,UnsafemtableRawPointer和UnsafemtablePointer实际指向同一个缓冲区。UnsafemtablePointer可用于轻松使用数据填充缓冲区,UnsafemtableRawPointer可轻松用于创建数据

// Assuming VertexInfo type holds the information for a vertex
// Assuming we have vectorMaxCount as the number of vertices we want

// Compute the size of the buffer to allocate
var size       = vectorMaxCount * MemoryLayout<VertexInfo>.stride
// Allocate the buffer as a UnsafeMutableRawPointer
var buffer     = malloc(size)
// Create a UnsafeMutablePointer<VertexInfo> that points to the allocated buffer
var vertexList = buffer!.bindMemory(to: VertexInfo.self, capacity: vectorMaxCount)

// Fill the buffer with the needed data
for index in 0..< vectorMaxCount {
    let vertex = // Compute here your vertex info as a VertexInfo
    // Easily fill the buffer thanks to the UnsafeMutablePointer<VertexInfo>
    vertexList[index] = vertex
}

// Create the Data thanks to the UnsafeMutableRawPointer
let data = NSData(bytesNoCopy: buffer, length: size, freeWhenDone: true) as Data
//假设VertexInfo类型保存顶点的信息
//假设我们有vectorMaxCount作为我们想要的顶点数
//计算要分配的缓冲区的大小
变量大小=vectorMaxCount*MemoryLayout.stride
//将缓冲区分配为UnsafemeutableRawPointer
var buffer=malloc(大小)
//创建指向已分配缓冲区的Unsafemeutablepointer
var vertexList=buffer!.bindMemory(收件人:VertexInfo.self,容量:vectorMaxCount)
//用所需的数据填充缓冲区
对于0..
注意,由于参数
freeWhenDone:true
的作用,缓冲区的所有权被赋予了数据


这是非常通用的,可用于构建任何压缩的数据缓冲区,而不仅仅用于SceneKit。

缓冲区的类型是什么?
?问题中的编辑