I have a need where I need to have separate blending for alpha channel but its not working.
Let me explain with my test.
Alpha blending is enabled and I have set color and alpha write mask to true.
1) I am clearing my buffer with glClear(0,0,0,1)
2) I am drawing a big 2D quad which will have its color blend to half and will "set destination alpha values to zero" (-> this is what I need to achieve !)
Src Color is (1,1,1,0.5)
Here are the blend parameters I am setting
transparencyData.color_blendfunc = BlendInfo.BLEND_FUNC_ADD
transparencyData.alpha_blendfunc = BlendInfo.BLEND_FUNC_ADD
transparencyData.colorSrc_blendfactor = BlendInfo.BLEND_FACTOR_SRC_ALPHA
transparencyData.colorDst_blendfactor = BlendInfo.BLEND_FACTOR_ONE_MINUS_SRC_ALPHA
transparencyData.alphaSrc_blendfactor = BlendInfo.BLEND_FACTOR_ZERO
transparencyData.alphaDst_blendfactor = BlendInfo.BLEND_FACTOR_ZERO
3) I am drawing a smaller 2D quad inside the above quad
Src Color is (1,1,1,1)
Here are the blend parameters I am setting
transparencyData.color_blendfunc = BlendInfo.BLEND_FUNC_ADD
transparencyData.alpha_blendfunc = BlendInfo.BLEND_FUNC_ADD
transparencyData.colorSrc_blendfactor = BlendInfo.BLEND_FACTOR_ONE_MINUS_DST_ALPHA
transparencyData.colorDst_blendfactor = BlendInfo.BLEND_FACTOR_DST_ALPHA
transparencyData.alphaSrc_blendfactor = BlendInfo.BLEND_FACTOR_ONE
transparencyData.alphaDst_blendfactor = BlendInfo.BLEND_FACTOR_ZERO
Now while quad rendered by step 2 appears half blended, my desired need where it sets the final alpha value of the destination to zero is not happening.
How I know this is because the quad rendered in step 3 should appear if destination alpha is zero and will not appear if destination alpha is one.
This is making me believe that either the alpha blending which might be happening is not working and getting written to the destination or I am doing something not right !!!
I am using glBlendEquationSeparate and glBlendFuncSeparate for this purpose.
Can you please shed light and help me in what I am trying to do ?
Related
I am trying to assign a standardised color on each point on a 3d point cloud on load and the point cloud is represented with a Buffer Geometry using Three.js. The buffer geometry does not have an initial color attribute attached to it at load.
The issue I'm facing is that every time I reload the point cloud, the color of the point cloud changes even though the color is supposed to be hardcoded in. Even the rgb values in the color attribute array remained the same in the separate loads as seen in the following image.
Thus, I am wondering where the additional shade of color came from that causes my buffer geometry to appear differently from the way I coded it. Thank you for your help.
The following is the code I used to assign the color to the points on the point cloud:
if (!points.geometry.attributes.color) {
// set a default color attribute
const colorsArr = []
for (let i = 0, n = points.geometry.attributes.position.count; i < n; i++) {
colorsArr.push(0.69, 0.78, 1)
}
points.geometry.setAttribute(
'color',
new THREE.Float32BufferAttribute(colorsArr, 3, false)
)
}
points.geometry.attributes.color.needsUpdate = true
I need to sample the RGBA values of a texture, and I'm using the alpha channel to store custom data, not really to as opacity. The problem is that as alpha approaches 0, my RGB values also get multiplied towards 0, but I need them to remain independent.
My first render pass gets rendered into a WebGLRenderTarget, and this is how I'm storing data in GLSL into the alpha channel:
gl_FragColor.rgb = mainColor;
gl_FragColor.a = depthData;
the result shows Alpha fading to white as expected:
My second render pass gets rendered to the canvas, so I read the RGB values from from the first texture in GLSL, with alpha as 1:
gl_FragColor.rgb = texture2D(tColor, vUv).rgb;
gl_FragColor.a = 1.0;
The result should be the regular RGB values of the spheres, but it seems that the RGB values fade to black when Alpha approaches 0.
Is there a way to use the alpha channel as data storage without affecting the RGB values? I've tried setting the target texture's premultiplyAlpha = false as follows, but it doesn't change anything:
var target = new THREE.WebGLRenderTarget(
window.innerWidth,
window.innerHeight, {
format: THREE.RGBAFormat,
type: THREE.UnsignedByteType
});
target.texture.premultiplyAlpha = false;
Update:
I was building a working demo here, and I was unable to reproduce the bug. It looks like I'm going to have to rip apart my entire project until I get to the bottom of this...
I create a polygon on image_area in matlab.
I used impoly.
But after creation polygon.
I need to block possibility to move and drag impoly (ROI is already created).
I don't know how I should do it ?
I would appreciate for any help please.
You can set the makeConstrainToRectFcn such that it is a rectangle encompassing your ROI, then whenever you try to move the latter it won't work. You can also, after creating the ROI, set the setVerticesDraggable method to false in order to prevent vertices from being dragged.
Sample code (adapted from example by the Mathworks):
clc
clear
figure
imshow('gantrycrane.png');
h = impoly(gca, [188,30; 189,142; 93,141; 13,41; 14,29]);
%// Get currentposition
Pos = getPosition(h);
%// Prevent draggable vertices
setVerticesDraggable(h,0);
%// Set up rectangle to prvent movement of ROI
fcn = makeConstrainToRectFcn('impoly', [min(Pos(:,1)) max(Pos(:,1))], [min(Pos(:,2)) max(Pos(:,2))]);
%// Apply function
h.setPositionConstraintFcn(fcn);
which results in this kind of situation (with red rectangle for illustration):
I'm trying to combine the two images based on the information from the mask. I'm using the color information from the background image if the mask is 0 and color information from foreground image if the mask is 1. Because the mask and both
Images are of the same size, I would like to use logical indexing of matrices to achieve this.
My attempt:
mask = imread('mask.png');
foreground = imread('fg.jpg');
background = imread('bg.jpg');
[r,c,~]=size(mask);
A = zeros(size(mask));
for i=1:r
for j=1:c
if mask(i,j) == 0
A(i,j,:) = background(i,j,:);
end
if mask(i,j) > 0
A(i,j,:) = foreground(i,j,:);
end
end
end
imshow(A);
The result looks like a flickering blue image, but I don't want that. Please help.
You can do this a bit more concisely:
f = double(foreground).*double(mask);
b = double(background).*double(~mask);
blend = f+b;
imshow(blend, []);
Using logical indexing you could also do
foreground(logical(mask)) = 0;
background(logical(~mask)) = 0;
blend = foreground+background;
The ISNOT operator '~' inverts your matrix in the second line, so you cut out the area you would like for background.
NOTE: This works for black and white (one channel). For coloured images see rayryeng's solution.
There are two problems with your code. The first problem is that you are trying to assign colour pixels to the output image A, yet this image is only two-dimensional. You want an image with three channels, not two. In addition, the output image type you are specifying is wrong. By default, the output image A is of type double, yet you are copying values into it that aren't double... most likely unsigned 8-bit integer.
As such, cast the image to the same type as the input images. Assuming both input images are the same type, initialize your A so that:
A = zeros(size(foreground), class(foreground));
This correctly makes a colour image with the same type as any of the inputs, assuming that they're both the same type.
Now, your for loop is fine, but it's better if you do this in one shot with logical indexing. If you want to use logical indexing, create a new image that's initially blank like what you've done, but then make sure your mask has three channels to match the number of channels the other images have. After, you simply need to index into each image and set the right locations accordingly:
mask = imread('mask.png');
foreground = imread('fg.jpg');
background = imread('bg.jpg');
[r,c,d]=size(mask); %// Change
%// If your mask isn't three channels, make it so
%// Change
if d ~= 3
mask = cat(3, mask, mask, mask);
end
A = zeros(size(foreground), class(foreground)); %// Change
A(mask) = foreground(mask); %// Assign pixels to foreground
A(~mask) = background(~mask); %// Assign pixels to background
imshow(A);
Here is some minimal code to show an issue:
static const int MAX_WIDTH = 320;
static const int MAX_HEIGHT = 320;
Gdiplus::Bitmap foregroundImg(MAX_WIDTH,MAX_HEIGHT,PixelFormat32bppPARGB);
{
Gdiplus::Graphics g(&foregroundImg);
g.Clear(Gdiplus::Color(10,255,255,255));
}
Gdiplus::Bitmap softwareBitmap(MAX_WIDTH,MAX_HEIGHT,PixelFormat32bppPARGB);
Gdiplus::Graphics g(&softwareBitmap);
g.SetCompositingMode(Gdiplus::CompositingModeSourceOver);
g.SetCompositingQuality(Gdiplus::CompositingQualityDefault);
g.Clear(Gdiplus::Color(255,0,0,0));
g.DrawImage(foregroundImg,0,0);
CLSID encoder;
GetEncoderClsid(L"image/png",&encoder);
softwareBitmap.Save(L"d:\\image.png",&encoder);
As result I'm getting image filled by RGB values equals to 10. It seems GDI+ uses the conventional algorithm:
255*(10/255) + 0*(1-10/255) == 10.
But I'm expecting that premultiplied algorithm will be used (because foreground image has the premultiplied PixelFormat32bppPARGB format):
255 + 0*(1-10/255) == 255
So my question, why GDI+ uses conventional formula when image is in premultiplied alpha format? And is there any workaround to make GDI+ to use the premultiplied alpha algorithm?
The format of your foreground image doesn't matter (given that it has alpha) because you're setting it to a Gdiplus::Color. Color values are defined as non-premultiplied, so gdiplus multiplies the components by the alpha value when it clears the foreground image. The alternative would be for Color values to have different meaning depending on the format of the render target, and that way lies madness.
You might be able to do what you intend by setting the source image bits directly, or you might not. Components with values greater than 100% aren't really valid in gdiplus's rendering model, so I'd not be surprised if it caps them during rendering. If you really want this level of control over the rendering, you'll have to lock the bitmap bits and do it yourself.