I'm using Blender 2.8 and trying to get a grasp on procedural textures. For fun, I was following through the list here at https://thndl.com/square-shaped-shaders.html and now I'm stuck.

Equation I'm trying to reproduce:

 float a=atan(c.x,c.y);

 f=vec4(step(.5,cos( floor(a*.636+.5)* 1.57-a)*length(c.xy)))

Result = A Square

I have a feeling it has to do with the Polar Coordinates, but I'm not sure where considering I believe the math is correct everywhere else.

Can someone take a look and see where the mistake is and perhaps explain though it if possible?


Shader Node Setup: enter image description here (Length = sqrt(x^2 + y^2)

The Result:

enter image description here


I'm not sure what you've actually got wrong with your setup but I've tried to replicate it by entering your math into a Node Expressions expression (see https://baldingwizard.wixsite.com/blog/node-expressions) - which builds the nodes directly from the text.

This produces the following result :


The generated 'inner' nodes for the 'f' function are :

inner nodes

And here's the Blend file (Blender 2.8) for reference

EDIT: Actually, I think I've spotted the error. Your nodes actually perform the calculation :

f = cos(floor(a * 0.636+0.5)*(1.57-a)) * dist

ie, Instead of multiplying by 1.57 and then subtracting 'a', you're nodes subtract a from 1.57 and then multiply - note the brackets I added around '1.57-a'). Here's the amended (wrong) calculation, replicating your result :

incorrect calculation

  • $\begingroup$ Hehe... just as i finished building the tree by hand, you nipped in before me! That'll teach me to use your kit.... Can I add 2 recommendations? $\endgroup$ Oct 6 '19 at 9:28
  • $\begingroup$ @RobinBetts Haha - sorry about that! :-) Sure can - always open to improvement. $\endgroup$ Oct 6 '19 at 9:30
  • $\begingroup$ 1. It doesn't make a difference here, but the GLSL call is of the [-pi,pi] overload of atan(), so Arctan2 node would be a better choice, further down the page the OP is looking at. 2. Blender 2.81 provides a better Vector Math node for following GLSL whole-vector operations.. $\endgroup$ Oct 6 '19 at 9:34
  • $\begingroup$ @RobinBetts Thanks. The addon does actually support atan2 - yes, I probably should have used it (but as you say, not relevant here since it’s a square). I must admit to not really understanding the ‘vec4’ in the original expression - is that what you mean by ‘whole-vector’ operations? What’s that doing in this case? $\endgroup$ Oct 6 '19 at 9:41
  • 1
    $\begingroup$ @RichSedman: Thanks so much! I made the adjustment and it turned out as it should. Also thanks for the link above! I'll be using that from now on to test. $\endgroup$
    – Podis
    Oct 6 '19 at 11:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.