Exemple #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;
    }
Exemple #2
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;
    }
Exemple #3
0
    // manually renderportal camera
    public void Render()
    {
        CreateViewTexture();
        //allow for portal recursion
        var localToWorldMatrix = playerCam.transform.localToWorldMatrix;
        var renderPositions    = new Vector3[recursionLimit];
        var renderRotations    = new Quaternion[recursionLimit];

        Debug.Log("Portal view rendered");
        int startIndex = 0;

        portalCam.projectionMatrix = playerCam.projectionMatrix;
        for (int i = 0; i < recursionLimit; i++)
        {
            if (i > 0)
            {
                //dont render recursion if linked portal isnt visble
                if (!CameraUtility.BoundsOverlap(screenMeshFilter, linkedPortal.screenMeshFilter, portalCam))
                {
                    Debug.Log("Portal not visible");
                    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 portal screen so that camera can see through portal
        screen.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.ShadowsOnly;
        linkedPortal.screen.material.SetInt("displayMask", 0);
        //limit amount of recursions
        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("displayMask", 1);
            }
        }

        // show hidden objects at render
        screen.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.On;
    }
Exemple #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);
        }
Exemple #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;
        }
    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;
    }
Exemple #7
0
    // Manually render the camera attached to this portal
    // Called after PrePortalRender, and before PostPortalRender
    public void Render()
    {
        //HandleTravellers();

        // Skip rendering the view from this portal if player is not looking at the linked portal
        //if (!CameraUtility.VisibleFromCamera(linkedPortal.screen, playerCam))
        //{
        //    return;
        //}

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

        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;
        }

        tempLayer   = Level.layer;
        playerLayer = Level.layer;

        Material tempPlayerSkybox2 = playerCam.GetComponent <Skybox>().material;
        Material tempPortalSkybox2 = portalCam.GetComponent <Skybox>().material;

        Light tempDirectionalLight   = RenderSettings.sun;
        Light playerDirectionalLight = RenderSettings.sun;
        Light portalAlternateDirectionalLight;

        if (playerDirectionalLight == DirectionalMoonLight)
        {
            portalAlternateDirectionalLight = DirectionalSunLight;
        }
        else
        {
            portalAlternateDirectionalLight = DirectionalMoonLight;
        }

        if (Level.layer == 11)
        {
            portalAlternatingLayerMask = 12;
        }
        else
        {
            portalAlternatingLayerMask = 11;
        }


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

            HandleTravellers();

            SetNearClipPlane();
            HandleClipping();

            //if (portalTravellers != null)
            //{
            //    foreach (PortalTraveller traveller in portalTravellers)
            //    {
            //        UpdateSliceParams(traveller);
            //    }
            //}

            Transform[] children;
            Transform[] skeleChildren;

            children = Level.GetComponentsInChildren <Transform>();

            Transform[] travellerChildren;

            if ((recursionLimit - i) % 2 == 1)
            {
                portalCam.GetComponent <Skybox>().material = tempPortalSkybox2;
                foreach (Transform go in children)
                {
                    if (go.gameObject.layer != 13 && go.gameObject.tag != "DirectionalLight")
                    {
                        go.gameObject.layer = portalAlternatingLayerMask.value;
                    }
                }
                if (Skeleton != null)
                {
                    skeleChildren = Skeleton.GetComponentsInChildren <Transform>();
                    foreach (Transform sgo in skeleChildren)
                    {
                        if (sgo.gameObject.layer == 11 && sgo.gameObject.tag == "Skeleton" && sgo.gameObject.GetComponent <MeshRenderer>() != null)
                        {
                            sgo.GetComponent <MeshRenderer>().enabled = false;
                            if (sgo.gameObject.GetComponent <MeshCollider>() != null)
                            {
                                sgo.GetComponent <MeshCollider>().enabled = false;
                            }
                        }

                        if (sgo.gameObject.layer == 12 && sgo.gameObject.tag == "Skeleton" && sgo.gameObject.GetComponent <MeshRenderer>() != null)
                        {
                            sgo.GetComponent <MeshRenderer>().enabled = true;
                            if (sgo.gameObject.GetComponent <MeshCollider>() != null)
                            {
                                sgo.GetComponent <MeshCollider>().enabled = true;
                            }
                        }
                    }
                }
                foreach (PortalTraveller traveller in portalTravellers)
                {
                    travellerChildren = traveller.GetComponentsInChildren <Transform>();
                    foreach (Transform go2 in travellerChildren)
                    {
                        if (go2.gameObject.layer != 9 && go2.gameObject.tag != "DirectionalLight")
                        {
                            go2.gameObject.layer = portalAlternatingLayerMask.value;
                        }
                    }
                }

                RenderSettings.sun = portalAlternateDirectionalLight;
            }
            else
            {
                portalCam.GetComponent <Skybox>().material = tempPlayerSkybox2;
                foreach (Transform go in children)
                {
                    if (go.gameObject.layer != 13 && go.gameObject.tag != "DirectionalLight")
                    {
                        go.gameObject.layer = playerLayer.value;
                    }
                }
                foreach (PortalTraveller traveller in portalTravellers)
                {
                    travellerChildren = traveller.GetComponentsInChildren <Transform>();
                    foreach (Transform go2 in travellerChildren)
                    {
                        if (go2.gameObject.layer != 9 && go2.gameObject.tag != "DirectionalLight")
                        {
                            go2.gameObject.layer = playerLayer.value;
                        }
                    }
                }
                RenderSettings.sun = playerDirectionalLight;
            }
            foreach (PortalTraveller go in portalTravellers)
            {
                if (go.tag != "Player")
                {
                    if (go.gameObject.layer == 11)
                    {
                        go.transform.GetChild(0).GetComponent <Renderer>().material = sunMat;
                    }
                    else
                    {
                        go.transform.GetChild(0).GetComponent <Renderer>().material = moonMat;
                    }
                }
            }
            portalCam.Render();

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


            foreach (Transform go in children)
            {
                if (go.gameObject.layer != 13 && go.gameObject.tag != "DirectionalLight")
                {
                    go.gameObject.layer = tempLayer;
                }
            }
            if (Skeleton != null)
            {
                skeleChildren = Skeleton.GetComponentsInChildren <Transform>();
                foreach (Transform sgo in skeleChildren)
                {
                    if (sgo.gameObject.layer == 11 && sgo.gameObject.tag == "Skeleton" && sgo.gameObject.GetComponent <MeshRenderer>() != null)
                    {
                        sgo.GetComponent <MeshRenderer>().enabled = false;
                        if (sgo.gameObject.GetComponent <MeshCollider>() != null)
                        {
                            sgo.GetComponent <MeshCollider>().enabled = false;
                        }
                    }

                    if (sgo.gameObject.layer == 12 && sgo.gameObject.tag == "Skeleton" && sgo.gameObject.GetComponent <MeshRenderer>() != null)
                    {
                        sgo.GetComponent <MeshRenderer>().enabled = true;
                        if (sgo.gameObject.GetComponent <MeshCollider>() != null)
                        {
                            sgo.GetComponent <MeshCollider>().enabled = true;
                        }
                    }
                }
            }
            foreach (PortalTraveller traveller in portalTravellers)
            {
                travellerChildren = traveller.GetComponentsInChildren <Transform>();
                foreach (Transform go2 in travellerChildren)
                {
                    if (go2.gameObject.layer != 9 && go2.gameObject.tag != "DirectionalLight")
                    {
                        go2.gameObject.layer = tempLayer;
                    }
                }
            }
            foreach (PortalTraveller go in portalTravellers)
            {
                if (go.tag != "Player")
                {
                    if (go.gameObject.layer == 11)
                    {
                        go.transform.GetChild(0).GetComponent <Renderer>().material = sunMat;
                    }
                    else
                    {
                        go.transform.GetChild(0).GetComponent <Renderer>().material = moonMat;
                    }
                }
            }
            RenderSettings.sun = tempDirectionalLight;
            //portalCam.Render();
        }


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