void CreateRenderBuffer(ScriptableRenderContext context) { int bufferWidth = Screen.width; int bufferHeight = Screen.height; #if !UNITY_EDITOR bufferWidth >>= 1; bufferHeight >>= 1; #endif CmdBuff.GetTemporaryRT(RenderBuffer1ID, bufferWidth, bufferHeight, 0, FilterMode.Bilinear, RenderTextureFormat.DefaultHDR); CmdBuff.GetTemporaryRT(RenderBuffer2ID, bufferWidth, bufferHeight, 0, FilterMode.Bilinear, RenderTextureFormat.DefaultHDR); CmdBuff.GetTemporaryRT(DepthBufferID, bufferWidth, bufferHeight, 16, FilterMode.Point, RenderTextureFormat.Depth); CmdBuff.GetTemporaryRT(ShadowBufferID, (int)shadowMapSize, (int)shadowMapSize, 16, FilterMode.Bilinear, RenderTextureFormat.Shadowmap); if (renderBuffers == null) { renderBuffers = new RenderTargetIdentifier[4]; } renderBuffers[0] = new RenderTargetIdentifier(RenderBuffer1ID); renderBuffers[1] = new RenderTargetIdentifier(RenderBuffer2ID); renderBuffers[2] = new RenderTargetIdentifier(DepthBufferID); renderBuffers[3] = new RenderTargetIdentifier(ShadowBufferID); context.ExecuteCommandBuffer(CmdBuff); CmdBuff.Clear(); }
void SetCamera(ScriptableRenderContext context, Camera camera) { CmdBuff.SetGlobalVector(WorldSpaceCameraPosID, camera.transform.position); context.ExecuteCommandBuffer(CmdBuff); CmdBuff.Clear(); context.SetupCameraProperties(camera); }
void ReleaseRenderBuffer(ScriptableRenderContext context) { CmdBuff.ReleaseTemporaryRT(RenderBuffer1ID); CmdBuff.ReleaseTemporaryRT(RenderBuffer2ID); CmdBuff.ReleaseTemporaryRT(DepthBufferID); CmdBuff.ReleaseTemporaryRT(ShadowBufferID); context.ExecuteCommandBuffer(CmdBuff); CmdBuff.Clear(); }
void ClearRenderBuffer(ScriptableRenderContext context, Camera camera) { bool isClearColor, isClearDepth; GetClearFlags(camera, out isClearColor, out isClearDepth); CmdBuff.ClearRenderTarget(isClearDepth, isClearColor, camera.backgroundColor); context.ExecuteCommandBuffer(CmdBuff); CmdBuff.Clear(); }
void DefaultRenderFlow(ScriptableRenderContext context, Camera camera) { ScriptableCullingParameters cullingParameters; if (!camera.TryGetCullingParameters(out cullingParameters)) { return; } #if UNITY_EDITOR if (camera.cameraType == CameraType.SceneView) { ScriptableRenderContext.EmitWorldGeometryForSceneView(camera); } #endif cullingParameters.shadowDistance = Mathf.Min(setting.shadow.maxDistance, camera.farClipPlane); CmdBuff.BeginSample(camera.name); cullingResults = context.Cull(ref cullingParameters); SetShadow(context, camera); SetCamera(context, camera); ClearRenderBuffer(context, camera); SetLighting(context, camera); DrawDepthOnly(context, camera); DrawOpaque(context, camera); DrawSky(context, camera); #if UNITY_EDITOR if (UnityEditor.Handles.ShouldRenderGizmos()) { context.DrawGizmos(camera, GizmoSubset.PreImageEffects); } #endif #if UNITY_EDITOR if (UnityEditor.Handles.ShouldRenderGizmos()) { context.DrawGizmos(camera, GizmoSubset.PostImageEffects); } #endif CmdBuff.EndSample(camera.name); context.ExecuteCommandBuffer(CmdBuff); CmdBuff.Clear(); }
void SetRenderBuffer(ScriptableRenderContext context, Camera camera) { bool isClearColor, isClearDepth; GetClearFlags(camera, out isClearColor, out isClearDepth); //if (camera.cameraType == CameraType.Game) { CmdBuff.SetRenderTarget(renderBuffers[0], renderBuffers[2]); CmdBuff.ClearRenderTarget(isClearDepth, isClearColor, camera.backgroundColor); } context.ExecuteCommandBuffer(CmdBuff); CmdBuff.Clear(); }
void SetLighting(ScriptableRenderContext context, Camera camera) { int lightIndex = -1; for (int i = 0; i < cullingResults.visibleLights.Length; ++i) { VisibleLight curLight = cullingResults.visibleLights[i]; if (curLight.lightType != LightType.Directional) { continue; } lightIndex = i; break; } if (lightIndex < 0) { CmdBuff.DisableShaderKeyword("ENABLE_DIRECTIONAL_LIGHT"); context.ExecuteCommandBuffer(CmdBuff); CmdBuff.Clear(); return; } CmdBuff.EnableShaderKeyword("ENABLE_DIRECTIONAL_LIGHT"); CmdBuff.SetGlobalColor(LightColorID, cullingResults.visibleLights[lightIndex].finalColor); Vector4 lightDirection = cullingResults.visibleLights[lightIndex].localToWorldMatrix.GetColumn(2); // cullingResults.visibleLights[lightIndex].light.transform.forward lightDirection.x *= -1f; lightDirection.y *= -1f; lightDirection.z *= -1f; CmdBuff.SetGlobalVector(LightDirectionID, lightDirection); context.ExecuteCommandBuffer(CmdBuff); CmdBuff.Clear(); }
void CustomRenderFlow(ScriptableRenderContext context, Camera camera) { ScriptableCullingParameters cullingParameters; if (!camera.TryGetCullingParameters(out cullingParameters)) { return; } cullingParameters.shadowDistance = Mathf.Min(setting.shadow.maxDistance, camera.farClipPlane); CmdBuff.BeginSample(camera.name); cullingResults = context.Cull(ref cullingParameters); SetShadow(context, camera); SetCamera(context, camera); ClearRenderBuffer(context, camera); SetRenderBuffer(context, camera); SetLighting(context, camera); DrawDepthOnly(context, camera); DrawOpaque(context, camera); DrawSky(context, camera); Flip(context, camera); CmdBuff.EndSample(camera.name); context.ExecuteCommandBuffer(CmdBuff); CmdBuff.Clear(); }
void SetShadow(ScriptableRenderContext context, Camera camera) { if (camera.cameraType != CameraType.Game) { return; } int lightIndex = -1; for (int i = 0; i < cullingResults.visibleLights.Length; ++i) { VisibleLight curLight = cullingResults.visibleLights[i]; if (curLight.lightType != LightType.Directional) { continue; } if (curLight.light.shadows == LightShadows.None) { continue; } if (curLight.light.shadowStrength <= 0f) { continue; } Bounds bounds; if (cullingResults.GetShadowCasterBounds(i, out bounds) == false) { continue; } lightIndex = i; break; } if (lightIndex < 0) { CmdBuff.SetGlobalTexture(ShadowMapID, Texture2D.whiteTexture); context.ExecuteCommandBuffer(CmdBuff); CmdBuff.Clear(); return; } CmdBuff.SetGlobalFloat(ShadowBiasID, cullingResults.visibleLights[lightIndex].light.shadowBias); CmdBuff.SetGlobalFloat(ShadowStrengthID, cullingResults.visibleLights[lightIndex].light.shadowStrength); CmdBuff.SetGlobalVector(ShadowMapSizeID, new Vector4(invShadowMapSize, invShadowMapSize, shadowMapSize, shadowMapSize)); if (cullingResults.visibleLights[lightIndex].light.shadows == LightShadows.Soft) { CmdBuff.EnableShaderKeyword("_SHADOWS_SOFT"); } else { CmdBuff.DisableShaderKeyword("_SHADOWS_SOFT"); } Matrix4x4 viewMatrix; Matrix4x4 projMatrix; ShadowSplitData shadowSplitData; bool res = cullingResults.ComputeDirectionalShadowMatricesAndCullingPrimitives( lightIndex, // int activeLightIndex, 0, // int splitIndex, 1, // int splitCount, new Vector3(1.0f, 0.0f, 0.0f), // Vector3 splitRatio, (int)setting.shadow.atlasSize, // int shadowResolution, QualitySettings.shadowNearPlaneOffset, // float shadowNearPlaneOffset, out viewMatrix, // out Matrix4x4 viewMatrix, out projMatrix, // out Matrix4x4 projMatrix, out shadowSplitData // out Experimental.Rendering.ShadowSplitData shadowSplitData ); if (res == false) { CmdBuff.SetGlobalTexture(ShadowMapID, Texture2D.whiteTexture); context.ExecuteCommandBuffer(CmdBuff); CmdBuff.Clear(); return; } CoreUtils.SetRenderTarget(CmdBuff, renderBuffers[3], RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store, ClearFlag.Depth); CmdBuff.SetViewProjectionMatrices(viewMatrix, projMatrix); context.ExecuteCommandBuffer(CmdBuff); CmdBuff.Clear(); ShadowDrawingSettings shadowDrawingSettings = new ShadowDrawingSettings(cullingResults, lightIndex); shadowDrawingSettings.splitData = shadowSplitData; context.DrawShadows(ref shadowDrawingSettings); // https://docs.unity3d.com/2019.2/Documentation/Manual/SL-PlatformDifferences.html if (SystemInfo.usesReversedZBuffer) { projMatrix.m20 = -projMatrix.m20; projMatrix.m21 = -projMatrix.m21; projMatrix.m22 = -projMatrix.m22; projMatrix.m23 = -projMatrix.m23; } // [-1,1] remap to [0, 1] var scaleOffset = Matrix4x4.identity; scaleOffset.m00 = scaleOffset.m11 = scaleOffset.m22 = 0.5f; scaleOffset.m03 = scaleOffset.m13 = scaleOffset.m23 = 0.5f; CmdBuff.SetGlobalTexture(ShadowMapID, renderBuffers[3]); CmdBuff.SetGlobalMatrix(ShadowMatrixID, scaleOffset * (projMatrix * viewMatrix)); context.ExecuteCommandBuffer(CmdBuff); CmdBuff.Clear(); }