Tinting and Alpha

Started by
0 comments, last by brekehan 14 years, 11 months ago
I've got a texture that only has RGB channels and consists of a white circle on a black background. When rendering they obviously set a tinting color with a dynamic alpha to the shader. I think they want anything that appears white on the texture to be the color of the value of the shader's color variable, including the alpha. Anything black on the texture, should just not be rendered at all. I am really not sure what to write in my shader code. Their XNA code
[source lang ="cpp"]
void DrawGlow(Vector2 lightPosition)
        {
            Vector4 color = new Vector4(1, 1, 1, occlusionAlpha);
            Vector2 origin = new Vector2(glowSprite.Width, glowSprite.Height) / 2;
            float scale = glowSize * 2 / glowSprite.Width;

            spriteBatch.Begin(SpriteBlendMode.AlphaBlend);

            spriteBatch.Draw(glowSprite, lightPosition, null, new Color(color), 0,
                             origin, scale, SpriteEffects.None, 0);

            spriteBatch.End();
        }

My D3D Code:

void LensFlare::RenderGlow(const D3DXVECTOR2 & lightPosition)
{
   // Set the world matrix and material
   D3DXMATRIX matScale;
   D3DXMATRIX matTranslation;
   D3DXMATRIX world;

   D3DXMatrixScaling(&matScale, m_glowSize, m_glowSize, 1.0f);
   D3DXMatrixTranslation(&matTranslation, lightPosition.x, lightPosition.y, 0.0f);
   D3DXMatrixMultiply(&world, &matScale, &matTranslation);
   
   m_flareMaterial->SetFloat4("diffuseColor", D3DXVECTOR4(1.0f, 1.0f, 1.0f, m_occlusionAlpha));
   m_flareMaterial->SetTextureName("diffuseTexture", m_glowTextureName);

   try
   {
      m_effect->SetWorldMatrix(world);
      m_effect->SetMaterial(*m_flareMaterial);
   }
   catch(BaseException & e)
   {
      throw e;
   }

   // Bind the input layout
   m_device.IASetInputLayout(m_flareInputLayout);
   m_device.IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);

   // Bind the vertex buffers
   ID3D10Buffer * buffers[2] = {m_positionBuffer->GetD3DBuffer(), 
                                m_texCoordBuffer->GetD3DBuffer()};
   
   UINT strides[2] = {GetStride(m_positionBuffer->GetContentType()),
                      GetStride(m_texCoordBuffer->GetContentType())};
   
   UINT offsets[2] = {0, 
                      0};
   
   m_device.IASetVertexBuffers(0,
                               2,
                               buffers, 
                               strides, 
                               offsets);

   // Apply the pass
   m_flarePass->Apply();

   // Draw the glow
   m_device.Draw(4, 0);
}


//--------------------------------------------------------------------------------------
// File: LensFlare.fx
//
//--------------------------------------------------------------------------------------

#include "EffectPool.fxh"

matrix world                 : World;
matrix worldInverseTranspose : WorldInverseTranspose;

//-------------------
// Diffuse Variables
//
// Diffuse Color can be a single color or sampled from a texture

float4    diffuseColor    = float4(1.0f, 1.0f, 1.0f, 1.0f);
Texture2D diffuseTexture;

SamplerState samplerLinear
{
   Filter = MIN_MAG_MIP_LINEAR;
   AddressU = Wrap;
   AddressV = Wrap;
};

//////// CONNECTOR DATA STRUCTURES ///////////

// SNIP

struct VS_IN_FLARE
{
   float4 position : POSITION;
   float2 texCoord : TEXCOORD;
};

struct PS_IN_FLARE
{
   float4 position : SV_POSITION;
   float2 texCoord : TEXCOORD;
};

//////////// STATES ///////////////

// ??????????
BlendState SrcAlphaBlend
{
   BlendEnable[0]           = TRUE;
   SrcBlend                 = SRC_ALPHA;
   DestBlend                = INV_SRC_ALPHA;
   BlendOp                  = ADD;
   SrcBlendAlpha            = ONE;
   DestBlendAlpha           = ONE;
   BlendOpAlpha             = ADD;
   RenderTargetWriteMask[0] = 0x0F;
};

// SNIP

//--------------------------------------------------------------------------------------
// Colors the object with the diffuse color multiplied by the texture color
//
float4 PS_Flare( PS_IN_FLARE input ) : SV_Target
{  
   // ????????????????????
   float4 finalColor;
   
   finalColor.rgb = diffuseColor.rgb * diffuseTexture.Sample(samplerLinear, input.texCoord).rgb;
}

//--------------------------------------------------------------------------------------
// Techniques
//--------------------------------------------------------------------------------------

//--------------------------------------------------------------------------------------
// Renders an object at 100% opacity, with no lights or shadows
//
technique10 RenderFlare
{
    pass P0
    {
        SetVertexShader( CompileShader( vs_4_0, VS_Flare() ) );
        SetGeometryShader( NULL );
        SetPixelShader( CompileShader( ps_4_0, PS_Flare() ) );
        
        SetBlendState( SrcAlphaBlend, float4(0.0f, 0.0f, 0.0f, 0.0f), 0xFFFFFFFF);
        SetDepthStencilState( DefaultDepthStencil, 0);
    }
}

// SNIP


Advertisement
I think I got it.

I just multiplied textureColor by diffuseColor. The alpha was confusing me. But a regular AlphaBlend seemed to work OK. My problem were mainly from depth and correct textures.

This topic is closed to new replies.

Advertisement