Exemplo n.º 1
0
        public static bool LineCast(Vector3 _origin, Vector3 _end, ref VLSRayHit _hitInfo)
        {
            //rHit2D = Physics2D.Linecast(_origin, _end);

            //if (rHit2D.collider != null)
            //{
            //    _hitInfo.point = rHit2D.point;
            //    return true;
            //}
            //else
            //{
            //    _hitInfo.point = _end;
            //    return false;
            //}

            lc_outPnt.Set(0, 0, 0);    // = Vector3.zero;
            _hitInfo.sqrDist = Mathf.Infinity;
            lc_curDist       = 0;
            lc_Intersects    = false;

#if UNITY_EDITOR
            if (VLSDebug.IsModeActive(VLSDebugMode.Raycasting))
            {
                Debug.DrawLine(_origin, _end, new Color(.2f, .2f, .2f, .2f));
            }
#endif

            for (int e = 0; e < visibleEdges.Count; e++)
            {
                if (LineIntersects(visibleEdges[e].PointA.position, visibleEdges[e].PointB.position, _origin, _end, ref lc_outPnt))
                {
                    lc_curDist = Vector3.SqrMagnitude(lc_outPnt - _origin);

                    if (lc_curDist < _hitInfo.sqrDist)
                    {
                        _hitInfo.sqrDist = lc_curDist;
                        _hitInfo.point.Set(lc_outPnt.x, lc_outPnt.y, lc_outPnt.z);    // +(Vector3)(-edge.Normal);
                        //_hitInfo.obstructor = edge.Parent as VLSObstructor;
                        lc_Intersects = true;
                    }
                }
            }

            if (!lc_Intersects)
            {
                _hitInfo.point = _end;
                //_hitInfo.obstructor = null;
            }

            //_hitInfo.direction = (_hitInfo.point - _origin).normalized;

#if UNITY_EDITOR
            if (VLSDebug.IsModeActive(VLSDebugMode.Raycasting))
            {
                Debug.DrawLine(_origin, _hitInfo.point, new Color(.8f, .8f, .2f, .8f));
            }
#endif

            return(lc_Intersects);
        }
Exemplo n.º 2
0
        protected virtual void DebugBounds(Color _color)
        {
            if (!VLSDebug.IsModeActive(VLSDebugMode.Bounds))
            {
                return;
            }

            Gizmos.color = (isActive) ? _color : Color.gray;
            Gizmos.DrawWireCube(bounds.center, bounds.size);
        }
Exemplo n.º 3
0
        protected virtual void DebugShape(Color _color, bool _force = false)
        {
            if (!_force && !VLSDebug.IsModeActive(VLSDebugMode.Geometry))
            {
                return;
            }

            Gizmos.color = (isActive) ? _color : Color.gray;
            for (int i = 0; i < edges.Count; i++)
            {
                Gizmos.DrawLine(edges[i].PointA.position, edges[i].PointB.position);
            }
        }
Exemplo n.º 4
0
        protected virtual void DebugNormals(Color _color)
        {
            if (!VLSDebug.IsModeActive(VLSDebugMode.Geometry))
            {
                return;
            }

#if UNITY_EDITOR
            Gizmos.color = _color;
            for (int i = 0; i < edges.Count; i++)
            {
                Gizmos.DrawRay((edges[i].PointA.position + edges[i].PointB.position) * 0.5f, edges[i].Normal * UnityEditor.HandleUtility.GetHandleSize(transform.position) * 0.5f);
            }
#endif
        }
Exemplo n.º 5
0
        void OnGUI()
        {
            int value = 0;

            System.Collections.Generic.List <string> layerOptions = new System.Collections.Generic.List <string>();
            for (int i = 0; i < 31; i++)
            {
                string layerName = LayerMask.LayerToName(i);
                if (layerName.Length > 0)
                {
                    layerOptions.Add(layerName);
                }
            }

            EditorGUI.indentLevel++;
            {
                EditorGUILayout.LabelField("Default Settings");
                EditorGUI.indentLevel++;
                {
                    VLSGlobals.DEFAULT_LIGHT_LAYER        = EditorGUILayout.LayerField("Light Layer", VLSGlobals.DEFAULT_LIGHT_LAYER);
                    VLSGlobals.DEFAULT_LIGHT_SHADOW_LAYER = EditorGUILayout.MaskField("Light ShadowLayer", (int)VLSGlobals.DEFAULT_LIGHT_SHADOW_LAYER, layerOptions.ToArray());
                    VLSGlobals.DEFAULT_LIGHT_SCALE        = EditorGUILayout.FloatField("Scale", VLSGlobals.DEFAULT_LIGHT_SCALE);
                }
                EditorGUI.indentLevel--;

                EditorGUILayout.Space();

                EditorGUILayout.LabelField("Debuging");
                EditorGUI.indentLevel++;
                {
                    value += (EditorGUILayout.Toggle("Geometry", VLSDebug.IsModeActive(VLSDebugMode.Geometry))) ? 1 : 0;
                    value += (EditorGUILayout.Toggle("Bounds", VLSDebug.IsModeActive(VLSDebugMode.Bounds))) ? 2 : 0;
                    value += (EditorGUILayout.Toggle("Raycasting", VLSDebug.IsModeActive(VLSDebugMode.Raycasting))) ? 4 : 0;
                }
                EditorGUI.indentLevel--;
            }
            //GUILayout.Label(System.Convert.ToString((VLSDebugMode)value));

            if (GUI.changed)
            {
                VLSDebug.SetDebugMode((VLSDebugMode)value);
                VLSGlobals.SaveEditorPrefs();
                SceneView.RepaintAll();
            }
        }
Exemplo n.º 6
0
        protected override void DebugShape(Color _color, bool _force = false)
        {
            if (!_force && !VLSDebug.IsModeActive(VLSDebugMode.Geometry))
            {
                return;
            }

            base.DebugShape(new Color(_color.r, _color.g, _color.b, 0.1f), _force);

            Gizmos.color = _color;
            if (buffer.VertexCount > 0)
            {
                for (int i = 0; i < buffer.VertexCount - 1; i++)
                {
                    Gizmos.DrawLine(transform.TransformPoint(buffer.vertices[i].position), transform.TransformPoint(buffer.vertices[(i + 1) % (buffer.VertexCount - 1)].position));
                }
            }
        }
Exemplo n.º 7
0
        //private static Vector3 zOffset = new Vector3(0, 0, 0);
        public static void GenerateRadialMesh(VLSLight _light, LayerMask _layerMask, float _obstPenetration = 0)
        {
            lightTransform = _light.transform;
            lightPosition  = lightTransform.position;
            //zOffset.Set(0, 0, lightPosition.z);

            _light.Buffer.Clear();

            CollectVisibleEdges(_light, _layerMask);
            visibleEdges.AddRange(_light.edges);

#if UNITY_EDITOR
            if (VLSDebug.IsModeActive(VLSDebugMode.Geometry))
            {
                for (int e = 0; e < visibleEdges.Count; e++)
                {
                    if (VLSDebug.IsModeActive(VLSDebugMode.Raycasting))
                    {
                        Debug.DrawLine(visibleEdges[e].PointA.position, visibleEdges[e].PointB.position, Color.magenta);

                        if (visibleEdges[e].IsEnd)
                        {
                            Debug.DrawRay(visibleEdges[e].PointB.position, visibleEdges[e].Normal * 0.2f, Color.red);
                        }

                        if (visibleEdges[e].IsStart)
                        {
                            Debug.DrawRay(visibleEdges[e].PointA.position, visibleEdges[e].Normal * 0.2f, Color.green);
                        }
                    }
                }
            }
#endif

            target.Set(0, 0, 0);
            hit = false;
            for (int e = 0; e < visibleEdges.Count; e++)
            {
                for (int e2 = 0; e2 < visibleEdges.Count; e2++)
                {
                    if (LineIntersects(visibleEdges[e].PointA.position, visibleEdges[e].PointB.position, visibleEdges[e2].PointA.position, visibleEdges[e2].PointB.position, ref kPoint))
                    {
                        LineCast(lightPosition, kPoint, ref rHit);
                        _light.Buffer.AddPoint(lightTransform.InverseTransformPoint(rHit.point), Mathf.Atan2(rHit.point.y - lightPosition.y, rHit.point.x - lightPosition.x));
                    }
                }

                if (_light.bounds.Contains(visibleEdges[e].PointA.position))
                {
                    if (visibleEdges[e].IsStart)
                    {
                        target = visibleEdges[e].PointA.position - (visibleEdges[e].Direction * 0.001f);
                        hit    = LineCast(lightPosition, target, ref rHit);
                        _light.Buffer.AddPoint(lightTransform.InverseTransformPoint(rHit.point), Mathf.Atan2(visibleEdges[e].PointA.position.y - lightPosition.y, visibleEdges[e].PointA.position.x - lightPosition.x));

                        if (!hit)
                        {
                            RayCast(lightPosition, target, ref rHit);
                            _light.Buffer.AddPoint(lightTransform.InverseTransformPoint(rHit.point), Mathf.Atan2(rHit.point.y - lightPosition.y, rHit.point.x - lightPosition.x));
                        }
                    }
                    else
                    {
                        target = visibleEdges[e].PointA.position;
                        LineCast(lightPosition, target, ref rHit);
                        _light.Buffer.AddPoint(lightTransform.InverseTransformPoint(rHit.point), Mathf.Atan2(visibleEdges[e].PointA.position.y - lightPosition.y, visibleEdges[e].PointA.position.x - lightPosition.x));
                    }
                }

                if (_light.bounds.Contains(visibleEdges[e].PointB.position))
                {
                    if (visibleEdges[e].IsEnd)
                    {
                        target = visibleEdges[e].PointB.position + (visibleEdges[e].Direction * 0.001f);
                        hit    = LineCast(lightPosition, target, ref rHit);
                        _light.Buffer.AddPoint(lightTransform.InverseTransformPoint(rHit.point), Mathf.Atan2(visibleEdges[e].PointB.position.y - lightPosition.y, visibleEdges[e].PointB.position.x - lightPosition.x));

                        if (!hit)
                        {
                            RayCast(lightPosition, target, ref rHit);
                            _light.Buffer.AddPoint(lightTransform.InverseTransformPoint(rHit.point), Mathf.Atan2(rHit.point.y - lightPosition.y, rHit.point.x - lightPosition.x));
                        }
                    }
                }
            }

            _light.Buffer.vertices.Sort();
            _light.Buffer.AddPoint(Vector3.zero, 0);
        }
Exemplo n.º 8
0
        private static void CollectVisibleEdges(VLSLight _light, LayerMask _layerMask)
        {
            visibleEdges.Clear();

            #region Collect Radial Edges
            if (_light is VLSRadial || _light is VLSRadialCS)
            {
                for (int o = 0; o < VLSViewer.VisibleObstructions.Count; o++)//VLSObstructor obst in VLSViewer.VisibleObstructions)
                {
                    if (VLSViewer.VisibleObstructions[o] == null)
                    {
                        continue;
                    }

                    if ((_layerMask.value & (1 << VLSViewer.VisibleObstructions[o].gameObject.layer)) == 0)
                    {
                        continue;
                    }

                    if (VLSViewer.VisibleObstructions[o].bounds.Overlaps(_light.bounds, true))
                    {
                        bool prevEdgeVisible = false;
                        for (int i = 0; i < VLSViewer.VisibleObstructions[o].edges.Count; i++)
                        {
                            VLSViewer.VisibleObstructions[o].edges[i].IsStart = false;
                            VLSViewer.VisibleObstructions[o].edges[i].IsEnd   = false;

                            if (QuickDot(VLSViewer.VisibleObstructions[o].edges[i].PointA.position - _light.transform.position, VLSViewer.VisibleObstructions[o].edges[i].Normal) <= 0)
                            {
                                if (!prevEdgeVisible)
                                {
                                    VLSViewer.VisibleObstructions[o].edges[i].IsStart = true;
                                }

                                prevEdgeVisible = true;
                                visibleEdges.Add(VLSViewer.VisibleObstructions[o].edges[i]);
                            }
                            else if (prevEdgeVisible)
                            {
                                VLSViewer.VisibleObstructions[o].edges[i - 1].IsEnd = true;
                                prevEdgeVisible = false;
                            }
                        }

                        if (VLSViewer.VisibleObstructions[o].edges[0].IsEnd && VLSViewer.VisibleObstructions[o].edges[VLSViewer.VisibleObstructions[o].edges.Count - 1].IsStart)
                        {
                            VLSViewer.VisibleObstructions[o].edges[0].IsStart = false;
                            VLSViewer.VisibleObstructions[o].edges[VLSViewer.VisibleObstructions[o].edges.Count - 1].IsEnd = false;
                        }
                        else
                        {
                            VLSViewer.VisibleObstructions[o].edges[VLSViewer.VisibleObstructions[o].edges.Count - 1].IsEnd = true;
                        }
                    }
                }
            }
            #endregion

            #region Collect Directional Edges
            if (_light is VLSDirectional)
            {
                foreach (VLSObstructor obst in VLSViewer.VisibleObstructions)
                {
                    if ((_layerMask.value & (1 << obst.gameObject.layer)) == 0)
                    {
                        continue;
                    }

                    if (obst.bounds.Overlaps(_light.bounds))
                    {
                        bool prevEdgeVisible = false;
                        for (int i = 0; i < obst.edges.Count; i++)
                        {
                            obst.edges[i].IsStart = false;
                            obst.edges[i].IsEnd   = false;

                            if (QuickDot(-_light.transform.up, obst.edges[i].Normal) < 0)
                            {
                                if (VLSDebug.IsModeActive(VLSDebugMode.Raycasting))
                                {
                                    Debug.DrawLine(obst.edges[i].PointA.position, obst.edges[i].PointB.position, Color.red);
                                }

                                if (!prevEdgeVisible)
                                {
                                    obst.edges[i].IsStart = true;
                                }

                                prevEdgeVisible = true;
                                visibleEdges.Add(obst.edges[i]);
                            }
                            else if (prevEdgeVisible)
                            {
                                obst.edges[i - 1].IsEnd = true;
                                prevEdgeVisible         = false;
                            }
                        }

                        if (obst.edges[0].IsEnd && obst.edges[obst.edges.Count - 1].IsStart)
                        {
                            obst.edges[0].IsStart = false;
                            obst.edges[obst.edges.Count - 1].IsEnd = false;
                        }
                        else
                        {
                            obst.edges[obst.edges.Count - 1].IsEnd = true;
                        }
                    }
                }
            }
            #endregion
        }