Пример #1
0
    // Manually render the camera attached to this portal
    // Called after PrePortalRender, and before PostPortalRender
    public void Render()
    {
        // Skip rendering the view from this portal if player is not looking at the linked portal
        if (!CameraUtility.VisibleFromCamera(linkedPortal.screen, playerCam))
        {
            return;
        }

        CreateViewTexture();

        var localToWorldMatrix = playerCam.transform.localToWorldMatrix;
        var renderPositions    = new Vector3[recursionLimit];
        var renderRotations    = new Quaternion[recursionLimit];

        int startIndex = 0;

        portalCam.projectionMatrix = playerCam.projectionMatrix;
        for (int i = 0; i < recursionLimit; i++)
        {
            if (i > 0)
            {
                // No need for recursive rendering if linked portal is not visible through this portal
                if (!CameraUtility.BoundsOverlap(screenMeshFilter, linkedPortal.screenMeshFilter, portalCam))
                {
                    break;
                }
            }
            localToWorldMatrix = transform.localToWorldMatrix * linkedPortal.transform.worldToLocalMatrix * localToWorldMatrix;
            int renderOrderIndex = recursionLimit - i - 1;
            renderPositions[renderOrderIndex] = localToWorldMatrix.GetColumn(3);
            renderRotations[renderOrderIndex] = localToWorldMatrix.rotation;

            portalCam.transform.SetPositionAndRotation(renderPositions[renderOrderIndex], renderRotations[renderOrderIndex]);
            startIndex = renderOrderIndex;
        }

        // Hide screen so that camera can see through portal
        screen.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.ShadowsOnly;
        linkedPortal.screen.material.SetInt("active", 0);

        for (int i = startIndex; i < recursionLimit; i++)
        {
            portalCam.transform.SetPositionAndRotation(renderPositions[i], renderRotations[i]);
            SetNearClipPlane();
            HandleClipping();
            portalCam.Render();

            if (i == startIndex)
            {
                linkedPortal.screen.material.SetInt("active", 1);
            }
        }

        // Unhide objects hidden at start of render
        screen.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.On;
    }
Пример #2
0
    public bool Judge(Camera _viewCamera, int limit)
    {
        Vector3 viewCamerPos = _viewCamera.cameraToWorldMatrix.GetColumn(3);
        float   distance     = Vector3.Distance(viewCamerPos, transform.position);

        if (!CameraUtility.VisibleFromCamera(screen, _viewCamera) || limit < 0 || distance > hideDistance || linkedPortal.enabled == false || this.enabled == false || !linkedPortal.gameObject.activeInHierarchy || !gameObject.activeInHierarchy)
        {
            return(false);
        }
        return(true);
    }
Пример #3
0
    public void Render()
    {
        if (!CameraUtility.VisibleFromCamera(other.screen, playerCam))
        {
            return;
        }

        CreateRenderTexture();

        var localToWorldMatrix = playerCam.transform.localToWorldMatrix;
        var renderPositions    = new Vector3[recursionLimit];
        var renderRotations    = new Quaternion[recursionLimit];

        int startIndex = 0;

        portalCam.projectionMatrix = playerCam.projectionMatrix;

        for (int i = 0; i < recursionLimit; i++)
        {
            if (i > 0 && !CameraUtility.BoundsOverlap(screenMeshFilter, other.screenMeshFilter, portalCam))
            {
                break;
            }

            localToWorldMatrix = transform.localToWorldMatrix * other.transform.worldToLocalMatrix * localToWorldMatrix;
            int renderOrderIndex = recursionLimit - i - 1;
            renderPositions[renderOrderIndex] = localToWorldMatrix.GetColumn(3);
            renderRotations[renderOrderIndex] = localToWorldMatrix.rotation;

            portalCam.transform.SetPositionAndRotation(renderPositions[renderOrderIndex], renderRotations[renderOrderIndex]);
            startIndex = renderOrderIndex;
        }

        // Hides Screen from rendering pass
        screen.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.ShadowsOnly;
        other.screen.material.SetInt("displayMask", 0);

        for (int i = startIndex; i < recursionLimit; i++)
        {
            portalCam.transform.SetPositionAndRotation(renderPositions[i], renderRotations[i]);
            SetNearClipPlane();
            HandleClipping();

            portalCam.Render();

            if (i == startIndex)
            {
                other.screen.material.SetInt("displayMask", 1);
            }
        }

        screen.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.On;
    }
Пример #4
0
        public bool Render(
            Camera playerCamera,
            Matrix4x4 playerProjectionMatrix, Matrix4x4 playerTransformMatrix,
            Camera portalCamera,
            Matrix4x4 portalMatrix, Bounds portalBounds,
            Matrix4x4 linkedPortalMatrix, Bounds linkedPortalBounds,
            float3 portalPosition, float3 portalForward,
            Entity portal, ref RenderMesh renderMesh, float distanceToCamera)    //, Material linkedPortalMaterial)
        {
            // Skip rendering the view from this portal if player is not looking at the linked portal
            //Debug.LogError(linkedPortalBounds);
            if (distanceToCamera > 2 && !CameraUtility.VisibleFromCamera(linkedPortalBounds, playerCamera))
            {
                return(false);
            }
            Matrix4x4 localToWorldMatrix = playerTransformMatrix;//playerCamera.transform.localToWorldMatrix;
            var       renderPositions    = new float3[recursionLimit];
            var       renderRotations    = new quaternion[recursionLimit];
            int       startIndex         = 0;

            portalCamera.projectionMatrix = playerProjectionMatrix; //playerCamera.projectionMatrix;
            Matrix4x4 linkedPortalWorldToLocalMatrix = math.inverse(linkedPortalMatrix);

            for (int i = 0; i < recursionLimit; i++)
            {
                if (i > 0)
                {
                    // No need for recursive rendering if linked portal is not visible through this portal
                    if (!CameraUtility.BoundsOverlap(portalMatrix, portalBounds, linkedPortalMatrix, linkedPortalBounds, portalCamera))
                    {
                        break;
                    }
                }
                localToWorldMatrix = portalMatrix * linkedPortalWorldToLocalMatrix * localToWorldMatrix;
                int renderOrderIndex = recursionLimit - i - 1;
                renderPositions[renderOrderIndex] = ((Vector3)localToWorldMatrix.GetColumn(3));
                if (float.IsNaN(renderPositions[renderOrderIndex].x))
                {
                    //Debug.LogError("NAN in portal system.");
                    return(false);
                }
                renderRotations[renderOrderIndex] = localToWorldMatrix.rotation;
                startIndex = renderOrderIndex;
            }
            // Hide portal mesh so that camera can see through portal
            var preMesh = renderMesh.mesh;

            renderMesh.mesh = null;
            World.EntityManager.SetSharedComponentData(portal, renderMesh);
            //linkedPortalMaterial.SetInt ("displayMask", 0);
            for (int i = startIndex; i < recursionLimit; i++)
            {
                portalCamera.transform.SetPositionAndRotation(renderPositions[i], renderRotations[i]);
                //var preFarClipPlane = playerCamera.farClipPlane;
                //playerCamera.farClipPlane = portalCamera.farClipPlane;
                //SetNearClipPlane(playerCamera, portalCamera, portalPosition, portalForward);
                SetNearClipPlane(playerCamera, portalCamera, portalPosition, portalForward);
                portalCamera.Render();
                //playerCamera.farClipPlane = preFarClipPlane;
            }
            renderMesh.mesh = preMesh;
            World.EntityManager.SetSharedComponentData(portal, renderMesh);
            return(true);
        }
Пример #5
0
        protected internal override void SpecularRender(Camera receiver)
        {
            // Manually render the camera attached to this portal
            // Called after PrePortalRender, and before PostPortalRender

            // Skip rendering the view from this portal if player is not looking at the linked portal
            if (!CameraUtility.VisibleFromCamera(renderer: this.linkedPortal._portal_renderer, camera: receiver)
                )
            {
                return;
            }

            this._portal_renderer.CreateViewTexture(this_portal: this);

            var local_to_world_matrix = receiver.transform.localToWorldMatrix;
            var render_positions      = new Vector3[this.recursionLimit];
            var render_rotations      = new Quaternion[this.recursionLimit];

            var start_index = 0;

            this._Specular_Camera.projectionMatrix = receiver.projectionMatrix;
            for (var i = 0; i < this.recursionLimit; i++)
            {
                if (i > 0)
                {
                    // No need for recursive rendering if linked portal is not visible through this portal
                    if (!CameraUtility.BoundsOverlap(near_object : this._portal_renderer.ScreenMeshFilter,
                                                     far_object : this.linkedPortal._portal_renderer.ScreenMeshFilter,
                                                     camera : this._Specular_Camera))
                    {
                        break;
                    }
                }

                local_to_world_matrix = this.transform.localToWorldMatrix
                                        * this.linkedPortal.transform.worldToLocalMatrix
                                        * local_to_world_matrix;
                var render_order_index = this.recursionLimit - i - 1;
                render_positions[render_order_index] = local_to_world_matrix.GetColumn(3);
                render_rotations[render_order_index] = local_to_world_matrix.rotation;

                this._Specular_Camera.transform.SetPositionAndRotation(position: render_positions
                                                                       [render_order_index],
                                                                       rotation: render_rotations
                                                                       [render_order_index]);
                start_index = render_order_index;
            }

            // Hide screen so that camera can see through portal
            this._portal_renderer.MeshRenderer.shadowCastingMode =
                UnityEngine.Rendering.ShadowCastingMode.ShadowsOnly;
            this.linkedPortal._portal_renderer.MeshRenderer.material.SetInt(nameID: _Active, 0);

            for (var i = start_index; i < this.recursionLimit; i++)
            {
                this._Specular_Camera.transform.SetPositionAndRotation(position: render_positions[i],
                                                                       rotation: render_rotations[i]);
                Rendering.SetNearClipPlane(this_portal: this, receiver: receiver);
                Rendering.HandleClipping(this_portal: this, receiver: receiver);
                this._Specular_Camera.Render();

                if (i == start_index)
                {
                    this.linkedPortal._portal_renderer.MeshRenderer.material.SetInt(nameID: _Active, 1);
                }
            }

            // Unhide objects hidden at start of render
            this._portal_renderer.MeshRenderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.On;
        }
Пример #6
0
    // Manually render the camera attached to this portal
    // Called after PrePortalRender, and before PostPortalRender
    public void Render(Camera _viewCamera, int limit)
    {
        if (linkedPortal == null)
        {
            Debug.LogError(gameObject.name + "未连接传送门");
        }
        // Skip rendering the view from this portal if player is not looking at the linked portal
        Vector3 viewCamerPos = _viewCamera.cameraToWorldMatrix.GetColumn(3);
        float   distance     = Vector3.Distance(viewCamerPos, transform.position);

        if (!CameraUtility.VisibleFromCamera(screen, _viewCamera) || limit < 0 || distance > hideDistance || linkedPortal.enabled == false || this.enabled == false || !linkedPortal.gameObject.activeInHierarchy || !gameObject.activeInHierarchy)
        {
            return;
        }
        viewCam = _viewCamera;

        var localToWorldMatrix = viewCam.worldToCameraMatrix;

        localToWorldMatrix = localToWorldMatrix * transform.localToWorldMatrix * linkedPortal.transform.worldToLocalMatrix;



        portalCam.worldToCameraMatrix = localToWorldMatrix;
        SetNearClipPlane();
        Matrix4x4 projection = portalCam.projectionMatrix;

        Portal[] portalArr;
        if (useRecursionPortal)
        {
            portalArr = recursionPortal;
        }
        else
        {
            portalArr = MainCamera.portals;
        }
        bool[] vis = new bool[portalArr.Length];
        for (int i = 0; i < portalArr.Length; i++)
        {
            Portal curPortal = portalArr[i];
            if (curPortal != linkedPortal && curPortal.enabled == true && curPortal != this)
            {
                if (!curPortal.Judge(portalCam, limit - 1))
                {
                    vis[i] = false;
                    continue;
                }
                else
                {
                    vis[i] = true;
                }

                curPortal.Render(portalCam, limit - 1);
                portalCam.worldToCameraMatrix = localToWorldMatrix;
                portalCam.projectionMatrix    = projection;
                viewCam = _viewCamera;
            }
        }
        for (int i = 0; i < portalArr.Length; i++)
        {
            if (!vis[i])
            {
                continue;
            }
            Portal curPortal = portalArr[i];
            curPortal.SetViewTexture();
        }

        CreateViewTexture();
        CreateViewBuffer();

        SetNearClipPlane();
        HandleClipping();
        // Hide screen so that camera can see through portal
        linkedPortal.screen.enabled = false;
        portalCam.Render();
        renderCount++;
        Graphics.Blit(viewBuffer, viewTexture.Peek());
        RenderTexture.ReleaseTemporary(viewBuffer);
        for (int i = 0; i < portalArr.Length; i++)
        {
            if (!vis[i])
            {
                continue;
            }
            Portal curPortal = portalArr[i];
            curPortal.ReleaseViewTexture();
        }
        screen.material.SetInt("displayMask", 1);
        linkedPortal.screen.enabled = true;
        CancelClipping();
    }
    public void Render()
    {
        // TODO: if player doesn't look at linked portal dont do anything

        if (!CameraUtility.VisibleFromCamera(linkedPortal.screen, playerCam))
        {
            return;
        }

        var localToWorldMatrix = playerCam.transform.localToWorldMatrix;

        RenderPositionAndRotation[] renders = new RenderPositionAndRotation[recursionLimit];

        int startIndex = 0;

        portalCam.projectionMatrix = playerCam.projectionMatrix;
        for (int i = 0; i < recursionLimit; i++)
        {
            if (i > 0)
            {
                // No need for recursive rendering if linked portal is not visible through this portal
                if (!CameraUtility.BoundsOverlap(screenMeshFilter, linkedPortal.screenMeshFilter, portalCam))
                {
                    break;
                }
            }
            localToWorldMatrix = transform.localToWorldMatrix * linkedPortal.transform.worldToLocalMatrix * localToWorldMatrix;
            int renderOrderIndex = recursionLimit - i - 1;
            renders[renderOrderIndex] = new RenderPositionAndRotation(localToWorldMatrix.GetColumn(3), localToWorldMatrix.rotation);

            portalCam.transform.SetPositionAndRotation(renders[renderOrderIndex].linked.renderPosition, renders[renderOrderIndex].linked.renderRotation);
            startIndex = renderOrderIndex;

            foreach (MinePortal newPortal in alsoVisiblePortals)
            {
                if (CameraUtility.BoundsOverlap(screenMeshFilter, newPortal.screenMeshFilter, portalCam))
                {
                    var newLocalToWorldMatrix = portalCam.transform.localToWorldMatrix;
                    newPortal.linkedPortal.portalCam.projectionMatrix = portalCam.projectionMatrix;

                    newLocalToWorldMatrix = newPortal.linkedPortal.transform.localToWorldMatrix * newPortal.transform.worldToLocalMatrix * newLocalToWorldMatrix;
                    renders[renderOrderIndex].Add(new PositionAndRotation(newLocalToWorldMatrix.GetColumn(3), newLocalToWorldMatrix.rotation, newPortal));
                }
            }
        }

        // Hide screen so that camera can see through portal
        screen.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.ShadowsOnly;
        linkedPortal.screen.material.SetInt("displayMask", 0);

        for (int i = startIndex; i < recursionLimit; i++)
        {
            foreach (PositionAndRotation par in renders[i].alsoVisible)
            {
                par.alsoPortal.linkedPortal.screen.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.ShadowsOnly;
                par.alsoPortal.screen.material.SetInt("displayMask", 0);

                par.alsoPortal.linkedPortal.portalCam.transform.SetPositionAndRotation(par.renderPosition, par.renderRotation);
                par.alsoPortal.linkedPortal.SetNearClipPlane();
                par.alsoPortal.linkedPortal.HandleClipping();
                par.alsoPortal.linkedPortal.portalCam.Render();

                par.alsoPortal.linkedPortal.screen.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.On;
                par.alsoPortal.screen.material.SetInt("displayMask", 1);
            }

            portalCam.transform.SetPositionAndRotation(renders[i].linked.renderPosition, renders[i].linked.renderRotation);
            SetNearClipPlane();
            HandleClipping();
            portalCam.Render();

            if (i == startIndex)
            {
                linkedPortal.screen.material.SetInt("displayMask", 1);
            }
        }

        // Unhide objects hidden at start of render
        screen.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.On;
    }