colors2dgradientpovray

POV-Ray 2D gradient (polygon)


So, what I want to do is apply a color gradient on a custom 2D polygon and I can't find anywhere instructions/examples on how to do it (all I can find are 3D ones). Specifically, I want to do this:

enter image description here

Also in general, I would like to know how to apply a simple radial gradient say from the center (of the polygon) out.


Solution

  • Ok, so I figured it out and since I can't seem to find a straight-forward answer anywhere I might as well answer my own question:

    1) To do one gradient on your polygon you just need the spherical pigment with a color_map. I give an example with a triangle:

    #declare pol1=polygon{3,<-1,0> <1,0> <0,1>} //Declare a triangle
    polygon{pol1 pigment{spherical color_map{[0.0 rgb <1,0,0>] [1.0 rgb <0,0,1>] } translate<0,0,0>} }    
    

    So what this does is it creates a radial gradient with its center at the absolute (0,0,0) and you can translate it depending where you want it. The color map defines two colors (rgb <1,0,0> and rgb <0,0,1>) and the number in front of them basically controls the gradient (where the colors start/end relative to the grid system). Won't explain it in detail so you can read more here.

    2) To do more gradients, say one for each corner I couldn't find a way to apply 3 different gradients on a single shape. Like color_map there is also a gradient_map but it's of no help because of the way you have to define where one pigment ends and where the next one starts. So the only way I found around this is by removing the polygon's corners with cylinders (with the difference command) and then replace them with cylinders (with the intersection command) whose phase is the desired gradient. And even though it's a messy way it doesn't actually impacts performance which is my main concern so I'm going with it until I find something better. So here is an example (I am not going to explain it any further, this answer is getting big as it is):

    #declare pos1=array[2]{-1,0}; //the corners' coordinates
    #declare pos2=array[2]{1,0};   
    #declare pos3=array[2]{0,1};
    #declare cyl_rad=0.5;     
    
    #declare pol1=polygon{3,<pos1[0],pos1[1],0> <pos2[0],pos2[1],0> <pos3[0],pos3[1],0>} //Declare the polygon
    #declare cyl1=cylinder{<pos1[0],pos1[1],-0.01> <pos1[0],pos1[1],0.01>,cyl_rad} //Declare the cylinder that will "replace" the first corner
    #declare cyl2=cylinder{<pos2[0],pos2[1],-0.01> <pos2[0],pos2[1],0.01>,cyl_rad} //Declare the cylinder that will "replace" the second corner
    #declare cyl3=cylinder{<pos3[0],pos3[1],-0.01> <pos3[0],pos3[1],0.01>,cyl_rad} //Declare the cylinder that will "replace" the third corner
    difference{polygon{pol1} cyl1 cyl2 cyl3 pigment {color_part}} //The polygon with its corners "cut"
    intersection{polygon{pol1} cyl3 pigment{spherical color_map{[0.0 rgb <1,0,0>] [0.95 rgb <0,0,1>]} scale<cyl_rad,cyl_rad,cyl_rad> translate <pos3[0],pos3[1],0>} } //Replacing the corners with the coloured cylinders
    intersection{polygon{pol1} cyl2 pigment{spherical color_map{[0.0 rgb <1,0,0>] [0.95 rgb <0,1,1>]} scale<cyl_rad,cyl_rad,cyl_rad> translate <pos2[0],pos2[1],0>} }
    intersection{polygon{pol1} cyl1 pigment{spherical color_map{[0.0 rgb <1,0,0>] [0.95 rgb <0,1,0>]} scale<cyl_rad,cyl_rad,cyl_rad> translate <pos1[0],pos1[1],0>} }