static Material CreateLightMaterial(Light2D light, bool isVolume) { bool isShape = light.IsShapeLight(); Material material; if (isVolume) { material = CoreUtils.CreateEngineMaterial(isShape ? s_RendererData.shapeLightVolumeShader : s_RendererData.pointLightVolumeShader); } else { material = CoreUtils.CreateEngineMaterial(isShape ? s_RendererData.shapeLightShader : s_RendererData.pointLightShader); if (!light.alphaBlendOnOverlap) { SetBlendModes(material, BlendMode.One, BlendMode.One); material.EnableKeyword(k_UseAdditiveBlendingKeyword); } else { SetBlendModes(material, BlendMode.SrcAlpha, BlendMode.OneMinusSrcAlpha); } } if (light.lightType == Light2D.LightType.Sprite) { material.EnableKeyword(k_SpriteLightKeyword); } if (!isShape && light.lightCookieSprite != null && light.lightCookieSprite.texture != null) { material.EnableKeyword(k_UsePointLightCookiesKeyword); } if (!isShape && light.pointLightQuality == Light2D.PointLightQuality.Fast) { material.EnableKeyword(k_LightQualityFastKeyword); } if (light.useNormalMap) { material.EnableKeyword(k_UseNormalMap); } return(material); }
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 public void RenderLights(Camera camera, CommandBuffer cmdBuffer, int layerToRender) { for (int i = 0; i < s_LightOperations.Length; ++i) { if (!s_LightOperations[i].enabled) { continue; } string sampleName = "2D Lights - " + s_LightOperations[i].name; cmdBuffer.BeginSample(sampleName); cmdBuffer.SetRenderTarget(s_RenderTargets[i].Identifier()); bool rtDirty = false; Color clearColor; if (!Light2D.globalClearColors[i].TryGetValue(layerToRender, out clearColor)) { clearColor = Color.black; } else { rtDirty = true; } if (s_RenderTargetsDirty[i] || rtDirty) { cmdBuffer.ClearRenderTarget(false, true, clearColor); } rtDirty |= RenderLightSet( camera, i, cmdBuffer, layerToRender, Light2D.GetLightsByLightOperation(i) ); s_RenderTargetsDirty[i] = rtDirty; cmdBuffer.EndSample(sampleName); } }
private void Awake() { uiManager = FindObjectOfType <UIManager>(); Assert.IsNotNull(uiManager, "No reference to UIManager script."); Assert.IsNotNull(playerValues, "No reference to PlayerValues scriptable object."); invaderExplosionAnim = GetComponentInParent <Animator>(); Assert.IsNotNull(invaderExplosionAnim, "Failed to access to EnemyInvader's Animator."); invaderScript = GetComponentInParent <EnemyInvader>(); Assert.IsNotNull(invaderScript, "Failed to access to EnemyInvader's script."); spriteRenderer = GetComponent <SpriteRenderer>(); Assert.IsNotNull(spriteRenderer, "Failed to find Sprite Renderer component."); lt = GetComponentInChildren <UnityEngine.Experimental.Rendering.LWRP.Light2D>(); Assert.IsNotNull(spriteRenderer, "Failed to find child light."); explosionClip = GetComponentInParent <AudioSource>(); Assert.IsNotNull(explosionClip, "Failed to access to parent's Audio Source component."); }
static public void GetScaledLightInvMatrix(Light2D light, out Matrix4x4 retMatrix, bool includeRotation) { float outerRadius = light.pointLightOuterRadius; //Vector3 lightScale = light.transform.lossyScale; Vector3 lightScale = Vector3.one; Vector3 outerRadiusScale = new Vector3(lightScale.x * outerRadius, lightScale.y * outerRadius, lightScale.z * outerRadius); Quaternion rotation; if (includeRotation) { rotation = light.transform.rotation; } else { rotation = Quaternion.identity; } Matrix4x4 scaledLightMat = Matrix4x4.TRS(light.transform.position, rotation, outerRadiusScale); retMatrix = Matrix4x4.Inverse(scaledLightMat); }
static uint GetLightMaterialIndex(Light2D light, bool isVolume) { int bitIndex = 0; uint volumeBit = isVolume ? 1u << bitIndex : 0u; bitIndex++; uint shapeBit = light.IsShapeLight() ? 1u << bitIndex : 0u; bitIndex++; uint additiveBit = (light.IsShapeLight() && light.shapeLightOverlapMode == Light2D.LightOverlapMode.Additive) ? 1u << bitIndex : 0u; bitIndex++; uint spriteBit = light.lightType == Light2D.LightType.Sprite ? 1u << bitIndex : 0u; bitIndex++; uint pointCookieBit = (!light.IsShapeLight() && light.lightCookieSprite != null && light.lightCookieSprite.texture != null) ? 1u << bitIndex : 0u; bitIndex++; uint pointFastQualityBit = (!light.IsShapeLight() && light.pointLightQuality == Light2D.PointLightQuality.Fast) ? 1u << bitIndex : 0u; return(pointFastQualityBit | pointCookieBit | spriteBit | additiveBit | shapeBit | volumeBit); }
static public void RenderLightVolumes(Camera camera, CommandBuffer cmdBuffer, int layerToRender) { for (int i = 0; i < s_LightOperations.Length; ++i) { if (!s_LightOperations[i].enabled) { continue; } string sampleName = "2D Shape Light Volumes - " + s_LightOperations[i].name; cmdBuffer.BeginSample(sampleName); RenderLightVolumeSet( camera, i, cmdBuffer, layerToRender, s_RenderTargets[i].Identifier(), Light2D.GetLightsByLightOperation(i) ); cmdBuffer.EndSample(sampleName); } }
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); }
static public float GetNormalizedInnerRadius(Light2D light) { return(light.pointLightInnerRadius / light.pointLightOuterRadius); }
void Start() { light2d = GetComponent <UnityEngine.Experimental.Rendering.LWRP.Light2D>(); gate = GameObject.FindGameObjectWithTag("Gate"); }
void Start() { theLight = this.gameObject; globalLight = theLight.GetComponent <UnityEngine.Experimental.Rendering.LWRP.Light2D>(); }