Point in polygon with dot product?

I made some code to check when the player camera is in front of a quad and the camera point is projected on a plane that faces the same direction as the quad.

The vertices go around the quad clockwise order.

I used dot product to check if the camera point is inside the quad.

I wanted to use a cross product with it but, I had a problem with vector 3 and int in the if statement.

The code seems to work.

How do I use a cross product with the if statement in the loop?

???

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

public class PointInPolygon : MonoBehaviour
{
    private Mesh mesh;

    public bool PointInside;

    private Vector3 Point;

    private Vector3 CamPoint;

    public Vector3[] vertices;

    public List<Plane> Planes = new List<Plane>();

    public List<Vector3> tpvertices = new List<Vector3>();

    // Start is called before the first frame update
    void Start()
    {
        mesh = GetComponent<MeshFilter>().sharedMesh;

        int[] triangles = mesh.triangles;
        vertices = mesh.vertices;

        for (int i = 0; i < triangles.Length; i += 3)
        {
            Vector3 p1 = vertices[triangles[i + 0]];
            Vector3 p2 = vertices[triangles[i + 1]];
            Vector3 p3 = vertices[triangles[i + 2]];

            Vector3 tp1 = transform.TransformPoint(p1);
            Vector3 tp2 = transform.TransformPoint(p2);
            Vector3 tp3 = transform.TransformPoint(p3);

            Planes.Add(new Plane(tp1, tp2, tp3));
        }

        for (int i = 0; i < vertices.Length; i++)
        {
            Vector3 p = vertices[i];
            Vector3 tp = transform.TransformPoint(p);
            tpvertices.Add(tp);
        }
    }

    // Update is called once per frame
    void Update()
    {
        CamPoint = Camera.main.transform.position;

        Point = Vector3.ProjectOnPlane(CamPoint, Planes[0].normal);

        for (int i = 0; i < tpvertices.Count; i++)
        {
            int j = i + 1;

            if (j == tpvertices.Count)
            {
                j = 0;
            }

            Vector3 tp1 = tpvertices[i];
            Vector3 tp2 = tpvertices[j];

            float DotProduct = Vector3.Dot(Point - tp1, tp2 - tp1);

            PointInside = true;

            if (DotProduct < 0)
            {
                PointInside = false;
                break;
            }
        }

        if (PointInside == true)
        {
            Debug.Log("Point inside");
        }
    }
}

Is this just an experiment? You already say:

so… what are you trying to do? Just use another method than the one you are using?

I would start with google, perhaps something like:

test inside polygon with cross product

I was trying to find other ways to do point in polygon for 3D and the example code that I’m finding is for 2D.

The code above only works with quads, so I got the polygon triangles and did a point in triangle test.

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

public class PointInPolygon2 : MonoBehaviour
{
    public Mesh mesh;

    public int[] triangles;

    public Vector3[] vertices;

    public bool PointInside;

    private Vector3 Point;

    private Vector3 CamPoint;

    public Plane TriPlane;

    // Start is called before the first frame update
    void Start()
    {
        mesh = GetComponent<MeshFilter>().sharedMesh;

        triangles = mesh.triangles;
        vertices = mesh.vertices;

        Vector3 tp1 = transform.TransformPoint(vertices[triangles[0]]);
        Vector3 tp2 = transform.TransformPoint(vertices[triangles[1]]);
        Vector3 tp3 = transform.TransformPoint(vertices[triangles[2]]);

        TriPlane = new Plane(tp1, tp2, tp3);
    }

    // Update is called once per frame
    void Update()
    {
        CamPoint = Camera.main.transform.position;

        Point = Vector3.ProjectOnPlane(CamPoint, TriPlane.normal);

        for (int i = 0; i < triangles.Length; i += 3)
        {
            Vector3 tp1 = transform.TransformPoint(vertices[triangles[i + 0]]);
            Vector3 tp2 = transform.TransformPoint(vertices[triangles[i + 1]]);
            Vector3 tp3 = transform.TransformPoint(vertices[triangles[i + 2]]);

            PointInside = false;

            //Point in triangle test: blackpawn.com/texts/pointinpoly

            // Compute vectors
            Vector3 v0 = tp3 - tp1;
            Vector3 v1 = tp2 - tp1;
            Vector3 v2 = Point - tp1;

            // Compute dot products
            float dot00 = Vector3.Dot(v0, v0);
            float dot01 = Vector3.Dot(v0, v1);
            float dot02 = Vector3.Dot(v0, v2);
            float dot11 = Vector3.Dot(v1, v1);
            float dot12 = Vector3.Dot(v1, v2);

            // Compute barycentric coordinates
            float invDenom = 1 / (dot00 * dot11 - dot01 * dot01);
            float u = (dot11 * dot02 - dot01 * dot12) * invDenom;
            float v = (dot00 * dot12 - dot01 * dot02) * invDenom;

            // Check if point is in triangle
            if (u >= 0 && v >= 0 && u + v < 1)
            {
                PointInside = true;
                break;
            }
        }

        if (PointInside == true)
        {
            Debug.Log("Point inside");
        }
    }
}