Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Normalizing coordinates in shaders #341

Closed
schiller-manuel opened this issue Apr 3, 2022 · 8 comments · Fixed by #342
Closed

Normalizing coordinates in shaders #341

schiller-manuel opened this issue Apr 3, 2022 · 8 comments · Fixed by #342
Assignees
Labels
question Further information is requested

Comments

@schiller-manuel
Copy link
Contributor

schiller-manuel commented Apr 3, 2022

Can you please explain why and how coordinates in shaders must be normalized?

I looked at the example here:

https://shopify.github.io/react-native-skia/docs/shaders/overview#simple-shader

const source = Skia.RuntimeEffect.Make(`
vec4 main(vec2 pos) {
  vec2 normalized = pos/vec2(256);
  return vec4(normalized.x, normalized.y, 0.5, 1);
}`)!;

It produces the following output:
Simulator Screen Shot - iPhone 13 - 2022-04-03 at 20 53 08

Without normalizing, it produces:
Simulator Screen Shot - iPhone 13 - 2022-04-03 at 20 53 25

Where does the 256 come from?

If I want to fully fill the screen, should pos.x be divided by screen width and pos.y be divided by screen height?
This produces a uniformly distributed gradient:

Simulator Screen Shot - iPhone 13 - 2022-04-03 at 20 57 49

If my canvas is not fullscreen, where do I get its dimensions from for normalization?

@chrfalch
Copy link
Collaborator

chrfalch commented Apr 4, 2022

Great question, @schiller-manuel - will get back to you soon with a quick example and some pointers!

@chrfalch chrfalch self-assigned this Apr 4, 2022
@chrfalch chrfalch added the question Further information is requested label Apr 4, 2022
@wcandillon
Copy link
Collaborator

256 is the width and height of the documentation examples. when doing pos/256 is equivalent to pos/vec2(width, height), the goal behind to get a value that goes from 0 to 1

@chrfalch
Copy link
Collaborator

chrfalch commented Apr 4, 2022

Another way to solve this which is common when writing shader code is to provide the resolution x/y as uniforms to the shader.

@schiller-manuel
Copy link
Contributor Author

OK, how can I get the canvas size in case it's not fullscreen?
e.g. if layout is calculated using flexbox, the size is not explicitly given.

@schiller-manuel
Copy link
Contributor Author

schiller-manuel commented Apr 4, 2022

There once were intrinsics in skia that provided access to width and height:
google/skia@d3a1df8
Unfortunately, those were ultimately removed: https://skia-review.googlesource.com/c/skia/+/394898/2

I tried using the onLayout event to measure the Canvas, but it is never called.
Do you have any other ideas how I can access the Canvas dimensions?

@wcandillon
Copy link
Collaborator

Haha thks for pointing us to these commits :))

We do have a useCanvas() hooks that will provide you the width and height.
This is indeed a very common use case.
This needs to be slightly cleaned up and documented, will do it now.

@wcandillon
Copy link
Collaborator

I just had a look and it looks like this will need further discussion with @chrfalch so we agree on the final form of this and document it.

@chrfalch
Copy link
Collaborator

chrfalch commented Apr 5, 2022

Sounds like a plan!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants