hi

i wanna share some math part here i use in Shader - or code they help me for create some short and useful code

they are tested and always work so you can use them without you wanna understand the inner code algorithm

hi

i wanna share some math part here i use in Shader - or code they help me for create some short and useful code

they are tested and always work so you can use them without you wanna understand the inner code algorithm

4 Likes

negative or positive

if(x>0) return 1 else return -1;

(x > 0. ? 1. : -1)

or

( x / (abs(x) + 0.00001) )

2 Likes

circle path

position.x = sin(time)*circle_radius;

position.z = cos(time)*circle_radius;

2 Likes

rounded sguare

var a = Math.sin(i*3.14159265/180.);
var b = Math.cos(i*3.14159265/180.);

sphere.position.x = ( a / ( Math.abs(a) + 0.0001) ) * ( Math.pow(Math.abs(a),0.5))*3.;

sphere.position.z = ( b / ( Math.abs(b) + 0.0001) ) * ( Math.pow(Math.abs(b),0.5))*3.;

https://www.babylonjs-playground.com/#ZNQP8G#2

https://www.babylonjs-playground.com/#ZNQP8G#3

3 Likes

exact square edge path

```
var sq_seg = 100; // any edge segments
var p = {x:0.,y:0.,z:0.};
var i1 =time+sq_seg-1 ;
var i2 = Math.floor(i1/sq_seg);
var i3 = sq_seg- i1%sq_seg;
var i31 = 1.-(i3-1)*(1./sq_seg);
var i4 = 1.- 1./sq_seg*(i1%sq_seg)-1./sq_seg;
if(i2==0 ) p = {x:0.,y:0,z:0};
if(i2==1 ) p = {x:i31,y:0,z:0};
if(i2==2 ) p = {x:1 ,y:0,z:i31};
if(i2==3 ) p = {x:i4,y:0,z:1};
if(i2==4 ) p = {x:0,y:0.,z:1.*i4};
p.x -= 0.5;
p.z -= 0.5;
p.x *= edge_length;
p.z *= edge_length;
```

https://www.babylonjs-playground.com/#ZNQP8G#4

https://www.babylonjs-playground.com/#ZNQP8G#5

https://www.babylonjs-playground.com/#ZNQP8G#7

4 Likes

Why not rely on sign : sign - OpenGL 4 Reference Pages ?

I usually try the (x-xc) ^ 2 + (y-yc) ^ 2 - radius ^ 2 == 0 to prevent the expensive sin and cos when I can

For some others amazing ones: Inigo Quilez :: fractals, computer graphics, mathematics, shaders, demoscene and more

2 Likes

are you scared about this 2 beauty ( sin, cos ) ?

not scared but if it can be replaced by 2 add and mul, I dunno I am trying to keep it there

But agree it is a heavily premature optim

1 Like

i know you wanna help to me to continue some more math helpers

same as dance challenge on stage

3 Likes

the important math key

keep your base numbers between 0 - 1

you can do anything on the result

for example if you have the x between 0-1 you can use pow function and the result always stay in that range too

https://www.babylonjs-playground.com/#ZNQP8G#11

https://www.babylonjs-playground.com/#ZNQP8G#12

2 Likes

I had never heard about â€śbranchless programmingâ€ť until yet. Since javascript is quite slow this programming style could increase the performance considerably.

This is the trick :

2 Likes

the important stuff is you make it understandable

The geometryBuilder code use them i stat make them object oriented math function

i next few days i show how can detect wall without raycast only by matematical

2020-07-12-21-38-11

3 Likes

2 way for scale a big number between 0 - 1

- use y = 1.- ( a / ( x-a ));

it is start from 0 and never meet 1

a is the fixed number for control result scaling speed

https://www.babylonjs-playground.com/#ZNQP8G#18

2 . use y = log(x+1) / log(max+1.);

it is start from 0 and meet 1

max is the maximum x number you have

how can correct math frequency for â€ścosâ€ť function

problem : when we use math in shader we call any function for each pixel so GPU dont understand any before and after so for example when we use â€śCos( x * y )â€ť

that work fine in the small area

but that not work well in â€śCos(x*y*100)â€ť

nomaly that make a lot noise

you can use smoothStep function and mixing some other cos value for fix that

For complex applications it is advisable to rethink the often used functions and make them faster. QT has fast implementations of sine (qFastSin) and cosine (qFastCos) that uses look up table with interpolation. http://synthmaker.co.uk/forum/viewtopic.php?f=4&t=6457&st=0&sk=t&sd=a

You should also avoid divisions and multiply instead.

Found via

2 Likes