p5.js-website icon indicating copy to clipboard operation
p5.js-website copied to clipboard

"Passing Shader Uniforms" example

Open JetStarBlues opened this issue 4 years ago • 12 comments

Actual Behaviour

In the "Passing Shader Uniforms" example, the generated image is off-center.

su_current

Expected Behaviour

Image is centered.

su_intended

I think somewhere along the way, the original values got lost.

Would you like to work on the issue?

Yes

JetStarBlues avatar Apr 22 '21 16:04 JetStarBlues

@JetStarBlues What browser are you using? Everything looks correct on my screen.

Screen Shot 2021-04-22 at 10 21 09 AM

aferriss avatar Apr 22 '21 17:04 aferriss

Interesting!

I tried it on Windows 10

  • Chrome Version 90.0.4430.85 (Official Build) (64-bit) su_ss_chrome-90 0 4430 85-64bit

  • Firefox 88.0 (64-bit) su_ss_firefox-88 0-64bit

I got similar results on Ubuntu 20.04

  • Chrome - Version 90.0.4430.72 (Official Build) (64-bit)
  • Firefox - 87.0 (64-bit)

JetStarBlues avatar Apr 22 '21 17:04 JetStarBlues

@aferriss Hey, the screenshot you posted failed to upload... Also curious which browser you were using.

JetStarBlues avatar Apr 27 '21 16:04 JetStarBlues

@JetStarBlues Oops! Re-uploaded. I was using chrome 90, but I also checked in firefox (v88) and safari (v14.03) and it was looking fine in all of those as well. I'm on OSX 10.15 (Catalina)

aferriss avatar Apr 27 '21 17:04 aferriss

(visible confusion)
If you are running on a 64-bit machine, then the only difference is the OS...

I tried opening the site on my phone (Android) on both Chrome and Firefox, and the current code looks slightly off-center... su_ss_chrome_android

JetStarBlues avatar Apr 27 '21 19:04 JetStarBlues

@aferriss Curious, how do the following look on you screen (I do not have access to a Mac for testing)

Thanks for your time!

JetStarBlues avatar Apr 27 '21 19:04 JetStarBlues

  • the matrix aware version renders properly centered on my phone

JetStarBlues avatar Apr 27 '21 20:04 JetStarBlues

Hmm, I'm seeing very different things than you are.

Screen Shot 2021-04-27 at 1 49 14 PM Screen Shot 2021-04-27 at 1 49 07 PM

We can skirt the issue entirely by not relying on the resolution values to calculate the shape's position. Here's an updated example that should work the same on every platform

https://editor.p5js.org/aferriss/sketches/uxphKm8AK

aferriss avatar Apr 27 '21 20:04 aferriss

We can skirt the issue entirely by not relying on the resolution values to calculate the shape's position

Oh, I see! Thank you for pinpointing the problem! 🎉🎉

I'm seeing very different things than you are.

I was expecting the results you got. (The matrix-aware version centers correctly on your screen.) But I was mistaken as to why! (Has nothing to do with use of matrix, but rather use of resolution independent calculations as you have pointed out!)

JetStarBlues avatar Apr 27 '21 22:04 JetStarBlues

The new version of the code only works with a 1:1 aspect ratio. Otherwise, the shape is stretched.

su_stretch

  • One possible solution is to change the canvas size used in the example from the default of 710x400 to 400x400.

  • Another possible solution is to tweak the code to consider the canvas's aspect ratio and use it to scale the uv coordinates. This approach however, has the downside of not working well with matrix-aware coordinates.

  // move the coordinates to where we want to draw the shape
  vec2 pos = vec2(x,y) - coord;
  
  // force 1:1 ratio...
  float ax = resolution.x / resolution.y;
  float ay = resolution.y / resolution.x;
  if ( ax > ay )
  {
    pos.x *= ax;
  }
  if ( ay > ax )
  {
    pos.y *= ay;
  }

JetStarBlues avatar Apr 28 '21 00:04 JetStarBlues

I have update the linked PR accordingly.

JetStarBlues avatar Apr 28 '21 00:04 JetStarBlues

Ah yea, good catch on the scaling!

aferriss avatar Apr 28 '21 16:04 aferriss