Beispiel #1
0
    private void UpdateMesh(bool firstTime)
    {
        Thing[]         things               = universe.Things;
        ThingPosition[] thingsPositions      = universe.ThingsPositions;
        ushort[]        thingsToRender       = universe.ThingsToRender;
        ushort          thingsToRenderAmount = universe.ThingsToRenderAmount;

        int vertexCount   = thingsToRenderAmount * 4;
        int triangleCount = thingsToRenderAmount * 6;

        Vector3[] vertices = DataPools.poolVector3.GetArray(vertexCount);

        int vertexOffset = 0;

        //Update all positions
        for (int i = 0; i < thingsToRenderAmount; i++)
        {
            ushort thingIndex = thingsToRender[i];

            ThingPosition position = thingsPositions[thingIndex];

            vertices[vertexOffset + 0].x = position.x - position.radius;
            vertices[vertexOffset + 0].y = position.y - position.radius;

            vertices[vertexOffset + 1].x = position.x - position.radius;
            vertices[vertexOffset + 1].y = position.y + position.radius;

            vertices[vertexOffset + 2].x = position.x + position.radius;
            vertices[vertexOffset + 2].y = position.y + position.radius;

            vertices[vertexOffset + 3].x = position.x + position.radius;
            vertices[vertexOffset + 3].y = position.y - position.radius;

            vertexOffset += 4;
        }

        //Don't draw preview of active planets (except the first time that everything is draw)
        if (!firstTime)
        {
            for (int i = 0; i < activePlanetViews.Count; i++)
            {
                ushort thingIndex = activePlanetViews[i].Planet.ThingIndex;

                int thingsToRenderIndex = System.Array.BinarySearch(thingsToRender, 0, thingsToRenderAmount, thingIndex);

                if (thingsToRenderIndex >= 0)
                {
                    vertexOffset = thingsToRenderIndex * 4;

                    vertices[vertexOffset + 0].x = 0;
                    vertices[vertexOffset + 0].y = 0;

                    vertices[vertexOffset + 1].x = 0;
                    vertices[vertexOffset + 1].y = 0;

                    vertices[vertexOffset + 2].x = 0;
                    vertices[vertexOffset + 2].y = 0;

                    vertices[vertexOffset + 3].x = 0;
                    vertices[vertexOffset + 3].y = 0;
                }
            }
        }

        if (firstTime)
        {
            //Update triangles and uvs only the first time that the mesh is updated

            int triangleOffset = 0;
            vertexOffset = 0;

            int[]     triangles = DataPools.poolInt.GetArray(triangleCount);
            Vector2[] uvs       = DataPools.poolVector2.GetArray(vertexCount);

            for (ushort i = 0; i < thingsToRenderAmount; i++)
            {
                Thing thing = things[thingsToRender[i]];

                PlanetType planetType;

                if (thing.type == (ushort)ThingType.Sun)
                {
                    planetType = planetTypes[Mathf.Abs(thing.seed % 2) + 4]; //suns!
                }
                else
                {
                    planetType = planetTypes[Mathf.Abs(thing.seed % 4)]; //planets!
                }
                Rect planetUV = planetType.planetSprite.UV;

                uvs[vertexOffset + 0] = new Vector2(planetUV.xMin, planetUV.yMax);
                uvs[vertexOffset + 1] = new Vector2(planetUV.xMax, planetUV.yMax);
                uvs[vertexOffset + 2] = new Vector2(planetUV.xMax, planetUV.yMin);
                uvs[vertexOffset + 3] = new Vector2(planetUV.xMin, planetUV.yMin);

                triangles[triangleOffset + 0] = vertexOffset + 0;
                triangles[triangleOffset + 1] = vertexOffset + 1;
                triangles[triangleOffset + 2] = vertexOffset + 2;

                triangles[triangleOffset + 3] = vertexOffset + 2;
                triangles[triangleOffset + 4] = vertexOffset + 3;
                triangles[triangleOffset + 5] = vertexOffset + 0;

                triangleOffset += 6;
                vertexOffset   += 4;
            }

            mesh1.vertices = vertices;
            mesh2.vertices = vertices;

            mesh1.triangles = triangles;
            mesh1.uv        = uvs;
            mesh1.bounds    = new Bounds(Vector3.zero, new Vector3(ushort.MaxValue * 2, ushort.MaxValue * 2, 0.0f));

            mesh2.triangles = triangles;
            mesh2.uv        = uvs;
            mesh2.bounds    = new Bounds(Vector3.zero, new Vector3(ushort.MaxValue * 2, ushort.MaxValue * 2, 0.0f));

            mesh1.Optimize();
            mesh2.Optimize();

            DataPools.poolInt.ReturnArray(triangles);
            DataPools.poolVector2.ReturnArray(uvs);
        }

        if ((frameCount % 2) == 0)
        {
            mesh1.vertices        = vertices;
            meshFilter.sharedMesh = mesh1;
        }
        else
        {
            mesh2.vertices        = vertices;
            meshFilter.sharedMesh = mesh2;
        }

        DataPools.poolVector3.ReturnArray(vertices);

        frameCount++;
    }
Beispiel #2
0
    private void UpdateArrows()
    {
        if (GameLogic.Instace.State != GameLogicState.PlayingShip)
        {
            HideAllArrows();
            return;
        }

        if (!UniverseViewCamera.Instance.FollowingObject)
        {
            HideAllArrows();
            return;
        }

        UniverseObjectView followingObjectView = UniverseViewCamera.Instance.FollowingObject.GetComponent <UniverseObjectView>();

        if (!followingObjectView)
        {
            HideAllArrows();
            return;
        }

        closeThings = universeView.Universe.FindClosestRenderedThings(followingObjectView.UniverseObject.Position, searchRadius, closeThings);

        float arrowDistance = new Vector2(Screen.width, Screen.height).magnitude / 6.0f;

        int newActiveArrows = 0;

        for (int i = 0; i < closeThings.Count && newActiveArrows < arrows.Length; i++)
        {
            Thing         thing         = universeView.Universe.GetThing(closeThings[i]);
            ThingPosition thingPosition = universeView.Universe.GetThingPosition(closeThings[i]);

            Vector2 thingPositionV2 = new Vector2(thingPosition.x, thingPosition.y);

            Vector2 delta = thingPositionV2 - followingObjectView.UniverseObject.Position;

            Vector2 closestThingPositionV2 = thingPositionV2 - delta.normalized * thingPosition.radius * 0.75f;

            Vector3 thingPositionOnCamera = UniverseViewCamera.Instance.camera.WorldToViewportPoint(closestThingPositionV2);

            if (thingPositionOnCamera.x >= 0 && thingPositionOnCamera.x <= 1 &&
                thingPositionOnCamera.y >= 0 && thingPositionOnCamera.y <= 1)
            {
                //Thing on screen, no need to draw arrow!
                continue;
            }

            PlanetArrow arrow = arrows[newActiveArrows];

            arrow.UpdatePlanet(thing);

            arrow.trans.position   = (Vector3)(delta.normalized * arrowDistance);
            arrow.trans.rotation   = Quaternion.AngleAxis(Mathf.Atan2(delta.y, delta.x) * Mathf.Rad2Deg, Vector3.forward);
            arrow.trans.localScale = Vector3.Lerp(Vector3.one * 0.5f, Vector3.one * 2, 1.0f - delta.magnitude / searchRadius);

            arrow.Show();

            newActiveArrows++;

            //Debug.Log("Arrow " + i + " -> " + closeThings[i]);
        }

        //Deactive arrows not in use anymore
        for (int i = newActiveArrows; i < arrows.Length; i++)
        {
            arrows[i].Hide();
        }

        activeArrows = newActiveArrows;
    }