Пример #1
0
void Sprite3D::createAsync(const std::string &modelPath, const std::string &texturePath, const std::function<void(Sprite3D*, void*)>& callback, void* callbackparam)
{
    Sprite3D *sprite = new (std::nothrow) Sprite3D();
    if (sprite->loadFromCache(modelPath))
    {
        sprite->autorelease();
        if (!texturePath.empty())
            sprite->setTexture(texturePath);
        callback(sprite, callbackparam);
        return;
    }
    
    sprite->_asyncLoadParam.afterLoadCallback = callback;
    sprite->_asyncLoadParam.texPath = texturePath;
    sprite->_asyncLoadParam.modlePath = modelPath;
    sprite->_asyncLoadParam.callbackParam = callbackparam;
    sprite->_asyncLoadParam.materialdatas = new (std::nothrow) MaterialDatas();
    sprite->_asyncLoadParam.meshdatas = new (std::nothrow) MeshDatas();
    sprite->_asyncLoadParam.nodeDatas = new (std::nothrow) NodeDatas();
    AsyncTaskPool::getInstance()->enqueue(AsyncTaskPool::TaskType::TASK_IO, CC_CALLBACK_1(Sprite3D::afterAsyncLoad, sprite), (void*)(&sprite->_asyncLoadParam), [sprite]()
    {
        sprite->_asyncLoadParam.result = sprite->loadFromFile(sprite->_asyncLoadParam.modlePath, sprite->_asyncLoadParam.nodeDatas, sprite->_asyncLoadParam.meshdatas, sprite->_asyncLoadParam.materialdatas);
    });
    
}
Пример #2
0
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;
    }
}