void Start() { inter = new intersector(); //test cube pool pool = new DCube_pool(); dCube_rays = new List <DCube_Ray>(); //camera rays cam = this.gameObject.GetComponent(typeof(Camera)) as Camera; // print("cam wolr position :" +cam.transform.position); w_rays = 10; h_rays = (int)((float)w_rays * cam.pixelHeight / cam.pixelWidth); cam_rays = new List <AB_RAY>(); for (int i = 0; i < h_rays; i++) { //need to create the last ray at right edge for (int j = 0; j < w_rays; j++) { //create ray at correct gap Ray myRay = cam.ViewportPointToRay(new Vector3(1.0f / (w_rays - 1) * j, 1.0f / (h_rays - 1) * i, 0)); GameObject A_object = new GameObject(); A_object.transform.position = myRay.origin; Vector3 B = (myRay.origin - cam.transform.position) * cam.farClipPlane / cam.nearClipPlane + cam.transform.position; GameObject B_object = new GameObject(); B_object.transform.position = B; // DCube fc = pool.getDCube(); // fc.position = B; // fc.setParent(cam.transform); // DCube nc = pool.getDCube(); // nc.position = myRay.origin; // nc.setParent(cam.transform); AB_RAY cam_ab_ray = new AB_RAY(A_object.transform, B_object.transform); cam_rays.Add(cam_ab_ray); } } //test the cube is a obb or aabb if (is_aabb) { aabb = new AABB(test_cube); //debug cubes for a ray dcubes = new DCube_Ray(pool); } else { obb = new OBB(test_cube); //debug cubes for a ray dcubes = new DCube_Ray(pool); } //create ray ab_ray = new AB_RAY(cam.transform, GetComponent <Transform>().Find("cam_ray_handeler")); //ray march parameters max_distance = cam.farClipPlane - cam.nearClipPlane; step_size = max_distance / max_steps; }
void cam_rays_marching() { for (int i = 0; i < h_rays; i++) { for (int j = 0; j < w_rays; j++) { AB_RAY _ray = cam_rays[i * w_rays + j]; Debug.DrawLine(_ray.PA.position, _ray.PB.position, Color.gray); if (is_aabb) { // inter_point inter_p = inter.aabb_intersection(ab_ray , aabb , dcubes); // print("inter_p.p0_w " + inter_p.p0_world ); } else { // obb_intersection(ab_ray , obb); inter_point inter_p = inter.obb_intersection_cube(_ray, obb, dcubes); if (inter_p.p0_exist && inter_p.p1_exist) { rayMarch2(inter_p, step_size, max_distance, max_steps, obb, cam.transform.position); } } } } }
public void obb_intersection(AB_RAY _ray, OBB _box, DCube_Ray _dcubes) { Vector3 min_inter, max_inter; // intersection point bool min_exist, max_exist; Vector3 ray_min = _box.min - _ray.PA.position; Vector3 ray_max = _box.max - _ray.PA.position; Vector3 xyz_sclae_min; Vector3 xyz_sclae_max; //full ray on x , y , z value Vector3 ray_projected = new Vector3(Vector3.Dot(_box.x_axis, _ray.fullRay), Vector3.Dot(_box.y_axis, _ray.fullRay), Vector3.Dot(_box.z_axis, _ray.fullRay)); ray_projected.x = ray_projected.x == 0?0.0000001f : ray_projected.x; ray_projected.y = ray_projected.y == 0?0.0000001f : ray_projected.y; ray_projected.z = ray_projected.z == 0?0.0000001f : ray_projected.z; float _x1 = Vector3.Dot(_box.x_axis, ray_min) / ray_projected.x; float _x2 = Vector3.Dot(_box.x_axis, ray_max) / ray_projected.x; xyz_sclae_min.x = _x1 < _x2 ? _x1 : _x2; xyz_sclae_max.x = _x1 > _x2 ? _x1 : _x2; float _y1 = Vector3.Dot(_box.y_axis, ray_min) / ray_projected.y; float _y2 = Vector3.Dot(_box.y_axis, ray_max) / ray_projected.y; xyz_sclae_min.y = _y1 < _y2 ? _y1 : _y2; xyz_sclae_max.y = _y1 > _y2 ? _y1 : _y2; float _z1 = Vector3.Dot(_box.z_axis, ray_min) / ray_projected.z; float _z2 = Vector3.Dot(_box.z_axis, ray_max) / ray_projected.z; xyz_sclae_min.z = _z1 < _z2 ? _z1 : _z2; xyz_sclae_max.z = _z1 > _z2 ? _z1 : _z2; float min_scale = Mathf.Max(Mathf.Max(xyz_sclae_min.x, xyz_sclae_min.y), xyz_sclae_min.z); float max_scale = Mathf.Min(Mathf.Min(xyz_sclae_max.x, xyz_sclae_max.y), xyz_sclae_max.z); min_exist = false; max_exist = false; if (min_scale < max_scale) { if (min_scale > 0 && min_scale < 1) { min_exist = true; } if (max_scale > 0 && max_scale < 1) { max_exist = true; } } //debug info display _dcubes.p0.position = _ray.PA.position + min_scale * _ray.fullRay; _dcubes.p1.position = _ray.PA.position + max_scale * _ray.fullRay; _dcubes.x0.position = _ray.PA.position + xyz_sclae_min.x * _ray.fullRay; _dcubes.x1.position = _ray.PA.position + xyz_sclae_max.x * _ray.fullRay; _dcubes.y0.position = _ray.PA.position + xyz_sclae_min.y * _ray.fullRay; _dcubes.y1.position = _ray.PA.position + xyz_sclae_max.y * _ray.fullRay; _dcubes.z0.position = _ray.PA.position + xyz_sclae_min.z * _ray.fullRay; _dcubes.z1.position = _ray.PA.position + xyz_sclae_max.z * _ray.fullRay; if (!min_exist) { _dcubes.p0.release(); } else { _dcubes.p0.use(); } if (!max_exist) { _dcubes.p1.release(); } else { _dcubes.p1.use(); } }
public inter_point obb_intersection_cube(AB_RAY _ray, OBB _box, DCube_Ray _dcubes) { Vector3 _PA_pos_cube = _box.w2o.MultiplyPoint3x4(_ray.PA.position); Vector3 _PB_pos_cube = _box.w2o.MultiplyPoint3x4(_ray.PB.position); //object space Vector3 ray_full = _PB_pos_cube - _PA_pos_cube; Vector3 min_inter, max_inter; // intersection point bool min_exist, max_exist; Vector3 ray_min = _box.min_o - _PA_pos_cube; Vector3 ray_max = _box.max_o - _PA_pos_cube; Vector3 xyz_sclae_min; Vector3 xyz_sclae_max; //full ray on x , y , z value Vector3 ray_projected = new Vector3(ray_full.x, ray_full.y, ray_full.z); ray_projected.x = ray_projected.x == 0?0.0000001f : ray_projected.x; ray_projected.y = ray_projected.y == 0?0.0000001f : ray_projected.y; ray_projected.z = ray_projected.z == 0?0.0000001f : ray_projected.z; float _x1 = ray_min.x / ray_projected.x; float _x2 = ray_max.x / ray_projected.x; xyz_sclae_min.x = _x1 < _x2 ? _x1 : _x2; xyz_sclae_max.x = _x1 > _x2 ? _x1 : _x2; float _y1 = ray_min.y / ray_projected.y; float _y2 = ray_max.y / ray_projected.y; xyz_sclae_min.y = _y1 < _y2 ? _y1 : _y2; xyz_sclae_max.y = _y1 > _y2 ? _y1 : _y2; float _z1 = ray_min.z / ray_projected.z; float _z2 = ray_max.z / ray_projected.z; xyz_sclae_min.z = _z1 < _z2 ? _z1 : _z2; xyz_sclae_max.z = _z1 > _z2 ? _z1 : _z2; float min_scale = Mathf.Max(Mathf.Max(xyz_sclae_min.x, xyz_sclae_min.y), xyz_sclae_min.z); float max_scale = Mathf.Min(Mathf.Min(xyz_sclae_max.x, xyz_sclae_max.y), xyz_sclae_max.z); //two intersect point in object space Vector3 p0_o = _PA_pos_cube + Mathf.Max(Mathf.Max(xyz_sclae_min.x, xyz_sclae_min.y), xyz_sclae_min.z) * ray_full; Vector3 p1_o = _PA_pos_cube + Mathf.Min(Mathf.Min(xyz_sclae_max.x, xyz_sclae_max.y), xyz_sclae_max.z) * ray_full; //two intersect point in object space Vector3 p0_w = _box.o2w.MultiplyPoint3x4(p0_o); Vector3 p1_w = _box.o2w.MultiplyPoint3x4(p1_o); min_exist = false; max_exist = false; if (min_scale < max_scale) { if (min_scale > 0 && min_scale < 1) { min_exist = true; } if (max_scale > 0 && max_scale < 1) { max_exist = true; } } //debug info display _dcubes.p0.position = p0_w; _dcubes.p1.position = p1_w; _dcubes.x0.position = _ray.PA.position + xyz_sclae_min.x * _ray.fullRay; _dcubes.x1.position = _ray.PA.position + xyz_sclae_max.x * _ray.fullRay; _dcubes.y0.position = _ray.PA.position + xyz_sclae_min.y * _ray.fullRay; _dcubes.y1.position = _ray.PA.position + xyz_sclae_max.y * _ray.fullRay; _dcubes.z0.position = _ray.PA.position + xyz_sclae_min.z * _ray.fullRay; _dcubes.z1.position = _ray.PA.position + xyz_sclae_max.z * _ray.fullRay; if (!min_exist) { _dcubes.p0.release(); } else { _dcubes.p0.use(); } if (!max_exist) { _dcubes.p1.release(); } else { _dcubes.p1.use(); } inter_point inter = new inter_point(p0_o, p1_o, p0_w, p1_w, min_exist, max_exist); return(inter); }
public inter_point aabb_intersection(AB_RAY _ray, AABB _box, DCube_Ray _dcubes) { bool min_exist, max_exist; Vector3 ray_min = _box.min - _ray.PA.position; Vector3 ray_max = _box.max - _ray.PA.position; Vector3 xyz_sclae_min; Vector3 xyz_sclae_max; Vector3 fullRay = new Vector3(_ray.fullRay.x == 0? 0.0001f : _ray.fullRay.x, _ray.fullRay.y == 0? 0.0001f : _ray.fullRay.y, _ray.fullRay.z == 0? 0.0001f : _ray.fullRay.z); float _x1 = ray_min.x / _ray.fullRay.x; float _x2 = ray_max.x / _ray.fullRay.x; xyz_sclae_min.x = _x1 < _x2 ? _x1 : _x2; xyz_sclae_max.x = _x1 > _x2 ? _x1 : _x2; float _y1 = ray_min.y / _ray.fullRay.y; float _y2 = ray_max.y / _ray.fullRay.y; xyz_sclae_min.y = _y1 < _y2 ? _y1 : _y2; xyz_sclae_max.y = _y1 > _y2 ? _y1 : _y2; float _z1 = ray_min.z / _ray.fullRay.z; float _z2 = ray_max.z / _ray.fullRay.z; xyz_sclae_min.z = _z1 < _z2 ? _z1 : _z2; xyz_sclae_max.z = _z1 > _z2 ? _z1 : _z2; float min_scale = Mathf.Max(Mathf.Max(xyz_sclae_min.x, xyz_sclae_min.y), xyz_sclae_min.z); float max_scale = Mathf.Min(Mathf.Min(xyz_sclae_max.x, xyz_sclae_max.y), xyz_sclae_max.z); min_exist = false; max_exist = false; if (min_scale < max_scale) { if (min_scale > 0 && min_scale < 1) { min_exist = true; } if (max_scale > 0 && max_scale < 1) { max_exist = true; } } _dcubes.p0.position = _ray.PA.position + min_scale * _ray.fullRay; _dcubes.p1.position = _ray.PA.position + max_scale * _ray.fullRay; _dcubes.x0.position = _ray.PA.position + xyz_sclae_min.x * _ray.fullRay; _dcubes.x1.position = _ray.PA.position + xyz_sclae_max.x * _ray.fullRay; _dcubes.y0.position = _ray.PA.position + xyz_sclae_min.y * _ray.fullRay; _dcubes.y1.position = _ray.PA.position + xyz_sclae_max.y * _ray.fullRay; _dcubes.z0.position = _ray.PA.position + xyz_sclae_min.z * _ray.fullRay; _dcubes.z1.position = _ray.PA.position + xyz_sclae_max.z * _ray.fullRay; if (!min_exist) { _dcubes.p0.release(); } else { _dcubes.p0.use(); } if (!max_exist) { _dcubes.p1.release(); } else { _dcubes.p1.use(); } inter_point inter = new inter_point(Vector3.zero, Vector3.zero, _dcubes.p0.position, _dcubes.p1.position, min_exist, max_exist); return(inter); }