hype / HYPE_Processing

HYPE for Processing

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

HMath.removeTempSeed() is not resetting usingTempSeed

mrd0ll4r opened this issue · comments

... which causes any subsequent calls to removeTempSeed to reset the seed to the one produced at the very start. See https://github.com/hype/HYPE_Processing/blob/lib_staging/src/main/java/hype/HMath.java#L442

Can you provide an example of what you are trying to achieve with HMath.removeTempSeed(), and why its not working?

Assume you're using a pair of HMath.tempSeed() and HMath.removeTempSeed() in your code. tempSeed will save a seed to return to later, but only on the first ever call made to it, because removeTempSeed doesn't reset usingTempSeed.

Concrete problem: I have a bunch of things I want to color with an HColorPool, randomly, but not-changing per frame. I take the index of the thing I'm coloring, multiply it by a large-ish constant to get some random mapping, then call HColorPool.getColor(int). Additionally, I'm using Perlin noise to do whatever, and want to re-seed the noise on a keypress. For that, I generate a random value with PApplet.random(Long.MAX_VALUE). But, because the random seed is not correctly reset in HMath, which is used by HColorPool.getColor(int), subsequent calls to seed the noise field will always use the same seed.

In the example, observe how pressing 'g' multiple times behaves differently based on whether or not the color pool is being used.

Code:

import hype.*;
import hype.extended.behavior.*;
import hype.extended.colorist.*;
import hype.extended.layout.*;

void setup() {
  size(640, 640);
  H.init(this).background(#C0C0C0);

  pool = new HDrawablePool(100).autoAddToStage().add(new HRect(10).anchorAt(H.CENTER).noStroke().fill(0xFF8736A3)).layout(new HGridLayout(10, 10).startLoc(100, 100).spacing(50, 50)).requestAll();

  c = new HColorPool(#713131,#317131,#313171);
}

private HColorPool c;
private boolean withColors;
private HDrawablePool pool;

void draw() {
  int i = 0;
  for (HDrawable o : pool) {
    // This is the issue: c.getColor(int) calls HMath.tempSeed with the given value, then resets the temp seed with HMath.removeTempSeed.
    // However, because removeTempSeed doesn't reset HMath.usingTempSeed, the next call to HMath.tempSeed will not overwrite the saved resetSeedValue.
    // This then makes any call to PApplet.random() afterwards deterministic.
    // In this case: Try to generate new noise seeds with coloring enabled.
    // Because the call to random() produces the same value for each frame drawn, well... you see.
    if (withColors)
      o.fill(c.getColor(i*541233));
    else
      o.fill(0xFF8736A3);

    o.size(50f*noise(o.x()*0.01f, o.y()*0.01f, frameCount*0.01f));

    i++;
  }

  H.drawStage();
}

void keyPressed() {
  switch (key) {
  case 'c':
    withColors = !withColors;
    break;
  case 'g':
    noiseSeed((long) random(Long.MAX_VALUE));
    break;
  }
}