I tried simplest example to use .polygon type:
let vertices: [SCNVector3] = [
SCNVector3(-1, 0, 0),
SCNVector3( 0, 1, 0),
SCNVector3( 1, 0, 0)
]
let indices: [Int32] = [3, 0, 1, 2]
let indexData = NSData(bytes: indices,
length: MemoryLayout<Int32>.size * indices.count)
as Data
let element = SCNGeometryElement(data: indexData,
primitiveType: .polygon,
primitiveCount: 1,
bytesPerIndex: MemoryLayout<Int32>.size)
Now when I try simple vertex source as
let vertexSource = SCNGeometrySource(vertices: vertices)
All works well. However when I use NSData it does not work:
let data = NSData(bytes: vertices,
length: MemoryLayout<SCNVector3>.size * vertices.count)
as Data
let vertexSource = SCNGeometrySource(data: data,
semantic: .vertex,
vectorCount: vertices.count,
usesFloatComponents: true,
componentsPerVector: 3,
bytesPerComponent: MemoryLayout<CGFloat>.size,
dataOffset: 0,
dataStride: MemoryLayout<SCNVector3>.stride)
I need to use NSData approach later on but I want to understand what is wrong in this simplest example.
Any help is appreciated.
Just to complete it:
let geometry = SCNGeometry(sources: [vertexSource], elements: [element])
let node = SCNNode(geometry: geometry)
// to ignore orientation problem
node.geometry?.firstMaterial?.isDoubleSided = true
Post
Replies
Boosts
Views
Activity
I am trying to add into @Model this class:
@Model
class Test: Identifiable {
var id: Int
var tasks: [Task] = []
init(id: Int, tasks: [Task]) {
self.id = id
self.tasks = tasks
}
}
while Task is a simple struct
struct Task: Identifiable {
var id: String
var name: String
init(name: String) {
self.id = UUID().uuidString
self.name = name
}
}
And I am getting this 3 errors: Referencing instance method 'setValue(for:to:)' on 'Array' requires that 'Task' conform to 'PersistentModel' and Referencing instance method 'getValue(for:)' on 'Array' requires that 'Task' conform to 'PersistentModel' and Referencing instance method 'setValue(for:to:)' on 'Array' requires that 'Task' conform to 'PersistentModel' exactly for the line in the Test class:
var tasks: [Task] = []
which opens hidden code as following:
{
init(newValue) accesses (_$backingData) {
_$backingData.setValue(for: \.tasks, to: newValue)
}
get {
_$observationRegistrar.access(self, keyPath: \.tasks)
return self.getValue(for: \.tasks)
}
set {
_$observationRegistrar.withMutation(of: self, keyPath: \.tasks) {
self.setValue(for: \.tasks, to: newValue)
}
}
}
Is it not possible to have an array as part of the new @Model SwiftData approach?
Is there a simple fix for it that I cannot find out?