예제 #1
0
    // Handle map in the leaf level, so no care face level
    //ArrayList findVisibleFaces(ArrayList visibleLeafs) {
    //    if (visibleLeafs == null) {
    //        throw new ArgumentException();
    //    }
    //    bool[] alreadyVisibleFaces = new bool[_faces.Length];
    //    ArrayList visibleFaces = new ArrayList();
    //    foreach(int i in visibleLeafs) {
    //        BSPTreeLeaf lf = _leafs[i];
    //        for(int j=lf.leafface; j<lf.leafface+lf.n_leaffaces; j++) {
    //            int faceIndex = _leaffaces[j];
    //            if(!alreadyVisibleFaces[faceIndex]) {
    //                alreadyVisibleFaces[faceIndex] = true;
    //                visibleFaces.Add(faceIndex);
    //            }
    //        }
    //    }
    //    return visibleFaces;
    //}

    // Handle map in the leaf level, so no care face level
    //ArrayList backfaceCulling(Camera cam, ArrayList visibleFaces) {
    //    ArrayList visible = new ArrayList ();
    //    foreach (int i in visibleFaces) {
    //        BSPFileParser.Face f = _faces[i];
    //        Vector3 normal = Right2Left(new Vector3(f.normal[0], f.normal[1], f.normal[2]));
    //        if(Vector3.Dot(cam.transform.forward, normal) < BACKCULLINGFACTOR) {
    //            visible.Add(i);
    //        }
    //    }
    //    return visible;
    //}

    int findLeafIndex(Vector3 position)
    {
        if (position == null)
        {
            throw new ArgumentException();
        }

        int index = 0;

        while (index >= 0)
        {
            BSPFileParser.BSPTreeNode node  = _nodes[index];
            UnityEngine.Plane         plane = planes[node.plane];
            if (plane.GetSide(position))
            {
                index = node.children[0];
            }
            else
            {
                index = node.children[1];
            }
        }
        // When index < 0, meaning this is a BSP Leaf Index
        // convert into correct leaf index: -index-1
        return(-index - 1);
    }
예제 #2
0
    int findLeaf(Vector3 position)
    {
        if (position == null)
        {
            throw new ArgumentException();
        }
        int index = 0;

        while (index >= 0)
        {
            //Debug.Log(index);
            BSPFileParser.BSPTreeNode node  = _nodes[index];
            UnityEngine.Plane         plane = planes[node.plane];
            float distance = plane.GetDistanceToPoint(position);
            if (distance >= 0)
            {
                index = node.children[0];
            }
            else
            {
                index = node.children[1];
            }
        }
        // When index < 0, meaning this is a BSP Leaf Index
        // convert into correct leaf index: -index-1
        return(-index - 1);
    }
예제 #3
0
    void GizmosDrawCameraInsideLeaf(Camera cam)
    {
        Color tc = Gizmos.color;

        Color[] cs         = new Color[] { Color.gray, Color.yellow, Color.magenta, Color.red }; // from light to dark
        int     colorIndex = 0;

        Vector3 position = cam.transform.position;

        // Camera
        Gizmos.DrawCube(position, new Vector3(200, 200, 200));
        int index = 0;

        while (index >= 0)
        {
            Gizmos.color = cs[colorIndex++ % cs.Length];
            BSPFileParser.BSPTreeNode node  = _nodes[index];
            UnityEngine.Plane         plane = planes[node.plane];
            Bounds b = new Bounds();
            b.SetMinMax(new Vector3(node.mins[0], node.mins[1], node.mins[2]),
                        new Vector3(node.maxs[0], node.maxs[1], node.maxs[2]));
            b = Right2Left(b);
            // Draw Sub-Space(BSP Tree Node bounding box)
            Gizmos.DrawWireCube(b.center, b.size);
            // Draw Split-Plane
            GizmosDrawPlane(GetOnePointOnPlane(plane), plane.normal, 5000.0f);
            if (plane.GetSide(position))
            {
                index = node.children[0];
                //Debug.Log("Front");
            }
            else
            {
                index = node.children[1];
                //Debug.Log("Back");
            }
        }

        // Draw BSP Tree Leaf bounding box
        BSPFileParser.BSPTreeLeaf lf = _leafs[-index - 1];
        Bounds temp = new Bounds();

        temp.SetMinMax(new Vector3(lf.mins[0], lf.mins[1], lf.mins[2]),
                       new Vector3(lf.maxs[0], lf.maxs[1], lf.maxs[2]));
        temp         = Right2Left(temp);
        Gizmos.color = Color.white;
        Gizmos.DrawWireCube(temp.center, temp.size);

        Gizmos.color = tc;
    }
예제 #4
0
    int findLeafIndex(Vector3 position)
    {
        if (position == null)
        {
            throw new ArgumentException();
        }

        int index = 0;

        while (index >= 0)
        {
            BSPFileParser.BSPTreeNode node  = _nodes[index];
            UnityEngine.Plane         plane = planes[node.plane];
            if (plane.GetSide(position))
            {
                index = node.children[0];
            }
            else
            {
                index = node.children[1];
            }
        }
        return(-index - 1);
    }