Jump to content

How to Write an Effect Plugin (Part 3 of 4 - Complex)


Recommended Posts

I've never been happy with the C# Code Syntax Highlighter here on this site, so this tutorial has been moved here:

If you have questions about the tutorial, you can post them here in this thread and I will address them.

Link to comment
Share on other sites

Woah! What a marvelous tutorial!

Now we'll get a landslide of new plugins from a host of new authors :D

Thank you for explaining it so well.

Link to comment
Share on other sites

  • 2 weeks later...

As an amendment to BoltBait's excellent tutorial, here a codelab script that renders clouds based on @Sepcot's original Perlin Noise code.

Note that this cloud effect is not exactly the same as the Cloud effect built in to PDN.

/* ===================== */
/* */
/* Perlin Noise.cs */
/* */
/* Description: Draws clouds based on Sepcot's Perlin Noise routines 	*/
/* */
/* ==================== */

// Name: Sepcot's Perlin Noise
// Author: Ego Eram Reputo
// Submenu: Render
// URL: http://www.getpaint.net/redirect/plugins.html

#region UICode 
double Amount1 = 1.5; // [1, 12] Frequency
double Amount2 = 65; // [20, 70] Persistence (edge blur)
double Amount3 = 8.0; // [2,14] Octaves
double Amount4 = 1.0; // [0,5] Amplitude (brightness or density)
double Amount5 = 0.0; // [-2,3] cloudCoverage
double Amount6 = 1.0; // [0,4] cloudDensity
#endregion

void Render(Surface dst, Surface src, Rectangle rect)
{
// Delete any of these lines you don't need
Rectangle selection = this.EnvironmentParameters.GetSelection(src.Bounds).GetBoundsInt();
long CenterX = (long)(((selection.Right - selection.Left) / 2)+selection.Left);
long CenterY = (long)(((selection.Bottom - selection.Top) / 2)+selection.Top);
ColorBgra PrimaryColor = (ColorBgra)EnvironmentParameters.PrimaryColor;
ColorBgra SecondaryColor = (ColorBgra)EnvironmentParameters.SecondaryColor;
int BrushWidth = (int)EnvironmentParameters.BrushWidth;

//	ColorBgra srcBgra;
for(int y = rect.Top; y < rect.Bottom; y++)
{
	for (int x = rect.Left; x < rect.Right; x++)
	{
	ColorBgra srcBgra = src[x, y];
	byte c = (byte)(PerlinNoise2d(x, y) * 255);
	dst[x, y] = ColorBgra.FromBgra(srcBgra.B, srcBgra.G, srcBgra.R, c);
	}
}
}

static Random r = new Random();
int r1 = r.Next(1000, 10000);
int r2 = r.Next(100000, 1000000);
int r3 = r.Next(1000000000, 2000000000);

double PerlinNoise2d(int x, int y)
{
double total = 0.0;
double frequency = Amount1/100; 	// was .015;	// USER ADJUSTABLE
double persistence = Amount2/100; // was .65; // USER ADJUSTABLE
double octaves = Amount3; 	// was 8; 	// USER ADJUSTABLE
double amplitude = Amount4; 	// was 1; 	// USER ADJUSTABLE
double cloudCoverage = Amount5; 	// was 0; // USER ADJUSTABLE
double cloudDensity = Amount6; 	// was 1;	// USER ADJUSTABLE

for(int lcv = 0; lcv < octaves; lcv++)
{
	total = total + Smooth(x * frequency, y * frequency) * amplitude;
	frequency = frequency * 2;
	amplitude = amplitude * persistence;
}

total = (total + cloudCoverage) * cloudDensity;

if(total < 0) total = 0.0;
if(total > 1) total = 1.0;

return total;
}

double Smooth(double x, double y)
{
double n1 = Noise((int)x, (int)y);
double n2 = Noise((int)x + 1, (int)y);
double n3 = Noise((int)x, (int)y + 1);
double n4 = Noise((int)x + 1, (int)y + 1);

double i1 = Interpolate(n1, n2, x - (int)x);
double i2 = Interpolate(n3, n4, x - (int)x);

return Interpolate(i1, i2, y - (int)y);
}

double Noise(int x, int y)
{
int n = x + y * 57;
n = (n<<13) ^ n;

return ( 1.0 - ( (n * (n * n * r1 + r2) + r3) & 0x7fffffff) / 1073741824.0);
}

double Interpolate(double x, double y, double a)
{
double val = (1 - Math.Cos(a * Math.PI)) * .5;
return x * (1 - val) + y * val;
}


Link to comment
Share on other sites

  • 2 years later...

Is there a list or examples of the actions in Paint.net similar to the composite effects list / effects examples that were given such as "gaussian blur", "emboss", "add noise", etc. ?

In particular things like "cut selection", "copy selection", "delete selection", and "paste selection" is what I'm trying to find or is that something completely different? I know that copying and such are actions and emboss and whatnot are effects but I can't find any examples which explains how to use actions, or maybe I just missed / overlooked them.

RFX5A1.jpg

Link to comment
Share on other sites

lol well there goes my idea facepalmc.gif There is an alternate method to what I have in mind however there are a few plug ins that already exist and sort of relate to it, just nothing I've seen that does exactly what I had in mind. Thanks though for the info guys, I'll do a bit more research on it.

RFX5A1.jpg

Link to comment
Share on other sites

Have a look at this list of what plugins can and cannot do: plugin limitations

  • Upvote 1
Link to comment
Share on other sites

  • 2 weeks later...

Thanks EER! I checked it out and I think what I had in mind is just not possible, an plotted section cutter similar to Gimp's path tool. As it can't cut or make the selected area transparent, perhaps I can try a different approach from those 2 ideas and try an area-to-fill but I'm not sure if a plotted path is even possible to do as an effect. Something like this:

 

selectareafill.jpg

RFX5A1.jpg

Link to comment
Share on other sites

Looks like you need to write a tool interface like Smudge or Liquify.  Possible, but very difficult compared to 'standard' plugins.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...