0
\$\begingroup\$

I made the plane to work with mesh vertices

var plane = new Plane(
    hit.transform.InverseTransformDirection(normal),
    hit.transform.InverseTransformPoint(position)
);

If the object has the uniform scale then all looks good

uniform is good

but otherwise the plane normal has weird angle and I don't understand why.

non-uniform has problems

with InverseTransformVector even worser. The angle goes 2 times bigger

worser

Blue line - my plane
Green spheres - plane intersections
white sphere - plane center

Maybe it can be calculated directly with worldToLocalMatrix?

UPDATE:

Okay It is so weird for me. After InverseTransform plane prints in console right normal but has wrong real angle.

There is code to reproduce

Place component to default cube in zero position, rotate it with Y != 0, scale ONE horizontal dimesion. You will see onesided plane.

using UnityEngine;

public class PlaceOnCube : MonoBehaviour
{
    public Vector3 PlaneOrigin = new Vector3(0, 0, 0);
    public Vector3 PlaneNormal = new Vector3(1, 0, 0);

    private void OnDrawGizmos()
    {
        Gizmos.DrawSphere(PlaneOrigin, .03f);

        var plane = new Plane(
               transform.InverseTransformDirection(PlaneNormal),
               transform.InverseTransformPoint(PlaneOrigin)
               );

        ReGeneratePlane(plane);
    }

    private void ReGeneratePlane(Plane plane)
    {
        var meshfilter = transform.GetComponent<MeshFilter>();
        var mesh = meshfilter.mesh;
        var tris = mesh.triangles;
        var verts = mesh.vertices;
        var newTris = new int[36 + 6];
        var newVerts = new Vector3[24 + 4];

        for (int j = 0; j < verts.Length; j++)
            newVerts[j] = verts[j];

        newVerts[24] = plane.ClosestPointOnPlane(new Vector3(-2, -1, 0));
        newVerts[25] = plane.ClosestPointOnPlane(new Vector3(2, -1, 0));
        newVerts[26] = plane.ClosestPointOnPlane(new Vector3(-2, 1, 0));
        newVerts[27] = plane.ClosestPointOnPlane(new Vector3(2, 1, 0));

        for (int j = 0; j < tris.Length; j++)
            newTris[j] = tris[j];

        newTris[36] = 24;
        newTris[37] = 26;
        newTris[38] = 25;
        newTris[39] = 26;
        newTris[40] = 27;
        newTris[41] = 25;

        mesh.vertices = newVerts;
        mesh.triangles = newTris;

        meshfilter.mesh = mesh;
    }
}
\$\endgroup\$
3
  • \$\begingroup\$ Did you mean to use InverseTransformVector if you wanted scale to be included? \$\endgroup\$
    – DMGregory
    Commented Mar 3, 2021 at 15:23
  • \$\begingroup\$ @DMGregory I updated question. InverseTransformVector dont work \$\endgroup\$
    – ArtyGrand
    Commented Mar 3, 2021 at 15:33
  • 1
    \$\begingroup\$ Sounds like you should show us your code for placing these points so we can help you debug it. \$\endgroup\$
    – DMGregory
    Commented Mar 3, 2021 at 15:46

1 Answer 1

0
\$\begingroup\$

A good person suggested the correct answer on another resource

To fix the scaling, instead of

transform.InverseTransformDirection(PlaneNormal)

should use

 transform.localToWorldMatrix.transpose.MultiplyVector(PlaneNormal)
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .