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
but otherwise the plane normal has weird angle and I don't understand why.
with InverseTransformVector even worser. The angle goes 2 times bigger
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;
}
}