Scenekit SCNShadable accessing _surface.diffuseTex

2019-02-28 07:44发布

I'm experiencing odd behaviour when trying to extend a PBR material from scenekit. All i want to do is read a texture, map it using the first uv channel (same as normal). As soon as i mention _surface.diffuseTexcoord, _surface.diffuse seems to turn to white. It doesn't seem to be constant (_output.rgb = vec3(1.)) but rather color white is passed through the lighting pipeline.

let myShader = "#pragma arguments\n" +
    "sampler uMaskTex;\n" +
    "uniform sampler2D uMaskTex;\n" +

    "#pragma body\n" +

    //"vec2 myDuv = _surface.diffuseTexcoord;\n" //paints the object white

    OR

    //"vec3 mask = texture2D( uMaskTex, _surface.diffuseTexcoord ).xyz;\n" //paints the object white

    OR

    "vec3 mask = texture2D( uMaskTex, vec2(1.) ).xyz;\n" //does not paint the object white

I do not understand at all why are these channels tied to "ambient", "diffuse", "specular" etc, when they exist in the vertex shader at _geometry.texcoords[MY_CHANNEL].

My attempt to transfer _geometry.texcoords[0] via a varying vec2 v_my_abstract_uv_channel_not_diffuse_necesserily ended in failure.

edit

After doing some voodoo magic i managed to get this thing working the way i want to but still have no idea what is causing this behaviour.

Note, i do not have a texture in the diffuse slot for the material, it is set to constant color. Thought that something might be getting filled with garbage, but oddly, _surface.diffuseTexcoord does contain values and they are correct. It's just that any mention of it causes everything to turn white. I am however using a normal texture, assigned through the gui.

I've guessed that there is a corresponding _surface.normalTexcoord even though its not documented and this seems to have worked. Even though it contains the same values, it doesn't cause the material to go white.

"#pragma arguments\n" +
"sampler uMaskTex;\n" +
"uniform sampler2D uMaskTex;\n" +
//"varying vec2 vTexCoord0;\n" + //varyings don't work :( , 

    "#pragma body\n" + 
    "vec3 mask = texture2D(uMaskTex,_surface.diffuseTexcoord).xyz;\n" + //does not work, even though i don't do anything with this variable, it affects _surface.diffuse

    "_output.color.rgb = vec3(_surface.diffuseTexcoord,1.);\n" + //actually contains values, not garbage as i thought

    "_output.color.rgb = _surface.diffuse.rgb;\n"; //does not work, this outputs white, even though a different color is set

    "_output.color.rgb = mask;\n"; //i do however get the texture to map correctly, i do see the texture with _surface.diffuseTexcoord lookup... ?!

On the other hand:

    "#pragma body\n" + 
    "vec3 mask = texture2D(uMaskTex,_surface.normalTexcoord).xyz;\n" + //works!

    //works! even though there is no mention of this in the documentation. 
    //Gives exactly the same values as _surface.diffuseTexcoord
    "_output.color.rgb = vec3( _surface.normalTexcoord , 1.);\n" + 

    "_output.color.rgb = _surface.diffuse.rgb;\n"; //works! no gremlins, doesn't turn to white, shows the color from gui!

0条回答
登录 后发表回答