Skip to content

Commit

Permalink
Complex Colors
Browse files Browse the repository at this point in the history
This adds a features which I've named Complex Colors. Essentially it allows a separate blend color to be applied to each corner of a sprite. This allows much more complex blending scenarios that can mimic lighting. Complex colors can also be used with the individual sprites of a Composite Sprite.
  • Loading branch information
greenfire27 committed Oct 16, 2024
1 parent 6a7798a commit 3461e66
Show file tree
Hide file tree
Showing 19 changed files with 825 additions and 68 deletions.
1 change: 1 addition & 0 deletions engine/compilers/VisualStudio 2022/Torque 2D.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1087,6 +1087,7 @@
<ClInclude Include="..\..\source\game\gameInterface.h" />
<ClInclude Include="..\..\source\game\gameInterface_ScriptBinding.h" />
<ClInclude Include="..\..\source\game\version_ScriptBinding.h" />
<ClInclude Include="..\..\source\graphics\FadeToColor.h" />
<ClInclude Include="..\..\source\graphics\gColor.h" />
<ClInclude Include="..\..\source\graphics\gColor_ScriptBinding.h" />
<ClInclude Include="..\..\source\graphics\dgl.h" />
Expand Down
3 changes: 3 additions & 0 deletions engine/compilers/VisualStudio 2022/Torque 2D.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -3091,6 +3091,9 @@
<ClInclude Include="..\..\source\gui\guiColorPopupCtrl_ScriptBinding.h">
<Filter>gui</Filter>
</ClInclude>
<ClInclude Include="..\..\source\graphics\FadeToColor.h">
<Filter>graphics</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="Torque 2D.rc" />
Expand Down
34 changes: 25 additions & 9 deletions engine/source/2d/core/BatchRender.cc
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,10 @@ void BatchRender::SubmitQuad(
const Vector2& texturePos2,
const Vector2& texturePos3,
TextureHandle& texture,
const ColorF& color )
const ColorF& color0,
const ColorF& color1,
const ColorF& color2,
const ColorF& color3)
{
// Sanity!
AssertFatal( mpDebugStats != NULL, "Debug stats have not been configured." );
Expand All @@ -229,13 +232,13 @@ void BatchRender::SubmitQuad(
if ( mColorCount == 0 )
{
// No, so flush if color is specified.
if ( color != NoColor )
if ( color0 != NoColor )
flush( mpDebugStats->batchColorStateFlush );
}
else
{
// Yes, so flush if color is not specified.
if ( color == NoColor )
if ( color0 == NoColor )
flush( mpDebugStats->batchColorStateFlush );
}
}
Expand Down Expand Up @@ -268,13 +271,26 @@ void BatchRender::SubmitQuad(
}

// Is a color specified?
if ( color != NoColor )
if ( color0 != NoColor )
{
// Yes, so add colors.
mColorBuffer[mColorCount++] = color;
mColorBuffer[mColorCount++] = color;
mColorBuffer[mColorCount++] = color;
mColorBuffer[mColorCount++] = color;
// Yes, all or one. Do we have four colors?
if (color1 != NoColor && color2 != NoColor && color3 != NoColor)
{
// We have four colors!
// NOTE: We swap #2/#3 here.
mColorBuffer[mColorCount++] = color0;
mColorBuffer[mColorCount++] = color1;
mColorBuffer[mColorCount++] = color3;
mColorBuffer[mColorCount++] = color2;
}
else
{
// No, we only have one color.
mColorBuffer[mColorCount++] = color0;
mColorBuffer[mColorCount++] = color0;
mColorBuffer[mColorCount++] = color0;
mColorBuffer[mColorCount++] = color0;
}
}

// Add textured vertices.
Expand Down
5 changes: 4 additions & 1 deletion engine/source/2d/core/BatchRender.h
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,10 @@ class BatchRender
const Vector2& texturePos2,
const Vector2& texturePos3,
TextureHandle& texture,
const ColorF& color = ColorF(-1.0f, -1.0f, -1.0f) );
const ColorF& color0 = ColorF(-1.0f, -1.0f, -1.0f),
const ColorF& color1 = ColorF(-1.0f, -1.0f, -1.0f),
const ColorF& color2 = ColorF(-1.0f, -1.0f, -1.0f),
const ColorF& color3 = ColorF(-1.0f, -1.0f, -1.0f));

/// Render a quad immediately without affecting current batch.
/// All render state should be set beforehand directly.
Expand Down
46 changes: 46 additions & 0 deletions engine/source/2d/core/ImageFrameProviderCore.cc
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,52 @@ void ImageFrameProviderCore::render(
getProviderTexture());
}

//------------------------------------------------------------------------------

void ImageFrameProviderCore::renderComplex(
const bool flipX,
const bool flipY,
const Vector2& vertexPos0,
const Vector2& vertexPos1,
const Vector2& vertexPos2,
const Vector2& vertexPos3,
BatchRender* pBatchRenderer,
const ColorF& colorTL,
const ColorF& colorTR,
const ColorF& colorBR,
const ColorF& colorBL) const
{
// Finish if we can't render.
if (!validRender())
return;

// Fetch texel area.
ImageAsset::FrameArea::TexelArea texelArea = getProviderImageFrameArea().mTexelArea;

// Flip texture coordinates appropriately.
texelArea.setFlip(flipX, flipY);

// Fetch lower/upper texture coordinates.
const Vector2& texLower = texelArea.mTexelLower;
const Vector2& texUpper = texelArea.mTexelUpper;

// Submit batched quad.
pBatchRenderer->SubmitQuad(
vertexPos0,
vertexPos1,
vertexPos2,
vertexPos3,
Vector2(texLower.x, texUpper.y),
Vector2(texUpper.x, texUpper.y),
Vector2(texUpper.x, texLower.y),
Vector2(texLower.x, texLower.y),
getProviderTexture(),
colorBL,
colorBR,
colorTR,
colorTL);
}

//------------------------------------------------------------------------------
void ImageFrameProviderCore::renderGui( GuiControl& owner, Point2I offset, const RectI &updateRect ) const
{
Expand Down
13 changes: 13 additions & 0 deletions engine/source/2d/core/ImageFrameProviderCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,19 @@ class ImageFrameProviderCore :
const ColorF *colorArray,
BatchRender* pBatchRenderer) const;

void renderComplex(
const bool flipX,
const bool flipY,
const Vector2& vertexPos0,
const Vector2& vertexPos1,
const Vector2& vertexPos2,
const Vector2& vertexPos3,
BatchRender* pBatchRenderer,
const ColorF& colorTL,
const ColorF& colorTR,
const ColorF& colorBR,
const ColorF& colorBL) const;

void renderGui( GuiControl& owner, Point2I offset, const RectI &updateRect ) const;

/// Static-Image Frame.
Expand Down
48 changes: 48 additions & 0 deletions engine/source/2d/core/SpriteBatch.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1014,6 +1014,54 @@ F32 SpriteBatch::getSpriteAlphaTest( void ) const

//------------------------------------------------------------------------------

void SpriteBatch::setSpriteUseComplexColor(const bool complexColor)
{
// Finish if a sprite is not selected.
if (!checkSpriteSelected())
return;

// Set complex color.
mSelectedSprite->setUseComplexColor(complexColor);
}

//------------------------------------------------------------------------------

bool SpriteBatch::getSpriteUseComplexColor(void) const
{
// Finish if a sprite is not selected.
if (!checkSpriteSelected())
return true;

// Get complex color.
return mSelectedSprite->getUseComplexColor();
}

//------------------------------------------------------------------------------

void SpriteBatch::setSpriteComplexColor(const ColorF& blendColor0, const ColorF& blendColor1, const ColorF& blendColor2, const ColorF& blendColor3)
{
// Finish if a sprite is not selected.
if (!checkSpriteSelected())
return;

// Set blend color.
mSelectedSprite->setComplexColor(blendColor0, blendColor1, blendColor2, blendColor3);
}

//------------------------------------------------------------------------------

const ColorF& SpriteBatch::getSpriteComplexColor(const S8 cornerID) const
{
// Finish if a sprite is not selected.
if (!checkSpriteSelected())
return ColorF::StockColor("White");

// Get blend color.
return mSelectedSprite->getComplexColor(cornerID);
}

//------------------------------------------------------------------------------

void SpriteBatch::setSpriteDataObject( SimObject* pDataObject )
{
// Finish if a sprite is not selected.
Expand Down
6 changes: 6 additions & 0 deletions engine/source/2d/core/SpriteBatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,12 @@ class SpriteBatch
const ColorF& getSpriteBlendColor( void ) const;
void setSpriteBlendAlpha( const F32 alpha );
F32 getSpriteBlendAlpha( void ) const;

void setSpriteUseComplexColor(const bool complexColor);
bool getSpriteUseComplexColor(void) const;

void setSpriteComplexColor(const ColorF& blendColor0, const ColorF& blendColor1, const ColorF& blendColor2, const ColorF& blendColor3);
const ColorF& getSpriteComplexColor(const S8 cornerID) const;

void setSpriteAlphaTest( const F32 alphaTestMode );
F32 getSpriteAlphaTest( void ) const;
Expand Down
44 changes: 44 additions & 0 deletions engine/source/2d/core/SpriteBatchItem.cc
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ void SpriteBatchItem::resetState( void )
mBlendColor = ColorF(1.0f,1.0f,1.0f,1.0f);
mAlphaTest = -1.0f;

mUseComplexColor = false;
mComplexColor0 = ColorF(1.0f, 1.0f, 1.0f, 1.0f);
mComplexColor1 = ColorF(1.0f, 1.0f, 1.0f, 1.0f);
mComplexColor2 = ColorF(1.0f, 1.0f, 1.0f, 1.0f);
mComplexColor3 = ColorF(1.0f, 1.0f, 1.0f, 1.0f);

mDataObject = NULL;

mLocalTransformDirty = true;
Expand Down Expand Up @@ -183,6 +189,32 @@ void SpriteBatchItem::copyTo( SpriteBatchItem* pSpriteBatchItem ) const

//------------------------------------------------------------------------------

const ColorF& SpriteBatchItem::getComplexColor(const S8 cornerID)
{
if (cornerID == 1)
{
return mComplexColor0;
}
else if (cornerID == 2)
{
return mComplexColor1;
}
else if (cornerID == 3)
{
return mComplexColor2;
}
else if (cornerID == 4)
{
return mComplexColor3;
}
else
{
return ColorF(1.0f, 1.0f, 1.0f, 1.0f);
}
}

//------------------------------------------------------------------------------

void SpriteBatchItem::prepareRender( SceneRenderRequest* pSceneRenderRequest, const U32 batchTransformId )
{
// Debug Profiling.
Expand Down Expand Up @@ -244,6 +276,18 @@ void SpriteBatchItem::render( BatchRender* pBatchRenderer, const SceneRenderRequ
mExplicitUVs[3],
pBatchRenderer);
}
else if (mUseComplexColor) {
Parent::renderComplex(mFlipX, mFlipY,
mRenderOOBB[0],
mRenderOOBB[1],
mRenderOOBB[2],
mRenderOOBB[3],
pBatchRenderer,
mComplexColor0,
mComplexColor1,
mComplexColor2,
mComplexColor3);
}
else {
Parent::render(mFlipX, mFlipY,
mRenderOOBB[0],
Expand Down
11 changes: 11 additions & 0 deletions engine/source/2d/core/SpriteBatchItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,12 @@ class SpriteBatchItem : public ImageFrameProvider
ColorF mBlendColor;
F32 mAlphaTest;

bool mUseComplexColor;
ColorF mComplexColor0;
ColorF mComplexColor1;
ColorF mComplexColor2;
ColorF mComplexColor3;

SimObjectPtr<SimObject> mDataObject;

Vector2 mLocalOOBB[4];
Expand Down Expand Up @@ -314,6 +320,11 @@ class SpriteBatchItem : public ImageFrameProvider
inline void setBlendAlpha( const F32 alpha ) { mBlendColor.alpha = alpha; }
inline F32 getBlendAlpha( void ) const { return mBlendColor.alpha; }

inline void setUseComplexColor(const bool complexColor) { mUseComplexColor = complexColor; }
inline bool getUseComplexColor(void) const { return mUseComplexColor; }
inline void setComplexColor(const ColorF& blendColor0, const ColorF& blendColor1, const ColorF& blendColor2, const ColorF& blendColor3) { mComplexColor0 = blendColor0; mComplexColor1 = blendColor1; mComplexColor2 = blendColor2; mComplexColor3 = blendColor3; }
const ColorF& getComplexColor(const S8 cornerID);

inline void setAlphaTest( const F32 alphaTest ) { mAlphaTest = alphaTest; }
inline F32 getAlphaTest( void ) const { return mAlphaTest; }

Expand Down
Loading

0 comments on commit 3461e66

Please sign in to comment.