Jump to content
  • Advertisement
Sign in to follow this  
vadevaman

[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.

If you intended to correct an error in the post then please contact us.

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;
        
        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!

 

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!