Beispiel #1
0
    public static Vector3 ProjectPointToPlane(Vector3 point, DPlane plane)
    {
        Vector3 originToPoint = point - plane.Origin;
        float   dist          = Vector3.Dot(originToPoint, plane.Normal);

        return(point - dist * plane.Normal);
    }
Beispiel #2
0
    public void SetVerts(List <Vector2> vertices2D, DPlane plane)
    {
        transform.rotation = Quaternion.identity;
        List <Vector2>    toTriangulate = new List <Vector2>();
        HashSet <Vector2> duplicates    = new HashSet <Vector2>();

        for (int i = vertices2D.Count - 1; i > 0; i--)
        {
            Vector3 toInsert = vertices2D[i];
            if (duplicates.Contains(toInsert))
            {
                continue;         // Don't allow dups
            }
            duplicates.Add(toInsert);
            toTriangulate.Add(toInsert);
        }

        // Use the triangulator to get indices for creating triangles
        Triangulator tr = new Triangulator(toTriangulate.ToArray());

        int[] indices = tr.Triangulate();

        // Create the Vector3 vertices
        if (_vertices == null || _vertices.Length != vertices2D.Count)
        {
            _vertices = new Vector3[vertices2D.Count];
            _normals  = new Vector3[vertices2D.Count];
            _uvs      = new Vector2[vertices2D.Count];
        }
        float epsilonOffset = 0.001f;

        for (int i = 0; i < toTriangulate.Count; i++)
        {
            _normals[i]  = plane.Normal;
            _vertices[i] = ProjectionMath.ThreeDimCoordsOnPlane(new Vector2(-toTriangulate[i].x, toTriangulate[i].y), plane) + plane.Normal * epsilonOffset;
            // TODO(Julian): set the uvs if we need shadow textures
        }
        Mesh mesh = _meshFilter.mesh;

        mesh.vertices  = _vertices;
        mesh.triangles = indices;
        mesh.normals   = _normals;
        mesh.uv        = _uvs;
        mesh.RecalculateBounds();
    }
Beispiel #3
0
    private IntVector2D MouseLocOnPlane(DPlane plane)
    {
        Ray   ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        float rayDistance;

        if (plane.MathPlane.Raycast(ray, out rayDistance))
        {
            Vector3     planePos   = ray.GetPoint(rayDistance);
            Vector2     planePos2D = ProjectionMath.TwoDimCoordsOnPlane(planePos, plane);
            IntVector2D tileLoc    = new IntVector2D(planePos2D / _tileSize - Vector2.one * _tileSize / 2f);
            if (plane.Orientation == PlaneOrientation.XY)
            {
                tileLoc.x *= -1;
                tileLoc.x -= 1;
            }
            return(tileLoc);
        }
        return(new IntVector2D(-1, -1));
    }
Beispiel #4
0
 public static Vector3 ThreeDimCoordsOnPlane(Vector2 point, DPlane plane)
 {
     return((point.x * plane.Right) + (point.y * plane.Up) + plane.Origin);
 }
Beispiel #5
0
 public static Vector2 TwoDimCoordsOnPlane(Vector3 point, DPlane plane)
 {
     return(new Vector2(Vector3.Dot(point - plane.Origin, plane.Right), Vector3.Dot(point - plane.Origin, plane.Up)));
 }