Links

Categories

Tags


« | Main | »

Render Clouds Effect

By Jewe | May 12, 2014

Renders realistic clouds using 2D Perlin noise (IrfanView Filter Sandbox 1.2)

class RenderClouds : Effect
{
    Slider XSeed, XFreq, YSeed, YFreq, SPers, SOcts, SInt;
    Slider Hue1, Sat1, Lum1;
    Slider Hue2, Sat2, Lum2;
    Label Rand, Color1, Color2;

    PerlinNoise Perl;
    float SeedX, SeedY;
    float FreqX, FreqY;
    float Int;

    method RenderClouds()
    {
        Perl = null;
        SeedX = 0;
        SeedY = 0;
        FreqX = 0;
        FreqY = 0;
        Int = 0;

        Rand   = new Label("Perlin Properties");
        XSeed  = new Slider("X Seed (Start Position)",   "", 0,   0, 255,   90);
        YSeed  = new Slider("Y Seed (Start Position)",   "", 0,   0, 255,  125);
        XFreq  = new Slider("X Frequency (Zoom Factor)", "", 3, 0.1,   2,    1);
        YFreq  = new Slider("Y Frequency (Zoom Factor)", "", 3, 0.1,   2,    1);
        SPers  = new Slider("Persistence (Smoothing)",   "", 2,   0,   1, 0.25);
        SOcts  = new Slider("Octaves (Level of Detail)", "", 0,   1,  10,   10);
        SInt   = new Slider("Intensity (Contrast)",      "", 2, 0.1,   2,  0.5);
        Color1 = new Label("Sky Color");
        Hue1   = new Slider("Hue",                       "", 0,   0, 359,  210);
        Sat1   = new Slider("Saturation",                "", 0,   0, 255,   63);
        Lum1   = new Slider("Luminance",                 "", 0,   0, 255,   63);
        Color2 = new Label("Cloud Color");
        Hue2   = new Slider("Hue",                       "", 0,   0, 359,   27);
        Sat2   = new Slider("Saturation",                "", 0,   0, 255,  127);
        Lum2   = new Slider("Luminance",                 "", 0,   0, 255,  239);
    }

    method Parameter[] GetParameters() { return {Rand, XSeed, YSeed, XFreq, YFreq, SPers, SOcts, 
SInt, Color1, Hue1, Lum1, Sat1, Color2, Hue2, Sat2, Lum2}; }
    method EffectInfo GetEffectInfo() { return new EffectInfo("Render Clouds", "by Jewe", "Renders
 clouds. May take a minute with big images."); }

    method DoEffect(Image img)
    {
        // create sky image and fill with sky color
        RasterizerSSE sky = new RasterizerSSE(img);
        sky.Fill(Color::FromHSL(Hue1.Value, Sat1.Value / 255.0, Lum1.Value / 255.0));
        // create cloud image and fill with cloud color
        RasterizerSSE clouds = new RasterizerSSE(sky.Width, sky.Height);
        clouds.Fill(Color::FromHSL(Hue2.Value, Sat2.Value / 255.0, Lum2.Value / 255.0));
        // create soft radial gradient
        RasterizerSSE dome = new RasterizerSSE(sky.Width, sky.Height);
        dome.CreateRadialBrush(0.5);
        dome.Brightness(dome, 0.25);
        dome.Invert(dome);
        // create noise image
        RasterizerSSE nois = new RasterizerSSE(sky.Width, sky.Height);
        nois.CreateNoiseBrush(0.01);
        // add noise image to gradient for dithering
        dome.AddPixels(nois);
        // multiply gradient with sky image
        sky.MultiplyPixels(dome);
        // set up Perlin noise
        Perl = new PerlinNoise(SOcts.Value, SPers.Value);
        SeedX = XSeed.Value * 256.0;
        SeedY = YSeed.Value * 256.0;
        FreqX = 0.002 * XFreq.Value;
        FreqY = 0.002 * YFreq.Value;
        Int = SInt.Value;
        // render cloud alpha-channel
        clouds.ProcessAlpha( method(x, y, a) {
            float r = Perl.Random(SeedX + x * FreqX, SeedY + y * FreqY);
            if( r > 0 )
                r *= 0.5;
            return r * Int + 0.4;
        } );
        // draw cloud image on sky image and finalize
        sky.DrawAlpha(clouds, img.Rectangle, new Point());
        sky.Finalize();
    }
}

Topics: code examples, filter sandbox | Comments Off on Render Clouds Effect

Comments are closed.