-- Max amount of panels/poles places
local amount = 200
-- Length between poles = length of 1 panel piece
local fenceLength = 2
-- Node in your scene that will be copied as panel
local panel = 61843
-- Node in your scene that will be copied as pole
local pole = 61839
-- Update the yOffset for the vertex offset shader with the angle
-- 0 or nil to disable
local offsetMultiplier = 2


if getNumSelected() ~= 1 then
    print("Select exactly 1 spline")
    return
end

function connectPoints(template, startX, startY, startZ, endX, endY, endZ)
    local splinePoints = {endX, endY, endZ,startX, endY, startZ}
    local newSpline = createSplineFromEditPoints(getRootNode(),splinePoints,true,false)
    local rX, rY, rZ = getSplineOrientation(newSpline,0,0,-1,0)
    delete(newSpline)

    local newFence = clone(template,false)
    link(getRootNode(),newFence)
    setWorldRotation(newFence, rX, rY, rZ)
    setWorldTranslation(newFence, startX,startY,startZ)
    return newFence
end

local spline = getSelection(0)
local spawnedObject = {}

local mSplineLength = getSplineLength(spline)
print("Spline length: " .. mSplineLength)
local fenceLengthTime = 1 / (mSplineLength / fenceLength)
print("Fence length: " .. fenceLengthTime)
local splinePos = 0.0


-- Spawn all Objects
for i=0,amount do
    if splinePos <= 1.0 and ((splinePos + fenceLengthTime) <= 1.0) then
        local x, y, z = getSplinePosition(spline,splinePos)
        local endX, endY, endZ = getSplinePosition(spline,(splinePos + fenceLengthTime))

        local panel = connectPoints(panel, x, y, z, endX, endY, endZ)
        if (offsetMultiplier ~= nil and offsetMultiplier ~= 0) then
            local heightDifference = (endY - y) * offsetMultiplier
            print("Height diff: " .. heightDifference)
            local shaderParameterName = "yOffset"
            local xOffset, yOffset, zOffset, wOffset = 0, 0, 0, 0
            xOffset = math.atan(heightDifference/fenceLength)
            print("Offset: " .. xOffset)
            print("Offset: " .. math.deg(xOffset) .. "deg")
            setShaderParameterRecursive(panel, shaderParameterName, xOffset, yOffset, zOffset, wOffset, false)
        end
        table.insert(spawnedObject, panel)

        local pole = connectPoints(pole, x, y, z, endX, endY, endZ)
        table.insert(spawnedObject, pole)

        if (splinePos + fenceLengthTime) >= 1 then
            local endPole = connectPoints(pole, endX, endY, endZ, x,y,z)
            table.insert(spawnedObject, endPole)
            i = amount + 1
        end

        splinePos = splinePos + fenceLengthTime
    end
end

-- Move Objects to parent Group
local parentGroup = createTransformGroup("fence")
link(getRootNode(),parentGroup)
setWorldTranslation(parentGroup, getSplinePosition(spline,0))

for i=1,#spawnedObject do
    local x, y, z = getWorldTranslation(spawnedObject[i])
    -- x, y, z = worldToLocal(parentGroup, x, y, z)
    link(parentGroup, spawnedObject[i])
    setWorldTranslation(spawnedObject[i], x, y, z)
end