2015-09-28 194 views
4

我一直在玩SceneKit,但我不知道如何創建每個頂點顏色幾何。每個頂點顏色的SceneKit

所以更準確地說,我想這樣做:http://openglbook.com/chapter-2-vertices-and-shapes.html

讓我知道,如果現在還不清楚

感謝。

+1

我想我在這裏找到一些有趣的事情[鏈接](https://developer.apple.com/library/prerelease/ios/documentation/SceneKit/Reference/SceneKit_Constants/index。 html#// apple_ref/doc/constant_group/Geometry_Semantic_Identifiers),SCNGeometrySource中的例子只討論頂點位置,法線和texCoord,但顏色也有語義 – MonkeyDMat

回答

2

傾信息:

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) 
+0

Swift 3需要一些小的更新。 MemoryLayout <___>。尺寸和語義參數可以只是例如。顏色 – James

+0

您也可以隱藏了很多與擴展函數低電平C-ISH這場可怕的在夫特例如 ''' 延伸SCNGeometrySource { 方便的init(數據:[SCNVector3],語義:SCNGeometrySource.Semantic){ 讓dataData = NSData(字節: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,d​​ataStride:MemoryLayout .size) } } ''' – James