每个顶点颜色的SceneKit [英] SceneKit per vertex color

查看:87
本文介绍了每个顶点颜色的SceneKit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用SceneKit,但是我不知道如何创建每个顶点的颜色几何.

I've been playing with SceneKit, but I can't figure how to create a per vertex color geometry.

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

So to be more precise, I would like to do this : http://openglbook.com/chapter-2-vertices-and-shapes.html

让我知道是否不清楚

谢谢.

推荐答案

倾倒信息:

    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)

这篇关于每个顶点颜色的SceneKit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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