1

拡散雷のために、色と法線の2つのレンダーターゲットを組み合わせて、結果を画面にレンダリングしようとしています。アイデアは、ピクセル シェーダーのみを含む効果を持つスプライトを使用して、テクスチャのレンダー ターゲットを結合することです。XNA コード:

GraphicsDevice.SetRenderTarget(null);
GraphicsDevice.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.Black, 1.0f, 0);
effect.CurrentTechnique = effect.Techniques["show_buffer"];
effect.Parameters["normalTex"].SetValue(normalRendertarget);
effect.Parameters["colorTex"].SetValue(colorRendertarget);
effect.Parameters["AmbientIntensity"].SetValue(ambientIntesity);
effect.Parameters["LightDirection"].SetValue(lightDirection);
effect.Parameters["DiffuseIntensity"].SetValue(diffuseIntensity);

spriteBatch.Begin(0, BlendState.Opaque, null, null, null,effect);
spriteBatch.Draw(normalRT, Vector2.Zero, Color.White);
spriteBatch.End();

何らかの理由で spriteBatch.Draw() で使用される rendertarget が結果に影響します。

ピクセル シェーダー:

void Tex_PixelShader(float2 texCoord : TEXCOORD0, out float4 color : COLOR0)
{
 float4 normal = tex2D(normalTexSampler, texCoord);
 //tranform normal back into [-1,1] range
 normal.rgb = (normal.rgb*2)-1; 
 float4 baseColor = tex2D(colorTexSampler, texCoord);
 float3 lightDirectionNorm = normalize(LightDirection);
 float diffuse = saturate(dot(-lightDirectionNorm,normal.rgb));

 //only works with normalRT in spriteBatch.Draw()
 //colorRT  in spriteBatch.Draw() gives colorRT but darker as result
 color = float4 (baseColor.rgb * (AmbientIntensity + diffuse*DiffuseIntensity), 1.0f);

 //only works with colorRT in spriteBatch.Draw()
 //normalRT in spriteBatch.Draw() gives normalRT as result
 //color = tex2D(colorTexSampler, texCoord); 

 //only works with NormalRT
 //colorRT in spriteBatch.Draw() gives colorRT as result
 //color=tex2D(normalTexSampler, texCoord);

 // works with any rendertarget in spriteBatch.Draw()
 //color = float4(0.0f,1.0f,0.0f,1.0f);
}

両方のレンダー ターゲットのアルファ値は常に 1 です。エフェクトに頂点シェーダーを追加すると、結果は黒になります。spriteBatch.Draw() で何の効果もなしにレンダー ターゲットを 1 つ描画すると、各レンダー ターゲットのコンテンツが問題ないことがわかります。私はこれを理解することはできません。何か案は?

4

1 に答える 1

0

GraphicsDevice.Textures[1]=tex; でテクスチャを設定します。effect.Parameters["tex"]=tex;の代わりに 働きました。ありがとうアンドリュー。

xna コードを次のように変更しました。

GraphicsDevice.SetRenderTarget(null);
GraphicsDevice.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.Black, 1.0f, 0);
effect.CurrentTechnique = effect.Techniques["show_buffer"];
GraphicsDevice.Textures[1] = normalRT;   //changed
GraphicsDevice.Textures[2] = colorRT;    //changed
effect.Parameters["AmbientIntensity"].SetValue(ambientIntesity);
effect.Parameters["LightDirection"].SetValue(lightDirection);
effect.Parameters["DiffuseIntensity"].SetValue(diffuseIntensity);

spriteBatch.Begin(0, BlendState.Opaque, null, null, null,effect);
spriteBatch.Draw((Texture2D)colorRT, Vector2.Zero, Color.White);
spriteBatch.End();

シェーダー コードは次のようになります。

sampler normalSampler: register(s1);   //added
sampler colorSampler: register(s2);    //added

void Tex_PixelShader(float2 texCoord : TEXCOORD0, out float4 color : COLOR0)
{
 float4 normal = tex2D(normalSampler, texCoord); //changed
 normal.rgb = (normal.rgb*2)-1;
 float4 baseColor = tex2D(colorSampler, texCoord);  //changed

 float3 lightDirectionNorm = normalize(LightDirection);
 float diffuse = saturate(dot(-lightDirectionNorm,normal.rgb));

 color = float4 (baseColor.rgb * (AmbientIntensity + diffuse*DiffuseIntensity), 1.0f);
}
于 2012-09-17T07:16:08.567 に答える