我试图对表值进行算术运算并继续出错。这是我的代码

时间:2015-09-18 01:40:33

标签: lua

我正在尝试对表值执行算术并继续收到错误。这是我的总代码。我基本上试图生成单纯形噪声。我创建了一个多维数组(表),并尝试对值执行操作,但我不断收到一个错误,表示我无法对表值执行算术运算。我不知道如果我必须把它转换成什么或什么。请帮助。

totalNoiseMap = {}

function simplex_noise(width, height)
   simplexnoise = {}
   for i = 1,512 do
       simplexnoise[i] = {}
       for j = 1, 512 do
           simplexnoise[i][j] = 0
       end
   end
   frequency = 5.0 / width

   for x = 1, width do
       for y = 1, height do
           simplexnoise[x][y] = noise(x * frequency,y * frequency)
           simplexnoise[x][y] = (simplexnoise[x][y] + 1) / 2
       end
   end

   return simplexnoise
end

function noise(x, y, frequency)
    return simplex_noise(x / frequency, y / frequency)
end

function generateOctavedSimplexNoise(width,height,octaves,roughness,scale)
    totalnoise = {}
    for i = 1,512 do
        totalnoise[i] = {}
        for j = 1, 512 do
            totalnoise[i][j] = 0
        end
    end
    layerFrequency = scale
    layerWeight = 1
    weightSum = 0

    for octave = 1, octaves do
        for x = 1, width do
            for y = 1, height do
            totalnoise[x][y] = (totalnoise[x][y] + noise(x * layerFrequency,y * layerFrequency, 2) * layerWeight)

            end
        end

        --Increase variables with each incrementing octave
        layerFrequency = layerFrequency * 2
        weightSum = weightSum + layerWeight
        layerWeight = layerWeight * roughness

    end
    return totalnoise
end

totalNoiseMap = generateOctavedSimplexNoise(512, 512, 3, 0.4, 0.005)

1 个答案:

答案 0 :(得分:0)

totalnoise[x][y] + noise(x * layerFrequency,y * layerFrequency, 2) * layerWeight

您可以在此处获得表格noise(x * layerFrequency,y * layerFrequency, 2),通过标量layerWeight对其进行多处理,然后将其添加到标量totalnoise[x][y]

我可以想到如何将表乘以标量 - 它应该像

for i = 1,512 do
   for j = 1,512 do
       a[i][j] = t[i][j] * scalar
   end
end

但是我无法通过添加来获得你想要做的事情。假设应该添加两个表

for i = 1,512 do
   for j = 1,512 do
       a[i][j] = b[i][j] + c[i][j]
   end
end

但它仅适用于相同大小的表