Fog blending not working with object.

Started by
3 comments, last by terryeverlast 9 years, 1 month ago

I'm on a new blending question. I have fog in my scene and when my object goes in the part where it should disappear, it does not.

2z4k7ti.jpgIn front of the fog still visible

2nrof43.jpg

As you see, you can still see the object it the part where it should disappear!

I used a multiply blend.

my code


D3D11_BLEND_DESC myblend2 = { 0 };
	myblend2.AlphaToCoverageEnable = false;
	myblend2.IndependentBlendEnable = false;

	myblend2.RenderTarget[0].BlendEnable = true;
	myblend2.RenderTarget[0].SrcBlend = D3D11_BLEND_ZERO;
	myblend2.RenderTarget[0].DestBlend = D3D11_BLEND_SRC_COLOR;
	myblend2.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
	myblend2.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE;
	myblend2.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ONE;
	myblend2.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
	myblend2.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
	HR(md3dDevice->CreateBlendState(&myblend2, &MULTIPLYBLEND));

For the BlendOp, when I use D3D11_BLEND_OP_MIN or MAX, it will disappear in the fog, but using ADD,SUBTRACT,REV_SUBTRACT, it does not. I also narrowed it down to the DestBlend, when it is set to zero(D3D11_BLEND_ZERO), it will disappear in the fog.

Maybe its my image? maybe the pixel shader?

I also have my HLSL pixel shader code, here it is


float4 PS(VertexOut pin, uniform int gLightCount, uniform bool gUseTexure, uniform bool gAlphaClip, uniform bool gFogEnabled) : 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);
		if (gUseTexure)
		{
			// Sample texture.
			texColor = gDiffuseMap.Sample(samAnisotropic, pin.Tex);

					}

	//
	// 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.  
				[unroll]
			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;
			}

			// Modulate with late add.
			litColor = texColor*(ambient + diffuse) + spec;
		}

	//
	// 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;
}

Maybe something wrong in there?

Advertisement

For fog you don't need (pipeline) blending, you just blend it with the background color manually, which you actually do here with that lerp. Try opaque blending (e.g. NULL blendstate, the default) and make sure the fog color is the same as your background.

In the code:


teContext->IASetVertexBuffers(0, 1, &mBoxVB, &stride, &offset);
		md3dImmediateContext->IASetIndexBuffer(mBoxIB, DXGI_FORMAT_R32_UINT, 0);

	

		
	
		
	
		// Draw the box.
		 XMMATRIX world = XMLoadFloat4x4(&mBoxWorld);
		 XMMATRIX worldInvTranspose = MathHelper::InverseTranspose(world);
		 XMMATRIX worldViewProj = world*view*proj;
		Effects::BasicFX->SetWorld(world);
		Effects::BasicFX->SetWorldInvTranspose(worldInvTranspose);
		Effects::BasicFX->SetWorldViewProj(worldViewProj);
		Effects::BasicFX->SetTexTransform(XMMatrixIdentity());
		Effects::BasicFX->SetMaterial(mBoxMat);
		activeTech->GetPassByIndex(p)->Apply(0, md3dImmediateContext);
		Effects::BasicFX->SetDiffuseMap(mDiffuseMapSRV); //BRICK WALL
		md3dImmediateContext->OMSetBlendState(0, 0, 0xffffffff);
		md3dImmediateContext->DrawIndexed(mBoxIndexCount, mBoxIndexOffset, mBoxVertexOffset);
		activeTech->GetPassByIndex(p)->Apply(0, md3dImmediateContext);
		Effects::BasicFX->SetDiffuseMap(mDiffuseMapSRV2); // ORANGE SUN
		md3dImmediateContext->OMSetBlendState(MULTIPLYBLEND, blendFactor, 0xffffffff);
		md3dImmediateContext->OMSetDepthStencilState(mydepth, 0); // set to  D3D11_COMPARISON_LESS_EQUAL
		md3dImmediateContext->DrawIndexed(mBoxIndexCount, mBoxIndexOffset, mBoxVertexOffset);


		
		


 }

I use md3dImmediateContext->OMSetBlendState(MULTIPLYBLEND, blendFactor, 0xffffffff);
to blend two images, which is causing the box not to blend in. The fog color is silver and so is the background, still not working. I tried editing depth, no luck. so its either depth or the blend, which i cannot figure out. Any more ideas? I will post more code if needed

The problem only happens when using additive blend or subtractive blend and multiplitie blend.

Basically when adding,subtracting or multiplying two images, kina like the picture below.

2e5tpiv.jpg

Here is my code for blending.

When adding two images.


myblend2.RenderTarget[0].BlendEnable = true;
	myblend2.RenderTarget[0].SrcBlend = D3D11_BLEND_ONE;
	myblend2.RenderTarget[0].DestBlend = D3D11_BLEND_ONE;
	myblend2.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
	myblend2.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE;
	myblend2.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ONE;
	myblend2.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
	myblend2.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
	HR(md3dDevice->CreateBlendState(&myblend2, &addBLEND));

	myblend2.RenderTarget[0].BlendEnable = FALSE;

	HR(md3dDevice->CreateBlendState(&myblend2, &mb2));

Subtractive blend:


myblend2.RenderTarget[0].BlendEnable = true;
	myblend2.RenderTarget[0].SrcBlend = D3D11_BLEND_ONE;
	myblend2.RenderTarget[0].DestBlend = D3D11_BLEND_ONE;
	myblend2.RenderTarget[0].BlendOp = D3D11_BLEND_OP_SUBTRACT;
	myblend2.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE;
	myblend2.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ONE;
	myblend2.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
	myblend2.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
	HR(md3dDevice->CreateBlendState(&myblend2, &subBLEND));

	myblend2.RenderTarget[0].BlendEnable = FALSE;

	HR(md3dDevice->CreateBlendState(&myblend2, &mb2));

?

When doing a transparent blend, for some reason, it will disapperar in the fog and I get the right results


D3D11_BLEND_DESC myblend2 = { 0 };
	myblend2.AlphaToCoverageEnable = false;
	myblend2.IndependentBlendEnable = false;

	myblend2.RenderTarget[0].BlendEnable = true;
	myblend2.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA;
	myblend2.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
	myblend2.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
	myblend2.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE;
	myblend2.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO;
	myblend2.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
	myblend2.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
	HR(md3dDevice->CreateBlendState(&myblend2, &transBLEND));

also, the fog and background are both silver.

Like when im adding, i will see a white box. when subtracting, I will see a black box in the fog and multiplying, a gray box?

anyone? need some insight.Could the problem be from depth settings? I tried some different ones with no luck.

This topic is closed to new replies.

Advertisement