/// <summary> /// 储备方向光阴影 Vector2存取强度和光线的索引 支持四盏光的ShadowMask /// </summary> /// <param name="light"></param> /// <param name="visibleLightIndex"></param> public Vector4 ReserveDirectionalShadows(Light light, int visibleLightIndex) { //检测阴影强度 None &&cullingResults.GetShadowCasterBounds(visibleLightIndex, out Bounds b) if (shadowedDirLightCount < maxShadowedDirLightCount && light.shadows != LightShadows.None && light.shadowStrength > 0f) { float maskChannel = -1; //shadowMask LightBakingOutput lightBaking = light.bakingOutput; if (lightBaking.lightmapBakeType == LightmapBakeType.Mixed && lightBaking.mixedLightingMode == MixedLightingMode.Shadowmask) { useShadowMask = true; maskChannel = lightBaking.occlusionMaskChannel; } //直接Return 出去 不需要Cacade数据 阴影跑出CullSphere里面 if (!cullingResults.GetShadowCasterBounds(visibleLightIndex, out Bounds b)) { //负数防止Shader 采样ShadowMap return(new Vector4(-light.shadowStrength, 0f, 0f, maskChannel)); } ShadowDirectionalLights[shadowedDirLightCount] = new ShadowDirectionalLight { visibleLightIndex = visibleLightIndex, slopeScaleBias = light.shadowBias, nearPlaneOffset = light.shadowNearPlane }; //* 级联ShadowMap的数量 return(new Vector4(light.shadowStrength, settings.directional.cascadeCount * shadowedDirLightCount++, light.shadowNormalBias, maskChannel)); } return(new Vector4(0f, 0f, 0f, -1f)); }
void RenderDirectionalShadows(int index, int split, int tileSize) { ShadowDirectionalLight light = shadowDirectionalLights[index]; var shadowSettings = new ShadowDrawingSettings(shadowParams.cullingResults, light.visibleLightIndex); int cascadeCount = shadowParams.settings.directional.cascadeCount; int tileOffset = index * cascadeCount; Vector3 ratios = shadowParams.settings.directional.CasadeRatios; for (int i = 0; i < cascadeCount; ++i) { shadowParams.cullingResults.ComputeDirectionalShadowMatricesAndCullingPrimitives(light.visibleLightIndex, i, cascadeCount, ratios, tileSize, 0f, out Matrix4x4 viewMatrix, out Matrix4x4 projectionMatrix, out ShadowSplitData splitData); shadowSettings.splitData = splitData; if (index == 0) { //cullingSphere是以摄像机位置为球心的 Vector4 cullingSphere = splitData.cullingSphere; cullingSphere.w *= cullingSphere.w; cascadeCullingSpheres[i] = cullingSphere; } int tileIndex = tileOffset + i; dirShadowMatrices[tileIndex] = ConvertToAtlasMatrix(projectionMatrix * viewMatrix, SetTileViewPort(tileIndex, split, tileSize), split); //绘制指定区域的 buffer.SetViewProjectionMatrices(viewMatrix, projectionMatrix); buffer.SetGlobalDepthBias(50000f, 0f); ExecuteBuffer(); GlobalUniqueParamsForRP.context.DrawShadows(ref shadowSettings); buffer.SetGlobalDepthBias(0f, 0f); } }
public void ReserveDirectionalShadows(Light light, int visibleLightIndex) { if (_shadowDirectionalLightCount < _maxShadowedDirectionalLightCount && light.shadows != LightShadows.None && light.shadowStrength > 0f && _cullingResults.GetShadowCasterBounds(visibleLightIndex, out Bounds b)) { _shadowDirectionalLights[_shadowDirectionalLightCount++] = new ShadowDirectionalLight { visibleLightIndex = visibleLightIndex }; } }
private void RenderDirectionalShadows(int index, int tileSize) { ShadowDirectionalLight light = _shadowDirectionalLights[index]; var shadowDrawSettings = new ShadowDrawingSettings(_cullingResults, light.visibleLightIndex); _cullingResults.ComputeDirectionalShadowMatricesAndCullingPrimitives(light.visibleLightIndex, 0, 1, Vector3.zero, tileSize, 0f, out Matrix4x4 viewMatrix, out Matrix4x4 projectionMatrix, out ShadowSplitData splitData); shadowDrawSettings.splitData = splitData; _buffer.SetViewProjectionMatrices(viewMatrix, projectionMatrix); ExecuteBuffer(); _context.DrawShadows(ref shadowDrawSettings); }
void RenderDirectionalShadows(int index, int split, int tileSize) { ShadowDirectionalLight light = ShadowDirectionalLights[index]; var shadowSettings = new ShadowDrawingSettings(cullingResults, light.visibleLightIndex) { useRenderingLayerMaskTest = true }; int cascadeCount = settings.directional.cascadeCount; int tileOffset = index * cascadeCount; Vector3 ratios = settings.directional.CascadeRatios; //裁剪参数 float cullingFactor = Mathf.Max(0f, 0.8f - settings.directional.cascadeFade); float tileScale = 1f / split; //遍历Cascade的数量 for (int i = 0; i < cascadeCount; i++) { //Debug.Log(light.nearPlaneOffset); //Unity cullingResults.ComputeDirectionalShadowMatricesAndCullingPrimitives( light.visibleLightIndex, i, cascadeCount, ratios, tileSize, light.nearPlaneOffset, out Matrix4x4 viewMatrix, out Matrix4x4 projectionMatrix, out ShadowSplitData splitData); //??????????? splitData.shadowCascadeBlendCullingFactor = cullingFactor; shadowSettings.splitData = splitData; if (index == 0) { //裁剪球 判断片段在哪一级的Cascade里面 w为半径 Vector4 cullingSphere = splitData.cullingSphere; SetCascadeData(i, splitData.cullingSphere, tileSize); } int tileIndex = tileOffset + i; //设置ViewPort大小 //SetTileViewport(index, split, tileSize); //从世界空间到灯光阴影投影空间 dirShadowMatrices[tileIndex] = ConvertToAtlasMatrix(projectionMatrix * viewMatrix, SetTileViewport(tileIndex, split, tileSize), tileScale); buffer.SetViewProjectionMatrices(viewMatrix, projectionMatrix); //球覆盖 buffer.SetGlobalVectorArray(cascadeCullingSpheresId, cascadeCullingSpheres); //Cascade数据 buffer.SetGlobalVectorArray(cascadeDataId, cascadeData); //转换物体到灯光空间 buffer.SetGlobalMatrixArray(dirShadowMatricesId, dirShadowMatrices); buffer.SetGlobalDepthBias(0f, light.slopeScaleBias); ExecuteBuffer(); context.DrawShadows(ref shadowSettings); buffer.SetGlobalDepthBias(0f, 0f); } }
public Vector2 ReserveDirecionalShadows(Light light, int visibleLightIndex) { //目前只接受Light列表里的第一个直线光 if (ShadowDirectionalLightCount < maxShadowDirectionalLightCount && //可生产阴影的直线光数量不超过上限 light.shadows != LightShadows.None && light.shadowStrength > 0f && //灯光的设置里开启阴影,且阴影强度不为0 shadowParams.cullingResults.GetShadowCasterBounds(visibleLightIndex, out Bounds b) //查看该灯光的影响范围是否在相机可见视锥体内 ) { shadowDirectionalLights[ShadowDirectionalLightCount] = new ShadowDirectionalLight { visibleLightIndex = visibleLightIndex }; return(new Vector2(light.shadowStrength, shadowParams.settings.directional.cascadeCount * ShadowDirectionalLightCount++)); } return(Vector2.zero); }