gpt4 book ai didi

ios - 每个顶点颜色的 SceneKit

转载 作者:搜寻专家 更新时间:2023-10-30 22:15:15 24 4
gpt4 key购买 nike

我一直在玩 SceneKit,但我不知道如何创建逐顶点颜色几何体。

所以更准确地说,我想这样做: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)

关于ios - 每个顶点颜色的 SceneKit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32821653/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com