WebGL semi-transparent appears white - opengl-es

I am having a problem with WebGL. Semi-transparent textures appear semi-transparent, but they're also getting white, color from the texture isn't considered while rendering...
That's what I've set:
gl.blendFunc(BlendingFactorSrc.SRC_ALPHA, BlendingFactorDest.ONE_MINUS_SRC_ALPHA);
What are the possible solutions?

Typically if white appears unexpectedly in a WebGL scene, it's blending in from the CSS background of the web page itself (which defaults to white unless you change it).
In other words, with the default WebGL settings, any shader that writes alpha values less than 1.0 to the color buffer will make the <canvas> itself become translucent and show the web page's background. I believe you can change the WebGLContextAttributes settings to disable this behavior, or just make sure your shaders always output alpha values of 1.0.

For me setting these WebGL context attributes solved the issue:
const gl = canvas.getContext('webgl', {
alpha: true,
premultipliedAlpha: false,
});
Without premultipliedAlpha: false WebGL rendered not fully transparent pixels as white, instead of having opacity like 0.1.
When WebGL premultipliedAlpha disabled, pixels rendered at proper opacity level.

Related

Setting Background of Viewport3DX to Transparent

I am using the Viewport3DX from Helix Toolkit only to Draw into the Content2D Element.
This works fine and very fast.
But the background is White and I would like to have an transparent background.
I can set BackgroundColor to another color but not to Transparent.
ALso the Background Property has no effect.
Is this normal issue is there an hidden option?
So now I know more.
With EnableSwapChainRendering = true the viewport uses HWND to render in the WPF control and therefore there's no transparency.
With EnableSwapChainRendering = false it renders to an D3DImage and transparency works.

WebGL rendering on Firefox - Light effects show darker than in Chrome

I have a scene where one light is present, and diamonds.
Light properties:
Point light, position: 0 0 30, intensity: 1, distance 60, color: White.
The diamonds material is Phong, color:Red, no emissive, specular: White, shininess 10.
On Chrome, the diamons shine as suppose to, but on Firefox the diamonds not shine at all, and looks very dark (like have something black on it).
I have tried to use both Firefox on desktop Windows and Android mobile phone.
I would like to ask what I am missing?
Below are the settings in my code:
// Renderer:
ren=new THREE.WebGLRenderer({ antialias:true,alpha:true });
ren.shadowMap.enabled=true;
elm.appendChild(ren.domElement); // the renderer is added to a DOM element "elm"
// Light
var o=new THREE.PointLight(0xffffff,1,60);
o.position.set(0,0,30);
o.name="sun"; // light will be later added to the scene, and use "update materials" flag to update the materials of the entire scene.
// The diamond's material: (I gave a new parameter "name", for later use. I guess it should not makes trouble to the engine....)
var mt=new THREE.MeshPhongMaterial({ name:"RedDiamond", transparent:true, opacity:0.85, fog:false, color:0xff0020, specular:0xffffff, shininess:10 });
Live example can see here: https://www.crazygao.com/VideoGames/Lamps, since the first level (loading may takes a bit time only for the first time, the opening scene though is yet not complete). The lighting difference issue can be seen even in the progress scene (with the flash one)
My question: What should I do to make the diamonds shine in Firefox, but not make the entire scene too bright in Chrome? (I tried adding Ambient light to the scene, then in Chrome it becomes too bright....)
Is the problem comes from my settings, or it is the nature of Firefox? What are the best steps I can take to solve this issue?
Thanks a lot
My guess is that you're missing that the webgl canvas is composited with the HTML behind it. By default the browser expects the values of the pixels in the canvas to represent premultiplied alpha values. That means there are many possible invalid colors
Example RGBA = 1,1,1,0
That's an invalid color because since alpha = 0 and multiplying by 0 = 0 then R, G, and B also have to be zero
When the values are invalid the results are undefined and so you'll get different results on different browsers
This answers covers some of the solutions.

Webgl canvas appears semi transparent over another canvas

I am trying to implement webgl for my game. The problem is I use multiple canvases, and my webgl enabled canvas is semi-transparent over my terrain canvas (which is drawn once and just moves with the player). Here is a picture:
I've searched for the past 2 hours on google and can't find anything that has helped.
I have tried:
getContext("experimental-webgl",{alpha: false});
This just hides the terrain completely (now all black), but my webgl drawn objects have the correct color.
gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false);
gl.pixelStorei(gl.UNPACK_COLORSPACE_CONVERSION_WEBGL, false);
Neither of these did anything noticeable.
gl.clearColor(0, 0, 0, 0)
Didn't affect the outcome, still looks like the screenshot above.
Everything else here: http://games.greggman.com/game/webgl-and-alpha/
Nothing seems to work. Why is what is drawn on the canvas semi transparent? There is no CSS affecting the canvas element.
Figured it out! I am using WebGL-2D, which is a javascript file that adds the context2D API to webGL. So if I call drawImage, it actually handles that with webgl. In the getContext definition I had to change:
gl.colorMask(1,1,1,0);
to
gl.colorMask(1,1,1,1);
The colorMask() method specifies whether red, green, blue, and alpha can or cannot be written into the frame buffer.
I have no idea why it was 0 before.

Three.js FXAA background transparency

Created a simple scene with a cube in it. Able to see the color of the containing element, (body), in the background.
Added an FXAA shader and the antialiasing works well. However the background is now black, so can no longer see the color of the background container.
Added the following code:
var target = new THREE.WebGLRenderTarget(512, 512);
var composer = new THREE.EffectComposer( renderer, target );
in order to set the effect composer render target format to THREE.RGBAFormat, rather than the default THREE.RGBFormat.
This makes the background work properly, but then there are black and white edges around the cube and the antialiasing does not look very good.
Repeated the above but used the Sepia shader instead of the FXAA shader. This works correctly. The cube looks sepia and the background containing element color is correct.
Are there any workarounds to allow antialiasing and transparent background?
Thanks for any help
I read your issue and there seems to be a good source that can solve or at least lead you down the right path. Go check out: https://github.com/mrdoob/three.js/issues/2125
Hope this helps.
Check my answer in https://stackoverflow.com/a/21056080/2482976
The FXAA needed to be updated to handle the transparent background

OpenGL blending renders black on black as gray?

I'm working in webGL. I'm pretty new to OpenGL. Having trouble with the blending function. My options look like:
gl.enable(gl.BLEND)
gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
I render a source rectangle with color [0,0,0,0.5] on top of a destination background with color [0,0,0,1]. Based on everything I've read, I expect the result to be black. Instead it looks to be about 25% white. Here's what I get when I render red and black rectangles with alpha values ranging from 0.0 to 1.0.
View live demo and source here. Am I misunderstanding the blending function, and if so, how do I get what I expect? Thanks!
You should specify separate function for alpha:
gl.enable(gl.BLEND);
gl.blendEquation(gl.FUNC_ADD);
gl.blendFuncSeparate(
gl.SRC_ALPHA,
gl.ONE_MINUS_SRC_ALPHA,
gl.ONE,
gl.ONE_MINUS_SRC_ALPHA
);

Resources