我使用标准的 DirectX 函数(如 CreateTexture2D
、D3DX11SaveTextureToFile
和 D3DX11CreateShaderResourceViewFromFile
)加载 PNG 图像,在新创建的纹理上渲染它,然后比保存到文件。所有的纹理都是两种尺寸的力量。
但在此过程中,我注意到 PNG 中的某些颜色有点损坏(与源纹理中的颜色相似但不相同)。透明度相同(它适用于 0 和 100% 透明度部分,但不适用于例如 34%)。
是否有一些大的颜色近似值或我做错了什么?如果是,我该如何解决?
这是这两张图片(左边是来源:底部有一些不同的颜色和一些渐变透明度;右边是加载第一张图片并在新纹理上渲染后的图片,然后保存到文件中):
我不知道是什么导致了这种行为,也许是新纹理的描述:
textureDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
我尝试将其更改为 DXGI_FORMAT_R32G32B32A32_FLOAT
,但效果更奇怪:
这是在新纹理上渲染源纹理的代码:
context->OMSetRenderTargets(1, &renderTargetView, depthStencilView); //to render on new texture instead of the screen
float clearColor[4] = {0.0f, 0.0f, 0.0f, 0.0f}; //red, green, blue, alpha
context->ClearRenderTargetView(renderTargetView, clearColor);
//clear the depth buffer to 1.0 (max depth)
context->ClearDepthStencilView(depthStencilView, D3D11_CLEAR_DEPTH, 1.0f, 0);
//rendering
turnZBufferOff();
shader->set(context);
object->render(shader, camera, textureManager, context, 0);
swapChain->Present(0, 0);
在 object->render()
中:
UINT stride;
stride = sizeof(Vertex);
UINT offset = 0;
context->IASetVertexBuffers( 0, 1, &buffers->vertexBuffer, &stride, &offset ); //set vertex buffer
context->IASetIndexBuffer( buffers->indexBuffer, DXGI_FORMAT_R16_UINT, 0 ); //set index buffer
context->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST ); //set primitive topology
if(textureID){
context->PSSetShaderResources( 0, 1, &textureManager->get(textureID)->texture);
}
ConstantBuffer2DStructure cbPerObj;
cbPerObj.positionAndScale = XMFLOAT4(center.getX(), center.getY(), halfSize.getX(), halfSize.getY());
cbPerObj.textureCoordinates = XMFLOAT4(textureRectToUse[0].getX(), textureRectToUse[0].getY(), textureRectToUse[1].getX(), textureRectToUse[1].getY());
context->UpdateSubresource(constantBuffer, 0, NULL, &cbPerObj, 0, 0);
context->VSSetConstantBuffers(0, 1, &constantBuffer);
context->PSSetConstantBuffers(0, 1, &constantBuffer);
context->DrawIndexed(6, 0, 0);
着色器非常简单:
VS_OUTPUT VS(float4 inPos : POSITION, float2 inTexCoord : TEXCOORD)
{
VS_OUTPUT output;
output.Pos.zw = float2(0.0f, 1.0f);
//inPos(x,y) = {-1,1}
output.Pos.xy = (inPos.xy * positionAndScale.zw) + positionAndScale.xy;
output.TexCoord.xy = inTexCoord.xy * (textureCoordinates.zw - textureCoordinates.xy) + textureCoordinates.xy;
return output;
}
float4 PS(VS_OUTPUT input) : SV_TARGET
{
return ObjTexture.Sample(ObjSamplerState, input.TexCoord);
}
为了进行一些优化,我将 Sprite 的大小解析为着色器的参数(它工作正常,纹理大小、边框等都是正确的)。
最佳答案
你设置混合状态了吗?默认情况下,Alpha 将不起作用,因为默认混合根本就没有混合。
这是一个标准的 alpha 混合状态:
D3D11_BLEND_DESC desc;
desc.AlphaToCoverageEnable=false;
desc.IndependentBlendEnable = false;
for (int i =0; i < 8 ; i++)
{
desc.RenderTarget[i].BlendEnable = true;
desc.RenderTarget[i].BlendOp = D3D11_BLEND_OP::D3D11_BLEND_OP_ADD;
desc.RenderTarget[i].BlendOpAlpha = D3D11_BLEND_OP::D3D11_BLEND_OP_ADD;
desc.RenderTarget[i].DestBlend = D3D11_BLEND::D3D11_BLEND_INV_SRC_ALPHA;
desc.RenderTarget[i].DestBlendAlpha = D3D11_BLEND::D3D11_BLEND_ONE;
desc.RenderTarget[i].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE::D3D11_COLOR_WRITE_ENABLE_ALL;
desc.RenderTarget[i].SrcBlend = D3D11_BLEND::D3D11_BLEND_SRC_ALPHA;
desc.RenderTarget[i].SrcBlendAlpha = D3D11_BLEND::D3D11_BLEND_ONE;
}
ID3D11BlendState* state;
device->CreateBlendState(&desc,&state);
return state;
此外,我会使用 Clear 并将 alpha 分量设置为 1 而不是 0
关于c++ - 在加载和保存期间缺少 DirectX 中 PNG 纹理的某些颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14184980/