public Texture2D SnapshotGameView()
    {
        Util.FindIfNotSet(this, ref userMain);
        Util.FindIfNotSet(this, ref post);
        RenderTexture currentRT = RenderTexture.active;

        RenderTexture.active = gameViewTexture;

        targetCamera.CopyFrom(userMain.GetCamera());
        targetCamera.targetTexture = gameViewTexture;
        // IMPORTANT: For some reason, Unity forward path, with Camera.Render(),
        // does not like alphatest surface shaders. The end effect is our terrain
        // does not show up in thumbnails for Low Quality. Could not figure out
        // why...but whatever, for this 1 render let's just do deferred.
        targetCamera.renderingPath = RenderingPath.DeferredShading;
        targetCamera.rect          = new Rect(0, 0, 1, 1);
        bool blurEnabled = SetMotionBlurEnabled(false);

        targetCamera.Render();
        SetMotionBlurEnabled(blurEnabled);

        Texture2D screenshotTexture = new Texture2D(gameViewTexture.width, gameViewTexture.height);

        screenshotTexture.ReadPixels(new Rect(0, 0, gameViewTexture.width, gameViewTexture.height), 0, 0);
        screenshotTexture.Apply();
        RenderTexture.active = currentRT;
        return(screenshotTexture);
    }
    // Converts a point in Unity's SCREEN COORDINATE system to our
    // GAME UI COORD system (see comment at the top of file).
    public Vector2 UnityScreenPointToGameUiPoint(Vector3 screenPoint)
    {
        Vector2 surfPoint;

        if (userMain != null)
        {
            Vector3 normalizedVpPoint = userMain.GetCamera().ScreenToViewportPoint(screenPoint);
            // normalizedVpPoint has (0,0) at bottom-left, (1,1) at top-right.
            Rect pixelRect = userMain.GetCamera().pixelRect;
            surfPoint = new Vector2(
                // Drawing2D expects X coordinates to be screen coordinates, 0 being the left edge.
                pixelRect.x + normalizedVpPoint.x * pixelRect.width,
                // Drawing2D expects Y coordinates to be relative to the top of the viewport, not screen (strangely!)
                (1 - normalizedVpPoint.y) * pixelRect.height);
        }
        else
        {
            // No userMain yet. Temporarily use full screen as viewport.
            surfPoint = new Vector2(screenPoint.x, Screen.height - screenPoint.y);
        }
        return(SurfacePointToGameUiPoint(surfPoint));
    }
    IEnumerator SetupRoutine()
    {
        UserMain userMainCheck = null;

        while (userMainCheck == null)
        {
            userMainCheck = FindObjectOfType <UserMain>();
            yield return(null);
        }
        Util.FindIfNotSet(this, ref voosEngine);
        Util.FindIfNotSet(this, ref userMain);
        Util.FindIfNotSet(this, ref behaviorSystem);
        mainCamera = userMain.GetCamera();

        // Removing all this until we need it, since logging is complicated
        // enough.
        // behaviorSystem.onLogEvent += OnBehaviorLogEvent;

        setupComplete = true;
    }
Exemple #4
0
    void PrepareInputForScript(ControllerInput input)
    {
        if (input == null)
        {
            return;
        }

        if (userMain == null)
        {
            // Not ready yet.
            return;
        }

        inputStateForScript = new InputStateForScript();

        if (input.GetKeyDown(PlayerBody.VKEY_JUMP))
        {
            // V1 name:
            voosActor.EnqueueMessage("JumpTriggered");
            // V2 name:
            voosActor.EnqueueMessage("Jump");
        }

        if (input.GetKeyDown(PlayerBody.VKEY_PRIMARY_ACTION))
        {
            // V1 name:
            voosActor.EnqueueMessage("Action1Triggered");
            // V2 name:
            voosActor.EnqueueMessage("PrimaryAction");
        }

        if (input.GetKeyDown(PlayerBody.VKEY_SECONDARY_ACTION))
        {
            // V1 name:
            voosActor.EnqueueMessage("Action2Triggered");
            // V2 name:
            voosActor.EnqueueMessage("SecondaryAction");
        }

        List <string> keysHeld         = new List <string>();
        List <string> keysJustPressed  = new List <string>();
        List <string> keysJustReleased = new List <string>();

        foreach (string key in ScriptableKeyNames)
        {
            bool isDown = input.GetKeyDown(key) && !keysHeldAsReportedToScript.Contains(key);
            bool isUp   = !input.GetKeyHeld(key) && keysHeldAsReportedToScript.Contains(key);
            bool isHeld = input.GetKeyHeld(key);

            if (isHeld)
            {
                keysHeldAsReportedToScript.Add(key);
            }
            else
            {
                keysHeldAsReportedToScript.Remove(key);
            }

            if (isDown)
            {
                voosActor.EnqueueMessage("KeyDown", $"{{\"keyName\": \"{key}\"}}");
                keysJustPressed.Add(key);
            }
            if (isHeld)
            {
                voosActor.EnqueueMessage("KeyHeld", $"{{\"keyName\": \"{key}\"}}");
                keysHeld.Add(key);
            }
            if (isUp)
            {
                voosActor.EnqueueMessage("KeyUp", $"{{\"keyName\": \"{key}\"}}");
                keysJustReleased.Add(key);
            }
        }

        bool mouseIsDown = Input.GetMouseButton(0) && !userMain.CursorOverUI() && !userMain.InEditMode();

        if (mouseIsDown && !reportedMouseDown)
        {
            voosActor.EnqueueMessage("MouseDown");
            reportedMouseDown = true;
            inputStateForScript.mouseButtonJustPressed = true;
        }
        if (reportedMouseDown)
        {
            voosActor.EnqueueMessage("MouseHeld");
            inputStateForScript.mouseButtonIsPressed = true;
        }
        if (!mouseIsDown && reportedMouseDown)
        {
            voosActor.EnqueueMessage("MouseUp");
            reportedMouseDown = false;
            inputStateForScript.mouseButtonJustReleased = true;
        }

        Vector2 mousePosUiCoords;
        Vector3 mousePosRaw = Input.mousePosition;

        mousePosUiCoords = gameUiMain.UnityScreenPointToGameUiPoint(mousePosRaw);

        inputStateForScript.mouseX           = mousePosUiCoords.x;
        inputStateForScript.mouseY           = mousePosUiCoords.y;
        inputStateForScript.keysHeld         = keysHeld.ToArray();
        inputStateForScript.keysJustPressed  = keysJustPressed.ToArray();
        inputStateForScript.keysJustReleased = keysJustReleased.ToArray();

        Ray mouseRay = userMain.GetCamera().ScreenPointToRay(mousePosRaw);

        inputStateForScript.mouseRayOrigin    = mouseRay.origin;
        inputStateForScript.mouseRayDirection = mouseRay.direction;

        if (inputStateForScript.mouseButtonJustPressed)
        {
            RaycastHit hit;
            if (Physics.Raycast(mouseRay.origin, mouseRay.direction, out hit, 1000, VoosActor.LayerMaskValue, QueryTriggerInteraction.Collide))
            {
                VoosActor clickedActor = hit.collider.GetComponentInParent <VoosActor>();
                if (clickedActor != null)
                {
                    clickedActor.GetEngine().EnqueueMessage(new VoosEngine.ActorMessage
                    {
                        name        = "ActorClicked",
                        targetActor = clickedActor.GetName(),
                        argsJson    = "{}"
                    });
                }
            }
        }

        inputStateForScript.mouseWheelRaw = Input.GetAxis("Mouse ScrollWheel");
        inputStateForScript.mouseWheel    = inputStateForScript.mouseWheelRaw * MOUSEWHEEL_FACTOR;
    }
 void UpdateCameraRect()
 {
     userMain.GetCamera().rect = new Rect(horizontalLeftPercentOffset, 0, 1 - horizontalLeftPercentOffset - horizontalRightPercentOffset, 1 - verticalPercentOffset);
 }