Author Topic: z0w0's Perlin Noise Implementation is broken - brian makes new perlin noise  (Read 2686 times)

It is, i dont know how or why, but it is. I took a picture for your amusement.


I know this is his script because I tested it out on other scripts that use Perlin Noise, and got the same error, when it isn't infinite this will take the form of "that one odd line" that is completely off (and i believe) opposite.

to z0w0,
fix it. i know you've done it before and you probably can do it again, because you'll need to. This is the only public Perlin Noise Script and i can imagine people getting frustrated from it.

to everyone else,
if this isn't fixed soon i release an implementation i made.
« Last Edit: November 29, 2012, 04:51:50 PM by Brian Smithers »

function Perlin::onAdd(%this)
{
   %list = "151\t160\t137\t91\t90\t15\t131\t13\t201\t95\t96.... Etc, I'll pm u this if u ask me";
   for(%i=0;%i<256;%i++)
   {
      %field = getField(%list,%i);
      
      if(%field $= "")
         continue;
      
      %this.p[%i] = %field;
      %this.p[256 + %i] = %field;
   }
}
function Perlin::fade(%this,%t)
{
   return %t * %t * %t * (%t * (%t * 6 - 15) + 10);
}
function Perlin::lerp(%this,%t,%a,%b)
{
   return %a + %t * (%b - %a);
}
function Perlin::grad(%this,%hash,%x,%y,%z)
{
   %h = %hash & 15;
   %u = %h < 8 ? %x : %y;
   %v = %h < 4 ? %y : %h == 12 || %h == 14 ? %x : %z;
   
   return ((%h & 1) == 0 ? %u : -%u) + ((%h & 2) == 0 ? %v : -%v);
}
function Perlin::noise(%this,%x,%y,%z)
{
   %floorX = ~~%x;
   %floorY = ~~%y;
   %floorZ = ~~%z;

   %xb = %floorX & 255;
   %yb = %floorY & 255;
   %zb = %floorZ & 255;

   %x -= %floorX;
   %y -= %floorY;
   %z -= %floorZ;

   %xMinus1 = %x - 1;
   %yMinus1 = %y - 1;
   %zMinus1 = %z - 1;

   %u = %this.fade(%x);
   %v = %this.fade(%y);
   %w = %this.fade(%z);

   %A = %this.p[%xb] + %yb;
   %AA = %this.p[%A] + %zb;
   %AB = %this.p[%A + 1] + %zb;
   %B = %this.p[%xb + 1] + %yb;
   %BA = %this.p[%B] + %zb;
   %BB = %this.p[%B + 1] + %zb;

   return %this.lerp
   (
      %w,
      %this.lerp
      (
         %v,
         %this.lerp(%u,%this.grad(%this.p[%AA],%x,%y,%z),%this.grad(%this.p[%BA],%xMinus1,%y,%z)),
         %this.lerp(%u,%this.grad(%this.p[%AB],%x,%yMinus1,%z),%this.grad(%this.p[%BB],%xMinus1,%yMinus1,%z))
      ),
      %this.lerp
      (
         %v,
         %this.lerp(%u,%this.grad(%this.p[%AA + 1],%x,%y,%zMinus1),%this.grad(%this.p[%BA + 1],%xMinus1,%y,%z - 1)),
         %this.lerp(%u,%this.grad(%this.p[%AB + 1],%x,%yMinus1,%zMinus1),%this.grad(%this.p[%BB + 1],%xMinus1,%yMinus1,%zMinus1))
      )
   );
}
it was fixed
thanks z0w0
(it was actually in my history, he told me to forget off, but still thanks)

Edit: still broken, just less broken
« Last Edit: November 25, 2012, 11:06:03 PM by Brian Smithers »

holy loving stuff dude, I'm not helping you until you truncate that list or remove it from tt tags.

holy loving stuff dude, I'm not helping you until you truncate that list or remove it from tt tags.
Uh ok

Btw it's still broken guys :(

if this isn't fixed soon i release an implementation i made.
DUN DUN DUNNNN

this sounds like you're blackmailing z0w0

"FIX IT OR ELSE I RELEASE MINE"

DUN DUN DUNNNN

this sounds like you're blackmailing z0w0

"FIX IT OR ELSE I RELEASE MINE"

I'm not trying to sound like that I'm just saying that just in case he doesn't fix it I have one that's not as fast but doesn't have this bug

Why aren't you on steam? I wrote an extremely fast seeded RNG for you and you disappeared.

I would also like to see this fixed


What does the ~~ do?
It floors the value with double bitwise not. Read the variable names.
« Last Edit: November 27, 2012, 04:48:39 PM by dotdottriangle »

it was fixed
thanks z0w0
(it was actually in my history, he told me to forget off, but still thanks)

Edit: still broken, just less broken
So it was updated? What do you mean by this?

So it was updated? What do you mean by this?
it was updated
but it's still broke

Post the code you're using to generate the bricks based on the Perlin object. It's a direct port of Ken Perlin's improved noise code -- you might be using it wrong, or there's something wrong with the port.

EDIT: Here. I haven't tested it because I don't have Blockland installed, but if it has syntax errors I'm sure you can fix it yourself. If it doesn't work, you're using it wrong or it is an issue with TorqueScript's number support.

Code: [Select]
// # Perlin Noise
// <http://mrl.nyu.edu/~perlin/noise>

%list = "151\t160\t137\t91\t90\t15\t131\t13\t201\t95\t96\t53\t194\t233\t7\t225\t140\t36" TAB
        "103\t30\t69\t142\t8\t99\t37\t240\t21\t10\t23\t190\t6\t148\t247\t120\t234\t75\t0" TAB
        "26\t197\t62\t94\t252\t219\t203\t117\t35\t11\t32\t57\t177\t33\t88\t237\t149\t56" TAB
        "87\t174\t20\t125\t136\t171\t168\t68\t175\t74\t165\t71\t134\t139\t48\t27\t166" TAB
        "77\t146\t158\t231\t83\t111\t229\t122\t60\t211\t133\t230\t220\t105\t92\t41\t55" TAB
        "46\t245\t40\t244\t102\t143\t54\t65\t25\t63\t161\t1\t216\t80\t73\t209\t76\t132" TAB
        "187\t208\t89\t18\t169\t200\t196\t135\t130\t116\t188\t159\t86\t164\t100\t109" TAB
        "198\t173\t186\t3\t64\t52\t217\t226\t250\t124\t123\t5\t202\t38\t147\t118\t126" TAB
        "255\t82\t85\t212\t207\t206\t59\t227\t47\t16\t58\t17\t182\t189\t28\t42\t223\t183" TAB
        "170\t213\t119\t248\t152\t2\t44\t154\t163\t70\t221\t153\t101\t155\t167\t43\t172" TAB
        "9\t129\t22\t39\t253\t19\t98\t108\t110\t79\t113\t224\t232\t178\t185\t112\t104" TAB
        "218\t246\t97\t228\t251\t34\t242\t193\t238\t210\t144\t12\t191\t179\t162\t241" TAB
        "81\t51\t145\t235\t249\t14\t239\t107\t49\t192\t214\t31\t181\t199\t106\t157\t184" TAB
        "84\t204\t176\t115\t121\t50\t45\t127\t4\t150\t254\t138\t236\t205\t93\t222\t114" TAB
        "67\t29\t24\t72\t243\t141\t128\t195\t78\t66\t215\t61\t156\t180";

// Store permutation array from the precomputed permutations.

for(%i = 0; %i < 256; %i++) {
    %field = getField(%list, %i);
    $Perlin::P[%i] = %field;
    $Perlin::P[256 + %i] = %field;
}

// ### Perlin.fade(%t)
// Compute the fade curve at `%t`.
function Perlin::fade(%this, %t) {
    return %t * %t * %t * (%t * (%t * 6 - 15) + 10);
}

// ### Perlin.lerp(%t, %a, %b)
// Linear interpolation at time `%t` from `%a` to `%b`.
function Perlin::lerp(%this, %t, %a, %b) {
    return %a + %t * (%b - %a);
}

// #### Perlin.grad(%hash, %x, %y, %z)
// Compute the gradient based on the direction at point `(%x, %y, %z)` stored in `%hash`.
function Perlin::grad(%this, %hash, %x, %y, %z) {
    // Convert lower 4 bits of hash code into 12 gradient directions.
    %h = %hash & 15;
    %u = %h < 8 ? %x : %y;
    %v = %h < 4 ? %y : %h == 12 || %h == 14 ? %x : %z;

    return ((%h & 1) == 0 ? %u : -%u) + ((%h & 2) == 0 ? %v : -%v);
}

// #### Perlin.noise(%x, %y, %z)
// Compute the noise value at `(%x, %y, %z)` in the range [-1, 1].
function Perlin::noise(%this, %x, %y, %z) {
    // Find the unit cube that contains the point.
    %X = mFloor(%x) & 255;
    %Y = mFloor(%y) & 255;
    %Z = mFloor(%z) & 255;

    // Find relative point within that cube.
    %x -= mFloor(%x);
    %y -= mFloor(%y);
    %z -= mFloor(%z);

    // Compute fade curves.
    %u = %this.fade(%x);
    %v = %this.fade(%y);
    %w = %this.fade(%z);

    // Hash coordinates of the 8 cube corners.
    %A = $Perlin::P[%X] + %Y;
    %AA = $Perlin::P[%A] + %Z;
    %AB = $Perlin::P[%A + 1] + %Z;
    %B = $Perlin::P[%X + 1] + %Y;
    %BA = $Perlin::P[%B] + %Z;
    %BB = $Perlin::P[%B + 1] + %Z;

    // Finally, blend the results of the 8 corners of the cube.
    return %this.lerp(%w, %this.lerp(%v, %this.lerp(%u, %this.grad($Perlin::P[%AA], %x, %y, %z),
               %this.grad($Perlin::P[%BA], %x - 1, %y, %z)),
           %this.lerp(%u, %this.grad(%p[%AB], %x, %y - 1, %z),
               %this.grad($Perlin::P[%BB], %x - 1, %y - 1, %z))),
           %this.lerp(%v, %this.lerp(%u, %this.grad($Perlin::P[%AA + 1], %x, %y, %z - 1),
               %this.grad($Perlin::P[%BA + 1], %x - 1, %y, %z - 1)),
           %this.lerp(%u, %this.grad($Perlin::P[%AB + 1], %x, %y - 1, %z - 1),
               %this.grad($Perlin::P[%BB + 1], %x - 1, %y - 1, %z - 1))));
}
« Last Edit: November 29, 2012, 05:30:00 AM by Destiny/Zack0Wack0 »

this has the same bug as the one before the fix. Mine outputs weird stuff which im working on. But uh hmm

Turns out, the entire thing is based around how you round xyz.
change the floor to ~~ and see it have 1 bug, put back to floor have the other. i really dont even know what to do now lol.
« Last Edit: November 29, 2012, 04:19:40 PM by Brian Smithers »


to happy people enjoying bug free terrain using Brian Smith's home made snake perlin noise :)