Shader tests

General questions about Indigo, the scene format, rendering etc...
User avatar
fused
Developer
Posts: 3648
Joined: Fri Sep 22, 2006 7:19 am
Location: Berlin, Germany
3D Software: Cinema 4D

Post by fused » Thu Aug 14, 2008 1:12 am

Deus wrote:That was cool :) 1.1.9?
yes :D

User avatar
fused
Developer
Posts: 3648
Joined: Fri Sep 22, 2006 7:19 am
Location: Berlin, Germany
3D Software: Cinema 4D

Post by fused » Thu Aug 14, 2008 1:32 am

im not bored yet :)

more displacement:

Code: Select all

    <material>
        <name>shader</name>
        <diffuse>
            <albedo>
                <shader>
                    <shader>
                        <![CDATA[   
 
                        def colorCalc(real x) vec3 :
                            vec3(
                                # compute color mix
 
                                #  color1 \/                      color2 \/
                                add(mul(0.75,x),mul(sub(1.0,x),0.00)),
                                add(mul(0.52,x),mul(sub(1.0,x),0.21)),
                                add(mul(0.00,x),mul(sub(1.0,x),0.88))
                            )
 
                        def eval(vec3 pos) vec3 :
                            colorCalc(
                                div(add(sin(mul(div(mod(doti(getTexCoords(0)),0.333333),0.333333),6.2831852)),
                                    sin(mul(div(mod(dotj(getTexCoords(0)),0.333333),0.333333),6.2831852))),2.0)
                            )
                        ]]>
                    </shader>
                </shader>
            </albedo>
            <displacement>
                <shader>
                    <shader>
                        <![CDATA[   
 
                        # sinus waves displacment U
 
                        def eval() real :
                            add(
			        mul(sin(mul(div(mod(doti(getTexCoords(0)),0.333333),0.333333),6.2831852)),7.5),
			        mul(sin(mul(div(mod(dotj(getTexCoords(0)),0.333333),0.333333),6.2831852)),7.5)
                            )
                        ]]>
                    </shader>
                </shader>
            </displacement>
        </diffuse>
    </material>
Attachments
im1218632453.png
im1218632453.png (533.13 KiB) Viewed 3503 times

User avatar
Kosmokrator
Posts: 1141
Joined: Sat Jul 29, 2006 11:52 am
Location: Greece-Athens

Post by Kosmokrator » Thu Aug 14, 2008 3:00 am

thats niiice!!!! :wink:
1)Core i7 965XE stock CLOCK ,PSU:CHIEFTEC 850W
M/B ASUS P6T DELUXE,WATERCOOLING ZALMAN RESERATOR 2
MEMORY:6GB CORRSAIR @1600,Ati HD 4870x2,
MONITOR:LG 1950SQ,CASE:THERMALTAKE SOPRANO

User avatar
CTZn
Posts: 7240
Joined: Thu Nov 16, 2006 4:34 pm
Location: Paris, France

Post by CTZn » Thu Aug 14, 2008 10:49 am

fused wrote:im not bored yet :)
Nor are we :D
obsolete asset

User avatar
fused
Developer
Posts: 3648
Joined: Fri Sep 22, 2006 7:19 am
Location: Berlin, Germany
3D Software: Cinema 4D

Post by fused » Thu Sep 11, 2008 12:03 am

heres a the checkerboard shader ive been planing to do for a long time.

with the shder lang updates in 1.1.10 its finally possible.

Code: Select all

<albedo>
   <shader>
      <shader>
         <![CDATA[   
               
         # checkerboard
                  
         def eval(vec3 pos) vec3 :
            vec3(
               if(
                  eq(
                     add(                                                 #\/ this controls the tiles
                        if(gt(mod(mul(doti(getTexCoords(0)),2.0),2.0),1.0),1,0),
                        if(gt(mod(mul(dotj(getTexCoords(0)),2.0),2.0),1.0),1,0)
                     ),1
                  ),0.85,0.025 # these values control the color
               )
            )
         ]]>
      </shader>
   </shader>
</albedo>
Attachments
im1221046925.jpg
im1221046925.jpg (43.17 KiB) Viewed 3216 times

User avatar
pixie
Indigo 100
Posts: 2345
Joined: Sat Dec 29, 2007 4:54 am
Location: Away from paradise
3D Software: Cinema 4D
Contact:

Post by pixie » Thu Sep 11, 2008 1:09 am

Awesome! :twisted:

User avatar
Kram1032
Posts: 6649
Joined: Tue Jan 23, 2007 3:55 am
Location: Austria near Vienna

Post by Kram1032 » Thu Sep 11, 2008 7:36 am

nice stuff :D

User avatar
CTZn
Posts: 7240
Joined: Thu Nov 16, 2006 4:34 pm
Location: Paris, France

Post by CTZn » Thu Sep 11, 2008 9:36 am

Thanks fused, it's good to see advanced Indigo features being used !

I was wondering if there was existing an alternate way for scripting this checker effect, just another way to lay the code out ? Or does this language, as "functional", have only one way to be written (concatenating operations) ?

Subsidiary question: could one translate this checker code in another (readable) language, eventually pseudocode, so we can learn by comparison ?

Thanks, none of the above is mandatory.
obsolete asset

User avatar
fused
Developer
Posts: 3648
Joined: Fri Sep 22, 2006 7:19 am
Location: Berlin, Germany
3D Software: Cinema 4D

Post by fused » Thu Sep 11, 2008 6:49 pm

CTZn wrote:Thanks fused, it's good to see advanced Indigo features being used !

I was wondering if there was existing an alternate way for scripting this checker effect, just another way to lay the code out ? Or does this language, as "functional", have only one way to be written (concatenating operations) ?
im sure there are easier ways, ono said something about doing it with xor and floor, but this isnt in the shader language yet.
he also said what i did is a bit like xor.
CTZn wrote:Subsidiary question: could one translate this checker code in another (readable) language, eventually pseudocode, so we can learn by comparison ?

Thanks, none of the above is mandatory.
i think i can translate it to some pseudo code for you :)

Code: Select all

function eval returns vec3
{
   integer x

   integer color_u, color_v

   float tiles = 2.0

   if modulo(texture_coordinate_u * tiles, 2.0) > 1.0 color_u = 1
   else color_u = 0

   if modulo(texture_coordinate_v * tiles, 2.0) > 1.0 color_v = 1
   else color_v = 0

   x = color_u + color_v

   if x equal 1 return Vec3(0.0) //black_color
   else return Vec3(1.0) //white_color
}
i attatched an image that hopefully makes it a bit clearer for you.
as you can see black fields always have the values 0 or 2 and only white fileds can have the value 1.

and im sure there are better ways to do it or to explain it :)
Attachments
tcheckerboard.jpg
tcheckerboard.jpg (17.09 KiB) Viewed 3105 times

User avatar
Kram1032
Posts: 6649
Joined: Tue Jan 23, 2007 3:55 am
Location: Austria near Vienna

Post by Kram1032 » Fri Sep 12, 2008 2:15 am

Code: Select all

z = sgn( sin(x)+sin(y) )
if z == -1 col = col 1
if z ==  1 col = col 2
if z ==  NAN or 0 (depending on how sgn is defined...) col = (col1 + col2)/2)
sgn is the signum function.... sgn(x) = x/|x| and if specially defined, sgn(0) = 0

In case of higher dimensions (or complex numbers or such),

Code: Select all

sgn(x+y+....+?) = sqrt(x²+y²+...+?²)(x+y+...+?)/(x*y*...*?)
and represents an n-sphere...

http://en.wikipedia.org/wiki/Sign_function

I've no idea how fast that'd be, though....

to control the tile-width, you can modify the formula...

sin(x) has a wavelength of 2pi
to get a wavelength of 1, you need to use sin(2pi*x)
To get a wavelenght w, you need to use sin(2pi*x/w)

so the total formula is:

Code: Select all

z=sgn( sin(2pi*x/w_1) + sin(2pi*y/w_2) )
if z = -1 then col = col_1
if z =  1 then col = col_2
if z = NAN or 0 then col = (col_1 + col_2) /2

Code: Select all

x, y and z are real variables
w_1 and w_2 are constants
pi == pi                  ( d'oh )

User avatar
CTZn
Posts: 7240
Joined: Thu Nov 16, 2006 4:34 pm
Location: Paris, France

Post by CTZn » Fri Sep 12, 2008 4:13 am

Woaow fused, that's a bit more than I expected, thanks a lot ! Now I understand the logic, I am able to run through (hopefully). The picture helps a lot :mrgreen:

Kram: I don't understand your formula yet, but I bet fused way is faster since it involves less calculations but compares more (faster). A friend of mine called when I was reading your post, and he told me that signum was not reliable since it can't manage x = 0 (wich should not happen when dealing with a checker, but with computers you never know), he proposed:

Code: Select all

if x < 0
    return -1

if x > 0
    return 1

else return 0
Thanks !
obsolete asset

User avatar
cpfresh
Posts: 501
Joined: Thu Jun 14, 2007 12:20 pm
Location: California, USA
Contact:

Post by cpfresh » Fri Sep 12, 2008 4:35 am

this is all chinese to me, but is there a way to control the size of the checker grid? i appreciate the attempts at a 'dumbed down' explaination of the code in more code terms, but how about a plain english explaination? :/ eeerrgh

User avatar
PureSpider
Posts: 1459
Joined: Tue Apr 08, 2008 9:37 am
Location: Karlsruhe, BW, Germany
Contact:

Post by PureSpider » Fri Sep 12, 2008 5:16 am

fused wrote:

Code: Select all

<albedo>
   <shader>
      <shader>
         <![CDATA[   
               
         # checkerboard
                  
         def eval(vec3 pos) vec3 :
            vec3(
               if(
                  eq(
                     add(                                                 #\/ this controls the tiles
                        if(gt(mod(mul(doti(getTexCoords(0)),2.0),2.0),1.0),1,0),
                        if(gt(mod(mul(dotj(getTexCoords(0)),2.0),2.0),1.0),1,0)
                     ),1
                  ),0.85,0.025 # these values control the color
               )
            )
         ]]>
      </shader>
   </shader>
</albedo>
watch out for the "this controls the tiles"

User avatar
fused
Developer
Posts: 3648
Joined: Fri Sep 22, 2006 7:19 am
Location: Berlin, Germany
3D Software: Cinema 4D

Post by fused » Fri Sep 12, 2008 6:18 am

@cpfresh: purespider is right :) but maybe text size is playing tricks on you, so i added a better formatted version for you.

basically i calculate the color in u and v direction by multiplying the u/v value with the value for tilesu/tilesv. then i do a modulo of the value and 2 to get it in the range of 0-1.9999999. >1 is 1 and <1 is 0. at the end i add the resulting values for u and v together and if this value is 1 the texture at this position is white, else its black (have a look at the picture).

again, this is no perfect solution. i had this idea at work (so i was half absent minded :P ). ono posted a better way to do it on irc today. but it will only work in future versions (due to floor).

@CTZn: np

@Kram: nice solution, pretty clever :) (cleverer than mine :( )

Code: Select all

<albedo>
   <shader>
      <shader>
         <![CDATA[   
               
         # checkerboard
                  
         def eval(vec3 pos) vec3 :
            vec3(
               if(
                  eq(
                     add(
                        if(gt(mod(mul(
                           doti(
                              getTexCoords(0)),
                              2.0   # this value controls the tiles in u direction
                           ),2.0),1.0),1,0),

                        if(gt(mod(mul(
                           dotj(
                              getTexCoords(0)),
                              2.0   # this value controls the tiles in v direction
                           ),2.0),1.0),1,0)
                     ),1
                  ),0.85,0.025 # these values control the color
               )
            )
         ]]>
      </shader>
   </shader>
</albedo>

User avatar
Whaat
Developer
Posts: 1827
Joined: Fri Dec 22, 2006 6:15 am
Location: Canada
Contact:

Post by Whaat » Fri Sep 12, 2008 4:32 pm

Your tests are great, fused! Thanks!

Post Reply
223 posts

Who is online

Users browsing this forum: No registered users and 11 guests