Displacement map from image

Hi there,

Should be a quick one. I wrote some code to import an image and create a mesh based on the displacement of the brightness of the pixels corresponding with an amount of vertices that is set parametrically. This is my first time doing displacement mapping in grasshopper and the result is totally off. I followed a tutorial with the only difference being that the size of the mesh is extracted from the image imported as a surface and the resolution of the mesh is set parametrically.

This is the tutorial I followed: [displacing a mesh with an image]

image displacement.gh (1.5 MB)

Hello

I think your problem is that your Image Sampler has a 2D Domain going from 0 to 1, but your surface is most likely bigger, so the point coordinates are outside this domain. You need to remap your X & Y coordinates from the original surface domain to [0,1].

2 Likes

Oops, forgot to internalize itâ€¦

Aah, ok. Thats most likely it. Will try once I get home

I found out I have no idea how to remap coordinates. The best thing I could come up with is to extract the max number of the coordinates list and divide the coordinates by it. However, even this doesnâ€™t fit the total x length of the image, and it doesnâ€™t fix the problem of the v-coordinates being cut off because of the image not being square.

So Iâ€™m at your mercy once again â€¦

This time I did internalize the image

image displacement.zip (1.5 MB)

Hello,

The simplest solution was to Reparameterize the input surface, so its domain goes from 0 to 1 (right clic > Reparameterize). With your soltion it could have worked if you did the division on U and V coordinates separately.

I also took the liberty of simplifying your grid creationâ€¦ You could also use the Paneling Tools library, downloadable from Package Manager.

image displacement.gh (1.5 MB)

To reparameterize was my first guess actually. Except I didnâ€™t consider reparameterising the surface itself. Instead I tried the input and the last component, both of which didnâ€™t work of course. Well, this is how you learn.

I am a little confused by one thing, if you donâ€™t mind me asking. What does the â€śdimensionâ€ť component measure exactly? Because the dimensions remain unchanged after repam. You would assume both u and v dimensions would change to 1 after. Instead, they remain unchanged. Pretty curious about that.

Does that mean I should extract the u and v coordinated seperately, divide them and then create points based on u and v coordinates? I thought about that too, but I didnâ€™t find the right components for it. What would you have used?

Thanks a lot for everything. People like you make learning grasshopper manageable

It measures the lenghts of the surface, in both directions.
Not exactly sure what happens if the surface is not a nice rectangle though ! I guess it takes sort of a bounding box in that case. Maybe better to use `Deconstruct Brep` to retrieve the real edges and measure their lengths.

The 2D parameters are actually Points, so you can access coordinates using `Deconstruct`. Then normalize the U/V (X/Y) by the surface dimensions.
There is a slight complexity coming from the fact that your surface doesnâ€™t start at 0,0,0, I used the parameters here, if you want to stick with points, then you have to compute the coordinates of the bottom left corner of the surface and add them before dividing.

I know, but I meant: doesnâ€™t repam reduce those lengths to 1?

Ok, that all makes perfect sense. I forgot about the â€śDeconstructâ€ť function. So many to remember haha. How do you wright your functions so that they appear with mockup?

No, it only changes the surface domain, which is a sort of local coordinate system. It doesnâ€™t scale the surface.
Usually, the surface domain is defined by Rhino when the surface is created, based of the curve domains of the borders, or the rail curves (for a loft or a sweep for instance).

Without reparameterize :

With reparameterize :

I donâ€™t understand what you are asking for ?

Did you try import image?

disp_.gh (8.3 KB)

Ahh, ok. Thanks. Good to know

I studied this code for a bit, but frankly thereâ€™s too many functions I have never used nor know the purpose of in here. For now I wouldnâ€™t know how to use it. But thanks for the effort and at some point it might be of use for me.

Is there a different way to create a mesh from the displacement? Due to the triangulated nature, the mesh becomes very jittery and uneven.

Hi again,

Iâ€™ve changed the mesh creation using `Mesh Plane`, to have a regular grid. This has a better aspect in XY plane.

Then you could use `Smooth Mesh`, or convert the mesh to a SubD at the end to have something less kinky. Reducing the amount of points will also help getting somethign smoother.

image displacement.gh (1.5 MB)

Youâ€™re amazing. Thank you so much

1 Like