Пример #1
0
        /// <summary>
        /// Intersects Bounds with Ray
        /// </summary>
        /// <param name="entity">Entity for Bounds</param>
        /// <param name="index">Index for Job (parallel)</param>
        /// <param name="c0">WorldRenderBounds for Entity</param>
        public void Execute([ReadOnly] Entity entity, [ReadOnly] int index, [ReadOnly] ref WorldRenderBounds c0)
        {
            bool result = c0.Value.ToBounds().IntersectRay(Ray, out float distance);

            // Set distance to Float.MaxValue if there was no intersection
            Results[index] = new RayIntersectionResult(result, result ? distance : float.MaxValue, entity);
        }
Пример #2
0
        public RayIntersectionResult RayIntersection(Ray ray)
        {
            var closestIntersection = new RayIntersectionResult {
                distance = float.PositiveInfinity, face = null
            };

            foreach (var face in Faces)
            {
                var plane = ConstructPlane(face);
                var intersectionDistance = ray.Intersects(plane);
                if (intersectionDistance.HasValue && intersectionDistance.Value < closestIntersection.distance)
                {
                    var intersectionPoint = ray.Position + (ray.Direction * intersectionDistance.Value);
                    if (IsPointOnFace(intersectionPoint, face))
                    {
                        closestIntersection.distance = intersectionDistance.Value;
                        closestIntersection.face     = face;
                    }
                }
            }

            if (closestIntersection.face == null)
            {
                return(null);
            }
            return(closestIntersection);
        }
Пример #3
0
    private void ProcessTerminal(Coords nodeCoords, float tx0, float ty0,
                                 float tz0)
    {
        var entryDistance = Mathf.Max(tx0, ty0, tz0);

        var entryPlane = GetEntryPlane(tx0, ty0, tz0);

        var normal = GetNormal(entryPlane);

        if (_debug)
        {
            const float normalSize = 1f;
            Debug.DrawLine(_ray.origin, _ray.GetPoint(entryDistance), Color.white, 0, false);

            Debug.DrawLine(_ray.GetPoint(entryDistance),
                           _ray.GetPoint(entryDistance) + _transform.TransformDirection(normal) * normalSize, Color.green, 0, false);

            var bounds = _rootNode.GetChildBounds(nodeCoords);
            DrawBounds(bounds, Color.red, true);
        }

        var result = new RayIntersectionResult(_tree, null, nodeCoords, entryDistance, _ray.GetPoint(entryDistance),
                                               normal, GetNeighbourSide(entryPlane));

        if (_filter == null || _filter(result))
        {
            results.Add(result);
        }
    }
Пример #4
0
        /// <summary>
        /// Adds Selected-Component to closest intersection (if Click is true)
        /// Adds Hovered-Component to closest intersection (always)
        /// Removes Selected- & Hovered-Component from previous selection/hovered
        /// </summary>
        public void Execute()
        {
            RayIntersectionResult result = intersectionResults[0];

            for (int i = 0; i < hovered.Length; i++) //  Should be Length 1 at most
            {
                if (!result || hovered[i] != result.Entity)
                {
                    buffer.RemoveComponent(hovered[i], typeof(Hovered)); // Remove previous hovered (if not currently hovered)
                }
            }
            if (result)
            {
                if (!hovered.Contains(result.Entity))
                {
                    buffer.AddComponent(result.Entity, new Hovered()); // Add current Hovered (if not yet added previously)
                }
                if (click)
                {
                    for (int i = 0; i < selected.Length; i++) //  Should be Length 1 at most
                    {
                        if (selected[i] != result.Entity)
                        {
                            buffer.RemoveComponent(selected[i], typeof(Selected)); // Remove previous Selected (if not currently selecting)
                        }
                    }
                    if (!selected.Contains(result.Entity))                  // Only add once
                    {
                        buffer.AddComponent(result.Entity, new Selected()); // Add current Selected (if not yet added previously)
                    }
                }
            }
        }
Пример #5
0
    public virtual bool Intersect(Transform transform, Ray ray, out RayIntersectionResult result,
                                  int?wantedDepth = null, bool debug = false)
    {
        if (wantedDepth != null && wantedDepth < 0)
        {
            throw new ArgumentOutOfRangeException("wantedDepth", "Wanted depth should not be less than zero.");
        }

        var results = new RayIntersection(transform, this, ray, false, wantedDepth, null, debug).results;

        if (results.Count > 0)
        {
            result = results[0];
            return(true);
        }

        result = new RayIntersectionResult(false);
        return(false);
    }
Пример #6
0
        public RayIntersectionResult RayIntersection(Ray ray)
        {
            var closestIntersection = new RayIntersectionResult{ distance = float.PositiveInfinity, face = null };

            foreach (var face in Faces)
            {
                var plane = ConstructPlane(face);
                var intersectionDistance = ray.Intersects(plane);
                if (intersectionDistance.HasValue && intersectionDistance.Value < closestIntersection.distance)
                {
                    var intersectionPoint = ray.Position + (ray.Direction * intersectionDistance.Value);
                    if (IsPointOnFace(intersectionPoint, face))
                    {
                        closestIntersection.distance = intersectionDistance.Value;
                        closestIntersection.face = face;
                    }
                }
            }

            if (closestIntersection.face == null) return null;
            return closestIntersection;
        }
Пример #7
0
    public override bool Intersect(Transform transform, Ray ray, out RayIntersectionResult result, int?wantedDepth = null,
                                   bool debugRaycasts = false)
    {
        var subIntersectionResult = new RayIntersectionResult(false);

        var intersection = new RayIntersection(transform, GetOwnerNode().GetTree(), ray, false, null, intersectionResult => {
            var intersectionSuperNode = intersectionResult.node as SuperVoxelTree.Node;
            if (intersectionSuperNode == null)
            {
                return(false);
            }

            var intersectedVoxel = intersectionSuperNode.GetItem();

            if (intersectedVoxel == null)
            {
                return(false);
            }

            if (intersectedVoxel.IntersectInternal(transform, ray, out subIntersectionResult, wantedDepth, debugRaycasts))
            {
                return(true);
            }

            return(false);
        }, debugRaycasts);

        result = subIntersectionResult;

        if (intersection.results.Count > 0)
        {
            return(true);
        }

        return(false);
    }
Пример #8
0
        public RayIntersectionResult RayIntersection(Ray ray)
        {
            var closestIntersection = new RayIntersectionResult { Distance = float.PositiveInfinity, Intersects = false };

            for (int vindex = 0; vindex < indicies.Length; vindex += 3)
            {
                var p = indicies.Skip(vindex).Take(3).Select(i => verticies[i].Position).ToArray();
                var plane = new Plane(p[0], p[1], p[2]);
                var intersectionDistance = ray.Intersects(plane);
                if (intersectionDistance.HasValue && intersectionDistance.Value < closestIntersection.Distance)
                {
                    var intersectionPoint = ray.Position + (ray.Direction * intersectionDistance.Value);
                    if (IsPointOnFace(intersectionPoint, p))
                    {
                        closestIntersection.Distance = intersectionDistance.Value;
                        closestIntersection.Intersects = true;

                        var tc = indicies.Skip(vindex).Take(3).Select(i => verticies[i].TextureCoordinate).ToArray();

                        var bv = p.Select(v => v - intersectionPoint).ToArray();
                        var area = Vector3.Cross(p[0] - p[1], p[0] - p[2]).Length();
                        var baryArea = new float[] {
                            Vector3.Cross(bv[1], bv[2]).Length() / area,
                            Vector3.Cross(bv[2], bv[0]).Length() / area,
                            Vector3.Cross(bv[0], bv[1]).Length() / area
                        };

                        var uv = (tc[0] * baryArea[0]) + (tc[1] * baryArea[1]) + (tc[2] * baryArea[2]);

                        closestIntersection.UV = uv;
                    }
                }
            }

            return closestIntersection;
        }
Пример #9
0
    private void ProcessTerminal(Coords nodeCoords, float tx0, float ty0,
		float tz0)
    {
        var entryDistance = Mathf.Max(tx0, ty0, tz0);

        var entryPlane = GetEntryPlane(tx0, ty0, tz0);

        var normal = GetNormal(entryPlane);
        if (_debug) {
            const float normalSize = 1f;
            Debug.DrawLine(_ray.origin, _ray.GetPoint(entryDistance), Color.white, 0, false);

            Debug.DrawLine(_ray.GetPoint(entryDistance),
                _ray.GetPoint(entryDistance) + _transform.TransformDirection(normal) * normalSize, Color.green, 0, false);

            var bounds = _rootNode.GetChildBounds(nodeCoords);
            DrawBounds(bounds, Color.red, true);
        }

        var result = new RayIntersectionResult(_tree, null, nodeCoords, entryDistance, _ray.GetPoint(entryDistance),
            normal, GetNeighbourSide(entryPlane));

        if (_filter == null || _filter(result)) {
            results.Add(result);
        }
    }
Пример #10
0
 private bool IntersectInternal(Transform transform, Ray ray, out RayIntersectionResult result, int?wantedDepth,
                                bool debugRaycasts)
 {
     return(base.Intersect(transform, ray, out result, wantedDepth, debugRaycasts));
 }