【问题标题】:How to solve SceneKit double not supported error?如何解决 SceneKit 双不支持错误?
【发布时间】:2015-01-09 12:39:37
【问题描述】:

过去几天我一直在研究适用于 iOS 的 SceneKit。我在尝试创建自定义几何图形时遇到了一个问题。每当我尝试显示几何图形时,它都不会绘制,并在运行时显示此错误。

SceneKit:错误,C3DRendererContextSetupResidentMeshSourceAtLocation - 不支持双精度

我创建了一个针对 iOS 的 Playground 来测试一个更简单的自定义几何示例,并查看了 this question 关于使用 swift vs Objective c 自定义几何的信息。

我使用目标 c 尝试了另一个项目,但仍然收到相同的错误消息。

在操场或整个项目上定位桌面时不会出现该错误,并且几何图形绘制正确。只有在针对 iOS 时才会出现错误消息。

import SceneKit
import QuartzCore   // for the basic animation
import XCPlayground // for the live preview

// create a scene view with an empty scene
var sceneView = SCNView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
var scene = SCNScene()
sceneView.scene = scene

// start a live preview of that view
XCPShowView("The Scene View", sceneView)

// default lighting
sceneView.autoenablesDefaultLighting = true

// a camera
var camera = SCNCamera()
var cameraNode = SCNNode()
cameraNode.camera = camera
cameraNode.position = SCNVector3(x: 0, y: 0, z: 10)
scene.rootNode.addChildNode(cameraNode)

// create geometry
var verts = [SCNVector3(x: 0,y: 0,z: 0),SCNVector3(x: 1,y: 0,z: 0),SCNVector3(x: 0,y: 1,z: 0)]

let src = SCNGeometrySource(vertices: &verts, count: 3)
let indexes: [CInt] = [0, 1, 2]

let dat  = NSData(
  bytes: indexes,
  length: sizeof(CInt) * countElements(indexes)
)
let ele = SCNGeometryElement(
  data: dat,
  primitiveType: .Triangles,
  primitiveCount: 1,
  bytesPerIndex: sizeof(CInt)
)
let geo = SCNGeometry(sources: [src], elements: [ele])

let nd = SCNNode(geometry: geo)
scene.rootNode.addChildNode(nd)

这是我在操场上用来绘制三角形的代码。定位桌面时使用相同的代码。

如何解决这个问题并显示 iOS 的几何图形?

【问题讨论】:

  • 你应该分享构建几何的代码

标签: ios swift scenekit


【解决方案1】:

我通过更改创建几何源的方式解决了错误。

按照this question's 创建几何源的方法,错误得到修复,三角形绘制正确。

我相信解决方案是在使用

+ (instancetype)geometrySourceWithData:(NSData *)data
                              semantic:(NSString *)semantic
                           vectorCount:(NSInteger)vectorCount
                       floatComponents:(BOOL)floatComponents
                   componentsPerVector:(NSInteger)componentsPerVector
                     bytesPerComponent:(NSInteger)bytesPerComponent
                            dataOffset:(NSInteger)offset
                            dataStride:(NSInteger)stride

而不是

+ (instancetype)geometrySourceWithVertices:(const SCNVector3 *)vertices
                                     count:(NSInteger)count

因为它指定我使用的是浮点组件而不是双精度。

【讨论】:

  • 谢谢 - 这解决了我的问题。 (更简单的调用在我尝试过的所有设备上都可以正常工作,除了 iPhone 6+,它似乎用世界原点替换了一些顶点。)
  • 有点晚了,但是您是否尝试过在场景中添加大量节点?超过200? stackoverflow.com/questions/38941570/…
  • 你能把创建几何的全部代码贴出来吗?
【解决方案2】:

我的猜测是SCNVector3 定义为CGFloat 用于桌面(可以是32 位或64 位,具体取决于主机)和Float 用于iOS 设备(iOS 模拟器平台)是一个问题(这是在 Playground 中以 iOS 为目标时所得到的)既不像设备也不像 OS X。Filing a bug with Apple 是个好主意。

与此同时,一个好的解决方法可能是使用更详细的初始化程序(以 init(data:semantic:... 开头)来创建几何源。

【讨论】:

    【解决方案3】:

    更准确地说,它对我有用的是:

    struct FloatPoint {
      var x: Float
      var y: Float
    }
    let textCoords = [FloatPoint]()
    ... fill it
    
    let textureData = NSData(bytes: textCoords, length: textCoords.count * sizeof(FloatPoint.self))
    let textSource = SCNGeometrySource(
      data: textureData as Data,
      semantic: SCNGeometrySourceSemanticTexcoord,
      vectorCount: textCoords.count,
      floatComponents: true,
      componentsPerVector: 2,
      bytesPerComponent: sizeof(Float.self),
      dataOffset: 0,
      dataStride: sizeof(FloatPoint.self)
    )
    

    我也用苹果填了一个但是

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-18
      • 2021-10-02
      • 1970-01-01
      • 2021-02-14
      相关资源
      最近更新 更多