I am struggling to find any clear information on how to access the properties of a material inside a metal shader, specifically the diffuse color.

My geometry sources are defined with vertices, normals and texture coordinates:

let sources = [SCNGeometrySource(vertices: vertices),
                   SCNGeometrySource(normals: normals),
                   SCNGeometrySource(textureCoordinates: textureCoordinates)]

let elements = meshIndices.map { SCNGeometryElement(indices: $0, primitiveType: .triangles) }

self.init(sources: sources, elements: elements)

Inside my shader I can access the vertex position and normal using the semantic attributes:

typedef struct {

    float3 position [[ attribute(SCNVertexSemanticPosition) ]];
    float3 normal [[ attribute(SCNVertexSemanticNormal) ]];

} Vertex;

Whilst SCNVertexSemanticColor is a valid attribute, these are not defined on a per vertex basis in my case with the color instead being derived from the diffuse property of the material attached to the shader program.

Is there a way to access the properties of the material attached to my geometry? I would assume that creating a struct and binding it to an input attribute is the correct way forward but I am unsure how these are surfaced to the shader.

typedef struct {

    float3 diffuse [[ materials? ]];//how to reference material.diffuse.contents?

} Material;

I assume that these properties must be available to the shader as everything is rendered as expected if you leave SceneKit to do its own thing. Do I need to bind these properties as uniforms myself or is there a specific argument I can use to access them?


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.