Esempio n. 1
0
        private void MouseCursorEvents()    // cursor position
        //Vector3 pos = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, 10.0f);
        {
            VoxelInfo raycast = Engine.VoxelRaycast(Camera.main.ScreenPointToRay(Input.mousePosition), 9999.9f, false);

            if (raycast != null)
            {
                // create a local copy of the hit voxel so we can call functions on it
                GameObject voxelObject = Instantiate(Engine.GetVoxelGameObject(raycast.GetVoxel())) as GameObject;

                // only execute this if the voxel actually has any events (either VoxelEvents component, or any component that inherits from it)
                if (voxelObject.GetComponent <VoxelEvents>() != null)
                {
                    voxelObject.GetComponent <VoxelEvents>().OnLook(raycast);

                    // for all mouse buttons, send events
                    for (int i = 0; i < 3; i++)
                    {
                        if (Input.GetMouseButtonDown(i))
                        {
                            voxelObject.GetComponent <VoxelEvents>().OnMouseDown(i, raycast);
                        }
                        if (Input.GetMouseButtonUp(i))
                        {
                            voxelObject.GetComponent <VoxelEvents>().OnMouseUp(i, raycast);
                        }
                        if (Input.GetMouseButton(i))
                        {
                            voxelObject.GetComponent <VoxelEvents>().OnMouseHold(i, raycast);
                        }
                    }
                }

                Destroy(voxelObject);
            }

            else
            {
                // disable selected block ui when no block is hit

                if (SelectedBlockGraphics != null)
                {
                    SelectedBlockGraphics.GetComponent <Renderer>().enabled = false;
                }
                if (CrossHairs != null)
                {
                    CrossHairs.GetComponent <Image>().color = Color.white;
                }
            }
        }
Esempio n. 2
0
        private void CameraLookEvents()    // first person camera
        {
            VoxelInfo raycast = Engine.VoxelRaycast(Camera.main.transform.position, Camera.main.transform.forward, Range, false);

            if (raycast != null)
            {
                // create a local copy of the hit voxel so we can call functions on it
                GameObject voxelObject = Instantiate(Engine.GetVoxelGameObject(raycast.GetVoxel())) as GameObject;

                // only execute this if the voxel actually has any events (either VoxelEvents component, or any component that inherits from it)
                if (voxelObject.GetComponent <VoxelEvents>() != null)
                {
                    voxelObject.GetComponent <VoxelEvents>().OnLook(raycast);

                    // for all mouse buttons, send events
                    for (int i = 0; i < 3; i++)
                    {
                        if (Input.GetMouseButtonDown(i))
                        {
                            voxelObject.GetComponent <VoxelEvents>().OnMouseDown(i, raycast);
                        }
                        if (Input.GetMouseButtonUp(i))
                        {
                            voxelObject.GetComponent <VoxelEvents>().OnMouseUp(i, raycast);
                        }
                        if (Input.GetMouseButton(i))
                        {
                            voxelObject.GetComponent <VoxelEvents>().OnMouseHold(i, raycast);
                        }
                    }
                }

                Destroy(voxelObject);
            }

            else
            {
                // disable selected block ui when no block is hit
                if (SelectedBlockGraphics != null)
                {
                    SelectedBlockGraphics.GetComponent <Renderer>().enabled = false;
                }
            }
        }
Esempio n. 3
0
        // a raycast which returns the index of the hit voxel and the gameobject of the hit chunk
        public static VoxelInfo VoxelRaycast(Vector3 origin, Vector3 direction, float range, bool ignoreTransparent)
        {
            RaycastHit hit = new RaycastHit();

            if (Physics.Raycast(origin, direction, out hit, range))
            {
                if (hit.collider.GetComponent <Chunk>() != null ||
                    hit.collider.GetComponent <ChunkExtension>() != null ||
                    hit.collider.GetComponent <Blocks>() != null)
                { // check if we're actually hitting a chunk
                    GameObject hitObject = hit.collider.gameObject;

                    if (hitObject.GetComponent <ChunkExtension>() != null || hit.collider.GetComponent <Blocks>() != null)
                    {                                                      // if we hit a mesh container instead of a chunk
                        hitObject = hitObject.transform.parent.gameObject; // swap the mesh container for the actual chunk object
                    }

                    Index hitIndex = hitObject.GetComponent <Chunk>().PositionToVoxelIndex(hit.point, hit.normal, false);

                    if (ignoreTransparent)
                    { // punch through transparent voxels by raycasting again when a transparent voxel is hit
                        ushort hitVoxel = hitObject.GetComponent <Chunk>().GetVoxel(hitIndex.x, hitIndex.y, hitIndex.z);
                        if (Engine.GetVoxelType(hitVoxel).VTransparency != Transparency.solid)
                        {                        // if the hit voxel is transparent
                            Vector3 newOrigin = hit.point;
                            newOrigin.y -= 0.5f; // push the new raycast down a bit
                            return(Engine.VoxelRaycast(newOrigin, Vector3.down, range - hit.distance, true));
                        }
                    }


                    return(new VoxelInfo(
                               hitObject.GetComponent <Chunk>().PositionToVoxelIndex(hit.point, hit.normal, false), // get hit voxel index
                               hitObject.GetComponent <Chunk>().PositionToVoxelIndex(hit.point, hit.normal, true),  // get adjacent voxel index
                               hitObject.GetComponent <Chunk>()));                                                  // get chunk
                }
            }

            // else
            return(null);
        }
Esempio n. 4
0
 public static VoxelInfo VoxelRaycast(Ray ray, float range, bool ignoreTransparent)
 {
     return(Engine.VoxelRaycast(ray.origin, ray.direction, range, ignoreTransparent));
 }