CCPURender.cpp 36.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
/****************************************************************************
 Copyright (C) 2013 Henry van Merode. All rights reserved.
 Copyright (c) 2015-2016 Chukong Technologies Inc.
 Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
 
 http://www.cocos2d-x.org
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 ****************************************************************************/
#include "extensions/Particle3D/CCParticleSystem3D.h"
#include <stddef.h> // offsetof
#include "base/ccTypes.h"
#include "extensions/Particle3D/PU/CCPURender.h"
#include "extensions/Particle3D/PU/CCPUParticleSystem3D.h"
#include "extensions/Particle3D/PU/CCPUUtil.h"
#include "renderer/CCMeshCommand.h"
#include "renderer/CCRenderer.h"
#include "renderer/CCTextureCache.h"
#include "renderer/ccShaders.h"
#include "renderer/backend/Device.h"
#include "renderer/backend/Buffer.h"
#include "base/CCDirector.h"
#include "3d/CCSprite3D.h"
#include "3d/CCMesh.h"
#include "2d/CCCamera.h"

NS_CC_BEGIN

void PURender::updateRender(PUParticle3D* /*particle*/, float /*deltaTime*/, bool /*firstParticle*/)
{}

void PURender::copyAttributesTo( PURender *render )
{
    Particle3DRender::copyAttributesTo(render);

    render->_renderType = _renderType;
}

//static bool compareParticle3D(PUParticle3D* left, PUParticle3D* right)
//{
//    return left->depthInView > right->depthInView;
//}

PUParticle3DQuadRender* PUParticle3DQuadRender::create(const std::string& texFile)
{
    auto ret = new (std::nothrow) PUParticle3DQuadRender();
    if (ret && ret->initRender(texFile))
    {
        ret->_texFile = texFile;
        ret->autorelease();
    }
    else
    {
        CC_SAFE_DELETE(ret);
    }
    return ret;
}

void PUParticle3DQuadRender::render(Renderer* renderer, const Mat4 &transform, ParticleSystem3D* particleSystem)
{
    //batch and generate draw
    const ParticlePool &particlePool = particleSystem->getParticlePool();
    if (!_isVisible || particlePool.empty())
        return;
    
    if (_vertexBuffer == nullptr){
        size_t stride = sizeof(VertexInfo);
        _vertexBuffer = backend::Device::getInstance()->newBuffer(stride * 4 * particleSystem->getParticleQuota(), backend::BufferType::VERTEX, backend::BufferUsage::DYNAMIC);
        if (_vertexBuffer == nullptr)
        {
            CCLOG("PUParticle3DQuadRender::render create vertex buffer failed");
            return;
        }
    }

    if (_indexBuffer == nullptr){
        _indexBuffer = backend::Device::getInstance()->newBuffer(6 * particleSystem->getParticleQuota()* sizeof(uint16_t), backend::BufferType::INDEX, backend::BufferUsage::DYNAMIC);
        if (_indexBuffer == nullptr)
        {
            CCLOG("PUParticle3DQuadRender::render create index buffer failed");
            return;
        }
    }
    const ParticlePool::PoolList &activeParticleList = particlePool.getActiveDataList();
    if (_vertices.size() < activeParticleList.size() * 4)
    {
        _vertices.resize(activeParticleList.size() * 4);
        _indices.resize(activeParticleList.size() * 6);
    }

    auto camera = Camera::getVisitingCamera();
    auto cameraMat = camera->getNodeToWorldTransform();
    

    //for (auto iter : activeParticleList){
    //    iter->depthInView = -(viewMat.m[2] * iter->positionInWorld.x + viewMat.m[6] * iter->positionInWorld.y + viewMat.m[10] * iter->positionInWorld.z + viewMat.m[14]);
    //}

    //std::sort(activeParticleList.begin(), activeParticleList.end(), compareParticle3D);
    Vec3 right(cameraMat.m[0], cameraMat.m[1], cameraMat.m[2]);
    Vec3 up(cameraMat.m[4], cameraMat.m[5], cameraMat.m[6]);
    Vec3 backward(cameraMat.m[8], cameraMat.m[9], cameraMat.m[10]);
    
    Mat4 pRotMat;
    Vec3 position; //particle position
    int vertexindex = 0;
    int index = 0;
    int offsetX,offsetY;
    getOriginOffset(offsetX, offsetY);

    if (_type == PERPENDICULAR_COMMON){
        up = _commonUp;
        up.normalize();
        Vec3::cross(up, _commonDir, &right);
        right.normalize();
        backward = _commonDir;
    }else if (_type == ORIENTED_COMMON){
        up = _commonDir;
        up.normalize();
        Vec3::cross(up, backward, &right);
        right.normalize();
    }

    for (auto iter : activeParticleList)
    {
        auto particle = static_cast<PUParticle3D *>(iter);
        determineUVCoords(particle);
        if (_type == ORIENTED_SELF){
            Vec3 direction = particle->direction;
            //transform.transformVector(particle->direction, &direction);
            up = direction;
            up.normalize();
            Vec3::cross(direction, backward, &right);
            right.normalize();
        }else if (_type == PERPENDICULAR_SELF){
            Vec3 direction = particle->direction;
            //transform.transformVector(particle->direction, &direction);
            direction.normalize();
            //up = PUUtil::perpendicular(direction);
            //up.normalize();
            Vec3::cross(_commonUp, direction, &right);
            right.normalize();
            Vec3::cross(direction, right, &up);
            up.normalize();
            backward = direction;
        }else if (_type == ORIENTED_SHAPE){
            up.set(particle->orientation.x, particle->orientation.y, particle->orientation.z);
            up.normalize();
            Vec3::cross(up, backward, &right);
            right.normalize();
        }
        Vec3 halfwidth = particle->width * 0.5f * right;
        Vec3 halfheight = particle->height * 0.5f * up;
        Vec3 offset = halfwidth * offsetX + halfheight * offsetY;
        //transform.transformPoint(particle->position, &position);
        position = particle->position;

        if (_rotateType == TEXTURE_COORDS){
            float costheta = cosf(-particle->zRotation);
            float sintheta = sinf(-particle->zRotation);
            Vec2 texOffset = 0.5f * (particle->lb_uv + particle->rt_uv);
            Vec2 val;
            val.set((particle->lb_uv.x - texOffset.x), (particle->lb_uv.y - texOffset.y));
            val.set(val.x * costheta - val.y * sintheta, val.x * sintheta + val.y * costheta);
            fillVertex(vertexindex, (position + (-halfwidth - halfheight + offset)), particle->color, val + texOffset);

            val.set(particle->rt_uv.x - texOffset.x, particle->lb_uv.y - texOffset.y);
            val.set(val.x * costheta - val.y * sintheta, val.x * sintheta + val.y * costheta);
            fillVertex(vertexindex + 1, (position + (halfwidth - halfheight + offset)), particle->color, val + texOffset);

            val.set(particle->lb_uv.x - texOffset.x, particle->rt_uv.y - texOffset.y);
            val.set(val.x * costheta - val.y * sintheta, val.x * sintheta + val.y * costheta);
            fillVertex(vertexindex + 2, (position + (-halfwidth + halfheight + offset)), particle->color, val + texOffset);

            val.set(particle->rt_uv.x - texOffset.x, particle->rt_uv.y - texOffset.y);
            val.set(val.x * costheta - val.y * sintheta, val.x * sintheta + val.y * costheta);
            fillVertex(vertexindex + 3, (position + (halfwidth + halfheight + offset)), particle->color, val + texOffset);
        }else{
            Mat4::createRotation(backward, -particle->zRotation, &pRotMat);
            fillVertex(vertexindex    , (position + pRotMat * (- halfwidth - halfheight + offset)), particle->color, particle->lb_uv);
            fillVertex(vertexindex + 1, (position + pRotMat * (halfwidth - halfheight + offset)), particle->color, Vec2(particle->rt_uv.x, particle->lb_uv.y));
            fillVertex(vertexindex + 2, (position + pRotMat * (-halfwidth + halfheight + offset)), particle->color, Vec2(particle->lb_uv.x, particle->rt_uv.y));
            fillVertex(vertexindex + 3, (position + pRotMat * (halfwidth + halfheight + offset)), particle->color, particle->rt_uv);
        }

        fillTriangle(index, vertexindex, vertexindex + 1, vertexindex + 3);
        fillTriangle(index + 3, vertexindex, vertexindex + 3, vertexindex + 2);

        //_posuvcolors[vertexindex].position = (position + (- halfwidth - halfheight + halfwidth * offsetX + halfheight * offsetY));
        //_posuvcolors[vertexindex].color = particle->color;
        //_posuvcolors[vertexindex].uv.set(val.x + texOffset.x, val.y + texOffset.y);

        //val.set(particle->rt_uv.x - texOffset.x, particle->lb_uv.y - texOffset.y);
        //val.set(val.x * costheta - val.y * sintheta, val.x * sintheta + val.y * costheta);
        //_posuvcolors[vertexindex + 1].position = (position + (halfwidth - halfheight + halfwidth * offsetX + halfheight * offsetY));
        //_posuvcolors[vertexindex + 1].color = particle->color;
        //_posuvcolors[vertexindex + 1].uv.set(val.x + texOffset.x, val.y + texOffset.y);
        //
        //val.set(particle->lb_uv.x - texOffset.x, particle->rt_uv.y - texOffset.y);
        //val.set(val.x * costheta - val.y * sintheta, val.x * sintheta + val.y * costheta);
        //_posuvcolors[vertexindex + 2].position = (position + (- halfwidth + halfheight + halfwidth * offsetX + halfheight * offsetY));
        //_posuvcolors[vertexindex + 2].color = particle->color;
        //_posuvcolors[vertexindex + 2].uv.set(val.x + texOffset.x, val.y + texOffset.y);
        //
        //val.set(particle->rt_uv.x - texOffset.x, particle->rt_uv.y - texOffset.y);
        //val.set(val.x * costheta - val.y * sintheta, val.x * sintheta + val.y * costheta);
        //_posuvcolors[vertexindex + 3].position = (position + (halfwidth + halfheight + halfwidth * offsetX + halfheight * offsetY));
        //_posuvcolors[vertexindex + 3].color = particle->color;
        //_posuvcolors[vertexindex + 3].uv.set(val.x + texOffset.x, val.y + texOffset.y);
        //
        //
        //_indexData[index] = vertexindex;
        //_indexData[index + 1] = vertexindex + 1;
        //_indexData[index + 2] = vertexindex + 3;
        //_indexData[index + 3] = vertexindex;
        //_indexData[index + 4] = vertexindex + 3;
        //_indexData[index + 5] = vertexindex + 2;
        
        index += 6;
        vertexindex += 4;

    }
    
    _vertices.erase(_vertices.begin() + vertexindex, _vertices.end());
    _indices.erase(_indices.begin() + index, _indices.end());
    
    if (!_vertices.empty() && !_indices.empty()){
        _vertexBuffer->updateData(&_vertices[0], vertexindex * sizeof(_vertices[0]));
        _indexBuffer->updateData(&_indices[0], index * sizeof(_indices[0]));

        _stateBlock.setBlendFunc(particleSystem->getBlendFunc());
        
        _meshCommand.init(0.0);
        _meshCommand.setSkipBatching(true);
        _meshCommand.setTransparent(true);

        _meshCommand.setVertexBuffer(_vertexBuffer);
        _meshCommand.setIndexBuffer(_indexBuffer, MeshCommand::IndexFormat::U_SHORT);
        _meshCommand.setIndexDrawInfo(0, index);

        if (_texture)
        {
            _programState->setTexture(_locTexture, 0, _texture->getBackendTexture());
        }

        auto uColor = Vec4(1, 1, 1, 1);
        _programState->setUniform(_locColor, &uColor, sizeof(uColor));

        auto &projectionMatrix = Director::getInstance()->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
        _programState->setUniform(_locPMatrix, &projectionMatrix.m, sizeof(projectionMatrix.m));

        renderer->addCommand(&_meshCommand);
    }
}

void PUParticle3DEntityRender::onBeforeDraw()
{
    auto *renderer = Director::getInstance()->getRenderer();
    auto &pipelineDescriptor = _meshCommand.getPipelineDescriptor();
    _rendererDepthTestEnabled = renderer->getDepthTest();
    _rendererDepthCmpFunc = renderer->getDepthCompareFunction();
    _rendererCullMode = renderer->getCullMode();
    _rendererDepthWrite = renderer->getDepthWrite();
    _rendererWinding = renderer->getWinding();
    _stateBlock.bind(&pipelineDescriptor);
    renderer->setDepthTest(true);
}

void PUParticle3DEntityRender::onAfterDraw()
{
    auto *renderer = Director::getInstance()->getRenderer();
    renderer->setDepthTest(_rendererDepthTestEnabled);
    renderer->setDepthCompareFunction(_rendererDepthCmpFunc);
    renderer->setCullMode(_rendererCullMode);
    renderer->setDepthWrite(_rendererDepthWrite);
    renderer->setWinding(_rendererWinding);
}

PUParticle3DQuadRender::PUParticle3DQuadRender()
    : _type(POINT)
    , _origin(CENTER)
    , _rotateType(TEXTURE_COORDS)
    , _commonDir(Vec3(0.0f, 0.0f, 1.0f))
    , _commonUp(Vec3(0.0f, 1.0f, 0.0f))
    , _textureCoordsRows(1)
    , _textureCoordsColumns(1)
    , _textureCoordsRowStep(1.0f)
    , _textureCoordsColStep(1.0f)
{
    autoRotate = false;
}

PUParticle3DQuadRender::~PUParticle3DQuadRender()
{
}

void PUParticle3DQuadRender::getOriginOffset( int &offsetX, int &offsetY )
{
    switch (_origin)
    {
    case TOP_LEFT:
        {
            offsetX = 1;
            offsetY = -1;
        }
        break;

    case TOP_CENTER:
        {
            offsetX = 0;
            offsetY = -1;
        }
        break;

    case TOP_RIGHT:
        {
            offsetX = -1;
            offsetY = -1;
        }
        break;

    case CENTER_LEFT:
        {
            offsetX = 1;
            offsetY = 0;
        }
        break;

    case CENTER:
        {
            offsetX = 0;
            offsetY = 0;
        }
        break;

    case CENTER_RIGHT:
        {
            offsetX = -1;
            offsetY = 0;
        }
        break;

    case BOTTOM_LEFT:
        {
            offsetX = 1;
            offsetY = 1;
        }
        break;

    case BOTTOM_CENTER:
        {
            offsetX = 0;
            offsetY = 1;
        }
        break;

    case BOTTOM_RIGHT:
        {
            offsetX = -1;
            offsetY = 1;
        }
        break;
    }
}

unsigned short PUParticle3DQuadRender::getTextureCoordsRows() const
{
    return _textureCoordsRows;
}

void PUParticle3DQuadRender::setTextureCoordsRows( unsigned short textureCoordsRows )
{
    _textureCoordsRows = textureCoordsRows;
    _textureCoordsRowStep = 1.0f / (float)_textureCoordsRows;
}

unsigned short PUParticle3DQuadRender::getTextureCoordsColumns() const
{
    return _textureCoordsColumns;
}

void PUParticle3DQuadRender::setTextureCoordsColumns( unsigned short textureCoordsColumns )
{
    _textureCoordsColumns = textureCoordsColumns;
    _textureCoordsColStep = 1.0f / (float)_textureCoordsColumns;
}

unsigned int PUParticle3DQuadRender::getNumTextureCoords()
{
    return _textureCoordsColumns * _textureCoordsRows;
}

void PUParticle3DQuadRender::determineUVCoords( PUParticle3D *particle )
{
    if (_textureCoordsColumns == 1 && _textureCoordsRows == 1) return;

    unsigned short currentRow = particle->textureCoordsCurrent / _textureCoordsColumns;
    unsigned short currentCol = particle->textureCoordsCurrent - _textureCoordsColumns * currentRow;
    currentRow = _textureCoordsRows - currentRow - 1;

    particle->lb_uv.set(_textureCoordsColStep * currentCol, _textureCoordsRowStep * currentRow);
    particle->rt_uv = particle->lb_uv + Vec2(_textureCoordsColStep, _textureCoordsRowStep);
}

void PUParticle3DQuadRender::fillVertex( unsigned short index, const Vec3 &pos, const Vec4 &color, const Vec2 &uv )
{
    _vertices[index].position = pos;
    _vertices[index].color = color;
    _vertices[index].uv = uv;
}

void PUParticle3DQuadRender::fillTriangle( unsigned short index, unsigned short v0, unsigned short v1, unsigned short v2 )
{
    _indices[index] = v0;
    _indices[index + 1] = v1;
    _indices[index + 2] = v2;
}

void PUParticle3DQuadRender::setType( Type type )
{
    _type = type;
    if (_type == PERPENDICULAR_COMMON || _type == PERPENDICULAR_SELF){
        _stateBlock.setCullFace(false);
    }else{
        _stateBlock.setCullFace(true);
    }
}

void PUParticle3DQuadRender::copyAttributesTo(PUParticle3DQuadRender *quadRender)
{
    PURender::copyAttributesTo(quadRender);
    quadRender->_type = _type;
    quadRender->_origin = _origin;
    quadRender->_rotateType = _rotateType;
    quadRender->_commonDir = _commonDir;
    quadRender->_commonUp = _commonUp;
    quadRender->_textureCoordsRows = _textureCoordsRows;
    quadRender->_textureCoordsColumns = _textureCoordsColumns;
    quadRender->_textureCoordsRowStep = _textureCoordsRowStep;
    quadRender->_textureCoordsColStep = _textureCoordsColStep;
}

PUParticle3DQuadRender* PUParticle3DQuadRender::clone()
{
    auto render = PUParticle3DQuadRender::create(_texFile);
    copyAttributesTo(render);
    return render;
}

PUParticle3DModelRender* PUParticle3DModelRender::create( const std::string& modelFile, const std::string &texFile /*= ""*/ )
{
    auto ret = new (std::nothrow) PUParticle3DModelRender();
    ret->_modelFile = modelFile;
    ret->_texFile = texFile;
    return ret;
}

void PUParticle3DModelRender::render( Renderer* renderer, const Mat4 &transform, ParticleSystem3D* particleSystem )
{
    if (!_isVisible)
        return;

    if (_spriteList.empty()){
        for (unsigned int i = 0; i < particleSystem->getParticleQuota(); ++i){
            Sprite3D *sprite = Sprite3D::create(_modelFile);
            if (sprite == nullptr)
            {
                CCLOG("failed to load file %s", _modelFile.c_str());
                continue;
            }
            sprite->setTexture(_texFile);
            sprite->setBlendFunc(particleSystem->getBlendFunc());
            sprite->setCullFaceEnabled(false);
            sprite->retain();
            _spriteList.push_back(sprite);
        }
        if (!_spriteList.empty()){
            const AABB &aabb = _spriteList[0]->getAABB();
            Vec3 corners[8];
            aabb.getCorners(corners);
            _spriteSize = corners[3] - corners[6];
        }else{
            _isVisible = false;
            return;
        }
    }


    const ParticlePool& particlePool = particleSystem->getParticlePool();
    ParticlePool::PoolList activeParticleList = particlePool.getActiveDataList();
    Mat4 mat;
    Mat4 rotMat;
    Mat4 sclMat;
    Quaternion q;
    transform.decompose(nullptr, &q, nullptr);
    unsigned int index = 0;
    for (auto iter : activeParticleList)
    {
        auto particle = static_cast<PUParticle3D *>(iter);
        Mat4::createRotation(q * particle->orientation, &rotMat);
        sclMat.m[0] = particle->width / _spriteSize.x;
        sclMat.m[5]  = particle->height / _spriteSize.y; 
        sclMat.m[10] = particle->depth / _spriteSize.z;
        mat = rotMat * sclMat;
        mat.m[12] = particle->position.x;
        mat.m[13] = particle->position.y;
        mat.m[14] = particle->position.z;
        if (_spriteList[index]->getCameraMask() != particleSystem->getCameraMask())
            _spriteList[index]->setCameraMask(particleSystem->getCameraMask());
        _spriteList[index]->setColor(Color3B(particle->color.x * 255, particle->color.y * 255, particle->color.z * 255));
        _spriteList[index]->setOpacity(particle->color.w * 255);
        _spriteList[index]->visit(renderer, mat, Node::FLAGS_DIRTY_MASK);
        ++index;
    }
}

PUParticle3DModelRender::PUParticle3DModelRender()
{
    autoRotate = true;
}

PUParticle3DModelRender::~PUParticle3DModelRender()
{
    for (auto iter : _spriteList){
        iter->release();
    }
}

void PUParticle3DModelRender::copyAttributesTo(PUParticle3DModelRender *render)
{
    PURender::copyAttributesTo(render);
}

PUParticle3DModelRender* PUParticle3DModelRender::clone()
{
    auto mr = PUParticle3DModelRender::create(_modelFile, _texFile);
    copyAttributesTo(mr);
    return mr;
}

void PUParticle3DModelRender::reset()
{
    for (auto iter : _spriteList){
        iter->release();
    }
    _spriteList.clear();
}


PUParticle3DEntityRender::PUParticle3DEntityRender()
    : _texture(nullptr)
    , _programState(nullptr)
    , _indexBuffer(nullptr)
    , _vertexBuffer(nullptr)
{
    _stateBlock.setCullFace(false);
    _stateBlock.setCullFaceSide(backend::CullMode::BACK);
    _stateBlock.setDepthTest(false);
    _stateBlock.setDepthWrite(false);
    _stateBlock.setBlend(true);
}

PUParticle3DEntityRender::~PUParticle3DEntityRender()
{;
    //CC_SAFE_RELEASE(_texture);
    CC_SAFE_RELEASE(_programState);
    CC_SAFE_RELEASE(_vertexBuffer);
    CC_SAFE_RELEASE(_indexBuffer);
}

bool PUParticle3DEntityRender::initRender( const std::string &texFile )
{
    CC_SAFE_RELEASE_NULL(_programState);
    if (!texFile.empty())
    {
        auto tex = Director::getInstance()->getTextureCache()->addImage(texFile);
        if (tex)
        {
            _texture = tex;
            auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::PARTICLE_TEXTURE_3D);
            _programState = new backend::ProgramState(program);
        }
    }

    if (!_programState)
    {
        auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::PARTICLE_COLOR_3D);
        _programState = new backend::ProgramState(program);
    }
    
    auto &pipelineDescriptor = _meshCommand.getPipelineDescriptor();
    pipelineDescriptor.programState = _programState;
    auto layout = _programState->getVertexLayout();
    const auto& attributeInfo = _programState->getProgram()->getActiveAttributes();
    auto iter = attributeInfo.find("a_position");
    if(iter != attributeInfo.end())
    {
        layout->setAttribute("a_position", iter->second.location, backend::VertexFormat::FLOAT3, offsetof(VertexInfo, position), false);
    }
    iter = attributeInfo.find("a_texCoord");
    if(iter != attributeInfo.end())
    {
        layout->setAttribute("a_texCoord", iter->second.location, backend::VertexFormat::FLOAT2, offsetof(VertexInfo, uv), false);
    }
    iter = attributeInfo.find("a_color");
    if(iter != attributeInfo.end())
    {
        layout->setAttribute("a_color", iter->second.location, backend::VertexFormat::FLOAT4, offsetof(VertexInfo, color), false);
    }
    layout->setLayout(sizeof(VertexInfo));

    _locColor = _programState->getUniformLocation("u_color");
    _locPMatrix = _programState->getUniformLocation("u_PMatrix");
    _locTexture = _programState->getUniformLocation("u_texture");

    _meshCommand.setTransparent(true);
    _meshCommand.setSkipBatching(true);

    _stateBlock.setDepthTest(true);
    _stateBlock.setDepthWrite(false);
    _stateBlock.setCullFaceSide(backend::CullMode::BACK);
    _stateBlock.setCullFace(true);

    _meshCommand.setBeforeCallback(CC_CALLBACK_0(PUParticle3DEntityRender::onBeforeDraw, this));
    _meshCommand.setAfterCallback(CC_CALLBACK_0(PUParticle3DEntityRender::onAfterDraw, this));

    return true;
}

void PUParticle3DEntityRender::copyAttributesTo(PUParticle3DEntityRender *render)
{
    PURender::copyAttributesTo(render);
}

void PUParticle3DEntityRender::reset()
{
    this->initRender(_texFile);
}

PUParticle3DBoxRender::PUParticle3DBoxRender()
{
    autoRotate = false;
}

PUParticle3DBoxRender::~PUParticle3DBoxRender()
{
}

PUParticle3DBoxRender* PUParticle3DBoxRender::create( const std::string &texFile )
{
    auto ret = new (std::nothrow) PUParticle3DBoxRender();
    if (ret && ret->initRender(texFile))
    {
        ret->autorelease();
    }
    else
    {
        CC_SAFE_DELETE(ret);
    }
    return ret;
}

void PUParticle3DBoxRender::render( Renderer* renderer, const Mat4 &transform, ParticleSystem3D* particleSystem )
{
    //batch and generate draw
    const ParticlePool &particlePool = particleSystem->getParticlePool();
    if (!_isVisible || particlePool.empty())
        return;

    auto camera = Camera::getVisitingCamera();
    auto cameraMat = camera->getNodeToWorldTransform();
    Vec3 backward(cameraMat.m[8], cameraMat.m[9], cameraMat.m[10]);

    if (_vertexBuffer == nullptr && _indexBuffer == nullptr){
        size_t stride = sizeof(VertexInfo);
        _vertexBuffer = backend::Device::getInstance()->newBuffer(stride * 8 * particleSystem->getParticleQuota(), backend::BufferType::VERTEX, backend::BufferUsage::DYNAMIC);
        if (_vertexBuffer == nullptr)
        {
            CCLOG("PUParticle3DBoxRender::render create vertex buffer failed");
            return;
        }
        _vertices.resize(8 * particleSystem->getParticleQuota());

        _indexBuffer = backend::Device::getInstance()->newBuffer(sizeof(uint16_t) * 36 * particleSystem->getParticleQuota(), backend::BufferType::INDEX, backend::BufferUsage::DYNAMIC);
        if (_indexBuffer == nullptr)
        {
            CCLOG("PUParticle3DBoxRender::render create index buffer failed");
            return;
        }
        _indices.resize(36 * particleSystem->getParticleQuota());
        reBuildIndices(particleSystem->getParticleQuota());
    }

    unsigned int vertexindex = 0;
    unsigned int index = 0;
    Mat4 texRot;
    Vec3 val;
    for (auto iter : particlePool.getActiveDataList())
    {
        auto particle = static_cast<PUParticle3D *>(iter);
        float halfHeight = particle->height * 0.5f;
        float halfWidth = particle->width * 0.5f;
        float halfDepth = particle->depth * 0.5f;
        Mat4::createRotation(backward, particle->zRotation, &texRot);
        val = texRot * Vec3(0.0f, 0.75f, 0.0);
        _vertices[vertexindex + 0].position = particle->position + Vec3(-halfWidth, -halfHeight, halfDepth);
        _vertices[vertexindex + 0].color = particle->color;
        _vertices[vertexindex + 0].uv.x = val.x; _vertices[vertexindex + 0].uv.y = val.y; 
        val = texRot * Vec3(0.0f, 0.25f, 0.0);
        _vertices[vertexindex + 1].position = particle->position + Vec3(halfWidth, -halfHeight, halfDepth);
        _vertices[vertexindex + 1].color = particle->color;
        _vertices[vertexindex + 1].uv.x = val.x; _vertices[vertexindex + 1].uv.y = val.y;
        val = texRot * Vec3(0.5f, 0.25f, 0.0);
        _vertices[vertexindex + 2].position = particle->position + Vec3(halfWidth, halfHeight, halfDepth);
        _vertices[vertexindex + 2].color = particle->color;
        _vertices[vertexindex + 2].uv.x = val.x; _vertices[vertexindex + 2].uv.y = val.y;
        val = texRot * Vec3(0.5f, 0.75f, 0.0);
        _vertices[vertexindex + 3].position = particle->position + Vec3(-halfWidth, halfHeight, halfDepth);
        _vertices[vertexindex + 3].color = particle->color;
        _vertices[vertexindex + 3].uv.x = val.x; _vertices[vertexindex + 3].uv.y = val.y;

        val = texRot * Vec3(0.0f, 0.0f, 0.0);
        _vertices[vertexindex + 4].position = particle->position + Vec3(halfWidth, -halfHeight, -halfDepth);
        _vertices[vertexindex + 4].color = particle->color;
        _vertices[vertexindex + 4].uv.x = val.x; _vertices[vertexindex + 4].uv.y = val.y;
        val = texRot * Vec3(0.0f, 1.0f, 0.0);
        _vertices[vertexindex + 5].position = particle->position + Vec3(-halfWidth, -halfHeight, -halfDepth);
        _vertices[vertexindex + 5].color = particle->color;
        _vertices[vertexindex + 5].uv.x = val.x; _vertices[vertexindex + 5].uv.y = val.y;
        val = texRot * Vec3(0.5f, 1.0f, 0.0);
        _vertices[vertexindex + 6].position = particle->position + Vec3(-halfWidth, halfHeight, -halfDepth);
        _vertices[vertexindex + 6].color = particle->color;
        _vertices[vertexindex + 6].uv.x = val.x; _vertices[vertexindex + 6].uv.y = val.y;
        val = texRot * Vec3(0.5f, 0.0f, 0.0);
        _vertices[vertexindex + 7].position = particle->position + Vec3(halfWidth, halfHeight, -halfDepth);
        _vertices[vertexindex + 7].color = particle->color;
        _vertices[vertexindex + 7].uv.x = val.x; _vertices[vertexindex + 7].uv.y = val.y;

        vertexindex += 8;
        index += 36;
    }

    if (!_vertices.empty() && !_indices.empty()){
        _vertexBuffer->updateData(&_vertices[0], vertexindex * sizeof(_vertices[0]));
        _indexBuffer->updateData(&_indices[0], sizeof(_indices[0]) * sizeof(uint16_t));

        _stateBlock.setBlendFunc(_particleSystem->getBlendFunc());

        auto uColor = Vec4(1, 1, 1, 1);
        _programState->setUniform(_locColor, &uColor, sizeof(uColor));
        
        if (_texture)
        {
            _programState->setTexture(_locTexture, 0, _texture->getBackendTexture());
        }

        auto &projectionMatrix = Director::getInstance()->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
        _programState->setUniform(_locPMatrix, &projectionMatrix.m, sizeof(projectionMatrix.m));

        _meshCommand.setIndexDrawInfo(0, _indices.size());

        renderer->addCommand(&_meshCommand);
    }
}

void PUParticle3DBoxRender::reBuildIndices(unsigned short count)
{
    unsigned short vertexIndex = 0;
    for (unsigned short i = 0; i < 36 * count;){
        //front
        _indices[i++] = vertexIndex + 0;
        _indices[i++] = vertexIndex + 2;
        _indices[i++] = vertexIndex + 3;
        _indices[i++] = vertexIndex + 0;
        _indices[i++] = vertexIndex + 1;
        _indices[i++] = vertexIndex + 2;
        //right
        _indices[i++] = vertexIndex + 1;
        _indices[i++] = vertexIndex + 7;
        _indices[i++] = vertexIndex + 2;
        _indices[i++] = vertexIndex + 1;
        _indices[i++] = vertexIndex + 4;
        _indices[i++] = vertexIndex + 7;
        //back
        _indices[i++] = vertexIndex + 4;
        _indices[i++] = vertexIndex + 6;
        _indices[i++] = vertexIndex + 7;
        _indices[i++] = vertexIndex + 4;
        _indices[i++] = vertexIndex + 5;
        _indices[i++] = vertexIndex + 6;
        //left
        _indices[i++] = vertexIndex + 5;
        _indices[i++] = vertexIndex + 3;
        _indices[i++] = vertexIndex + 6;
        _indices[i++] = vertexIndex + 5;
        _indices[i++] = vertexIndex + 0;
        _indices[i++] = vertexIndex + 3;
        //top
        _indices[i++] = vertexIndex + 3;
        _indices[i++] = vertexIndex + 7;
        _indices[i++] = vertexIndex + 6;
        _indices[i++] = vertexIndex + 3;
        _indices[i++] = vertexIndex + 2;
        _indices[i++] = vertexIndex + 7;
        //bottom
        _indices[i++] = vertexIndex + 5;
        _indices[i++] = vertexIndex + 1;
        _indices[i++] = vertexIndex + 0;
        _indices[i++] = vertexIndex + 5;
        _indices[i++] = vertexIndex + 4;
        _indices[i++] = vertexIndex + 1;

        vertexIndex += 8;
    }
}

PUParticle3DBoxRender* PUParticle3DBoxRender::clone()
{
    auto render = PUParticle3DBoxRender::create(_texFile);
    copyAttributesTo(render);
    return render;
}


PUSphereRender* PUSphereRender::create( const std::string &texFile)
{
    auto ret = new (std::nothrow) PUSphereRender();
    if (ret && ret->initRender(texFile))
    {
        ret->autorelease();
    }
    else
    {
        CC_SAFE_DELETE(ret);
    }
    return ret;
}

void PUSphereRender::render( Renderer* renderer, const Mat4 &transform, ParticleSystem3D* particleSystem )
{
    //batch and generate draw
    const ParticlePool &particlePool = particleSystem->getParticlePool();
    if (!_isVisible || particlePool.empty())
        return;

    auto camera = Camera::getVisitingCamera();
    auto cameraMat = camera->getNodeToWorldTransform();
    Vec3 backward(cameraMat.m[8], cameraMat.m[9], cameraMat.m[10]);

    unsigned int vertexCount = (_numberOfRings + 1) * (_numberOfSegments + 1);
    unsigned int indexCount = 6 * _numberOfRings * (_numberOfSegments + 1);
    if (_vertexBuffer == nullptr && _indexBuffer == nullptr){
        size_t stride = sizeof(VertexInfo);
        _vertexBuffer = backend::Device::getInstance()->newBuffer(stride * vertexCount * particleSystem->getParticleQuota(), backend::BufferType::VERTEX, backend::BufferUsage::DYNAMIC);
        if (_vertexBuffer == nullptr)
        {
            CCLOG("PUSphereRender::render create vertex buffer failed");
            return;
        }
        _vertices.resize(vertexCount * particleSystem->getParticleQuota());

        _indexBuffer = backend::Device::getInstance()->newBuffer(sizeof(uint16_t) * indexCount * particleSystem->getParticleQuota(), backend::BufferType::INDEX, backend::BufferUsage::DYNAMIC);
        if (_indexBuffer == nullptr)
        {
            CCLOG("PUSphereRender::render create index buffer failed");
            return;
        }
        _indices.resize(indexCount * particleSystem->getParticleQuota());

        buildBuffers(particleSystem->getParticleQuota());
    }

    unsigned int vertexindex = 0;
    unsigned int index = 0;

    Mat4 mat;
    Mat4 rotMat;
    Mat4 sclMat;
    Mat4 texRot;
    Vec3 val;
    for (auto iter : particlePool.getActiveDataList())
    {
        auto particle = static_cast<PUParticle3D *>(iter);
        float radius = particle->width * 0.5f;
        Mat4::createRotation(particle->orientation, &rotMat);
        Mat4::createScale(radius, radius, radius, &sclMat);
        Mat4::createRotation(backward, particle->zRotation, &texRot);
        mat = rotMat * sclMat;
        mat.m[12] = particle->position.x;
        mat.m[13] = particle->position.y;
        mat.m[14] = particle->position.z;

        for (unsigned int i = 0; i < vertexCount; ++i){
            val = texRot * Vec3(_vertexTemplate[vertexindex + i].uv.x, _vertexTemplate[vertexindex + i].uv.y, 0.0f);
            mat.transformPoint(_vertexTemplate[vertexindex + i].position, &_vertices[vertexindex + i].position);
            _vertices[vertexindex + i].color = particle->color;
            _vertices[vertexindex + i].uv.x = val.x; _vertices[vertexindex + i].uv.y = val.y;
        }
        vertexindex += vertexCount;
        index += indexCount;
    }

    if (!_vertices.empty() && !_indices.empty()){
        _meshCommand.init(0.0f);

        _vertexBuffer->updateData(&_vertices[0], vertexindex * sizeof(_vertices[0]));
        _indexBuffer->updateData(&_indices[0], index * sizeof(_indices[0]));

        _meshCommand.setVertexBuffer(_vertexBuffer);
        _meshCommand.setIndexBuffer(_indexBuffer, MeshCommand::IndexFormat::U_SHORT);
        _meshCommand.setIndexDrawInfo(0, index);

        auto uColor = Vec4(1, 1, 1, 1);
        _programState->setUniform(_locColor, &uColor, sizeof(uColor));

        if (_texture)
        {
            _programState->setTexture(_locTexture, 0, _texture->getBackendTexture());
        }

        auto &projectionMatrix = Director::getInstance()->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
        _programState->setUniform(_locPMatrix, &projectionMatrix.m, sizeof(projectionMatrix.m));

        renderer->addCommand(&_meshCommand);
    }
}

void PUSphereRender::buildBuffers( unsigned short count )
{
    float stepRingAngle = (M_PI / _numberOfRings);
    float stepSegmentAngle = (2.0 * M_PI / _numberOfSegments);

    unsigned short vertexIndex = 0;
    unsigned short index = 0;

    for (unsigned short i = 0; i < count; ++i){
        for(unsigned int ring = 0; ring <= _numberOfRings; ring++)
        {
            float r0 = sinf (ring * stepRingAngle);
            float y0 = cosf (ring * stepRingAngle);

            for(unsigned int segment = 0; segment <= _numberOfSegments; segment++)
            {
                VertexInfo vi;
                float x0 = r0 * sinf(segment * stepSegmentAngle);
                float z0 = r0 * cosf(segment * stepSegmentAngle);

                // Vertex
                vi.position.set(x0, y0, z0);

                // Colour
                vi.color = Vec4::ONE;

                // Texture Coordinates
                vi.uv.x = (float) segment / (float) _numberOfSegments;
                vi.uv.y = 1.0f - (float) ring / (float) _numberOfRings;

                if (ring != _numberOfRings)
                {
                    // each vertex (except the last) has six indices pointing to it
                    _indices[index++] = vertexIndex + _numberOfSegments + 1;
                    _indices[index++] = vertexIndex;
                    _indices[index++] = vertexIndex + _numberOfSegments;
                    _indices[index++] = vertexIndex + _numberOfSegments + 1;
                    _indices[index++] = vertexIndex + 1;
                    _indices[index++] = vertexIndex;
                }
                ++vertexIndex;
                _vertexTemplate.push_back(vi);
            }
        }
    }
}

PUSphereRender::PUSphereRender()
    : _numberOfRings(16)
    , _numberOfSegments(16)
{
    autoRotate = false;
}

PUSphereRender::~PUSphereRender()
{

}

void PUSphereRender::copyAttributesTo(PUSphereRender *sphereRender)
{
    PURender::copyAttributesTo(sphereRender);
    sphereRender->_numberOfRings = _numberOfRings;
    sphereRender->_numberOfSegments = _numberOfSegments;
}

PUSphereRender* PUSphereRender::clone()
{
    auto render = PUSphereRender::create(_texFile);
    copyAttributesTo(render);
    return render;
}

NS_CC_END