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

2 Likes

negative or positive

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

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

or

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

circle path

position.x = sin(time)*circle_radius;

position.z = cos(time)*circle_radius;

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

1 Like

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

1 Like

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

1 Like

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

2 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

1 Like