5
$\begingroup$

I need to change the color of the cube surface depending on the normal direction. For example, the surface of a cube should change color to red if its normal is directed upwards +/- 10 ° (on the Z axis). Using shader editor of course. Thank you.

$\endgroup$

2 Answers 2

10
$\begingroup$

This is a normal output of the texture coordinate:

enter image description here

X is red, Y - green, Z - blue

This is in the local space. If you have to get in world space, use vector transform node:

enter image description here

Then you can use Separate XYZ and Map Range to get some effects:

enter image description here

$\endgroup$
1
  • $\begingroup$ Just for the sake of completion, if it should be within the range of +/-10° the From Min value (and the From Max accordingly) has to be much higher, around 0.985 (surely there exists a formula somewhere to get it more accurate). And if you want a gradual increase in strength, why not set the From Max value to 1 to get the full range? If you just want to switch the strength between 0 and 1 I'd recommend adding a Greater Than > 0 node after the Map Range. $\endgroup$ Commented Jun 21, 2022 at 9:45
6
$\begingroup$

The Geometry > Normal output returns the shading-point normal in World Space.

The Math > Arccosine of the Dot-Product of that vector with (0,0,1) will give you the angle between the shaded normal and world 'up', in radians.

enter image description here

$\endgroup$
4
  • $\begingroup$ +1 Ah, that's the formula I meant in my comment above. And using the Geometry node instead of the Texture Coordinate node makes this setup work universal no matter if you rotate the mesh in Object Mode or Edit Mode without the need for the Vector Transform. @arachnoden I actually would choose this as the better answer because it gives you the more accurate result where you can easily specify the +/-10° or any other angle you like. $\endgroup$ Commented Jun 21, 2022 at 13:40
  • $\begingroup$ @GordonBrinkmann Cranitzs ' answer is just fine, IMO, and it was there first :). I wouldn't want to tread on its toes at all. This answer is more to your comment, above, I guess, than to arachnoden. $\endgroup$
    – Robin Betts
    Commented Jun 21, 2022 at 14:49
  • 1
    $\begingroup$ Crantisz' answer is fine for sure, and I think having a gradient can be very useful which is easily done with his setup. But for the sake of the question where it says "change to red" and where is asked for a certain angle range I still think your answer gives the more accurate result. However, I don't mind his answer being accepted, why should I? 😊 It's just my personal opinion or preference. $\endgroup$ Commented Jun 21, 2022 at 19:40
  • $\begingroup$ This is exactly what I needed. $\endgroup$
    – Unknow0059
    Commented Jan 2 at 7:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .