AdiTheExplorer
AdiTheExplorer

Reputation: 259

Texture coordinates for custom geometry in Scenekit

I am trying to texture the a custom plane shape I created in scenekit on iOS9. I need the texture to spread out and stretch over the entire surface. I have a vertex and fragment shader on an SCNprogram. But it seems I am doing something wrong and fiddling with this all day, I have come to the conclusion that somehow the Texture coordinates I specify are not correct.

My question is, when creating a geometry, does SceneKit create the texture coordinates automatically so when I specify CGpoint(0,0) for one corner of the texture, it automatically maps it to the bottom left corner of the geometry? Or do I have to manually specify the texture coordinates/uv for the geometry I create?

This problem is a part of a larger question posted here : Custom Shader SCNProgram iOS 9 Scenekit

Please help, I have spent all day tinkering but no success :(

EDIT:Based on Locks response below, I have changed the frag shader to help me troubleshoot it. What it gives I believe makes sense?? See pic below. There does seem to be an issue with my UVs. But why? I can't understand.

enter image description here

Upvotes: 3

Views: 3600

Answers (1)

lock
lock

Reputation: 2897

Yes, when creating custom geometry you will also need to create UV coordinates for each vertex. This isn't too much on what you'll already be doing to create custom geometry, just fit the following bits in.

var uvList:[vector_float2] = []

//fill UV list with texture coords

let uvData = NSData(bytes: uvList, length: uvList.count * sizeof(vector_float2))
let uvSource = SCNGeometrySource(data: uvData,
    semantic: SCNGeometrySourceSemanticTexcoord,
    vectorCount: uvList.count,
    floatComponents: true,
    componentsPerVector: 2,
    bytesPerComponent: sizeof(Float),
    dataOffset: 0,
    dataStride: sizeof(vector_float2))

let geo = SCNGeometry(
    sources: [vertexSource, normalSource, colourSource, uvSource], 
    elements: [indexElement])

Then to check if your texture coordinates are correct, I'd change your fragment shader to display them. Something like the following (been a while since I've written an OpenGL shader).

void main() {  
    gl_FragColor = vec4(texCoord.x, texCoord.y, 0, 1);  
}

The red and green shading of your object should then inform you what texture coordinates are where on your model. If that checks out ok, then switch back to reading data from the texture.

Upvotes: 3

Related Questions