[英]How do I create an alpha mask which is rewritten when added to?
我試圖在這樣一個應用程序中創建一個類似霧的效果,在該應用程序中,精靈周圍的紋理(在白色示例中為白色)變得透明(下面顯示黑色紋理),並且密度逐漸遠離其中心。 但是,每當我向Alpha蒙版添加一個新的圓時,它都會覆蓋蒙版紋理的先前添加的內容,從而產生上圖所示的震顫效果。 我一直在嘗試為蒙版紋理添加新的內容,以便與以前的內容很好地融合在一起,但是我不知道該怎么做。
我嘗試做一些事情,例如為我的遮罩創建一個遮罩以與之融合,但是它總是不顯示任何內容,或者使用我的Alpha着色器fx文件繪制到我的Alpha遮罩(這會產生一些非常奇怪的視覺效果)
如何將添加到Alpha蒙版的混合在一起,以免它們互相覆蓋?
任何幫助,不勝感激。
- - - - - - -碼: - - - - - - -
通過以下方法可以更新Alpha蒙版:
public void createLightSource(Vector2 RemovePosition, Texture2D circleTexture)
{
// Create a render target, which we will draw to instead of the screen
RenderTarget2D target = new RenderTarget2D(graphics.GraphicsDevice, mainTexture.Width, mainTexture.Height);
// set the RenderTarget2D as the target for all future Draw calls untill we say otherwise
graphics.GraphicsDevice.SetRenderTarget(target);
// start our batch as usual..
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.PointClamp, null, null);
// start with a transparent canvas
graphics.GraphicsDevice.Clear(Color.Transparent);
// add in the previously drawn dots from the current alpha map.
spriteBatch.Draw(alphaMask, new Vector2(0, 0), null, Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 1f);
// add a new dot to the map.
play.game.spriteBatch.Draw(circleTexture,
new Vector2((float)(RemovePosition.X - mainTexture.x), (float)(RemovePosition.Y - mainTexture.y)),
null,
Color.White,
0f,
new Vector2(circleTexture.Width / 2f, circleTexture.Height / 2f),
1f,
SpriteEffects.None,
1f);
// end the draw call
spriteBatch.End();
// start drawing to the screen again
graphics.GraphicsDevice.SetRenderTarget(null);
// set our Texture2D Alpha Mask to equal the current render target (the new mask).
// RenderTarget2D can be cast to a Texture2D without a problem
alphaMask = target;
}
通過以下方法繪制到主紋理:
public void mainTextureDraw()
{
//alpha shader is the fx file (below)
alphaShader.Parameters["MaskTexture"]
.SetValue(alphaMask);
// start a spritebatch for our effect
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend,
null, null, null, alphaShader);
play.game.spriteBatch.Draw(mainTexture,
position,
null, Color.White, 0f,
new Vector2(0, 0),
1f, SpriteEffects.None, 1f);
spriteBatch.End();
}
以及fx文件中的重要方法:
float4 PixelShaderFunction(float2 inCoord: TEXCOORD0) : COLOR
{
// we retrieve the color in the original texture at
// the current coordinate remember that this function
// is run on every pixel in our texture.
float4 color = tex2D(mainTexture, inCoord);
// Since we are using a black and white mask the black
// area will have a value of 0 and the white areas will
// have a value of 255. Hence the black areas will subtract
// nothing from our original color, and the white areas of
// our mask will subtract all color from the color.
color.rgba = color.rgba - tex2D(alphaMask, inCoord).r;
// return the new color of the pixel.
return color;
}
我不確定100%確定采用哪種方法,因為我不知道實際結果應該是什么樣。 這是我正在談論的幾個例子。
但是,我認為最好的方法是查看PixelShaderFunction
。 您已經擁有了現有的color
紋理,並且正在使用tex2D()
的紅色分量。 我想知道是否可能有用的方法是采用tex2D()。r並查看它是否大於color.r,如果使用它,則使用color的值而不是減去tex2D()。
基本上,您有一個要維護的閾值。 您需要測試像素的條件,然后決定如何以及是否要更改它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.