예제 #1
0
        private void UpdateLine()
        {
            _size = size;

            float   totalDistance = 0;
            Vector3 lastPosition  = Vector3.zero;

            List <Vector3> vertices  = new List <Vector3>();
            List <Vector2> uvs       = new List <Vector2>();
            List <Vector3> normals   = new List <Vector3>();
            List <int>     triangles = new List <int>();

            List <Vector3> positions = new List <Vector3>();

            for (int i = 0; i < coords.Length; i++)
            {
                // Get world position by coordinates
                Vector3 position = OnlineMapsTileSetControl.instance.GetWorldPosition(coords[i]);
                positions.Add(position);

                if (i != 0)
                {
                    // Calculate angle between coordinates.
                    float a = OnlineMapsUtils.Angle2DRad(lastPosition, position, 90);

                    // Calculate offset
                    Vector3 off = new Vector3(Mathf.Cos(a) * size, 0, Mathf.Sin(a) * size);

                    // Init vertices, normals and triangles.
                    int vCount = vertices.Count;

                    vertices.Add(lastPosition + off);
                    vertices.Add(lastPosition - off);
                    vertices.Add(position + off);
                    vertices.Add(position - off);

                    normals.Add(Vector3.up);
                    normals.Add(Vector3.up);
                    normals.Add(Vector3.up);
                    normals.Add(Vector3.up);

                    triangles.Add(vCount);
                    triangles.Add(vCount + 3);
                    triangles.Add(vCount + 1);
                    triangles.Add(vCount);
                    triangles.Add(vCount + 2);
                    triangles.Add(vCount + 3);

                    totalDistance += (lastPosition - position).magnitude;
                }

                lastPosition = position;
            }

            float tDistance = 0;

            for (int i = 1; i < positions.Count; i++)
            {
                float distance = (positions[i - 1] - positions[i]).magnitude;

                // Updates UV
                uvs.Add(new Vector2(tDistance / totalDistance, 0));
                uvs.Add(new Vector2(tDistance / totalDistance, 1));

                tDistance += distance;

                uvs.Add(new Vector2(tDistance / totalDistance, 0));
                uvs.Add(new Vector2(tDistance / totalDistance, 1));
            }

            // Update mesh
            mesh.vertices  = vertices.ToArray();
            mesh.normals   = normals.ToArray();
            mesh.uv        = uvs.ToArray();
            mesh.triangles = triangles.ToArray();
            mesh.RecalculateBounds();

            // Scale texture
            Vector2 scale = new Vector2(totalDistance / size, 1);

            scale.Scale(uvScale);
            meshRenderer.material.mainTextureScale = scale;
        }
    protected void DrawActivePoints(OnlineMapsTileSetControl control, ref List <Vector2> activePoints, ref List <Vector3> verticles, ref List <Vector3> normals, ref List <int> triangles, ref List <Vector2> uv, float weight)
    {
        if (activePoints.Count < 2)
        {
            activePoints.Clear();
            return;
        }
        List <Vector3> side1 = new List <Vector3>();
        List <Vector3> side2 = new List <Vector3>();

        for (int i = 0; i < activePoints.Count; i++)
        {
            Vector3 p = new Vector3(-activePoints[i].x, 0, activePoints[i].y);
            if (i == 0)
            {
                float   a   = OnlineMapsUtils.Angle2DRad(p, new Vector3(-activePoints[i + 1].x, 0, activePoints[i + 1].y), 90);
                Vector3 off = new Vector3(Mathf.Cos(a) * weight, 0, Mathf.Sin(a) * weight);
                Vector3 s1  = p + off;
                Vector3 s2  = p - off;
                s1.y = control.GetElevationValue(s1.x, s1.z, bestElevationYScale, tlx, tly, brx, bry);
                s2.y = control.GetElevationValue(s2.x, s2.z, bestElevationYScale, tlx, tly, brx, bry);
                side1.Add(s1);
                side2.Add(s2);
            }
            else if (i == activePoints.Count - 1)
            {
                float   a   = OnlineMapsUtils.Angle2DRad(new Vector3(-activePoints[i - 1].x, 0, activePoints[i - 1].y), p, 90);
                Vector3 off = new Vector3(Mathf.Cos(a) * weight, 0, Mathf.Sin(a) * weight);
                Vector3 s1  = p + off;
                Vector3 s2  = p - off;
                s1.y = control.GetElevationValue(s1.x, s1.z, bestElevationYScale, tlx, tly, brx, bry);
                s2.y = control.GetElevationValue(s2.x, s2.z, bestElevationYScale, tlx, tly, brx, bry);
                side1.Add(s1);
                side2.Add(s2);
            }
            else
            {
                Vector3 p1 = new Vector3(-activePoints[i - 1].x, 0, activePoints[i - 1].y);
                Vector3 p2 = new Vector3(-activePoints[i + 1].x, 0, activePoints[i + 1].y);
                float   a1 = OnlineMapsUtils.Angle2DRad(p1, p, 90);
                float   a2 = OnlineMapsUtils.Angle2DRad(p, p2, 90);
                Vector3 off1 = new Vector3(Mathf.Cos(a1) * weight, 0, Mathf.Sin(a1) * weight);
                Vector3 off2 = new Vector3(Mathf.Cos(a2) * weight, 0, Mathf.Sin(a2) * weight);
                Vector3 p21 = p + off1;
                Vector3 p22 = p - off1;
                Vector3 p31 = p + off2;
                Vector3 p32 = p - off2;
                int     state1, state2;
                Vector2 is1 = OnlineMapsUtils.GetIntersectionPointOfTwoLines(p1 + off1, p21, p31, p2 + off2, out state1);
                Vector2 is2 = OnlineMapsUtils.GetIntersectionPointOfTwoLines(p1 - off1, p22, p32, p2 - off2, out state2);
                if (state1 == 1)
                {
                    side1.Add(new Vector3(is1.x, control.GetElevationValue(is1.x, is1.y, bestElevationYScale, tlx, tly, brx, bry), is1.y));
                }
                if (state2 == 1)
                {
                    side2.Add(new Vector3(is2.x, control.GetElevationValue(is2.x, is2.y, bestElevationYScale, tlx, tly, brx, bry), is2.y));
                }
            }
        }

        for (int i = 0; i < Mathf.Min(side1.Count, side2.Count) - 1; i++)
        {
            int ti = verticles.Count;

            verticles.Add(side1[i]);
            verticles.Add(side1[i + 1]);
            verticles.Add(side2[i + 1]);
            verticles.Add(side2[i]);

            normals.Add(Vector3.up);
            normals.Add(Vector3.up);
            normals.Add(Vector3.up);
            normals.Add(Vector3.up);

            uv.Add(new Vector2(0, 0));
            uv.Add(new Vector2(0, 1));
            uv.Add(new Vector2(1, 1));
            uv.Add(new Vector2(1, 0));

            triangles.Add(ti);
            triangles.Add(ti + 1);
            triangles.Add(ti + 2);
            triangles.Add(ti);
            triangles.Add(ti + 2);
            triangles.Add(ti + 3);
        }

        activePoints.Clear();
    }
    protected void DrawActivePoints(OnlineMapsTileSetControl control, ref List <Vector2> activePoints, ref List <Vector3> vertices, ref List <Vector3> normals, ref List <int> triangles, ref List <Vector2> uv, float width)
    {
        if (activePoints.Count < 2)
        {
            activePoints.Clear();
            return;
        }

        float w2 = width * 2;

        Vector3 prevS1 = Vector3.zero;
        Vector3 prevS2 = Vector3.zero;

        int  c = activePoints.Count - 1;
        bool extraPointAdded = false;

        for (int i = 0; i < activePoints.Count; i++)
        {
            float px = -activePoints[i].x;
            float pz = activePoints[i].y;

            Vector3 s1;
            Vector3 s2;

            if (i == 0 || i == c)
            {
                float p1x, p1z, p2x, p2z;

                if (i == 0)
                {
                    p1x = px;
                    p1z = pz;
                    p2x = -activePoints[i + 1].x;
                    p2z = activePoints[i + 1].y;
                }
                else
                {
                    p1x = -activePoints[i - 1].x;
                    p1z = activePoints[i - 1].y;
                    p2x = px;
                    p2z = pz;
                }

                float a = OnlineMapsUtils.Angle2DRad(p1x, p1z, p2x, p2z, 90);

                float offX = Mathf.Cos(a) * width;
                float offZ = Mathf.Sin(a) * width;

                float s1x = px + offX;
                float s1z = pz + offZ;
                float s2x = px - offX;
                float s2z = pz - offZ;

                float s1y = OnlineMapsElevationManagerBase.GetElevation(s1x, s1z, bestElevationYScale, tlx, tly, brx, bry);
                float s2y = OnlineMapsElevationManagerBase.GetElevation(s2x, s2z, bestElevationYScale, tlx, tly, brx, bry);

                s1 = new Vector3(s1x, s1y, s1z);
                s2 = new Vector3(s2x, s2y, s2z);
            }
            else
            {
                float p1x = -activePoints[i - 1].x;
                float p1z = activePoints[i - 1].y;
                float p2x = -activePoints[i + 1].x;
                float p2z = activePoints[i + 1].y;

                float a1 = OnlineMapsUtils.Angle2DRad(p1x, p1z, px, pz, 90);
                float a3 = OnlineMapsUtils.AngleOfTriangle(activePoints[i - 1], activePoints[i + 1], activePoints[i]) * Mathf.Rad2Deg;
                if (a3 < 60 && !extraPointAdded)
                {
                    activePoints.Insert(i + 1, Vector2.Lerp(activePoints[i], activePoints[i + 1], 0.001f));
                    activePoints[i] = Vector2.Lerp(activePoints[i], activePoints[i - 1], 0.001f);
                    c++;
                    i--;
                    extraPointAdded = true;
                    continue;
                }

                extraPointAdded = false;
                float a2 = OnlineMapsUtils.Angle2DRad(px, pz, p2x, p2z, 90);

                float off1x = Mathf.Cos(a1) * width;
                float off1z = Mathf.Sin(a1) * width;
                float off2x = Mathf.Cos(a2) * width;
                float off2z = Mathf.Sin(a2) * width;

                float p21x = px + off1x;
                float p21z = pz + off1z;
                float p22x = px - off1x;
                float p22z = pz - off1z;
                float p31x = px + off2x;
                float p31z = pz + off2z;
                float p32x = px - off2x;
                float p32z = pz - off2z;

                float is1x, is1z, is2x, is2z;

                int state1 = OnlineMapsUtils.GetIntersectionPointOfTwoLines(p1x + off1x, p1z + off1z, p21x, p21z, p31x, p31z, p2x + off2x, p2z + off2z, out is1x, out is1z);
                int state2 = OnlineMapsUtils.GetIntersectionPointOfTwoLines(p1x - off1x, p1z - off1z, p22x, p22z, p32x, p32z, p2x - off2x, p2z - off2z, out is2x, out is2z);

                if (state1 == 1 && state2 == 1)
                {
                    float o1x = is1x - px;
                    float o1z = is1z - pz;
                    float o2x = is2x - px;
                    float o2z = is2z - pz;

                    float m1 = Mathf.Sqrt(o1x * o1x + o1z * o1z);
                    float m2 = Mathf.Sqrt(o2x * o2x + o2z * o2z);

                    if (m1 > w2)
                    {
                        is1x = o1x / m1 * w2 + px;
                        is1z = o1z / m1 * w2 + pz;
                    }
                    if (m2 > w2)
                    {
                        is2x = o2x / m2 * w2 + px;
                        is2z = o2z / m2 * w2 + pz;
                    }

                    s1 = new Vector3(is1x, OnlineMapsElevationManagerBase.GetElevation(is1x, is1z, bestElevationYScale, tlx, tly, brx, bry), is1z);
                    s2 = new Vector3(is2x, OnlineMapsElevationManagerBase.GetElevation(is2x, is2z, bestElevationYScale, tlx, tly, brx, bry), is2z);
                }
                else
                {
                    float po1x = p1x + off1x;
                    float po1z = p1z + off1z;
                    float po2x = p2x - off1x;
                    float po2z = p2z - off1z;

                    s1 = new Vector3(po1x, OnlineMapsElevationManagerBase.GetElevation(po1x, po1z, bestElevationYScale, tlx, tly, brx, bry), po1z);
                    s2 = new Vector3(po2x, OnlineMapsElevationManagerBase.GetElevation(po2x, po2z, bestElevationYScale, tlx, tly, brx, bry), po2z);
                }
            }

            if (i > 0)
            {
                AddLineSegment(vertices, normals, triangles, uv, s1, s2, prevS1, prevS2);
            }

            prevS1 = s1;
            prevS2 = s2;
        }

        activePoints.Clear();
    }
예제 #4
0
    protected void DrawActivePoints(OnlineMapsTileSetControl control, ref List <Vector2> activePoints, ref List <Vector3> verticles, ref List <Vector3> normals, ref List <int> triangles, ref List <Vector2> uv, float weight)
    {
        if (activePoints.Count < 2)
        {
            activePoints.Clear();
            return;
        }

        float w2 = weight * 2;

        Vector3 prevS1 = Vector3.zero;
        Vector3 prevS2 = Vector3.zero;

        int c = activePoints.Count - 1;

        for (int i = 0; i < activePoints.Count; i++)
        {
            float px = -activePoints[i].x;
            float pz = activePoints[i].y;

            Vector3 s1 = Vector3.zero;
            Vector3 s2 = Vector3.zero;

            if (i == 0 || i == c)
            {
                float p1x, p1z, p2x, p2z;

                if (i == 0)
                {
                    p1x = px;
                    p1z = pz;
                    p2x = -activePoints[i + 1].x;
                    p2z = activePoints[i + 1].y;
                }
                else
                {
                    p1x = -activePoints[i - 1].x;
                    p1z = activePoints[i - 1].y;
                    p2x = px;
                    p2z = pz;
                }

                float a    = OnlineMapsUtils.Angle2DRad(p1x, p1z, p2x, p2z, 90);
                float offX = Mathf.Cos(a) * weight;
                float offZ = Mathf.Sin(a) * weight;
                float s1x  = px + offX;
                float s1z  = pz + offZ;
                float s2x  = px - offX;
                float s2z  = pz - offZ;

                float s1y = control.GetElevationValue(s1x, s1z, bestElevationYScale, tlx, tly, brx, bry);
                float s2y = control.GetElevationValue(s2x, s2z, bestElevationYScale, tlx, tly, brx, bry);

                s1 = new Vector3(s1x, s1y, s1z);
                s2 = new Vector3(s2x, s2y, s2z);
            }
            else
            {
                float p1x = -activePoints[i - 1].x;
                float p1z = activePoints[i - 1].y;
                float p2x = -activePoints[i + 1].x;
                float p2z = activePoints[i + 1].y;

                float a1 = OnlineMapsUtils.Angle2DRad(p1x, p1z, px, pz, 90);
                float a2 = OnlineMapsUtils.Angle2DRad(px, pz, p2x, p2z, 90);

                float off1x = Mathf.Cos(a1) * weight;
                float off1z = Mathf.Sin(a1) * weight;
                float off2x = Mathf.Cos(a2) * weight;
                float off2z = Mathf.Sin(a2) * weight;

                float p21x = px + off1x;
                float p21z = pz + off1z;
                float p22x = px - off1x;
                float p22z = pz - off1z;
                float p31x = px + off2x;
                float p31z = pz + off2z;
                float p32x = px - off2x;
                float p32z = pz - off2z;

                float is1x, is1z, is2x, is2z;

                int state1 = OnlineMapsUtils.GetIntersectionPointOfTwoLines(p1x + off1x, p1z + off1z, p21x, p21z, p31x, p31z, p2x + off2x, p2z + off2z, out is1x, out is1z);
                int state2 = OnlineMapsUtils.GetIntersectionPointOfTwoLines(p1x - off1x, p1z - off1z, p22x, p22z, p32x, p32z, p2x - off2x, p2z - off2z, out is2x, out is2z);

                if (state1 == 1 && state2 == 1)
                {
                    float o1x = is1x - px;
                    float o1z = is1z - pz;
                    float o2x = is2x - px;
                    float o2z = is2z - pz;

                    float m1 = Mathf.Sqrt(o1x * o1x + o1z * o1z);
                    float m2 = Mathf.Sqrt(o2x * o2x + o2z * o2z);

                    if (m1 > w2)
                    {
                        is1x = o1x / m1 * w2 + px;
                        is1z = o1z / m1 * w2 + pz;
                    }
                    if (m2 > w2)
                    {
                        is2x = o2x / m2 * w2 + px;
                        is2z = o2z / m2 * w2 + pz;
                    }

                    s1 = new Vector3(is1x, control.GetElevationValue(is1x, is1z, bestElevationYScale, tlx, tly, brx, bry), is1z);
                    s2 = new Vector3(is2x, control.GetElevationValue(is2x, is2z, bestElevationYScale, tlx, tly, brx, bry), is2z);
                }
                else
                {
                    float po1x = p1x + off1x;
                    float po1z = p1z + off1z;
                    float po2x = p2x - off1x;
                    float po2z = p2z - off1z;

                    s1 = new Vector3(po1x, control.GetElevationValue(po1x, po1z, bestElevationYScale, tlx, tly, brx, bry), po1z);
                    s2 = new Vector3(po2x, control.GetElevationValue(po2x, po2z, bestElevationYScale, tlx, tly, brx, bry), po2z);
                }
            }

            if (i > 0)
            {
                int ti = verticles.Count;

                verticles.Add(prevS1);
                verticles.Add(s1);
                verticles.Add(s2);
                verticles.Add(prevS2);

                normals.Add(Vector3.up);
                normals.Add(Vector3.up);
                normals.Add(Vector3.up);
                normals.Add(Vector3.up);

                uv.Add(new Vector2(0, 0));
                uv.Add(new Vector2(0, 1));
                uv.Add(new Vector2(1, 1));
                uv.Add(new Vector2(1, 0));

                triangles.Add(ti);
                triangles.Add(ti + 1);
                triangles.Add(ti + 2);
                triangles.Add(ti);
                triangles.Add(ti + 2);
                triangles.Add(ti + 3);
            }

            prevS1 = s1;
            prevS2 = s2;
        }

        activePoints.Clear();
    }