Nature Render targeted terrain layer

using nature renderer trying to apply a detail to specific terrain layer, not just whole terrain. Anyone ever do this?
this script is almost working but there is an x and z offset of about 2f x and 1 f z. Tried some other methods trying to scale alphamap resolution to terrain resolution but it failed badly. This is the closest I have come up with

using UnityEngine;

public class GrassRenderer : MonoBehaviour
{

    public Terrain terrain;
    public int density = 1; // Density for the detail layer.
    public int targetTextureLayer = 3; // Target the 4th texture layer (index 3).
    public int detailPrototypeIndex = 0; // The detail prototype to paint.


    // Define delegates for layer operations
    public delegate int GetLayerCount();
    public delegate string GetLayerName(int index);

    // Delegate instances
    public GetLayerCount getLayerCount;
    public GetLayerName getLayerName;

    private void Start()
    {
        terrain = GetComponent<Terrain>();
        var terrainData = terrain.terrainData;

        if (terrainData == null || targetTextureLayer >= terrainData.alphamapLayers)
        {
            Debug.LogWarning("Invalid terrain or target texture layer index.");
            return;
        }

        // Get detail resolution
        int width = terrainData.detailResolution;
        int height = terrainData.detailResolution;

        // Prepare the detail array
        int[,] details = terrainData.GetDetailLayer(0, 0, width, height, detailPrototypeIndex);

        // Access the alpha map for the target texture layer
        float[,,] alphaMap = terrainData.GetAlphamaps(0, 0, width, height);

        for (int y = 0; y < height; y++)
        {
            for (int x = 0; x < width; x++)
            {
                // Paint details only where the target texture layer is present
                if (alphaMap[x, y, targetTextureLayer] > 0.5f) // Adjust the threshold as necessary
                {
                    details[x, y] = density;
                }
                else
                {
                    details[x, y] = 0; // Clear details where the texture layer is not present
                }
            }
        }

        // Apply the detail layer
        terrainData.SetDetailLayer(0, 0, detailPrototypeIndex, details);
    }
}

if it helps anyone. issue is scale difference of detail texture and terrain texture. unless they match like at 2048 or lower rendering won’t match up. Using top settings of 4032 and 4096 have to scale and it still doesn’t come out quite right. But you can render to terrain layer you want, remove script and clean it up. This is what I used.

using UnityEngine;

public class GrassRenderer : MonoBehaviour
{
    public Terrain terrain;
    public int density = 1; // Density for the detail layer.
    public int targetTextureLayer = 3; // Target the 4th texture layer (index 3).
    public int detailPrototypeIndex = 0; // The detail prototype to paint.

    private void Start()
    {
        terrain = GetComponent<Terrain>();
        var terrainData = terrain.terrainData;

        if (terrainData == null || targetTextureLayer >= terrainData.alphamapLayers)
        {
            Debug.LogWarning("Invalid terrain or target texture layer index.");
            return;
        }

        // Get resolutions
        int detailWidth = terrainData.detailResolution;
        int detailHeight = terrainData.detailResolution;
        int controlWidth = terrainData.alphamapResolution;
        int controlHeight = terrainData.alphamapResolution;

        // Scale factor between detail and control texture
        float scaleX = (float)controlWidth / detailWidth;
        float scaleY = (float)controlHeight / detailHeight;

        // Prepare the detail array
        int[,] details = terrainData.GetDetailLayer(0, 0, detailWidth, detailHeight, detailPrototypeIndex);

        // Access the alpha map for the target texture layer
        float[,,] alphaMap = terrainData.GetAlphamaps(0, 0, controlWidth, controlHeight);

        for (int y = 0; y < detailHeight; y++)
        {
            for (int x = 0; x < detailWidth; x++)
            {
                // Map detail coordinates to control texture coordinates
                int controlX = Mathf.FloorToInt(x * scaleX);
                int controlY = Mathf.FloorToInt(y * scaleY);

                // Paint details only where the target texture layer is present
                if (alphaMap[controlX, controlY, targetTextureLayer] > 0.5f) // Adjust the threshold as necessary
                {
                    details[x, y] = density;
                }
                else
                {
                    details[x, y] = 0; // Clear details where the texture layer is not present
                }
            }
        }

        // Apply the detail layer
        terrainData.SetDetailLayer(0, 0, detailPrototypeIndex, details);

        Debug.Log("Detail layer scaled and applied successfully.");
    }
}