- Home /
Skew an imported mesh?
Using Unity free, I have a situation where I want to conform a mesh to a slope...which would be skewing the y coordinates. I -don't- want to rotate it so that it fits the normal of the slope. Picture a segment of a brick wall...if you wanted to be able to dynamically place these on a terrain, it would need to skew, rotating it would leave weird gaps at the intersections.
https://docs.google.com/drawings/d/1t9iN0A8sVbJwPhTt5CZSS9FDycHyCYpjWTkh7z91xuc/edit?hl=en_US
The only way I can think to do this with an imported mesh would be to create a new mesh from the original, and for each y coordinate in its vertices, translate the y coordinate value appropriately.
Anyone know of a better way? I'm assuming I can easily create a new mesh from an imported one...haven't tried it yet, though :)
[1]: http://docs.google.com/drawings/d/1t9iN0A8sVbJwPhTt5CZSS9FDycHyCYpjWTkh7z91xuc/edit?hl=en_US
Answer by Bunny83 · Nov 09, 2011 at 10:06 PM
Well, i guess the easiest way to accomplish that is to modify a copied Mesh with a custom skew-matrix. Something like this:
1 0 0 0
S 1 0 0
0 0 1 0
0 0 0 1
where S is the tangens of the desired angle. This will skew coordinates along the x axis. So when the x coordinate increases it also increases the y coordinate. Íf S is 1.0f it would exactly by 45°.
Instead of the tangens you can of course use a x-y or x-z ratio. If one point is at 0,0,0 and the furthest point is at 100,0,0 which should be skewed upwards to 100,20,0 just do:
S = 20.0f / 100.0f;
Just keep in mind that scheme:
1 0 0 0 --> x out
0 1 0 0 --> y out
0 0 1 0 --> z out
0 0 0 1 --> w out
x in ___| | | |
y in _____| | |
z in _______| |
w in _________|
Well, it's just basic matrix-math but a lot get confused with rows and columns since it depends on the multiplication order. However unity only supports this way. Also keep in mind that the skewing will happen around 0,0,0
Also keep in $$anonymous$$d that skewing a mesh will distort / skew the texture that's on the mesh since the texture coordinates are linked to the vertices.
Crap. So a brick wall that looks fine on flat ground will have distorted bricks?
It just follows your skewing... So if you have a wall with a brick texture it will still follow the same mesh. Texture coordinates are not bound to world-points, they are bound to your vertices. You will still see the same part of the texture.
If your texture contains a square pattern, the square-pattern will turn into a rhombus-pattern.
It's possible to reverse the skewing but that needs a bit more fiddling to keep the texture in place. It only would help if you use tileable textures of course since your mesh is "growing" in height there is more surface to fill. (To be mathematically correct the area would stay the same, but it is more spreaded ;)).
How to apply this to a GameObject such as an Unity Cube in a script ? And will the collider follow ? If not how ?
Answer by DaveA · Nov 09, 2011 at 10:02 PM
You could probably construct a skew matrix. Google what that is, and see http://unity3d.com/support/documentation/ScriptReference/Matrix4x4.html
If you need it to skew other-than around 0,0,0 you can first move it to 0,0,0, skew it, then move it back (and you do this by concatenating the matrices, not making calls to MoveTo or Translate, of course)
Just to be clear: I will still need to apply the matrix against each vertex in the mesh, is this correct? I saw this documentation, but wasn't sure how to apply that to a model in script. You can modify the rotation, scale, etc...but I haven't seen how to apply a matrix to an entire object.
Yes, you have to modify each vertex because you can't set the matrix of a transform. Transform.localToWorld$$anonymous$$atrix is read only because it get generated out of it's localPosition / localRotation / localScale.
Your answer
![](https://koobas.hobune.stream/wayback/20220613040230im_/https://answers.unity.com/themes/thub/images/avi.jpg)