LibGdx:将光线与模型实例中的网格相交

LibGdx: Intersect Ray with Mesh from ModelInstance

我想将一些对象 (ModelInstance) 放置在我的游戏世界的地板上(也是一个 ModelInstance)。为了获得这些物体的位置,我让光线与地板相交。那么交点应该就是需要的位置了。

我的计划是将光线的原点设置在地板下方,这样光线的方向直接向上并从下方打到地板上。两个 ModelInstances 都是在 Blender 中制作的 .g3db 模型。

        Vector3 dir = new Vector3(0, 10, 0); //Vector points upwards
        Ray ray = new Ray(new Vector3(), dir.cpy());

        Mesh mesh = landscape.model.meshes.first(); //The floor ModelInstance, has only a single mesh

        int fac = mesh.getVertexSize();
        float[] verts = new float[mesh.getNumVertices() * fac];
        short[] inds = new short[mesh.getNumIndices()];
        mesh.getVertices(verts);
        mesh.getIndices(inds);

        for (int j = 0; j < 10; j++) { //add 10 objects to the floor
            Vector3 out = new Vector3(- 15, -50f, - j * 5); 
            ray.origin.set(out.cpy()); //set the origin of the vector below the floor

            if (Intersector.intersectRayTriangles(ray, verts, inds, fac, out)) {
                System.out.println(j + " out = " + out); //out should be the position for my objects
            }
        }

intersectRayTriangles方法的输出恰好是地板下方的初始位置。但这一点离地板不远。如何获得正确的交点?

我终于找到了一个可行的(半最优)解决方案。 landscape 是一个 ModelInstance,用 Blender 创建。

ArrayList<Vector3> vertices = new ArrayList<>();

landscape.calculateTransforms();
    Renderable rend = new Renderable();
    Mesh mesh = landscape.getRenderable(rend).meshPart.mesh;

    int vertexSize = mesh.getVertexSize() / 4;
    float[] verts = new float[mesh.getNumVertices() * vertexSize];
    short[] inds = new short[mesh.getNumIndices()];
    mesh.getVertices(verts);
    mesh.getIndices(inds);

    for (int i = 0; i < inds.length; i++) {
        int i1 = inds[i] * vertexSize;
        Vector3 v = new Vector3(verts[i1], verts[i1 + 1], verts[i1 + 2]);

        v.set(v.prj(rend.worldTransform));


        vertices.add(v);
    }

    Vector3 dir = new Vector3(0, 10, 0);
    Vector3 pos = new Vector3(random.nextFloat(),random.nextFloat(),random.nextFloat());
    Ray ray = new Ray(pos, dir.cpy());

        for (int i = 0; i < vertices.size() - 3; i+=3){
            if (Intersector.intersectRayTriangle(ray, vertices.get(i), vertices.get(i + 1), vertices.get(i + 2), pos)) {
                //pos now contains the correct coordinates
                break;
            }
        }

请注意 y-Axis 朝上