public void InitRenderSystem() { //Get realworld camera image DualCameraL.clearFlags = CameraClearFlags.SolidColor; DualCameraL.backgroundColor = Color.clear; _dualCameraLRT = DualCameraL.gameObject.AddComponent <CopyCameraImage>(); _dualCameraLRT.rtRefreshCallback = LDualCamRTRefreshCallback; DualCameraR.clearFlags = CameraClearFlags.SolidColor; DualCameraR.backgroundColor = Color.clear; _dualCameraRRT = DualCameraR.gameObject.AddComponent <CopyCameraImage>(); _dualCameraRRT.rtRefreshCallback = RDualCamRTRefreshCallback; //Create None DepthTest Camera, which is clear the depth buffer ,and render mesh on screen top. _createRenderOnTopCamera(); #if ADVANCE_RENDER advanceRender.InitRenderSystem(_dualCameraLRT, _dualCameraRRT); #endif foreach (Transform t in unityrenderWithDepthObject) { MyHelpLayer.SetSceneLayer(t, ARRender.UnityRenderWithDepthLayer); } foreach (Transform t in unityrenderWithDepthNoShadowObject) { MyHelpLayer.SetSceneLayer(t, ARRender.UnityRenderWithDepthNoShadowLayer); } }
void RDualCamRTRefreshCallback(CopyCameraImage copyCameraImage, RenderTexture OnRenderImageDest) { //Update the real world camera image to shader GameManager.Instance.UpdateRDualCamRT(copyCameraImage.GetRT()); #if !ADVANCE_RENDER Graphics.Blit(copyCameraImage.GetRT(), OnRenderImageDest); #endif }
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; }