Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUGFIX] Consecutive Masked Sprites use only the first's mask #138 #139

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 24 additions & 35 deletions oxygine/src/oxygine/STDRenderDelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ namespace oxygine
driver->setScissorRect(scissorEnabled ? &scissorRect : 0);
}
}

static std::list<Vector4> clipMask_stack;
static std::list<ClipUV> msk_stack;
void STDRenderDelegate::render(MaskedSprite* sprite, const RenderState& parentRS)
{
spSprite maskSprite = sprite->getMask();
Expand All @@ -109,6 +110,7 @@ namespace oxygine

Material::null->apply();

Vector3 msk[4];
Transform world = maskSprite->computeGlobalTransform();

RectF maskDest = maskSprite->getDestRect();
Expand All @@ -118,12 +120,16 @@ namespace oxygine
bool useR = sprite->getUseRChannel();
bool rchannel = useR ? true : (df.alpha ? true : false);
spNativeTexture maskTexture = useR ? df.base : (df.alpha ? df.alpha : df.base);

STDRenderer* renderer = STDRenderer::getCurrent();

int sflags = renderer->getBaseShaderFlags();
int baseShaderFlags = sflags;

baseShaderFlags |= UberShaderProgram::MASK;
if (rchannel)
baseShaderFlags |= UberShaderProgram::MASK_R_CHANNEL;

#if 1
spNativeTexture oldMask = rsCache().getTexture(UberShaderProgram::SAMPLER_MASK);
ClipUV clipUV = ClipUV(
world.transform(maskDest.getLeftTop()),
world.transform(maskDest.getRightTop()),
Expand All @@ -135,61 +141,44 @@ namespace oxygine
Vector2 v(1.0f / maskTexture->getWidth(), 1.0f / maskTexture->getHeight());
maskSrc.expand(v, v);



int sflags = renderer->getBaseShaderFlags();
int baseShaderFlags = sflags;

baseShaderFlags |= UberShaderProgram::MASK;
if (rchannel)
baseShaderFlags |= UberShaderProgram::MASK_R_CHANNEL;

Vector3 msk[4];

clipUV.get(msk);
Vector4 clipMask = Vector4(maskSrc.getLeft(), maskSrc.getTop(), maskSrc.getRight(), maskSrc.getBottom());

rsCache().setTexture(UberShaderProgram::SAMPLER_MASK, maskTexture);
clipMask_stack.push_back(clipMask);
msk_stack.push_back(clipUV);


rsCache().setTexture(UberShaderProgram::SAMPLER_MASK, maskTexture);


ShaderProgramChangedHook hook;
hook.hook = [&]()
{

IVideoDriver::instance->setUniform("clip_mask", clipMask);
IVideoDriver::instance->setUniform("msk", msk, 4);

};


renderer->pushShaderSetHook(&hook);
renderer->setBaseShaderFlags(baseShaderFlags);
hook.hook();

sprite->Sprite::render(parentRS);

Material::null->apply();

renderer->popShaderSetHook();
renderer->setBaseShaderFlags(sflags);
#else

MaskedRenderer mr(maskTexture, maskSrc, maskDest, world, rchannel, renderer->getDriver());
renderer->swapVerticesData(mr);

mr.setViewProj(renderer->getViewProjection());
mr.begin();

RenderState rs = parentRS;
sprite->Sprite::render(rs);
mr.end();

Material::null->apply();

renderer->swapVerticesData(mr);
renderer->begin();
#endif

if (oldMask) rsCache().setTexture(UberShaderProgram::SAMPLER_MASK,oldMask);
clipMask_stack.pop_back();
if (!clipMask_stack.empty())
IVideoDriver::instance->setUniform("clip_mask", clipMask_stack.back());
msk_stack.pop_back();
if (!msk_stack.empty()) {
msk_stack.back().get(msk);
IVideoDriver::instance->setUniform("msk", msk, 4);
}
}

void STDRenderDelegate::doRender(Sprite* sprite, const RenderState& rs)
Expand Down Expand Up @@ -236,4 +225,4 @@ namespace oxygine
{

}
}
}