/**
  * Query for spefici voxel using hashkey
  * @param hashkey haskey of the target voxel
  * @return the voxel if it exsits, otherwise null
  */
 public Voxel Query(int hashkey)
 {
     if (hashkey == m_hashKey)
     {
         return(m_voxel);
     }
     if (hashkey < m_hashKey)
     {
         if (m_leftHashTree == null)
         {
             return(null);
         }
         else
         {
             return(m_leftHashTree.Query(hashkey));
         }
     }
     else
     {
         if (m_rightHashTree == null)
         {
             return(null);
         }
         else
         {
             return(m_rightHashTree.Query(hashkey));
         }
     }
 }
Exemplo n.º 2
0
    /// <summary>
    /// Raycast into this meshing cube for a list of voxels that are intersected.
    /// </summary>
    /// <returns>List of populated voxels that are intersected by the ray.</returns>
    /// <param name="start">Start point of the ray.</param>
    /// <param name="stop">Stop point of the ray.</param>
    /// <param name="dir">The unit direction vector of the ray.</param>
    public List <Voxel> RayCastVoxelHitlist(Vector3 start, Vector3 stop, Vector3 dir)
    {
        List <Voxel> hits    = new List <Voxel>();
        List <int>   hitKeys = new List <int>();

        Vector3 adjustedStart = start - transform.position;
        Vector3 adjustedStop  = stop - transform.position;

        ClipRayToBounds(ref adjustedStart, dir);
        ClipRayToBounds(ref adjustedStop, dir);

        // converts it to integer space
        adjustedStart *= m_voxelResolution;
        adjustedStop  *= m_voxelResolution;

        // x crosses
        if (dir.x > 0)
        {
            for (float x = Mathf.Ceil(adjustedStart.x) + m_epsilon; x < adjustedStop.x; x++)
            {
                float scale = (x - adjustedStart.x) / dir.x;
                hitKeys.Add(ComputeVoxelHashKey(adjustedStart + (scale * dir)));
            }
        }
        else
        {
            for (float x = Mathf.Floor(adjustedStart.x) - m_epsilon; x > adjustedStop.x; x -= 1)
            {
                float scale = (x - adjustedStart.x) / dir.x;
                hitKeys.Add(ComputeVoxelHashKey(adjustedStart + (scale * dir)));
            }
        }

        // y crosses
        if (dir.y > 0)
        {
            for (float y = Mathf.Ceil(adjustedStart.y) + m_epsilon; y < adjustedStop.y; y += 1)
            {
                float scale = (y - adjustedStart.y) / dir.y;
                hitKeys.Add(ComputeVoxelHashKey(adjustedStart + (scale * dir)));
            }
        }
        else
        {
            for (float y = Mathf.Floor(adjustedStart.y) - m_epsilon; y > adjustedStop.y; y -= 1)
            {
                float scale = (y - adjustedStart.y) / dir.y;
                hitKeys.Add(ComputeVoxelHashKey(adjustedStart + (scale * dir)));
            }
        }

        // z crosses
        if (dir.z > 0)
        {
            for (float z = Mathf.Ceil(adjustedStart.z) + m_epsilon; z < adjustedStop.z; z += 1)
            {
                float scale = (z - adjustedStart.z) / dir.z;
                hitKeys.Add(ComputeVoxelHashKey(adjustedStart + (scale * dir)));
            }
        }
        else
        {
            for (float z = Mathf.Floor(adjustedStart.z) - m_epsilon; z > adjustedStop.z; z -= 1)
            {
                float scale = (z - adjustedStart.z) / dir.z;
                hitKeys.Add(ComputeVoxelHashKey(adjustedStart + (scale * dir)));
            }
        }

        foreach (int key in hitKeys)
        {
            Voxel v = voxelStorage.Query(key);
            if (v != null)
            {
                hits.Add(v);
            }
        }

        return(hits);
    }