Search code examples
shaderunreal-engine4

UE4 - Why in shaders it seems that cosine() seems to return 1 for natural numbers


I need help understanding cosine function behavior in UE4 shaders.

I have such simple shader:

Simple line shader

It renders a line at each cellsize step while i would expect it to render a line roughly each 2pi * cellsize.

As you can (hopefuly) see i simply feed (world position x)/cellsize value to cosine func and take result as an output color.

The result im getting is:

enter image description here

The balls are just for reference. Blue one has world coords 5300,0,0 Red one has 5500,0,0

Shader is applied to a plane.

It is clearly visible that shader generated lines are perfectly under balls so it seems that cosine function is generating 1 (so efectively white color in my example) every cellsize step (which is 100 in my example).

I've deliberately chosen x=5500 because cos(5500/100) equals ~0 so there should actually be black under red ball.

The question is, can anyone explain how come that UE4 cosine func in shader seem to generate 1 for natural numbers?

This puts me in disbelief i can rely on any math class ever taken when trying to apply them to UE4 ;]

Any help is greatly appreciated.

Thanks


Solution

  • If you look at the cosine and sine material graph nodes, they have a 'period' parameter which defaults to 1.0. This would make the period of the cosine 1.0 and not 2 PI as you'd expect. You can modify it to give you the result you expect.