【问题标题】:SceneKit per vertex color每个顶点颜色的 SceneKit
【发布时间】:2015-12-25 14:47:18
【问题描述】:

我一直在玩 SceneKit,但我不知道如何创建每个顶点颜色的几何图形。

所以更准确地说,我想这样做:http://openglbook.com/chapter-2-vertices-and-shapes.html

如果不清楚请告诉我

谢谢。

【问题讨论】:

  • 我想我在这里link 发现了一些有趣的东西,SCNGeometrySource 中的示例只讨论了顶点位置、法线和 texCoord,但也有颜色的语义

标签: ios swift scenekit vertex


【解决方案1】:

浇注信息:

    sceneView = SCNView(frame: sceneContainer.bounds)
    sceneView.scene = SCNScene()
    sceneView.allowsCameraControl = true
    sceneView.autoenablesDefaultLighting = true
    sceneView.showsStatistics = true
    sceneView.backgroundColor = UIColor.darkGrayColor()

    self.sceneContainer.addSubview(sceneView)

    // Vertex
    let vertices: [SCNVector3] = [SCNVector3(0, 0, 0),
        SCNVector3(1, 0, 0),
        SCNVector3(0.5, 1, 0)]

    let vertexData = NSData(bytes: vertices, length: vertices.count * sizeof(SCNVector3))
    let vertexSource = SCNGeometrySource(data: vertexData, semantic: SCNGeometrySourceSemanticVertex, vectorCount: vertices.count, floatComponents: true, componentsPerVector: 3, bytesPerComponent: sizeof(Float), dataOffset: 0, dataStride: sizeof(SCNVector3))

    // Faces
    let indices: [Int32] = [0,1,2]

    let indexData = NSData(bytes: indices, length: sizeof(Int32) * indices.count)
    let indexElement = SCNGeometryElement(data: indexData, primitiveType: SCNGeometryPrimitiveType.Triangles, primitiveCount: indices.count / 3, bytesPerIndex: sizeof(CInt))

    // Normals
    let normals: [SCNVector3] = [SCNVector3(0, 0, 1),
        SCNVector3(0, 0, 1),
        SCNVector3(0, 0, 1)]

    let normalData = NSData(bytes: normals, length: sizeof(SCNVector3) * normals.count)
    let normalSource = SCNGeometrySource(data: normalData, semantic: SCNGeometrySourceSemanticNormal, vectorCount: normals.count, floatComponents: true, componentsPerVector: 3, bytesPerComponent: sizeof(Float), dataOffset: 0, dataStride: sizeof(SCNVector3))

    // Colors
    let colors: [SCNVector3] = [SCNVector3(1, 0, 0),
        SCNVector3(0, 1, 0),
        SCNVector3(0, 0, 1)]

    let colorData = NSData(bytes: colors, length: sizeof(SCNVector3) * colors.count)
    let colorSource = SCNGeometrySource(data: colorData, semantic: SCNGeometrySourceSemanticColor, vectorCount: colors.count, floatComponents: true, componentsPerVector: 3, bytesPerComponent: sizeof(Float), dataOffset: 0, dataStride: sizeof(SCNVector3))

    // Geometry
    let voxelGeometry = SCNGeometry(sources: [vertexSource, normalSource, colorSource], elements: [indexElement])
    let voxelMaterial = SCNMaterial()
    voxelMaterial.diffuse.contents = UIColor.whiteColor()
    voxelGeometry.materials = [voxelMaterial]

    voxelNode = SCNNode(geometry: voxelGeometry)
    voxelNode.position = SCNVector3(0, 0, 0)

    sceneView.scene?.rootNode.addChildNode(voxelNode)

【讨论】:

  • Swift 3 需要一些小的更新。 sizeof 被 MemoryLayout<___>.size 替换,语义参数可以是例如.color
  • 你也可以用 Swift 中的扩展函数隐藏很多低级的 C-ish 糟糕,例如 ``` extension SCNGeometrySource { 便利 init(data: [SCNVector3], semantic: SCNGeometrySource.Semantic) { let dataData = NSData(bytes: data, length: data.count * MemoryLayout.size) self.init(data: dataData as Data, semantic: semantic, vectorCount: data.count, usesFloatComponents: true, componentsPerVector: 3 , bytesPerComponent: MemoryLayout.size, dataOffset: 0, dataStride: MemoryLayout.size) } } ``
猜你喜欢
  • 2021-05-22
  • 1970-01-01
  • 2017-02-02
  • 1970-01-01
  • 2013-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多