Jump to content
How to Install Plugins ×

TRs Displacement Map 3D Version 2.1


TechnoRobbo

Recommended Posts

The grayscale of an image copied to the Clipboard modifies your image's pixel locations in 3D space. Endless varieties of can be achieved. 

 

Effect->Distort->TRs Displacement Map 3D

 

Version 2.0 added a bunch of fun creative features.

 

  • Highligth and Shadow
  • Light Source Positioning
  • Color Adjustment
  • Alpha Transparency.

 

Menu.PNG?raw=1

 

Here's the Quick Demo on Youtube: http://youtu.be/VfLRw4wsE2Q

TR.png?raw=1

 

 

Here's the Demo on how to create the 3 Skulls on Youtube:  http://youtu.be/aXQeIOBKabk

 

3skulls.png?raw=1

 

Download:

 

DispMap.zip

 

 

Here's the code:

Spoiler

// Name: TRs Displacement Map 3D
// Submenu: Distort
// Author: TechnoRobbo
// Title: Displacement Map 3D PlugIn - v2.1
// Version: 2.1
// Desc:
// Keywords:
// URL: http://www.technorobbo.com
// Help:
#region UICode
DoubleSliderControl Amount1 = 0.5; // [0,1.5] Control
PanSliderControl Amount2 = Pair.Create(0.000,0.000); // Light Source
DoubleSliderControl Amount3 = 15; // [0,32] Intensity
DoubleSliderControl Amount4 = 1; // [0,1] Red Channel
DoubleSliderControl Amount5 = 1; // [0,1] Green Channel
DoubleSliderControl Amount6 = 1; // [0,1] Blue Channel
CheckboxControl Amount7 = false; // [0,1] Alpha Transparency
#endregion

private Surface clipboardSurface = null;
private bool readClipboard = false;

protected override void OnDispose(bool disposing)
{
    if (disposing)
    {
        // Release any surfaces or effects you've created.
        if (clipboardSurface != null) clipboardSurface.Dispose();
        clipboardSurface = null;
    }

    base.OnDispose(disposing);
}

void PreRender(Surface dst, Surface src)
{
    if (!readClipboard)
    {
        readClipboard = true;
        clipboardSurface = Services.GetService<IClipboardService>().TryGetSurface();
    }
}

private static void getNormal(double[] v0, double[] v1, double[] v2, double[] normal)
{
    double[] A = new double[3], B = new double[3], product = new double[3];
    A[0] = v2[0] - v1[0];
    A[1] = v2[1] - v1[1];
    A[2] = v2[2] - v1[2];
    B[0] = v0[0] - v1[0];
    B[1] = v0[1] - v1[1];
    B[2] = v0[2] - v1[2];
    product[0] = A[1] * B[2] - A[2] * B[1];
    product[1] = A[2] * B[0] - A[0] * B[2];
    product[2] = A[0] * B[1] - A[1] * B[0];
    double length = Math.Sqrt(product[0] * product[0] + product[1] * product[1] + product[2] * product[2]);
    if (length == 0)
    {
        normal[0] = 0;
        normal[1] = 1;
        normal[2] = 0;
    }
    else
    {
        normal[0] = product[0] / length;
        normal[1] = product[1] / length;
        normal[2] = product[2] / length;
    }
}

void Render(Surface dst, Surface src, Rectangle rect)
{
    if (clipboardSurface == null)
    {
        dst.CopySurface(src, rect.Location, rect);
        return;
    }

    Rectangle selection = EnvironmentParameters.GetSelection(src.Bounds).GetBoundsInt();
    double CenterX = ((selection.Right - selection.Left) / 2) + selection.Left;
    double CenterY = ((selection.Bottom - selection.Top) / 2) + selection.Top;
    double offx = 0;
    double offy = 0;
    double z = 0;
    double tmp = 0, tmp2 = 0;
    int nx = 0, ny = 0;
    const double zp = 255;
    double SunX = Amount2.First * -1;
    double SunY = Amount2.Second * -1;
    double SunZ = 1;

    ColorBgra CP;
    ColorBgra shadePixel;
    double[] shade1 = new double[3];
    double[] shade2 = new double[3];
    double[] shade3 = new double[3];
    double shade = 0;
    double[] norms = new double[3];

    //test variables=======
    //
    //SunX=-1;
    //SunY=-1;
    //Amount1=1;
    //Amount3=.7;
    //=======================

    for (int y = rect.Top; y < rect.Bottom; y++)
    {
        if (IsCancelRequested) return;
        for (int x = rect.Left; x < rect.Right; x++)
        {
            //================================
            //Get Real Pixels
            CP = clipboardSurface.GetBilinearSampleClamped(x, y);
            tmp = ((double)CP.R + (double)CP.B + (double)CP.G) / 3;

            z = 255 + tmp * Amount1;

            // 3D Projection
            tmp2 = tmp;
            tmp = (double)x - CenterX;

            offx = tmp * zp / z + CenterX;

            tmp = (double)y - CenterY;

            offy = tmp * zp / z + CenterY;

            CP = src.GetBilinearSample((int)offx, (int)offy);

            if (tmp2 > 0)
            {
                //===============================
                //Plot Reflections and shading
                nx = x - (x % 2);
                ny = y - (y % 2);

                shadePixel = clipboardSurface.GetBilinearSampleClamped(nx, ny);
                tmp = ((double)shadePixel.R + (double)shadePixel.B + (double)shadePixel.G) / 3;
                shade1[0] = nx;
                shade1[1] = ny;
                shade1[2] = tmp;

                shadePixel = clipboardSurface.GetBilinearSampleClamped(nx + 1, ny);
                tmp = ((double)shadePixel.R + (double)shadePixel.B + (double)shadePixel.G) / 3;
                shade2[0] = nx + 1;
                shade2[1] = ny;
                shade2[2] = tmp;

                shadePixel = clipboardSurface.GetBilinearSampleClamped(nx, ny + 1);
                tmp = ((double)shadePixel.R + (double)shadePixel.B + (double)shadePixel.G) / 3;
                shade3[0] = nx;
                shade3[1] = ny + 1;
                shade3[2] = tmp;

                getNormal(shade1, shade2, shade3, norms);

                shade = Math.Sqrt(Math.Pow(norms[0] - SunX, 2) + Math.Pow(norms[1] - SunY, 2) + Math.Pow(norms[2] - SunZ, 2));
                shade = (shade * 2 - 1) * Amount3;

                CP.R = Int32Util.ClampToByte((int)(shade + (double)CP.R * Amount4));
                CP.G = Int32Util.ClampToByte((int)(shade + (double)CP.G * Amount5));
                CP.B = Int32Util.ClampToByte((int)(shade + (double)CP.B * Amount6));
            }
            else if (Amount7)
            {
                CP.A = 0;
            }

            dst[x, y] = CP;
        }
    }
}

 

 

 

Old Version:

Spoiler

OLDER  2.0  version

 

DispMap.zip

 


// Submenu: Distort
// Name: TRs Displacement Map 3D
// Title: Displacement Map 3D PlugIn - v2.0
// Author: TechnoRobbo
// URL: http://www.technorobbo.com


#region UICode
double Amount1 = 0.5; // [0,1.5] Control
Pair<double, double> Amount2 = Pair.Create( 0.0 , 0.0 ); // Light Source
double Amount3 = 15; // [0,32] Intensity
double Amount4 = 1; // [0,1] Red Channel
double Amount5 = 1; // [0,1] Green Channel
double Amount6 = 1; // [0,1] Blue Channel
bool Amount7 = false; // [0,1] Alpha Transparency
#endregion


protected Surface img
{
    get { if (_img != null)
            return _img; 
          else
          {
            System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(GetImageFromClipboard));
            t.SetApartmentState(System.Threading.ApartmentState.STA); 
            t.Start();
            t.Join();
            return _img;
          }
        }
}
private Surface _img = null;
private void GetImageFromClipboard()
{
    Bitmap aimg = null;
    IDataObject clippy;
    try
    {
        clippy = Clipboard.GetDataObject();
        if (clippy != null)
        {
            aimg = (Bitmap)clippy.GetData(typeof(System.Drawing.Bitmap));
        }
    }
    catch (Exception )
    {
    }
    if (aimg != null)
    {
        _img = Surface.CopyFromBitmap(aimg);
    }
    else
    {
        _img = null;
    }
}


private void getNormal(double[] v0, double[]  v1, double[] v2,double[] normal)
    {
    double[] A = new double[3], B= new double[3], product= new double[3];
    double  length;
    A[0] = v2[0] - v1[0];
    A[1] = v2[1] - v1[1];
    A[2] = v2[2] - v1[2];
    B[0] = v0[0] - v1[0];
    B[1] = v0[1] - v1[1];
    B[2] = v0[2] - v1[2];
    product[0] = A[1] * B[2] - A[2] * B[1];
    product[1] = A[2] * B[0] - A[0] * B[2];
    product[2] = A[0] * B[1] - A[1] * B[0];
    length = Math.Sqrt(product[0]*product[0] + product[1]*product[1] + product[2]*product[2]);
    if (length == 0)    
        {               
        normal[0] = 0;
        normal[1] = 1;
        normal[2] = 0;
    }else{
        normal[0] = product[0] / length;
        normal[1] = product[1] / length;
        normal[2] = product[2] / length;
        }
    }






void Render(Surface dst, Surface src, Rectangle rect)
{
  
    Rectangle selection = EnvironmentParameters.GetSelection(src.Bounds).GetBoundsInt();
    double CenterX = ((selection.Right - selection.Left) / 2)+selection.Left;
    double CenterY= ((selection.Bottom - selection.Top) / 2)+selection.Top;
    double offx =0;
    double offy =0;
    double z=0;
    double tmp =0, tmp2 =0;
    int nx =0, ny =0;
    double zp=255;
    double SunX = Amount2.First * -1;
    double SunY = Amount2.Second * -1;
    double SunZ = 1;
    
    ColorBgra CP;
    double[] shade1  = new double[3];
    double[] shade2 = new double[3];
    double[] shade3 = new double[3];
    double shade = 0;
    double[] norms = new double[3];
    double[] RGB={0,0,0};
    
    //test variables=======
    //
    //SunX=-1;
    //SunY=-1;
    //Amount1=1;
    //Amount3=.7;
    //=======================
    
    for (int y = rect.Top; y < rect.Bottom; y++)
    {
        shade=0;
        for (int x = rect.Left; x < rect.Right; x++)
        {
            
            
            if (img == null){
              dst[x,y] = src[x,y];
            }else {
                
                //===============================
                //Plot Reflections and shading
                nx= x - (x % 2);
                ny= y - (y % 2);
                
             
                CP= img.GetBilinearSampleClamped(nx,ny);
                tmp =((double) CP.R + (double) CP.B + (double) CP.G)/3;
                shade1[0]=nx;
                shade1[1]=ny;
                shade1[2]=tmp;
                


                CP= img.GetBilinearSampleClamped(nx+1,ny);
                tmp =((double) CP.R + (double) CP.B + (double) CP.G)/3;                
                shade2[0]=nx+1;
                shade2[1]=ny;
                shade2[2]=tmp;
                
                CP= img.GetBilinearSampleClamped(nx,ny+1);
                tmp =((double) CP.R + (double) CP.B + (double) CP.G)/3;                  
                shade3[0]=nx;
                shade3[1]=ny+1;
                shade3[2]=tmp;
                
                getNormal(shade1,shade2,shade3,norms);
                
                shade = Math.Sqrt(Math.Pow(norms[0] - SunX , 2) + Math.Pow(norms[1] - SunY , 2) + Math.Pow(norms[2] - SunZ , 2));
                shade = (shade * 2 - 1) * Amount3;
                //================================
                //Get Real Pixels
                CP= img.GetBilinearSampleClamped(x,y);
                tmp =((double) CP.R + (double) CP.B + (double) CP.G)/3;


                z=255 + tmp * Amount1;
                   
                // 3D Projection
                tmp2=tmp;
                tmp=(double)x-CenterX;
                
                offx = tmp *  zp / z + CenterX;
                
                tmp=(double)y-CenterY;
                
                offy= tmp *  zp / z + CenterY;
                
                
                CP = src.GetBilinearSample((int)offx,(int)offy);
                if (tmp2 >0){
                    
                    CP.R = Int32Util.ClampToByte((int)(shade +  (double) CP.R * Amount4 ));
                    CP.G = Int32Util.ClampToByte((int)(shade +  (double) CP.G * Amount5 ));
                    CP.B = Int32Util.ClampToByte((int)(shade +  (double) CP.B * Amount6 ));
                }else if (Amount7){
                    CP.A=0;
                }
                dst[x,y]=CP;  
            }
        }
    }
}

 

 

Edited by toe_head2001
  • Upvote 2

Go out there and be amazing. Have Fun, TR
TRsSig.png?raw=1
Some Pretty Pictures Some Cool Plugins

Link to comment
Share on other sites

This seems vastly different to the other bump map plugins.  Are you sure you wish to call it that?  That said - it's an interesting distortion!  Thanks for sharing it.

 

Edit:  Seems incredibly slow??

Link to comment
Share on other sites

It's only slow when the size of the image doesn't match the size of the map. When it does match the transformation is instant. I wasn't sure whether to call it a bump map or a displacement map. Since both refer to 3D shaders. Perhaps Displacement would be better.

 

Update: I've changed the name and included the recommendation to use same size image and map , I also modified the Youtube tutorial to reflect the changes.

 

 

Thanks for your help Ego.

Edited by TechnoRobbo

Go out there and be amazing. Have Fun, TR
TRsSig.png?raw=1
Some Pretty Pictures Some Cool Plugins

Link to comment
Share on other sites

Using a filled shape ellipse can produce some cool selective bulge type effect. Nice work TechnoRobbo B)

displace.png

Edited by DrewDale

BREtKQW.png

 

Link to comment
Share on other sites

Nice...I like that it uses copy to clipboard.

 

The name change doesn't show in the menu options.  :( 

 



 

SloppySig.png

Link to comment
Share on other sites

Sorry I meant the name change doesn't show in the effects menu. I deleted, redownloaded and reinstalled, but please tell me if I'm not doing something right.

 

dispmap.png

 

Edit: Just noticed I have multiples of a few things.

Edited by doughty

 



 

SloppySig.png

Link to comment
Share on other sites

Doughty 

 

That's it, the Displacement Map without an icon. The others are different plugins probably from different plugin packs.

 

Maybe I should change the name to TR's Displacement Map to avoid confusion. 

 

Ok - done reinstall 

Edited by TechnoRobbo

Go out there and be amazing. Have Fun, TR
TRsSig.png?raw=1
Some Pretty Pictures Some Cool Plugins

Link to comment
Share on other sites

Sorry for my confusion. :bertie:

 

I used your Line Tracer plugin on the photo of my cat to create a map. The frame from another project worked just as I hoped.

 

Zoedisp.png

  • Upvote 1

 



 

SloppySig.png

Link to comment
Share on other sites

  • 2 weeks later...

Very nice plug-in TechnoRobbo. Nice image example Welshblue :)

 

I found it works just as effective on non-Gaussian blurred text too.

TRdisplacement_zpsc7a1f102.png

 

What I did extra was copy the background, tick the alpha transparency box then added drop shadow to the text.

 

ZXCBOoZ.png

 

 

Link to comment
Share on other sites

  • 1 month later...
  • 6 months later...

Hello,

 

I'm having more than one problem today:

First I tried pasting my crash log right into this post but it wouldn't let me so I had to make an attachment of the crash log.

Second when I tried to use this TR's Displacement I had PDN crash on me twice.

Can anyone please read the crash log and tell me what I need to do to fix this?

Link to comment
Share on other sites

Update:

 

For some reason I cannot paste or insert an  image of the crash log: but when inserting the image link from Photobucket  this website froze on me and it also won't let me do an attachment.....Paint.net doesn't like me :(. Anyone else having problems with posting?

 

I also e-mailed the crash log to the professionals at this website hopefully they will get back to me soon.   

Link to comment
Share on other sites

Hi Kelly - I tried it too and it's incompatible with the new version. I have added it to the Incompatible Thread for you.

FYI - if you want to add the crash log next time to your post, here is a way. C&P the text then, when you are in your post, click on the little light switch (upper left corner of the tool bar) and add your text between this code: [hide}C&P your text here[/hide} Replace the 2 }'s with ]and[ then it will appear like this


Hidden Content:
text here


Bye :D .

30b8T8B.gif

How I made Jennifer & Halle in Paint.net

My Gallery | My Deviant Art

"Rescuing one animal may not change the world, but for that animal their world is changed forever!" anon.

 
Link to comment
Share on other sites

@Kelly - it may not be TR's that is crashing.  I got confused between the Displacement plugins in the Effects folder.  One shows as 'Displacement' and another shows as 'Displacement Map'.  It's hard to tell which belongs to whom :( .

Edit: I just tried TR's D-Map in Beta v4 and it's working fine - hopefully you will get it sorted soon. I know I've had awful trouble with internet connections the past few days .... perhaps it's work-wide and causing chaos :/ .

Edited by Pixey

30b8T8B.gif

How I made Jennifer & Halle in Paint.net

My Gallery | My Deviant Art

"Rescuing one animal may not change the world, but for that animal their world is changed forever!" anon.

 
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...