# Texturing a cube based on direction of normals

So I've been trying to find a way to texture a cube based on the location/direction of face normals so that any extruded geometry is automatically aligned perfectly with the texture.

With my limited knowledge on nodes, I got this to work somewhat, but it only works for faces pointing towards positive XYZ. I found another post here which fixes this by putting a math node set to absolute after the separate XYZ node. Unfortunately, this doesn't flip the texture properly for faces pointing in the negative directions, and it's the reason I'm making this post. To hopefully get some help to fix the mirroring issue for negative faces, or see if there's a better method for this altogether.

I'd like to keep all of this to only one material, and (if it's even possible), use only one image texture node to make it easier to swap textures.

This is what I have so far:

Current node setup:

• I think you may just need to slap a Vector Math > Absolute node immediately on the 'True Normal' input.... and you should really mix the vectors, not the images. Less evaluation, and can be grouped to apply to any image, more conveniently. Aug 24 '21 at 10:22
• Ahh! Box Mapping in the Image Texture node. Very neat. I get to learn something, too. I guess, strictly, you should post your edit as an answer instead, and accept it for yourself. :) Aug 25 '21 at 7:46

A fruitful search-term for this problem would be 'Triplanar Mapping'

This is a slightly whacky approach to triplanar mapping, trying to avoid complicated conditionals and rotations in the node tree, by snapping to [0, 1/3, and 2/3] in HSV 'Hue', to get the dominant axis of the normal, (which will be our W for any particular shading-point), and adding 1/3 and 2/3 to that, to get U and V at that point, all in Object Space.

It seems to work OK, but if you don't like it, you'll find many more conventional approaches out there.

So a user in this thread named Robin Betts informed me that this is called "triplanar mapping," which helped a TON in finding information online on how to accomplish this. I found a video here that outlines several methods with their own pros and cons. One of them used the "box" projection in the image texture node, which immediately showed promising results while fixing the texture flipping issue. The only problem was that the rotation for the Z faces were off by 90 degrees but it was nothing a few extra nodes couldn't fix. I'm sure I can do the same for the X and Y if needed. Thanks for the help.

The same person also said I can just mix the vectors directly with a mixRGB node, which I didn't know I could do...

EDIT: So the previous node setup that was here had a huge flaw in that some Z fighting texture glitches would begin to show and get exponentially worse the farther you moved from the world origin. I believe this was due to the way the separate XYZ node converted to a factor for the mixRGB. Adding a color ramp set to constant (with the white slider set to anything except 1) between the math node and the mixRGB seemed to fix the issue. I also went ahead and added the extra nodes necessary to make the textures for all sides of the cube rotatable:

new Solution (blender v2.93.1):

• Way better than mine :D Aug 25 '21 at 18:58

In one material you can have two shaders, and join them with a Mix shader. This will give you two materials in one. The two different shaders will show up where the faction value of the mix shader tell them to show up. I am not smart enough to say how you should manipulate the faction to get a 1 on the positive xyz sides. My 2 cents.