2014-02-05 2 views
-1

Im пытается построить RenderTarget альфа-маску для тумана войны, например .. я сказать ему, что я хочу, чтобы очистить RenderTarget с твердым белым (Alpha 255)AlphaMask в monogame не работает должным образом

Тогда я рисую мой изображение с прозрачным кругом, на альфа-канал, оттуда я ожидаю иметь изображение в rendertarget с отверстиями в нем. Это, однако, не так. Я очень хорошо знаком с альфа-маскировкой и сделал это успешно, когда рисую одно изображение в альфа-маску, но при рисовании пучка, как у нас здесь, он полностью не дает мне правильных значений альфа ,

if (mFogOfWarRT == null) 
{ 
    mFogOfWarRT = new RenderTarget2D(pGraphics.GraphicsDevice, MapSize, MapSize); 
    pGraphics.GraphicsDevice.SetRenderTarget(mFogOfWarRT); 
    //pGraphicsDevice.Clear(Color.Black); 
} 
else 
{ 
    pGraphics.GraphicsDevice.SetRenderTarget(mFogOfWarRT); 
} 

pGraphicsDevice.Clear(Color.White); 
pSpriteBatch.Begin(); 
BlendState lKeep = new BlendState(); 
lKeep.AlphaSourceBlend = Blend.One; 
lKeep.AlphaDestinationBlend = Blend.Zero; 
lKeep.ColorSourceBlend = Blend.Zero; 
lKeep.ColorDestinationBlend = Blend.One; 
lKeep.ColorBlendFunction = BlendFunction.Subtract; 
lKeep.AlphaBlendFunction = BlendFunction.Add; 
pGraphicsDevice.BlendState = lKeep; 
pGraphicsDevice.BlendState.ColorWriteChannels = ColorWriteChannels.Alpha; 

foreach (ClearArea lArea in mDrawQueue) 
{ 
    pSpriteBatch.Draw(mAlphaMask, new Rectangle((int)lArea.X, lArea.Y, lArea.Diameter, lArea.Diameter), Color.White); 
} 
//pSpriteBatch.Draw(mDot, new Rectangle(0, 0, 100, 100), Color.White); 
pSpriteBatch.End(); 

pGraphicsDevice.SetRenderTarget(null); 

Это мой рисунок код альфа-маски .. когда рисует на экране мы получаем белый квадрат, больше ничего.

вниз Я рисую мой «Карта» в RGB, а затем установите A, и обратить мое alphamask текстуру и с треском проваливается

pGraphicsDevice.SetRenderTarget(null); 

// Draw minimap texture 
pGraphicsDevice.Clear(Color.CornflowerBlue); 
pGraphicsDevice.BlendState = lKeep; 
pGraphicsDevice.BlendState.ColorWriteChannels = ColorWriteChannels.Red | ColorWriteChannels.Blue | ColorWriteChannels.Green; 

pSpriteBatch.Begin(); 
pSpriteBatch.Draw(mDot, new Rectangle(0, 0, 400, 400), Color.Blue); 
pGraphicsDevice.BlendState.ColorWriteChannels = ColorWriteChannels.Alpha; 
pSpriteBatch.Draw(mFogOfWarRT, Vector2.Zero, Color.White); 

pSpriteBatch.End(); 

Любая помощь с этим было бы очень признателен ..

паста бин полный код

http://pastebin.com/KsEVaW0d

снимок экрана результата доступны в случае необходимости.

ответ

1

Принял решение, что альфа-маскировка не является наилучшим подходом после проведения исследований. Я нашел трафареты.

Приведенный ниже код устраняет проблему путем записи в буфер трафарета.

graphics.PreferredDepthStencilFormat = DepthFormat.Depth24Stencil8; 
graphics.ApplyChanges(); 

AlphaTestEffect alphaTestEffect = new AlphaTestEffect(pGraphicsDevice); 
alphaTestEffect.VertexColorEnabled = true; 
alphaTestEffect.DiffuseColor = Color.White.ToVector3(); 
alphaTestEffect.AlphaFunction = CompareFunction.Equal; 
alphaTestEffect.ReferenceAlpha = 0; 
alphaTestEffect.World = Matrix.Identity; 
alphaTestEffect.View = Matrix.Identity; 
Matrix projection = Matrix.CreateOrthographicOffCenter(0, 400,400, 0, 0, 1); 
alphaTestEffect.Projection = projection; 
     // Create fog of war mask 
     if (mFogOfWarRT == null) 
     { 
      mFogOfWarRT = new RenderTarget2D(pGraphics.GraphicsDevice, MapSize, MapSize, false, SurfaceFormat.Color, DepthFormat.Depth24Stencil8); 
      pGraphics.GraphicsDevice.SetRenderTarget(mFogOfWarRT); 

     } 
     else 
     { 
      pGraphics.GraphicsDevice.SetRenderTarget(mFogOfWarRT); 
     } 
     //important both stencil states be created in their own object, cannot modify once set for some reason. 
     DepthStencilState lState = new DepthStencilState(); 
     lState.StencilEnable = true; 
     lState.StencilFunction = CompareFunction.Always; 
     lState.ReferenceStencil = 1; 
     lState.StencilPass = StencilOperation.Replace; 
     lState.DepthBufferEnable = false; 
     pGraphicsDevice.Clear(ClearOptions.Target | ClearOptions.Stencil, 
            new Color(0, 0, 0, 1), 0, 0); 
     pSpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, null, lState, null, alphaTestEffect); 
     foreach (ClearArea lArea in mDrawQueue) 
     { 
//draw whatever you want "visible" anything in the texture with an alpha of 0 will be allowed to draw. 
      pSpriteBatch.Draw(mAlphaMask, new Rectangle((int)lArea.X, lArea.Y, lArea.Diameter, lArea.Diameter), Color.White); 
     } 

     pSpriteBatch.End(); 

     // Draw minimap texture 
     DepthStencilState lState2 = new DepthStencilState(); 
     lState2.StencilEnable = true; 
     lState2.StencilFunction = CompareFunction.Equal; 
     lState2.ReferenceStencil = 0; 
     lState2.StencilPass = StencilOperation.Keep; 
     lState2.DepthBufferEnable = false; 

     pSpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, null, lState2, null); 
     pSpriteBatch.Draw(mDot, new Rectangle(0, 0, 400, 400), Color.Black); 

     pSpriteBatch.End(); 
     //done drawing to the render target 
     pGraphicsDevice.SetRenderTarget(null); 
     pGraphicsDevice.Clear(Color.Gray); 
     pSpriteBatch.Begin(); 
     pSpriteBatch.Draw(mDot, new Rectangle(0, 0, 400, 400), Color.Blue); 
     pSpriteBatch.Draw(mFogOfWarRT,Vector2.Zero,Color.White); 
     pSpriteBatch.End(); 
+0

попробуем это позже – Emily

Смежные вопросы