simplify compute global z when it is transparent

This commit is contained in:
yangxiao 2014-10-17 11:52:39 +08:00
parent 87850d0378
commit 9e1656e5b0
1 changed files with 11 additions and 8 deletions

View File

@ -32,6 +32,7 @@
#include "base/CCDirector.h"
#include "base/CCLight.h"
#include "base/CCCamera.h"
#include "base/ccMacros.h"
#include "platform/CCPlatformMacros.h"
#include "platform/CCFileUtils.h"
@ -514,13 +515,14 @@ void Sprite3D::removeAllAttachNode()
//Generate a dummy texture when the texture file is missing
static Texture2D * getDummyTexture()
{
static Texture2D * texture=NULL;
auto texture = Director::getInstance()->getTextureCache()->getTextureForKey("/dummyTexture");
if(!texture)
{
unsigned char data[] ={255,0,0,255};//1*1 pure red picture
Image * image =new Image();
Image * image =new (std::nothrow) Image();
image->initWithRawData(data,sizeof(data),1,1,sizeof(unsigned char));
texture=TextureCache::sharedTextureCache()->addImage(image,"");
texture=Director::getInstance()->getTextureCache()->addImage(image,"/dummyTexture");
image->release();
}
return texture;
}
@ -564,15 +566,16 @@ void Sprite3D::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
}else
{ //let the mesh use a dummy texture instead of the missing or crashing texture file
auto texture = getDummyTexture();
textureID = texture->getName();
mesh->setTexture(texture);
textureID = texture->getName();
}
#endif
float globalZ = _globalZOrder;
if (mesh->_isTransparent)
bool isTransparent = (mesh->_isTransparent || color.a < 1.f);
if (isTransparent && Camera::getVisitingCamera())
{ // use the view matrix for Applying to recalculating transparent mesh's Z-Order
Mat4 result = Camera::getVisitingCamera()->getViewMatrix() *transform;
globalZ = -result.m[14];//fetch the Z from the result matrix
const auto& viewMat = Camera::getVisitingCamera()->getViewMatrix();
globalZ = -(viewMat.m[2] * transform.m[12] + viewMat.m[6] * transform.m[13] + viewMat.m[10] * transform.m[14] + viewMat.m[14]);//fetch the Z from the result matrix
}
meshCommand.init(globalZ, textureID, programstate, _blend, mesh->getVertexBuffer(), mesh->getIndexBuffer(), mesh->getPrimitiveType(), mesh->getIndexFormat(), mesh->getIndexCount(), transform);
@ -586,7 +589,7 @@ void Sprite3D::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
}
//support tint and fade
meshCommand.setDisplayColor(Vec4(color.r, color.g, color.b, color.a));
meshCommand.setTransparent(mesh->_isTransparent);
meshCommand.setTransparent(isTransparent);
renderer->addCommand(&meshCommand);
}
}