Community
Arnold General Rendering Forum
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

rombotools::freebie::mathparser

4 REPLIES 4
Reply
Message 1 of 5
maxtarpini
696 Views, 4 Replies

rombotools::freebie::mathparser

this is a math expression parser.. ie. you can simply input a basic math expression and use a variable 'x' to source the connected input. This unleashes some tremendous power if you know what to do with math. So instead to have to use many math nodes you can use one to rule most of them.

Current operators : + - / * ^ %
Current functions : sin, cos, tan, floor, fract
Parentheses : ()

 

For example : floor(x*12)%2 , where x is the floating input node (U/V coord) and % is the modulo operator

 

https://www.rombo.tools/downloads/romboMathParser.zip

 

rombomathparser_app.png

4 REPLIES 4
Message 2 of 5
maxtarpini
in reply to: maxtarpini

Updated to three inputs and so three variables : x,y and z.

Added following ops : (bitwise) & | << >> 

Added following fncs : asin acos atan sinh cosh tanh exp floor ceil fract sqrt log log2 logn inv(ert) comp(lement) abs

Added : verbosity for incorrect expressions, MTD file for DCCs

 

Remarkably, this doesn't work : 3+(-2) .. meaning, I don't plan to fix it 🙂

 

We can now re-do the previous example with just one romboMathParser.

 

Screenshot 2024-07-10 171810.png



Btw, you can have any number of nested parentheses ..
like with the following expression that will draw a diamond pattern. 

rombomathparser_tris.png

 

This simple expression instead will return the luminance of a color.
In Maya, romboMathParser is under Utility/Math.
No need for parentheses here because the order of operations is exploited.

Screenshot 2024-07-10 230840.png

Message 3 of 5
maxtarpini
in reply to: maxtarpini

Added following fncs : rnd(seed) .. generates random numbers in 0-1

Added support for two-arguments fncs : max, min, atanz (atan2), mod, pow, step

 

Now with two-args fncs we can do more interesting stuff easily.

rombomathparse_twoargsfnc.png


The 'sin' thing with magic numbers generates random numbers, the 'fract' ensures they are in 0-1.
Or try this exp 'fract(fract(p * 0.1031) * (p * 33.33))' which avoids 'sin'.
We could have used also just the 'rnd' fnc.

 

Below with both X and Y. ScreenSpaceX/Y is in Arnold math node : state_float->imagespaceU/V.
Thickness node is used to tweak how thick are the lines without touching the expression.


rombomathparse_twoargsfncs.png


This last example would involve at least 12 nodes and all the pain to keep track of that. Btw, romboMathParser doesn't need to compile anything and does not embed full-fledged scripting language like Python, Lua or OSL because does everything inline based on a simple and effective math parser algorithm. I provide some initial source code here : https://github.com/RomboDev/Miscellaneous/tree/master/ShuntingYard%20Algorithm 

 

Message 4 of 5
maxtarpini
in reply to: maxtarpini

Addded fncs :
- radtodeg -> converts radians to degrees
- degtorad -> converts degrees to radians
- union -> perlin boolean op -> ((a+b) - (a*b))
- diff -> perlin boolean op -> ((a) - (a*b))

Added 3-args fncs :
- clamp -> constrains x to lie between a and b ..(x,a,b)
- lerp -> interpolates linearly x between a and b .. (a,b,x)
- exerp -> exponential interpolation
- logerp -> logarithmic interpolation
- sstep -> smoothstep function .. (a,b,x)
- pulse -> step(edge0, x) - step(edge1, x);
- pulset -> pulse (edge, period, mod(x,period));

Added constants :
- PI -> π -> 3.1459...
- E -> euler -> 2.7182...
- GR -> goldenratio -> 1.6180...

Added globals :
all shading float/int globals are available.
Name convention is : SG(shading globals) + uppercase variable name, ie:
SGU -> float u .. U surface parameter
SGDUDX -> float dudx .. U derivative wrt screen X-axis
SGSC -> uint8_t sc .. type of shading context

 

Added : README file


Now, to test for example constants and new fncs .. we know that Pi is the ratio of a circle's circumference to its diameter .. so if you input radtodeg(PI) .. and assign the shader to a cube .. go to the Pixel tab of your Arnold IPR view .. and put the mouse cursor over the rendered cube .. you should see 180 under Lum (remember that what you see in the IPR is not what you source from the pixel which is linear raw.. so values are not altered).


With globals instead we can re-write the previous exps without having to leave mathParser .. for example following :
(floor(SGU*12-SGV*12) + floor(SGU*12+SGV*12)) %2
does the same as above .. where SGU and SGV are the previous state_float->U and state_float->V connected to x and y slots.


Eventually a bit more involved example where we design some antialised circles.

 

rombomathparser_circles.png

Message 5 of 5
maxtarpini
in reply to: maxtarpini

Re-engineered the parser to better cope with Arnold initialize and evaluate phases.

We now parse and tokenize the expression in the init/update phase. Still there we convert the math expression from infix to postfix notation (also known as Reverse Polish Notation or RPN). Eventually we use the RPN like 'compiled code' in the evaluate phase because together with the token to parse the expression we have associated a lambda function that will be called from the RPN with the dynamic content from Arnold (xyz user-defined variables and globals) because the funny part was to be able to support the dynamic stuff while tokenizing/converting the expression as static.

This makes romboMathParser x20 faster and at least on pair (generally faster) with any shader network (or OSL code) you'd want to use to accomplish the same thing while remaining easier faster and funnier to setup.

Please re-download the lib (v1.4) from the initial post.

Below I'm using an rnd fnc to displace the circles we modelled previously.

rombomathparser_displacedcircles.png

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Technology Administrators


Autodesk Design & Make Report