public override bool OnPaint(Terrain terrain, IOnPaint editContext)
        {
            MaterialManager mgr = terrain.GetComponent <MaterialManager>();

            if (mgr != null)
            {
                //                IndexMap indexMap = mgr.GetIndexMap();
                //                RenderTexture rt= indexMap.GetTempRenderTexture(true);
                paintInProgress = true;
                BrushTransform brushXform = TerrainPaintUtility.CalculateBrushTransform(terrain, editContext.uv, editContext.brushSize, 0.0f);

                PaintContext indexCtx = PaintContext.CreateFromBounds(terrain, brushXform.GetBrushXYBounds(), 64, 64, 1);
                indexCtx.CreateRenderTargets(RenderTextureFormat.ARGB32);

                PaintContext normalCtx = TerrainPaintUtility.CollectNormals(terrain, brushXform.GetBrushXYBounds(), 2);

                Material blitMaterial = TerrainPaintUtility.GetBlitMaterial();
                indexCtx.Gather(
                    TerrainToIndexMapTexture,
                    new Color(0.0f, 0.0f, 0.0f, 0.0f),
                    blitMaterial, 0,
                    null,       // before
                    null);      // after

                // render source -> dest here
                //                RenderTexture.active = ctx.oldRenderTexture;

                Material paintMaterial = GetPaintMaterial();

                float   brushStrength = Event.current.shift ? -editContext.brushStrength : editContext.brushStrength;
                Vector4 brushParams   = new Vector4(
                    brushStrength,
                    0.0f,
                    0.0f, 0.0f);
                paintMaterial.SetTexture("_BrushTex", editContext.brushTexture);
                paintMaterial.SetVector("_BrushParams", brushParams);
                paintMaterial.SetFloat("materialIndex", materialIndex);
                paintMaterial.SetTexture("_NormalMap", normalCtx.sourceRenderTexture);

                Vector4 indexToNormalXform;
                TerrainPaintUtility.BuildTransformPaintContextUVToPaintContextUV(
                    indexCtx,
                    normalCtx,
                    out indexToNormalXform);
                paintMaterial.SetVector("_indexToNormalXform", indexToNormalXform);

                Vector4 xformParams = new Vector4();
                xformParams.x = (randomRotation ? minRotation : fixedRotation) / 360.0f;
                xformParams.y = (randomRotation ? maxRotation : fixedRotation) / 360.0f;
                paintMaterial.SetVector("_xformParams", xformParams);

                Vector4 randoms = new Vector4(
                    UnityEngine.Random.Range(0.0f, 1.0f),
                    UnityEngine.Random.Range(0.0f, 1.0f),
                    UnityEngine.Random.Range(0.0f, 1.0f),
                    UnityEngine.Random.Range(0.0f, 1.0f));
                paintMaterial.SetVector("_randoms", randoms);

                TerrainPaintUtility.SetupTerrainToolMaterialProperties(indexCtx, brushXform, paintMaterial);
                Graphics.Blit(indexCtx.sourceRenderTexture, indexCtx.destinationRenderTexture, paintMaterial, 0);

                //  ctx.ScatterToTexture()          // we should do this ... less temp render textures
                // and users don't have to store render textures at all...
                indexCtx.Scatter(
                    TerrainToIndexMapRenderTexture,
                    blitMaterial, 0,
                    null,
                    null);

                normalCtx.Cleanup();
                indexCtx.Cleanup();
            }

            /*
             *          BrushTransform brushXform = TerrainPaintUtility.CalculateBrushTransform(terrain, editContext.uv, editContext.brushSize, 0.0f);
             *          PaintContext paintContext = TerrainPaintUtility.BeginPaintTexture(terrain, brushXform.GetBrushXYBounds(), m_SelectedTerrainLayer);
             *          if (paintContext == null)
             *              return false;
             *
             *          Material mat = TerrainPaintUtility.GetBuiltinPaintMaterial();
             *
             *          // apply brush
             *          float targetAlpha = 1.0f;       // always 1.0 now -- no subtractive painting (we assume this in the ScatterAlphaMap)
             *          Vector4 brushParams = new Vector4(editContext.brushStrength, targetAlpha, 0.0f, 0.0f);
             *          mat.SetTexture("_BrushTex", editContext.brushTexture);
             *          mat.SetVector("_BrushParams", brushParams);
             *
             *          TerrainPaintUtility.SetupTerrainToolMaterialProperties(paintContext, brushXform, mat);
             *
             *          Graphics.Blit(paintContext.sourceRenderTexture, paintContext.destinationRenderTexture, mat, (int)TerrainPaintUtility.BuiltinPaintMaterialPasses.PaintTexture);
             *
             *          TerrainPaintUtility.EndPaintTexture(paintContext, "Terrain Paint - Texture");
             */
            return(true);
        }