[API] Generating textures with alpha

Hi there,

we are developing an app with multi language support. So we are swapping some textures depending on the selected language. We are generating the textures on the fly by writing text to canvas elements, and then we generate the image textures. Works perfectly. But now we have a text which should have a transparent background. Generating the image works as expected, but when we generate a texture from the image, the background is black when we put it in an element. So I investigated the textures with transparency exported by shapespark and saw, that those textures seem to work with cutouts. So using simply createTextureFromHtmlImage won’t work here I guess.

Is there any way to generate textures from PNGs with alpha on the fly?

Right. The viewer API assumes the textures are opaque. Could you check if setting

texture.format = GLC.RGBA;
texture.hasAlpha = true;
texture.needsUpdate = true;

on the texture object returned by createTextureFromHtmlImage makes the transparency information transferred to the viewer?

var texture = viewer.createTextureFromHtmlImage(MY_IMAGE);
texture.format = GLC.RGBA;
texture.needsUpdate = true;

I tried this code above, but no success.

@Simon, I’ve updated the code in my previous post by adding one more statement: texture.hasAlpha = true;. This should work now.

In the next release we’ll add an additional argument to createTextureFromHtmlImage specifying whether the image has an alpha channel.

It works perfectly, thanks a lot!

Has anything changed about
“texture.format = GLC.RGBA;
texture.hasAlpha = true;
texture.needsUpdate = true;” recently?

An error occurred in the texture image conversion.

Hi @heung_aile

We have refactored our code and removed some functions visibility. GLC is now made private.

Official API for creating texture is Viewer.createTextureFromHTMLImage(image, hasAlpha=false), where setting ‘hasAlpha’ to true will set proper texture format.

I would recommend to use official API functions where possible.
However, if you would prefer to not change your code, you could replace “GLC.RGBA” directly with value for this property which is “0x1908”,

texture.format = 0x1908

1 Like

origin

Using the existing grammar using api changes the image, but the transparent part of the png file is broken and exposed

Hi

Have you set “hasAlpha” to true?

The function call should like this:

Viewer.createTextureFromHTMLImage(image, true)
var textureElement = document.getElementById(textureElementId);
      texture = viewer.createTextureFromHtmlImage(textureElement, ture);

Do i understand correctly??

This should work, but you have a typo. true not ture

2 Likes

I solved it. Thank you.

-Before-

texture = viewer.createTextureFromHtmlImage(textureElement);
texture.format = 0x1908;
texture.hasAlpha = true;
texture.needsUpdate = true;

-After-

“same code”

Hi!

How are you creating your textureElement?

Maybe your textureElement is missing property: textureElement.crossOrigin = "anonymous"?