コード例 #1
0
    /// <summary>
    /// Updates marker instance.
    /// </summary>
    /// <param name="bounds">Bounds of the map mesh</param>
    /// <param name="tlx">Longitude of top-left corner of the map</param>
    /// <param name="tly">Latitude of top-left corner of the map</param>
    /// <param name="brx">Longitude of bottom-right corner of the map</param>
    /// <param name="bry">Latitude of bottom-right corner of the map</param>
    /// <param name="zoom">Zoom of the map</param>
    /// <param name="ttlx">Tile X of top-left corner of the map</param>
    /// <param name="ttly">Tile Y of top-left corner of the map</param>
    /// <param name="tbrx">Tile X of bottom-right corner of the map</param>
    /// <param name="tbry">Tile Y of bottom-right corner of the map</param>
    /// <param name="bestYScale">Best y scale for current map view</param>
    public void Update(Bounds bounds, double tlx, double tly, double brx, double bry, int zoom, double ttlx, double ttly, double tbrx, double tbry, float bestYScale)
    {
        if (!enabled)
        {
            return;
        }
        if (instance == null)
        {
            Init(map.transform);
        }

        if (!range.InRange(zoom))
        {
            visible = false;
        }
        else if (OnCheckMapBoundaries != null)
        {
            visible = OnCheckMapBoundaries();
        }
        else if (checkMapBoundaries)
        {
            if (latitude > tly || latitude < bry)
            {
                visible = false;
            }
            else if (tlx < brx &&
                     (longitude < tlx || longitude > brx) &&
                     (longitude - 360 < tlx || longitude - 360 > brx) &&
                     (longitude + 360 < tlx || longitude + 360 > brx))
            {
                visible = false;
            }
            else if (tlx > brx && longitude < tlx && longitude > brx)
            {
                visible = false;
            }
            else
            {
                visible = true;
            }
        }
        else
        {
            visible = true;
        }

        if (!visible)
        {
            return;
        }

        if (_prefab != prefab)
        {
            Reinit(tlx, tly, brx, bry, zoom);
        }

        double mx, my;

        map.projection.CoordinatesToTile(longitude, latitude, zoom, out mx, out my);

        int maxX = 1 << zoom;

        double sx  = tbrx - ttlx;
        double mpx = mx - ttlx;

        if (sx <= 0)
        {
            sx += maxX;
        }

        if (checkMapBoundaries)
        {
            if (mpx < 0)
            {
                mpx += maxX;
            }
            else if (mpx > maxX)
            {
                mpx -= maxX;
            }
        }
        else
        {
            double dx1 = Math.Abs(mpx - ttlx);
            double dx2 = Math.Abs(mpx - tbrx);
            double dx3 = Math.Abs(mpx - tbrx + maxX);
            if (dx1 > dx2 && dx1 > dx3)
            {
                mpx += maxX;
            }
        }

        double px = mpx / sx;
        double pz = (ttly - my) / (ttly - tbry);

        _relativePosition = new Vector3((float)px, 0, (float)pz);

        OnlineMapsTileSetControl tsControl = control as OnlineMapsTileSetControl;

        if (tsControl != null)
        {
            px = -tsControl.sizeInScene.x / 2 - (px - 0.5) * tsControl.sizeInScene.x;
            pz = tsControl.sizeInScene.y / 2 + (pz - 0.5) * tsControl.sizeInScene.y;
        }
        else
        {
            Vector3 center = bounds.center;
            Vector3 size   = bounds.size;
            px = center.x - (px - 0.5) * size.x / map.transform.lossyScale.x - map.transform.position.x;
            pz = center.z + (pz - 0.5) * size.z / map.transform.lossyScale.z - map.transform.position.z;
        }

        Vector3 oldPosition = instance.transform.localPosition;
        float   y           = 0;

        if (altitude.HasValue)
        {
            float yScale = OnlineMapsElevationManagerBase.GetBestElevationYScale(tlx, tly, brx, bry);
            y = altitude.Value;
            if (altitudeType == OnlineMapsAltitudeType.relative && tsControl != null)
            {
                y += OnlineMapsElevationManagerBase.GetUnscaledElevation(px, pz, tlx, tly, brx, bry);
            }
            y *= yScale;

            if (tsControl != null && OnlineMapsElevationManagerBase.isActive)
            {
                if (OnlineMapsElevationManagerBase.instance.bottomMode == OnlineMapsElevationBottomMode.minValue)
                {
                    y -= OnlineMapsElevationManagerBase.instance.minValue * bestYScale;
                }
                y *= OnlineMapsElevationManagerBase.instance.scale;
            }
        }
        else if (tsControl != null)
        {
            y = OnlineMapsElevationManagerBase.GetElevation(px, pz, bestYScale, tlx, tly, brx, bry);
        }

        Vector3 newPosition = new Vector3((float)px, y, (float)pz);

        if (sizeType == SizeType.meters)
        {
            double dx, dy;
            OnlineMapsUtils.DistanceBetweenPoints(tlx, tly, brx, bry, out dx, out dy);
            if (tsControl != null)
            {
                dx = tsControl.sizeInScene.x / dx / 1000;
                dy = tsControl.sizeInScene.y / dy / 1000;
            }

            double d  = (dx + dy) / 2 * scale;
            float  fd = (float)d;

            instance.transform.localScale = new Vector3(fd, fd, fd);
        }

        if (oldPosition != newPosition)
        {
            instance.transform.localPosition = newPosition;
        }
    }
コード例 #2
0
    private void UpdateCameraPosition()
    {
        if (rotation.x > maxRotationX)
        {
            rotation.x = maxRotationX;
        }
        else if (rotation.x < 0)
        {
            rotation.x = 0;
        }

        float rx = 90 - rotation.x;

        if (rx > 89.9)
        {
            rx = 89.9f;
        }

        double px = Math.Cos(rx * Mathf.Deg2Rad) * distance;
        double py = Math.Sin(rx * Mathf.Deg2Rad) * distance;
        double pz = Math.Cos(rotation.y * Mathf.Deg2Rad) * px;

        px = Math.Sin(rotation.y * Mathf.Deg2Rad) * px;

        Vector3 targetPosition;

        if (adjustTo == OnlineMapsCameraAdjust.gameObject && adjustToGameObject != null)
        {
            targetPosition = adjustToGameObject.transform.position;
        }
        else
        {
            targetPosition = map.transform.position;
            Vector3 offset = new Vector3(sizeInScene.x / -2, 0, sizeInScene.y / 2);

            if (OnlineMapsElevationManagerBase.useElevation)
            {
                double tlx, tly, brx, bry;
                map.GetCorners(out tlx, out tly, out brx, out bry);
                float yScale = OnlineMapsElevationManagerBase.GetBestElevationYScale(tlx, tly, brx, bry);
                if (adjustTo == OnlineMapsCameraAdjust.maxElevationInArea)
                {
                    offset.y = OnlineMapsElevationManagerBase.instance.GetMaxElevation(yScale);
                }
                else
                {
                    offset.y = OnlineMapsElevationManagerBase.GetElevation(targetPosition.x, targetPosition.z, yScale, tlx, tly, brx, bry);
                }
            }

            offset.Scale(map.transform.lossyScale);
            targetPosition += map.transform.rotation * offset;
        }

        Vector3 oldPosition = activeCamera.transform.position;
        Vector3 newPosition = map.transform.rotation * new Vector3((float)px, (float)py, (float)pz) + targetPosition;

        activeCamera.transform.position = newPosition;
        activeCamera.transform.LookAt(targetPosition);

        if (oldPosition != newPosition && OnCameraControl != null)
        {
            OnCameraControl();
        }
    }
コード例 #3
0
    private void OnDrawMarkers()
    {
        if (markersGameObjects == null)
        {
            InitMarkersMesh(0);
        }
        if (markerBillboards == null)
        {
            markerBillboards = new Dictionary <int, OnlineMapsMarkerBillboard>();
        }

        double tlx, tly, brx, bry;

        map.GetCorners(out tlx, out tly, out brx, out bry);
        if (brx < tlx)
        {
            brx += 360;
        }

        int maxX = 1 << map.buffer.renderState.zoom;

        double px, py;

        map.projection.CoordinatesToTile(tlx, tly, map.zoom, out px, out py);

        float yScale = OnlineMapsElevationManagerBase.GetBestElevationYScale(tlx, tly, brx, bry);

        Bounds  mapBounds      = control.cl.bounds;
        Vector3 positionOffset = control.transform.position - mapBounds.min;
        Vector3 size           = mapBounds.size;

        size = control.transform.rotation * size;
        if (!control.resultIsTexture)
        {
            positionOffset.x -= size.x;
        }

        float zoomCoof = map.buffer.renderState.zoomCoof;

        foreach (KeyValuePair <int, OnlineMapsMarkerBillboard> billboard in markerBillboards)
        {
            billboard.Value.used = false;
        }

        foreach (OnlineMapsMarker marker in OnlineMapsMarkerManager.instance)
        {
            if (!marker.enabled || !marker.range.InRange(map.zoom))
            {
                continue;
            }

            double mx, my;
            marker.GetPosition(out mx, out my);

            if (!((mx > tlx && mx < brx || mx + 360 > tlx && mx + 360 < brx ||
                   mx - 360 > tlx && mx - 360 < brx) &&
                  my < tly && my > bry))
            {
                continue;
            }

            int markerHashCode = marker.GetHashCode();
            OnlineMapsMarkerBillboard markerBillboard;

            if (!markerBillboards.ContainsKey(markerHashCode))
            {
                markerBillboard = OnlineMapsMarkerBillboard.Create(marker);
                markerBillboard.transform.parent = markersGameObjects[0].transform;
                markerBillboard.gameObject.layer = markersGameObjects[0].layer;

                markerBillboards.Add(markerHashCode, markerBillboard);
            }
            else
            {
                markerBillboard = markerBillboards[markerHashCode];
            }

            if (markerBillboard == null)
            {
                continue;
            }

            float sx = size.x / map.buffer.renderState.width * marker2DSize * marker.scale;
            float sz = size.z / map.buffer.renderState.height * marker2DSize * marker.scale;
            float s  = Mathf.Max(sx, sz);

            markerBillboard.transform.localScale = new Vector3(s, s, s);

            double mpx, mpy;
            map.projection.CoordinatesToTile(mx, my, map.buffer.renderState.zoom, out mpx, out mpy);

            mpx  = OnlineMapsUtils.Repeat(mpx - px, 0, maxX);
            mpy -= py;

            float x = (float)(-mpx / map.width * OnlineMapsUtils.tileSize * size.x / zoomCoof + positionOffset.x);
            float z = (float)(mpy / map.height * OnlineMapsUtils.tileSize * size.z / zoomCoof - positionOffset.z);

            float y = OnlineMapsElevationManagerBase.instance != null?OnlineMapsElevationManagerBase.GetElevation(x, z, yScale, tlx, tly, brx, bry) : 0;

            markerBillboard.transform.localPosition = control.transform.rotation * new Vector3(x, y, z);
            markerBillboard.used = true;
        }

        List <int> keysForRemove = new List <int>();

        foreach (KeyValuePair <int, OnlineMapsMarkerBillboard> billboard in markerBillboards)
        {
            if (!billboard.Value.used)
            {
                billboard.Value.Dispose();
                keysForRemove.Add(billboard.Key);
            }
        }

        foreach (int key in keysForRemove)
        {
            markerBillboards.Remove(key);
        }
    }
コード例 #4
0
    private void OnDrawMarkers()
    {
        if (markersGameObjects == null)
        {
            InitMarkersMesh(0);
        }

        double tlx, tly, brx, bry;

        map.GetCorners(out tlx, out tly, out brx, out bry);
        if (brx < tlx)
        {
            brx += 360;
        }

        int zoom = map.buffer.renderState.zoom;
        int maxX = 1 << zoom;

        double tx, ty;

        map.projection.CoordinatesToTile(tlx, tly, zoom, out tx, out ty);

        float yScale = OnlineMapsElevationManagerBase.GetBestElevationYScale(tlx, tly, brx, bry);

        OnlineMapsControlBaseDynamicMesh tileset = control;

        float cx = -tileset.sizeInScene.x / map.buffer.renderState.width;
        float cy = tileset.sizeInScene.y / map.buffer.renderState.height;

        if (usedMarkers == null)
        {
            usedMarkers = new List <FlatMarker>(32);
        }
        else
        {
            for (int i = 0; i < usedMarkers.Count; i++)
            {
                usedMarkers[i].Dispose();
            }
            usedMarkers.Clear();
        }

        List <Texture> usedTextures = new List <Texture>(32)
        {
            OnlineMapsMarkerManager.instance.defaultTexture
        };
        List <List <int> > usedTexturesMarkerIndex = new List <List <int> >(32)
        {
            new List <int>(32)
        };

        int usedMarkersCount = 0;

        Bounds tilesetBounds = new Bounds(
            new Vector3(tileset.sizeInScene.x / -2, 0, tileset.sizeInScene.y / 2),
            new Vector3(tileset.sizeInScene.x, 0, tileset.sizeInScene.y));

        IEnumerable <OnlineMapsMarker> markers = OnlineMapsMarkerManager.instance.Where(delegate(OnlineMapsMarker marker)
        {
            if (!marker.enabled || !marker.range.InRange(zoom))
            {
                return(false);
            }

            if (OnCheckMarker2DVisibility != null)
            {
                if (!OnCheckMarker2DVisibility(marker))
                {
                    return(false);
                }
            }
            else if (control.checkMarker2DVisibility == OnlineMapsTilesetCheckMarker2DVisibility.pivot)
            {
                double mx, my;
                marker.GetPosition(out mx, out my);

                bool a = my > tly ||
                         my < bry ||
                         (
                    (mx <tlx || mx> brx) &&
                    (mx + 360 < tlx || mx + 360 > brx) &&
                    (mx - 360 < tlx || mx - 360 > brx)
                         );
                if (a)
                {
                    return(false);
                }
            }

            return(true);
        });

        float[] offsets    = null;
        bool    useOffsetY = false;

        int index = 0;

        if (markerComparer != null)
        {
            markers = markers.OrderBy(m => m, markerComparer);
        }
        else
        {
            markers = markers.OrderBy(m =>
            {
                double mx, my;
                m.GetPosition(out mx, out my);
                return(90 - my);
            });
            useOffsetY = OnGetFlatMarkerOffsetY != null;

            if (useOffsetY)
            {
                int countMarkers = markers.Count();

                SortedMarker[] sortedMarkers = new SortedMarker[countMarkers];
                foreach (OnlineMapsMarker marker in markers)
                {
                    sortedMarkers[index++] = new SortedMarker
                    {
                        marker = marker,
                        offset = OnGetFlatMarkerOffsetY(marker)
                    };
                }

                offsets = new float[countMarkers];
                OnlineMapsMarker[] nMarkers = new OnlineMapsMarker[countMarkers];
                int i = 0;
                foreach (SortedMarker sm in sortedMarkers.OrderBy(m => m.offset))
                {
                    nMarkers[i] = sm.marker;
                    offsets[i]  = sm.offset;
                    i++;
                    sm.Dispose();
                }
                markers = nMarkers;
            }
        }

        if (markersVertices == null)
        {
            markersVertices = new List <Vector3>(64);
        }
        else
        {
            markersVertices.Clear();
        }

        Vector3 tpos = control.transform.position;

        foreach (Mesh mesh in markersMeshes)
        {
            mesh.Clear();
        }

        float zoomCoof = map.buffer.renderState.zoomCoof;

        Matrix4x4 matrix    = new Matrix4x4();
        int       meshIndex = 0;

        index = -1;
        foreach (OnlineMapsMarker marker in markers)
        {
            index++;
            double fx, fy;
            marker.GetTilePosition(out fx, out fy);

            Vector2 offset = marker.GetAlignOffset();
            offset *= marker.scale;

            //Debug.Log(fx + "   " + tx + "   " + zoomCoof);

            fx = fx - tx;

            if (fx < 0)
            {
                fx += maxX;
            }
            else if (fx > maxX)
            {
                fx -= maxX;
            }

            fx /= zoomCoof;

            fx = fx * OnlineMapsUtils.tileSize - offset.x;
            fy = (fy - ty) / zoomCoof * OnlineMapsUtils.tileSize - offset.y;

            if (marker.texture == null)
            {
                marker.texture = OnlineMapsMarkerManager.instance.defaultTexture;
                marker.Init();
            }

            float markerWidth  = marker.texture.width * marker.scale;
            float markerHeight = marker.texture.height * marker.scale;

            float rx1 = (float)(fx * cx);
            float ry1 = (float)(fy * cy);
            float rx2 = (float)((fx + markerWidth) * cx);
            float ry2 = (float)((fy + markerHeight) * cy);

            Vector3 center = new Vector3((float)((fx + offset.x) * cx), 0, (float)((fy + offset.y) * cy));

            Vector3 p1 = new Vector3(rx1 - center.x, 0, ry1 - center.z);
            Vector3 p2 = new Vector3(rx2 - center.x, 0, ry1 - center.z);
            Vector3 p3 = new Vector3(rx2 - center.x, 0, ry2 - center.z);
            Vector3 p4 = new Vector3(rx1 - center.x, 0, ry2 - center.z);

            float angle = Mathf.Repeat(marker.rotation, 1) * 360;

            if (Math.Abs(angle) > float.Epsilon)
            {
                matrix.SetTRS(Vector3.zero, Quaternion.Euler(0, angle, 0), Vector3.one);

                p1 = matrix.MultiplyPoint(p1) + center;
                p2 = matrix.MultiplyPoint(p2) + center;
                p3 = matrix.MultiplyPoint(p3) + center;
                p4 = matrix.MultiplyPoint(p4) + center;
            }
            else
            {
                p1 += center;
                p2 += center;
                p3 += center;
                p4 += center;
            }

            if (control.checkMarker2DVisibility == OnlineMapsTilesetCheckMarker2DVisibility.bounds)
            {
                Vector3 markerCenter = (p2 + p4) / 2;
                Vector3 markerSize   = p4 - p2;
                if (!tilesetBounds.Intersects(new Bounds(markerCenter, markerSize)))
                {
                    continue;
                }
            }

            float y       = OnlineMapsElevationManagerBase.GetElevation((rx1 + rx2) / 2, (ry1 + ry2) / 2, yScale, tlx, tly, brx, bry);
            float yOffset = useOffsetY ? offsets[index] : 0;

            p1.y = p2.y = p3.y = p4.y = y + yOffset;

            int vIndex = markersVertices.Count;

            markersVertices.Add(p1);
            markersVertices.Add(p2);
            markersVertices.Add(p3);
            markersVertices.Add(p4);

            usedMarkers.Add(new FlatMarker(marker, p1 + tpos, p2 + tpos, p3 + tpos, p4 + tpos));

            if (OnGenerateMarkerVertices != null)
            {
                OnGenerateMarkerVertices(marker, markersVertices, vIndex);
            }

            if (marker.texture == OnlineMapsMarkerManager.instance.defaultTexture)
            {
                usedTexturesMarkerIndex[0].Add(usedMarkersCount);
            }
            else
            {
                int textureIndex = usedTextures.IndexOf(marker.texture);
                if (textureIndex != -1)
                {
                    usedTexturesMarkerIndex[textureIndex].Add(usedMarkersCount);
                }
                else
                {
                    usedTextures.Add(marker.texture);
                    usedTexturesMarkerIndex.Add(new List <int>(32));
                    usedTexturesMarkerIndex[usedTexturesMarkerIndex.Count - 1].Add(usedMarkersCount);
                }
            }

            usedMarkersCount++;

            if (usedMarkersCount == 16250)
            {
                SetMarkersMesh(usedMarkersCount, usedTextures, usedTexturesMarkerIndex, meshIndex);
                meshIndex++;
                markersVertices.Clear();
                usedMarkersCount = 0;
                usedTextures.Clear();
                usedTextures.Add(OnlineMapsMarkerManager.instance.defaultTexture);
                usedTexturesMarkerIndex.Clear();
                usedTexturesMarkerIndex.Add(new List <int>(32));
            }
        }

        SetMarkersMesh(usedMarkersCount, usedTextures, usedTexturesMarkerIndex, meshIndex);
    }
コード例 #5
0
    public override void DrawOnTileset(OnlineMapsTileSetControl control, int index)
    {
        if (points == null)
        {
            return;
        }

        base.DrawOnTileset(control, index);

        if (!visible)
        {
            active = false;
            return;
        }

        InitMesh(control, borderColor, backgroundColor);
        InitLineMesh(points, control, ref vertices, ref normals, ref triangles, ref uv, borderWidth, true, false);

        mesh.Clear();

        if (vertices.Count < 4)
        {
            return;
        }

        active = true;

        Vector3 v1 = (vertices[0] + vertices[3]) / 2;
        Vector3 v2 = (vertices[vertices.Count - 3] + vertices[vertices.Count - 2]) / 2;

        if ((v1.x - v2.x) * (v1.x - v2.x) + (v1.z - v2.z) * (v1.z - v2.z) < float.Epsilon)
        {
            int     s1, s2;
            Vector3 v0 = vertices[0];
            v1 = vertices[1];
            v2 = vertices[2];
            Vector3 v3 = vertices[3];
            Vector3 vs1 = vertices[vertices.Count - 1];
            Vector3 vs2 = vertices[vertices.Count - 2];
            Vector3 vs3 = vertices[vertices.Count - 3];
            Vector3 vs4 = vertices[vertices.Count - 4];
            Vector3 nv1 = Vector3.zero, nv2 = Vector3.zero;
            s1 = OnlineMapsUtils.GetIntersectionPointOfTwoLines(v0.x, v0.z, v1.x, v1.z, vs4.x, vs4.z, vs3.x, vs3.z, out nv1.x, out nv1.z);
            s2 = OnlineMapsUtils.GetIntersectionPointOfTwoLines(v3.x, v3.z, v2.x, v2.z, vs1.x, vs1.z, vs2.x, vs2.z, out nv2.x, out nv2.z);

            if (s1 == 1 && s2 == 1)
            {
                nv1.y       = OnlineMapsElevationManagerBase.GetElevation(nv1.x, nv1.z, bestElevationYScale, tlx, tly, brx, bry);
                nv2.y       = OnlineMapsElevationManagerBase.GetElevation(nv2.x, nv2.z, bestElevationYScale, tlx, tly, brx, bry);
                vertices[0] = vertices[vertices.Count - 3] = nv1;
                vertices[3] = vertices[vertices.Count - 2] = nv2;
            }
            else
            {
                vertices[0] = vertices[vertices.Count - 3] = (vertices[0] + vertices[vertices.Count - 3]) / 2;
                vertices[3] = vertices[vertices.Count - 2] = (vertices[3] + vertices[vertices.Count - 2]) / 2;
            }
        }

        int[] fillTriangles = null;

        if (!checkMapBoundaries && backgroundColor.a > 0 && vertices.Count > 0)
        {
            float l1 = 0;
            float l2 = 0;

            for (int i = 0; i < vertices.Count / 4 - 1; i++)
            {
                Vector3 p11 = vertices[i * 4];
                Vector3 p12 = vertices[(i + 1) * 4];

                Vector3 p21 = vertices[i * 4 + 3];
                Vector3 p22 = vertices[(i + 1) * 4 + 3];

                l1 += (p11 - p12).magnitude;
                l2 += (p21 - p22).magnitude;
            }

            bool side = l2 < l1;
            int  off1 = side ? 3 : 0;
            int  off2 = side ? 2 : 1;

            Vector2        lastPoint       = Vector2.zero;
            List <int>     internalIndices = new List <int>(vertices.Count / 4);
            List <Vector2> internalPoints  = new List <Vector2>(vertices.Count / 4);
            float          w = borderWidth / 2;
            w *= w;
            for (int i = 0, j = 0; i < vertices.Count / 4; i++, j += 4)
            {
                Vector3 p  = vertices[j + off1];
                Vector2 p2 = new Vector2(p.x, p.z);
                if (i > 0)
                {
                    if ((lastPoint - p2).sqrMagnitude > w)
                    {
                        internalIndices.Add(j + off1);
                        internalPoints.Add(p2);
                        lastPoint = p2;
                    }
                }
                else
                {
                    internalIndices.Add(j + off1);
                    internalPoints.Add(p2);
                    lastPoint = p2;
                }
                p  = vertices[j + off2];
                p2 = new Vector2(p.x, p.z);
                if ((lastPoint - p2).sqrMagnitude > w)
                {
                    internalIndices.Add(j + off2);
                    internalPoints.Add(p2);
                    lastPoint = p2;
                }
            }

            if (internalPoints[0] == internalPoints[internalPoints.Count - 1])
            {
                internalPoints.RemoveAt(internalPoints.Count - 1);
            }

            fillTriangles = OnlineMapsUtils.Triangulate(internalPoints).ToArray();
            //fillTriangles = OMTriangulator.Triangulate(internalPoints);


            if (fillTriangles.Length > 2)
            {
                for (int i = 0; i < fillTriangles.Length; i++)
                {
                    fillTriangles[i] = internalIndices[fillTriangles[i]];
                }

                Vector3 side1 = vertices[fillTriangles[1]] - vertices[fillTriangles[0]];
                Vector3 side2 = vertices[fillTriangles[2]] - vertices[fillTriangles[0]];
                Vector3 perp  = Vector3.Cross(side1, side2);

                bool reversed = perp.y < 0;
                if (reversed)
                {
                    fillTriangles = fillTriangles.Reverse().ToArray();
                }
            }
            else
            {
                fillTriangles = null;
            }
        }

        mesh.subMeshCount = 2;

        mesh.SetVertices(vertices);
        mesh.SetNormals(normals);
        mesh.SetUVs(0, uv);

        mesh.SetTriangles(triangles.ToArray(), 0);
        if (fillTriangles != null)
        {
            mesh.SetTriangles(fillTriangles.ToArray(), 1);
        }

        UpdateMaterialsQuote(control, index);
    }
コード例 #6
0
    private void UpdateMapSubMesh(int x, int y, int w, int h, double subMeshSizeX, double subMeshSizeY, int subMeshVX, int subMeshVZ, double startPosX, double startPosZ, float yScale, double tlx, double tly, double brx, double bry, Material[] materials, ref float minY, ref float maxY, bool hasTraffic, bool hasOverlayBack, bool hasOverlayBackAlpha, bool hasOverlayFront, bool hasOverlayFrontAlpha)
    {
        int mi = x + y * w;
        int i  = mi * (subMeshVX + 1) * (subMeshVZ + 1);

        double cellSizeX = subMeshSizeX / subMeshVX;
        double cellSizeY = subMeshSizeY / subMeshVZ;

        double uvX = 1.0 / subMeshVX;
        double uvZ = 1.0 / subMeshVZ;

        int bx = x + bufferPosition.x;
        int by = y + bufferPosition.y;

        int zoom = map.buffer.renderState.zoom;
        int maxX = 1 << zoom;

        if (bx >= maxX)
        {
            bx -= maxX;
        }
        if (bx < 0)
        {
            bx += maxX;
        }

        OnlineMapsTile tile;

        OnlineMapsTile.GetTile(zoom, bx, by, out tile);

        OnlineMapsTile currentTile = tile;

        if (currentTile != null && currentTile.status != OnlineMapsTileStatus.loaded)
        {
            currentTile = null;
        }
        Vector2 offset = Vector2.zero;
        float   scale  = 1;
        int     z      = zoom;

        Texture tileTexture = currentTile != null ? currentTile.texture : null;
        bool    sendEvent   = true;

        while ((currentTile == null || tileTexture == null) && z > 2)
        {
            z--;

            int            s   = 1 << (zoom - z);
            int            ctx = bx / s;
            int            cty = by / s;
            OnlineMapsTile t;
            OnlineMapsTile.GetTile(z, ctx, cty, out t);
            if (t != null && t.status == OnlineMapsTileStatus.loaded)
            {
                currentTile = t;
                tileTexture = t.texture;
                scale       = 1f / s;
                offset.x    = bx % s * scale;
                offset.y    = (s - by % s - 1) * scale;
                sendEvent   = false;
                break;
            }
        }

        bool needGetElevation = OnlineMapsElevationManagerBase.useElevation;

        float  fy        = 0;
        double spx       = startPosX - x * subMeshSizeX;
        double spz       = startPosZ + y * subMeshSizeY;
        float  tilesizeX = sizeInScene.x;
        float  tilesizeZ = sizeInScene.y;

        for (int ty = 0; ty <= subMeshVZ; ty++)
        {
            double uvy = 1 - uvZ * ty;
            double pz  = spz + ty * cellSizeY;

            if (pz < 0)
            {
                uvy = uvZ * ((pz + cellSizeY) / cellSizeY - 1) + uvy;
                pz  = 0;
            }
            else if (pz > tilesizeZ)
            {
                uvy = uvZ * ((pz - tilesizeZ) / cellSizeY) + uvy;
                pz  = tilesizeZ;
            }

            for (int tx = 0; tx <= subMeshVX; tx++)
            {
                double uvx = uvX * tx;
                double px  = spx - tx * cellSizeX;

                if (px > 0)
                {
                    uvx = uvX * (px - cellSizeX) / cellSizeX + uvx + uvX;
                    px  = 0;
                }
                else if (px < -tilesizeX)
                {
                    uvx = uvX * ((px + tilesizeX) / cellSizeX - 1) + uvx + uvX;
                    px  = -tilesizeX;
                }

                if (needGetElevation)
                {
                    fy = OnlineMapsElevationManagerBase.GetElevation(px, pz, yScale, tlx, tly, brx, bry);
                }

                float fx = (float)px;
                float fz = (float)pz;

                float f*x = (float)uvx;
                float fuy = (float)uvy;

                if (fy < minY)
                {
                    minY = fy;
                }
                if (fy > maxY)
                {
                    maxY = fy;
                }

                if (f*x < 0)
                {
                    f*x = 0;
                }
                else if (f*x > 1)
                {
                    f*x = 1;
                }

                if (fuy < 0)
                {
                    fuy = 0;
                }
                else if (fuy > 1)
                {
                    fuy = 1;
                }

                vertices[i] = new Vector3(fx, fy, fz);
                uv[i++]     = new Vector2(f*x, fuy);
            }
        }

        Material material = materials[mi];

        if (currentTile != null)
        {
            bool hasTileTexture = tileTexture != null;
            if (!hasTileTexture)
            {
                if (map.defaultTileTexture != null)
                {
                    tileTexture = map.defaultTileTexture;
                }
                else if (OnlineMapsRasterTile.emptyColorTexture != null)
                {
                    tileTexture = OnlineMapsRasterTile.emptyColorTexture;
                }
                else
                {
                    tileTexture      = OnlineMapsRasterTile.emptyColorTexture = new Texture2D(1, 1, TextureFormat.ARGB32, mipmapForTiles);
                    tileTexture.name = "Empty Texture";
                    OnlineMapsRasterTile.emptyColorTexture.SetPixel(0, 0, map.emptyColor);
                    OnlineMapsRasterTile.emptyColorTexture.Apply(false);
                }

                sendEvent = false;
            }

            material.mainTextureOffset = offset;
            material.mainTextureScale  = new Vector2(scale, scale);

            if (material.mainTexture != tileTexture)
            {
                material.mainTexture = tileTexture;
                if (sendEvent && OnChangeMaterialTexture != null)
                {
                    OnChangeMaterialTexture(currentTile, material);
                }
            }

            if (hasTraffic)
            {
                material.SetTexture("_TrafficTex", (currentTile as OnlineMapsRasterTile).trafficTexture);
                material.SetTextureOffset("_TrafficTex", material.mainTextureOffset);
                material.SetTextureScale("_TrafficTex", material.mainTextureScale);
            }
            if (hasOverlayBack)
            {
                material.SetTexture("_OverlayBackTex", currentTile.overlayBackTexture);
                material.SetTextureOffset("_OverlayBackTex", material.mainTextureOffset);
                material.SetTextureScale("_OverlayBackTex", material.mainTextureScale);
            }
            if (hasOverlayBackAlpha)
            {
                material.SetFloat("_OverlayBackAlpha", currentTile.overlayBackAlpha);
            }
            if (hasOverlayFront)
            {
                if (drawingMode == OnlineMapsTilesetDrawingMode.overlay)
                {
                    if (currentTile.status == OnlineMapsTileStatus.loaded && (currentTile.drawingChanged || currentTile.overlayFrontTexture == null))
                    {
                        if (overlayFrontBuffer == null)
                        {
                            overlayFrontBuffer = new Color32[OnlineMapsUtils.sqrTileSize];
                        }
                        else
                        {
                            for (int k = 0; k < OnlineMapsUtils.sqrTileSize; k++)
                            {
                                overlayFrontBuffer[k] = new Color32();
                            }
                        }
                        foreach (OnlineMapsDrawingElement drawingElement in OnlineMapsDrawingElementManager.instance)
                        {
                            drawingElement.Draw(overlayFrontBuffer, new OnlineMapsVector2i(currentTile.x, currentTile.y), OnlineMapsUtils.tileSize, OnlineMapsUtils.tileSize, currentTile.zoom, true);
                        }
                        if (currentTile.overlayFrontTexture == null)
                        {
                            currentTile.overlayFrontTexture          = new Texture2D(OnlineMapsUtils.tileSize, OnlineMapsUtils.tileSize, TextureFormat.ARGB32, mipmapForTiles);
                            currentTile.overlayFrontTexture.wrapMode = TextureWrapMode.Clamp;
                        }
                        currentTile.overlayFrontTexture.SetPixels32(overlayFrontBuffer);
                        currentTile.overlayFrontTexture.Apply(false);
                    }
                }

                material.SetTexture("_OverlayFrontTex", currentTile.overlayFrontTexture);
                material.SetTextureOffset("_OverlayFrontTex", material.mainTextureOffset);
                material.SetTextureScale("_OverlayFrontTex", material.mainTextureScale);
            }
            if (hasOverlayFrontAlpha)
            {
                material.SetFloat("_OverlayFrontAlpha", currentTile.overlayFrontAlpha);
            }
            if (OnDrawTile != null)
            {
                OnDrawTile(currentTile, material);
            }
        }
        else
        {
            if (OnlineMapsRasterTile.emptyColorTexture == null)
            {
                OnlineMapsRasterTile.emptyColorTexture      = new Texture2D(1, 1, TextureFormat.ARGB32, mipmapForTiles);
                OnlineMapsRasterTile.emptyColorTexture.name = "Empty Texture";
                OnlineMapsRasterTile.emptyColorTexture.SetPixel(0, 0, map.emptyColor);
                OnlineMapsRasterTile.emptyColorTexture.Apply(false);
            }

            material.mainTexture = OnlineMapsRasterTile.emptyColorTexture;
            if (hasTraffic)
            {
                material.SetTexture("_TrafficTex", null);
            }
            if (hasOverlayBack)
            {
                material.SetTexture("_OverlayBackTex", null);
            }
            if (hasOverlayFront)
            {
                material.SetTexture("_OverlayFrontTex", null);
            }
        }
    }