【问题标题】:libgdx 3d rendering, I'm getting low fpslibgdx 3d 渲染,我的 fps 越来越低
【发布时间】:2019-11-04 14:04:40
【问题描述】:

我不确定我是否正确地进行了 3d 渲染?下面是我的 3d 渲染方法。当我在桌面上运行我的游戏时,它在 60fps 下运行良好,但当我在 android 上运行它时,它只在 15fps 下运行。当我注释掉这个方法时,我也得到了 60fps,所以我认为问题出在我的 3d 渲染代码中。

你能告诉我它是否正确吗?

private void draw3d() {
        gameViewport.apply();

        modelBatch.begin(gameCamera);
        modelBatch.render(highwayInstance, environment);
        modelBatch.render(highway2Instance, environment);
        modelBatch.render(highwayLines, environment);
        modelBatch.render(buttons, environment);
        if (activeMetronomeFirsts.size != 0) {
            int activeMetronomeFirstLength = activeMetronomeFirsts.size;
            for (int i = activeMetronomeFirstLength; --i >= 0;) {
                modelBatch.render(activeMetronomeFirsts.get(i).getMetronomeInstance(), environment);
            }
        }
        if (activeMetronomes.size != 0) {
            int activeMetronomeLength = activeMetronomes.size;
            for (int i = activeMetronomeLength; --i >= 0;) {
                modelBatch.render(activeMetronomes.get(i).getMetronomeInstance(),environment);
            }
        }
        if (activeSnares.size != 0) {
            int activeSnareLength = activeSnares.size;
            for (int i = activeSnareLength; --i >= 0;) {
                modelBatch.render(activeSnares.get(i).getSnareInstance(),environment);
                //activeSnares.get(i).draw(modelBatch);
            }
        }
        if (activeHiHats.size != 0) {
            int activeHiHatLength = activeHiHats.size;
            for (int i = activeHiHatLength; --i >= 0;) {
                modelBatch.render(activeHiHats.get(i).getHiHatInstance(), environment);
            }
        }
        if (activeTom1s.size != 0) {
            int activeTom1Length = activeTom1s.size;
            for (int i = activeTom1Length; --i >= 0;) {
                modelBatch.render(activeTom1s.get(i).getTom1Instance(), environment);
            }
        }
        if (activeRides.size != 0) {
            int activeRideLength = activeRides.size;
            for (int i = activeRideLength; --i >= 0;) {
                modelBatch.render(activeRides.get(i).getRideInstance(), environment);
            }
        }
        if (activeTom2s.size != 0) {
            int activeTom2Length = activeTom2s.size;
            for (int i = activeTom2Length; --i >= 0;) {
                modelBatch.render(activeTom2s.get(i).getTom2Instance(), environment);
            }
        }
        if (activeCrashs.size != 0) {
            int activeCrashLength = activeCrashs.size;
            for (int i = activeCrashLength; --i >= 0;) {
                modelBatch.render(activeCrashs.get(i).getCrashInstance(), environment);
            }
        }
        if (activeTom3s.size != 0) {
            int activeTom3Length = activeTom3s.size;
            for (int i = activeTom3Length; --i >= 0;) {
                modelBatch.render(activeTom3s.get(i).getTom3Instance(), environment);
            }
        }
        if (activeBasses.size != 0) {
            int activeBassLength = activeBasses.size;
            for (int i = activeBassLength; --i >= 0;) {
                modelBatch.render(activeBasses.get(i).getBassInstance(), environment);
            }
        }
        modelBatch.end();
    }

【问题讨论】:

  • 提供了你的方法,我找不到导致 FPS 下降的确切原因。为了诊断您的问题,您必须进行一些 GL 分析并查看它为您提供的值。阅读此内容以执行此操作:github.com/libgdx/libgdx/wiki/Profiling#OpenGL 欢迎您分享使用该方法与不使用该方法获得的值,这将有助于分析问题
  • 我得到了这些值:
  • drawCalls: 55 shaderSwitches: 14 监听器: com.badlogic.gdx.graphics.profiling.GLErrorListener$1@2ef2bb8b textureBindings: 1 vertexCount: 55
  • 好吧,虽然我不认为这是导致 FPS 下降的原因,但我会说着色器开关有点高。试试看:注释掉该方法中的每个渲染。现在取消注释第一个并检查它是否导致重大下降。如果不是,请尝试第二个,第三个等等...但是这些值不被认为是高的,因此问题可能与渲染无关
  • 当我取消注释“modelBatch.render(highwayLines, environment);”时,我得到 54-57fps,如果我取消注释“modelBatch.render(buttons, environment);”我得到 30-32fps 等等。我取消注释的渲染方法越多,得到的 fps 越低

标签: java 3d libgdx rendering


【解决方案1】:

你觉得我的模型创建代码怎么样?:

材质highwayMaterial = new Material(TextureAttribute.createDiffuse(highwayTexture), ColorAttribute.createSpecular(1, 1, 1, 1)); long roadAttributes = VertexAttributes.Usage.Position | VertexAttributes.Usage.Normal | VertexAttributes.Usage.TextureCoordinates; 高速公路 = modelBuilder.createBox(30f,0.01f,60f,highwayMaterial,highwayAttributes); highwayInstance = new ModelInstance(highway, -10, 0, 0); highway2Instance = new ModelInstance(highway, -10, 0, -60);

    //model testing
    Material metronomeMaterial = new Material(ColorAttribute.createDiffuse(Color.LIGHT_GRAY));
    long theAttributes = VertexAttributes.Usage.Position | VertexAttributes.Usage.Normal;

    Material highwayLineMaterial = new Material(ColorAttribute.createDiffuse(Color.DARK_GRAY));

    //ještě highway line
    highwayLine = modelBuilder.createBox(0.2f, 0.01f, 60f, highwayLineMaterial, theAttributes);
    highwayLine1 = new ModelInstance(highwayLine, -21.47058823529412f, 0.01f, 0);
    highwayLine2 = new ModelInstance(highwayLine, -16.17647058823529f, 0.01f, 0);
    highwayLine3 = new ModelInstance(highwayLine, -12.64705882352941f, 0.01f, 0);
    highwayLine4 = new ModelInstance(highwayLine, -7.352941176470588f, 0.01f, 0);
    highwayLine5 = new ModelInstance(highwayLine, -3.823529411764705f, 0.01f, 0);
    highwayLine6 = new ModelInstance(highwayLine, 1.470588235294118f, 0.01f, 0);
    highwayLines.add(highwayLine1);
    highwayLines.add(highwayLine2);
    highwayLines.add(highwayLine3);
    highwayLines.add(highwayLine4);
    highwayLines.add(highwayLine5);
    highwayLines.add(highwayLine6);

    Material aroundHighwayMaterial = new Material(ColorAttribute.createDiffuse(Color.LIGHT_GRAY));
    aroundHighway = modelBuilder.createBox(1f, 2f, 60f, aroundHighwayMaterial, theAttributes);
    aroundHighwayInstance1 = new ModelInstance(aroundHighway, -25.5f, 0f, 0f);
    aroundHighwayInstance2 = new ModelInstance(aroundHighway, 5.5f, 0f, 0f);
    highwayLines.add(aroundHighwayInstance1);
    highwayLines.add(aroundHighwayInstance2);

    //noteCatcher
    Material aroundButtonsMaterial = new Material(ColorAttribute.createDiffuse(Color.ORANGE),
            ColorAttribute.createSpecular(1, 1, 1, 1));
    aroundButtons = modelBuilder.createBox(30f, 0.6f, 0.3f, aroundButtonsMaterial, theAttributes);
    aroundButtonsInstance1 = new ModelInstance(aroundButtons, -10f, 0.6f, 29f);
    aroundButtonsInstance2 = new ModelInstance(aroundButtons, -10f, 0.6f, 27f);
    //snare button
    drumMetalButton = modelBuilder.createBox(3.529411764705883f, 1f, 1.5f, highwayLineMaterial, theAttributes);
    snareMetalButtonInstance = new ModelInstance(drumMetalButton, -23.23529411764706f, 0.6f, 28f);
    buttons.add(snareMetalButtonInstance);
    Material snareMaterial = new Material(ColorAttribute.createDiffuse(Color.RED));
    snareButton = modelBuilder.createBox(3.176470588235295f, 1f, 1.28f, snareMaterial, theAttributes);
    snareButtonInstance = new ModelInstance(snareButton, -23.23529411764706f, 0.67f, 28f);
    buttons.add(snareButtonInstance);
    //hiHat button
    cymbalMetalButton = modelBuilder.createBox(5.294117647058823f, 1f, 1.5f, highwayLineMaterial, theAttributes);
    hiHatMetalButtonInstance = new ModelInstance(cymbalMetalButton, -18.82352941176471f, 0.6f, 28f);
    buttons.add(hiHatMetalButtonInstance);
    Material hiHatMaterial = new Material(ColorAttribute.createDiffuse(Color.YELLOW));
    hiHatButton = modelBuilder.createBox(4.94f, 1f, 1.28f, hiHatMaterial, theAttributes);
    hiHatButtonInstance = new ModelInstance(hiHatButton, -18.82352941176471f, 0.6f, 28f);
    buttons.add(hiHatButtonInstance);
    //tom1 button
    tom1MetalButtonInstance = new ModelInstance(drumMetalButton, -14.41176470588236f, 0.6f, 28f);
    buttons.add(tom1MetalButtonInstance);
    tom1Button = modelBuilder.createBox(3.176470588235295f, 1f, 1.28f, hiHatMaterial, theAttributes);
    tom1ButtonInstance = new ModelInstance(tom1Button, -14.41176470588236f, 0.6f, 28f);
    buttons.add(tom1ButtonInstance);
    //ride button
    rideMetalButtonInstance = new ModelInstance(cymbalMetalButton, -10.00000000000001f, 0.6f, 28f);
    buttons.add(rideMetalButtonInstance);
    Material rideMaterial = new Material(ColorAttribute.createDiffuse(Color.BLUE));
    rideButton = modelBuilder.createBox(4.94f, 1f, 1.28f, rideMaterial, theAttributes);
    rideButtonInstance = new ModelInstance(rideButton, -10.00000000000001f, 0.6f, 28f);
    buttons.add(rideButtonInstance);
    //tom2 button
    tom2MetalButtonInstance = new ModelInstance(drumMetalButton, -5.588235294117746f, 0.6f, 28f);
    buttons.add(tom2MetalButtonInstance);
    tom2Button = modelBuilder.createBox(3.176470588235295f, 1f, 1.28f, rideMaterial, theAttributes);
    tom2ButtonInstance = new ModelInstance(tom2Button, -5.588235294117746f, 0.6f, 28f);
    buttons.add(tom2ButtonInstance);
    //crash button
    crashMetalButtonInstance = new ModelInstance(cymbalMetalButton, -1.176470588235392f, 0.6f, 28f);
    buttons.add(crashMetalButtonInstance);
    Material crashMaterial = new Material(ColorAttribute.createDiffuse(Color.GREEN));
    crashButton = modelBuilder.createBox(4.94f, 1f, 1.28f, crashMaterial, theAttributes);
    crashButtonInstance = new ModelInstance(crashButton, -1.176470588235392f, 0.6f, 28f);
    buttons.add(crashButtonInstance);
    //tom3 button
    tom3MetalButtonInstance = new ModelInstance(drumMetalButton, 3.235294117646962f, 0.6f, 28f);
    buttons.add(tom3MetalButtonInstance);
    tom3Button = modelBuilder.createBox(3.176470588235295f, 1f, 1.28f, crashMaterial, theAttributes);
    tom3ButtonInstance = new ModelInstance(tom3Button, 3.235294117646962f, 0.6f, 28f);
    buttons.add(tom3ButtonInstance);
    //for bass
    buttons.add(aroundButtonsInstance1);
    buttons.add(aroundButtonsInstance2);

    metronomeFirstModel = modelBuilder.createBox(30f, 0.02f, 0.6f, metronomeMaterial, theAttributes);
    metronomeModel = modelBuilder.createBox(30f, 0.02f, 0.2f, metronomeMaterial, theAttributes);
    snareNote = modelBuilder.createBox(3.176470588235295f, 0.5f, 1f, snareMaterial, theAttributes);
    hiHatNote = modelBuilder.createCone(4.94f, 1f, 2f, 8, hiHatMaterial, theAttributes);
    tom1Note = modelBuilder.createBox(3.176470588235295f, 0.5f, 1f, hiHatMaterial, theAttributes);
    rideNote = modelBuilder.createCone(4.94f, 1f, 2f, 8, rideMaterial, theAttributes);
    tom2Note = modelBuilder.createBox(3.176470588235295f, 0.5f, 1f, rideMaterial, theAttributes);
    crashNote = modelBuilder.createCone(4.94f, 1f, 2f, 8, crashMaterial, theAttributes);
    tom3Note = modelBuilder.createBox(3.176470588235295f, 0.5f, 1f, crashMaterial, theAttributes);
    Material bassMaterial = new Material(ColorAttribute.createDiffuse(Color.CYAN));
    bassNote = modelBuilder.createBox(30f, 0.2f, 0.4f, bassMaterial, theAttributes);

【讨论】:

  • 您应该阅读 Xoppa 关于使用模型构建器的 create__() 方法的回答:stackoverflow.com/questions/37353883/…
  • 值得尝试删除这些调用并按照 Xoppa 的说明进行操作
  • 所以,我需要这样做:ModelBuilder builder = new ModelBuilder(); builder.begin(); builder.node(); MeshPartBuilder mpb = builder.part("box", originalType, attributes, material); BoxShapeBuilder.build(mpb, x1, y1, z1, width1, height1, depth1); BoxShapeBuilder.build(mpb, x2, y2, z2, width2, height2, depth2); //... BoxShapeBuilder.build(mpb, xn, yn, zn, widthn, heightn, depthn);模型 = builder.end();
  • 是的。合并网格越多(分离的模型实例和节点越少),对 GPU 进行的绘制调用就越少
  • 但是如果我想分离一些模型实例怎么办,因为我需要向玩家平移的模型实例
猜你喜欢
  • 1970-01-01
  • 2012-06-25
  • 1970-01-01
  • 2019-11-22
  • 2015-01-14
  • 2012-07-25
  • 1970-01-01
  • 1970-01-01
  • 2016-11-28
相关资源
最近更新 更多