예제 #1
0
 // 用ScreenPointToRay就可以了
 public static void GetShootingRay(Camera camera, Vector3 mousePos, out Vector3 from, out Vector3 clickWorldPoint, out Vector3 normalDir)
 {
     if (camera.orthographic)
     {
         GeometryTool.GetGetShootingRayOrthographic(camera, mousePos, out from, out clickWorldPoint, out normalDir);
     }
     else
     {
         GeometryTool.GetShootingRayPerspective(camera, mousePos, out from, out clickWorldPoint, out normalDir);
     }
 }
예제 #2
0
        public void hitBox(Ray ray_camera_space)
        {
            // camera to world
            var ray = new Ray();

            ray.origin    = target_camera.transform.TransformPoint(ray_camera_space.origin);
            ray.direction = target_camera.transform.TransformVector(ray_camera_space.direction);

            // to local
            ray.origin    = transform.InverseTransformPoint(ray.origin);
            ray.direction = transform.InverseTransformVector(ray.direction);

            /*
             * 雖然荷蘭兄已經有提供教學
             * 但我還是想試試自己的方法
             *
             * group 0 : x+y+z=0
             * https://www.geogebra.org/m/jmznkghr
             *
             * group 1 : x+y+z=1
             * https://www.geogebra.org/m/h7nqemzh
             */

            // 擊中 x+y+z=2的平面
            Vector3 hit_pos;
            var     C      = new Vector3(1.0f / 3, 1.0f / 3, 4.0f / 3);
            var     N      = new Vector3(1, 1, 1).normalized;
            var     result = GeometryTool.RayHitPlane(ray.origin, ray.direction, N, C, out hit_pos);

            // 分組:看看取floor之後落在 x+y+z=0 還是 x+y+z=1
            var index = new Vector3(Mathf.Floor(hit_pos.x), Mathf.Floor(hit_pos.y), Mathf.Floor(hit_pos.z));
            var group = index.x + index.y + index.z;

            Debug.Log("group value:" + group + " ; " + index);

            if (group == 0)
            {
                hit_cube.localPosition = index;
            }
            else
            {
                hit_cube.localPosition = index + getNearBoxOffset(ref hit_pos);
            }

            Debug.DrawLine(transform.TransformPoint(ray.origin), transform.TransformPoint(hit_pos));
        }