Ejemplo n.º 1
0
    void OnPostRender()
    {
        //https://github.com/Unity-Technologies/VolumetricLighting/blob/master/Assets/AreaLight/Scripts/AreaLight.cs

#if USE_SCAN_DEPTH
        //deferred
        _shadowRecieveDepthMat.SetTexture("_CameraDepthNormal", cameraDepthN);
        Matrix4x4 camera2World = renderCamera.cameraToWorldMatrix;
        //DeferredLightMapMaterial.EnableKeyword("PERSPECTIVE");
        _shadowRecieveDepthMat.SetMatrix("_ViewToWorld", camera2World);
        var lpoints = DeferredLightMap.RecalculateFrustrumPoints(renderCamera);
        _shadowRecieveDepthMat.SetVector("_FrustrumPoints", new Vector4(lpoints[2].x, lpoints[1].x, lpoints[1].y, lpoints[0].y));
        _shadowRecieveDepthMat.SetFloat("_CameraFar", renderCamera.farClipPlane);

        //shadow
        _shadowRecieveDepthMat.SetMatrix("_MyShadowVP", shadowLightCamera.GetWorld2LightMatrix());
        _shadowRecieveDepthMat.SetTexture("_MyShadowDepth", shadowLightCamera.GetDepthMap());
        _shadowRecieveDepthMat.SetFloat("_MyShadowBais", bais);
        _shadowRecieveDepthMat.SetVector("_MyShadowLightDir", shadowLight.transform.forward);
        _shadowRecieveDepthMat.SetFloat("_MyShadowFactor", ShadowFactor);

        Graphics.Blit(null, ScreenShadowMap, _shadowRecieveDepthMat);
#else
        if (_shaderReplacementCamera == null)
        {
            _shaderReplacementCamera           = new GameObject("RecieveShadowCameraClone");
            _shaderReplacementCamera.hideFlags = HideFlags.HideAndDontSave;
            Camera c = _shaderReplacementCamera.AddComponent <Camera>();
            c.enabled = false;
        }

        Camera cam = _shaderReplacementCamera.GetComponent <Camera>();
        cam.CopyFrom(renderCamera);
        //cam.CopyFrom(shadowLightCamera.GetComponent<Camera>());

        //   cam.aspect = lightcamera.aspect;
        cam.backgroundColor     = Color.white;//new Color(0, 0, 0, 0);
        cam.clearFlags          = CameraClearFlags.SolidColor;
        cam.renderingPath       = RenderingPath.Forward;
        cam.useOcclusionCulling = false;
        cam.targetTexture       = ScreenShadowMap;
        cam.depthTextureMode    = DepthTextureMode.None;

        cam.depth       = 100;
        cam.cullingMask = recieveShadowLayer;//-1;//everythingCullingMask

        {
            Shader.SetGlobalMatrix("_MyShadowVP", shadowLightCamera.GetWorld2LightMatrix());
            Shader.SetGlobalTexture("_MyShadowDepth", shadowLightCamera.GetDepthMap());
            Shader.SetGlobalFloat("_MyShadowBais", bais);
            Shader.SetGlobalVector("_MyShadowLightDir", shadowLight.transform.forward);
            Shader.SetGlobalFloat("_MyShadowFactor", ShadowFactor);
            cam.stereoTargetEye = StereoTargetEyeMask.None;
            cam.RenderWithShader(_ShadowRecieveShader, "");
        }
#endif

        if (BlurFactor > 0)
        {
#if USE_BLURHV
            blurHMaterial.SetFloat("_Factor", BlurFactor);
            blurVMaterial.SetFloat("_Factor", BlurFactor);
            Vector4 texelSize = new Vector4(ScreenShadowMap.texelSize.x, ScreenShadowMap.texelSize.y, 0, 0);
            blurHMaterial.SetVector("_MainTex_TexelSize", texelSize);
            blurVMaterial.SetVector("_MainTex_TexelSize", texelSize);

            Graphics.Blit(ScreenShadowMap, ScreenShadowMapBlur, blurHMaterial);
            Graphics.Blit(ScreenShadowMapBlur, ScreenShadowMap, blurVMaterial);
#else
            blurMaterial.SetFloat("_Factor", BlurFactor);

            //http://www.shaderslab.com/demo-39---blur-effect-with-grab-pass.html
            //blur shadow map
            Graphics.Blit(ScreenShadowMap, ScreenShadowMapBlur, blurMaterial);
            RenderTexture tmp = ScreenShadowMap;
            ScreenShadowMap     = ScreenShadowMapBlur;
            ScreenShadowMapBlur = tmp;
#endif
        }

        if (debugRender != null)
        {
            debugRender.material.mainTexture = ScreenShadowMap;
        }
    }
        public void InitRenderSystem(CopyCameraImage _dualCameraLRT, CopyCameraImage _dualCameraRRT)
        {
            //Set reconstruct mesh to ScanLiveMeshLayer if exist
            Material reconMeshMat = new Material(Shader.Find("Unlit/Color"));

            reconMeshMat.color = Color.white * 0.5f;
            MeshRenderer[] reconMeshes = SRWorkControl.Instance.GetReconstructStaticMeshes();
            if (reconMeshes != null)
            {
                foreach (MeshRenderer r in reconMeshes)
                {
                    r.gameObject.layer = ScanLiveMeshLayer;
                    r.gameObject.GetComponent <MeshRenderer>().sharedMaterial = reconMeshMat;
                    r.GetComponent <MeshFilter>().mesh.RecalculateNormals();//must RecalculateNormals since original normal is wrong
                }
            }

            //-------------------------------------------------------------------------------------------------
            //Add culling processor
            //-------------------------------------------------------------------------------------------------
            _renderLivemeshDepthN               = ARRender.Instance.VRCamera.gameObject.AddComponent <CameraRenderRT>();
            _renderLivemeshDepthN.RTType        = CameraRenderRT.RTTYPE.FLOAT4;
            _renderLivemeshDepthN.RenderShader  = ScanLiveMeshShaderDepthNormal;
            _renderLivemeshDepthN.cullMask      = 1 << ScanLiveMeshLayer;
            _renderLivemeshDepthN.clearColor    = Color.white;
            _renderLivemeshDepthN.blurHMaterial = new Material(blurHMaterial);
            _renderLivemeshDepthN.blurVMaterial = new Material(blurVMaterial);
            _renderLivemeshDepthN.BlurFactor    = 3f;// 2.7f;//0 turn off blur
            _renderLivemeshDepthN.Init();

            //VRCamera.cullingMask = VRCamera.cullingMask & ~(1 << ScanLiveMeshLayer);//Not render 'LiveMesh' in vr camera
            ARRender.Instance.VRCamera.cullingMask     = MyHelpLayer.RemoveMaskLayer(ARRender.Instance.VRCamera.cullingMask, ScanLiveMeshLayer);
            ARRender.Instance.VRCamera.clearFlags      = CameraClearFlags.Color;
            ARRender.Instance.VRCamera.backgroundColor = new Color(0, 0, 0, 0);

            //Add dilation for later pass 'DownSample' not sample outter edge's black color.
            Dilation dilation = ARRender.Instance.VRCamera.gameObject.AddComponent <Dilation>();

            dilation.dilateCount         = 2;
            dilation.dilateMaskShader    = dilateMaskShader;
            dilation.genBinaryMaskShader = genBinaryMaskShader;
            dilation.refineGBufferShader = refineGBufferShader;

            //Add down sample for matching the low resolution see through camera.
            RTDownSample downsample = _createCameraFilter <RTDownSample>() as RTDownSample;

            downsample.DownScale = 3;

            //Render block pattern for simulate noise for matching low resolution see through camera.
            CamFilterDrawBlocks drawblocks = _createCameraFilter <CamFilterDrawBlocks>() as CamFilterDrawBlocks;

            drawblocks.Fade = 0.003f;

            CopyCameraDepthColor vrdepth = ARRender.Instance.VRCamera.gameObject.AddComponent <CopyCameraDepthColor>();

            vrdepth.CopyCameraDepthColorMaterial = CopyCameraDepthColorMaterial;

#if USE_MERGE_DEPTH
            MergeDepth mergeDepth = ARRender.Instance.VRCamera.gameObject.AddComponent <MergeDepth>();
            mergeDepth.cameraRenderRT       = _renderLivemeshDepthN.RT;
            mergeDepth.mergeDepthMat        = MergeDepthMat;
            mergeDepth.copyCameraColorDepth = vrdepth;
#endif

            SoftEdgeWeight softEdgeWeight = ARRender.Instance.VRCamera.gameObject.AddComponent <SoftEdgeWeight>();
            softEdgeWeight.renderDepth            = _renderLivemeshDepthN;
            softEdgeWeight.SoftEdgeWeightMaterial = softEdgeWeightMaterial;
            softEdgeWeight.factor = 2f;

            RenderWithARDepth renderWithARDepth = ARRender.Instance.VRCamera.gameObject.AddComponent <RenderWithARDepth>();
            renderWithARDepth.RenderWithARDepthMaterial = RenderWithARDepthMaterial;
            renderWithARDepth.MRDepthNormal             = _renderLivemeshDepthN.RT;
            renderWithARDepth.VRCamera       = vrdepth;
            renderWithARDepth.softCullLength = 0.0001f;
            renderWithARDepth.glowAmount     = 0.018f;
            renderWithARDepth.coefAmount     = 0.062f;
            renderWithARDepth.seeThroughL    = _dualCameraLRT;
            renderWithARDepth.seeThroughR    = _dualCameraRRT;
            renderWithARDepth.softEdgeWeight = softEdgeWeight;
            renderWithARDepth.CullingBaise   = 0.000017f;//1.7e-05

            //-------------------------------------------------------------------------------------------------
            //Add shadow processor
            //-------------------------------------------------------------------------------------------------
            ARRender.Instance.shadowCastDirLight.transform.position = ARRender.Instance.VRCamera.transform.position;

            ShadowRecieve shadowRecieve = ARRender.Instance.VRCamera.gameObject.AddComponent <ShadowRecieve>();
            shadowRecieve.ScreenMapScale   = 0.5f;
            shadowRecieve.shadowLight      = ARRender.Instance.shadowCastDirLight;
            shadowRecieve.screenShadowBlit = screenShadowBlit;
            // shadowRecieve.blurMaterial = shadowBlur;
            shadowRecieve.blurHMaterial = new Material(blurHMaterial);
            shadowRecieve.blurVMaterial = new Material(blurVMaterial);
            shadowRecieve.BlurFactor    = 3;
            shadowRecieve.ShadowFactor  = 10;
            shadowRecieve.VRDepthColor  = vrdepth;//set VR scene depth for screen shadow culling (no need recieve mesh's depth)

            shadowRecieve.cameraDepthN = _renderLivemeshDepthN.RT;
            //shadowRecieve.recieveShadowLayer = (1 << ScanLiveMeshLayer);

            GameObject shadowCastCameraObj = Instantiate(shadowCastCameraPrefab);
            shadowCastCameraObj.transform.parent = ARRender.Instance.shadowCastDirLight.transform;
            ShadowCastCamera shadowCastCamera = shadowCastCameraObj.GetComponent <ShadowCastCamera>();
            shadowCastCamera.GetComponent <Camera>().orthographicSize = 4; //4 is enough
            shadowCastCamera.shadowMapSize = 400;

            //cast shadow for default layer
            shadowCastCamera.shadowRenderMask = shadowCastMask;

            //shadowRecieve.shadowColor = Color.white * 50f / 255f;
            shadowRecieve.bais           = 0;
            shadowRecieve.shadowBlitBais = 0;

            //-------------------------------------------------------------------------------------------------
            //Add point light processor
            //-------------------------------------------------------------------------------------------------
            deferredLightMap = ARRender.Instance.VRCamera.gameObject.AddComponent <DeferredLightMap>();
            deferredLightMap.DeferredPointLightMapMaterial = DeferredPointLightMapMaterial;
            deferredLightMap.DeferredSpotLightMapMaterial  = DeferredSpotLightMapMaterial;
            deferredLightMap.BlitLightMapMaterial          = BlitLightMapMaterial;
            deferredLightMap.LightMapFactor = 0.2f;

            //deferredLightMap.mergeDepth = mergeDepth;
            deferredLightMap.cameraRenderDepthNormal = _renderLivemeshDepthN.RT; //not use merge depth, since game objects use unity lighting.

            deferredLightMap.TurnOffUnityLight = false;                          //Dont' turn off unity light, because current time my deferred lighting is not concern normal, and I need unity lighting to render object.


            //-------------------------------------------------------------------------------------------------
            //write Depth Camera and render alpha object
            //-------------------------------------------------------------------------------------------------
            GameObject writeDepthCameraRoot = new GameObject(ARRender.Instance.VRCamera.name + "_writeDepth +" + _writeDepthCamLOrder);
            writeDepthCameraRoot.transform.parent = null;
            Vive.Plugin.SR.ViveSR_HMDCameraShifter shifter = writeDepthCameraRoot.AddComponent <Vive.Plugin.SR.ViveSR_HMDCameraShifter>();
            writeDepthCameraRoot.transform.position = Vector3.zero;
            writeDepthCameraRoot.transform.rotation = Quaternion.identity;

            GameObject writeDepthCameraObj = Instantiate(writeDepthCameraPrefab);
            writeDepthCameraObj.transform.parent        = writeDepthCameraRoot.transform;
            writeDepthCameraObj.transform.localPosition = Vector3.zero;
            writeDepthCameraObj.transform.localRotation = Quaternion.identity;
            _writeDepthCamera       = writeDepthCameraObj.GetComponent <Camera>();
            _writeDepthCamera.depth = ARRender.Instance.VRCamera.depth + _writeDepthCamLOrder;
            _writeDepthCamera.name  = _writeDepthCamera.name + " +" + _writeDepthCamLOrder;
            _writeDepth             = _writeDepthCamera.GetComponent <WriteDepthPerspective>();
            _writeDepth.mainCam     = ARRender.Instance.VRCamera;
            _writeDepth.addShiftR   = 0.00145f;   //0.00807f;
            _writeDepth.addShiftUP  = -0.00005f;; // -0.0001f;
            _writeDepth.Init(0);
            //_writeDepth.IsRenderRightEye = null;
            //shifter.TargetCamera = _writeDepthCamera;
            bool setTargetCam = MyReflection.SetMemberVariable(shifter, "TargetCamera", _writeDepthCamera);
            if (!setTargetCam)
            {
                Debug.LogError("shifter.TargetCamera set fail..._writeDepthCamera");
            }
#if USE_MERGE_DEPTH
            _writeDepth.mergeDepth = mergeDepth;
#else
            _writeDepth.cameraDepth = renderLiveMeshDepthN.RT;
#endif

            ARRender.Instance.shadowCastDirLight.shadowBias = 0.02f;
        }