示例#1
0
        void setLight()
        {
            bool sortAngles = false;

            allVertices.Clear();// Since these lists are populated every frame, clear them first to prevent overpopulation


            //layer = 1 << 8;


            //--Step 2: Obtain vertices for each mesh --//
            //---------------------------------------------------------------------//

            // las siguientes variables usadas para arregla bug de ordenamiento cuando
            // los angulos calcuados se encuentran en cuadrantes mixtos (1 y 4)
            bool  lows     = false; // check si hay menores a -0.5
            bool  his      = false; // check si hay mayores a 2.0
            float magRange = 0.15f;

            List <verts> tempVerts = new List <verts>();

            for (int m = 0; m < allMeshes.Length; m++)
            {
                //for (int m = 0; m < 1; m++) {
                tempVerts.Clear();
                PolygonCollider2D mf = allMeshes[m];

                // las siguientes variables usadas para arregla bug de ordenamiento cuando
                // los angulos calcuados se encuentran en cuadrantes mixtos (1 y 4)
                lows = false; // check si hay menores a -0.5
                his  = false; // check si hay mayores a 2.0

                if (((1 << mf.transform.gameObject.layer) & layer) != 0)
                {
                    for (int i = 0; i < mf.GetTotalPointCount(); i++)
                    {                                                              // ...and for ever vertex we have of each mesh filter...
                        verts v = new verts();
                        // Convert to world space
                        Vector3 worldPoint = mf.transform.TransformPoint(mf.points[i]);



                        // Reforma fecha 24/09/2014 (ultimo argumento lighradius X worldPoint.magnitude (expensivo pero preciso))
                        RaycastHit2D ray = Physics2D.Raycast(transform.position, worldPoint - transform.position, (worldPoint - transform.position).magnitude, layer);


                        if (ray)
                        {
                            v.pos = ray.point;
                            if (worldPoint.sqrMagnitude >= (ray.point.sqrMagnitude - magRange) && worldPoint.sqrMagnitude <= (ray.point.sqrMagnitude + magRange))
                            {
                                v.endpoint = true;
                            }
                        }
                        else
                        {
                            v.pos      = worldPoint;
                            v.endpoint = true;
                        }

                        Debug.DrawLine(transform.position, v.pos, Color.white);

                        //--Convert To local space for build mesh (mesh craft only in local vertex)
                        v.pos = transform.InverseTransformPoint(v.pos);
                        //--Calculate angle
                        v.angle = getVectorAngle(true, v.pos.x, v.pos.y);



                        // -- bookmark if an angle is lower than 0 or higher than 2f --//
                        //-- helper method for fix bug on shape located in 2 or more quadrants
                        if (v.angle < 0f)
                        {
                            lows = true;
                        }

                        if (v.angle > 2f)
                        {
                            his = true;
                        }


                        //--Add verts to the main array
                        if ((v.pos).sqrMagnitude <= lightRadius * lightRadius)
                        {
                            tempVerts.Add(v);
                        }

                        if (sortAngles == false)
                        {
                            sortAngles = true;
                        }
                    }
                }



                // Indentify the endpoints (left and right)
                if (tempVerts.Count > 0)
                {
                    sortList(tempVerts);  // sort first

                    int posLowAngle  = 0; // save the indice of left ray
                    int posHighAngle = 0; // same last in right side

                    //Debug.Log(lows + " " + his);

                    if (his == true && lows == true)
                    {                             //-- FIX BUG OF SORTING CUANDRANT 1-4 --//
                        float lowestAngle  = -1f; //tempVerts[0].angle; // init with first data
                        float highestAngle = tempVerts[0].angle;


                        for (int d = 0; d < tempVerts.Count; d++)
                        {
                            if (tempVerts[d].angle <1f && tempVerts[d].angle> lowestAngle)
                            {
                                lowestAngle = tempVerts[d].angle;
                                posLowAngle = d;
                            }

                            if (tempVerts[d].angle > 2f && tempVerts[d].angle < highestAngle)
                            {
                                highestAngle = tempVerts[d].angle;
                                posHighAngle = d;
                            }
                        }
                    }
                    else
                    {
                        //-- convencional position of ray points
                        // save the indice of left ray
                        posLowAngle  = 0;
                        posHighAngle = tempVerts.Count - 1;
                    }


                    tempVerts[posLowAngle].location  = 1;  // right
                    tempVerts[posHighAngle].location = -1; // left



                    //--Add vertices to the main meshes vertexes--//
                    allVertices.AddRange(tempVerts);
                    //allVertices.Add(tempVerts[0]);
                    //allVertices.Add(tempVerts[tempVerts.Count - 1]);



                    // -- r ==0 --> right ray
                    // -- r ==1 --> left ray
                    for (int r = 0; r < 2; r++)
                    {
                        //-- Cast a ray in same direction continuos mode, start a last point of last ray --//
                        Vector3 fromCast   = new Vector3();
                        bool    isEndpoint = false;

                        if (r == 0)
                        {
                            fromCast   = transform.TransformPoint(tempVerts[posLowAngle].pos);
                            isEndpoint = tempVerts[posLowAngle].endpoint;
                        }
                        else if (r == 1)
                        {
                            fromCast   = transform.TransformPoint(tempVerts[posHighAngle].pos);
                            isEndpoint = tempVerts[posHighAngle].endpoint;
                        }



                        if (isEndpoint == true)
                        {
                            Vector2 from = (Vector2)fromCast;
                            Vector2 dir  = (from - (Vector2)transform.position);



                            float       mag = (lightRadius);// - fromCast.magnitude;
                            const float checkPointLastRayOffset = 0.005f;

                            from += (dir * checkPointLastRayOffset);


                            RaycastHit2D rayCont = Physics2D.Raycast(from, dir, mag, layer);
                            Vector3      hitp;
                            if (rayCont)
                            {
                                hitp = rayCont.point;
                            }
                            else
                            {
                                Vector2 newDir = transform.InverseTransformDirection(dir);         //local p
                                hitp = (Vector2)transform.TransformPoint(newDir.normalized * mag); //world p
                            }

                            if (((Vector2)hitp - (Vector2)transform.position).sqrMagnitude > (lightRadius * lightRadius))
                            {
                                dir  = (Vector2)transform.InverseTransformDirection(dir);       //local p
                                hitp = (Vector2)transform.TransformPoint(dir.normalized * mag);
                            }

                            Debug.DrawLine(fromCast, hitp, Color.green);

                            verts vL = new verts();
                            vL.pos = transform.InverseTransformPoint(hitp);

                            vL.angle = getVectorAngle(true, vL.pos.x, vL.pos.y);
                            allVertices.Add(vL);
                        }
                    }
                }
            }



            //--Step 3: Generate vectors for light cast--//
            //---------------------------------------------------------------------//

            int theta = 0;
            //float amount = (Mathf.PI * 2) / lightSegments;
            int amount = 360 / lightSegments;



            for (int i = 0; i < lightSegments; i++)
            {
                theta = amount * (i);
                if (theta == 360)
                {
                    theta = 0;
                }

                verts v = new verts();
                //v.pos = new Vector3((Mathf.Sin(theta)), (Mathf.Cos(theta)), 0); // in radians low performance
                v.pos = new Vector3((TablaSenoCoseno.SenArray[theta]), (TablaSenoCoseno.CosArray[theta]), 0); // in dregrees (previous calculate)

                v.angle = getVectorAngle(true, v.pos.x, v.pos.y);
                v.pos  *= lightRadius;
                v.pos  += transform.position;



                RaycastHit2D ray = Physics2D.Raycast(transform.position, v.pos - transform.position, lightRadius, layer);
                //Debug.DrawRay(transform.position, v.pos - transform.position, Color.white);

                if (!ray)
                {
                    //Debug.DrawLine(transform.position, v.pos, Color.white);

                    v.pos = transform.InverseTransformPoint(v.pos);
                    allVertices.Add(v);
                }
            }



            //-- Step 4: Sort each vertice by angle (along sweep ray 0 - 2PI)--//
            //---------------------------------------------------------------------//
            if (sortAngles == true)
            {
                sortList(allVertices);
            }
            //-----------------------------------------------------------------------------


            //--auxiliar step (change order vertices close to light first in position when has same direction) --//
            float rangeAngleComparision = 0.00001f;

            for (int i = 0; i < allVertices.Count - 1; i += 1)
            {
                verts uno = allVertices[i];
                verts dos = allVertices[i + 1];

                // -- Comparo el angulo local de cada vertex y decido si tengo que hacer un exchange-- //
                if (uno.angle >= dos.angle - rangeAngleComparision && uno.angle <= dos.angle + rangeAngleComparision)
                {
                    if (dos.location == -1)
                    { // Right Ray
                        if (uno.pos.sqrMagnitude > dos.pos.sqrMagnitude)
                        {
                            allVertices[i]     = dos;
                            allVertices[i + 1] = uno;
                            //Debug.Log("changing left");
                        }
                    }


                    // ALREADY DONE!!
                    if (uno.location == 1)
                    { // Left Ray
                        if (uno.pos.sqrMagnitude < dos.pos.sqrMagnitude)
                        {
                            allVertices[i]     = dos;
                            allVertices[i + 1] = uno;
                            //Debug.Log("changing");
                        }
                    }
                }
            }
        }
示例#2
0
 private static void NewMethod2(int theta, verts v)
 {
     v.pos = new Vector3((TablaSenoCoseno.SenArray[theta]), (TablaSenoCoseno.CosArray[theta]), 0); // in dregrees (previous calculate)
 }