Get a normalized position related to a plane

Hello people, I’m working in a projection system involving planes. There are 2 types of planes in my system, the bigger ones represent static textures panels, while the smaller ones represent decals and are mobile.

The thing is, I need to know the normalized position of the mobile planes within the bigger ones and I don’t know how to that. This is one of those things who are better represented with an image support, so I invested a lot of time (~4 minutes) in order to exemplify better:

Anyway, I believe that this shouldn’t be that complicated, but no matter how much time I dedicate in Unity (no sarcasm this time), I’m still struggle with these seemingly basic problems…

The general solution for this is to use Mathf.InverseLerp() on each of the blue coordinates in the left picture… then normally one would take each of those fractions and look them up with Mathf.Lerp() in your right coordinate system… which in this case would NOT be necessary as Mathf.Lerp (0,1, alpha) is just an identity function.

Thanks, this method seems to be useful for my goal. It happens that I need more than the position of the decal plane, I need it’s vertices normalized positions related to the bigger plane. It’s a simple plane with just 4 vertices, but I’ve been struggling to find consistent values in the 4 directions. Here’s my current code:

void  NormalizeDecalLocation()
    {

        var vertices = decalMeshFilter.mesh.vertices;

        //LEFT BOTTOM VERTEX
        var xVertex = vertices[0].x + meshFilter.transform.position.x;
        //LEFT TOP VERTEX
        var yVertex = vertices[1].y + meshFilter.transform.position.y;
        //RIGHT TOP VERTEX
        var zVertex = vertices[2].x + meshFilter.transform.position.x;
        //LEFT BOTTOM VERTEX
        var wVertex = vertices[3].y + meshFilter.transform.position.y;

        var minXSize = biggerPlane.GetComponent<MeshFilter>().mesh.bounds.min.x;
        var maxXSize = biggerPlane.GetComponent<MeshFilter>().mesh.bounds.max.x;
        var minYSize = biggerPlane.GetComponent<MeshFilter>().mesh.bounds.min.y;
        var maxYSize = biggerPlane.GetComponent<MeshFilter>().mesh.bounds.max.y;

        var xNormalized = Mathf.InverseLerp(minXSize, maxXSize, xVertex);
        var yNormalized = Mathf.InverseLerp(minYSize, maxYSize, yVertex);
        var zNormalized = Mathf.InverseLerp(maxXSize, minXSize, zVertex);
        var wNormalized = Mathf.InverseLerp(maxYSize, minYSize, wVertex);

        Debug.Log("X coord: " + xNormalized);
        Debug.Log("Y coord: " + yNormalized);
        Debug.Log("Z coord: " + zNormalized);
        Debug.Log("W coord: " + wNormalized);
    }

I have a reference of what would be the correct results, with the code above, the X and Z coordinates (representing the lateral edges of the plane) present consistent results, while the Y and W coordinates (representing the top and bottom edges) aren’t consistent. Not sure what I’m missing this time.

I question your lines 6 to 13 above… you’re manipulating 4 floats when you need to be changing 4 Vector3s… the most that xVertex/yVertex/zVertex/wVertex could ever hold would be two corners, not four corners.

Don’t call those variables “vertexes”… they are just a scalar.

I just need 1 axis position per vertex actually, that’s why I deal with just one coordinate each time.

I did an example on Blender to check the positions manually and it worked as expected, but I’m failing to translate this to code. This is an image of my example in blender with each coordinate highlighted:


Note that I’ve selected the vertices in pairs wrongfully/unnecessarily, it should be one of each corner actually, this way the system would work for rectangular decals too. Anyway, those highlighted values are the correct ones, the whole point is to pass a decal mesh to the NormalizeDecalLocation() method and receive a Vector4 in return, with 4 coordinates from 0 to 1, according the position of the decal plane within the bigger plane. For now, it’s void and without parameters because I’m still testing…

Hopefully this helps you to understand my goal better.