libgdx ModelBatch是否适合Ashley?

时间:2018-12-26 05:29:37

标签: design-patterns libgdx

换句话说,在ashley的渲染子系统中渲染模型零件的最佳实践是什么?

  • 详细问题:

最初的想法是将任何视觉效果处理尽可能地放入着色器中。这样的性能更好,因为它们是在GPU中处理的。 在three.js和shadertoy中有很多这样的示例。

要在Ashley中做到这一点,我可以在渲染任何网格之前将组件的所有参数都发送到GPU。因此,渲染子系统将如下所示:

public class SysDynamicRender extends IteratingSystem {
    ShaderProgram program;
    private void initShader() {
        program = new ShaderProgram("vert", "frag");

        // variables only about the shader program, not about meshes
        u_projTrans = program.getUniformLocation("u_projTrans");
        u_worldTrans = program.getUniformLocation("u_worldTrans");
        u_colorLoc = program.getUniformLocation("u_color");
        texBase = new Texture("data/modlib/color-blocks.jpg");
    }

    @Override
    protected void processEntity(Entity entity, float deltaTime) {
        // in Ashley, we only get meshparts from entity
        renderQueue.add(entity);
    }

    @Override
    public void update(float deltaTime) {
        super.update(deltaTime);
        // render
        for (Entity e : renderQueue) {
            // way 1: use the help of ModelBatch
            CmpModelInst cmpModelInst = e.getComponent(CmpModelInst.class)
            modelBatch.render(cmpModelInst.inst(), shader);

            // way 2: rendering meshpart directly (not work)
            // 2.1 get the Renderable (out)
            DynaAttr attr = e.getComponent(CmpVisuals.class).attr;
            ModelInstance inst = cmpModelInst.class).inst();
            Node n = inst.getNode("cube1_cube1_auv");
            out = inst.getRenderable(out, n);
            // 2.2 rendert meshpart with shader program
            program.setUniformMatrix(u_worldTrans, out.worldTransform);
            program.setUniformf(u_colorLoc, attr.effects());
            program.setUniformf(u_factorLoc, attr.u_factorF());
            out.meshPart.render(program);
        }

    renderQueue.clear();
    }
}

我认为必须有更好的方法来实现update()。

问题是什么?

或者有Ashley渲染3d模型的示例吗?

  • 更多考虑

视觉效果参数(例如颜色,反射角度(针对不同的颜色))是着色器的属性,不应在每次对网格物体进行迭代的每次渲染调用时对其进行更新。这是性能成本,因为如果我理解正确,CPU需要将数据发送到GPU。

Ashley提供了解决此问题的方案。只需实现一个动态着色器,然后将引用组件放入实体中,让Ashley调用正确的子系统即可。

这样,ModelBatch一定不合适吗? ModelBatch.flush()只是对其各个部分进行迭代。

参考:

ModelBatch.java:https://github.com/libgdx/libgdx/blob/master/gdx/src/com/badlogic/gdx/graphics/g3d/ModelBatch.java

Wiki:https://github.com/libgdx/libgdx/wiki/ModelBatch

我不了解的东西?

0 个答案:

没有答案