Jump to content
  • Advertisement
Sign in to follow this  

DX11 Transparency Blending in Directx 11?

This topic is 959 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Trying to set up a floor with transparency but not getting transparency.

Here is my code so far.

float4 PS(VertexOut pin, 
          uniform int gLightCount, 
		  uniform bool gUseTexure, 
		  uniform bool gAlphaClip, 
		  uniform bool gFogEnabled, 
		  uniform bool gReflectionEnabled) : SV_Target
	// Interpolating normal can unnormalize it, so normalize it.
    pin.NormalW = normalize(pin.NormalW);

	// The toEye vector is used in lighting.
	float3 toEye = gEyePosW - pin.PosW;

	// Cache the distance to the eye from this surface point.
	float distToEye = length(toEye);

	// Normalize.
	toEye /= distToEye;
    // Default to multiplicative identity.
    float4 texColor = float4(1, 1, 1, 1);
		// Sample texture.
		texColor = gDiffuseMap.Sample( samAnisotropic, pin.Tex );

			// Discard pixel if texture alpha < 0.1.  Note that we do this
			// test as soon as possible so that we can potentially exit the shader 
			// early, thereby skipping the rest of the shader code.
			clip(texColor.a - 0.1f);
	// Lighting.

	float4 litColor = texColor;
	if( gLightCount > 0  )
		// Start with a sum of zero. 
		float4 ambient = float4(0.0f, 0.0f, 0.0f, 0.0f);
		float4 diffuse = float4(0.0f, 0.0f, 0.0f, 0.0f);
		float4 spec    = float4(0.0f, 0.0f, 0.0f, 0.0f);

		// Sum the light contribution from each light source.  
		for(int i = 0; i < gLightCount; ++i)
			float4 A, D, S;
			ComputeDirectionalLight(gMaterial, gDirLights[i], pin.NormalW, toEye, 
				A, D, S);

			ambient += A;
			diffuse += D;
			spec    += S;

		litColor = texColor*(ambient + diffuse) + spec;

		if( gReflectionEnabled )
			float3 incident = -toEye;
			float3 reflectionVector = refract(incident, pin.NormalW, 1.51);
			float4 reflectionColor  = gCubeMap.Sample(samAnisotropic, reflectionVector);

			litColor += gMaterial.Reflect*reflectionColor;
	// Fogging

	if( gFogEnabled )
		float fogLerp = saturate( (distToEye - gFogStart) / gFogRange ); 

		// Blend the fog color and the lit color.
		litColor = lerp(litColor, gFogColor, fogLerp);

	// Common to take alpha from diffuse material and texture.
	litColor.a = gMaterial.Diffuse.a * texColor.a;

    return litColor;
D3D11_BLEND_DESC transparentDesc = {0};
	transparentDesc.AlphaToCoverageEnable = false;
	transparentDesc.IndependentBlendEnable = false;

	transparentDesc.RenderTarget[0].BlendEnable = true;
	transparentDesc.RenderTarget[0].SrcBlend       = D3D11_BLEND_SRC_ALPHA;
	transparentDesc.RenderTarget[0].DestBlend      = D3D11_BLEND_INV_SRC_ALPHA;
	transparentDesc.RenderTarget[0].BlendOp        = D3D11_BLEND_OP_ADD;
	transparentDesc.RenderTarget[0].SrcBlendAlpha  = D3D11_BLEND_ONE;
	transparentDesc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO;
	transparentDesc.RenderTarget[0].BlendOpAlpha   = D3D11_BLEND_OP_ADD;
	transparentDesc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;

	HR(device->CreateBlendState(&transparentDesc, &TransparentBS));
    activeTexTech->GetDesc( &techDesc );
    for(UINT p = 0; p < techDesc.Passes; ++p)
		md3dImmediateContext->IASetVertexBuffers(0, 1, &mShapesVB, &stride, &offset);
		md3dImmediateContext->IASetIndexBuffer(mShapesIB, DXGI_FORMAT_R32_UINT, 0);

		// Draw the grid.
		world = XMLoadFloat4x4(&mGridWorld);
		worldInvTranspose = MathHelper::InverseTranspose(world);
		worldViewProj = world*view*proj;

		Effects::BasicFX->SetTexTransform(XMMatrixScaling(6.0f, 8.0f, 1.0f));
		activeTexTech->GetPassByIndex(p)->Apply(0, md3dImmediateContext);
		md3dImmediateContext->DrawIndexed(mGridIndexCount, mGridIndexOffset, mGridVertexOffset);


Does anybody see why my code is not working?

Edited by terryeverlast

Share this post

Link to post
Share on other sites

Have you tried setting the BlendState after calling "Apply"?

I'm not familiar with FX11, but it sounds like the sort of thing an Effect might do.

Share this post

Link to post
Share on other sites

Putting OMSetBlendState after apply failed. I tried that and also tried just blending in the shader.

BlendState transparentBlend
    BlendEnable[0] = TRUE;
    SrcBlend = SRC_ALPHA;
    DestBlend = INV_SRC_ALPHA;
    BlendOp = ADD;
    SrcBlendAlpha = ZERO;
    DestBlendAlpha = ZERO;
    BlendOpAlpha = ADD;
    RenderTargetWriteMask[0] = 0x0F;

So putting(omsetblend) after apply gave me the original image(did not work) then,. Trying to set up in the shader gave me a grayish image.


Edited by terryeverlast

Share this post

Link to post
Share on other sites

Tranparent working but not on my CubeMap texture(the background in the distance). Here is my material code

m.Ambient  = XMFLOAT4(0.2f, 0.2f, 0.2f, 0.3f);
	m.Diffuse  = XMFLOAT4(0.2f, 0.2f, 0.2f, 0.2f);
	m.Specular = XMFLOAT4(0.8f, 0.8f, 0.8f, 1.0f);
	m.Reflect  = XMFLOAT4(0.5f, 0.5f, 0.5f, 0.3f);

Instead of being tranparent when the land hits the cubemap background texture, it is white.Any ideas why so?

Does the cubmap background have to have alpha channel ?

Edited by terryeverlast

Share this post

Link to post
Share on other sites

Question to everyone:

If you were making a sphere refract some image, would you use blending and make the sphere transparent first?

Or just apply refraction. without tranparent blending

Edited by terryeverlast

Share this post

Link to post
Share on other sites

I wouldn't use blending, but rather just look up in the cube map where the refraction points to.  That can of course be combined with partial reflection as well, but I wouldn't use alpha blending!

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!