# [C#] How to skew a mesh via matrix?

This topic is 2044 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello, I'm having a problem while writing my skew class.

The problem is that I dont have accuracy in my skewing, it's like time based, while I'd like to have accurate to a factor.
If I set a skewingAmmount of = 1 I'd like to see a skew at that ammount, but right now I see some random skewing.

So here's my code, and what am I doing wrong here? I suspect the Unity' class "MultiplyVector" needs to be changed.

using UnityEngine;
using System.Collections;

public class SkewMesh : MonoBehaviour
{
public float skewAmmount = 1.0f;
private MeshFilter mf;
private Vector3[] vertices;
private Matrix4x4 matrix;
private int vertLen;
private float lastSkewAmmount;
void Start ()
{
mf = transform.GetComponent<MeshFilter>();
vertLen = mf.sharedMesh.vertices.Length;
vertices = new Vector3[vertLen];
vertices = mf.sharedMesh.vertices;
}

void UpdateVert()
{
// create skew matrix
Matrix4x4 matrix = new Matrix4x4();
matrix[0,0] = 1.0f;
matrix[1,1] = 1.0f;
matrix[2,2] = 1.0f;
matrix[3,3] = 1.0f;

// apply skew matrix
for(int i = 0; i < vertLen; ++i){
vertices[i] = matrix.MultiplyVector(vertices[i]);
}

mf.sharedMesh.vertices = vertices;
lastSkewAmmount = skewAmmount;
}
void FixedUpdate()
{
if(skewAmmount != lastSkewAmmount)
UpdateVert();
}
}


If there is a simpler way than using Matrix, i'd be glad to hear about it :) thanks!

##### Share on other sites

At the moment, your post says: "This code doesn't work. What's wrong?" Providing code that doesn't work without an explanation of how you want it to work doesn't provide enough information.

##### Share on other sites

This code works ok with Unity3d, and it shouldnt be hard to port it to another framework, the principle stays the same, skew/shear a mesh/offsett verts by code.
What sort of information do you need to know?

##### Share on other sites

I figured it out myself, thanks.

##### Share on other sites

I figured it out myself, thanks.

Don't you just love when this happens :) People not posting the answers they find to their questions themselves, just stating that they found it.

Not that I need the answer, it could help someone else though, and this keeps happening on forums.

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 15
• 18
• 19
• 11
• 9