private void CreateCameras(Camera currentCamera, out Camera ProfileCamera, out Camera LightingCamera) { ProfileCamera = null; if (ProfilePerObject) { ProfileCameraGO = GameObject.Find("SSS profile Camera"); if (!ProfileCameraGO) { ProfileCameraGO = new GameObject("SSS profile Camera", typeof(Camera)); ProfileCameraGO.transform.parent = transform; ProfileCameraGO.transform.localPosition = Vector3.zero; ProfileCameraGO.transform.localEulerAngles = Vector3.zero; ProfileCamera = ProfileCameraGO.GetComponent <Camera>(); ProfileCamera.backgroundColor = Color.black; ProfileCamera.enabled = false; ProfileCamera.depth = -254; ProfileCamera.allowMSAA = false; } ProfileCamera = ProfileCameraGO.GetComponent <Camera>(); } // Camera for lighting LightingCamera = null; LightingCameraGO = GameObject.Find("SSS Lighting Pass Camera"); if (!LightingCameraGO) { LightingCameraGO = new GameObject("SSS Lighting Pass Camera", typeof(Camera)); LightingCameraGO.transform.parent = transform; LightingCameraGO.transform.localPosition = Vector3.zero; LightingCameraGO.transform.localEulerAngles = Vector3.zero; LightingCamera = LightingCameraGO.GetComponent <Camera>(); LightingCamera.enabled = false; LightingCamera.depth = -846; sss_convolution = LightingCameraGO.AddComponent <SSS_convolution>(); sss_convolution.BlurShader = Shader.Find("Hidden/SeparableSSS"); if (null == sss_convolution.BlurShader && null != _separableSSS) { sss_convolution.BlurShader = _separableSSS; } } LightingCamera = LightingCameraGO.GetComponent <Camera>(); LightingCamera.allowMSAA = currentCamera.allowMSAA; LightingCamera.backgroundColor = currentCamera.backgroundColor; LightingCamera.clearFlags = currentCamera.clearFlags; LightingCamera.cullingMask = currentCamera.cullingMask; }
private void OnPreRender() { if (Enabled && !ReferenceEquals(cam, null)) { Shader.DisableKeyword("SCENE_VIEW"); if (ReferenceEquals(null, LightingPassShader)) { LightingPassShader = Shader.Find("Hidden/LightingPass"); if (ReferenceEquals(null, LightingPassShader) && !ReferenceEquals(null, _lightingPass)) { LightingPassShader = _lightingPass; } } if (ReferenceEquals(null, ProfileShader)) { ProfileShader = Shader.Find("Hidden/SSS_Profile"); if (ReferenceEquals(null, ProfileShader) && !ReferenceEquals(null, _profile)) { ProfileShader = _profile; } } m_TextureSize.x = cam.pixelWidth / Downsampling; m_TextureSize.y = cam.pixelHeight / Downsampling; CreateCameras(cam, out ProfileCamera, out LightingCamera); #region Render Profile if (ProfilePerObject) { UpdateCameraModes(cam, ProfileCamera); //ProfileCamera.allowHDR = false; ////humm, removes a lot of artifacts when far away InitialpixelLights = QualitySettings.pixelLightCount; InitialShadows = QualitySettings.shadows; QualitySettings.pixelLightCount = 0; QualitySettings.shadows = ShadowQuality.Disable; Shader.EnableKeyword("SSS_PROFILES"); ProfileCamera.cullingMask = SSS_Layer; ProfileCamera.backgroundColor = Color.black; ProfileCamera.clearFlags = CameraClearFlags.SolidColor; /* * if (cam.stereoEnabled) * { //Left eye * if (cam.stereoTargetEye == StereoTargetEyeMask.Both || cam.stereoTargetEye == StereoTargetEyeMask.Left) * { * ProfileCamera.stereoTargetEye = StereoTargetEyeMask.Left; * * //ProfileCamera.transform.localPosition = InputTracking.GetLocalPosition(XRNode.LeftEye); * //ProfileCamera.transform.localRotation = InputTracking.GetLocalRotation(XRNode.LeftEye); * ProfileCamera.projectionMatrix = cam.GetStereoProjectionMatrix(Camera.StereoscopicEye.Left); * ProfileCamera.worldToCameraMatrix = cam.GetStereoViewMatrix(Camera.StereoscopicEye.Left); * GetProfileRT(ref SSS_ProfileTex, (int)m_TextureSize.x, (int)m_TextureSize.y, "SSS_ProfileTex"); * Util.RenderToTarget(ProfileCamera, SSS_ProfileTex, ProfileShader); * Shader.SetGlobalTexture("SSS_ProfileTex", SSS_ProfileTex); * } * //Right eye * if (cam.stereoTargetEye == StereoTargetEyeMask.Both || cam.stereoTargetEye == StereoTargetEyeMask.Left) * { * ProfileCamera.stereoTargetEye = StereoTargetEyeMask.Right; * //ProfileCamera.transform.localPosition = InputTracking.GetLocalPosition(XRNode.RightEye); * //ProfileCamera.transform.localRotation = InputTracking.GetLocalRotation(XRNode.RightEye); * ProfileCamera.projectionMatrix = cam.GetStereoProjectionMatrix(Camera.StereoscopicEye.Right); * ProfileCamera.projectionMatrix = cam.GetStereoProjectionMatrix(Camera.StereoscopicEye.Right); * ProfileCamera.worldToCameraMatrix = cam.GetStereoViewMatrix(Camera.StereoscopicEye.Right); * GetProfileRT(ref SSS_ProfileTexR, (int)m_TextureSize.x, (int)m_TextureSize.y, "SSS_ProfileTexR"); * Rendering.RenderToTarget(ProfileCamera, SSS_ProfileTexR, ProfileShader); * Shader.SetGlobalTexture("SSS_ProfileTexR", SSS_ProfileTexR); * } * * } * else * { */ //Mono //ProfileCamera.projectionMatrix = cam.projectionMatrix;//avoid frustum jitter from taa GetProfileRT(ref SSS_ProfileTex, (int)m_TextureSize.x, (int)m_TextureSize.y, "SSS_ProfileTex"); Util.RenderToTarget(ProfileCamera, SSS_ProfileTex, ProfileShader); Shader.SetGlobalTexture("SSS_ProfileTex", SSS_ProfileTex); //} QualitySettings.pixelLightCount = InitialpixelLights; QualitySettings.shadows = InitialShadows; } else { Shader.DisableKeyword("SSS_PROFILES"); SafeDestroy(SSS_ProfileTex); //SafeDestroy(SSS_ProfileTexR); } #endregion #region Render Lighting UpdateCameraModes(cam, LightingCamera); LightingCamera.allowHDR = cam.allowHDR; // if (SurfaceScattering) { if (ReferenceEquals(null, sss_convolution)) { sss_convolution = LightingCameraGO.GetComponent <SSS_convolution>(); } if (sss_convolution && sss_convolution._BlurMaterial) { sss_convolution._BlurMaterial.SetFloat("DepthTest", Mathf.Max(.00001f, DepthTest / 20)); maxDistance = Mathf.Max(0, maxDistance); sss_convolution._BlurMaterial.SetFloat("maxDistance", maxDistance); sss_convolution._BlurMaterial.SetFloat("NormalTest", Mathf.Max(.001f, NormalTest)); sss_convolution._BlurMaterial.SetFloat("EdgeOffset", EdgeOffset); sss_convolution._BlurMaterial.SetInt("_SSS_NUM_SAMPLES", ShaderIterations + 1); sss_convolution._BlurMaterial.SetColor("sssColor", sssColor); if (Dither) { sss_convolution._BlurMaterial.EnableKeyword("RANDOMIZED_ROTATION"); sss_convolution._BlurMaterial.SetFloat("DitherScale", DitherScale); //sss_convolution._BlurMaterial.SetFloat("DitherSpeed", DitherSpeed * 10); sss_convolution._BlurMaterial.SetFloat("DitherIntensity", DitherIntensity); if (NoiseTexture) { sss_convolution._BlurMaterial.SetTexture("NoiseTexture", NoiseTexture); } else { Debug.Log("Noise texture not available"); } } else { sss_convolution._BlurMaterial.DisableKeyword("RANDOMIZED_ROTATION"); } if (UseProfileTest && ProfilePerObject) { sss_convolution._BlurMaterial.EnableKeyword("PROFILE_TEST"); } else { sss_convolution._BlurMaterial.DisableKeyword("PROFILE_TEST"); } if (DEBUG_DISTANCE) { sss_convolution._BlurMaterial.EnableKeyword("DEBUG_DISTANCE"); } else { sss_convolution._BlurMaterial.DisableKeyword("DEBUG_DISTANCE"); } if (FixPixelLeaks) { sss_convolution._BlurMaterial.EnableKeyword("OFFSET_EDGE_TEST"); } else { sss_convolution._BlurMaterial.DisableKeyword("OFFSET_EDGE_TEST"); } if (DitherEdgeTest) { sss_convolution._BlurMaterial.EnableKeyword("DITHER_EDGE_TEST"); } else { sss_convolution._BlurMaterial.DisableKeyword("DITHER_EDGE_TEST"); } } sss_convolution.iterations = ScatteringIterations; sss_convolution.BlurRadius = ScatteringRadius; LightingCamera.depthTextureMode = DepthTextureMode.DepthNormals; //LightingCamera.transform.position = cam.transform.position; //LightingCamera.transform.rotation = cam.transform.rotation; //LightingCamera.projectionMatrix = cam.projectionMatrix;//avoid frustum jitter from taa GetRT(ref LightingTex, (int)m_TextureSize.x, (int)m_TextureSize.y, "LightingTexture"); GetRT(ref LightingTexBlurred, (int)m_TextureSize.x, (int)m_TextureSize.y, "SSSLightingTextureBlurred"); sss_convolution.blurred = LightingTexBlurred; sss_convolution.rtFormat = LightingTex.format; if (!ReferenceEquals(null, LightingPassShader)) { Util.RenderToTarget(LightingCamera, LightingTex, LightingPassShader); Shader.SetGlobalTexture("LightingTexBlurred", LightingTexBlurred); Shader.SetGlobalTexture("LightingTex", LightingTex); } } #endregion } else { //LightingCamera.depthTextureMode = DepthTextureMode.None; } }