/// <summary> /// Coroutine to paint control texture /// </summary> /// <param name="arguments">Arguments from painting event</param> private IEnumerator PaintControlTexture(AlphaMapsNeedPaintArgs arguments) { // Get a temporary render texture to be painted onto var destinationTexture = arguments.Terrain.terrainData.GetAlphamapTexture(0); var renderTextureDescriptor = new RenderTextureDescriptor(destinationTexture.width, destinationTexture.height, RenderTextureFormat.ARGB32, 0); var temporaryRenderTexture = RenderTexture.GetTemporary(renderTextureDescriptor); arguments.RegisterFinalizer(() => RenderTexture.ReleaseTemporary(temporaryRenderTexture)); yield return(null); // Make the active render texture our temporary one, storing the current one for restoration once we're done var renderTexture = RenderTexture.active; RenderTexture.active = temporaryRenderTexture; // Paint the control texture from the feature mask using the configured material/shader Graphics.Blit(arguments.FeatureMaskRenderTexture, temporaryRenderTexture, MapStyleProvider.Instance.TerrainControlTexture); destinationTexture.ReadPixels(new Rect(0, 0, destinationTexture.width, destinationTexture.height), 0, 0); destinationTexture.Apply(); arguments.Terrain.terrainData.SetBaseMapDirty(); // Restore the previously active render texture RenderTexture.active = renderTexture; }
/// <summary> /// Coroutine to paint control texture. /// </summary> /// <param name="args">Arguments from painting event</param>. private IEnumerator PaintControlTexture(AlphaMapsNeedPaintArgs args) { // Get a temporary render texture to be painted onto. Texture2D destinationTexture = args.Terrain.terrainData.GetAlphamapTexture(0); RenderTextureDescriptor renderTextureDescriptor = new RenderTextureDescriptor( destinationTexture.width, destinationTexture.height, RenderTextureFormat.ARGB32, 0); RenderTexture temporaryRenderTexture = RenderTexture.GetTemporary(renderTextureDescriptor); args.RegisterFinalizer(delegate { RenderTexture.ReleaseTemporary(temporaryRenderTexture); }); // Yield to allow work to be split across frames if necessary. yield return(null); // Make the active render texture our temporary one (storing the current one for restoration // once we're done). // // Note that this is done before Graphics.Blit, which appears to set RenderTexture.active as // well, but this doesn't appear to be documented behavior so isn't relied upon. // https://docs.unity3d.com/ScriptReference/Graphics.Blit.html RenderTexture renderTextureToRestore = RenderTexture.active; RenderTexture.active = temporaryRenderTexture; // Paint the control texture from the feature mask using the configured material/shader. Graphics.Blit( args.FeatureMaskRenderTexture, temporaryRenderTexture, ControlTexturePaintMaterial); destinationTexture.ReadPixels( new Rect(0, 0, destinationTexture.width, destinationTexture.height), 0, 0); destinationTexture.Apply(); args.Terrain.terrainData.SetBaseMapDirty(); // Restore the previously active render texture. RenderTexture.active = renderTextureToRestore; }