//------------------------------------------------------------------------------------- // //------------------------------------------------------------------------------------- private void Init() { _geometry = new Mesh(); _buffer = new CommandBuffer(); _camera = GetComponent <Camera>(); _shadowMapInitialTexture = new RenderTexture((int)_lightmapResolution, (int)_maxShadowCastingLights, 0, RenderTextureFormat.RFloat); _shadowMapFinalTexture = new RenderTexture((int)_lightmapResolution, (int)_maxShadowCastingLights, 0, RenderTextureFormat.RFloat); _material = new Material(Shader.Find("Soraphis/RTLM/Mapping")); GeometryCollector.CollectStatic(_geometry, _staticData); }
private void Init() { if (!_updated) // static init: { _geometry = new Mesh(); GeometryCollector.CollectStatic(_geometry, StaticData); _updated = true; } _material = new Material(_shader); _lightMesh = new Mesh(); _lightMesh.MarkDynamic(); }
//------------------------------------------------------------------------------------- // //------------------------------------------------------------------------------------- private void StaticUpdate() { if (_updated) { return; } if (!Application.isPlaying && _geometry == null) { Init(); } if (!Application.isPlaying) { GeometryCollector.CollectStatic(_geometry, StaticData); } GeometryCollector.CollectDynamic(_geometry, StaticData); _updated = true; }
private void LateUpdate() { if (!Application.isPlaying && _geometry == null) { Init(); // possible in EditMode } if (!Application.isPlaying) { GeometryCollector.CollectStatic(_geometry, _staticData); } GeometryCollector.CollectDynamic(_geometry, _staticData); _buffer.Clear(); _buffer.SetRenderTarget(_shadowMapInitialTexture); _buffer.ClearRenderTarget(true, true, Color.white, 0f); int slot = 0; foreach (var light2D in Light2D_LM.Lights) { var properties = light2D.GetMaterialProperties(slot, (int)_maxShadowCastingLights, _shadowMapFinalTexture); int shaderPass = light2D.LightType == Light2D.Light2DType.Line ? 2 : 0; _buffer.DrawMesh(_geometry, Matrix4x4.identity, _material, 0, shaderPass, properties); ++slot; if (slot >= (int)_maxShadowCastingLights) { break; } } _buffer.SetRenderTarget(_shadowMapFinalTexture); _buffer.ClearRenderTarget(true, true, Color.white, 1.0f); _buffer.SetGlobalTexture("_MainTex", _shadowMapInitialTexture); _buffer.Blit(_shadowMapInitialTexture, _shadowMapFinalTexture, _material, 1); _buffer.SetGlobalTexture("_ShadowTex", _shadowMapFinalTexture); //if(!Application.isPlaying) Graphics.ExecuteCommandBuffer(_buffer); }
private void RenderShadows(ScriptableRenderContext context) { GeometryCollector.CollectDynamic(GeometryMesh, _staticData); if (_shadowMapInitialTexture == null) { Init(); // may happen in scene switches } _shadowbuffer.Clear(); context.ExecuteCommandBuffer(_shadowbuffer); _shadowbuffer.SetRenderTarget(_shadowMapInitialTexture); _shadowbuffer.ClearRenderTarget(false, true, Color.white); context.ExecuteCommandBuffer(_shadowbuffer); /*CoreUtils.SetRenderTarget(_shadowbuffer, _ShadowMapInitialTexture, * RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store, * ClearFlag.All, Color.white);*/ //using (new ProfilingSample(_shadowbuffer, "Mapping Shadows")) { int slot = 0; int maxSlots = Mathf.Min(_shadowMapInitialTexture.height, _maxLightSources); foreach (var light in Light2D_Pipeline.Lights) { if (!light.IsLightVisible()) { continue; } _shadowMapParams[slot] = GetShadowMapParams(slot, maxSlots); _shadowPropertyBlock.Clear(); _shadowPropertyBlock.SetVector(LightAttenuation, _visibleLightAttenuations[slot]); _shadowPropertyBlock.SetVector(ShadowMapParams, _shadowMapParams[slot]); _shadowPropertyBlock.SetVector(LightPosition, light.LightData()); _shadowbuffer.DrawMesh(GeometryMesh, Matrix4x4.identity, _shadowMaterial, 0, 0, _shadowPropertyBlock); ++slot; if (slot >= maxSlots) { break; } } _shadowbuffer.SetGlobalVectorArray(ShadowMapParamId, _shadowMapParams); context.ExecuteCommandBuffer(_shadowbuffer); _shadowbuffer.Clear(); //} context.ExecuteCommandBuffer(_shadowbuffer); //using (new ProfilingSample(_shadowbuffer, "Refit Shadowmap")) //{ _shadowbuffer.SetRenderTarget(_shadowMapFinalTexture); _shadowbuffer.ClearRenderTarget(true, true, Color.white, 1.0f); context.ExecuteCommandBuffer(_shadowbuffer); _shadowbuffer.Clear(); _shadowbuffer.SetGlobalTexture("_MainTex", _shadowMapInitialTexture); // fixes a weird behaviour in Unity with the blit function on command buffers _shadowbuffer.Blit(_shadowMapInitialTexture, _shadowMapFinalTexture, _shadowMaterial, 1); context.ExecuteCommandBuffer(_shadowbuffer); _shadowbuffer.Clear(); //} context.ExecuteCommandBuffer(_shadowbuffer); }