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
<script id="long_house-vs" type="x-shader/x-vertex">
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 > 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;
}
</script>
fragment shader
<script id="long_house-fs" type="x-shader/x-fragment">
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);
}
</script>
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?

