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

Started by
3 comments, last by alkisbkn 10 years, 1 month ago

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;
        
        matrix[2,0] = Mathf.Tan(skewAmmount * Mathf.Deg2Rad);
        // 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!

Advertisement

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.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

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?

I figured it out myself, thanks.

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.

This topic is closed to new replies.

Advertisement