What is the correct way to put different textures on different parts of an object? I tried passing a variable from the vertex shader to tell the fragment shader whether or not I wanted part of an object textured. That way I could write another shader for the other parts of the object. Unfortunately, what I have creates the undesirable effect seen here.



vertex shader for house

&lt;script id="long_house-vs" type="x-shader/x-vertex"&gt;
    precision highp float;

    attribute vec3 vpos;
    attribute vec3 vnormal;
    attribute vec2 vtexCoords;

    varying vec3 outNormal;
    varying vec3 outPos;
    varying vec2 outTexCoords;
    // 1 if part of roof
    varying float roof;


    uniform mat4 view;
    uniform mat4 proj;
    uniform mat4 model;
    uniform vec3 lightdir;
    uniform vec3 long_housecolor;

    void main(void) {
        if(vpos.y &gt; 1.2) {
            roof = 1.0;
        } else {
            roof = 0.0;
        }
        gl_Position = proj * view * model * vec4(vpos, 1.0);
        outTexCoords = vtexCoords;
        outPos =  (view * model * vec4(vpos, 1.0)).xyz;
        outNormal = normalize(view * model * vec4(vnormal,0.0)).xyz;
    }
&lt;/script&gt;


fragment shader

&lt;script id="long_house-fs" type="x-shader/x-fragment"&gt;
    precision highp float;
    varying vec3 outNormal;
    varying vec3 outPos;
    varying vec2 outTexCoords;
    varying float roof;

    uniform vec3 lightdir;
    uniform vec3 long_housecolor;
    uniform sampler2D usampler;
    uniform sampler2D urooftext;

    void main(void) {
        float diffuse = .5 + .5*abs(dot(outNormal, lightdir));
        vec3 outColor = long_housecolor * diffuse;
        vec4 textureColor;
        if(roof == 1.0) {
            textureColor = texture2D(urooftext, outTexCoords);
        } else {
            discard;
        }
        gl_FragColor = vec4(textureColor.rgb * diffuse, textureColor.a);
    }
&lt;/script&gt;


When I rearrange the code a bit so that the bottom half of the house is textured instead of the top I get this



So what would be the correct way to write a shader that only affects part of an object?
In vertex, put  vPos as varying. Then instead of doing the height check in vertex, do it in fragment.

if(outVPos.y &gt; 1.2) 
{
   textureColor = texture2D(urooftext, outTexCoords);
} 
else 
{
   discard;
}


By the way, varying qualifier is deprecated and replaced with out in vertex and in in fragment.