【问题标题】:Set Color of Custom Geometry in SceneKit在 SceneKit 中设置自定义几何图形的颜色
【发布时间】:2019-05-16 01:10:40
【问题描述】:

我已经从这篇文章成功地在 SceneKit 中构建并显示了一个自定义几何体(一个立方体)

Custom Geometry

现在,我想将每张脸的颜色设置为不同的颜色。我发现这篇文章应该这样做

Per Vertex Color

不幸的是,我似乎无法将立方体面的颜色设置为特定颜色。例如,如果我将所有顶点的颜色设置为SCNVector3(x:1,y:0,z:0),我希望所有面都是红色的,但相反,它们都是绿色的。将颜色设置为SCNVector3(x:0,y:1,z:0) 会将面变为黑色。这是相关代码

let colors = [SCNVector3](count:vertices.count, repeatedValue:SCNVector3(x:1, y:0, z:0))

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

let geometry = SCNGeometry(sources: [colorSource, vertexSource, normalSource], elements: [element])

// Create a node and assign our custom geometry
let node = SCNNode()

let material = SCNMaterial()
material.diffuse.contents = NSColor.whiteColor()
geometry.materials = [material]

node.geometry = geometry

有谁知道为什么它不起作用?

【问题讨论】:

    标签: scenekit vertex


    【解决方案1】:

    您必须为每个面创建不同颜色的材质。

    例如:

    let material1 = SCNMaterial()
    material1.diffuse.contents = NSColor.whiteColor()
    let material2 = SCNMaterial()
    material2.diffuse.contents = NSColor.greenColor()
    let material3 = SCNMaterial()
    material3.diffuse.contents = NSColor.redColor()
    let material4 = SCNMaterial()
    material4.diffuse.contents = NSColor.blackColor()
    let material5 = SCNMaterial()
    material5.diffuse.contents = NSColor.blueColor()
    let material6 = SCNMaterial()
    material6.diffuse.contents = NSColor.whiteColor()
    
    geometry.materials = [material1,material2,material3,material4,material5,material6]
    

    【讨论】:

    • 这适用于面相对较少的几何体,例如立方体。我想用数千个顶点构建更复杂的东西,并且需要设置每个顶点的颜色。
    猜你喜欢
    • 2016-04-01
    • 2016-04-26
    • 1970-01-01
    • 2016-03-12
    • 2021-08-01
    • 2019-03-02
    • 1970-01-01
    • 1970-01-01
    • 2016-11-25
    相关资源
    最近更新 更多