BitmapData.noise() not working on target html5

The method noise of BitmapData does not seams to work with html5 targets.
After hours passed trying to find a fault in my code, the following test convinced me that the problem must be in openFl.

class Main extends Sprite {
	public function new () {
		super ();
        var noise = new Bitmap();
        noise.bitmapData = new BitmapData(50,50);
        noise.bitmapData.noise(42);
        noise.scaleX = 6;noise.scaleY = 6;
        addChild(noise);
	}
}

html5 on the left, neko on the right.

The lone red square at (0,0) intrigued me. With some help from chrome we can see that there is something wrong with the rand() function that goes to 0 after three calls.

As a workout, I replaced the rand function from BitmapData.hx by an other one, but a given seed doesn’t give me the same pattern on neko and html5

var rand:Void->Int = {
			function func():Int 
			{	
				//randomSeed = randomSeed * 1103515245 + 12345;
				//return Std.int(Math.abs(randomSeed / 65536)) % 32768;
				return randomSeed = (randomSeed * 16807) % 2147483647;
			}
		};
1 Like

I’m no javascript expert, so I did not see it from the start, but I did a little more digging and the problem seems related to overflow behavior in haxe. (https://haxe.org/manual/types-overflow.html)
The original random generator, being coded in C, expect randomSeed to overflow witch doesn’t happen in javascript. Instead it grows so big that 32768 is no longer significant and modulus 32768 equals to 0.
Changing randomSeed from Int to Int32 make it work.

//public function noise (randomSeed:Int, low:Int = 0, high:Int = 255, channelOptions:Int = 7, grayScale:Bool = false):Void {
public function noise (randomSeed:haxe.Int32, low:Int = 0, high:Int = 255, channelOptions:Int = 7, grayScale:Bool = false):Void {

I would submit a pull request, but considering the computational cost of Int32 mention in the haxe documentation, I’m not sure if Int32 should be used as is or if the overflow behavior should be manually added in noise() on target that don’t overflow.

2 Likes

I was preparing a pull request, but I decided to take a look at perlinNoise which is also “broken” (for all non-flash target this time, not only html5) before submitting it.
I’m really puzzled by the perlinNoise implementation, it disregard and discard half of the parameters it get with no warning about this behavior neither in the doc, nor in comments.
I understand that it provide a “truer” perlin noise than the one in flash, but I think consistency across target is an important goal. If it can’t be achieved it should at lest be documented.

1 Like

Totally agree. fractalNoise and offsets parameters are completely unused in BitmapData.perlinNoise. I was using a lightning class that I found online that works great in Flash, but doesn’t move at all because the offsets parameter was ignored and therefore returns the same bitmap values every time.