kennetek / gridfinity-rebuilt-openscad

A ground-up rebuild of the stock gridfinity bins in OpenSCAD
Other
1.27k stars 187 forks source link

How did you make a color changing render? #83

Closed spuder closed 1 year ago

spuder commented 1 year ago

I've spent a long time figuring out how to make a rotating render using openscad

https://stackoverflow.com/a/70468907/1626687 https://github.com/spuder/CAD-scripts/blob/main/stl2gif.sh

How did you make the render change colors?

kennetek commented 1 year ago

I don't think I still have the code snippet, it got lost somewhere and I do not know where. But I can remember vaguely how I did it. To sum it up, I used the animation window in the OpenSCAD GUI to program the movement/color, get an image dump, and used Image Magick to compile into a gif. Animating in OpenSCAD can be done with the variable $t (see the wiki). I think I normalized $t to be between 0 and 1, so $t was divided by the number of frames I wanted. There are three steps to this animation:

  1. Rotation: use the rotate command around the Z axis. Since it only does one rotation, this is simply 360*$t
  2. Translation: The up and down movement is a sine function of $t. So something like sin(360*$t) inside a translate command. There may have been frequency modifiers.
  3. Color: using the color([r,g,b,a]) command, all that is needed is some continuous mapping function of $t to a hue. I don't remember what I used, most likely something copied off Stack Overflow.

After playing the animation and having the dump images box checked, the files were in order in the script directory. With ImageMagick, you can collect all these image files and generate a gif.

spuder commented 1 year ago

Thanks!

Got it working with the <hsvtorgb.scad> library

It takes a value between 0.0 and 1.0 and translates it to an RGB value (e.g. [1,0,0] would be red, [0,1,0] would be green)

    $openscad_path /dev/null \
        -D 'use <hsvtorgb.scad>;' \
        -D '$vpr = [60, 0, 360 * $t];' \
        -o "${MYTMPDIR}/foo.png"  \
        -D "color(hsvToRGB(\$t,1,1)) import(\"${MYTMPDIR}/foo-centered.stl\");" \
        --imgsize=600,600 \
        --animate 60 \
        --viewall --autocenter \
        --preview \
        --quiet

I've created a docker container/script that can do this with any .stl file on my github spuder/CAD-scripts/stl2gif.sh

Thanks again

benchy