private void OnPreRender()
    {
        Camera cam = GetComponent <Camera>();

#if USE_CopyCameraDepth
        if (copyCameraDepth != null)
        {
            depthMat.mainTexture = copyCameraDepth.DepthRT;
        }
#else
#if USE_MERGE_DEPTH
        depthMat.mainTexture = mergeDepth.GetRT(cam.stereoActiveEye);
#else
        depthMat.mainTexture = cameraDepth;
#endif
#endif


        //int screenW = cam.pixelWidth;// Screen.width;
        //int screenH = cam.pixelHeight;// Screen.height;
        float aspectRatio = 1f / cam.aspect;//(float)screenH / (float)screenW
        transform.GetChild(0).localScale = new Vector3(1, aspectRatio, 1);
        cam.orthographicSize             = aspectRatio * 0.5f;

        depthMat.SetFloat("_MainCamNear", mainCam.nearClipPlane);
        depthMat.SetFloat("_MainCamFar", mainCam.farClipPlane);
    }
Example #2
0
    private void LateUpdate()
    {
        Camera  cam    = GetComponent <Camera>();
        Vector3 camDir = cam.transform.forward;//camMat.rotation * Vector3.forward;

        //  float aspectRatio = 1f / cam.aspect;
        // fullScreenQuad.localScale = new Vector3(1, aspectRatio, 1);

        //float currentShift = (cam.stereoActiveEye == Camera.MonoOrStereoscopicEye.Left) ? addShiftL : addShiftR;


        Matrix4x4 camMat = cam.GetStereoViewMatrix(Camera.StereoscopicEye.Left);

        camMat = camMat.inverse;
        Vector3 camPosL = camMat.GetColumn(3);

        camMat = cam.GetStereoViewMatrix(Camera.StereoscopicEye.Right);
        camMat = camMat.inverse;
        Vector3 camPosR = camMat.GetColumn(3);

        //https://answers.unity.com/questions/314049/how-to-make-a-plane-fill-the-field-of-view.html
        float pos = (cam.nearClipPlane + 0.01f);

        fullScreenQuadL.position = camPosL + camDir * pos + cam.transform.right * -addShiftR + cam.transform.up * addShiftUP;
        float h = Mathf.Tan(cam.fieldOfView * Mathf.Deg2Rad * 0.5f) * pos * 2f;

        fullScreenQuadL.localScale = new Vector3(h * cam.aspect, h, 0f);

        pos = (cam.nearClipPlane + 0.01f);
        fullScreenQuadR.position = camPosR + camDir * pos + cam.transform.right * addShiftR + cam.transform.up * addShiftUP;
        h = Mathf.Tan(cam.fieldOfView * Mathf.Deg2Rad * 0.5f) * pos * 2f;
        fullScreenQuadR.localScale = new Vector3(h * cam.aspect, h, 0f);

        //Set not culling
        //https://forum.unity.com/threads/can-i-disable-culling.43916/
        Bounds notCullingbounds = new Bounds(transform.position, new Vector3(float.MaxValue, float.MaxValue, float.MaxValue));

        fullScreenQuadL.GetComponent <MeshFilter>().mesh.bounds = fullScreenQuadR.GetComponent <MeshFilter>().mesh.bounds = notCullingbounds;

        Material depthMat = fullScreenQuadL.GetComponent <MeshRenderer>().material;

#if USE_MERGE_DEPTH
        depthMat.mainTexture = mergeDepth.GetRT(Camera.MonoOrStereoscopicEye.Left);
#else
        depthMat.mainTexture = cameraDepth;
#endif
        depthMat.SetFloat("_MainCamNear", mainCam.nearClipPlane);
        depthMat.SetFloat("_MainCamFar", mainCam.farClipPlane);
        depthMat.SetFloat("_RenderOnRight", 0);


        depthMat = fullScreenQuadR.GetComponent <MeshRenderer>().material;
#if USE_MERGE_DEPTH
        depthMat.mainTexture = mergeDepth.GetRT(Camera.MonoOrStereoscopicEye.Right);
#else
        depthMat.mainTexture = cameraDepth;
#endif
        depthMat.SetFloat("_MainCamNear", mainCam.nearClipPlane);
        depthMat.SetFloat("_MainCamFar", mainCam.farClipPlane);
        depthMat.SetFloat("_RenderOnRight", 1);

        //fullScreenQuadL.gameObject.layer = fullScreenQuadR.gameObject.layer = WriteDepthLayerL;
        //fullScreenQuadL.GetComponent<Renderer>().enabled = true;
        //fullScreenQuadR.GetComponent<Renderer>().enabled = true;
    }
    //int oldLightAmount;
    public void OnRenderImage(RenderTexture source, RenderTexture destination)
    {
        //    lightMapRT = RenderTexture.GetTemporary(mainCamera.pixelWidth, mainCamera.pixelHeight, 0, RenderTextureFormat.ARGB32);
        //    lightMapRT2 = RenderTexture.GetTemporary(mainCamera.pixelWidth, mainCamera.pixelHeight, 0, RenderTextureFormat.ARGB32);
        ClearRT(lightMapRT);
        //   ClearRT(lightMapRT2);

        /* Vector4 halfPixel;
         * halfPixel.x = -1f * 0.5f / (float)lightMapRT.width;
         * halfPixel.y = -1f * 0.5f / (float)lightMapRT.height;
         * halfPixel.z = halfPixel.w = 0;
         * DeferredLightMapMaterial.SetVector("halfPixel", halfPixel);*/

        //Point light=======================================================
#if USE_CopyCameraDepth
#if USE_Normal
        DeferredPointLightMapMaterial.SetTexture("_CameraDepthNormal", copyCameraDepthNormal.GetRT(mainCamera.stereoActiveEye));
#else
        DeferredPointLightMapMaterial.SetTexture("_CameraDepthNormal", copyCameraDepth.DepthRT);
#endif
#else
#if USE_MERGE_DEPTH
        DeferredPointLightMapMaterial.SetTexture("_CameraDepthNormal", mergeDepth.GetRT(mainCamera.stereoActiveEye));
#else
#if USE_Normal
        DeferredPointLightMapMaterial.SetTexture("_CameraDepthNormal", cameraRenderDepthNormal);
#else
        DeferredPointLightMapMaterial.SetTexture("_CameraDepthNormal", cameraRenderDepth);
#endif
#endif
#endif
        //Debug.LogError("[DeferredLightMap] there are no any depth input (copyCameraDepth, cameraRenderDepth)");


#if DEFERRED_USE_RAY
        Matrix4x4 camera2World = mainCamera.cameraToWorldMatrix;
        //DeferredLightMapMaterial.EnableKeyword("PERSPECTIVE");
        DeferredPointLightMapMaterial.SetMatrix("_ViewToWorld", camera2World);
        var lpoints = RecalculateFrustrumPoints(mainCamera);
        DeferredPointLightMapMaterial.SetVector("_FrustrumPoints", new Vector4(lpoints[2].x, lpoints[1].x, lpoints[1].y, lpoints[0].y));
        DeferredPointLightMapMaterial.SetFloat("_CameraFar", mainCamera.farClipPlane);
#else
        DeferredLightMapMaterial.SetMatrix("invVP", mainCameraDepth.GetInvVP());
#endif

        //https://www.alanzucconi.com/2016/01/27/arrays-shaders-heatmaps-in-unity3d/
        //https://stackoverflow.com/questions/45098671/how-to-define-an-array-of-floats-in-shader-properties
        int currentPack = 0;
        List <Light> .Enumerator pointItr = pointLightList.GetEnumerator();
        while (pointItr.MoveNext())
        {
            if (!UseDeferredLight)
            {
                pointItr.Current.enabled = true;
                continue;
            }
            if (TurnOffUnityLight)
            {
                pointItr.Current.enabled = false;
            }
            //else
            //    pointItr.Current.enabled = true;

            int id = currentPack % PLightPerPass;
            PointLightColorList[id].x              = pointItr.Current.color.r;
            PointLightColorList[id].y              = pointItr.Current.color.g;
            PointLightColorList[id].z              = pointItr.Current.color.b;
            PointLightColorList[id].w              = 1;
            PointLightPositionList[id].x           = pointItr.Current.transform.position.x;
            PointLightPositionList[id].y           = pointItr.Current.transform.position.y;
            PointLightPositionList[id].z           = pointItr.Current.transform.position.z;
            PointLightDecayRangeStrengthList[id].x = pointItr.Current.intensity;
            PointLightDecayRangeStrengthList[id].y = pointItr.Current.range;
            PointLightDecayRangeStrengthList[id].z = 0;

            if (id == PLightPerPass - 1 || currentPack == pointLightList.Count - 1)
            {
                //drawing to map
                int PointLightAmount = id + 1;
                DeferredPointLightMapMaterial.SetInt("PointLightAmount", PointLightAmount);
                DeferredPointLightMapMaterial.SetVectorArray("PointLightColor", PointLightColorList);
                DeferredPointLightMapMaterial.SetVectorArray("PointLightPosition", PointLightPositionList);
                DeferredPointLightMapMaterial.SetVectorArray("PointLightDecayRangeStrength", PointLightDecayRangeStrengthList);

                //GL.Clear(false, false, Color.black);
                DeferredPointLightMapMaterial.SetTexture("_MainLightMap", lightMapRT);
                Graphics.Blit(null, lightMapRT2, DeferredPointLightMapMaterial);

                RenderTexture tmpRT = lightMapRT;
                lightMapRT  = lightMapRT2;
                lightMapRT2 = tmpRT;

                //if (oldLightAmount != pointLightList.Count)
                //    Debug.LogWarning("[DeferredLightMap] render light map : light amount : " + PointLightAmount);
            }

            currentPack++;
        }

        //if (oldLightAmount != pointLightList.Count)
        //{
        //    oldLightAmount = pointLightList.Count;
        //    Debug.LogWarning("[DeferredLightMap] render light map : done");
        //}

        //Spot light=======================================================
#if USE_CopyCameraDepth
#if USE_Normal
        DeferredSpotLightMapMaterial.SetTexture("_CameraDepthNormal", copyCameraDepthNormal.GetRT(mainCamera.stereoActiveEye));
#else
        DeferredSpotLightMapMaterial.SetTexture("_CameraDepthNormal", copyCameraDepth.DepthRT);
#endif
#else
#if USE_MERGE_DEPTH
        DeferredSpotLightMapMaterial.SetTexture("_CameraDepth", mergeDepth.GetRT(mainCamera.stereoActiveEye));
#else
#if USE_Normal
        DeferredSpotLightMapMaterial.SetTexture("_CameraDepthNormal", cameraRenderDepthNormal);
#else
        DeferredSpotLightMapMaterial.SetTexture("_CameraDepthNormal", cameraRenderDepth);
#endif
#endif
#endif
        //Debug.LogError("[DeferredLightMap] there are no any depth input (copyCameraDepth, cameraRenderDepth)");

        DeferredSpotLightMapMaterial.SetMatrix("_ViewToWorld", camera2World);
        DeferredSpotLightMapMaterial.SetVector("_FrustrumPoints", new Vector4(lpoints[2].x, lpoints[1].x, lpoints[1].y, lpoints[0].y));
        DeferredSpotLightMapMaterial.SetFloat("_CameraFar", mainCamera.farClipPlane);

        currentPack = 0;
        List <Light> .Enumerator spotItr = spotLightList.GetEnumerator();
        while (spotItr.MoveNext())
        {
            if (!UseDeferredLight)
            {
                spotItr.Current.enabled = true;
                continue;
            }
            if (TurnOffUnityLight)
            {
                spotItr.Current.enabled = false;
            }
            //else
            //    spotItr.Current.enabled = true;

            int id = currentPack % SLightPerPass;

            SLightColorList[id].x     = spotItr.Current.color.r;
            SLightColorList[id].y     = spotItr.Current.color.g;
            SLightColorList[id].z     = spotItr.Current.color.b;
            SLightColorList[id].w     = 1;
            SLightPositionList[id].x  = spotItr.Current.transform.position.x;
            SLightPositionList[id].y  = spotItr.Current.transform.position.y;
            SLightPositionList[id].z  = spotItr.Current.transform.position.z;
            SLightDirectionList[id].x = spotItr.Current.transform.forward.x;
            SLightDirectionList[id].y = spotItr.Current.transform.forward.y;
            SLightDirectionList[id].z = spotItr.Current.transform.forward.z;
            //SLightThetaPhiStrengthRangeList[id].x = anglePhi * 0.5f;
            SLightThetaPhiStrengthRangeList[id].y = spotItr.Current.spotAngle;
            SLightThetaPhiStrengthRangeList[id].z = spotItr.Current.intensity;
            SLightThetaPhiStrengthRangeList[id].w = spotItr.Current.range;

            if (id == SLightPerPass - 1 || currentPack == spotLightList.Count - 1)
            {
                //drawing to map
                int spotLightAmount = id + 1;
                DeferredSpotLightMapMaterial.SetInt("SpotLightAmount", spotLightAmount);
                DeferredSpotLightMapMaterial.SetVectorArray("SpotLightColor", SLightColorList);
                DeferredSpotLightMapMaterial.SetVectorArray("SpotLightPosition", SLightPositionList);
                DeferredSpotLightMapMaterial.SetVectorArray("SpotLightDirection", SLightDirectionList);
                DeferredSpotLightMapMaterial.SetVectorArray("SLightThetaPhiStrengthRange", SLightThetaPhiStrengthRangeList);

                DeferredSpotLightMapMaterial.SetTexture("_MainLightMap", lightMapRT);
                Graphics.Blit(null, lightMapRT2, DeferredSpotLightMapMaterial);

                RenderTexture tmpRT = lightMapRT;
                lightMapRT  = lightMapRT2;
                lightMapRT2 = tmpRT;
            }

            currentPack++;
        }

        BlitLightMapMaterial.SetTexture("_ScreenLightMap", lightMapRT);
        BlitLightMapMaterial.SetFloat("_Factor", LightMapFactor);
        Graphics.Blit(source, destination, BlitLightMapMaterial);

        //   RenderTexture.ReleaseTemporary(lightMapRT);
        //    RenderTexture.ReleaseTemporary(lightMapRT2);
    }