Sprite3D 怎么自动合批(auto-batching)?

Sprite3D 怎么自动合批(auto-batching)?

Sprite在 Renderer::drawBatchedTriangles 自动合批

// draw
void Sprite::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
{
    // 初始化
        _trianglesCommand.init(_globalZOrder,
                               _texture,
                               _blendFunc,
                               _polyInfo.triangles,
                               transform,
                               flags);
		// 添加到渲染器
        renderer->addCommand(&_trianglesCommand);
}

void Renderer::drawBatchedTriangles()
{

Sprite3D


void Mesh::draw(Renderer* renderer, float globalZOrder, const Mat4& transform, uint32_t flags, unsigned int lightMask, const Vec4& color, bool forceDepthWrite)
{
	_material->draw(commands.data(), globalZ,
                    getVertexBuffer(),
                    getIndexBuffer(),
                    getPrimitiveType(),
                    getIndexFormat(),
                    getIndexCount(),
                    transform);

void Material::draw(MeshCommand* meshCommands, 
	float globalZOrder, 
	backend::Buffer* vertexBuffer, 
	backend::Buffer* indexBuffer,
	CustomCommand::PrimitiveType primitive, 
	CustomCommand::IndexFormat indexFormat,
	unsigned int indexCount, 
	const Mat4& modelView)
{
    int i = 0;
    for (const auto& pass: _currentTechnique->_passes)
    {
		// 遍历所有通道
        pass->draw(&meshCommands[i], globalZOrder, vertexBuffer, indexBuffer,primitive, indexFormat, indexCount, modelView);
        i++;
    }
}

void Pass::draw(
MeshCommand *meshCommand, 
float globalZOrder, 
backend::Buffer* vertexBuffer, 
backend::Buffer* indexBuffer,
MeshCommand::PrimitiveType primitive, 
MeshCommand::IndexFormat indexFormat,
unsigned int indexCount,
const Mat4& modelView)
{
	// 绘制前回调/绘制后回调
    meshCommand->setBeforeCallback(CC_CALLBACK_0(Pass::onBeforeVisitCmd, this, meshCommand));
    meshCommand->setAfterCallback(CC_CALLBACK_0(Pass::onAfterVisitCmd, this, meshCommand));
    meshCommand->init(globalZOrder, modelView);
    meshCommand->setPrimitiveType(primitive);//基元类型
	meshCommand->setIndexBuffer(indexBuffer, indexFormat);//索引缓冲区
    meshCommand->setVertexBuffer(vertexBuffer);//顶点缓冲区
    meshCommand->setIndexDrawInfo(0, indexCount);
    meshCommand->getPipelineDescriptor().programState = _programState;

    auto *renderer = Director::getInstance()->getRenderer();
    renderer->addCommand(meshCommand);
}

//---

void Renderer::drawMeshCommand(RenderCommand *command)
{
    // MeshCommand and CustomCommand are identical while rendering.
	// MeshCommand和CustomCommand在渲染时是完全相同的。
    drawCustomCommand(command);
}
// 绘制自定义命令
void Renderer::drawCustomCommand(RenderCommand *command)
{

Sprite3D怎么自动合批(auto-batching)?

Sprite3D没有找到自动合批的地方,
也没找到能手动合批的方法。

有大佬知道的吗?

想法

由于自己搞了个场景编辑器,场景由很多3D精灵组成,需要合批功能。

  1. 现在的思路是用 Bundle3D 合并多个模型数据成一个再创建一个Sprite3D
  2. 用blender等创建场景,合并
  3. 修改 Renderer::drawMeshCommand (不会)
1赞

找到一个相似的帖子,可能会有帮助

看了一遍,把多个mesh的vertex,indices合到一个mesh上,可以减少draw calls.

CCRender::processRenderCommand 里面有针对 MeshCommand 的auto-batching.

if (RenderCommand::Type::MESH_COMMAND == commandType)
    {
        flush2D();
        auto cmd = static_cast<MeshCommand*>(command);
        
        if (cmd->isSkipBatching() || _lastBatchedMeshCommand == nullptr || _lastBatchedMeshCommand->getMaterialID() != cmd->getMaterialID())
        {
            flush3D();

            CCGL_DEBUG_INSERT_EVENT_MARKER("RENDERER_MESH_COMMAND");

            if(cmd->isSkipBatching())
            {
                // XXX: execute() will call bind() and unbind()
                // but unbind() shouldn't be call if the next command is a MESH_COMMAND with Material.
                // Once most of cocos2d-x moves to Pass/StateBlock, only bind() should be used.
                cmd->execute();
            }
            else
            {
                cmd->preBatchDraw();
                cmd->batchDraw();
                _lastBatchedMeshCommand = cmd;
            }
        }
        else
        {
            CCGL_DEBUG_INSERT_EVENT_MARKER("RENDERER_MESH_COMMAND");
            cmd->batchDraw();
        }
    }

好,改天研究一下

别了,论坛内有官方回帖说 3.x 版本cocos不支持3D的自动合批。。。