public void Render(CommandBuffer cmd, ref LightRenderingData data) { var lightmap = ShaderIDLightMap; var shadowmap = ShaderIDShadowMap; var screenSize = new Vector2(data.camera.pixelWidth, data.camera.pixelHeight); cmd.GetTemporaryRT( lightmap, Mathf.FloorToInt(screenSize.x * data.settings.LightMapResolutionScale), Mathf.FloorToInt(screenSize.y * data.settings.LightMapResolutionScale), 0, FilterMode.Bilinear, data.settings.LightMapFormat); cmd.GetTemporaryRT( shadowmap, Mathf.FloorToInt(screenSize.x * data.settings.ShadowMapResolutionScale), Mathf.FloorToInt(screenSize.y * data.settings.ShadowMapResolutionScale), 0, FilterMode.Bilinear, data.settings.ShadowMapFormat); data.lightmap = lightmap; data.shadowmap = shadowmap; cmd.SetRenderTarget(lightmap); cmd.ClearRenderTarget(true, true, Color.black); bool shouldClearShadowMap = true; foreach (var light in Light2DBase.AssetsManager.Assets) { if (!light.enabled || !light.gameObject.activeInHierarchy) { continue; } if (shouldClearShadowMap) { cmd.SetRenderTarget(shadowmap); cmd.ClearRenderTarget(true, true, Color.black); shouldClearShadowMap = false; } if (light.LightShadows != LightShadows.None) { light.RenderShadow(cmd, ref data); shouldClearShadowMap = true; } light.RenderLight(cmd, ref data); } cmd.SetGlobalFloat("_ExposureLimit", data.settings.ExposureLimit); cmd.SetGlobalTexture("_LightMap", lightmap); cmd.SetGlobalColor("_GlobalLight", data.settings.GlobalLight); cmd.Blit(BuiltinRenderTextureType.None, data.cameraColorTarget, ShaderPool.Get("Lighting2D/DeferredLighting"), 0); cmd.ReleaseTemporaryRT(lightmap); cmd.ReleaseTemporaryRT(shadowmap); }
public void RenderShadow(CommandBuffer cmd, ref LightRenderingData data) { if (LightShadows == LightShadows.None) { return; } if (shadowMat == null) { shadowMat = new Material(Shader.Find("Lighting2D/Shadow")); } if (!ShadowMesh) { ShadowMesh = new Mesh(); } if (!tempMesh) { tempMesh = new Mesh(); } ShadowMesh.Clear(); //subShadowMesh.ForEach(mesh => mesh.Clear()); //subShadowMesh.Clear(); var meshBuilder = new MeshBuilder(); int count = Physics2D.OverlapCircleNonAlloc(transform.position, LightDistance, shadowCasters); CombineInstance[] combineArr = new CombineInstance[count]; for (var i = 0; i < count; i++) { Collider2D caster = shadowCasters[i]; if (caster is PolygonCollider2D) { var mesh = PolygonShadowMesh(caster as PolygonCollider2D); combineArr[i].mesh = mesh; meshBuilder.AddCopiedMesh(mesh); mesh.Clear(); } } ShadowMesh = meshBuilder.ToMesh(ShadowMesh); if (LightShadows == LightShadows.Soft && ShadowSmooth == ShadowSmooth.VolumnLight) { cmd.SetGlobalFloat("_LightSize", LightVolumn); cmd.DrawMesh(ShadowMesh, Matrix4x4.TRS(transform.position, transform.rotation, transform.localScale), shadowMat, 0, 1); } else { cmd.DrawMesh(ShadowMesh, Matrix4x4.TRS(transform.position, transform.rotation, transform.localScale), shadowMat, 0, 0); if (LightShadows == LightShadows.Soft && ShadowSmooth == ShadowSmooth.Blur) { GaussianBlur.Blur(SmoothRadius, cmd, data.shadowmap, data.shadowmap, ShaderPool.Get("GaussianBlur/Blur")); } } }