예제 #1
0
 public void Init(RenderStaticCollision col)
 {
     DoRender   = true;
     isSelected = false;
     Transform  = Matrix.Identity;
     instance   = col;
 }
예제 #2
0
 public void Init(RenderStaticCollision col)
 {
     DoRender    = true;
     Transform   = Matrix.Identity;
     BoundingBox = col.BoundingBox;
     instance    = col;
 }
예제 #3
0
        public PickOutParams Pick(int sx, int sy, int Width, int Height)
        {
            float   lowest            = float.MaxValue;
            int     lowestRefID       = -1;
            Vector3 WorldPosIntersect = Vector3.Zero;

            Ray ray = Camera.GetPickingRay(new Vector2(sx, sy), new Vector2(Width, Height));

            int index = 0;

            foreach (KeyValuePair <int, IRenderer> model in Assets)
            {
                if (!model.Value.DoRender)
                {
                    continue;
                }

                Matrix4x4 vWM = Matrix4x4.Identity;
                Matrix4x4.Invert(model.Value.Transform, out vWM);
                var localRay = new Ray(
                    Vector3Utils.TransformCoordinate(ray.Position, vWM),
                    Vector3.TransformNormal(ray.Direction, vWM)
                    );

                if (model.Value is RenderModel)
                {
                    RenderModel mesh = (model.Value as RenderModel);
                    var         bbox = mesh.BoundingBox;

                    if (localRay.Intersects(bbox) == 0.0f)
                    {
                        continue;
                    }

                    for (var i = 0; i < mesh.LODs[0].Indices.Length / 3; i++)
                    {
                        var   v0 = mesh.LODs[0].Vertices[mesh.LODs[0].Indices[i * 3]].Position;
                        var   v1 = mesh.LODs[0].Vertices[mesh.LODs[0].Indices[i * 3 + 1]].Position;
                        var   v2 = mesh.LODs[0].Vertices[mesh.LODs[0].Indices[i * 3 + 2]].Position;
                        float t;

                        if (!Toolkit.Mathematics.Collision.RayIntersectsTriangle(ref localRay, ref v0, ref v1, ref v2, out t))
                        {
                            continue;
                        }

                        if (t < 0.0f || float.IsNaN(t))
                        {
                            // TODO: Remove this from Graphics class. This should be an ambiguous class -
                            // Maybe we can send this to another class which handles erroneous requests
                            if (SceneData.FrameResource.FrameObjects.ContainsKey(model.Key))
                            {
                                var frame = (SceneData.FrameResource.FrameObjects[model.Key] as FrameObjectBase);
                                Utils.Logging.Log.WriteLine(string.Format("The toolkit has failed to analyse a model: {0} {1}", frame.Name, t));
                            }
                        }

                        var worldPosition = ray.Position + t * ray.Direction;
                        var distance      = (worldPosition - ray.Position).LengthSquared();
                        if (distance < lowest)
                        {
                            lowest      = distance;
                            lowestRefID = model.Key;
                        }
                    }
                }
                if (model.Value is RenderInstance)
                {
                    RenderInstance        instance  = (model.Value as RenderInstance);
                    RenderStaticCollision collision = instance.GetCollision();
                    var bbox = collision.BoundingBox;

                    if (localRay.Intersects(bbox) == 0.0f)
                    {
                        continue;
                    }

                    for (var i = 0; i < collision.Indices.Length / 3; i++)
                    {
                        var   v0 = collision.Vertices[collision.Indices[i * 3]].Position;
                        var   v1 = collision.Vertices[collision.Indices[i * 3 + 1]].Position;
                        var   v2 = collision.Vertices[collision.Indices[i * 3 + 2]].Position;
                        float t;

                        if (!Toolkit.Mathematics.Collision.RayIntersectsTriangle(ref localRay, ref v0, ref v1, ref v2, out t))
                        {
                            continue;
                        }

                        if (t < 0.0f || float.IsNaN(t))
                        {
                            //if (SceneData.FrameResource.FrameObjects.ContainsKey(model.Key))
                            //{
                            //    var frame = (SceneData.FrameResource.FrameObjects[model.Key] as FrameObjectBase);
                            //    Utils.Logging.Log.WriteLine(string.Format("The toolkit has failed to analyse a model: {0} {1}", frame.Name, t));
                            //}
                        }

                        var worldPosition = ray.Position + t * ray.Direction;
                        var distance      = (worldPosition - ray.Position).LengthSquared();

                        if (distance < lowest)
                        {
                            lowest            = distance;
                            lowestRefID       = model.Key;
                            WorldPosIntersect = worldPosition;
                        }
                    }
                }

                index++;
            }

            PickOutParams OutputParams = new PickOutParams();

            OutputParams.LowestRefID   = lowestRefID;
            OutputParams.WorldPosition = WorldPosIntersect;

            return(OutputParams);
        }