標準のDirectX関数(、、CreateTexture2D
などD3DX11SaveTextureToFile
)D3DX11CreateShaderResourceViewFromFile
を使用してPNG画像を読み込み、新しく作成したテクスチャにレンダリングしてから、ファイルに保存します。すべてのテクスチャは2つのサイズの力です。
しかし、その間に、PNGの一部の色が少し破損していることに気付きました(ソーステクスチャの色と似ていますが、同じではありません)。透明度についても同じです(0%と100%の透明度の部分では機能しますが、たとえば34%では機能しません)。
いくつかの大きな色の近似がありますか、それとも私は何か間違ったことをしますか?もしそうなら、どうすればそれを解決できますか?
これらの2つの画像は次のとおりです(左側はソースです。下部に少し異なる色とグラデーションの透明度があります。右側は最初の画像を読み込んで新しいテクスチャにレンダリングした後の画像で、ファイルに保存されています):
その動作の原因がわかりません。おそらく、新しいテクスチャの説明です。
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);
}
いくつかの最適化のために、スプライトのサイズをシェーダーのパラメーターとして解析します(正常に機能し、テクスチャのサイズ、境界線などは正しいです)。