1
\$\begingroup\$

I'm trying to create a quad at runtime and I'm encountering an issue where the mesh does not show up in the scene where it's vertices where set. In the following picture you can see the ship, and to the far right the mesh.

The 4 white dots around the ship where drawn after the mesh was made using the 4 vertices of the mesh.

enter image description here

My Code:

//Handles the method calls for the mesh creation
private void BoundingMeshCreation()
{
    Quaternion currentRotation = ResetPosition(new Quaternion(0, 0, 0, 0));
    BoundingPointsType boundingPoints = DetermineBounds();
    Mesh boundingMesh = CreateBoundingMesh(boundingPoints);
    AssignMesh(boundingMesh);
    ResetPosition(currentRotation);
}

//Creates the mesh filter and assigns the mesh
private void AssignMesh(Mesh boundingMesh)
{
    MeshFilter newMeshFilter = gameObject.AddComponent<MeshFilter>();
    newMeshFilter.mesh = boundingMesh;

    VectorLine testPoint = new VectorPoints("testPoint", boundingMesh.vertices, null, 5);
    testPoint.Draw3D();
}

//Creates a bounding mesh for the unit
private Mesh CreateBoundingMesh(BoundingPointsType boundingPoints)
{

    Mesh boundingMesh = new Mesh();
    boundingMesh.name = "Bounding Quad";


    Vector3[] vertices = new Vector3[4]
    {
        boundingPoints.LowerLeftPoint,
        boundingPoints.LowerRightPoint,
        boundingPoints.UpperRightPoint,
        boundingPoints.UpperLeftPoint
    };

    int[] triangles = new int[6]
    {
        0,
        2,
        1,

        0,
        3,
        2
    };

    boundingMesh.vertices = vertices;
    boundingMesh.triangles = triangles;

    return boundingMesh;
}

//Resets the units rotation to the specific quaternion and returns the quaterion from before the reset. Bit of a hack for now to make sure that the original box is the proper size.
private Quaternion ResetPosition(Quaternion toResetTo)
{
    Quaternion currentRotation = transformV.rotation;
    transformV.rotation = toResetTo;
    return currentRotation;
}

//Determines and returns the bounding box points
private BoundingPointsType DetermineBounds()
{
    Vector3 maxBounds = childSprite.GetComponent<Renderer>().bounds.max;
    Vector3 minBounds = childSprite.GetComponent<Renderer>().bounds.min;

    BoundingPointsType boundingPoints = new BoundingPointsType(new Vector3(maxBounds.x, 1, maxBounds.z),
                                                               new Vector3(minBounds.x, 1, maxBounds.z),
                                                               new Vector3(maxBounds.x, 1, minBounds.z),
                                                               new Vector3(minBounds.x, 1, minBounds.z));
    return boundingPoints;
}

I've even tried manually setting the vertices like the following:

    Vector3 UpRight = transformV.position;
    Vector3 UpLeft = transformV.position;
    Vector3 LowRight = transformV.position;
    Vector3 LowLeft = transformV.position;

    UpRight.z += 1;
    UpRight.x += 1;

    UpLeft.z += 1;
    UpLeft.x -= 1;

    LowRight.z -= 1;
    LowRight.x += 1;

    LowLeft.z -= 1;
    LowLeft.x -= 1;


    Vector3[] vertices = new Vector3[4]
    {
        LowLeft,
        LowRight,
        UpRight,
        UpLeft
        //boundingPoints.LowerLeftPoint,
        //boundingPoints.LowerRightPoint,
        //boundingPoints.UpperRightPoint,
        //boundingPoints.UpperLeftPoint
    };

I'm at a loss, and have no idea what I'm doing wrong. My dynamic meshes seem to work perfectly fine for everything else in the game.

Thanks.

\$\endgroup\$
1
\$\begingroup\$

The vertices of a mesh are always in local space. In your DetermineBounds function you are returning world space coordinates, and the solution to this is to just use InverseTransformPoint. Somewhat like this:

Vector3 maxBounds = transform.InverseTransformPoint(childSprite.GetComponent<Renderer>().bounds.max);

Do the same for minBounds too. The rest of your code should remain the same. The quad should now be properly overlaying your sprite, unless the position of the quad and the sprite is different.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.