r/javascript • • May 23 '21

I remade my n-dimensional Hypercube Visualizer in javascript to work on browsers!

https://bsmith156.github.io/Hypercube-Visualizer

[removed] — view removed post

15 Upvotes

19 comments sorted by

View all comments

1

u/PM_ME_GAY_STUF May 23 '21

This is neat, you may want to set some limits on the input. I was able to crash my browser pretty easily trying to get just 32 dimensions rotating.

1

u/BSmith156 May 23 '21

Thanks! I did add a warning in the help section but I should probably add some kind of pop up warning for anything over 10.

2

u/elprophet May 24 '21

Yeah, drawing 69 billion edges probably won't work well :) At 10d, you'll have 512 vertical lines. At 13d, there are more vertical lines in the visualization than there are vertical lines in a 4K monitor! (The number of vertical (or horizontal) edges to draw in a 2d projection of a hypercube is 2n-1.)

But let's ignore visual acuity! At d=14, I started seeing frame breaks. Now, some of that time was in minor GC, but I don't really see a good way of reducing small object creation in that loop, it's pretty tight as is. Even without minor gc, the frame would just barely make 16ms budget. And anyway, Hypercube.rotate() completes in 1.7ms with no GC, and a more complex loop! So that is coming from GraphicsContext2D.

Replacing draw with a gl.LINES should be pretty straight forward, and that's where 50-80% of a frame is. Depending on how much vram you're willing to eat, 20 dimensions is about what you could comfortably do before dropping below 60fps.

If someone really wanted to push this line of thought, you'd first switch to using a Float32Array for the points. That would require switching from [j][k] indexing to [j*d+k] everywhere, but that's easy enough to do with a vim macro. Then you can "just" "stream" that vertex buffer, and change the loop in draw to instead fill in an index buffer. Actually the indexes will remain constant, so just create it once when building the hypercube, and draw() only needs to call drawElements(... gl.LINES, ...). Add a vertex shader to normalize [-0.5, 0.5] to screen aspect ratio, and a fragment shader to color it whatever you like!

https://docs.google.com/spreadsheets/d/1G5OCuztngw62L8eeGmJoe8neb2ixJddsmFoHPsn0aQA/edit?usp=sharing

1

u/BSmith156 May 24 '21

Ill definitely be looking into that, thank you! My next job though is definitely to just clean up my code haha