Example #1
0
        /*public static bool Intersect(Ray a, Content.Model9 b, out object intersection)
         * {
         *
         *  if (b.XMesh != null)
         *      return Intersect(a, new BoundingMetaMesh
         *      {
         *          Mesh = b.XMesh,
         *          Tranformation = b.World
         *      }, out intersection);
         *  else if (b.SkinnedMesh != null)
         *      return Intersect(a, b.SkinnedMesh, out intersection);
         *
         *  intersection = null;
         *  return false;
         * }*/

        /*public static bool Intersect(Ray a, Content.SkinnedMesh b, out object intersection)
         * {
         *  float minD = float.MaxValue;
         *  foreach (var v in b.MeshContainers)
         *  {
         *      float d;
         *      if (v.Second.MeshData != null &&
         *          v.Second.MeshData.Mesh != null)
         *      {
         *          Matrix invTransformation = Matrix.Invert(v.First.CombinedTransform);
         *          var newRay = new Ray(
         *              Vector3.TransformCoordinate(a.Position, invTransformation),
         *              Vector3.Normalize(Vector3.TransformNormal(a.Direction, invTransformation)));
         *
         *          if(v.Second.MeshData.Mesh.Intersects(newRay, out d))
         *          {
         *              var newPos = Vector3.TransformCoordinate(newRay.Position + newRay.Direction * d,
         *                  v.First.CombinedTransform);
         *              d = (newPos - a.Position).Length();
         *              if(d < minD)
         *                  minD = d;
         *          }
         *      }
         *  }
         *  intersection = new Common.RayIntersection { Distance = minD };
         *  return minD != float.MaxValue;
         * }*/


        public static bool Intersect(Ray a, Renderer.Renderer.EntityAnimation b, out object intersection)
        {
            float minD = float.MaxValue;

            foreach (var v in b.StoredFrameMatrices)
            {
                if (v.Key != null)
                {
                    foreach (var m in v.Value)
                    {
                        float  d;
                        Matrix invTransformation = Matrix.Invert(m[0]);
                        var    newRay            = new Ray(
                            Vector3.TransformCoordinate(a.Position, invTransformation),
                            Vector3.Normalize(Vector3.TransformNormal(a.Direction, invTransformation)));

                        if (v.Key.Intersects(newRay, out d))
                        {
                            var newPos = Vector3.TransformCoordinate(newRay.Position + newRay.Direction * d,
                                                                     m[0]);
                            d = (newPos - a.Position).Length();
                            if (d < minD)
                            {
                                minD = d;
                            }
                        }
                    }
                }
            }
            intersection = new Common.RayIntersection {
                Distance = minD
            };
            return(minD != float.MaxValue);
        }
 public static bool Intersect(Triangle triangle, Ray ray, out object intersection)
 {
     Common.RayIntersection r = new Common.RayIntersection();
     Vector2 uv;
     bool hit = triangle.Intersect(ray, false, out r.Distance, out uv);
     r.Userdata = uv;
     intersection = r;
     return hit;
 }
Example #3
0
        // ----------------------------------------------------------------------------------------------
        // -- BoundingMetaMesh --------------------------------------------------------------------------
        // ----------------------------------------------------------------------------------------------

        public static bool Intersect(Ray a, BoundingMetaMesh b, out object intersection)
        {
            var r = new Common.RayIntersection();

            intersection = r;
            Matrix invTransformation = Matrix.Invert(b.Transformation);
            var    newRay            = new Ray(
                Vector3.TransformCoordinate(a.Position, invTransformation),
                Vector3.Normalize(Vector3.TransformNormal(a.Direction, invTransformation)));

            if (b.XMesh != null)
            {
#if BOUNDING_META_MESH_REAQUIRE_ON_DISPOSED
                if (b.XMesh.Disposed)
                {
                    b.Init(content);
                }
#endif
                if (!b.XMesh.Intersects(newRay, out r.Distance))
                {
                    return(false);
                }
            }
            else if (b.Mesh != null)
            {
                var mesh = content.Peek <SlimDX.Direct3D9.Mesh>(b.Mesh);
                if (!mesh.Intersects(newRay, out r.Distance))
                {
                    return(false);
                }
            }
            else if (b.SoftwareMesh != null)
            {
                Vector2 uv;
                Graphics.Software.Triangle triangle;
                if (!b.SoftwareMesh.Intersect(newRay, false, out triangle, out r.Distance, out uv))
                {
                    return(false);
                }
            }
            else if (b.SkinnedMeshInstance != null)
            {
                newRay = a;
                var    sm = content.Peek <Renderer.Renderer.EntityAnimation>(b.SkinnedMeshInstance);
                object o;
                if (!Intersect(newRay, sm, out o))
                {
                    return(false);
                }
                r = (Common.RayIntersection)o;
            }

            var newPos = Vector3.TransformCoordinate(newRay.Position + newRay.Direction * r.Distance,
                                                     b.Transformation);
            r.Distance = (newPos - a.Position).Length();
            return(true);
        }
        public static bool Intersect(Triangle triangle, Ray ray, out object intersection)
        {
            Common.RayIntersection r = new Common.RayIntersection();
            Vector2 uv;
            bool    hit = triangle.Intersect(ray, false, out r.Distance, out uv);

            r.Userdata   = uv;
            intersection = r;
            return(hit);
        }
Example #5
0
        public static bool Intersect(Ray a, Software.Mesh b, out object intersection)
        {
            var r = new Common.RayIntersection();

            intersection = r;
            Software.Triangle t;
            Vector2           uv;
            bool hit = b.Intersect(a, false, out t, out r.Distance, out uv);

            r.Userdata = uv;
            return(hit);
        }
        public static bool Intersect(Ray a, MetaBoundingImageGraphic b, out object intersection)
        {
#if false
            var model = content.Peek<Content.Model9>(b.Graphic);
            var sd = model.Texture.GetLevelDescription(0);
            var plane = b.Graphic.GetPlane(new Vector2(sd.Width, sd.Height));
            var mesh = Software.Meshes.Construct(plane);
            var texture = new Software.Texture<Software.Texel.A8R8G8B8>(model.Texture, 0);
            
            Matrix invTransformation = Matrix.Invert(Matrix.Scaling(1, 1, -1) * b.Transformation);
            var newRay = new Ray(
                Vector3.TransformCoordinate(a.Position, invTransformation),
                Vector3.Normalize(Vector3.TransformNormal(a.Direction, invTransformation)));

            Software.Triangle t;
            float d;
            Vector2 uv;
            //mesh.BuildKDTree();
            bool hit = mesh.Intersect(newRay, false, out t, out d, out uv);
            intersection = null;
            if (!hit) return false;

            var newPos = Vector3.TransformCoordinate(newRay.Position + newRay.Direction * d,
                b.Transformation);

            intersection = new Common.RayIntersection { Distance = (newPos - a.Position).Length() };
            var v = texture.Sample(uv);
            Console.WriteLine(uv);
            return v.R > 0.5f;
#endif
            var t = content.Peek<SlimDX.Direct3D9.Texture>(b.Graphic.Texture);
            var sd = t.GetLevelDescription(0);
            var plane = b.Graphic.GetPlane(new Vector2(sd.Width, sd.Height));
            Matrix invTransformation = Matrix.Invert(Matrix.Scaling(1, 1, -1) * b.Transformation);
            var newRay = new Ray(
                Vector3.TransformCoordinate(a.Position, invTransformation),
                Vector3.Normalize(Vector3.TransformNormal(a.Direction, invTransformation)));

            float worldX = newRay.Position.X - plane.Position.X;
            float worldY = newRay.Position.Y - plane.Position.Y;

            float percX = worldX;
            float percY = worldY;

            float u = plane.UVMin.X + percX * (plane.UVMax.X - plane.UVMin.X);
            float v = plane.UVMin.Y + percY * (plane.UVMax.Y - plane.UVMin.Y);

            var texture = content.Peek<Software.Texture<Software.Texel.A8R8G8B8>>(new Content.TextureUnconcretizer { Texture = t });
            //var texture = new Software.Texture<Software.Texel.A8R8G8B8>(t, 0);

            intersection = new Common.RayIntersection 
            {
                Distance = Vector3.TransformCoordinate(plane.Position, b.Transformation).Z
            };

            var va = texture.Sample(new Vector2(u, v));

            return va.R > 0.5f;
        }
        // ----------------------------------------------------------------------------------------------
        // -- BoundingMetaMesh --------------------------------------------------------------------------
        // ----------------------------------------------------------------------------------------------

        public static bool Intersect(Ray a, BoundingMetaMesh b, out object intersection)
        {
            var r = new Common.RayIntersection();
            intersection = r;
            Matrix invTransformation = Matrix.Invert(b.Transformation);
            var newRay = new Ray(
                Vector3.TransformCoordinate(a.Position, invTransformation),
                Vector3.Normalize(Vector3.TransformNormal(a.Direction, invTransformation)));

            if (b.XMesh != null)
            {
#if BOUNDING_META_MESH_REAQUIRE_ON_DISPOSED
                if (b.XMesh.Disposed)
                    b.Init(content);
#endif
                if (!b.XMesh.Intersects(newRay, out r.Distance)) return false;
            }
            else if (b.Mesh != null)
            {
                var mesh = content.Peek<SlimDX.Direct3D9.Mesh>(b.Mesh);
                if (!mesh.Intersects(newRay, out r.Distance)) return false;
            }
            else if (b.SoftwareMesh != null)
            {
                Vector2 uv;
                Graphics.Software.Triangle triangle;
                if (!b.SoftwareMesh.Intersect(newRay, false, out triangle, out r.Distance, out uv)) return false;
            }
            else if (b.SkinnedMeshInstance != null)
            {
                newRay = a;
                var sm = content.Peek<Renderer.Renderer.EntityAnimation>(b.SkinnedMeshInstance);
                object o;
                if (!Intersect(newRay, sm, out o)) return false;
                r = (Common.RayIntersection)o;
            }

            var newPos = Vector3.TransformCoordinate(newRay.Position + newRay.Direction * r.Distance, 
                b.Transformation);
            r.Distance = (newPos - a.Position).Length();
            return true;
        }
 public static bool Intersect(Ray a, Software.Mesh b, out object intersection)
 {
     var r = new Common.RayIntersection();
     intersection = r;
     Software.Triangle t;
     Vector2 uv;
     bool hit = b.Intersect(a, false, out t, out r.Distance, out uv);
     r.Userdata = uv;
     return hit;
 }
        /*public static bool Intersect(Ray a, Content.Model9 b, out object intersection)
        {

            if (b.XMesh != null)
                return Intersect(a, new BoundingMetaMesh
                {
                    Mesh = b.XMesh,
                    Tranformation = b.World
                }, out intersection);
            else if (b.SkinnedMesh != null)
                return Intersect(a, b.SkinnedMesh, out intersection);

            intersection = null;
            return false;
        }*/

        /*public static bool Intersect(Ray a, Content.SkinnedMesh b, out object intersection)
        {
            float minD = float.MaxValue;
            foreach (var v in b.MeshContainers)
            {
                float d;
                if (v.Second.MeshData != null && 
                    v.Second.MeshData.Mesh != null)
                {
                    Matrix invTransformation = Matrix.Invert(v.First.CombinedTransform);
                    var newRay = new Ray(
                        Vector3.TransformCoordinate(a.Position, invTransformation),
                        Vector3.Normalize(Vector3.TransformNormal(a.Direction, invTransformation)));

                    if(v.Second.MeshData.Mesh.Intersects(newRay, out d))
                    {
                        var newPos = Vector3.TransformCoordinate(newRay.Position + newRay.Direction * d,
                            v.First.CombinedTransform);
                        d = (newPos - a.Position).Length();
                        if(d < minD)
                            minD = d;
                    }
                }
            }
            intersection = new Common.RayIntersection { Distance = minD };
            return minD != float.MaxValue;
        }*/


        public static bool Intersect(Ray a, Renderer.Renderer.EntityAnimation b, out object intersection)
        {
            float minD = float.MaxValue;
            foreach (var v in b.StoredFrameMatrices)
            {
                if (v.Key != null)
                {
                    foreach (var m in v.Value)
                    {
                        float d;
                        Matrix invTransformation = Matrix.Invert(m[0]);
                        var newRay = new Ray(
                            Vector3.TransformCoordinate(a.Position, invTransformation),
                            Vector3.Normalize(Vector3.TransformNormal(a.Direction, invTransformation)));

                        if (v.Key.Intersects(newRay, out d))
                        {
                            var newPos = Vector3.TransformCoordinate(newRay.Position + newRay.Direction * d,
                                m[0]);
                            d = (newPos - a.Position).Length();
                            if (d < minD)
                                minD = d;
                        }
                    }
                }
            }
            intersection = new Common.RayIntersection { Distance = minD };
            return minD != float.MaxValue;
        }
Example #10
0
        public static bool Intersect(Ray a, MetaBoundingImageGraphic b, out object intersection)
        {
#if false
            var model   = content.Peek <Content.Model9>(b.Graphic);
            var sd      = model.Texture.GetLevelDescription(0);
            var plane   = b.Graphic.GetPlane(new Vector2(sd.Width, sd.Height));
            var mesh    = Software.Meshes.Construct(plane);
            var texture = new Software.Texture <Software.Texel.A8R8G8B8>(model.Texture, 0);

            Matrix invTransformation = Matrix.Invert(Matrix.Scaling(1, 1, -1) * b.Transformation);
            var    newRay            = new Ray(
                Vector3.TransformCoordinate(a.Position, invTransformation),
                Vector3.Normalize(Vector3.TransformNormal(a.Direction, invTransformation)));

            Software.Triangle t;
            float             d;
            Vector2           uv;
            //mesh.BuildKDTree();
            bool hit = mesh.Intersect(newRay, false, out t, out d, out uv);
            intersection = null;
            if (!hit)
            {
                return(false);
            }

            var newPos = Vector3.TransformCoordinate(newRay.Position + newRay.Direction * d,
                                                     b.Transformation);

            intersection = new Common.RayIntersection {
                Distance = (newPos - a.Position).Length()
            };
            var v = texture.Sample(uv);
            Console.WriteLine(uv);
            return(v.R > 0.5f);
#endif
            var    t                 = content.Peek <SlimDX.Direct3D9.Texture>(b.Graphic.Texture);
            var    sd                = t.GetLevelDescription(0);
            var    plane             = b.Graphic.GetPlane(new Vector2(sd.Width, sd.Height));
            Matrix invTransformation = Matrix.Invert(Matrix.Scaling(1, 1, -1) * b.Transformation);
            var    newRay            = new Ray(
                Vector3.TransformCoordinate(a.Position, invTransformation),
                Vector3.Normalize(Vector3.TransformNormal(a.Direction, invTransformation)));

            float worldX = newRay.Position.X - plane.Position.X;
            float worldY = newRay.Position.Y - plane.Position.Y;

            float percX = worldX;
            float percY = worldY;

            float u = plane.UVMin.X + percX * (plane.UVMax.X - plane.UVMin.X);
            float v = plane.UVMin.Y + percY * (plane.UVMax.Y - plane.UVMin.Y);

            var texture = content.Peek <Software.Texture <Software.Texel.A8R8G8B8> >(new Content.TextureUnconcretizer {
                Texture = t
            });
            //var texture = new Software.Texture<Software.Texel.A8R8G8B8>(t, 0);

            intersection = new Common.RayIntersection
            {
                Distance = Vector3.TransformCoordinate(plane.Position, b.Transformation).Z
            };

            var va = texture.Sample(new Vector2(u, v));

            return(va.R > 0.5f);
        }