Jump to content

can't use canvas antialiasing for DynamicTexture


mise
 Share

Recommended Posts

Hi! I am very new to Babylon. I am hoping to switch to Babylon for a game that's already well underway after trying two other engines.
I am really liking it so far!

For now my game is a lot of 2D and one of my requirements is nice text rendering (which I haven't found in the other engines).

https://playground.babylonjs.com/#O4OXG#16 

I want to render text on Planes like that, with dynamic backgrounds. This is how for I got with what I want (my camera will be straight facing and stationary) but as you can see if you zoom in, the canvas text's alpha values are mixed with black. The orange part is how I want it to look, but of course that is not a dynamic background.

How can I preserve those alpha values coming from the Canvas?

Thanks a lot!

Link to comment
Share on other sites

Nice one. With your help I figured it out: https://playground.babylonjs.com/#O4OXG#25
we paint the diffuse texture completely text color, which gets rid of the left over black pixels in your version.

I don't think you can get the orange rectangle in there without making more textures, but I think that is fine.

Thanks mate!

Link to comment
Share on other sites

I still think it's strange that you can't directly use the alpha values, at least not without also taking the black background pixels that have no business being there :).

I think you now need a texture layer for each color you want to draw on the canvas (at least if you want anti aliasing, which seems wise if you want any image quality).

Edit: Am I correct that hasAlpha doesn't work as expected? it seems to round alpha values to 1 or 0, that is why my original example looks so weird. Can someone explain why that is? That would also make it hard to display regular non-square sprites.

Link to comment
Share on other sites

5 hours ago, mise said:

Am I correct that hasAlpha doesn't work as expected?

In addition to what Sebavan has said, have a look at assigning an AdvancedDynamicTexture to a mesh 4th parameter:
https://github.com/BabylonJS/Babylon.js/blob/master/gui/src/2D/advancedDynamicTexture.ts#L867
You will see from the comment that on the variable 'onlyAlphaTesting' that  alpha testing will be used, but not alpha blending.  Also note how different textures are assigned to the material (ie: hasAlpha = true doesn't assign opacityTexture, but does assign diffuseTexture). HTH.

Link to comment
Share on other sites

7 hours ago, Sebavan said:

hasAlpha determines if alpha TEST is on not alpha Blend, alpha blend is defined by the presence of vertex alpha, opacity texture, alpha < 1 or the flag use alphaFromDiffuseTexture being true in the material. Hope that helps.

that is clear, thank you. useAlphaFromDiffuseTexture works well to create nice half alpha pixels, I definitely missed that.

I am still wondering where these part-black pixels are coming from then, now that the alpha is working.
updated example with useAlphaFromDiffuseTexture: https://playground.babylonjs.com/#O4OXG#26 (orange square is just there to make the black pixels more clear)

how I would like it to look, but without using opacityTexture (if this is possible of course!): https://playground.babylonjs.com/#O4OXG#25

I'm seriously loving Babylon so far and thanks for the quick responses.

 

Link to comment
Share on other sites

I am finding more reasons to figure out a way to do this, can anyone help?

Summary of what I want:

- draw to Canvas context, text and shapes of varying colors
- use texture in such a way that both color info and partial alpha values are preserved
- without having leftover color values that don't belong (like black values in above example)
- without using multiple Planes (this is how I do it now, but if you stack them too close strange things happen)

Thank you so much for helping.

Link to comment
Share on other sites

Main issue is it is not only partial alpha you are getting but color information from the antialiasing technique used in canvas 2D so either the background needs to be the color of the drawn objects or you need to use only the alpha info without the color.

Last possibilities would be to try disabling anti aliasing for the text in the canvas 2D (if it is possible  ???) and use your own AA technique when you are drawing the text.

Link to comment
Share on other sites

23 minutes ago, Sebavan said:

Main issue is it is not only partial alpha you are getting but color information from the antialiasing technique used in canvas 2D so either the background needs to be the color of the drawn objects or you need to use only the alpha info without the color.

Last possibilities would be to try disabling anti aliasing for the text in the canvas 2D (if it is possible  ???) and use your own AA technique when you are drawing the text.

The Canvas is transparent by default (https://stackoverflow.com/questions/4815166/how-do-i-make-a-transparent-canvas-in-html5), I confirmed this by right click saving and I got a transparent png. So I still don't understand where this extra color information comes from (my guess would be from the code that creates the texture from the Canvas). The way I see it, the canvas has alpha and color info and I want both of those values, so I'm not sure what you mean by 'you need to use only the alpha info without the color'.

Maybe that is the solution though. Create a canvas, save it to a image element, and then use that to create a texture. quite messy, but it just might work.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...