static private void RenderLightVolumeSet(Camera camera, int lightOpIndex, CommandBuffer cmdBuffer, int layerToRender, RenderTargetIdentifier renderTexture, List <Light2D> lights) { if (lights.Count > 0) { for (int i = 0; i < lights.Count; i++) { Light2D light = lights[i]; int topMostLayer = light.GetTopMostLitLayer(); if (layerToRender == topMostLayer) { if (light != null && light.lightType != Light2D.LightType.Global && light.volumeOpacity > 0.0f && light.lightOperationIndex == lightOpIndex && light.IsLitLayer(layerToRender) && light.IsLightVisible(camera)) { Material lightVolumeMaterial = GetLightMaterial(light, true); if (lightVolumeMaterial != null) { Mesh lightMesh = light.GetMesh(); if (lightMesh != null) { if (light.lightType == Light2D.LightType.Sprite && light.lightCookieSprite != null && light.lightCookieSprite.texture != null) { cmdBuffer.SetGlobalTexture("_CookieTex", light.lightCookieSprite.texture); } cmdBuffer.SetGlobalFloat("_FalloffIntensity", light.falloffIntensity); cmdBuffer.SetGlobalFloat("_FalloffDistance", light.shapeLightFalloffSize); cmdBuffer.SetGlobalVector("_FalloffOffset", light.shapeLightFalloffOffset); cmdBuffer.SetGlobalColor("_LightColor", light.intensity * light.color); cmdBuffer.SetGlobalFloat("_VolumeOpacity", light.volumeOpacity); // Is this needed if (light.useNormalMap || light.lightType == Light2D.LightType.Point) { RendererLighting.SetPointLightShaderGlobals(cmdBuffer, light); } // Could be combined... if (light.lightType == Light2D.LightType.Parametric || light.lightType == Light2D.LightType.Freeform || light.lightType == Light2D.LightType.Sprite) { cmdBuffer.DrawMesh(lightMesh, light.transform.localToWorldMatrix, lightVolumeMaterial); } else if (light.lightType == Light2D.LightType.Point) { Vector3 scale = new Vector3(light.pointLightOuterRadius, light.pointLightOuterRadius, light.pointLightOuterRadius); Matrix4x4 matrix = Matrix4x4.TRS(light.transform.position, Quaternion.identity, scale); cmdBuffer.DrawMesh(lightMesh, matrix, lightVolumeMaterial); } } } } } } } }
static private bool RenderLightSet(Camera camera, int lightOpIndex, CommandBuffer cmdBuffer, int layerToRender, List <Light2D> lights) { bool renderedAnyLight = false; foreach (var light in lights) { if (light != null && light.lightType != Light2D.LightType.Global && light.lightOperationIndex == lightOpIndex && light.IsLitLayer(layerToRender) && light.IsLightVisible(camera)) { Material lightMaterial = GetLightMaterial(light, false); if (lightMaterial != null) { Mesh lightMesh = light.GetMesh(); if (lightMesh != null) { if (!renderedAnyLight) { renderedAnyLight = true; } if (light.lightType == Light2D.LightType.Sprite && light.lightCookieSprite != null && light.lightCookieSprite.texture != null) { cmdBuffer.SetGlobalTexture("_CookieTex", light.lightCookieSprite.texture); } cmdBuffer.SetGlobalFloat("_FalloffIntensity", light.falloffIntensity); cmdBuffer.SetGlobalFloat("_FalloffDistance", light.shapeLightFalloffSize); cmdBuffer.SetGlobalVector("_FalloffOffset", light.shapeLightFalloffOffset); cmdBuffer.SetGlobalColor("_LightColor", light.intensity * light.color); cmdBuffer.SetGlobalFloat("_VolumeOpacity", light.volumeOpacity); if (light.useNormalMap || light.lightType == Light2D.LightType.Point) { RendererLighting.SetPointLightShaderGlobals(cmdBuffer, light); } // Light code could be combined... if (light.lightType == Light2D.LightType.Parametric || light.lightType == Light2D.LightType.Freeform || light.lightType == Light2D.LightType.Sprite) { cmdBuffer.DrawMesh(lightMesh, light.transform.localToWorldMatrix, lightMaterial); } else if (light.lightType == Light2D.LightType.Point) { Vector3 scale = new Vector3(light.pointLightOuterRadius, light.pointLightOuterRadius, light.pointLightOuterRadius); Matrix4x4 matrix = Matrix4x4.TRS(light.transform.position, Quaternion.identity, scale); cmdBuffer.DrawMesh(lightMesh, matrix, lightMaterial); } } } } } return(renderedAnyLight); }
static private void RenderLightVolumeSet(Camera camera, int lightOpIndex, CommandBuffer cmdBuffer, int layerToRender, RenderTargetIdentifier renderTexture, List <Light2D> lights) { if (lights.Count > 0) { for (int i = 0; i < lights.Count; i++) { Light2D light = lights[i]; int topMostLayer = light.GetTopMostLitLayer(); if (layerToRender == topMostLayer) { if (light != null && light.lightType != Light2D.LightType.Global && light.volumeOpacity > 0.0f && light.lightOperationIndex == lightOpIndex && light.IsLitLayer(layerToRender) && light.IsLightVisible(camera)) { Material shapeLightVolumeMaterial = GetLightMaterial(light, true); if (shapeLightVolumeMaterial != null) { Mesh lightMesh = light.GetMesh(); if (lightMesh != null) { if (light.lightType == Light2D.LightType.Sprite && light.lightCookieSprite != null && light.lightCookieSprite.texture != null) { cmdBuffer.SetGlobalTexture("_CookieTex", light.lightCookieSprite.texture); } cmdBuffer.SetGlobalFloat("_FalloffCurve", light.falloffCurve); if (!light.hasDirection) { cmdBuffer.DrawMesh(lightMesh, light.transform.localToWorldMatrix, shapeLightVolumeMaterial); } else { RendererLighting.SetPointLightShaderGlobals(cmdBuffer, light); //Vector3 scale = new Vector3(2 * light.m_PointLightOuterRadius, 2 * light.m_PointLightOuterRadius, 1); Vector3 scale = new Vector3(light.pointLightOuterRadius, light.pointLightOuterRadius, light.pointLightOuterRadius); Matrix4x4 matrix = Matrix4x4.TRS(light.transform.position, Quaternion.identity, scale); cmdBuffer.DrawMesh(lightMesh, matrix, shapeLightVolumeMaterial); } } } } } } } }
static private bool RenderLightSet(Camera camera, int lightOpIndex, CommandBuffer cmdBuffer, int layerToRender, List <Light2D> lights) { bool renderedAnyLight = false; foreach (var light in lights) { if (light != null && light.lightType != Light2D.LightType.Global && light.lightOperationIndex == lightOpIndex && light.IsLitLayer(layerToRender) && light.IsLightVisible(camera)) { Material shapeLightMaterial = GetLightMaterial(light, false); if (shapeLightMaterial != null) { Mesh lightMesh = light.GetMesh(); if (lightMesh != null) { if (!renderedAnyLight) { renderedAnyLight = true; } if (light.lightType == Light2D.LightType.Sprite && light.lightCookieSprite != null && light.lightCookieSprite.texture != null) { cmdBuffer.SetGlobalTexture("_CookieTex", light.lightCookieSprite.texture); } cmdBuffer.SetGlobalFloat("_FalloffCurve", light.falloffCurve); if (!light.hasDirection) { cmdBuffer.DrawMesh(lightMesh, light.transform.localToWorldMatrix, shapeLightMaterial); } else { RendererLighting.SetPointLightShaderGlobals(cmdBuffer, light); //Vector3 scale = new Vector3(2 * light.m_PointLightOuterRadius, 2 * light.m_PointLightOuterRadius, 1); Vector3 scale = new Vector3(light.pointLightOuterRadius, light.pointLightOuterRadius, light.pointLightOuterRadius); Matrix4x4 matrix = Matrix4x4.TRS(light.transform.position, Quaternion.identity, scale); cmdBuffer.DrawMesh(lightMesh, matrix, shapeLightMaterial); } } } } } return(renderedAnyLight); }
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData) { #if UNITY_EDITOR if (!Application.isPlaying) { s_SortingLayers = SortingLayer.layers; } #endif Camera camera = renderingData.cameraData.camera; RendererLighting.Setup(m_RendererData); CommandBuffer cmd = CommandBufferPool.Get("Render 2D Lighting"); cmd.Clear(); Profiler.BeginSample("RenderSpritesWithLighting - Create Render Textures"); RendererLighting.CreateRenderTextures(cmd, camera); Profiler.EndSample(); cmd.SetGlobalFloat("_HDREmulationScale", m_RendererData.hdrEmulationScale); cmd.SetGlobalFloat("_InverseHDREmulationScale", 1.0f / m_RendererData.hdrEmulationScale); #if UNITY_EDITOR bool isPreview = false; isPreview = EditorSceneManager.IsPreviewSceneObject(camera); if (isPreview) { RendererLighting.SetPreviewShaderGlobals(cmd); } else #endif RendererLighting.SetShapeLightShaderGlobals(cmd); context.ExecuteCommandBuffer(cmd); Profiler.BeginSample("RenderSpritesWithLighting - Prepare"); DrawingSettings combinedDrawSettings = CreateDrawingSettings(k_ShaderTags, ref renderingData, SortingCriteria.CommonTransparent); DrawingSettings normalsDrawSettings = CreateDrawingSettings(k_NormalsRenderingPassName, ref renderingData, SortingCriteria.CommonTransparent); FilteringSettings filterSettings = new FilteringSettings(); filterSettings.renderQueueRange = RenderQueueRange.all; filterSettings.layerMask = -1; filterSettings.renderingLayerMask = 0xFFFFFFFF; filterSettings.sortingLayerRange = SortingLayerRange.all; Profiler.EndSample(); for (int i = 0; i < s_SortingLayers.Length; i++) { // Some renderers override their sorting layer value with short.MinValue or short.MaxValue. // When drawing the first sorting layer, we should include the range from short.MinValue to layerValue. // Similarly, when drawing the last sorting layer, include the range from layerValue to short.MaxValue. short layerValue = (short)s_SortingLayers[i].value; var lowerBound = (i == 0) ? short.MinValue : layerValue; var upperBound = (i == s_SortingLayers.Length - 1) ? short.MaxValue : layerValue; filterSettings.sortingLayerRange = new SortingLayerRange(lowerBound, upperBound); int layerToRender = s_SortingLayers[i].id; Light2D.LightStats lightStats; lightStats = Light2D.GetLightStatsByLayer(layerToRender); if (lightStats.totalNormalMapUsage > 0) { RendererLighting.RenderNormals(context, renderingData.cullResults, normalsDrawSettings, filterSettings); } cmd.Clear(); if (lightStats.totalLights > 0) { #if UNITY_EDITOR cmd.name = "Render Lights - " + SortingLayer.IDToName(layerToRender); #endif RendererLighting.RenderLights(camera, cmd, layerToRender); } else { RendererLighting.ClearDirtyLighting(cmd); } CoreUtils.SetRenderTarget(cmd, colorAttachment, RenderBufferLoadAction.Load, RenderBufferStoreAction.Store, ClearFlag.None, Color.white); context.ExecuteCommandBuffer(cmd); Profiler.BeginSample("RenderSpritesWithLighting - Draw Transparent Renderers"); context.DrawRenderers(renderingData.cullResults, ref combinedDrawSettings, ref filterSettings); Profiler.EndSample(); if (lightStats.totalVolumetricUsage > 0) { cmd.Clear(); #if UNITY_EDITOR cmd.name = "Render Light Volumes" + SortingLayer.IDToName(layerToRender); #endif RendererLighting.RenderLightVolumes(camera, cmd, layerToRender); context.ExecuteCommandBuffer(cmd); cmd.Clear(); } } cmd.Clear(); Profiler.BeginSample("RenderSpritesWithLighting - Release RenderTextures"); RendererLighting.ReleaseRenderTextures(cmd); Profiler.EndSample(); context.ExecuteCommandBuffer(cmd); CommandBufferPool.Release(cmd); filterSettings.sortingLayerRange = SortingLayerRange.all; RenderingUtils.RenderObjectsWithError(context, ref renderingData.cullResults, camera, filterSettings, SortingCriteria.None); }
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData) { #if UNITY_EDITOR if (!Application.isPlaying) { s_SortingLayers = SortingLayer.layers; } #endif Camera camera = renderingData.cameraData.camera; RendererLighting.Setup(m_RendererData); CommandBuffer cmd = CommandBufferPool.Get("Render 2D Lighting"); Profiler.BeginSample("RenderSpritesWithLighting - Create Render Textures"); RendererLighting.CreateRenderTextures(cmd, camera); Profiler.EndSample(); cmd.SetGlobalFloat("_LightIntensityScale", m_RendererData.lightIntensityScale); cmd.SetGlobalFloat("_InverseLightIntensityScale", 1.0f / m_RendererData.lightIntensityScale); RendererLighting.SetShapeLightShaderGlobals(cmd); context.ExecuteCommandBuffer(cmd); Profiler.BeginSample("RenderSpritesWithLighting - Prepare"); DrawingSettings drawSettings = CreateDrawingSettings(k_CombinedRenderingPassName, ref renderingData, SortingCriteria.CommonTransparent); FilteringSettings filterSettings = new FilteringSettings(); filterSettings.renderQueueRange = RenderQueueRange.all; filterSettings.layerMask = -1; filterSettings.renderingLayerMask = 0xFFFFFFFF; filterSettings.sortingLayerRange = SortingLayerRange.all; Profiler.EndSample(); bool cleared = false; for (int i = 0; i < s_SortingLayers.Length; i++) { short layerValue = (short)s_SortingLayers[i].value; filterSettings.sortingLayerRange = new SortingLayerRange(layerValue, layerValue); RendererLighting.RenderNormals(context, renderingData.cullResults, drawSettings, filterSettings); cmd.Clear(); int layerToRender = s_SortingLayers[i].id; RendererLighting.RenderLights(camera, cmd, layerToRender); // This should have an optimization where I can determine if this needs to be called. // And the clear is only needed if no previous pass has cleared the camera RT yet. var clearFlag = cleared ? ClearFlag.None : ClearFlag.All; var clearColor = renderingData.cameraData.camera.backgroundColor; cleared = true; SetRenderTarget(cmd, BuiltinRenderTextureType.CameraTarget, RenderBufferLoadAction.Load, RenderBufferStoreAction.Store, clearFlag, clearColor, TextureDimension.Tex2D); context.ExecuteCommandBuffer(cmd); Profiler.BeginSample("RenderSpritesWithLighting - Draw Renderers"); context.DrawRenderers(renderingData.cullResults, ref drawSettings, ref filterSettings); Profiler.EndSample(); cmd.Clear(); RendererLighting.RenderLightVolumes(camera, cmd, layerToRender); context.ExecuteCommandBuffer(cmd); cmd.Clear(); } Profiler.BeginSample("RenderSpritesWithLighting - Release RenderTextures"); RendererLighting.ReleaseRenderTextures(cmd); Profiler.EndSample(); context.ExecuteCommandBuffer(cmd); CommandBufferPool.Release(cmd); filterSettings.sortingLayerRange = SortingLayerRange.all; RenderingUtils.RenderObjectsWithError(context, ref renderingData.cullResults, camera, filterSettings, SortingCriteria.None); }