# Skewing or Shearing a Mesh/GameObject

Hello everyone,

I’d like to skew/shear a mesh in a parallel direction (any axis). I’v tried it several ways, but none of the methods worked. I’ve also played around with the matrix4x4 but I could not figure out how to apply it to a mesh. So I turned to manually scaling and rotationg the mesh to get the desired effect.

Thank you all for any and all help.

Seriously? No one knows how to parallel skew a Gameobject or Mesh in Unity?

There isn’t a direct way to do this with the tranform properties, but you can apply shearing to the mesh using the Mesh API.

Shearing basically means offsetting the coordinates along one or two axes based on the distance along the remaining axis. Say you wanted to displace vertices in the XZ plane depending on the Y coordinate. You would define a vector with only X and Z components:-

``````var shear: Vector3 = new Vector3(1.0, 0, 1.0);
``````

Then, you would loop through all the vertices, getting the Y coordinate, multiplying that by the shear value and then adding that offset back to the original vertex:-

``````for (i = 0; i < verts.Length; i++) {
verts[i] = verts[i] + shear * verts[i].y;
}
``````

You can also do this and many other transformations using matrices (basically set up the matrix and then multiply it by each vertex in turn).

I too would like to do this, and it seems like a standard standard matrix operation that would be pretty useful.
anyone else managed to do this legit ?

I think I may have somehow accidentally gotten the same effect a year or so ago, by scaling objects non-uniformly plus rotating them, then unity got confused, and ended up giving me some deformed geometry that looked a lot like it was sheared. Had to re-import models to fix it.

anyone ?

You basically get a direction vector and alter the vertex positions according to this direction vector while preserving certain ones in place to get a skewing effect.
I cant paste any code because i just submitted a new pack to the assetstore which includes this functionality as a commercial product. But i think you get the idea.

Thanks,
yeah I wrote code already to do this one vertex at a time. but I was trying to do it just using a matrix (almost for free), but its not possible to write to transform.matrix4x4, as its read only.

However someone on another thread mentioned it could maybe de also done with a vertex shader.

I know I’m here a bit late, but how would I change the code to make the skew vector changeable at runtime? Obviously, the skew will keep adding to itself as is. Here’s my current code:

``````using UnityEngine;
using System.Collections;

[RequireComponent(typeof(MeshFilter))]
public class Skew : MonoBehaviour
{
//--Variables--
//-Public-
//Parameters
public Vector3 skewDirection;
public Vector3 skewPosition;
//-Private-
//References
Mesh mesh;
Vector3[] startVertices;
Vector3[] vertices;

//--Methods--
void Awake()
{
mesh = GetComponent<MeshFilter>().mesh;
startVertices = mesh.vertices;
vertices = startVertices;
}
void Update()
{
mesh.vertices = startVertices;
for(int i = 0; i < vertices.Length; i++)
{
vertices[i] = startVertices[i] + (skewDirection * startVertices[i].y);
}
mesh.vertices = vertices;
mesh.RecalculateBounds();
mesh.RecalculateNormals();
vertices = startVertices;
}
}
``````
1 Like

I’ve created a request for this feature: https://feedback.unity3d.com/suggestions/transform-set-matrix-make-affine-transformation-possible-skew

Did you ever release this product on the Asset Store? If so, what is it called? Thanks!

i need to do 3d shearing on unity…is it possible…if so what will be the scripting?

MegaFiers has a Skew modifier in it which is the same as Shearing.

Been a long ass time since I was on this thread haha, but if yall want a free way to do this I ended up making a tool for doing skewing (as well as a bunch of other modifiers)
https://github.com/keenanwoodall/Deform

3 Likes