Merge pull request #8682 from super626/fixbillboard

Fixbillboard
This commit is contained in:
minggo 2014-10-16 12:04:50 +08:00
commit fef079aee6
3 changed files with 37 additions and 11 deletions

View File

@ -101,13 +101,17 @@ void BillBoard::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
auto camera = Camera::getVisitingCamera(); auto camera = Camera::getVisitingCamera();
const Mat4& camWorldMat = camera->getNodeToWorldTransform(); const Mat4& camWorldMat = camera->getNodeToWorldTransform();
if (memcmp(_camWorldMat.m, camWorldMat.m, sizeof(float) * 16) != 0 || memcmp(_transform.m, transform.m, sizeof(float) * 16) != 0 || _modeDirty) if (memcmp(_camWorldMat.m, camWorldMat.m, sizeof(float) * 16) != 0 || memcmp(_mvTransform.m, transform.m, sizeof(float) * 16) != 0 || _modeDirty)
{ {
Vec3 anchorPoint(_anchorPointInPoints.x , _anchorPointInPoints.y , 0.0f);
Mat4 localToWorld = transform;
localToWorld.translate(anchorPoint);
Vec3 camDir; Vec3 camDir;
switch (_mode) switch (_mode)
{ {
case Mode::VIEW_POINT_ORIENTED: case Mode::VIEW_POINT_ORIENTED:
camDir = Vec3(transform.m[12] - camWorldMat.m[12], transform.m[13] - camWorldMat.m[13], transform.m[14] - camWorldMat.m[14]); camDir = Vec3(localToWorld.m[12] - camWorldMat.m[12], localToWorld.m[13] - camWorldMat.m[13], localToWorld.m[14] - camWorldMat.m[14]);
break; break;
case Mode::VIEW_PLANE_ORIENTED: case Mode::VIEW_PLANE_ORIENTED:
camWorldMat.transformVector(Vec3(0.0f, 0.0f, -1.0f), &camDir); camWorldMat.transformVector(Vec3(0.0f, 0.0f, -1.0f), &camDir);
@ -130,18 +134,22 @@ void BillBoard::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
Vec3::cross(camDir, y, &x); Vec3::cross(camDir, y, &x);
x.normalize(); x.normalize();
Vec3::cross(x, camDir, &y); Vec3::cross(x, camDir, &y);
float xlen = sqrtf(transform.m[0] * transform.m[0] + transform.m[1] * transform.m[1] + transform.m[2] * transform.m[2]); y.normalize();
float ylen = sqrtf(transform.m[4] * transform.m[4] + transform.m[5] * transform.m[5] + transform.m[6] * transform.m[6]);
float zlen = sqrtf(transform.m[8] * transform.m[8] + transform.m[9] * transform.m[9] + transform.m[10] * transform.m[10]); float xlen = sqrtf(localToWorld.m[0] * localToWorld.m[0] + localToWorld.m[1] * localToWorld.m[1] + localToWorld.m[2] * localToWorld.m[2]);
float ylen = sqrtf(localToWorld.m[4] * localToWorld.m[4] + localToWorld.m[5] * localToWorld.m[5] + localToWorld.m[6] * localToWorld.m[6]);
float zlen = sqrtf(localToWorld.m[8] * localToWorld.m[8] + localToWorld.m[9] * localToWorld.m[9] + localToWorld.m[10] * localToWorld.m[10]);
_billboardTransform.m[0] = x.x * xlen; _billboardTransform.m[1] = x.y * xlen; _billboardTransform.m[2] = x.z * xlen; _billboardTransform.m[0] = x.x * xlen; _billboardTransform.m[1] = x.y * xlen; _billboardTransform.m[2] = x.z * xlen;
_billboardTransform.m[4] = y.x * ylen; _billboardTransform.m[5] = y.y * ylen; _billboardTransform.m[6] = y.z * ylen; _billboardTransform.m[4] = y.x * ylen; _billboardTransform.m[5] = y.y * ylen; _billboardTransform.m[6] = y.z * ylen;
_billboardTransform.m[8] = -camDir.x * zlen; _billboardTransform.m[9] = -camDir.y * zlen; _billboardTransform.m[10] = -camDir.z * zlen; _billboardTransform.m[8] = -camDir.x * zlen; _billboardTransform.m[9] = -camDir.y * zlen; _billboardTransform.m[10] = -camDir.z * zlen;
_billboardTransform.m[12] = transform.m[12]; _billboardTransform.m[13] = transform.m[13]; _billboardTransform.m[14] = transform.m[14]; _billboardTransform.m[12] = localToWorld.m[12]; _billboardTransform.m[13] = localToWorld.m[13]; _billboardTransform.m[14] = localToWorld.m[14];
_billboardTransform.translate(-anchorPoint);
const Mat4 &viewMat = camWorldMat.getInversed(); const Mat4 &viewMat = camWorldMat.getInversed();
_zDepthInView = -(viewMat.m[2] * transform.m[12] + viewMat.m[6] * transform.m[13] + viewMat.m[10] * transform.m[14] + viewMat.m[14]); _zDepthInView = -(viewMat.m[2] * _billboardTransform.m[12] + viewMat.m[6] * _billboardTransform.m[13] + viewMat.m[10] * _billboardTransform.m[14] + viewMat.m[14]);
_transform = transform; _mvTransform = transform;
_camWorldMat = camWorldMat; _camWorldMat = camWorldMat;
} }

View File

@ -99,7 +99,7 @@ CC_CONSTRUCTOR_ACCESS:
protected: protected:
Mat4 _camWorldMat; Mat4 _camWorldMat;
Mat4 _transform; Mat4 _mvTransform;
Mat4 _billboardTransform; Mat4 _billboardTransform;
float _zDepthInView; float _zDepthInView;

View File

@ -105,6 +105,24 @@ BillBoardTest::BillBoardTest()
layer->runAction( RepeatForever::create( RotateBy::create( CCRANDOM_0_1(), Vec3(0.0f, 45.0f, 0.0f) ) ) ); layer->runAction( RepeatForever::create( RotateBy::create( CCRANDOM_0_1(), Vec3(0.0f, 45.0f, 0.0f) ) ) );
} }
{
auto billboard = BillBoard::create("Images/Icon.png");
billboard->setScale(0.2f);
billboard->setPosition3D(Vec3(0.0f, 30.0f, 0.0f));
auto billboard2 = BillBoard::create("Images/r2.png");
billboard2->setPosition3D(Vec3(0.0f, 0.0f, 100.0f));
billboard->addChild(billboard2);
_billboards.push_back(billboard);
_billboards.push_back(billboard2);
auto sprite3d = Sprite3D::create("Sprite3DTest/orc.c3t");
sprite3d->setScale(2.0f);
sprite3d->addChild(billboard);
sprite3d->runAction( RepeatForever::create( RotateBy::create( 10.0f, Vec3(0.0f, 360.0f, 0.0f) ) ) );
_layerBillBorad->addChild(sprite3d);
}
addNewBillBoradWithCoords(Vec3(20,5,0)); addNewBillBoradWithCoords(Vec3(20,5,0));
addNewBillBoradWithCoords(Vec3(60,5,0)); addNewBillBoradWithCoords(Vec3(60,5,0));
addNewBillBoradWithCoords(Vec3(100,5,0)); addNewBillBoradWithCoords(Vec3(100,5,0));
@ -115,8 +133,8 @@ BillBoardTest::BillBoardTest()
addNewAniBillBoradWithCoords(Vec3(-100,0,0)); addNewAniBillBoradWithCoords(Vec3(-100,0,0));
addNewAniBillBoradWithCoords(Vec3(-140,0,0)); addNewAniBillBoradWithCoords(Vec3(-140,0,0));
addNewAniBillBoradWithCoords(Vec3(-180,0,0)); addNewAniBillBoradWithCoords(Vec3(-180,0,0));
_camera->setPosition3D(Vec3(0, 130, 230)); _camera->setPosition3D(Vec3(0, 100, 230));
_camera->lookAt(Vec3(0,0,100), Vec3(0,1,0)); _camera->lookAt(Vec3(0,0,0), Vec3(0,1,0));
TTFConfig ttfConfig("fonts/arial.ttf", 16); TTFConfig ttfConfig("fonts/arial.ttf", 16);
auto label1 = Label::createWithTTF(ttfConfig,"rotate+"); auto label1 = Label::createWithTTF(ttfConfig,"rotate+");