2

我使用 Away3D 4.1(2500 个立方体)创建了一个简单的测试,但性能比我预期的要低很多 - 只有 10 FPS。

我假设我犯了一个菜鸟错误(作为菜鸟和所有)所以这里是相关的代码片段:

灯光:

var light1:DirectionalLight = new DirectionalLight();
light1.position = new Vector3D(400, 300, -200);
light1.lookAt(new Vector3D());
light1.color = 0xFFFFFF;
light1.ambient = 0.25;
lightPicker = new StaticLightPicker([light1]);

创建立方体:

var material:ColorMaterial = new ColorMaterial(0x999999);
material.lightPicker = lightPicker;
material.specular = 0;
var mesh:Mesh = new Mesh(new CubeGeometry(50, 50, 50), material);

for (var i:uint = 0; i < 50; i++)
{
    for (var j:uint = 0; j < 50; j++)
    {
        var cube:Mesh = Mesh(mesh.clone());
        cube.x = 100*(i-25);
        cube.y = 25;
        cube.z = 100*(j-25);
        scene.addChild(cube);
    }
}

还有相机:

camera = new Camera3D();
camera.position = new Vector3D(0, 1000, -5000);
camera.lookAt(new Vector3D(0, 0, 0));
camera.lens.far = 10000;

Scout 中的 Stage3D 输出显示每个 drawTriangles 调用之间有很多调用,而我的基本理解告诉我 drawTriangle 调用应该是“批处理”的。

我知道其他一些框架有批处理方法,但我找不到任何与 Away3D 相关的东西。

提前感谢您的帮助。

4

3 回答 3

3

看起来 Merge(感谢 Varnius 发现)是推荐使用以前版本的方法,但它在 4.1 中不起作用(请参阅away3d 论坛主题)。

但是,用户 kurono 在论坛上发布了一个可行的解决方案(至少对于我的场景),所以我在这里复制它,以防其他人有同样的问题:

var material:ColorMaterial = new ColorMaterial(0x999999);
material.lightPicker = lightPicker;
material.specular = 0;
var mesh:Mesh = new Mesh(new CubeGeometry(50, 50, 50));
var meshes:Vector.<Mesh> = new Vector.<Mesh>();
for (var i:uint = 0; i < 50; i++)
{
    for (var j:uint = 0; j < 50; j++)
    {
        var cube:Mesh = Mesh(mesh.clone());
        cube.x = 100*(i-25);
        cube.y = 25;
        cube.z = 100*(j-25);
        meshes.push(cube);
    }
}
var bigMesh:Mesh = doMerge(meshes, material);
scene.add(bigMesh);

神奇之处在于 doMerge() 方法:

function doMerge(meshes:Vector.<Mesh>, material:MaterialBase):Mesh
{
    var isub:ISubGeometry;
    var rawVertsAll:Vector.<Number> = new Vector.<Number>();
    var rawIndicesAll:Vector.<uint> = new Vector.<uint>();
    var rawUVsAll:Vector.<Number> = new Vector.<Number>();
    var rawNormalsAll:Vector.<Number> = new Vector.<Number>();
    var rawTangentsAll:Vector.<Number> = new Vector.<Number>();
    var offset:uint = 0;
    var verts:Vector.<Number>;
    var normals:Vector.<Number>;
    var tangents:Vector.<Number>;
    var uvs:Vector.<Number>;
    var indices:Vector.<uint>;
    var i:uint;
    var j:uint;
    var k:uint;
    for (k = 0; k < meshes.length; k++)
    {
        var m:Mesh = meshes[k];
        isub = m.geometry.subGeometries[0].cloneWithSeperateBuffers();
        isub.applyTransformation(m.transform.clone());
        verts = new Vector.<Number>();
        normals = new Vector.<Number>();
        tangents = new Vector.<Number>();
        uvs = new Vector.<Number>();
        indices = isub.indexData;
        for (i = 0; i < isub.numVertices; i++)
        {
            verts.push(isub.vertexData[i * isub.vertexStride + isub.vertexOffset]);
            verts.push(isub.vertexData[i * isub.vertexStride + isub.vertexOffset + 1]);
            verts.push(isub.vertexData[i * isub.vertexStride + isub.vertexOffset + 2]);
            normals.push(isub.vertexNormalData[i * isub.vertexNormalStride + isub.vertexNormalOffset]);
            normals.push(isub.vertexNormalData[i * isub.vertexNormalStride + isub.vertexNormalOffset + 1]);
            normals.push(isub.vertexNormalData[i * isub.vertexNormalStride + isub.vertexNormalOffset + 2]);
            tangents.push(isub.vertexTangentData[i * isub.vertexTangentStride + isub.vertexTangentOffset]);
            tangents.push(isub.vertexTangentData[i * isub.vertexTangentStride + isub.vertexTangentOffset + 1]);
            tangents.push(isub.vertexTangentData[i * isub.vertexTangentStride + isub.vertexTangentOffset + 2]);
            uvs.push(isub.UVData[i * isub.UVStride + isub.UVOffset]);
            uvs.push(isub.UVData[i * isub.UVStride + isub.UVOffset + 1]);
        }
        for (j = 0; j < indices.length; j++)
        {
            indices[j] += offset;
        }
        offset += isub.numVertices;
        rawVertsAll = rawVertsAll.concat(verts);
        rawNormalsAll = rawNormalsAll.concat(normals);
        rawTangentsAll = rawTangentsAll.concat(tangents);
        rawUVsAll = rawUVsAll.concat(uvs);
        rawIndicesAll = rawIndicesAll.concat(indices);
    }
    var geometry:Geometry = new Geometry();
    var subGeometry:SubGeometry = new SubGeometry();
    subGeometry.updateVertexData(rawVertsAll);
    subGeometry.updateIndexData(rawIndicesAll);
    subGeometry.updateUVData(rawUVsAll);
    subGeometry.updateVertexNormalData(rawNormalsAll);
    subGeometry.updateVertexTangentData(rawTangentsAll);
    geometry.subGeometries.push(subGeometry);

    return new Mesh(geometry, material);
}

瞧!10fps 变成 60fps

于 2013-05-01T05:51:46.157 回答
2

是的,你应该批处理你的绘图调用。我对 Away3D 没有太多经验,但在快速浏览了他们的 API 参考之后,似乎away3d.tools.commands.Merge应该可以帮助您将所有这些立方体合并到一个大型批处理网格中。

于 2013-04-30T09:07:09.500 回答
2

您是在调试播放器还是发布版本中获得 10 fps?

我运行了您的代码并在调试播放器中获得了 20 fps,但在发布版本中获得了 50+ fps。

我尝试合并并没有看到任何改进。另外,如果您想访问各个立方体,合并将使这变得非常复杂:)在此处输入图像描述

于 2013-04-30T13:42:37.683 回答