2

I'm trying out some procedural mesh extrusion but I've got two problems at the moment. The code that performs the extrusion is a part of a Unity example package, and is available here (MeshExtrusion.cs)

First problem is that the update to the mesh only shows when an additional point is added to the mesh, the GIF below should highlight this.

Second problem is that the mesh is twisting. I'm guessing this is due to the wrong vertices being extruded, but I'm not 100% sure. Here is my code that is implementing the mesh extrusion:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ExtrudeTest : MonoBehaviour {

private Vector3 currentMousePosition;
private float currentMouseDistance;
MeshExtrusion.Edge[] preComputedEdges;
private Mesh srcMesh;
private List<Matrix4x4> extrusionPoints = new List<Matrix4x4>();
private Matrix4x4 currentTransformMatrix;
GameObject lineObject;
LineRenderer drawLine;
private bool invertFaces = false;

// Use this for initialization
void Start () {
    srcMesh = GetComponent<MeshFilter>().sharedMesh;
    preComputedEdges = MeshExtrusion.BuildManifoldEdges(srcMesh);
    extrusionPoints.Add(transform.worldToLocalMatrix * Matrix4x4.TRS(transform.position, Quaternion.identity, Vector3.one));
    lineObject = new GameObject("lineRenderer");
    drawLine = lineObject.AddComponent<LineRenderer>();
}

//Store the current world mouse position.
public void storeMouseLocation()
{
    Ray ray = Camera.main.ScreenPointToRay(UnityEngine.Input.mousePosition);
    RaycastHit hit;
    if (Physics.Raycast(ray, out hit, Mathf.Infinity))
    {
        currentMousePosition = hit.point;
        currentMouseDistance = hit.distance;
        currentTransformMatrix = hit.transform.localToWorldMatrix;
    }
}

private void adjustMesh()
{
    Matrix4x4 worldToLocal = transform.worldToLocalMatrix;
    Matrix4x4[] finalSections = new Matrix4x4[extrusionPoints.Count];
    Quaternion rotation;
    Quaternion previousRotation = Quaternion.identity;
    Vector3 direction;

    for (int i=0; i < extrusionPoints.Count; i++)
    {
        if (i == 0)
        {
            direction = extrusionPoints[0].GetColumn(3) - extrusionPoints[1].GetColumn(3);
            rotation = Quaternion.LookRotation(direction, Vector3.up);
            previousRotation = rotation;
            finalSections[i] = worldToLocal * Matrix4x4.TRS(transform.position, rotation, Vector3.one);
        }

        else if (i != extrusionPoints.Count - 1)
        {
            direction = extrusionPoints[i].GetColumn(3) - extrusionPoints[i + 1].GetColumn(3);
            rotation = Quaternion.LookRotation(direction, Vector3.up);

            // When the angle of the rotation compared to the last segment is too high
            // smooth the rotation a little bit. Optimally we would smooth the entire sections array.
            if (Quaternion.Angle(previousRotation, rotation) > 20)
            {
                rotation = Quaternion.Slerp(previousRotation, rotation, 0.5f);
            }

            previousRotation = rotation;
            finalSections[i] = worldToLocal * Matrix4x4.TRS(extrusionPoints[i].GetColumn(3), rotation, Vector3.one);
        }

        else
        {
            finalSections[i] = finalSections[i - 1];
        }

    }

    extrudeMesh(finalSections);
}

private void extrudeMesh(Matrix4x4[] sections)
{
    Debug.Log("Extruding mesh");
    MeshExtrusion.ExtrudeMesh(srcMesh, GetComponent<MeshFilter>().mesh, sections, preComputedEdges, invertFaces);

}

// Update is called once per frame
void Update () {

    storeMouseLocation();

    drawLine.SetPosition(0, extrusionPoints[extrusionPoints.Count-1].GetColumn(3));
    drawLine.SetPosition(1, currentMousePosition);

    if (Input.GetMouseButtonDown(0))
    {
        extrusionPoints.Add(transform.worldToLocalMatrix * Matrix4x4.TRS(transform.position + new Vector3(currentMousePosition.x, currentMousePosition.y, currentMousePosition.z), 
            Quaternion.identity, 
            Vector3.one));
    }

    if (extrusionPoints.Count >=2)
    {
        adjustMesh();
    }  
}
}

Here is a GIF that shows how it's currently performing:

https://puu.sh/xeS7e/bfc3d71fba.gif

I'm not entirely sure what is causing these problems, so if anyone could offer any advice or input, I'd greatly appreciate it. Thanks

Tony
  • 3,587
  • 8
  • 44
  • 77

0 Answers0