Hide texture via shader?

Is it possible to hide a texture map via a shader? I know that you can set the texture map to null in three.js, but I don’t want to do it that way. I assume I can just disable it in shader, but I’m not sure. I want to just display the vertex colors and toggle the texture off/on. I’m not sure if that is tied with the texture though and if it can be separated. Any info is appreciated.

Do you mind explaining in more detail why you don’t want to set the texture to null?

My application references the material’s map data to perform other operations, so if it gets set to null things won’t work correctly in other areas of my program. So my plan is to implement a uniform to control whether to render the texture map. That way, the material will continue to retain the map data and I’ll just be able to toggle whether it is rendered- this seems like the simplest solution to me.

example of one case in my application:

if(undoRedo.redolist[i].obj[o].children[0].material.map.image == tileSet.canvas[num])

So this is what I came up with:

THREE.MeshBasicMaterial.prototype.onBeforeCompile = function( shader ) {
	shader.uniforms.renderTexture = { value: true };
	
	shader.fragmentShader = 'uniform bool renderTexture;\n' + shader.fragmentShader;
	shader.fragmentShader = shader.fragmentShader.replace(
		'#include <map_fragment>',
		[
			`#ifdef USE_MAP`,
				`vec4 texelColor = texture2D( map, vUv );`,
				`texelColor = renderTexture ? mapTexelToLinear( texelColor ) : vec4( 1.0,1.0,1.0,1.0 );`,
				`diffuseColor *= texelColor;`,
			`#endif`
		].join( '\n' )
	);
	this.userData.shader = shader;	//add reference to shader in the material
};

It seems to work for my purposes. Though, if there is a better way, let me know.

1 Like